Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions mysql-test/main/mdev_38752.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#
# MDEV-38752: Wrong result upon GROUP BY on a table with indexed virtual column after INSERT IGNORE
#
# Case 1: Unsafe narrowing substitution (INT -> TINYINT)
CREATE TABLE t1 (a INT PRIMARY KEY, va TINYINT AS (a), KEY(va));
INSERT IGNORE INTO t1 (a) VALUES (100),(150),(200);
Warnings:
Warning 1264 Out of range value for column 'va' at row 2
Warning 1264 Out of range value for column 'va' at row 3
# Should return 3 rows (fix: substitution disallowed)
# EXPLAIN should show that va index is NOT used
EXPLAIN SELECT a, va, COUNT(*) FROM t1 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL PRIMARY 4 NULL # Using index
Warnings:
Note 1105 Cannot substitute virtual column expression `t1`.`a` -> va due to type mismatch
SHOW WARNINGS;
Level Code Message
Note 1105 Cannot substitute virtual column expression `t1`.`a` -> va due to type mismatch
SELECT a, va, COUNT(*) FROM t1 GROUP BY a;
a va COUNT(*)
100 100 1
150 127 1
200 127 1
DROP TABLE t1;
# Case 2: Safe substitution (INT -> INT)
CREATE TABLE t2 (a INT PRIMARY KEY, va INT AS (a), KEY(va));
INSERT INTO t2 (a) VALUES (100),(150),(200);
# Should return 3 rows (substitution allowed, uses va index)
# EXPLAIN should show that va index is used
EXPLAIN SELECT va, COUNT(*) FROM t2 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index NULL va 5 NULL # Using index
SHOW WARNINGS;
Level Code Message
SELECT va, COUNT(*) FROM t2 GROUP BY a;
va COUNT(*)
100 1
150 1
200 1
DROP TABLE t2;
# Case 3: Unsafe narrowing for VARCHAR
CREATE TABLE t3 (a VARCHAR(20) PRIMARY KEY, va VARCHAR(5) AS (a), KEY(va));
INSERT IGNORE INTO t3 (a) VALUES ('apple'),('banana'),('apricot');
Warnings:
Warning 1265 Data truncated for column 'va' at row 2
Warning 1265 Data truncated for column 'va' at row 3
# 'apple' and 'apricot' both truncate to 'apple' in va
# Should return 3 rows
# EXPLAIN should show that va index is NOT used
EXPLAIN SELECT a, va, COUNT(*) FROM t3 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 index NULL PRIMARY 82 NULL # Using index
Warnings:
Note 1105 Cannot substitute virtual column expression `t3`.`a` -> va due to type mismatch
SHOW WARNINGS;
Level Code Message
Note 1105 Cannot substitute virtual column expression `t3`.`a` -> va due to type mismatch
SELECT a, va, COUNT(*) FROM t3 GROUP BY a;
a va COUNT(*)
apple apple 1
apricot apric 1
banana banan 1
DROP TABLE t3;
# Case 4: Unsafe narrowing for DECIMAL
CREATE TABLE t4 (a DECIMAL(10,5) PRIMARY KEY, va DECIMAL(10,2) AS (a), KEY(va));
INSERT IGNORE INTO t4 (a) VALUES (1.12345), (1.12346), (1.12999);
Warnings:
Note 1265 Data truncated for column 'va' at row 1
Note 1265 Data truncated for column 'va' at row 2
Note 1265 Data truncated for column 'va' at row 3
# All three values truncate to 1.12 in va
# Should return 3 rows
# EXPLAIN should show that va index is NOT used
EXPLAIN SELECT a, va, COUNT(*) FROM t4 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 index NULL PRIMARY 6 NULL # Using index
Warnings:
Note 1105 Cannot substitute virtual column expression `t4`.`a` -> va due to type mismatch
SHOW WARNINGS;
Level Code Message
Note 1105 Cannot substitute virtual column expression `t4`.`a` -> va due to type mismatch
SELECT a, va, COUNT(*) FROM t4 GROUP BY a;
a va COUNT(*)
1.12345 1.12 1
1.12346 1.12 1
1.12999 1.13 1
DROP TABLE t4;
# End of mdev_38752 tests
53 changes: 53 additions & 0 deletions mysql-test/main/mdev_38752.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--source include/have_innodb.inc

--echo #
--echo # MDEV-38752: Wrong result upon GROUP BY on a table with indexed virtual column after INSERT IGNORE
--echo #

--echo # Case 1: Unsafe narrowing substitution (INT -> TINYINT)
CREATE TABLE t1 (a INT PRIMARY KEY, va TINYINT AS (a), KEY(va));
INSERT IGNORE INTO t1 (a) VALUES (100),(150),(200);
--echo # Should return 3 rows (fix: substitution disallowed)
--echo # EXPLAIN should show that va index is NOT used
--replace_column 9 #
EXPLAIN SELECT a, va, COUNT(*) FROM t1 GROUP BY a;
SHOW WARNINGS;
SELECT a, va, COUNT(*) FROM t1 GROUP BY a;
DROP TABLE t1;

--echo # Case 2: Safe substitution (INT -> INT)
CREATE TABLE t2 (a INT PRIMARY KEY, va INT AS (a), KEY(va));
INSERT INTO t2 (a) VALUES (100),(150),(200);
--echo # Should return 3 rows (substitution allowed, uses va index)
--echo # EXPLAIN should show that va index is used
--replace_column 9 #
EXPLAIN SELECT va, COUNT(*) FROM t2 GROUP BY a;
SHOW WARNINGS;
SELECT va, COUNT(*) FROM t2 GROUP BY a;
DROP TABLE t2;

--echo # Case 3: Unsafe narrowing for VARCHAR
CREATE TABLE t3 (a VARCHAR(20) PRIMARY KEY, va VARCHAR(5) AS (a), KEY(va));
INSERT IGNORE INTO t3 (a) VALUES ('apple'),('banana'),('apricot');
--echo # 'apple' and 'apricot' both truncate to 'apple' in va
--echo # Should return 3 rows
--echo # EXPLAIN should show that va index is NOT used
--replace_column 9 #
EXPLAIN SELECT a, va, COUNT(*) FROM t3 GROUP BY a;
SHOW WARNINGS;
SELECT a, va, COUNT(*) FROM t3 GROUP BY a;
DROP TABLE t3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we customarily end tests with a print like that:

--echo End of ... tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also make sure all of the related failing tests are re-recorded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kjarir note this is --echo End of 12.3 tests rather the test name.


--echo # Case 4: Unsafe narrowing for DECIMAL
CREATE TABLE t4 (a DECIMAL(10,5) PRIMARY KEY, va DECIMAL(10,2) AS (a), KEY(va));
INSERT IGNORE INTO t4 (a) VALUES (1.12345), (1.12346), (1.12999);
--echo # All three values truncate to 1.12 in va
--echo # Should return 3 rows
--echo # EXPLAIN should show that va index is NOT used
--replace_column 9 #
EXPLAIN SELECT a, va, COUNT(*) FROM t4 GROUP BY a;
SHOW WARNINGS;
SELECT a, va, COUNT(*) FROM t4 GROUP BY a;
DROP TABLE t4;

--echo # End of mdev_38752 tests
86 changes: 67 additions & 19 deletions mysql-test/suite/vcol/r/order_by_group_by_subst.result
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ id select_type table type possible_keys key key_len ref rows Extra
explain select c + 1 from t order by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t order by vc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
explain select vc from t order by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10000 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t order by c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index
Expand All @@ -33,28 +37,40 @@ id select_type table type possible_keys key key_len ref rows Extra
explain select c from t order by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t order by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t order by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t order by vc limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2
explain delete from t order by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
alter table t add column d int;
explain update t set d = 500 order by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2 Using buffer
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain update t set d = 500 order by c limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 2 Using buffer
explain update t set c = 500 order by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2 Using buffer
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain update t set c = 500 order by c limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 2 Using buffer
Expand All @@ -66,10 +82,14 @@ add column vc int as (c + 1),
add index(vc);
explain select vc from t order by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10000 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t order by c + 1 limit 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
drop table t;
create table t (c int, key (c));
insert into t select seq from seq_1_to_10000;
Expand All @@ -84,10 +104,14 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
explain select vc2 from t order by vc1 * 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc2 5 NULL 10000 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`vc1` * 2 -> vc2 due to type mismatch
explain select vc2 from t order by vc1 * 2 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc2 5 NULL 2 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`vc1` * 2 -> vc2 due to type mismatch
drop table t;
create table t (c int, vc int generated always as (1 + 1) virtual, key (c));
insert into t values (42, default), (83, default);
Expand All @@ -108,7 +132,10 @@ add column vc2 int as (1 - c),
add index(vc1, vc2);
explain select vc1, vc2 from t order by c + 1, 1 - c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc1 10 NULL 10000 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc1 due to type mismatch
Note 1105 Cannot substitute virtual column expression 1 - `t`.`c` -> vc2 due to type mismatch
drop table t;
create table t (c int, key (c));
insert into t select seq from seq_1_to_10000;
Expand All @@ -127,12 +154,16 @@ id select_type table type possible_keys key key_len ref rows Extra
explain select c + 1 from t group by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t group by vc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
explain select vc from t group by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10000 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t group by c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index
Expand All @@ -142,12 +173,18 @@ id select_type table type possible_keys key key_len ref rows Extra
explain select c from t group by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t group by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t group by c + 1 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select c + 1 from t group by vc limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 2
Expand All @@ -159,10 +196,14 @@ add column vc int as (c + 1),
add index(vc);
explain select vc from t group by c + 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10000 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
explain select vc from t group by c + 1 limit 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc 5 NULL 10 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc due to type mismatch
drop table t;
create table t (c int, key (c));
insert into t select seq from seq_1_to_10000;
Expand All @@ -177,10 +218,14 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
explain select vc2 from t group by vc1 * 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc2 5 NULL 10000 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`vc1` * 2 -> vc2 due to type mismatch
explain select vc2 from t group by vc1 * 2 limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc2 5 NULL 2 Using index
1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`vc1` * 2 -> vc2 due to type mismatch
drop table t;
create table t (c int);
insert into t select seq from seq_1_to_10000;
Expand All @@ -191,7 +236,10 @@ add column vc2 int as (1 - c),
add index(vc1, vc2);
explain select vc1, vc2 from t group by c + 1, 1 - c;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL vc1 10 NULL 10000 Using index
1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using temporary; Using filesort
Warnings:
Note 1105 Cannot substitute virtual column expression `t`.`c` + 1 -> vc1 due to type mismatch
Note 1105 Cannot substitute virtual column expression 1 - `t`.`c` -> vc2 due to type mismatch
drop table t;
#
# MDEV-37435 Assertion `field' failed in virtual bool Item_field::fix_fields(THD *, Item **)
Expand Down
Loading
Loading