You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1994 lines
61 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
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.
20 years ago
21 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
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.
20 years ago
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.
20 years ago
21 years ago
22 years ago
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.
20 years ago
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.
20 years ago
26 years ago
23 years ago
This will be pushed only after I fix the testsuite. This is the main commit for Worklog tasks: * A more dynamic binlog format which allows small changes (1064) * Log session variables in Query_log_event (1063) Below 5.0 means 5.0.0. MySQL 5.0 is able to replicate FOREIGN_KEY_CHECKS, UNIQUE_KEY_CHECKS (for speed), SQL_AUTO_IS_NULL, SQL_MODE. Not charsets (WL#1062), not some vars (I can only think of SQL_SELECT_LIMIT, which deserves a special treatment). Note that this works for queries, except LOAD DATA INFILE (for this it would have to wait for Dmitri's push of WL#874, which in turns waits for the present push, so... the deadlock must be broken!). Note that when Dmitri pushes WL#874 in 5.0.1, 5.0.0 won't be able to replicate a LOAD DATA INFILE from 5.0.1. Apart from that, the new binlog format is designed so that it can tolerate a little variation in the events (so that a 5.0.0 slave could replicate a 5.0.1 master, except for LOAD DATA INFILE unfortunately); that is, when I later add replication of charsets it should break nothing. And when I later add a UID to every event, it should break nothing. The main change brought by this patch is a new type of event, Format_description_log_event, which describes some lengthes in other event types. This event is needed for the master/slave/mysqlbinlog to understand a 5.0 log. Thanks to this event, we can later add more bytes to the header of every event without breaking compatibility. Inside Query_log_event, we have some additional dynamic format, as every Query_log_event can have a different number of status variables, stored as pairs (code, value); that's how SQL_MODE and session variables and catalog are stored. Like this, we can later add count of affected rows, charsets... and we can have options --don't-log-count-affected-rows if we want. MySQL 5.0 is able to run on 4.x relay logs, 4.x binlogs. Upgrading a 4.x master to 5.0 is ok (no need to delete binlogs), upgrading a 4.x slave to 5.0 is ok (no need to delete relay logs); so both can be "hot" upgrades. Upgrading a 3.23 master to 5.0 requires as much as upgrading it to 4.0. 3.23 and 4.x can't be slaves of 5.0. So downgrading from 5.0 to 4.x may be complicated. Log_event::log_pos is now the position of the end of the event, which is more useful than the position of the beginning. We take care about compatibility with <5.0 (in which log_pos is the beginning). I added a short test for replication of SQL_MODE and some other variables. TODO: - after committing this, merge the latest 5.0 into it - fix all tests - update the manual with upgrade notes.
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
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.
20 years ago
26 years ago
26 years ago
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.
20 years ago
21 years ago
20 years ago
20 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
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.
20 years ago
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.
20 years ago
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.
20 years ago
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.
20 years ago
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.
20 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
26 years ago
26 years ago
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.
20 years ago
26 years ago
26 years ago
26 years ago
26 years ago
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.
20 years ago
20 years ago
26 years ago
26 years ago
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.
20 years ago
26 years ago
Fixed bug#15560: GROUP_CONCAT wasn't ready for WITH ROLLUP queries The GROUP_CONCAT uses its own temporary table. When ROLLUP is present it creates the second copy of Item_func_group_concat. This copy receives the same list of arguments that original group_concat does. When the copy is set up the result_fields of functions from the argument list are reset to the temporary table of this copy. As a result of this action data from functions flow directly to the ROLLUP copy and the original group_concat functions shows wrong result. Since queries with COUNT(DISTINCT ...) use temporary tables to store the results the COUNT function they are also affected by this bug. The idea of the fix is to copy content of the result_field for the function under GROUP_CONCAT/COUNT from the first temporary table to the second one, rather than setting result_field to point to the second temporary table. To achieve this goal force_copy_fields flag is added to Item_func_group_concat and Item_sum_count_distinct classes. This flag is initialized to 0 and set to 1 into the make_unique() member function of both classes. To the TMP_TABLE_PARAM structure is modified to include the similar flag as well. The create_tmp_table() function passes that flag to create_tmp_field(). When the flag is set the create_tmp_field() function will set result_field as a source field and will not reset that result field to newly created field for Item_func_result_field and its descendants. Due to this there will be created copy func to copy data from old result_field to newly created field.
20 years ago
21 years ago
20 years ago
21 years ago
21 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
21 years ago
26 years ago
25 years ago
21 years ago
21 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
24 years ago
24 years ago
24 years ago
24 years ago
  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* Classes in mysql */
  14. #ifdef USE_PRAGMA_INTERFACE
  15. #pragma interface /* gcc class implementation */
  16. #endif
  17. #include "log.h"
  18. #include "rpl_rli.h"
  19. #include "rpl_tblmap.h"
  20. class Query_log_event;
  21. class Load_log_event;
  22. class Slave_log_event;
  23. class sp_rcontext;
  24. class sp_cache;
  25. class Rows_log_event;
  26. enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
  27. enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
  28. enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
  29. enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON,
  30. DELAY_KEY_WRITE_ALL };
  31. enum enum_check_fields
  32. { CHECK_FIELD_IGNORE, CHECK_FIELD_WARN, CHECK_FIELD_ERROR_FOR_NULL };
  33. enum enum_mark_columns
  34. { MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE};
  35. extern char internal_table_name[2];
  36. extern const char **errmesg;
  37. #define TC_LOG_PAGE_SIZE 8192
  38. #define TC_LOG_MIN_SIZE (3*TC_LOG_PAGE_SIZE)
  39. #define TC_HEURISTIC_RECOVER_COMMIT 1
  40. #define TC_HEURISTIC_RECOVER_ROLLBACK 2
  41. extern uint tc_heuristic_recover;
  42. typedef struct st_user_var_events
  43. {
  44. user_var_entry *user_var_event;
  45. char *value;
  46. ulong length;
  47. Item_result type;
  48. uint charset_number;
  49. } BINLOG_USER_VAR_EVENT;
  50. #define RP_LOCK_LOG_IS_ALREADY_LOCKED 1
  51. #define RP_FORCE_ROTATE 2
  52. typedef struct st_copy_info {
  53. ha_rows records;
  54. ha_rows deleted;
  55. ha_rows updated;
  56. ha_rows copied;
  57. ha_rows error_count;
  58. enum enum_duplicates handle_duplicates;
  59. int escape_char, last_errno;
  60. bool ignore;
  61. /* for INSERT ... UPDATE */
  62. List<Item> *update_fields;
  63. List<Item> *update_values;
  64. /* for VIEW ... WITH CHECK OPTION */
  65. TABLE_LIST *view;
  66. } COPY_INFO;
  67. class key_part_spec :public Sql_alloc {
  68. public:
  69. const char *field_name;
  70. uint length;
  71. key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {}
  72. bool operator==(const key_part_spec& other) const;
  73. };
  74. class Alter_drop :public Sql_alloc {
  75. public:
  76. enum drop_type {KEY, COLUMN };
  77. const char *name;
  78. enum drop_type type;
  79. Alter_drop(enum drop_type par_type,const char *par_name)
  80. :name(par_name), type(par_type) {}
  81. };
  82. class Alter_column :public Sql_alloc {
  83. public:
  84. const char *name;
  85. Item *def;
  86. Alter_column(const char *par_name,Item *literal)
  87. :name(par_name), def(literal) {}
  88. };
  89. class Key :public Sql_alloc {
  90. public:
  91. enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FULLTEXT, SPATIAL, FOREIGN_KEY};
  92. enum Keytype type;
  93. KEY_CREATE_INFO key_create_info;
  94. List<key_part_spec> columns;
  95. const char *name;
  96. bool generated;
  97. Key(enum Keytype type_par, const char *name_arg,
  98. KEY_CREATE_INFO *key_info_arg,
  99. bool generated_arg, List<key_part_spec> &cols)
  100. :type(type_par), key_create_info(*key_info_arg), columns(cols),
  101. name(name_arg), generated(generated_arg)
  102. {}
  103. ~Key() {}
  104. /* Equality comparison of keys (ignoring name) */
  105. friend bool foreign_key_prefix(Key *a, Key *b);
  106. };
  107. class Table_ident;
  108. class foreign_key: public Key {
  109. public:
  110. enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
  111. FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
  112. enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
  113. FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
  114. Table_ident *ref_table;
  115. List<key_part_spec> ref_columns;
  116. uint delete_opt, update_opt, match_opt;
  117. foreign_key(const char *name_arg, List<key_part_spec> &cols,
  118. Table_ident *table, List<key_part_spec> &ref_cols,
  119. uint delete_opt_arg, uint update_opt_arg, uint match_opt_arg)
  120. :Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
  121. ref_table(table), ref_columns(cols),
  122. delete_opt(delete_opt_arg), update_opt(update_opt_arg),
  123. match_opt(match_opt_arg)
  124. {}
  125. };
  126. typedef struct st_mysql_lock
  127. {
  128. TABLE **table;
  129. uint table_count,lock_count;
  130. THR_LOCK_DATA **locks;
  131. } MYSQL_LOCK;
  132. class LEX_COLUMN : public Sql_alloc
  133. {
  134. public:
  135. String column;
  136. uint rights;
  137. LEX_COLUMN (const String& x,const uint& y ): column (x),rights (y) {}
  138. };
  139. #include "sql_lex.h" /* Must be here */
  140. class delayed_insert;
  141. class select_result;
  142. class Time_zone;
  143. #define THD_SENTRY_MAGIC 0xfeedd1ff
  144. #define THD_SENTRY_GONE 0xdeadbeef
  145. #define THD_CHECK_SENTRY(thd) DBUG_ASSERT(thd->dbug_sentry == THD_SENTRY_MAGIC)
  146. struct system_variables
  147. {
  148. ulonglong myisam_max_extra_sort_file_size;
  149. ulonglong myisam_max_sort_file_size;
  150. ha_rows select_limit;
  151. ha_rows max_join_size;
  152. ulong auto_increment_increment, auto_increment_offset;
  153. ulong bulk_insert_buff_size;
  154. ulong join_buff_size;
  155. ulong long_query_time;
  156. ulong max_allowed_packet;
  157. ulong max_error_count;
  158. ulong max_heap_table_size;
  159. ulong max_length_for_sort_data;
  160. ulong max_sort_length;
  161. ulong max_tmp_tables;
  162. ulong max_insert_delayed_threads;
  163. ulong multi_range_count;
  164. ulong myisam_repair_threads;
  165. ulong myisam_sort_buff_size;
  166. ulong myisam_stats_method;
  167. ulong net_buffer_length;
  168. ulong net_interactive_timeout;
  169. ulong net_read_timeout;
  170. ulong net_retry_count;
  171. ulong net_wait_timeout;
  172. ulong net_write_timeout;
  173. ulong optimizer_prune_level;
  174. ulong optimizer_search_depth;
  175. ulong preload_buff_size;
  176. ulong query_cache_type;
  177. ulong read_buff_size;
  178. ulong read_rnd_buff_size;
  179. ulong div_precincrement;
  180. ulong sortbuff_size;
  181. handlerton *table_type;
  182. ulong tmp_table_size;
  183. ulong tx_isolation;
  184. ulong completion_type;
  185. /* Determines which non-standard SQL behaviour should be enabled */
  186. ulong sql_mode;
  187. ulong max_sp_recursion_depth;
  188. /* check of key presence in updatable view */
  189. ulong updatable_views_with_limit;
  190. ulong default_week_format;
  191. ulong max_seeks_for_key;
  192. ulong range_alloc_block_size;
  193. ulong query_alloc_block_size;
  194. ulong query_prealloc_size;
  195. ulong trans_alloc_block_size;
  196. ulong trans_prealloc_size;
  197. ulong log_warnings;
  198. ulong group_concat_max_len;
  199. /*
  200. In slave thread we need to know in behalf of which
  201. thread the query is being run to replicate temp tables properly
  202. */
  203. ulong pseudo_thread_id;
  204. my_bool low_priority_updates;
  205. my_bool new_mode;
  206. my_bool query_cache_wlock_invalidate;
  207. my_bool engine_condition_pushdown;
  208. my_bool innodb_table_locks;
  209. my_bool innodb_support_xa;
  210. my_bool ndb_force_send;
  211. my_bool ndb_use_copying_alter_table;
  212. my_bool ndb_use_exact_count;
  213. my_bool ndb_use_transactions;
  214. my_bool ndb_index_stat_enable;
  215. ulong ndb_autoincrement_prefetch_sz;
  216. ulong ndb_index_stat_cache_entries;
  217. ulong ndb_index_stat_update_freq;
  218. ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
  219. my_bool old_alter_table;
  220. my_bool old_passwords;
  221. /* Only charset part of these variables is sensible */
  222. CHARSET_INFO *character_set_filesystem;
  223. CHARSET_INFO *character_set_client;
  224. CHARSET_INFO *character_set_results;
  225. /* Both charset and collation parts of these variables are important */
  226. CHARSET_INFO *collation_server;
  227. CHARSET_INFO *collation_database;
  228. CHARSET_INFO *collation_connection;
  229. Time_zone *time_zone;
  230. /* DATE, DATETIME and TIME formats */
  231. DATE_TIME_FORMAT *date_format;
  232. DATE_TIME_FORMAT *datetime_format;
  233. DATE_TIME_FORMAT *time_format;
  234. my_bool sysdate_is_now;
  235. };
  236. /* per thread status variables */
  237. typedef struct system_status_var
  238. {
  239. ulong bytes_received;
  240. ulong bytes_sent;
  241. ulong com_other;
  242. ulong com_stat[(uint) SQLCOM_END];
  243. ulong created_tmp_disk_tables;
  244. ulong created_tmp_tables;
  245. ulong ha_commit_count;
  246. ulong ha_delete_count;
  247. ulong ha_read_first_count;
  248. ulong ha_read_last_count;
  249. ulong ha_read_key_count;
  250. ulong ha_read_next_count;
  251. ulong ha_read_prev_count;
  252. ulong ha_read_rnd_count;
  253. ulong ha_read_rnd_next_count;
  254. ulong ha_rollback_count;
  255. ulong ha_update_count;
  256. ulong ha_write_count;
  257. ulong ha_prepare_count;
  258. ulong ha_discover_count;
  259. ulong ha_savepoint_count;
  260. ulong ha_savepoint_rollback_count;
  261. /* KEY_CACHE parts. These are copies of the original */
  262. ulong key_blocks_changed;
  263. ulong key_blocks_used;
  264. ulong key_cache_r_requests;
  265. ulong key_cache_read;
  266. ulong key_cache_w_requests;
  267. ulong key_cache_write;
  268. /* END OF KEY_CACHE parts */
  269. ulong net_big_packet_count;
  270. ulong opened_tables;
  271. ulong opened_shares;
  272. ulong select_full_join_count;
  273. ulong select_full_range_join_count;
  274. ulong select_range_count;
  275. ulong select_range_check_count;
  276. ulong select_scan_count;
  277. ulong long_query_count;
  278. ulong filesort_merge_passes;
  279. ulong filesort_range_count;
  280. ulong filesort_rows;
  281. ulong filesort_scan_count;
  282. /* Prepared statements and binary protocol */
  283. ulong com_stmt_prepare;
  284. ulong com_stmt_execute;
  285. ulong com_stmt_send_long_data;
  286. ulong com_stmt_fetch;
  287. ulong com_stmt_reset;
  288. ulong com_stmt_close;
  289. double last_query_cost;
  290. } STATUS_VAR;
  291. /*
  292. This is used for 'show status'. It must be updated to the last ulong
  293. variable in system_status_var
  294. */
  295. #define last_system_status_var com_stmt_close
  296. #ifdef MYSQL_SERVER
  297. void free_tmp_table(THD *thd, TABLE *entry);
  298. /* The following macro is to make init of Query_arena simpler */
  299. #ifndef DBUG_OFF
  300. #define INIT_ARENA_DBUG_INFO is_backup_arena= 0
  301. #else
  302. #define INIT_ARENA_DBUG_INFO
  303. #endif
  304. class Query_arena
  305. {
  306. public:
  307. /*
  308. List of items created in the parser for this query. Every item puts
  309. itself to the list on creation (see Item::Item() for details))
  310. */
  311. Item *free_list;
  312. MEM_ROOT *mem_root; // Pointer to current memroot
  313. #ifndef DBUG_OFF
  314. bool is_backup_arena; /* True if this arena is used for backup. */
  315. #endif
  316. enum enum_state
  317. {
  318. INITIALIZED= 0, INITIALIZED_FOR_SP= 1, PREPARED= 2,
  319. CONVENTIONAL_EXECUTION= 3, EXECUTED= 4, ERROR= -1
  320. };
  321. enum_state state;
  322. /* We build without RTTI, so dynamic_cast can't be used. */
  323. enum Type
  324. {
  325. STATEMENT, PREPARED_STATEMENT, STORED_PROCEDURE
  326. };
  327. Query_arena(MEM_ROOT *mem_root_arg, enum enum_state state_arg) :
  328. free_list(0), mem_root(mem_root_arg), state(state_arg)
  329. { INIT_ARENA_DBUG_INFO; }
  330. /*
  331. This constructor is used only when Query_arena is created as
  332. backup storage for another instance of Query_arena.
  333. */
  334. Query_arena() { INIT_ARENA_DBUG_INFO; }
  335. virtual Type type() const;
  336. virtual ~Query_arena() {};
  337. inline bool is_stmt_prepare() const { return state == INITIALIZED; }
  338. inline bool is_first_sp_execute() const
  339. { return state == INITIALIZED_FOR_SP; }
  340. inline bool is_stmt_prepare_or_first_sp_execute() const
  341. { return (int)state < (int)PREPARED; }
  342. inline bool is_first_stmt_execute() const { return state == PREPARED; }
  343. inline bool is_stmt_execute() const
  344. { return state == PREPARED || state == EXECUTED; }
  345. inline bool is_conventional() const
  346. { return state == CONVENTIONAL_EXECUTION; }
  347. inline gptr alloc(unsigned int size) { return alloc_root(mem_root,size); }
  348. inline gptr calloc(unsigned int size)
  349. {
  350. gptr ptr;
  351. if ((ptr=alloc_root(mem_root,size)))
  352. bzero((char*) ptr,size);
  353. return ptr;
  354. }
  355. inline char *strdup(const char *str)
  356. { return strdup_root(mem_root,str); }
  357. inline char *strmake(const char *str, uint size)
  358. { return strmake_root(mem_root,str,size); }
  359. inline char *memdup(const char *str, uint size)
  360. { return memdup_root(mem_root,str,size); }
  361. inline char *memdup_w_gap(const char *str, uint size, uint gap)
  362. {
  363. gptr ptr;
  364. if ((ptr=alloc_root(mem_root,size+gap)))
  365. memcpy(ptr,str,size);
  366. return ptr;
  367. }
  368. void set_query_arena(Query_arena *set);
  369. void free_items();
  370. /* Close the active state associated with execution of this statement */
  371. virtual void cleanup_stmt();
  372. };
  373. class Server_side_cursor;
  374. /*
  375. State of a single command executed against this connection.
  376. One connection can contain a lot of simultaneously running statements,
  377. some of which could be:
  378. - prepared, that is, contain placeholders,
  379. - opened as cursors. We maintain 1 to 1 relationship between
  380. statement and cursor - if user wants to create another cursor for his
  381. query, we create another statement for it.
  382. To perform some action with statement we reset THD part to the state of
  383. that statement, do the action, and then save back modified state from THD
  384. to the statement. It will be changed in near future, and Statement will
  385. be used explicitly.
  386. */
  387. class Statement: public ilink, public Query_arena
  388. {
  389. Statement(const Statement &rhs); /* not implemented: */
  390. Statement &operator=(const Statement &rhs); /* non-copyable */
  391. public:
  392. /* FIXME: these must be protected */
  393. MEM_ROOT main_mem_root;
  394. LEX main_lex;
  395. /*
  396. Uniquely identifies each statement object in thread scope; change during
  397. statement lifetime. FIXME: must be const
  398. */
  399. ulong id;
  400. /*
  401. MARK_COLUMNS_NONE: Means mark_used_colums is not set and no indicator to
  402. handler of fields used is set
  403. MARK_COLUMNS_READ: Means a bit in read set is set to inform handler
  404. that the field is to be read. If field list contains
  405. duplicates, then thd->dup_field is set to point
  406. to the last found duplicate.
  407. MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
  408. that it needs to update this field in write_row
  409. and update_row.
  410. */
  411. enum enum_mark_columns mark_used_columns;
  412. LEX_STRING name; /* name for named prepared statements */
  413. LEX *lex; // parse tree descriptor
  414. /*
  415. Points to the query associated with this statement. It's const, but
  416. we need to declare it char * because all table handlers are written
  417. in C and need to point to it.
  418. Note that (A) if we set query = NULL, we must at the same time set
  419. query_length = 0, and protect the whole operation with the
  420. LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
  421. non-NULL value if its previous value is NULL. We do not need to protect
  422. operation (B) with any mutex. To avoid crashes in races, if we do not
  423. know that thd->query cannot change at the moment, one should print
  424. thd->query like this:
  425. (1) reserve the LOCK_thread_count mutex;
  426. (2) check if thd->query is NULL;
  427. (3) if not NULL, then print at most thd->query_length characters from
  428. it. We will see the query_length field as either 0, or the right value
  429. for it.
  430. Assuming that the write and read of an n-bit memory field in an n-bit
  431. computer is atomic, we can avoid races in the above way.
  432. This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB
  433. STATUS.
  434. */
  435. char *query;
  436. uint32 query_length; // current query length
  437. Server_side_cursor *cursor;
  438. public:
  439. /* This constructor is called for backup statements */
  440. Statement() { clear_alloc_root(&main_mem_root); }
  441. Statement(enum enum_state state_arg, ulong id_arg,
  442. ulong alloc_block_size, ulong prealloc_size);
  443. virtual ~Statement();
  444. /* Assign execution context (note: not all members) of given stmt to self */
  445. void set_statement(Statement *stmt);
  446. void set_n_backup_statement(Statement *stmt, Statement *backup);
  447. void restore_backup_statement(Statement *stmt, Statement *backup);
  448. /* return class type */
  449. virtual Type type() const;
  450. };
  451. /*
  452. Container for all statements created/used in a connection.
  453. Statements in Statement_map have unique Statement::id (guaranteed by id
  454. assignment in Statement::Statement)
  455. Non-empty statement names are unique too: attempt to insert a new statement
  456. with duplicate name causes older statement to be deleted
  457. Statements are auto-deleted when they are removed from the map and when the
  458. map is deleted.
  459. */
  460. class Statement_map
  461. {
  462. public:
  463. Statement_map();
  464. int insert(THD *thd, Statement *statement);
  465. Statement *find_by_name(LEX_STRING *name)
  466. {
  467. Statement *stmt;
  468. stmt= (Statement*)hash_search(&names_hash, (byte*)name->str,
  469. name->length);
  470. return stmt;
  471. }
  472. Statement *find(ulong id)
  473. {
  474. if (last_found_statement == 0 || id != last_found_statement->id)
  475. {
  476. Statement *stmt;
  477. stmt= (Statement *) hash_search(&st_hash, (byte *) &id, sizeof(id));
  478. if (stmt && stmt->name.str)
  479. return NULL;
  480. last_found_statement= stmt;
  481. }
  482. return last_found_statement;
  483. }
  484. /*
  485. Close all cursors of this connection that use tables of a storage
  486. engine that has transaction-specific state and therefore can not
  487. survive COMMIT or ROLLBACK. Currently all but MyISAM cursors are closed.
  488. */
  489. void close_transient_cursors();
  490. void erase(Statement *statement);
  491. /* Erase all statements (calls Statement destructor) */
  492. void reset();
  493. ~Statement_map();
  494. private:
  495. HASH st_hash;
  496. HASH names_hash;
  497. I_List<Statement> transient_cursor_list;
  498. Statement *last_found_statement;
  499. };
  500. struct st_savepoint {
  501. struct st_savepoint *prev;
  502. char *name;
  503. uint length, nht;
  504. };
  505. enum xa_states {XA_NOTR=0, XA_ACTIVE, XA_IDLE, XA_PREPARED};
  506. extern const char *xa_state_names[];
  507. typedef struct st_xid_state {
  508. /* For now, this is only used to catch duplicated external xids */
  509. XID xid; // transaction identifier
  510. enum xa_states xa_state; // used by external XA only
  511. bool in_thd;
  512. } XID_STATE;
  513. extern pthread_mutex_t LOCK_xid_cache;
  514. extern HASH xid_cache;
  515. bool xid_cache_init(void);
  516. void xid_cache_free(void);
  517. XID_STATE *xid_cache_search(XID *xid);
  518. bool xid_cache_insert(XID *xid, enum xa_states xa_state);
  519. bool xid_cache_insert(XID_STATE *xid_state);
  520. void xid_cache_delete(XID_STATE *xid_state);
  521. class Security_context {
  522. public:
  523. Security_context() {} /* Remove gcc warning */
  524. /*
  525. host - host of the client
  526. user - user of the client, set to NULL until the user has been read from
  527. the connection
  528. priv_user - The user privilege we are using. May be "" for anonymous user.
  529. ip - client IP
  530. */
  531. char *host, *user, *priv_user, *ip;
  532. /* The host privilege we are using */
  533. char priv_host[MAX_HOSTNAME];
  534. /* points to host if host is available, otherwise points to ip */
  535. const char *host_or_ip;
  536. ulong master_access; /* Global privileges from mysql.user */
  537. ulong db_access; /* Privileges for current db */
  538. void init();
  539. void destroy();
  540. void skip_grants();
  541. inline char *priv_host_name()
  542. {
  543. return (*priv_host ? priv_host : (char *)"%");
  544. }
  545. bool set_user(char *user_arg);
  546. };
  547. /*
  548. A registry for item tree transformations performed during
  549. query optimization. We register only those changes which require
  550. a rollback to re-execute a prepared statement or stored procedure
  551. yet another time.
  552. */
  553. struct Item_change_record;
  554. typedef I_List<Item_change_record> Item_change_list;
  555. /*
  556. Type of prelocked mode.
  557. See comment for THD::prelocked_mode for complete description.
  558. */
  559. enum prelocked_mode_type {NON_PRELOCKED= 0, PRELOCKED= 1,
  560. PRELOCKED_UNDER_LOCK_TABLES= 2};
  561. /*
  562. Class that holds information about tables which were opened and locked
  563. by the thread. It is also used to save/restore this information in
  564. push_open_tables_state()/pop_open_tables_state().
  565. */
  566. class Open_tables_state
  567. {
  568. public:
  569. /*
  570. open_tables - list of regular tables in use by this thread
  571. temporary_tables - list of temp tables in use by this thread
  572. handler_tables - list of tables that were opened with HANDLER OPEN
  573. and are still in use by this thread
  574. */
  575. TABLE *open_tables, *temporary_tables, *handler_tables, *derived_tables;
  576. /*
  577. During a MySQL session, one can lock tables in two modes: automatic
  578. or manual. In automatic mode all necessary tables are locked just before
  579. statement execution, and all acquired locks are stored in 'lock'
  580. member. Unlocking takes place automatically as well, when the
  581. statement ends.
  582. Manual mode comes into play when a user issues a 'LOCK TABLES'
  583. statement. In this mode the user can only use the locked tables.
  584. Trying to use any other tables will give an error. The locked tables are
  585. stored in 'locked_tables' member. Manual locking is described in
  586. the 'LOCK_TABLES' chapter of the MySQL manual.
  587. See also lock_tables() for details.
  588. */
  589. MYSQL_LOCK *lock;
  590. /*
  591. Tables that were locked with explicit or implicit LOCK TABLES.
  592. (Implicit LOCK TABLES happens when we are prelocking tables for
  593. execution of statement which uses stored routines. See description
  594. THD::prelocked_mode for more info.)
  595. */
  596. MYSQL_LOCK *locked_tables;
  597. /*
  598. CREATE-SELECT keeps an extra lock for the table being
  599. created. This field is used to keep the extra lock available for
  600. lower level routines, which would otherwise miss that lock.
  601. */
  602. MYSQL_LOCK *extra_lock;
  603. /*
  604. prelocked_mode_type enum and prelocked_mode member are used for
  605. indicating whenever "prelocked mode" is on, and what type of
  606. "prelocked mode" is it.
  607. Prelocked mode is used for execution of queries which explicitly
  608. or implicitly (via views or triggers) use functions, thus may need
  609. some additional tables (mentioned in query table list) for their
  610. execution.
  611. First open_tables() call for such query will analyse all functions
  612. used by it and add all additional tables to table its list. It will
  613. also mark this query as requiring prelocking. After that lock_tables()
  614. will issue implicit LOCK TABLES for the whole table list and change
  615. thd::prelocked_mode to non-0. All queries called in functions invoked
  616. by the main query will use prelocked tables. Non-0 prelocked_mode
  617. will also surpress mentioned analysys in those queries thus saving
  618. cycles. Prelocked mode will be turned off once close_thread_tables()
  619. for the main query will be called.
  620. Note: Since not all "tables" present in table list are really locked
  621. thd::prelocked_mode does not imply thd::locked_tables.
  622. */
  623. prelocked_mode_type prelocked_mode;
  624. ulong version;
  625. uint current_tablenr;
  626. enum enum_flags {
  627. BACKUPS_AVAIL = (1U << 0) /* There are backups available */
  628. };
  629. /*
  630. Flags with information about the open tables state.
  631. */
  632. uint state_flags;
  633. /*
  634. This constructor serves for creation of Open_tables_state instances
  635. which are used as backup storage.
  636. */
  637. Open_tables_state() : state_flags(0U) { }
  638. Open_tables_state(ulong version_arg);
  639. void set_open_tables_state(Open_tables_state *state)
  640. {
  641. *this= *state;
  642. }
  643. void reset_open_tables_state()
  644. {
  645. open_tables= temporary_tables= handler_tables= derived_tables= 0;
  646. extra_lock= lock= locked_tables= 0;
  647. prelocked_mode= NON_PRELOCKED;
  648. state_flags= 0U;
  649. }
  650. };
  651. /* class to save context when executing a function or trigger */
  652. /* Defines used for Sub_statement_state::in_sub_stmt */
  653. #define SUB_STMT_TRIGGER 1
  654. #define SUB_STMT_FUNCTION 2
  655. class Sub_statement_state
  656. {
  657. public:
  658. ulonglong options;
  659. ulonglong last_insert_id, next_insert_id, current_insert_id;
  660. ulonglong limit_found_rows;
  661. ha_rows cuted_fields, sent_row_count, examined_row_count;
  662. ulong client_capabilities;
  663. uint in_sub_stmt;
  664. bool enable_slow_log, insert_id_used, clear_next_insert_id;
  665. bool last_insert_id_used;
  666. my_bool no_send_ok;
  667. SAVEPOINT *savepoints;
  668. };
  669. /* Flags for the THD::system_thread variable */
  670. enum enum_thread_type
  671. {
  672. NON_SYSTEM_THREAD= 0,
  673. SYSTEM_THREAD_DELAYED_INSERT= 1,
  674. SYSTEM_THREAD_SLAVE_IO= 2,
  675. SYSTEM_THREAD_SLAVE_SQL= 4,
  676. SYSTEM_THREAD_NDBCLUSTER_BINLOG= 8,
  677. SYSTEM_THREAD_EVENT_SCHEDULER= 16,
  678. SYSTEM_THREAD_EVENT_WORKER= 32
  679. };
  680. /*
  681. For each client connection we create a separate thread with THD serving as
  682. a thread/connection descriptor
  683. */
  684. class THD :public Statement,
  685. public Open_tables_state
  686. {
  687. public:
  688. /* Used to execute base64 coded binlog events in MySQL server */
  689. RELAY_LOG_INFO* rli_fake;
  690. /*
  691. Constant for THD::where initialization in the beginning of every query.
  692. It's needed because we do not save/restore THD::where normally during
  693. primary (non subselect) query execution.
  694. */
  695. static const char * const DEFAULT_WHERE;
  696. #ifdef EMBEDDED_LIBRARY
  697. struct st_mysql *mysql;
  698. unsigned long client_stmt_id;
  699. unsigned long client_param_count;
  700. struct st_mysql_bind *client_params;
  701. char *extra_data;
  702. ulong extra_length;
  703. struct st_mysql_data *cur_data;
  704. struct st_mysql_data *first_data;
  705. struct st_mysql_data **data_tail;
  706. void clear_data_list();
  707. struct st_mysql_data *alloc_new_dataset();
  708. #endif
  709. NET net; // client connection descriptor
  710. MEM_ROOT warn_root; // For warnings and errors
  711. Protocol *protocol; // Current protocol
  712. Protocol_simple protocol_simple; // Normal protocol
  713. Protocol_prep protocol_prep; // Binary protocol
  714. HASH user_vars; // hash for user variables
  715. String packet; // dynamic buffer for network I/O
  716. String convert_buffer; // buffer for charset conversions
  717. struct sockaddr_in remote; // client socket address
  718. struct rand_struct rand; // used for authentication
  719. struct system_variables variables; // Changeable local variables
  720. struct system_status_var status_var; // Per thread statistic vars
  721. struct system_status_var *initial_status_var; /* used by show status */
  722. THR_LOCK_INFO lock_info; // Locking info of this thread
  723. THR_LOCK_OWNER main_lock_id; // To use for conventional queries
  724. THR_LOCK_OWNER *lock_id; // If not main_lock_id, points to
  725. // the lock_id of a cursor.
  726. pthread_mutex_t LOCK_delete; // Locked before thd is deleted
  727. /* all prepared statements and cursors of this connection */
  728. Statement_map stmt_map;
  729. /*
  730. A pointer to the stack frame of handle_one_connection(),
  731. which is called first in the thread for handling a client
  732. */
  733. char *thread_stack;
  734. /*
  735. db - currently selected database
  736. catalog - currently selected catalog
  737. WARNING: some members of THD (currently 'db', 'catalog' and 'query') are
  738. set and alloced by the slave SQL thread (for the THD of that thread); that
  739. thread is (and must remain, for now) the only responsible for freeing these
  740. 3 members. If you add members here, and you add code to set them in
  741. replication, don't forget to free_them_and_set_them_to_0 in replication
  742. properly. For details see the 'err:' label of the handle_slave_sql()
  743. in sql/slave.cc.
  744. */
  745. char *db, *catalog;
  746. Security_context main_security_ctx;
  747. Security_context *security_ctx;
  748. /* remote (peer) port */
  749. uint16 peer_port;
  750. /*
  751. Points to info-string that we show in SHOW PROCESSLIST
  752. You are supposed to update thd->proc_info only if you have coded
  753. a time-consuming piece that MySQL can get stuck in for a long time.
  754. */
  755. const char *proc_info;
  756. ulong client_capabilities; /* What the client supports */
  757. ulong max_client_packet_length;
  758. HASH handler_tables_hash;
  759. /*
  760. One thread can hold up to one named user-level lock. This variable
  761. points to a lock object if the lock is present. See item_func.cc and
  762. chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
  763. */
  764. User_level_lock *ull;
  765. #ifndef DBUG_OFF
  766. uint dbug_sentry; // watch out for memory corruption
  767. #endif
  768. struct st_my_thread_var *mysys_var;
  769. /*
  770. Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
  771. first byte of the packet in do_command()
  772. */
  773. enum enum_server_command command;
  774. uint32 server_id;
  775. uint32 file_id; // for LOAD DATA INFILE
  776. /*
  777. Used in error messages to tell user in what part of MySQL we found an
  778. error. E. g. when where= "having clause", if fix_fields() fails, user
  779. will know that the error was in having clause.
  780. */
  781. const char *where;
  782. time_t start_time,time_after_lock,user_time;
  783. time_t connect_time,thr_create_time; // track down slow pthread_create
  784. thr_lock_type update_lock_default;
  785. delayed_insert *di;
  786. /* <> 0 if we are inside of trigger or stored function. */
  787. uint in_sub_stmt;
  788. /* container for handler's private per-connection data */
  789. void *ha_data[MAX_HA];
  790. #ifndef MYSQL_CLIENT
  791. int binlog_setup_trx_data();
  792. #ifdef HAVE_ROW_BASED_REPLICATION
  793. /*
  794. Public interface to write RBR events to the binlog
  795. */
  796. int binlog_write_table_map(TABLE *table, bool is_transactional);
  797. int binlog_write_row(TABLE* table, bool is_transactional,
  798. MY_BITMAP const* cols, my_size_t colcnt,
  799. const byte *buf);
  800. int binlog_delete_row(TABLE* table, bool is_transactional,
  801. MY_BITMAP const* cols, my_size_t colcnt,
  802. const byte *buf);
  803. int binlog_update_row(TABLE* table, bool is_transactional,
  804. MY_BITMAP const* cols, my_size_t colcnt,
  805. const byte *old_data, const byte *new_data);
  806. void set_server_id(uint32 sid) { server_id = sid; }
  807. /*
  808. Member functions to handle pending event for row-level logging.
  809. */
  810. template <class RowsEventT> Rows_log_event*
  811. binlog_prepare_pending_rows_event(TABLE* table, uint32 serv_id,
  812. MY_BITMAP const* cols,
  813. my_size_t colcnt,
  814. my_size_t needed,
  815. bool is_transactional,
  816. RowsEventT* hint);
  817. Rows_log_event* binlog_get_pending_rows_event() const;
  818. void binlog_set_pending_rows_event(Rows_log_event* ev);
  819. my_size_t max_row_length_blob(TABLE* table, const byte *data) const;
  820. my_size_t max_row_length(TABLE* table, const byte *data) const
  821. {
  822. TABLE_SHARE *table_s= table->s;
  823. my_size_t length= table_s->reclength + 2 * table_s->fields;
  824. if (table_s->blob_fields == 0)
  825. return length;
  826. return (length+max_row_length_blob(table,data));
  827. }
  828. my_size_t pack_row(TABLE* table, MY_BITMAP const* cols, byte *row_data,
  829. const byte *data) const;
  830. int binlog_flush_pending_rows_event(bool stmt_end);
  831. void binlog_delete_pending_rows_event();
  832. private:
  833. uint binlog_table_maps; // Number of table maps currently in the binlog
  834. public:
  835. uint get_binlog_table_maps() const {
  836. return binlog_table_maps;
  837. }
  838. #endif /* HAVE_ROW_BASED_REPLICATION */
  839. #endif /* MYSQL_CLIENT */
  840. #ifndef MYSQL_CLIENT
  841. public:
  842. enum enum_binlog_query_type {
  843. /*
  844. The query can be logged row-based or statement-based
  845. */
  846. ROW_QUERY_TYPE,
  847. /*
  848. The query has to be logged statement-based
  849. */
  850. STMT_QUERY_TYPE,
  851. /*
  852. The query represents a change to a table in the "mysql"
  853. database and is currently mapped to ROW_QUERY_TYPE.
  854. */
  855. MYSQL_QUERY_TYPE,
  856. QUERY_TYPE_COUNT
  857. };
  858. int binlog_query(enum_binlog_query_type qtype,
  859. char const *query, ulong query_len,
  860. bool is_trans, bool suppress_use);
  861. #endif
  862. public:
  863. struct st_transactions {
  864. SAVEPOINT *savepoints;
  865. THD_TRANS all; // Trans since BEGIN WORK
  866. THD_TRANS stmt; // Trans for current statement
  867. bool on; // see ha_enable_transaction()
  868. XID xid; // transaction identifier
  869. enum xa_states xa_state; // used by external XA only
  870. XID_STATE xid_state;
  871. #ifdef HAVE_ROW_BASED_REPLICATION
  872. Rows_log_event *m_pending_rows_event;
  873. #endif
  874. /*
  875. Tables changed in transaction (that must be invalidated in query cache).
  876. List contain only transactional tables, that not invalidated in query
  877. cache (instead of full list of changed in transaction tables).
  878. */
  879. CHANGED_TABLE_LIST* changed_tables;
  880. MEM_ROOT mem_root; // Transaction-life memory allocation pool
  881. void cleanup()
  882. {
  883. changed_tables= 0;
  884. savepoints= 0;
  885. #ifdef USING_TRANSACTIONS
  886. free_root(&mem_root,MYF(MY_KEEP_PREALLOC));
  887. #endif
  888. }
  889. st_transactions()
  890. {
  891. #ifdef USING_TRANSACTIONS
  892. bzero((char*)this, sizeof(*this));
  893. xid_state.xid.null();
  894. init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
  895. #else
  896. xid_state.xa_state= XA_NOTR;
  897. #endif
  898. }
  899. } transaction;
  900. Field *dup_field;
  901. #ifndef __WIN__
  902. sigset_t signals,block_signals;
  903. #endif
  904. #ifdef SIGNAL_WITH_VIO_CLOSE
  905. Vio* active_vio;
  906. #endif
  907. /*
  908. This is to track items changed during execution of a prepared
  909. statement/stored procedure. It's created by
  910. register_item_tree_change() in memory root of THD, and freed in
  911. rollback_item_tree_changes(). For conventional execution it's always
  912. empty.
  913. */
  914. Item_change_list change_list;
  915. /*
  916. A permanent memory area of the statement. For conventional
  917. execution, the parsed tree and execution runtime reside in the same
  918. memory root. In this case stmt_arena points to THD. In case of
  919. a prepared statement or a stored procedure statement, thd->mem_root
  920. conventionally points to runtime memory, and thd->stmt_arena
  921. points to the memory of the PS/SP, where the parsed tree of the
  922. statement resides. Whenever you need to perform a permanent
  923. transformation of a parsed tree, you should allocate new memory in
  924. stmt_arena, to allow correct re-execution of PS/SP.
  925. Note: in the parser, stmt_arena == thd, even for PS/SP.
  926. */
  927. Query_arena *stmt_arena;
  928. /*
  929. next_insert_id is set on SET INSERT_ID= #. This is used as the next
  930. generated auto_increment value in handler.cc
  931. */
  932. ulonglong next_insert_id;
  933. /* Remember last next_insert_id to reset it if something went wrong */
  934. ulonglong prev_insert_id;
  935. /*
  936. The insert_id used for the last statement or set by SET LAST_INSERT_ID=#
  937. or SELECT LAST_INSERT_ID(#). Used for binary log and returned by
  938. LAST_INSERT_ID()
  939. */
  940. ulonglong last_insert_id;
  941. /*
  942. Set to the first value that LAST_INSERT_ID() returned for the last
  943. statement. When this is set, last_insert_id_used is set to true.
  944. */
  945. ulonglong current_insert_id;
  946. ulonglong limit_found_rows;
  947. ulonglong options; /* Bitmap of states */
  948. longlong row_count_func; /* For the ROW_COUNT() function */
  949. ha_rows cuted_fields,
  950. sent_row_count, examined_row_count;
  951. /*
  952. The set of those tables whose fields are referenced in all subqueries
  953. of the query.
  954. TODO: possibly this it is incorrect to have used tables in THD because
  955. with more than one subquery, it is not clear what does the field mean.
  956. */
  957. table_map used_tables;
  958. USER_CONN *user_connect;
  959. CHARSET_INFO *db_charset;
  960. /*
  961. FIXME: this, and some other variables like 'count_cuted_fields'
  962. maybe should be statement/cursor local, that is, moved to Statement
  963. class. With current implementation warnings produced in each prepared
  964. statement/cursor settle here.
  965. */
  966. List <MYSQL_ERROR> warn_list;
  967. uint warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_END];
  968. uint total_warn_count;
  969. /*
  970. Id of current query. Statement can be reused to execute several queries
  971. query_id is global in context of the whole MySQL server.
  972. ID is automatically generated from mutex-protected counter.
  973. It's used in handler code for various purposes: to check which columns
  974. from table are necessary for this select, to check if it's necessary to
  975. update auto-updatable fields (like auto_increment and timestamp).
  976. */
  977. query_id_t query_id, warn_id;
  978. ulong thread_id, col_access;
  979. #ifdef ERROR_INJECT_SUPPORT
  980. ulong error_inject_value;
  981. #endif
  982. /* Statement id is thread-wide. This counter is used to generate ids */
  983. ulong statement_id_counter;
  984. ulong rand_saved_seed1, rand_saved_seed2;
  985. ulong row_count; // Row counter, mainly for errors and warnings
  986. long dbug_thread_id;
  987. pthread_t real_id;
  988. uint tmp_table, global_read_lock;
  989. uint server_status,open_options;
  990. enum enum_thread_type system_thread;
  991. uint32 db_length;
  992. uint select_number; //number of select (used for EXPLAIN)
  993. /* variables.transaction_isolation is reset to this after each commit */
  994. enum_tx_isolation session_tx_isolation;
  995. enum_check_fields count_cuted_fields;
  996. DYNAMIC_ARRAY user_var_events; /* For user variables replication */
  997. MEM_ROOT *user_var_events_alloc; /* Allocate above array elements here */
  998. enum killed_state { NOT_KILLED=0, KILL_BAD_DATA=1, KILL_CONNECTION=ER_SERVER_SHUTDOWN, KILL_QUERY=ER_QUERY_INTERRUPTED };
  999. killed_state volatile killed;
  1000. /* scramble - random string sent to client on handshake */
  1001. char scramble[SCRAMBLE_LENGTH+1];
  1002. bool slave_thread, one_shot_set;
  1003. /* tells if current statement should binlog row-based(1) or stmt-based(0) */
  1004. bool current_stmt_binlog_row_based;
  1005. bool locked, some_tables_deleted;
  1006. bool last_cuted_field;
  1007. bool no_errors, password, is_fatal_error;
  1008. bool query_start_used, rand_used, time_zone_used;
  1009. bool last_insert_id_used,insert_id_used, clear_next_insert_id;
  1010. bool in_lock_tables;
  1011. bool query_error, bootstrap, cleanup_done;
  1012. bool tmp_table_used;
  1013. bool charset_is_system_charset, charset_is_collation_connection;
  1014. bool charset_is_character_set_filesystem;
  1015. bool enable_slow_log; /* enable slow log for current statement */
  1016. bool no_trans_update, abort_on_warning;
  1017. bool got_warning; /* Set on call to push_warning() */
  1018. bool no_warnings_for_error; /* no warnings on call to my_error() */
  1019. /* set during loop of derived table processing */
  1020. bool derived_tables_processing;
  1021. my_bool tablespace_op; /* This is TRUE in DISCARD/IMPORT TABLESPACE */
  1022. sp_rcontext *spcont; // SP runtime context
  1023. sp_cache *sp_proc_cache;
  1024. sp_cache *sp_func_cache;
  1025. /*
  1026. If we do a purge of binary logs, log index info of the threads
  1027. that are currently reading it needs to be adjusted. To do that
  1028. each thread that is using LOG_INFO needs to adjust the pointer to it
  1029. */
  1030. LOG_INFO* current_linfo;
  1031. NET* slave_net; // network connection from slave -> m.
  1032. /* Used by the sys_var class to store temporary values */
  1033. union
  1034. {
  1035. my_bool my_bool_value;
  1036. long long_value;
  1037. ulong ulong_value;
  1038. } sys_var_tmp;
  1039. struct {
  1040. /*
  1041. If true, mysql_bin_log::write(Log_event) call will not write events to
  1042. binlog, and maintain 2 below variables instead (use
  1043. mysql_bin_log.start_union_events to turn this on)
  1044. */
  1045. bool do_union;
  1046. /*
  1047. If TRUE, at least one mysql_bin_log::write(Log_event) call has been
  1048. made after last mysql_bin_log.start_union_events() call.
  1049. */
  1050. bool unioned_events;
  1051. /*
  1052. If TRUE, at least one mysql_bin_log::write(Log_event e), where
  1053. e.cache_stmt == TRUE call has been made after last
  1054. mysql_bin_log.start_union_events() call.
  1055. */
  1056. bool unioned_events_trans;
  1057. /*
  1058. 'queries' (actually SP statements) that run under inside this binlog
  1059. union have thd->query_id >= first_query_id.
  1060. */
  1061. query_id_t first_query_id;
  1062. } binlog_evt_union;
  1063. #ifdef WITH_PARTITION_STORAGE_ENGINE
  1064. partition_info *work_part_info;
  1065. #endif
  1066. THD();
  1067. ~THD();
  1068. void init(void);
  1069. /*
  1070. Initialize memory roots necessary for query processing and (!)
  1071. pre-allocate memory for it. We can't do that in THD constructor because
  1072. there are use cases (acl_init, delayed inserts, watcher threads,
  1073. killing mysqld) where it's vital to not allocate excessive and not used
  1074. memory. Note, that we still don't return error from init_for_queries():
  1075. if preallocation fails, we should notice that at the first call to
  1076. alloc_root.
  1077. */
  1078. void init_for_queries();
  1079. void change_user(void);
  1080. void cleanup(void);
  1081. void cleanup_after_query();
  1082. bool store_globals();
  1083. #ifdef SIGNAL_WITH_VIO_CLOSE
  1084. inline void set_active_vio(Vio* vio)
  1085. {
  1086. pthread_mutex_lock(&LOCK_delete);
  1087. active_vio = vio;
  1088. pthread_mutex_unlock(&LOCK_delete);
  1089. }
  1090. inline void clear_active_vio()
  1091. {
  1092. pthread_mutex_lock(&LOCK_delete);
  1093. active_vio = 0;
  1094. pthread_mutex_unlock(&LOCK_delete);
  1095. }
  1096. void close_active_vio();
  1097. #endif
  1098. void awake(THD::killed_state state_to_set);
  1099. /*
  1100. For enter_cond() / exit_cond() to work the mutex must be got before
  1101. enter_cond(); this mutex is then released by exit_cond().
  1102. Usage must be: lock mutex; enter_cond(); your code; exit_cond().
  1103. */
  1104. inline const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex,
  1105. const char* msg)
  1106. {
  1107. const char* old_msg = proc_info;
  1108. safe_mutex_assert_owner(mutex);
  1109. mysys_var->current_mutex = mutex;
  1110. mysys_var->current_cond = cond;
  1111. proc_info = msg;
  1112. return old_msg;
  1113. }
  1114. inline void exit_cond(const char* old_msg)
  1115. {
  1116. /*
  1117. Putting the mutex unlock in exit_cond() ensures that
  1118. mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
  1119. locked (if that would not be the case, you'll get a deadlock if someone
  1120. does a THD::awake() on you).
  1121. */
  1122. pthread_mutex_unlock(mysys_var->current_mutex);
  1123. pthread_mutex_lock(&mysys_var->mutex);
  1124. mysys_var->current_mutex = 0;
  1125. mysys_var->current_cond = 0;
  1126. proc_info = old_msg;
  1127. pthread_mutex_unlock(&mysys_var->mutex);
  1128. }
  1129. inline time_t query_start() { query_start_used=1; return start_time; }
  1130. inline void set_time() { if (user_time) start_time=time_after_lock=user_time; else time_after_lock=time(&start_time); }
  1131. inline void end_time() { time(&start_time); }
  1132. inline void set_time(time_t t) { time_after_lock=start_time=user_time=t; }
  1133. inline void lock_time() { time(&time_after_lock); }
  1134. inline void insert_id(ulonglong id_arg)
  1135. {
  1136. last_insert_id= id_arg;
  1137. insert_id_used=1;
  1138. }
  1139. inline ulonglong insert_id(void)
  1140. {
  1141. if (!last_insert_id_used)
  1142. {
  1143. last_insert_id_used=1;
  1144. current_insert_id=last_insert_id;
  1145. }
  1146. return last_insert_id;
  1147. }
  1148. inline ulonglong found_rows(void)
  1149. {
  1150. return limit_found_rows;
  1151. }
  1152. inline bool active_transaction()
  1153. {
  1154. #ifdef USING_TRANSACTIONS
  1155. return server_status & SERVER_STATUS_IN_TRANS;
  1156. #else
  1157. return 0;
  1158. #endif
  1159. }
  1160. inline bool fill_derived_tables()
  1161. {
  1162. return !stmt_arena->is_stmt_prepare() && !lex->only_view_structure();
  1163. }
  1164. inline bool fill_information_schema_tables()
  1165. {
  1166. return !stmt_arena->is_stmt_prepare();
  1167. }
  1168. inline gptr trans_alloc(unsigned int size)
  1169. {
  1170. return alloc_root(&transaction.mem_root,size);
  1171. }
  1172. bool convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
  1173. const char *from, uint from_length,
  1174. CHARSET_INFO *from_cs);
  1175. bool convert_string(String *s, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs);
  1176. void add_changed_table(TABLE *table);
  1177. void add_changed_table(const char *key, long key_length);
  1178. CHANGED_TABLE_LIST * changed_table_dup(const char *key, long key_length);
  1179. int send_explain_fields(select_result *result);
  1180. #ifndef EMBEDDED_LIBRARY
  1181. inline void clear_error()
  1182. {
  1183. net.last_error[0]= 0;
  1184. net.last_errno= 0;
  1185. net.report_error= 0;
  1186. query_error= 0;
  1187. }
  1188. inline bool vio_ok() const { return net.vio != 0; }
  1189. #else
  1190. void clear_error();
  1191. inline bool vio_ok() const { return true; }
  1192. #endif
  1193. inline void fatal_error()
  1194. {
  1195. is_fatal_error= 1;
  1196. net.report_error= 1;
  1197. DBUG_PRINT("error",("Fatal error set"));
  1198. }
  1199. inline CHARSET_INFO *charset() { return variables.character_set_client; }
  1200. void update_charset();
  1201. inline Query_arena *activate_stmt_arena_if_needed(Query_arena *backup)
  1202. {
  1203. /*
  1204. Use the persistent arena if we are in a prepared statement or a stored
  1205. procedure statement and we have not already changed to use this arena.
  1206. */
  1207. if (!stmt_arena->is_conventional() && mem_root != stmt_arena->mem_root)
  1208. {
  1209. set_n_backup_active_arena(stmt_arena, backup);
  1210. return stmt_arena;
  1211. }
  1212. return 0;
  1213. }
  1214. void change_item_tree(Item **place, Item *new_value)
  1215. {
  1216. /* TODO: check for OOM condition here */
  1217. if (!stmt_arena->is_conventional())
  1218. nocheck_register_item_tree_change(place, *place, mem_root);
  1219. *place= new_value;
  1220. }
  1221. void nocheck_register_item_tree_change(Item **place, Item *old_value,
  1222. MEM_ROOT *runtime_memroot);
  1223. void rollback_item_tree_changes();
  1224. /*
  1225. Cleanup statement parse state (parse tree, lex) and execution
  1226. state after execution of a non-prepared SQL statement.
  1227. */
  1228. void end_statement();
  1229. inline int killed_errno() const
  1230. {
  1231. return killed != KILL_BAD_DATA ? killed : 0;
  1232. }
  1233. inline void send_kill_message() const
  1234. {
  1235. int err= killed_errno();
  1236. if (err)
  1237. my_message(err, ER(err), MYF(0));
  1238. }
  1239. /* return TRUE if we will abort query if we make a warning now */
  1240. inline bool really_abort_on_warning()
  1241. {
  1242. return (abort_on_warning &&
  1243. (!no_trans_update ||
  1244. (variables.sql_mode & MODE_STRICT_ALL_TABLES)));
  1245. }
  1246. void set_status_var_init();
  1247. bool is_context_analysis_only()
  1248. { return stmt_arena->is_stmt_prepare() || lex->view_prepare_mode; }
  1249. void reset_n_backup_open_tables_state(Open_tables_state *backup);
  1250. void restore_backup_open_tables_state(Open_tables_state *backup);
  1251. void reset_sub_statement_state(Sub_statement_state *backup, uint new_state);
  1252. void restore_sub_statement_state(Sub_statement_state *backup);
  1253. void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
  1254. void restore_active_arena(Query_arena *set, Query_arena *backup);
  1255. inline void set_current_stmt_binlog_row_based_if_mixed()
  1256. {
  1257. #ifdef HAVE_ROW_BASED_REPLICATION
  1258. if (variables.binlog_format == BINLOG_FORMAT_MIXED)
  1259. current_stmt_binlog_row_based= TRUE;
  1260. #endif
  1261. }
  1262. inline void set_current_stmt_binlog_row_based()
  1263. {
  1264. #ifdef HAVE_ROW_BASED_REPLICATION
  1265. current_stmt_binlog_row_based= TRUE;
  1266. #endif
  1267. }
  1268. inline void clear_current_stmt_binlog_row_based()
  1269. {
  1270. #ifdef HAVE_ROW_BASED_REPLICATION
  1271. current_stmt_binlog_row_based= FALSE;
  1272. #endif
  1273. }
  1274. inline void reset_current_stmt_binlog_row_based()
  1275. {
  1276. #ifdef HAVE_ROW_BASED_REPLICATION
  1277. current_stmt_binlog_row_based=
  1278. test(variables.binlog_format == BINLOG_FORMAT_ROW);
  1279. #else
  1280. current_stmt_binlog_row_based= FALSE;
  1281. #endif
  1282. }
  1283. };
  1284. #define tmp_disable_binlog(A) \
  1285. {ulonglong tmp_disable_binlog__save_options= (A)->options; \
  1286. (A)->options&= ~OPTION_BIN_LOG
  1287. #define reenable_binlog(A) (A)->options= tmp_disable_binlog__save_options;}
  1288. /*
  1289. Used to hold information about file and file structure in exchainge
  1290. via non-DB file (...INTO OUTFILE..., ...LOAD DATA...)
  1291. XXX: We never call destructor for objects of this class.
  1292. */
  1293. class sql_exchange :public Sql_alloc
  1294. {
  1295. public:
  1296. char *file_name;
  1297. String *field_term,*enclosed,*line_term,*line_start,*escaped;
  1298. bool opt_enclosed;
  1299. bool dumpfile;
  1300. ulong skip_lines;
  1301. sql_exchange(char *name,bool dumpfile_flag);
  1302. };
  1303. #include "log_event.h"
  1304. /*
  1305. This is used to get result from a select
  1306. */
  1307. class JOIN;
  1308. class select_result :public Sql_alloc {
  1309. protected:
  1310. THD *thd;
  1311. SELECT_LEX_UNIT *unit;
  1312. public:
  1313. select_result();
  1314. virtual ~select_result() {};
  1315. virtual int prepare(List<Item> &list, SELECT_LEX_UNIT *u)
  1316. {
  1317. unit= u;
  1318. return 0;
  1319. }
  1320. virtual int prepare2(void) { return 0; }
  1321. /*
  1322. Because of peculiarities of prepared statements protocol
  1323. we need to know number of columns in the result set (if
  1324. there is a result set) apart from sending columns metadata.
  1325. */
  1326. virtual uint field_count(List<Item> &fields) const
  1327. { return fields.elements; }
  1328. virtual bool send_fields(List<Item> &list, uint flags)=0;
  1329. virtual bool send_data(List<Item> &items)=0;
  1330. virtual bool initialize_tables (JOIN *join=0) { return 0; }
  1331. virtual void send_error(uint errcode,const char *err);
  1332. virtual bool send_eof()=0;
  1333. virtual bool simple_select() { return 0; }
  1334. virtual void abort() {}
  1335. /*
  1336. Cleanup instance of this class for next execution of a prepared
  1337. statement/stored procedure.
  1338. */
  1339. virtual void cleanup();
  1340. void set_thd(THD *thd_arg) { thd= thd_arg; }
  1341. #ifdef EMBEDDED_LIBRARY
  1342. virtual void begin_dataset() {}
  1343. #else
  1344. void begin_dataset() {}
  1345. #endif
  1346. };
  1347. /*
  1348. Base class for select_result descendands which intercept and
  1349. transform result set rows. As the rows are not sent to the client,
  1350. sending of result set metadata should be suppressed as well.
  1351. */
  1352. class select_result_interceptor: public select_result
  1353. {
  1354. public:
  1355. select_result_interceptor() {} /* Remove gcc warning */
  1356. uint field_count(List<Item> &fields) const { return 0; }
  1357. bool send_fields(List<Item> &fields, uint flag) { return FALSE; }
  1358. };
  1359. class select_send :public select_result {
  1360. int status;
  1361. public:
  1362. select_send() :status(0) {}
  1363. bool send_fields(List<Item> &list, uint flags);
  1364. bool send_data(List<Item> &items);
  1365. bool send_eof();
  1366. bool simple_select() { return 1; }
  1367. void abort();
  1368. };
  1369. class select_to_file :public select_result_interceptor {
  1370. protected:
  1371. sql_exchange *exchange;
  1372. File file;
  1373. IO_CACHE cache;
  1374. ha_rows row_count;
  1375. char path[FN_REFLEN];
  1376. public:
  1377. select_to_file(sql_exchange *ex) :exchange(ex), file(-1),row_count(0L)
  1378. { path[0]=0; }
  1379. ~select_to_file();
  1380. void send_error(uint errcode,const char *err);
  1381. bool send_eof();
  1382. void cleanup();
  1383. };
  1384. class select_export :public select_to_file {
  1385. uint field_term_length;
  1386. int field_sep_char,escape_char,line_sep_char;
  1387. bool fixed_row_size;
  1388. public:
  1389. select_export(sql_exchange *ex) :select_to_file(ex) {}
  1390. ~select_export();
  1391. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1392. bool send_data(List<Item> &items);
  1393. };
  1394. class select_dump :public select_to_file {
  1395. public:
  1396. select_dump(sql_exchange *ex) :select_to_file(ex) {}
  1397. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1398. bool send_data(List<Item> &items);
  1399. };
  1400. class select_insert :public select_result_interceptor {
  1401. public:
  1402. TABLE_LIST *table_list;
  1403. TABLE *table;
  1404. List<Item> *fields;
  1405. ulonglong last_insert_id;
  1406. COPY_INFO info;
  1407. bool insert_into_view;
  1408. select_insert(TABLE_LIST *table_list_par,
  1409. TABLE *table_par, List<Item> *fields_par,
  1410. List<Item> *update_fields, List<Item> *update_values,
  1411. enum_duplicates duplic, bool ignore);
  1412. ~select_insert();
  1413. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1414. int prepare2(void);
  1415. bool send_data(List<Item> &items);
  1416. virtual void store_values(List<Item> &values);
  1417. virtual bool can_rollback_data() { return 0; }
  1418. void send_error(uint errcode,const char *err);
  1419. bool send_eof();
  1420. /* not implemented: select_insert is never re-used in prepared statements */
  1421. void cleanup();
  1422. };
  1423. class select_create: public select_insert {
  1424. ORDER *group;
  1425. TABLE_LIST *create_table;
  1426. List<create_field> *extra_fields;
  1427. List<Key> *keys;
  1428. HA_CREATE_INFO *create_info;
  1429. Field **field;
  1430. public:
  1431. select_create (TABLE_LIST *table,
  1432. HA_CREATE_INFO *create_info_par,
  1433. List<create_field> &fields_par,
  1434. List<Key> &keys_par,
  1435. List<Item> &select_fields,enum_duplicates duplic, bool ignore)
  1436. :select_insert (NULL, NULL, &select_fields, 0, 0, duplic, ignore),
  1437. create_table(table), extra_fields(&fields_par),keys(&keys_par),
  1438. create_info(create_info_par)
  1439. {}
  1440. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1441. void binlog_show_create_table(TABLE **tables, uint count);
  1442. void store_values(List<Item> &values);
  1443. void send_error(uint errcode,const char *err);
  1444. bool send_eof();
  1445. void abort();
  1446. virtual bool can_rollback_data() { return 1; }
  1447. // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
  1448. THD *get_thd(void) { return thd; }
  1449. };
  1450. #include <myisam.h>
  1451. /*
  1452. Param to create temporary tables when doing SELECT:s
  1453. NOTE
  1454. This structure is copied using memcpy as a part of JOIN.
  1455. */
  1456. class TMP_TABLE_PARAM :public Sql_alloc
  1457. {
  1458. private:
  1459. /* Prevent use of these (not safe because of lists and copy_field) */
  1460. TMP_TABLE_PARAM(const TMP_TABLE_PARAM &);
  1461. void operator=(TMP_TABLE_PARAM &);
  1462. public:
  1463. List<Item> copy_funcs;
  1464. List<Item> save_copy_funcs;
  1465. Copy_field *copy_field, *copy_field_end;
  1466. Copy_field *save_copy_field, *save_copy_field_end;
  1467. byte *group_buff;
  1468. Item **items_to_copy; /* Fields in tmp table */
  1469. MI_COLUMNDEF *recinfo,*start_recinfo;
  1470. KEY *keyinfo;
  1471. ha_rows end_write_records;
  1472. uint field_count,sum_func_count,func_count;
  1473. uint hidden_field_count;
  1474. uint group_parts,group_length,group_null_parts;
  1475. uint quick_group;
  1476. bool using_indirect_summary_function;
  1477. /* If >0 convert all blob fields to varchar(convert_blob_length) */
  1478. uint convert_blob_length;
  1479. CHARSET_INFO *table_charset;
  1480. bool schema_table;
  1481. /*
  1482. True if GROUP BY and its aggregate functions are already computed
  1483. by a table access method (e.g. by loose index scan). In this case
  1484. query execution should not perform aggregation and should treat
  1485. aggregate functions as normal functions.
  1486. */
  1487. bool precomputed_group_by;
  1488. bool force_copy_fields;
  1489. TMP_TABLE_PARAM()
  1490. :copy_field(0), group_parts(0),
  1491. group_length(0), group_null_parts(0), convert_blob_length(0),
  1492. schema_table(0), precomputed_group_by(0), force_copy_fields(0)
  1493. {}
  1494. ~TMP_TABLE_PARAM()
  1495. {
  1496. cleanup();
  1497. }
  1498. void init(void);
  1499. inline void cleanup(void)
  1500. {
  1501. if (copy_field) /* Fix for Intel compiler */
  1502. {
  1503. delete [] copy_field;
  1504. save_copy_field= copy_field= 0;
  1505. }
  1506. }
  1507. };
  1508. class select_union :public select_result_interceptor
  1509. {
  1510. TMP_TABLE_PARAM tmp_table_param;
  1511. public:
  1512. TABLE *table;
  1513. select_union() :table(0) {}
  1514. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1515. bool send_data(List<Item> &items);
  1516. bool send_eof();
  1517. bool flush();
  1518. bool create_result_table(THD *thd, List<Item> *column_types,
  1519. bool is_distinct, ulonglong options,
  1520. const char *alias);
  1521. };
  1522. /* Base subselect interface class */
  1523. class select_subselect :public select_result_interceptor
  1524. {
  1525. protected:
  1526. Item_subselect *item;
  1527. public:
  1528. select_subselect(Item_subselect *item);
  1529. bool send_data(List<Item> &items)=0;
  1530. bool send_eof() { return 0; };
  1531. };
  1532. /* Single value subselect interface class */
  1533. class select_singlerow_subselect :public select_subselect
  1534. {
  1535. public:
  1536. select_singlerow_subselect(Item_subselect *item):select_subselect(item){}
  1537. bool send_data(List<Item> &items);
  1538. };
  1539. /* used in independent ALL/ANY optimisation */
  1540. class select_max_min_finder_subselect :public select_subselect
  1541. {
  1542. Item_cache *cache;
  1543. bool (select_max_min_finder_subselect::*op)();
  1544. bool fmax;
  1545. public:
  1546. select_max_min_finder_subselect(Item_subselect *item, bool mx)
  1547. :select_subselect(item), cache(0), fmax(mx)
  1548. {}
  1549. void cleanup();
  1550. bool send_data(List<Item> &items);
  1551. bool cmp_real();
  1552. bool cmp_int();
  1553. bool cmp_decimal();
  1554. bool cmp_str();
  1555. };
  1556. /* EXISTS subselect interface class */
  1557. class select_exists_subselect :public select_subselect
  1558. {
  1559. public:
  1560. select_exists_subselect(Item_subselect *item):select_subselect(item){}
  1561. bool send_data(List<Item> &items);
  1562. };
  1563. /* Structs used when sorting */
  1564. typedef struct st_sort_field {
  1565. Field *field; /* Field to sort */
  1566. Item *item; /* Item if not sorting fields */
  1567. uint length; /* Length of sort field */
  1568. uint suffix_length; /* Length suffix (0-4) */
  1569. Item_result result_type; /* Type of item */
  1570. bool reverse; /* if descending sort */
  1571. bool need_strxnfrm; /* If we have to use strxnfrm() */
  1572. } SORT_FIELD;
  1573. typedef struct st_sort_buffer {
  1574. uint index; /* 0 or 1 */
  1575. uint sort_orders;
  1576. uint change_pos; /* If sort-fields changed */
  1577. char **buff;
  1578. SORT_FIELD *sortorder;
  1579. } SORT_BUFFER;
  1580. /* Structure for db & table in sql_yacc */
  1581. class Table_ident :public Sql_alloc
  1582. {
  1583. public:
  1584. LEX_STRING db;
  1585. LEX_STRING table;
  1586. SELECT_LEX_UNIT *sel;
  1587. inline Table_ident(THD *thd, LEX_STRING db_arg, LEX_STRING table_arg,
  1588. bool force)
  1589. :table(table_arg), sel((SELECT_LEX_UNIT *)0)
  1590. {
  1591. if (!force && (thd->client_capabilities & CLIENT_NO_SCHEMA))
  1592. db.str=0;
  1593. else
  1594. db= db_arg;
  1595. }
  1596. inline Table_ident(LEX_STRING table_arg)
  1597. :table(table_arg), sel((SELECT_LEX_UNIT *)0)
  1598. {
  1599. db.str=0;
  1600. }
  1601. inline Table_ident(SELECT_LEX_UNIT *s) : sel(s)
  1602. {
  1603. /* We must have a table name here as this is used with add_table_to_list */
  1604. db.str=0; table.str= internal_table_name; table.length=1;
  1605. }
  1606. inline void change_db(char *db_name)
  1607. {
  1608. db.str= db_name; db.length= (uint) strlen(db_name);
  1609. }
  1610. };
  1611. // this is needed for user_vars hash
  1612. class user_var_entry
  1613. {
  1614. public:
  1615. user_var_entry() {} /* Remove gcc warning */
  1616. LEX_STRING name;
  1617. char *value;
  1618. ulong length;
  1619. query_id_t update_query_id, used_query_id;
  1620. Item_result type;
  1621. double val_real(my_bool *null_value);
  1622. longlong val_int(my_bool *null_value);
  1623. String *val_str(my_bool *null_value, String *str, uint decimals);
  1624. my_decimal *val_decimal(my_bool *null_value, my_decimal *result);
  1625. DTCollation collation;
  1626. };
  1627. /*
  1628. Unique -- class for unique (removing of duplicates).
  1629. Puts all values to the TREE. If the tree becomes too big,
  1630. it's dumped to the file. User can request sorted values, or
  1631. just iterate through them. In the last case tree merging is performed in
  1632. memory simultaneously with iteration, so it should be ~2-3x faster.
  1633. */
  1634. class Unique :public Sql_alloc
  1635. {
  1636. DYNAMIC_ARRAY file_ptrs;
  1637. ulong max_elements, max_in_memory_size;
  1638. IO_CACHE file;
  1639. TREE tree;
  1640. byte *record_pointers;
  1641. bool flush();
  1642. uint size;
  1643. public:
  1644. ulong elements;
  1645. Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
  1646. uint size_arg, ulong max_in_memory_size_arg);
  1647. ~Unique();
  1648. ulong elements_in_tree() { return tree.elements_in_tree; }
  1649. inline bool unique_add(void *ptr)
  1650. {
  1651. DBUG_ENTER("unique_add");
  1652. DBUG_PRINT("info", ("tree %u - %u", tree.elements_in_tree, max_elements));
  1653. if (tree.elements_in_tree > max_elements && flush())
  1654. DBUG_RETURN(1);
  1655. DBUG_RETURN(!tree_insert(&tree, ptr, 0, tree.custom_arg));
  1656. }
  1657. bool get(TABLE *table);
  1658. static double get_use_cost(uint *buffer, uint nkeys, uint key_size,
  1659. ulong max_in_memory_size);
  1660. inline static int get_cost_calc_buff_size(ulong nkeys, uint key_size,
  1661. ulong max_in_memory_size)
  1662. {
  1663. register ulong max_elems_in_tree=
  1664. (1 + max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
  1665. return sizeof(uint)*(1 + nkeys/max_elems_in_tree);
  1666. }
  1667. void reset();
  1668. bool walk(tree_walk_action action, void *walk_action_arg);
  1669. friend int unique_write_to_file(gptr key, element_count count, Unique *unique);
  1670. friend int unique_write_to_ptrs(gptr key, element_count count, Unique *unique);
  1671. };
  1672. class multi_delete :public select_result_interceptor
  1673. {
  1674. TABLE_LIST *delete_tables, *table_being_deleted;
  1675. Unique **tempfiles;
  1676. ha_rows deleted, found;
  1677. uint num_of_tables;
  1678. int error;
  1679. bool do_delete;
  1680. /* True if at least one table we delete from is transactional */
  1681. bool transactional_tables;
  1682. /* True if at least one table we delete from is not transactional */
  1683. bool normal_tables;
  1684. bool delete_while_scanning;
  1685. public:
  1686. multi_delete(TABLE_LIST *dt, uint num_of_tables);
  1687. ~multi_delete();
  1688. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1689. bool send_data(List<Item> &items);
  1690. bool initialize_tables (JOIN *join);
  1691. void send_error(uint errcode,const char *err);
  1692. int do_deletes();
  1693. bool send_eof();
  1694. };
  1695. class multi_update :public select_result_interceptor
  1696. {
  1697. TABLE_LIST *all_tables; /* query/update command tables */
  1698. TABLE_LIST *leaves; /* list of leves of join table tree */
  1699. TABLE_LIST *update_tables, *table_being_updated;
  1700. TABLE **tmp_tables, *main_table, *table_to_update;
  1701. TMP_TABLE_PARAM *tmp_table_param;
  1702. ha_rows updated, found;
  1703. List <Item> *fields, *values;
  1704. List <Item> **fields_for_table, **values_for_table;
  1705. uint table_count;
  1706. Copy_field *copy_field;
  1707. enum enum_duplicates handle_duplicates;
  1708. bool do_update, trans_safe;
  1709. /* True if the update operation has made a change in a transactional table */
  1710. bool transactional_tables;
  1711. bool ignore;
  1712. public:
  1713. multi_update(TABLE_LIST *ut, TABLE_LIST *leaves_list,
  1714. List<Item> *fields, List<Item> *values,
  1715. enum_duplicates handle_duplicates, bool ignore);
  1716. ~multi_update();
  1717. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1718. bool send_data(List<Item> &items);
  1719. bool initialize_tables (JOIN *join);
  1720. void send_error(uint errcode,const char *err);
  1721. int do_updates (bool from_send_error);
  1722. bool send_eof();
  1723. };
  1724. class my_var : public Sql_alloc {
  1725. public:
  1726. LEX_STRING s;
  1727. #ifndef DBUG_OFF
  1728. /*
  1729. Routine to which this Item_splocal belongs. Used for checking if correct
  1730. runtime context is used for variable handling.
  1731. */
  1732. sp_head *sp;
  1733. #endif
  1734. bool local;
  1735. uint offset;
  1736. enum_field_types type;
  1737. my_var (LEX_STRING& j, bool i, uint o, enum_field_types t)
  1738. :s(j), local(i), offset(o), type(t)
  1739. {}
  1740. ~my_var() {}
  1741. };
  1742. class select_dumpvar :public select_result_interceptor {
  1743. ha_rows row_count;
  1744. public:
  1745. List<my_var> var_list;
  1746. List<Item_func_set_user_var> vars;
  1747. List<Item_splocal> local_vars;
  1748. select_dumpvar(void) { var_list.empty(); local_vars.empty(); vars.empty(); row_count=0;}
  1749. ~select_dumpvar() {}
  1750. int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
  1751. bool send_data(List<Item> &items);
  1752. bool send_eof();
  1753. void cleanup();
  1754. };
  1755. /* Bits in sql_command_flags */
  1756. #define CF_CHANGES_DATA 1
  1757. #define CF_HAS_ROW_COUNT 2
  1758. #define CF_STATUS_COMMAND 4
  1759. #define CF_SHOW_TABLE_COMMAND 8
  1760. /* Functions in sql_class.cc */
  1761. void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var);
  1762. void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
  1763. STATUS_VAR *dec_var);
  1764. #endif /* MYSQL_SERVER */