Skip to content

Commit

Permalink
MDEV-29540 Incorrect sequence values in INSERT SELECT
Browse files Browse the repository at this point in the history
The population of default values in INSERT SELECT was being
performed twice. With sequences, this resulted in every
second sequence value being used.

With SELECT INSERT we remove the second invokation of
table->update_default_fields(). This was already performed
in store_values() invoking fill_record_n_invoke_before_triggers()
which invoked update_default_fields() previously.

We do need to return an error on duplicate values, so the
::store_values is extended to take the ignore option.
  • Loading branch information
grooverdan committed Oct 18, 2022
1 parent d6707ab commit 8c38939
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 14 deletions.
102 changes: 102 additions & 0 deletions mysql-test/suite/sql_sequence/default.result
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,105 @@ INSERT INTO t1 () values ();
EXECUTE stmt;
DROP TABLE t1;
DROP SEQUENCE s;
#
# MDEV-29540 Incorrect sequence values in INSERT SELECT
#
CREATE SEQUENCE s1;
CREATE TABLE t1 (
a BIGINT UNSIGNED NOT NULL PRIMARY KEY
DEFAULT (NEXT VALUE FOR s1),
b CHAR(1) NOT NULL
);
INSERT INTO t1 (b) VALUES ('a');
INSERT INTO t1 (b) VALUES ('b'), ('c');
INSERT INTO t1 (b) VALUES ('d');
INSERT INTO t1 (b) SELECT c FROM (
SELECT 'e' as c
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;
SELECT a, b FROM t1;
a b
1 a
2 b
3 c
4 d
5 e
6 f
7 g
ALTER SEQUENCE s1 RESTART;
INSERT INTO t1 (b) SELECT c FROM (
SELECT 'a' as c
UNION
SELECT 'b'
UNION
SELECT 'c'
UNION
SELECT 'd'
UNION
SELECT 'e'
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;
ERROR 23000: Duplicate entry '1' for key 'PRIMARY'
ALTER SEQUENCE s1 RESTART;
INSERT IGNORE INTO t1 (b) SELECT c FROM (
SELECT 'a' as c
UNION
SELECT 'b'
UNION
SELECT 'c'
UNION
SELECT 'd'
UNION
SELECT 'e'
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;
Warnings:
Warning 1062 Duplicate entry '1' for key 'PRIMARY'
Warning 1062 Duplicate entry '2' for key 'PRIMARY'
Warning 1062 Duplicate entry '3' for key 'PRIMARY'
Warning 1062 Duplicate entry '4' for key 'PRIMARY'
Warning 1062 Duplicate entry '5' for key 'PRIMARY'
Warning 1062 Duplicate entry '6' for key 'PRIMARY'
Warning 1062 Duplicate entry '7' for key 'PRIMARY'
SELECT a, b FROM t1;
a b
1 a
2 b
3 c
4 d
5 e
6 f
7 g
INSERT IGNORE INTO t1 (b) SELECT c FROM (
SELECT 'h' as c
UNION
SELECT 'i'
UNION
SELECT 'j'
) der;
SELECT a, b FROM t1;
a b
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
DROP TABLE t1;
DROP SEQUENCE s1;
#
# End of 10.3 tests
#
80 changes: 80 additions & 0 deletions mysql-test/suite/sql_sequence/default.test
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,83 @@ EXECUTE stmt;
# Cleanup
DROP TABLE t1;
DROP SEQUENCE s;

--echo #
--echo # MDEV-29540 Incorrect sequence values in INSERT SELECT
--echo #

CREATE SEQUENCE s1;
CREATE TABLE t1 (
a BIGINT UNSIGNED NOT NULL PRIMARY KEY
DEFAULT (NEXT VALUE FOR s1),
b CHAR(1) NOT NULL
);

INSERT INTO t1 (b) VALUES ('a');
INSERT INTO t1 (b) VALUES ('b'), ('c');
INSERT INTO t1 (b) VALUES ('d');
INSERT INTO t1 (b) SELECT c FROM (
SELECT 'e' as c
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;

SELECT a, b FROM t1;

ALTER SEQUENCE s1 RESTART;

--error ER_DUP_ENTRY
INSERT INTO t1 (b) SELECT c FROM (
SELECT 'a' as c
UNION
SELECT 'b'
UNION
SELECT 'c'
UNION
SELECT 'd'
UNION
SELECT 'e'
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;

ALTER SEQUENCE s1 RESTART;

INSERT IGNORE INTO t1 (b) SELECT c FROM (
SELECT 'a' as c
UNION
SELECT 'b'
UNION
SELECT 'c'
UNION
SELECT 'd'
UNION
SELECT 'e'
UNION
SELECT 'f'
UNION
SELECT 'g'
) der;

SELECT a, b FROM t1;

INSERT IGNORE INTO t1 (b) SELECT c FROM (
SELECT 'h' as c
UNION
SELECT 'i'
UNION
SELECT 'j'
) der;

SELECT a, b FROM t1;

DROP TABLE t1;
DROP SEQUENCE s1;

--echo #
--echo # End of 10.3 tests
--echo #
4 changes: 2 additions & 2 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -5455,7 +5455,7 @@ class select_insert :public select_result_interceptor {
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
virtual int prepare2(JOIN *join);
virtual int send_data(List<Item> &items);
virtual void store_values(List<Item> &values);
virtual bool store_values(List<Item> &values, bool ignore_errors);
virtual bool can_rollback_data() { return 0; }
bool prepare_eof();
bool send_ok_packet();
Expand Down Expand Up @@ -5497,7 +5497,7 @@ class select_create: public select_insert {
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);

int binlog_show_create_table(TABLE **tables, uint count);
void store_values(List<Item> &values);
bool store_values(List<Item> &values, bool ignore_errors);
bool send_eof();
virtual void abort_result_set();
virtual bool can_rollback_data() { return 1; }
Expand Down
23 changes: 11 additions & 12 deletions sql/sql_insert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3928,9 +3928,7 @@ int select_insert::send_data(List<Item> &values)
DBUG_RETURN(0);

thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
store_values(values);
if (table->default_field &&
unlikely(table->update_default_fields(info.ignore)))
if (store_values(values, info.ignore))
DBUG_RETURN(1);
thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
if (unlikely(thd->is_error()))
Expand Down Expand Up @@ -3988,18 +3986,19 @@ int select_insert::send_data(List<Item> &values)
}


void select_insert::store_values(List<Item> &values)
bool select_insert::store_values(List<Item> &values, bool ignore_errors)
{
DBUG_ENTER("select_insert::store_values");
bool error;

if (fields->elements)
fill_record_n_invoke_before_triggers(thd, table, *fields, values, 1,
TRG_EVENT_INSERT);
error= fill_record_n_invoke_before_triggers(thd, table, *fields, values,
ignore_errors, TRG_EVENT_INSERT);
else
fill_record_n_invoke_before_triggers(thd, table, table->field_to_fill(),
values, 1, TRG_EVENT_INSERT);
error= fill_record_n_invoke_before_triggers(thd, table, table->field_to_fill(),
values, ignore_errors, TRG_EVENT_INSERT);

DBUG_VOID_RETURN;
DBUG_RETURN(error);
}

bool select_insert::prepare_eof()
Expand Down Expand Up @@ -4670,10 +4669,10 @@ select_create::binlog_show_create_table(TABLE **tables, uint count)
return result;
}

void select_create::store_values(List<Item> &values)
bool select_create::store_values(List<Item> &values, bool ignore_errors)
{
fill_record_n_invoke_before_triggers(thd, table, field, values, 1,
TRG_EVENT_INSERT);
return fill_record_n_invoke_before_triggers(thd, table, field, values,
ignore_errors, TRG_EVENT_INSERT);
}


Expand Down

0 comments on commit 8c38939

Please sign in to comment.