From dc719597ee0b11da722e9813639e8b48018a8c10 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Mon, 26 Aug 2019 15:28:32 +0400 Subject: MDEV-18156 Assertion `0' failed or `btr_validate_index(index, 0, false)' in row_upd_sec_index_entry or error code 126: Index is corrupted upon DELETE with PAD_CHAR_TO_FULL_LENGTH This change takes into account a column's GENERATED ALWAYS AS expression dependcy on sql_mode's PAD_CHAR_TO_FULL_LENGTH and NO_UNSIGNED_SUBTRACTION flags. Indexed virtual columns as well as persistent generated columns are now not allowed to have such dependencies to avoid inconsistent data or index files on sql_mode changes. So an error is now returned in cases like this: CREATE OR REPLACE TABLE t1 ( a CHAR(5), v VARCHAR(5) AS (a) PERSISTENT -- CHAR->VARCHAR or CHAR->TEXT = ERROR ); Functions RPAD() and RTRIM() can now remove dependency on PAD_CHAR_TO_FULL_LENGTH. So this can be used instead: CREATE OR REPLACE TABLE t1 ( a CHAR(5), v VARCHAR(5) AS (RTRIM(a)) PERSISTENT ); Note, unlike CHAR->VARCHAR and CHAR->TEXT this still works, not RPAD(a) is needed: CREATE OR REPLACE TABLE t1 ( a CHAR(5), v CHAR(5) AS (a) PERSISTENT -- CHAR->CHAR is OK ); More sql_mode flags may affect values of generated columns. They will be addressed separately. See comments in sql_mode.h for implementation details. --- sql/item.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'sql/item.h') diff --git a/sql/item.h b/sql/item.h index 2239544dc88..ff4cfd6c1b8 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1216,6 +1216,32 @@ public: double val_real_from_decimal(); double val_real_from_date(); + /* + Returns true if this item can be calculated during + value_depends_on_sql_mode() + */ + bool value_depends_on_sql_mode_const_item() + { + /* + Currently we use value_depends_on_sql_mode() only for virtual + column expressions. They should not contain any expensive items. + If we ever get a crash on the assert below, it means + check_vcol_func_processor() is badly implemented for this item. + */ + DBUG_ASSERT(!is_expensive()); + /* + It should return const_item() actually. + But for some reasons Item_field::const_item() returns true + at value_depends_on_sql_mode() call time. + This should be checked and fixed. + */ + return basic_const_item(); + } + virtual Sql_mode_dependency value_depends_on_sql_mode() const + { + return Sql_mode_dependency(); + } + // Get TIME, DATE or DATETIME using proper sql_mode flags for the field type bool get_temporal_with_sql_mode(MYSQL_TIME *ltime); // Check NULL value for a TIME, DATE or DATETIME expression @@ -2694,6 +2720,10 @@ public: { return MONOTONIC_STRICT_INCREASING; } + Sql_mode_dependency value_depends_on_sql_mode() const + { + return Sql_mode_dependency(0, field->value_depends_on_sql_mode()); + } longlong val_int_endpoint(bool left_endp, bool *incl_endp); bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate); bool get_date_result(MYSQL_TIME *ltime,ulonglong fuzzydate); @@ -4102,6 +4132,7 @@ public: inline Item **arguments() const { return args; } inline uint argument_count() const { return arg_count; } inline void remove_arguments() { arg_count=0; } + Sql_mode_dependency value_depends_on_sql_mode_bit_or() const; }; @@ -4334,6 +4365,10 @@ public: bool const_item() const { return const_item_cache; } table_map used_tables() const { return used_tables_cache; } Item* build_clone(THD *thd, MEM_ROOT *mem_root); + Sql_mode_dependency value_depends_on_sql_mode() const + { + return Item_args::value_depends_on_sql_mode_bit_or().soft_to_hard(); + } }; -- cgit v1.2.1