Skip to content

Commit 72341bc

Browse files
montywivuvova
authored andcommitted
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 516f68a commit 72341bc

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
@@ -4241,7 +4241,7 @@ class Item_func_nextval :public Item_longlong_func
42414241
bool check_access(THD *, privilege_t);
42424242
public:
42434243
Item_func_nextval(THD *thd, TABLE_LIST *table_list_arg):
4244-
Item_longlong_func(thd), table_list(table_list_arg) {}
4244+
Item_longlong_func(thd), table_list(table_list_arg), table(0) {}
42454245
longlong val_int() override;
42464246
LEX_CSTRING func_name_cstring() const override
42474247
{
@@ -4270,14 +4270,8 @@ class Item_func_nextval :public Item_longlong_func
42704270
*/
42714271
void update_table()
42724272
{
4273-
if (!(table= table_list->table))
4274-
{
4275-
/*
4276-
If nextval was used in DEFAULT then next_local points to
4277-
the table_list used by to open the sequence table
4278-
*/
4279-
table= table_list->next_local->table;
4280-
}
4273+
table= table_list->table;
4274+
DBUG_ASSERT(table);
42814275
}
42824276
bool const_item() const override { return 0; }
42834277
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
@@ -2423,6 +2423,11 @@ bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
24232423
DBUG_ASSERT(table->file->pushed_cond == NULL);
24242424
table_list->updatable= 1; // It is not derived table nor non-updatable VIEW
24252425
table_list->table= table;
2426+
if (table_list->linked_table)
2427+
{
2428+
/* Update link for sequence tables in default */
2429+
table_list->linked_table->table= table;
2430+
}
24262431

24272432
if (!from_share && table->vcol_fix_expr(thd))
24282433
DBUG_RETURN(true);
@@ -5052,7 +5057,7 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx,
50525057
next_local value as it may have been changed by a previous
50535058
statement using the same table.
50545059
*/
5055-
tables->next_local= tmp;
5060+
tmp->linked_table= tables;
50565061
continue;
50575062
}
50585063

@@ -5067,10 +5072,10 @@ add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx,
50675072
&prelocking_ctx->query_tables_last,
50685073
tables->for_insert_data);
50695074
/*
5070-
Store link to the new table_list that will be used by open so that
5071-
Item_func_nextval() can find it
5075+
Store link to the sequences table so that we can in open_table() update
5076+
it to point to the opened table.
50725077
*/
5073-
tables->next_local= tl;
5078+
tl->linked_table= tables;
50745079
DBUG_PRINT("info", ("table name: %s added", tables->table_name.str));
50755080
} while ((tables= tables->next_global));
50765081
DBUG_RETURN(FALSE);

sql/table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,6 +2414,7 @@ struct TABLE_LIST
24142414
TABLE_LIST *next_local;
24152415
/* link in a global list of all queries tables */
24162416
TABLE_LIST *next_global, **prev_global;
2417+
TABLE_LIST *linked_table; // For sequence tables used in default
24172418
LEX_CSTRING db;
24182419
LEX_CSTRING table_name;
24192420
LEX_CSTRING schema_table_name;

0 commit comments

Comments
 (0)