From 2d5db535847e431c13ec930cde98e5387bcfd28c Mon Sep 17 00:00:00 2001 From: Monty Date: Sun, 14 Sep 2025 19:08:25 +0300 Subject: [PATCH] Simplify NEXTVAL(sequence) when used with DEFAULT Instead of adding another TABLE_LIST to Item_func_nextval->table_list->next_local, update instead Item_func_nextval->table_list->table with the correct table. This removes all checking of table_list->table and table_list->next_local when using sequences. --- sql/item_func.h | 12 +++--------- sql/sql_base.cc | 13 +++++++++---- sql/table.h | 1 + 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/sql/item_func.h b/sql/item_func.h index 90755f1e3d4..01a2a20cbd8 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -4448,7 +4448,7 @@ protected: bool check_access(THD *, privilege_t); public: Item_func_nextval(THD *thd, TABLE_LIST *table_list_arg): - Item_longlong_func(thd), table_list(table_list_arg) {} + Item_longlong_func(thd), table_list(table_list_arg), table(0) {} longlong val_int() override; LEX_CSTRING func_name_cstring() const override { @@ -4478,14 +4478,8 @@ public: */ void update_table() { - if (!(table= table_list->table)) - { - /* - If nextval was used in DEFAULT then next_local points to - the table_list used by to open the sequence table - */ - table= table_list->next_local->table; - } + table= table_list->table; + DBUG_ASSERT(table); } bool const_item() const override { return 0; } Item *do_get_copy(THD *thd) const override diff --git a/sql/sql_base.cc b/sql/sql_base.cc index befdb2a0820..37aaf8a2660 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2446,6 +2446,11 @@ retry_share: DBUG_ASSERT(table->file->pushed_cond == NULL); table_list->updatable= 1; // It is not derived table nor non-updatable VIEW table_list->table= table; + if (table_list->linked_table) + { + /* Update link for sequence tables in default */ + table_list->linked_table->table= table; + } if (!from_share && table->vcol_fix_expr(thd)) DBUG_RETURN(true); @@ -5068,7 +5073,7 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx, next_local value as it may have been changed by a previous statement using the same table. */ - tables->next_local= tmp; + tmp->linked_table= tables; continue; } @@ -5083,10 +5088,10 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx, &prelocking_ctx->query_tables_last, tables->for_insert_data); /* - Store link to the new table_list that will be used by open so that - Item_func_nextval() can find it + Store link to the sequences table so that we can in open_table() update + it to point to the opened table. */ - tables->next_local= tl; + tl->linked_table= tables; DBUG_PRINT("info", ("table name: %s added", tables->table_name.str)); } while ((tables= tables->next_global)); DBUG_RETURN(FALSE); diff --git a/sql/table.h b/sql/table.h index 8c466938505..f43b7794a0d 100644 --- a/sql/table.h +++ b/sql/table.h @@ -2581,6 +2581,7 @@ struct TABLE_LIST TABLE_LIST *next_local; /* link in a global list of all queries tables */ TABLE_LIST *next_global, **prev_global; + TABLE_LIST *linked_table; // For sequence tables used in default Lex_ident_db db; Lex_ident_table table_name; Lex_ident_i_s_table schema_table_name;