Skip to content

Commit 2d5db53

Browse files
committed
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.
1 parent 93ba9df commit 2d5db53

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

sql/item_func.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4448,7 +4448,7 @@ class Item_func_nextval :public Item_longlong_func
44484448
bool check_access(THD *, privilege_t);
44494449
public:
44504450
Item_func_nextval(THD *thd, TABLE_LIST *table_list_arg):
4451-
Item_longlong_func(thd), table_list(table_list_arg) {}
4451+
Item_longlong_func(thd), table_list(table_list_arg), table(0) {}
44524452
longlong val_int() override;
44534453
LEX_CSTRING func_name_cstring() const override
44544454
{
@@ -4478,14 +4478,8 @@ class Item_func_nextval :public Item_longlong_func
44784478
*/
44794479
void update_table()
44804480
{
4481-
if (!(table= table_list->table))
4482-
{
4483-
/*
4484-
If nextval was used in DEFAULT then next_local points to
4485-
the table_list used by to open the sequence table
4486-
*/
4487-
table= table_list->next_local->table;
4488-
}
4481+
table= table_list->table;
4482+
DBUG_ASSERT(table);
44894483
}
44904484
bool const_item() const override { return 0; }
44914485
Item *do_get_copy(THD *thd) const override

sql/sql_base.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,11 @@ bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
24462446
DBUG_ASSERT(table->file->pushed_cond == NULL);
24472447
table_list->updatable= 1; // It is not derived table nor non-updatable VIEW
24482448
table_list->table= table;
2449+
if (table_list->linked_table)
2450+
{
2451+
/* Update link for sequence tables in default */
2452+
table_list->linked_table->table= table;
2453+
}
24492454

24502455
if (!from_share && table->vcol_fix_expr(thd))
24512456
DBUG_RETURN(true);
@@ -5068,7 +5073,7 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx,
50685073
next_local value as it may have been changed by a previous
50695074
statement using the same table.
50705075
*/
5071-
tables->next_local= tmp;
5076+
tmp->linked_table= tables;
50725077
continue;
50735078
}
50745079

@@ -5083,10 +5088,10 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx,
50835088
&prelocking_ctx->query_tables_last,
50845089
tables->for_insert_data);
50855090
/*
5086-
Store link to the new table_list that will be used by open so that
5087-
Item_func_nextval() can find it
5091+
Store link to the sequences table so that we can in open_table() update
5092+
it to point to the opened table.
50885093
*/
5089-
tables->next_local= tl;
5094+
tl->linked_table= tables;
50905095
DBUG_PRINT("info", ("table name: %s added", tables->table_name.str));
50915096
} while ((tables= tables->next_global));
50925097
DBUG_RETURN(FALSE);

sql/table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,7 @@ struct TABLE_LIST
25812581
TABLE_LIST *next_local;
25822582
/* link in a global list of all queries tables */
25832583
TABLE_LIST *next_global, **prev_global;
2584+
TABLE_LIST *linked_table; // For sequence tables used in default
25842585
Lex_ident_db db;
25852586
Lex_ident_table table_name;
25862587
Lex_ident_i_s_table schema_table_name;

0 commit comments

Comments
 (0)