2001-07-10 14:53:08 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
/*
|
2001-09-02 12:47:00 +02:00
|
|
|
Delete of records and truncate of tables.
|
2001-11-22 16:55:18 +01:00
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
Multi-table deletes were introduced by Monty and Sinisa
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sql_select.h"
|
2004-09-07 14:29:46 +02:00
|
|
|
#include "sp_head.h"
|
|
|
|
#include "sql_trigger.h"
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
|
2005-08-30 11:39:20 +02:00
|
|
|
SQL_LIST *order, ha_rows limit, ulonglong options,
|
|
|
|
bool reset_auto_increment)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-05-07 14:06:07 +02:00
|
|
|
bool will_batch;
|
|
|
|
int error, loc_error;
|
2000-07-31 21:29:14 +02:00
|
|
|
TABLE *table;
|
2001-09-02 12:47:00 +02:00
|
|
|
SQL_SELECT *select=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
READ_RECORD info;
|
2005-01-27 22:38:56 +01:00
|
|
|
bool using_limit=limit != HA_POS_ERROR;
|
|
|
|
bool transactional_table, safe_update, const_cond;
|
2006-03-29 13:27:36 +02:00
|
|
|
ha_rows deleted= 0;
|
2005-09-30 13:21:37 +02:00
|
|
|
uint usable_index= MAX_KEY;
|
2004-11-11 20:16:46 +01:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("mysql_delete");
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (open_and_lock_tables(thd, table_list))
|
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-15 22:42:56 +02:00
|
|
|
if (!(table= table_list->table))
|
|
|
|
{
|
|
|
|
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
2005-05-20 15:14:35 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-15 22:42:56 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="init";
|
|
|
|
table->map=1;
|
2004-04-10 00:14:32 +02:00
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (mysql_prepare_delete(thd, table_list, &conds))
|
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2007-02-23 17:49:41 +01:00
|
|
|
/* check ORDER BY even if it can be ignored */
|
|
|
|
if (order && order->elements)
|
|
|
|
{
|
|
|
|
TABLE_LIST tables;
|
|
|
|
List<Item> fields;
|
|
|
|
List<Item> all_fields;
|
|
|
|
|
|
|
|
bzero((char*) &tables,sizeof(tables));
|
|
|
|
tables.table = table;
|
|
|
|
tables.alias = table_list->alias;
|
|
|
|
|
|
|
|
if (select_lex->setup_ref_array(thd, order->elements) ||
|
|
|
|
setup_order(thd, select_lex->ref_pointer_array, &tables,
|
|
|
|
fields, all_fields, (ORDER*) order->first))
|
|
|
|
{
|
|
|
|
delete select;
|
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-20 20:44:32 +01:00
|
|
|
const_cond= (!conds || conds->const_item());
|
|
|
|
safe_update=test(thd->options & OPTION_SAFE_UPDATES);
|
|
|
|
if (safe_update && const_cond)
|
|
|
|
{
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
|
|
|
|
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2002-11-20 20:44:32 +01:00
|
|
|
}
|
|
|
|
|
2005-01-03 22:04:52 +01:00
|
|
|
select_lex->no_error= thd->lex->ignore;
|
2003-11-17 21:45:07 +01:00
|
|
|
|
2004-11-12 15:04:07 +01:00
|
|
|
/*
|
|
|
|
Test if the user wants to delete all rows and deletion doesn't have
|
|
|
|
any side-effects (because of triggers), so we can use optimized
|
|
|
|
handler::delete_all_rows() method.
|
2007-06-15 13:50:56 +02:00
|
|
|
|
|
|
|
We implement fast TRUNCATE for InnoDB even if triggers are
|
|
|
|
present. TRUNCATE ignores triggers.
|
|
|
|
|
|
|
|
We can use delete_all_rows() if and only if:
|
|
|
|
- We allow new functions (not using option --skip-new), and are
|
|
|
|
not in safe mode (not using option --safe-mode)
|
|
|
|
- There is no limit clause
|
|
|
|
- The condition is constant
|
|
|
|
- If there is a condition, then it it produces a non-zero value
|
|
|
|
- If the current command is DELETE FROM with no where clause
|
|
|
|
(i.e., not TRUNCATE) then:
|
|
|
|
- We should not be binlogging this statement row-based, and
|
|
|
|
- there should be no delete triggers associated with the table.
|
2004-11-12 15:04:07 +01:00
|
|
|
*/
|
2003-05-19 15:35:49 +02:00
|
|
|
if (!using_limit && const_cond && (!conds || conds->val_int()) &&
|
2004-11-12 15:04:07 +01:00
|
|
|
!(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) &&
|
2006-11-21 09:11:43 +01:00
|
|
|
(thd->lex->sql_command == SQLCOM_TRUNCATE ||
|
2007-06-15 13:50:56 +02:00
|
|
|
(!thd->current_stmt_binlog_row_based &&
|
|
|
|
!(table->triggers && table->triggers->has_delete_triggers()))))
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
/* Update the table->file->stats.records number */
|
2006-02-06 16:33:39 +01:00
|
|
|
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
ha_rows const maybe_deleted= table->file->stats.records;
|
2006-06-01 11:53:27 +02:00
|
|
|
DBUG_PRINT("debug", ("Trying to use delete_all_rows()"));
|
2001-09-02 12:47:00 +02:00
|
|
|
if (!(error=table->file->delete_all_rows()))
|
|
|
|
{
|
|
|
|
error= -1; // ok
|
2005-12-22 06:39:02 +01:00
|
|
|
deleted= maybe_deleted;
|
2001-09-02 12:47:00 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (error != HA_ERR_WRONG_COMMAND)
|
|
|
|
{
|
|
|
|
table->file->print_error(error,MYF(0));
|
|
|
|
error=0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* Handler didn't support fast delete; Delete rows one by one */
|
|
|
|
}
|
2006-07-23 12:25:30 +02:00
|
|
|
if (conds)
|
|
|
|
{
|
|
|
|
Item::cond_result result;
|
|
|
|
conds= remove_eq_conds(thd, conds, &result);
|
|
|
|
if (result == Item::COND_FALSE) // Impossible where
|
|
|
|
limit= 0;
|
|
|
|
}
|
2001-09-02 12:47:00 +02:00
|
|
|
|
2006-01-26 18:17:17 +01:00
|
|
|
#ifdef WITH_PARTITION_STORAGE_ENGINE
|
|
|
|
if (prune_partitions(thd, table, conds))
|
|
|
|
{
|
|
|
|
free_underlaid_joins(thd, select_lex);
|
|
|
|
thd->row_count_func= 0;
|
|
|
|
send_ok(thd); // No matching records
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
#endif
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
/* Update the table->file->stats.records number */
|
2006-02-06 16:33:39 +01:00
|
|
|
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
|
2006-01-26 18:17:17 +01:00
|
|
|
|
2007-03-05 18:08:41 +01:00
|
|
|
table->covering_keys.clear_all();
|
2003-10-11 13:06:55 +02:00
|
|
|
table->quick_keys.clear_all(); // Can't use 'only index'
|
2005-03-16 15:11:01 +01:00
|
|
|
select=make_select(table, 0, 0, conds, 0, &error);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (error)
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2003-10-13 14:50:30 +02:00
|
|
|
if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= 0;
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd,0L);
|
2005-08-17 10:00:20 +02:00
|
|
|
/*
|
|
|
|
We don't need to call reset_auto_increment in this case, because
|
|
|
|
mysql_truncate always gives a NULL conds argument, hence we never
|
|
|
|
get here.
|
|
|
|
*/
|
2001-07-16 02:04:30 +02:00
|
|
|
DBUG_RETURN(0); // Nothing to delete
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If running in safe sql mode, don't allow updates without keys */
|
2003-10-11 13:06:55 +02:00
|
|
|
if (table->quick_keys.is_clear_all())
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-12-06 23:21:09 +01:00
|
|
|
thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
|
2002-11-15 15:37:44 +01:00
|
|
|
if (safe_update && !using_limit)
|
2000-11-20 01:57:02 +01:00
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
|
|
|
|
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-11-20 01:57:02 +01:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2000-09-20 03:54:10 +02:00
|
|
|
if (options & OPTION_QUICK)
|
2001-04-11 13:04:03 +02:00
|
|
|
(void) table->file->extra(HA_EXTRA_QUICK);
|
|
|
|
|
2003-12-19 19:16:26 +01:00
|
|
|
if (order && order->elements)
|
2001-04-11 13:04:03 +02:00
|
|
|
{
|
2007-01-19 16:34:09 +01:00
|
|
|
uint length= 0;
|
2001-04-11 13:04:03 +02:00
|
|
|
SORT_FIELD *sortorder;
|
2001-05-11 01:08:29 +02:00
|
|
|
ha_rows examined_rows;
|
2005-09-30 13:21:37 +02:00
|
|
|
|
2007-01-11 14:05:03 +01:00
|
|
|
if ((!select || table->quick_keys.is_clear_all()) && limit != HA_POS_ERROR)
|
2005-09-30 13:21:37 +02:00
|
|
|
usable_index= get_index_for_order(table, (ORDER*)(order->first), limit);
|
|
|
|
|
|
|
|
if (usable_index == MAX_KEY)
|
|
|
|
{
|
|
|
|
table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE),
|
|
|
|
MYF(MY_FAE | MY_ZEROFILL));
|
|
|
|
|
2005-10-27 14:15:01 +02:00
|
|
|
if (!(sortorder= make_unireg_sortorder((ORDER*) order->first,
|
2006-10-17 15:20:26 +02:00
|
|
|
&length, NULL)) ||
|
2003-04-26 14:58:39 +02:00
|
|
|
(table->sort.found_records = filesort(thd, table, sortorder, length,
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
select, HA_POS_ERROR, 1,
|
2005-10-27 14:15:01 +02:00
|
|
|
&examined_rows))
|
2003-04-09 17:22:17 +02:00
|
|
|
== HA_POS_ERROR)
|
2005-09-30 13:21:37 +02:00
|
|
|
{
|
|
|
|
delete select;
|
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
2005-10-27 14:15:01 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2005-09-30 13:21:37 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Filesort has already found and selected the rows we want to delete,
|
|
|
|
so we don't need the where clause
|
|
|
|
*/
|
2001-04-11 13:04:03 +02:00
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2005-09-30 13:21:37 +02:00
|
|
|
select= 0;
|
2001-04-11 13:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-15 21:11:58 +01:00
|
|
|
/* If quick select is used, initialize it before retrieving rows. */
|
|
|
|
if (select && select->quick && select->quick->reset())
|
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-03-15 21:11:58 +01:00
|
|
|
}
|
2005-09-30 13:21:37 +02:00
|
|
|
if (usable_index==MAX_KEY)
|
|
|
|
init_read_record(&info,thd,table,select,1,1);
|
|
|
|
else
|
|
|
|
init_read_record_idx(&info, thd, table, 1, usable_index);
|
|
|
|
|
2004-11-11 20:16:46 +01:00
|
|
|
init_ftfuncs(thd, select_lex, 1);
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="updating";
|
2007-04-04 13:21:49 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->has_triggers(TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER))
|
2007-04-04 12:50:39 +02:00
|
|
|
{
|
2007-04-04 13:21:49 +02:00
|
|
|
/*
|
|
|
|
The table has AFTER DELETE triggers that might access to subject table
|
|
|
|
and therefore might need delete to be done immediately. So we turn-off
|
|
|
|
the batching.
|
|
|
|
*/
|
|
|
|
(void) table->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
|
|
|
|
will_batch= FALSE;
|
2007-04-04 12:50:39 +02:00
|
|
|
}
|
2007-04-04 13:21:49 +02:00
|
|
|
else
|
|
|
|
will_batch= !table->file->start_bulk_delete();
|
2006-01-05 10:52:58 +01:00
|
|
|
|
|
|
|
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
table->mark_columns_needed_for_delete();
|
|
|
|
|
2002-11-30 23:11:22 +01:00
|
|
|
while (!(error=info.read_record(&info)) && !thd->killed &&
|
|
|
|
!thd->net.report_error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-11-30 23:11:22 +01:00
|
|
|
// thd->net.report_error is tested to disallow delete row on error
|
2004-02-02 17:25:39 +01:00
|
|
|
if (!(select && select->skip_record())&& !thd->net.report_error )
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-09-07 14:29:46 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
{
|
|
|
|
error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-09-07 14:29:46 +02:00
|
|
|
|
2005-12-22 06:39:02 +01:00
|
|
|
if (!(error= table->file->ha_delete_row(table->record[0])))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
{
|
|
|
|
error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!--limit && using_limit)
|
|
|
|
{
|
|
|
|
error= -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table->file->print_error(error,MYF(0));
|
2003-07-08 23:55:07 +02:00
|
|
|
/*
|
|
|
|
In < 4.0.14 we set the error number to 0 here, but that
|
|
|
|
was not sensible, because then MySQL would not roll back the
|
|
|
|
failed DELETE, and also wrote it to the binlog. For MyISAM
|
|
|
|
tables a DELETE probably never should fail (?), but for
|
|
|
|
InnoDB it can fail in a FOREIGN KEY error or an
|
|
|
|
out-of-tablespace error.
|
|
|
|
*/
|
|
|
|
error= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-08-21 19:06:00 +02:00
|
|
|
else
|
|
|
|
table->file->unlock_row(); // Row failed selection, release lock on it
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-03-04 17:16:10 +01:00
|
|
|
if (thd->killed && !error)
|
|
|
|
error= 1; // Aborted
|
2005-05-07 14:06:07 +02:00
|
|
|
if (will_batch && (loc_error= table->file->end_bulk_delete()))
|
|
|
|
{
|
|
|
|
if (error != 1)
|
|
|
|
table->file->print_error(loc_error,MYF(0));
|
|
|
|
error=1;
|
|
|
|
}
|
2005-05-09 10:30:25 +02:00
|
|
|
thd->proc_info= "end";
|
2000-07-31 21:29:14 +02:00
|
|
|
end_read_record(&info);
|
2000-09-20 03:54:10 +02:00
|
|
|
if (options & OPTION_QUICK)
|
2001-04-11 13:04:03 +02:00
|
|
|
(void) table->file->extra(HA_EXTRA_NORMAL);
|
2001-09-02 12:47:00 +02:00
|
|
|
|
2005-08-30 11:39:20 +02:00
|
|
|
if (reset_auto_increment && (error < 0))
|
2005-08-17 10:00:20 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We're really doing a truncate and need to reset the table's
|
|
|
|
auto-increment counter.
|
|
|
|
*/
|
2005-08-30 11:39:20 +02:00
|
|
|
int error2= table->file->reset_auto_increment(0);
|
2005-08-17 10:00:20 +02:00
|
|
|
|
|
|
|
if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
|
|
|
|
{
|
|
|
|
table->file->print_error(error2, MYF(0));
|
2005-08-30 11:39:20 +02:00
|
|
|
error= 1;
|
2005-08-17 10:00:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
cleanup:
|
2003-05-26 18:10:43 +02:00
|
|
|
/*
|
|
|
|
Invalidate the table in the query cache if something changed. This must
|
|
|
|
be before binlog writing and ha_autocommit_...
|
|
|
|
*/
|
|
|
|
if (deleted)
|
|
|
|
{
|
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
|
|
|
}
|
|
|
|
|
2004-06-23 12:29:05 +02:00
|
|
|
delete select;
|
2002-11-07 03:02:37 +01:00
|
|
|
transactional_table= table->file->has_transactions();
|
2006-01-05 10:52:58 +01:00
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
/* See similar binlogging code in sql_update.cc, for comments */
|
|
|
|
if ((error < 0) || (deleted && !transactional_table))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2000-09-16 03:27:21 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-09-20 17:41:47 +02:00
|
|
|
if (error < 0)
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
|
|
|
|
/*
|
2006-03-15 11:21:36 +01:00
|
|
|
[binlog]: If 'handler::delete_all_rows()' was called and the
|
|
|
|
storage engine does not inject the rows itself, we replicate
|
|
|
|
statement-based; otherwise, 'ha_delete_row()' was used to
|
|
|
|
delete specific rows which we might log row-based.
|
2005-12-22 06:39:02 +01:00
|
|
|
*/
|
2006-06-01 11:53:27 +02:00
|
|
|
int log_result= thd->binlog_query(THD::ROW_QUERY_TYPE,
|
2005-12-22 06:39:02 +01:00
|
|
|
thd->query, thd->query_length,
|
|
|
|
transactional_table, FALSE);
|
|
|
|
|
|
|
|
if (log_result && transactional_table)
|
|
|
|
{
|
2000-12-07 13:08:48 +01:00
|
|
|
error=1;
|
2005-12-22 06:39:02 +01:00
|
|
|
}
|
2000-09-16 03:27:21 +02:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
if (!transactional_table)
|
2007-03-23 16:12:58 +01:00
|
|
|
thd->no_trans_update.all= TRUE;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-12-12 19:11:56 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2002-11-16 19:19:10 +01:00
|
|
|
if (transactional_table)
|
|
|
|
{
|
|
|
|
if (ha_autocommit_or_rollback(thd,error >= 0))
|
|
|
|
error=1;
|
|
|
|
}
|
2002-11-21 23:33:15 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (thd->lock)
|
|
|
|
{
|
|
|
|
mysql_unlock_tables(thd, thd->lock);
|
|
|
|
thd->lock=0;
|
|
|
|
}
|
2006-10-25 18:00:51 +02:00
|
|
|
if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= deleted;
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd,deleted);
|
2006-11-20 21:42:06 +01:00
|
|
|
DBUG_PRINT("info",("%ld records deleted",(long) deleted));
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-11-12 13:34:00 +01:00
|
|
|
DBUG_RETURN(error >= 0 || thd->net.report_error);
|
2004-04-10 00:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Prepare items in DELETE statement
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_prepare_delete()
|
|
|
|
thd - thread handler
|
2004-07-16 00:15:55 +02:00
|
|
|
table_list - global/local table list
|
2004-04-10 00:14:32 +02:00
|
|
|
conds - conditions
|
|
|
|
|
|
|
|
RETURN VALUE
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE error
|
2004-04-10 00:14:32 +02:00
|
|
|
*/
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds)
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2006-09-16 18:50:48 +02:00
|
|
|
Item *fake_conds= 0;
|
2004-05-20 01:02:49 +02:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
2004-04-12 02:26:32 +02:00
|
|
|
DBUG_ENTER("mysql_prepare_delete");
|
2007-02-21 21:00:32 +01:00
|
|
|
List<Item> all_fields;
|
2004-04-10 00:14:32 +02:00
|
|
|
|
2005-10-15 23:32:37 +02:00
|
|
|
thd->lex->allow_sum_func= 0;
|
2006-05-26 10:47:53 +02:00
|
|
|
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
|
|
|
|
&thd->lex->select_lex.top_join_list,
|
2006-06-04 20:05:22 +02:00
|
|
|
table_list,
|
2006-05-26 10:47:53 +02:00
|
|
|
&select_lex->leaf_tables, FALSE,
|
2006-08-15 19:45:24 +02:00
|
|
|
DELETE_ACL, SELECT_ACL) ||
|
2004-09-14 18:28:29 +02:00
|
|
|
setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
|
2004-05-20 01:02:49 +02:00
|
|
|
setup_ftfuncs(select_lex))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
if (!table_list->updatable || check_key_in_view(thd, table_list))
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2007-04-17 12:32:01 +02:00
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "DELETE");
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-05-20 01:02:49 +02:00
|
|
|
{
|
2005-08-02 21:54:49 +02:00
|
|
|
TABLE_LIST *duplicate;
|
2007-03-01 22:09:22 +01:00
|
|
|
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
|
2005-08-02 21:54:49 +02:00
|
|
|
{
|
2007-04-17 12:32:01 +02:00
|
|
|
update_non_unique_table_error(table_list, "DELETE", duplicate);
|
2005-08-02 21:54:49 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2004-05-20 01:02:49 +02:00
|
|
|
}
|
2007-02-21 21:00:32 +01:00
|
|
|
|
|
|
|
if (select_lex->inner_refs_list.elements &&
|
|
|
|
fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
2006-09-16 18:50:48 +02:00
|
|
|
select_lex->fix_prepare_information(thd, conds, &fake_conds);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
/***************************************************************************
|
2002-06-11 10:20:31 +02:00
|
|
|
Delete multiple tables from join
|
2001-06-03 16:07:26 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2002-06-28 18:30:09 +02:00
|
|
|
#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2004-05-12 23:38:40 +02:00
|
|
|
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
|
2001-06-07 13:10:58 +02:00
|
|
|
{
|
2004-05-12 23:38:40 +02:00
|
|
|
handler *file= (handler*)arg;
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 11:59:39 +02:00
|
|
|
return file->cmp_ref((const uchar*)a, (const uchar*)b);
|
2001-06-07 13:10:58 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
/*
|
|
|
|
make delete specific preparation and checks after opening tables
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_multi_delete_prepare()
|
|
|
|
thd thread handler
|
|
|
|
|
|
|
|
RETURN
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2004-07-16 00:15:55 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_multi_delete_prepare(THD *thd)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
|
|
|
LEX *lex= thd->lex;
|
2006-07-04 00:07:41 +02:00
|
|
|
TABLE_LIST *aux_tables= (TABLE_LIST *)lex->auxiliary_table_list.first;
|
2004-07-16 00:15:55 +02:00
|
|
|
TABLE_LIST *target_tbl;
|
|
|
|
DBUG_ENTER("mysql_multi_delete_prepare");
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup_tables() need for VIEWs. JOIN::prepare() will not do it second
|
|
|
|
time.
|
|
|
|
|
|
|
|
lex->query_tables also point on local list of DELETE SELECT_LEX
|
|
|
|
*/
|
2006-05-26 10:47:53 +02:00
|
|
|
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
|
|
|
|
&thd->lex->select_lex.top_join_list,
|
2006-06-04 20:05:22 +02:00
|
|
|
lex->query_tables,
|
2006-05-26 10:47:53 +02:00
|
|
|
&lex->select_lex.leaf_tables, FALSE,
|
2006-08-15 19:45:24 +02:00
|
|
|
DELETE_ACL, SELECT_ACL))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
|
2005-03-28 14:13:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Multi-delete can't be constructed over-union => we always have
|
|
|
|
single SELECT on top and have to check underlying SELECTs of it
|
|
|
|
*/
|
|
|
|
lex->select_lex.exclude_from_table_unique_test= TRUE;
|
2004-07-16 00:15:55 +02:00
|
|
|
/* Fix tables-to-be-deleted-from list to point at opened tables */
|
|
|
|
for (target_tbl= (TABLE_LIST*) aux_tables;
|
|
|
|
target_tbl;
|
|
|
|
target_tbl= target_tbl->next_local)
|
|
|
|
{
|
2005-03-28 14:13:31 +02:00
|
|
|
if (!(target_tbl->table= target_tbl->correspondent_table->table))
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(target_tbl->correspondent_table->view &&
|
2005-10-27 23:18:23 +02:00
|
|
|
target_tbl->correspondent_table->merge_underlying_list &&
|
|
|
|
target_tbl->correspondent_table->merge_underlying_list->
|
|
|
|
next_local);
|
2005-03-28 14:13:31 +02:00
|
|
|
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
|
|
|
|
target_tbl->correspondent_table->view_db.str,
|
|
|
|
target_tbl->correspondent_table->view_name.str);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
if (!target_tbl->correspondent_table->updatable ||
|
|
|
|
check_key_in_view(thd, target_tbl->correspondent_table))
|
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
|
2005-01-06 12:00:13 +01:00
|
|
|
target_tbl->table_name, "DELETE");
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
/*
|
2005-03-28 14:13:31 +02:00
|
|
|
Check that table from which we delete is not used somewhere
|
|
|
|
inside subqueries/view.
|
2004-07-16 00:15:55 +02:00
|
|
|
*/
|
|
|
|
{
|
2005-08-02 21:54:49 +02:00
|
|
|
TABLE_LIST *duplicate;
|
2005-12-22 13:48:00 +01:00
|
|
|
if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
|
2007-03-01 22:09:22 +01:00
|
|
|
lex->query_tables, 0)))
|
2005-08-02 21:54:49 +02:00
|
|
|
{
|
|
|
|
update_non_unique_table_error(target_tbl->correspondent_table,
|
|
|
|
"DELETE", duplicate);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 22:23:56 +02:00
|
|
|
multi_delete::multi_delete(TABLE_LIST *dt, uint num_of_tables_arg)
|
|
|
|
: delete_tables(dt), deleted(0), found(0),
|
2002-11-16 19:19:10 +01:00
|
|
|
num_of_tables(num_of_tables_arg), error(0),
|
2005-01-27 22:38:56 +01:00
|
|
|
do_delete(0), transactional_tables(0), normal_tables(0)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
tempfiles= (Unique **) sql_calloc(sizeof(Unique *) * num_of_tables);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2002-05-08 22:14:40 +02:00
|
|
|
multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("multi_delete::prepare");
|
2002-05-08 22:14:40 +02:00
|
|
|
unit= u;
|
2002-11-29 15:40:18 +01:00
|
|
|
do_delete= 1;
|
2001-06-15 04:03:15 +02:00
|
|
|
thd->proc_info="deleting from main table";
|
2001-06-03 16:07:26 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2001-07-01 12:20:53 +02:00
|
|
|
|
2002-11-29 15:40:18 +01:00
|
|
|
bool
|
2001-07-01 12:20:53 +02:00
|
|
|
multi_delete::initialize_tables(JOIN *join)
|
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
TABLE_LIST *walk;
|
2002-11-29 15:40:18 +01:00
|
|
|
Unique **tempfiles_ptr;
|
|
|
|
DBUG_ENTER("initialize_tables");
|
|
|
|
|
|
|
|
if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
table_map tables_to_delete_from=0;
|
2004-07-16 00:15:55 +02:00
|
|
|
for (walk= delete_tables; walk; walk= walk->next_local)
|
2001-07-10 14:53:08 +02:00
|
|
|
tables_to_delete_from|= walk->table->map;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
walk= delete_tables;
|
2005-05-30 19:48:40 +02:00
|
|
|
delete_while_scanning= 1;
|
2001-07-01 12:20:53 +02:00
|
|
|
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
|
|
|
|
tab < end;
|
|
|
|
tab++)
|
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
if (tab->table->map & tables_to_delete_from)
|
2001-07-01 12:20:53 +02:00
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
/* We are going to delete from this table */
|
2002-06-11 21:45:51 +02:00
|
|
|
TABLE *tbl=walk->table=tab->table;
|
2004-07-16 00:15:55 +02:00
|
|
|
walk= walk->next_local;
|
2002-06-12 22:54:52 +02:00
|
|
|
/* Don't use KEYREAD optimization on this table */
|
2002-06-11 21:45:51 +02:00
|
|
|
tbl->no_keyread=1;
|
2004-09-19 13:15:01 +02:00
|
|
|
/* Don't use record cache */
|
|
|
|
tbl->no_cache= 1;
|
2007-03-05 18:08:41 +01:00
|
|
|
tbl->covering_keys.clear_all();
|
2002-11-07 03:02:37 +01:00
|
|
|
if (tbl->file->has_transactions())
|
2005-01-27 22:38:56 +01:00
|
|
|
transactional_tables= 1;
|
2002-11-07 03:02:37 +01:00
|
|
|
else
|
|
|
|
normal_tables= 1;
|
2007-04-04 13:21:49 +02:00
|
|
|
if (tbl->triggers &&
|
|
|
|
tbl->triggers->has_triggers(TRG_EVENT_DELETE,
|
2007-04-04 12:50:39 +02:00
|
|
|
TRG_ACTION_AFTER))
|
2007-04-04 13:21:49 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
The table has AFTER DELETE triggers that might access to subject
|
|
|
|
table and therefore might need delete to be done immediately.
|
|
|
|
So we turn-off the batching.
|
|
|
|
*/
|
|
|
|
(void) tbl->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
|
2007-04-04 12:50:39 +02:00
|
|
|
}
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
tbl->prepare_for_position();
|
|
|
|
tbl->mark_columns_needed_for_delete();
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2005-05-30 19:48:40 +02:00
|
|
|
else if ((tab->type != JT_SYSTEM && tab->type != JT_CONST) &&
|
|
|
|
walk == delete_tables)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We are not deleting from the table we are scanning. In this
|
|
|
|
case send_data() shouldn't delete any rows a we may touch
|
|
|
|
the rows in the deleted table many times
|
|
|
|
*/
|
|
|
|
delete_while_scanning= 0;
|
|
|
|
}
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2002-07-31 18:53:06 +02:00
|
|
|
walk= delete_tables;
|
2002-11-29 15:40:18 +01:00
|
|
|
tempfiles_ptr= tempfiles;
|
2005-05-30 19:48:40 +02:00
|
|
|
if (delete_while_scanning)
|
|
|
|
{
|
|
|
|
table_being_deleted= delete_tables;
|
|
|
|
walk= walk->next_local;
|
|
|
|
}
|
|
|
|
for (;walk ;walk= walk->next_local)
|
2002-07-31 18:53:06 +02:00
|
|
|
{
|
|
|
|
TABLE *table=walk->table;
|
2004-05-12 23:38:40 +02:00
|
|
|
*tempfiles_ptr++= new Unique (refpos_order_cmp,
|
|
|
|
(void *) table->file,
|
2002-11-29 15:40:18 +01:00
|
|
|
table->file->ref_length,
|
|
|
|
MEM_STRIP_BUF_SIZE);
|
2002-07-31 18:53:06 +02:00
|
|
|
}
|
2003-08-26 11:51:09 +02:00
|
|
|
init_ftfuncs(thd, thd->lex->current_select, 1);
|
2003-01-30 21:15:44 +01:00
|
|
|
DBUG_RETURN(thd->is_fatal_error != 0);
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
multi_delete::~multi_delete()
|
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
for (table_being_deleted= delete_tables;
|
|
|
|
table_being_deleted;
|
|
|
|
table_being_deleted= table_being_deleted->next_local)
|
2002-01-12 18:51:10 +01:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
TABLE *table= table_being_deleted->table;
|
|
|
|
table->no_keyread=0;
|
2002-01-12 18:51:10 +01:00
|
|
|
}
|
2001-07-10 14:53:08 +02:00
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
for (uint counter= 0; counter < num_of_tables; counter++)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
2001-06-03 16:07:26 +02:00
|
|
|
if (tempfiles[counter])
|
2001-06-15 04:03:15 +02:00
|
|
|
delete tempfiles[counter];
|
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
bool multi_delete::send_data(List<Item> &values)
|
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
int secure_counter= delete_while_scanning ? -1 : 0;
|
|
|
|
TABLE_LIST *del_table;
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_ENTER("multi_delete::send_data");
|
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
for (del_table= delete_tables;
|
|
|
|
del_table;
|
|
|
|
del_table= del_table->next_local, secure_counter++)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
TABLE *table= del_table->table;
|
2001-06-15 04:03:15 +02:00
|
|
|
|
|
|
|
/* Check if we are using outer join and we didn't find the row */
|
|
|
|
if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
table->file->position(table->record[0]);
|
2003-12-12 22:14:59 +01:00
|
|
|
found++;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
if (secure_counter < 0)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
/* We are scanning the current table */
|
|
|
|
DBUG_ASSERT(del_table == table_being_deleted);
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
table->status|= STATUS_DELETED;
|
2005-12-22 06:39:02 +01:00
|
|
|
if (!(error=table->file->ha_delete_row(table->record[0])))
|
2005-05-24 20:19:33 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-05-30 19:48:40 +02:00
|
|
|
else
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
table->file->print_error(error,MYF(0));
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-15 15:22:34 +02:00
|
|
|
error=tempfiles[secure_counter]->unique_add((char*) table->file->ref);
|
2001-06-15 04:03:15 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
error= 1; // Fatal error
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(0);
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 17:16:10 +01:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
void multi_delete::send_error(uint errcode,const char *err)
|
|
|
|
{
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_ENTER("multi_delete::send_error");
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/* First send error what ever it is ... */
|
2004-10-20 03:04:37 +02:00
|
|
|
my_message(errcode, err, MYF(0));
|
2001-10-04 20:52:41 +02:00
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/* If nothing deleted return */
|
|
|
|
if (!deleted)
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_VOID_RETURN;
|
2002-01-16 22:02:26 +01:00
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
/* Something already deleted so we have to invalidate cache */
|
2002-04-28 23:33:52 +02:00
|
|
|
query_cache_invalidate3(thd, delete_tables, 1);
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/*
|
2002-01-16 22:02:26 +01:00
|
|
|
If rows from the first table only has been deleted and it is
|
|
|
|
transactional, just do rollback.
|
2001-06-15 04:03:15 +02:00
|
|
|
The same if all tables are transactional, regardless of where we are.
|
|
|
|
In all other cases do attempt deletes ...
|
|
|
|
*/
|
2005-05-30 19:48:40 +02:00
|
|
|
if ((table_being_deleted == delete_tables &&
|
|
|
|
table_being_deleted->table->file->has_transactions()) ||
|
|
|
|
!normal_tables)
|
2001-10-19 16:43:30 +02:00
|
|
|
ha_rollback_stmt(thd);
|
2001-06-03 16:07:26 +02:00
|
|
|
else if (do_delete)
|
2002-01-29 17:32:16 +01:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We have to execute the recorded do_deletes() and write info into the
|
|
|
|
error log
|
|
|
|
*/
|
|
|
|
error= 1;
|
|
|
|
send_eof();
|
2002-01-29 17:32:16 +01:00
|
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-12-13 01:31:19 +01:00
|
|
|
/*
|
|
|
|
Do delete from other tables.
|
|
|
|
Returns values:
|
|
|
|
0 ok
|
|
|
|
1 error
|
|
|
|
*/
|
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
int multi_delete::do_deletes()
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-07 14:06:07 +02:00
|
|
|
int local_error= 0, counter= 0, error;
|
|
|
|
bool will_batch;
|
2003-02-17 01:14:37 +01:00
|
|
|
DBUG_ENTER("do_deletes");
|
2005-05-30 19:48:40 +02:00
|
|
|
DBUG_ASSERT(do_delete);
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
do_delete= 0; // Mark called
|
2003-12-12 22:14:59 +01:00
|
|
|
if (!found)
|
2003-12-11 23:55:48 +01:00
|
|
|
DBUG_RETURN(0);
|
2005-05-30 19:48:40 +02:00
|
|
|
|
|
|
|
table_being_deleted= (delete_while_scanning ? delete_tables->next_local :
|
|
|
|
delete_tables);
|
|
|
|
|
|
|
|
for (; table_being_deleted;
|
2004-07-16 00:15:55 +02:00
|
|
|
table_being_deleted= table_being_deleted->next_local, counter++)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
TABLE *table = table_being_deleted->table;
|
|
|
|
if (tempfiles[counter]->get(table))
|
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
local_error=1;
|
2001-06-15 04:03:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
READ_RECORD info;
|
2003-02-17 01:14:37 +01:00
|
|
|
init_read_record(&info,thd,table,NULL,0,1);
|
|
|
|
/*
|
|
|
|
Ignore any rows not found in reference tables as they may already have
|
|
|
|
been deleted by foreign key handling
|
|
|
|
*/
|
|
|
|
info.ignore_not_found_rows= 1;
|
2005-05-09 10:30:25 +02:00
|
|
|
will_batch= !table->file->start_bulk_delete();
|
2002-11-07 11:49:01 +01:00
|
|
|
while (!(local_error=info.read_record(&info)) && !thd->killed)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
{
|
|
|
|
local_error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-12-22 06:39:02 +01:00
|
|
|
if ((local_error=table->file->ha_delete_row(table->record[0])))
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
table->file->print_error(local_error,MYF(0));
|
2001-06-15 04:03:15 +02:00
|
|
|
break;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2001-12-13 01:31:19 +01:00
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
{
|
|
|
|
local_error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2005-05-09 10:30:25 +02:00
|
|
|
if (will_batch && (error= table->file->end_bulk_delete()))
|
2005-05-07 14:06:07 +02:00
|
|
|
{
|
|
|
|
if (!local_error)
|
|
|
|
{
|
|
|
|
local_error= error;
|
|
|
|
table->file->print_error(local_error,MYF(0));
|
|
|
|
}
|
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
end_read_record(&info);
|
2004-03-04 17:16:10 +01:00
|
|
|
if (thd->killed && !local_error)
|
|
|
|
local_error= 1;
|
2002-11-07 02:54:00 +01:00
|
|
|
if (local_error == -1) // End of file
|
|
|
|
local_error = 0;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2003-02-17 01:14:37 +01:00
|
|
|
DBUG_RETURN(local_error);
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2002-01-16 22:02:26 +01:00
|
|
|
/*
|
2002-06-11 10:20:31 +02:00
|
|
|
Send ok to the client
|
|
|
|
|
2002-01-16 22:02:26 +01:00
|
|
|
return: 0 sucess
|
|
|
|
1 error
|
|
|
|
*/
|
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
bool multi_delete::send_eof()
|
|
|
|
{
|
2002-01-16 22:02:26 +01:00
|
|
|
thd->proc_info="deleting from reference tables";
|
2001-10-25 13:41:49 +02:00
|
|
|
|
|
|
|
/* Does deletes for the last n - 1 tables, returns 0 if ok */
|
2005-05-30 19:48:40 +02:00
|
|
|
int local_error= do_deletes(); // returns 0 if success
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
/* compute a total error to know if something failed */
|
|
|
|
local_error= local_error || error;
|
|
|
|
|
2001-10-04 20:52:41 +02:00
|
|
|
/* reset used flags */
|
2001-06-03 16:07:26 +02:00
|
|
|
thd->proc_info="end";
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2003-05-26 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We must invalidate the query cache before binlog writing and
|
|
|
|
ha_autocommit_...
|
|
|
|
*/
|
2003-05-26 18:10:43 +02:00
|
|
|
if (deleted)
|
2003-12-21 01:07:45 +01:00
|
|
|
{
|
2003-05-26 18:10:43 +02:00
|
|
|
query_cache_invalidate3(thd, delete_tables, 1);
|
2003-12-21 01:07:45 +01:00
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
if ((local_error == 0) || (deleted && normal_tables))
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-12-13 01:31:19 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-09-20 17:41:47 +02:00
|
|
|
if (local_error == 0)
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
|
|
|
|
thd->query, thd->query_length,
|
|
|
|
transactional_tables, FALSE) &&
|
|
|
|
!normal_tables)
|
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
local_error=1; // Log write failed: roll back the SQL statement
|
2005-12-22 06:39:02 +01:00
|
|
|
}
|
2001-12-13 01:31:19 +01:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
if (!transactional_tables)
|
2007-03-23 16:12:58 +01:00
|
|
|
thd->no_trans_update.all= TRUE;
|
2002-04-29 11:24:14 +02:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
/* Commit or rollback the current SQL statement */
|
2002-11-29 15:40:18 +01:00
|
|
|
if (transactional_tables)
|
|
|
|
if (ha_autocommit_or_rollback(thd,local_error > 0))
|
|
|
|
local_error=1;
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (!local_error)
|
2004-05-04 13:45:20 +02:00
|
|
|
{
|
|
|
|
thd->row_count_func= deleted;
|
2002-11-21 14:56:48 +01:00
|
|
|
::send_ok(thd, deleted);
|
2004-05-04 13:45:20 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2001-09-02 12:47:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
2002-06-11 10:20:31 +02:00
|
|
|
TRUNCATE TABLE
|
2001-09-02 12:47:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Optimize delete of all rows by doing a full generate of the table
|
|
|
|
This will work even if the .ISM and .ISD tables are destroyed
|
|
|
|
|
|
|
|
dont_send_ok should be set if:
|
|
|
|
- We should always wants to generate the table (even if the table type
|
|
|
|
normally can't safely do this.
|
|
|
|
- We don't want an ok to be sent to the end user.
|
|
|
|
- We don't want to log the truncate command
|
2001-09-03 04:16:15 +02:00
|
|
|
- If we want to have a name lock on the table on exit without errors.
|
2001-09-02 12:47:00 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
|
|
|
HA_CREATE_INFO create_info;
|
|
|
|
char path[FN_REFLEN];
|
2005-11-23 21:45:02 +01:00
|
|
|
TABLE *table;
|
2004-10-20 03:04:37 +02:00
|
|
|
bool error;
|
2006-01-19 03:56:06 +01:00
|
|
|
uint closed_log_tables= 0, lock_logger= 0;
|
2005-12-31 06:01:26 +01:00
|
|
|
uint path_length;
|
2006-10-25 03:16:11 +02:00
|
|
|
uint log_type;
|
2001-09-02 12:47:00 +02:00
|
|
|
DBUG_ENTER("mysql_truncate");
|
|
|
|
|
2004-08-23 16:15:57 +02:00
|
|
|
bzero((char*) &create_info,sizeof(create_info));
|
2001-09-02 12:47:00 +02:00
|
|
|
/* If it is a temporary table, close and regenerate it */
|
2005-11-23 21:45:02 +01:00
|
|
|
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2007-03-02 17:43:45 +01:00
|
|
|
handlerton *table_type= table->s->db_type();
|
2005-11-23 21:45:02 +01:00
|
|
|
TABLE_SHARE *share= table->s;
|
2005-10-01 01:26:48 +02:00
|
|
|
if (!ha_check_storage_engine_flag(table_type, HTON_CAN_RECREATE))
|
2005-08-29 17:01:46 +02:00
|
|
|
goto trunc_by_del;
|
2005-11-23 21:45:02 +01:00
|
|
|
|
|
|
|
table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
|
|
|
|
|
|
|
|
close_temporary_table(thd, table, 0, 0); // Don't free share
|
|
|
|
ha_create_table(thd, share->normalized_path.str,
|
|
|
|
share->db.str, share->table_name.str, &create_info, 1);
|
2001-12-02 13:34:01 +01:00
|
|
|
// We don't need to call invalidate() because this table is not in cache
|
2005-11-23 21:45:02 +01:00
|
|
|
if ((error= (int) !(open_temporary_table(thd, share->path.str,
|
|
|
|
share->db.str,
|
|
|
|
share->table_name.str, 1))))
|
2001-09-02 12:47:00 +02:00
|
|
|
(void) rm_temporary_table(table_type, path);
|
2005-11-23 21:45:02 +01:00
|
|
|
free_table_share(share);
|
|
|
|
my_free((char*) table,MYF(0));
|
2002-06-11 10:20:31 +02:00
|
|
|
/*
|
2002-08-30 11:40:40 +02:00
|
|
|
If we return here we will not have logged the truncation to the bin log
|
|
|
|
and we will not send_ok() to the client.
|
2002-04-09 02:20:24 +02:00
|
|
|
*/
|
2005-02-16 17:34:02 +01:00
|
|
|
goto end;
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
|
|
|
|
2005-12-31 06:01:26 +01:00
|
|
|
path_length= build_table_filename(path, sizeof(path), table_list->db,
|
2006-08-02 17:57:06 +02:00
|
|
|
table_list->table_name, reg_ext, 0);
|
2001-09-02 12:47:00 +02:00
|
|
|
|
|
|
|
if (!dont_send_ok)
|
|
|
|
{
|
2005-12-21 19:18:40 +01:00
|
|
|
enum legacy_db_type table_type;
|
2007-04-17 12:32:01 +02:00
|
|
|
mysql_frm_type(thd, path, &table_type);
|
2005-11-03 15:10:11 +01:00
|
|
|
if (table_type == DB_TYPE_UNKNOWN)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NO_SUCH_TABLE, MYF(0),
|
2005-01-06 12:00:13 +01:00
|
|
|
table_list->db, table_list->table_name);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2005-12-21 19:18:40 +01:00
|
|
|
if (!ha_check_storage_engine_flag(ha_resolve_by_legacy_type(thd, table_type),
|
2006-04-06 13:44:26 +02:00
|
|
|
HTON_CAN_RECREATE))
|
2005-08-29 17:01:46 +02:00
|
|
|
goto trunc_by_del;
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
if (lock_and_wait_for_table_name(thd, table_list))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
|
|
|
|
2006-10-25 03:16:11 +02:00
|
|
|
log_type= check_if_log_table(table_list->db_length, table_list->db,
|
|
|
|
table_list->table_name_length,
|
|
|
|
table_list->table_name, 1);
|
2006-01-19 03:56:06 +01:00
|
|
|
/* close log tables in use */
|
2006-10-13 15:26:46 +02:00
|
|
|
if (log_type)
|
2006-01-19 03:56:06 +01:00
|
|
|
{
|
2006-10-13 15:26:46 +02:00
|
|
|
lock_logger= 1;
|
|
|
|
logger.lock();
|
|
|
|
logger.close_log_table(log_type, FALSE);
|
|
|
|
closed_log_tables= closed_log_tables | log_type;
|
2006-01-19 03:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the .frm extension AIX 5.2 64-bit compiler bug (BUG#16155): this
|
|
|
|
// crashes, replacement works. *(path + path_length - reg_ext_length)=
|
|
|
|
// '\0';
|
2006-01-06 17:02:57 +01:00
|
|
|
path[path_length - reg_ext_length] = 0;
|
2006-06-12 14:23:21 +02:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2005-11-23 21:45:02 +01:00
|
|
|
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
|
|
|
|
&create_info, 1);
|
2006-06-12 14:23:21 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
2005-01-27 22:38:56 +01:00
|
|
|
query_cache_invalidate3(thd, table_list, 0);
|
2002-11-07 03:02:37 +01:00
|
|
|
|
2002-04-09 02:20:24 +02:00
|
|
|
end:
|
2001-09-03 04:16:15 +02:00
|
|
|
if (!dont_send_ok)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2001-09-03 04:16:15 +02:00
|
|
|
if (!error)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2001-09-03 04:16:15 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-12-22 06:39:02 +01:00
|
|
|
/*
|
|
|
|
TRUNCATE must always be statement-based binlogged (not row-based) so
|
WL#2977 and WL#2712 global and session-level variable to set the binlog format (row/statement),
and new binlog format called "mixed" (which is statement-based except if only row-based is correct,
in this cset it means if UDF or UUID is used; more cases could be added in later 5.1 release):
SET GLOBAL|SESSION BINLOG_FORMAT=row|statement|mixed|default;
the global default is statement unless cluster is enabled (then it's row) as in 5.1-alpha.
It's not possible to use SET on this variable if a session is currently in row-based mode and has open temporary tables (because CREATE
TEMPORARY TABLE was not binlogged so temp table is not known on slave), or if NDB is enabled (because
NDB does not support such change on-the-fly, though it will later), of if in a stored function (see below).
The added tests test the possibility or impossibility to SET, their effects, and the mixed mode,
including in prepared statements and in stored procedures and functions.
Caveats:
a) The mixed mode will not work for stored functions: in mixed mode, a stored function will
always be binlogged as one call and in a statement-based way (e.g. INSERT VALUES(myfunc()) or SELECT myfunc()).
b) for the same reason, changing the thread's binlog format inside a stored function is
refused with an error message.
c) the same problems apply to triggers; implementing b) for triggers will be done later (will ask
Dmitri).
Additionally, as the binlog format is now changeable by each user for his session, I remove the implication
which was done at startup, where row-based automatically set log-bin-trust-routine-creators to 1
(not possible anymore as a user can now switch to stmt-based and do nasty things again), and automatically
set --innodb-locks-unsafe-for-binlog to 1 (was anyway theoretically incorrect as it disabled
phantom protection).
Plus fixes for compiler warnings.
2006-02-25 22:21:03 +01:00
|
|
|
we don't test current_stmt_binlog_row_based.
|
2005-12-22 06:39:02 +01:00
|
|
|
*/
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
thd->binlog_query(THD::STMT_QUERY_TYPE,
|
|
|
|
thd->query, thd->query_length, FALSE, FALSE);
|
2001-09-03 04:16:15 +02:00
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd); // This should return record count
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2001-09-03 04:16:15 +02:00
|
|
|
unlock_table_name(thd, table_list);
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2006-06-19 15:30:55 +02:00
|
|
|
if (opt_slow_log && (closed_log_tables & QUERY_LOG_SLOW))
|
2006-01-20 13:23:24 +01:00
|
|
|
logger.reopen_log_table(QUERY_LOG_SLOW);
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2006-06-19 15:30:55 +02:00
|
|
|
if (opt_log && (closed_log_tables & QUERY_LOG_GENERAL))
|
2006-01-20 13:23:24 +01:00
|
|
|
logger.reopen_log_table(QUERY_LOG_GENERAL);
|
2006-01-19 03:56:06 +01:00
|
|
|
if (lock_logger)
|
|
|
|
logger.unlock();
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2001-09-03 04:16:15 +02:00
|
|
|
else if (error)
|
2002-06-04 21:59:12 +02:00
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2001-09-03 04:16:15 +02:00
|
|
|
unlock_table_name(thd, table_list);
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(error);
|
2005-08-29 17:01:46 +02:00
|
|
|
|
2005-11-23 21:45:02 +01:00
|
|
|
trunc_by_del:
|
2005-08-29 17:01:46 +02:00
|
|
|
/* Probably InnoDB table */
|
2006-11-30 02:40:42 +01:00
|
|
|
ulonglong save_options= thd->options;
|
2005-08-29 17:01:46 +02:00
|
|
|
table_list->lock_type= TL_WRITE;
|
|
|
|
thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
|
|
|
|
ha_enable_transaction(thd, FALSE);
|
2005-08-29 18:18:30 +02:00
|
|
|
mysql_init_select(thd->lex);
|
2006-07-10 18:41:03 +02:00
|
|
|
bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
|
2006-06-01 11:53:27 +02:00
|
|
|
thd->clear_current_stmt_binlog_row_based();
|
2005-08-29 17:01:46 +02:00
|
|
|
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
|
2005-08-30 12:35:37 +02:00
|
|
|
HA_POS_ERROR, LL(0), TRUE);
|
2005-08-29 17:01:46 +02:00
|
|
|
ha_enable_transaction(thd, TRUE);
|
|
|
|
thd->options= save_options;
|
2006-07-10 18:41:03 +02:00
|
|
|
thd->current_stmt_binlog_row_based= save_binlog_row_based;
|
2005-08-29 17:01:46 +02:00
|
|
|
DBUG_RETURN(error);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|