#
# MDEV-38752 Wrong result upon GROUP BY on a table with indexed virtual column after INSERT IGNORE
#
# Type_handler_real_result
## float(4,2), double(4,2)
CREATE TABLE t (a float(4,2), b double(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(33.13);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-33.12	1
-33.11	1
33.13	1
SELECT * FROM t;
a	b
-33.11	-33.11
-33.12	-33.12
33.13	33.13
DROP TABLE t;
## float(4,2), float(5,3) unsigned
CREATE TABLE t (a float(4,2), b float(5,3) unsigned AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(0.00);
Warnings:
Warning	1264	Out of range value for column 'b' at row 1
Warning	1264	Out of range value for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-33.12	1
-33.11	1
0.00	1
SELECT * FROM t;
a	b
-33.11	0.000
-33.12	0.000
0.00	0.000
DROP TABLE t;
## float(5,3), float(4,2)
CREATE TABLE t (a float(5,3), b float(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.111),(33.112);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.111	1
33.112	1
SELECT * FROM t;
a	b
33.111	33.11
33.112	33.11
DROP TABLE t;
## float(4,2) < float(5,3)
CREATE TABLE t (a float(4,2), b float(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(-33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
-33.12	1
-33.11	2
SELECT * FROM t;
a	b
-33.11	-33.110
-33.12	-33.120
-33.11	-33.110
DROP TABLE t;
## float(4,2) unsigned < float(5,3) unsigned
CREATE TABLE t (a float(4,2), b float(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.12),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
33.11	2
33.12	1
SELECT * FROM t;
a	b
33.11	33.110
33.12	33.120
33.11	33.110
DROP TABLE t;
## float(4,2) unsigned < float(4,2)
CREATE TABLE t (a float(4,2) unsigned, b float(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.22),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
33.11	2
33.22	1
SELECT * FROM t;
a	b
33.11	33.11
33.22	33.22
33.11	33.11
DROP TABLE t;
## float(4,2) zerofill < float(4,2)
CREATE TABLE t (a float(4,2) zerofill, b float(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (3.1),(3.2),(3.1);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
3.10	2
3.20	1
SELECT * FROM t;
a	b
3.10	3.10
3.20	3.20
3.10	3.10
DROP TABLE t;
## double(4,2), double(5,3) unsigned
CREATE TABLE t (a double(4,2), b double(5,3) unsigned AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(0.00);
Warnings:
Warning	1264	Out of range value for column 'b' at row 1
Warning	1264	Out of range value for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-33.12	1
-33.11	1
0.00	1
SELECT * FROM t;
a	b
-33.11	0.000
-33.12	0.000
0.00	0.000
DROP TABLE t;
## double(5,3), double(4,2)
CREATE TABLE t (a double(5,3), b double(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.111),(33.112);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.111	1
33.112	1
SELECT * FROM t;
a	b
33.111	33.11
33.112	33.11
DROP TABLE t;
## double(4,2) < double(5,3)
CREATE TABLE t (a double(4,2), b double(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(-33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	9	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
-33.12	1
-33.11	2
SELECT * FROM t;
a	b
-33.11	-33.110
-33.12	-33.120
-33.11	-33.110
DROP TABLE t;
## double(4,2) unsigned < double(5,3) unsigned
CREATE TABLE t (a double(4,2), b double(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.12),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	9	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
33.11	2
33.12	1
SELECT * FROM t;
a	b
33.11	33.110
33.12	33.120
33.11	33.110
DROP TABLE t;
## double(4,2) unsigned < double(4,2)
CREATE TABLE t (a double(4,2) unsigned, b double(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.22),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	9	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
33.11	2
33.22	1
SELECT * FROM t;
a	b
33.11	33.11
33.22	33.22
33.11	33.11
DROP TABLE t;
## double(4,2) zerofill < double(4,2)
CREATE TABLE t (a double(4,2) zerofill, b double(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (3.1),(3.2),(3.1);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	9	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
3.10	2
3.20	1
SELECT * FROM t;
a	b
3.10	3.10
3.20	3.20
3.10	3.10
DROP TABLE t;
# Type_handler_decimal_result
## decimal(4,2), decimal(5,3) unsigned
CREATE TABLE t (a decimal(4,2), b decimal(5,3) unsigned AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(0.00);
Warnings:
Warning	1264	Out of range value for column 'b' at row 1
Warning	1264	Out of range value for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-33.12	1
-33.11	1
0.00	1
SELECT * FROM t;
a	b
-33.11	0.000
-33.12	0.000
0.00	0.000
DROP TABLE t;
## decimal(5,3), decimal(4,2)
CREATE TABLE t (a decimal(5,3), b decimal(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.111),(33.112);
Warnings:
Note	1265	Data truncated for column 'b' at row 1
Note	1265	Data truncated for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.111	1
33.112	1
SELECT * FROM t;
a	b
33.111	33.11
33.112	33.11
DROP TABLE t;
## decimal(4,2) < decimal(5,3)
CREATE TABLE t (a decimal(4,2), b decimal(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (-33.11),(-33.12),(-33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	4	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
-33.12	1
-33.11	2
SELECT * FROM t;
a	b
-33.11	-33.110
-33.12	-33.120
-33.11	-33.110
DROP TABLE t;
## decimal(4,2) unsigned, decimal(5,3) unsigned
CREATE TABLE t (a decimal(4,2), b decimal(5,3) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.12),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.11	2
33.12	1
SELECT * FROM t;
a	b
33.11	33.110
33.12	33.120
33.11	33.110
DROP TABLE t;
## decimal(4,2) unsigned < decimal(4,2)
CREATE TABLE t (a decimal(4,2) unsigned, b decimal(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.22),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	3	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
33.11	2
33.22	1
SELECT * FROM t;
a	b
33.11	33.11
33.22	33.22
33.11	33.11
DROP TABLE t;
## decimal(4,2) unsigned, decimal(3,2)
CREATE TABLE t (a decimal(4,2) unsigned, b decimal(3,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.22),(33.11);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.11	2
33.22	1
SELECT * FROM t;
a	b
33.11	3.11
33.22	3.22
33.11	3.11
DROP TABLE t;
## decimal(4,2) zerofill < decimal(4,2)
CREATE TABLE t (a decimal(4,2) zerofill, b decimal(4,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (3.1),(3.2),(3.1);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	3	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
03.10	2
03.20	1
SELECT * FROM t;
a	b
03.10	3.10
03.20	3.20
03.10	3.10
DROP TABLE t;
## decimal(4,2) zerofill, decimal(3,2)
CREATE TABLE t (a decimal(4,2) zerofill, b decimal(3,2) AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (3.1),(3.2),(3.1);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
03.10	2
03.20	1
SELECT * FROM t;
a	b
03.10	3.10
03.20	3.20
03.10	3.10
DROP TABLE t;
# Type_handler_general_purpose_int
## decimal, int
CREATE TABLE t (a decimal(6,3), b int AS (a), KEY(b));
INSERT IGNORE INTO t (a) VALUES (33.11),(33.222);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
33.110	1
33.222	1
SELECT * FROM t;
a	b
33.110	33
33.222	33
DROP TABLE t;
## int, tinyint
CREATE TABLE t (a INT, va TINYINT AS (a), KEY(va));
INSERT IGNORE INTO t (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
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
100	1
150	1
200	1
SELECT * FROM t;
a	va
100	100
150	127
200	127
DROP TABLE t;
## tinyint < int
CREATE TABLE t (a TINYINT, va INT AS (a), KEY(va));
INSERT INTO t (a) VALUES (100),(101),(100);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	va	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
100	2
101	1
SELECT * FROM t;
a	va
100	100
101	101
100	100
DROP TABLE t;
## int unsigned, tinyint unsigned
CREATE TABLE t (a INT UNSIGNED, va TINYINT UNSIGNED AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (200),(300),(400);
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
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
200	1
300	1
400	1
SELECT * FROM t;
a	va
200	200
300	255
400	255
DROP TABLE t;
## tinyint unsigned < int unsigned
CREATE TABLE t (a TINYINT UNSIGNED, va INT UNSIGNED AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (200),(201),(202);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	va	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
200	1
201	1
SELECT * FROM t;
a	va
200	200
201	201
202	202
DROP TABLE t;
## tinyint, int unsigned
CREATE TABLE t (a TINYINT, va INT UNSIGNED AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (-3),(-4),(100);
Warnings:
Warning	1264	Out of range value for column 'va' at row 1
Warning	1264	Out of range value for column 'va' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-4	1
-3	1
100	1
SELECT * FROM t;
a	va
-3	0
-4	0
100	100
DROP TABLE t;
## bigint unsigned, bigint
CREATE TABLE t (a BIGINT UNSIGNED, va BIGINT AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (123456789), (12345678901234567890), (12345678901234567891);
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
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
123456789	1
12345678901234567890	1
12345678901234567891	1
SELECT * FROM t;
a	va
123456789	123456789
12345678901234567890	9223372036854775807
12345678901234567891	9223372036854775807
DROP TABLE t;
## int unsigned, bigint
CREATE TABLE t (a INT UNSIGNED, va BIGINT AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (4123456789),(4223456789),(3123456789);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
3123456789	1
4123456789	1
4223456789	1
SELECT * FROM t;
a	va
4123456789	4123456789
4223456789	4223456789
3123456789	3123456789
DROP TABLE t;
## tinyint + 2, tinyint
CREATE TABLE t (a TINYINT, va TINYINT AS (a + 2), KEY(va));
INSERT IGNORE INTO t (a) VALUES (126),(127);
Warnings:
Warning	1264	Out of range value for column 'va' at row 1
Warning	1264	Out of range value for column 'va' at row 2
explain
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` + 2 -> va due to failed supertype check
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
a + 2	COUNT(*)
128	1
129	1
SELECT a, a + 2, va FROM t;
a	a + 2	va
126	128	127
127	129	127
DROP TABLE t;
## tinyint * tinyint, mediumint
CREATE TABLE t (a TINYINT, va MEDIUMINT AS (a * a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (-20),(19);
explain
SELECT a * a, COUNT(*) FROM t GROUP BY a * a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` * `t`.`a` -> va due to failed supertype check
SELECT a * a, COUNT(*) FROM t GROUP BY a * a;
a * a	COUNT(*)
361	1
400	1
SELECT a, a * a, va FROM t;
a	a * a	va
-20	400	400
19	361	361
DROP TABLE t;
## tinyint ** 4, mediumint
CREATE TABLE t (a TINYINT, va MEDIUMINT AS (a * a * a * a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (-101),(100);
Warnings:
Warning	1264	Out of range value for column 'va' at row 1
Warning	1264	Out of range value for column 'va' at row 2
explain
SELECT a * a * a * a, COUNT(*) FROM t GROUP BY a * a * a * a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` * `t`.`a` * `t`.`a` * `t`.`a` -> va due to failed supertype check
SELECT a * a * a * a, COUNT(*) FROM t GROUP BY a * a * a * a;
a * a * a * a	COUNT(*)
100000000	1
104060401	1
SELECT a, a * a * a * a, va FROM t;
a	a * a * a * a	va
-101	104060401	8388607
100	100000000	8388607
DROP TABLE t;
# Type_handler_year
## smallint, year
CREATE TABLE t (a SMALLINT, va YEAR AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (1999),(2000),(1999);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
1999	2
2000	1
SELECT * FROM t;
a	va
1999	1999
2000	2000
1999	1999
DROP TABLE t;
## year(2), year(4)
CREATE TABLE t (a YEAR(2), va YEAR(4) AS (a), KEY(va));
Warnings:
Warning	1287	'YEAR(2)' is deprecated and will be removed in a future release. Please use 'YEAR(4)' instead
INSERT IGNORE INTO t (a) VALUES (20),(00),(20);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
00	1
20	2
SELECT * FROM t;
a	va
20	2020
00	2000
20	2020
DROP TABLE t;
## year(4), year(2)
CREATE TABLE t (a YEAR(4), va YEAR(2) AS (a), KEY(va));
Warnings:
Warning	1287	'YEAR(2)' is deprecated and will be removed in a future release. Please use 'YEAR(4)' instead
INSERT IGNORE INTO t (a) VALUES (2000),(0000),(2000);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
0000	1
2000	2
SELECT * FROM t;
a	va
2000	00
0000	00
2000	00
DROP TABLE t;
## year(4) + 2, year(4)
CREATE TABLE t (a YEAR, va YEAR AS (a + 2), KEY(va));
INSERT IGNORE INTO t (a) VALUES (2000),(0000),(2154),(2155);
Warnings:
Warning	1264	Out of range value for column 'va' at row 3
Warning	1264	Out of range value for column 'va' at row 4
explain
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	4	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` + 2 -> va due to failed supertype check
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
a + 2	COUNT(*)
2	1
2002	1
2156	1
2157	1
SELECT a, a + 2, va FROM t;
a	a + 2	va
2000	2002	2002
0000	2	2002
2154	2156	0000
2155	2157	0000
DROP TABLE t;
## year(2) + 2, year(2)
CREATE TABLE t (a YEAR(2), va YEAR(2) AS (a + 2), KEY(va));
Warnings:
Warning	1287	'YEAR(2)' is deprecated and will be removed in a future release. Please use 'YEAR(4)' instead
Warning	1287	'YEAR(2)' is deprecated and will be removed in a future release. Please use 'YEAR(4)' instead
INSERT IGNORE INTO t (a) VALUES (00),(69),(68);
explain
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` + 2 -> va due to failed supertype check
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
a + 2	COUNT(*)
2	1
70	1
71	1
SELECT a, a + 2, va FROM t;
a	a + 2	va
00	2	02
69	71	71
68	70	70
DROP TABLE t;
## year(2) + 2, year(4)
CREATE TABLE t (a YEAR(2), va YEAR AS (a + 2), KEY(va));
Warnings:
Warning	1287	'YEAR(2)' is deprecated and will be removed in a future release. Please use 'YEAR(4)' instead
INSERT IGNORE INTO t (a) VALUES (00),(69),(68);
explain
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` + 2 -> va due to failed supertype check
SELECT a + 2, COUNT(*) FROM t GROUP BY a + 2;
a + 2	COUNT(*)
2	1
70	1
71	1
SELECT a, a + 2, va FROM t;
a	a + 2	va
00	2	2002
69	71	1971
68	70	1970
DROP TABLE t;
# Type_handler_bit
## tinyint unsigned, bit(8)
CREATE TABLE t (a tinyint unsigned, va bit(8) AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (101),(100),(101);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
100	1
101	2
SELECT * FROM t;
a	va
101	e
100	d
101	e
DROP TABLE t;
## bit(7) < bit(8)
CREATE TABLE t (a bit(7), va bit(8) AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (101),(100),(101);
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	va	2	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
e	1
e	2
SELECT * FROM t;
a	va
e	e
d	d
e	e
DROP TABLE t;
## bit(7), bit(6)
CREATE TABLE t (a bit(7), va bit(6) AS (a), KEY(va));
INSERT IGNORE INTO t (a) VALUES (101),(100),(101);
Warnings:
Warning	1264	Out of range value for column 'va' at row 1
Warning	1264	Out of range value for column 'va' at row 2
Warning	1264	Out of range value for column 'va' at row 3
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
d	1
e	2
SELECT * FROM t;
a	va
e	?
d	?
e	?
DROP TABLE t;
# Type_handler_time_common
## datetime, time
CREATE TABLE t (a datetime, b time as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-03-01 12:00');
Warnings:
Note	1265	Data truncated for column 'b' at row 1
Note	1265	Data truncated for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
2026-01-01 12:00:00	1
2026-03-01 12:00:00	1
SELECT * FROM t;
a	b
2026-01-01 12:00:00	12:00:00
2026-03-01 12:00:00	12:00:00
DROP TABLE t;
## time(3), time(2)
## table creation with index not allowed
CREATE TABLE t (a time(3), b time(2) as (a));
drop table t;
CREATE TABLE t (a time(3), b time(2) as (a), key (b));
ERROR HY000: Function or expression '`a`' cannot be used in the GENERATED ALWAYS AS clause of `b`
## time(2) < time(3)
CREATE TABLE t (a time(2), b time(3) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('12:00.00'),('16:00.00'),('12:00.00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	6	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
12:00:00.00	2
16:00:00.00	1
SELECT * FROM t;
a	b
12:00:00.00	12:00:00.000
16:00:00.00	16:00:00.000
12:00:00.00	12:00:00.000
DROP TABLE t;
# Type_handler_date_common
## datetime, date
CREATE TABLE t (a datetime, b date as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00');
Warnings:
Note	1265	Data truncated for column 'b' at row 1
Note	1265	Data truncated for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
2026-01-01 12:00:00	1
2026-01-01 16:00:00	1
SELECT * FROM t;
a	b
2026-01-01 12:00:00	2026-01-01
2026-01-01 16:00:00	2026-01-01
DROP TABLE t;
## date +/- 1, date
CREATE TABLE t (a date, b date as (a + 1), c date as (a - 1), key(b), key(c));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01'),('9999-12-31'),('0000-00-00');
Warnings:
Warning	1265	Data truncated for column 'b' at row 2
Warning	1265	Data truncated for column 'b' at row 3
Warning	1265	Data truncated for column 'c' at row 3
explain
SELECT a + 1, COUNT(*) FROM t GROUP BY a + 1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` + 1 -> b due to failed supertype check
SELECT a + 1, COUNT(*) FROM t GROUP BY a + 1;
a + 1	COUNT(*)
1	1
20260102	1
99991232	1
explain
SELECT a - 1, COUNT(*) FROM t GROUP BY a - 1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` - 1 -> c due to failed supertype check
SELECT a - 1, COUNT(*) FROM t GROUP BY a - 1;
a - 1	COUNT(*)
-1	1
20260100	1
99991230	1
SELECT a, a + 1, b, a - 1, c FROM t;
a	a + 1	b	a - 1	c
2026-01-01	20260102	2026-01-02	20260100	2026-01-00
9999-12-31	99991232	0000-00-00	99991230	9999-12-30
0000-00-00	1	0000-00-00	-1	0000-00-00
DROP TABLE t;
## date < date
CREATE TABLE t (a date, b date as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01'),('9999-12-31'),('0000-00-00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	4	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
0000-00-00	1
2026-01-01	1
SELECT * FROM t;
a	b
2026-01-01	2026-01-01
9999-12-31	9999-12-31
0000-00-00	0000-00-00
DROP TABLE t;
# Type_handler_datetime_common
## date, datetime
CREATE TABLE t (a date, b datetime as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01'),('2026-01-01');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
2026-01-01	2
SELECT * FROM t;
a	b
2026-01-01	2026-01-01 00:00:00
2026-01-01	2026-01-01 00:00:00
DROP TABLE t;
## datetime(1) < datetime(2)
CREATE TABLE t (a datetime(1), b datetime(2) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00'),('2026-01-01 16:00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	7	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
2026-01-01 12:00:00.0	1
2026-01-01 16:00:00.0	2
SELECT * FROM t;
a	b
2026-01-01 12:00:00.0	2026-01-01 12:00:00.00
2026-01-01 16:00:00.0	2026-01-01 16:00:00.00
2026-01-01 16:00:00.0	2026-01-01 16:00:00.00
DROP TABLE t;
## datetime(2), datetime(1)
CREATE TABLE t (a datetime(2), b datetime(1) as (a), key(b));
ERROR HY000: Function or expression '`a`' cannot be used in the GENERATED ALWAYS AS clause of `b`
## datetime < datetime
CREATE TABLE t (a datetime, b datetime as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00'),('2026-01-01 16:00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	6	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
2026-01-01 12:00:00	1
2026-01-01 16:00:00	2
SELECT * FROM t;
a	b
2026-01-01 12:00:00	2026-01-01 12:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
DROP TABLE t;
# Type_handler_timestamp_common
## datetime, timestamp
CREATE TABLE t (a datetime, b timestamp as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00'),('2026-01-01 16:00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
2026-01-01 12:00:00	1
2026-01-01 16:00:00	2
SELECT * FROM t;
a	b
2026-01-01 12:00:00	2026-01-01 12:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
DROP TABLE t;
## timestamp(1) < timestamp(2)
CREATE TABLE t (a timestamp(1), b timestamp(2) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00'),('2026-01-01 16:00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	6	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
2026-01-01 12:00:00.0	1
2026-01-01 16:00:00.0	2
SELECT * FROM t;
a	b
2026-01-01 12:00:00.0	2026-01-01 12:00:00.00
2026-01-01 16:00:00.0	2026-01-01 16:00:00.00
2026-01-01 16:00:00.0	2026-01-01 16:00:00.00
DROP TABLE t;
## timestamp(2), timestamp(1)
CREATE TABLE t (a timestamp(2), b timestamp(1) as (a), key(b));
ERROR HY000: Function or expression '`a`' cannot be used in the GENERATED ALWAYS AS clause of `b`
## timestamp < timestamp
CREATE TABLE t (a timestamp, b timestamp as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('2026-01-01 12:00'),('2026-01-01 16:00'),('2026-01-01 16:00');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
2026-01-01 12:00:00	1
2026-01-01 16:00:00	2
SELECT * FROM t;
a	b
2026-01-01 12:00:00	2026-01-01 12:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
2026-01-01 16:00:00	2026-01-01 16:00:00
DROP TABLE t;
# Type_handler_longstr
## tinyint, varchar(10)
CREATE TABLE t (a tinyint, b varchar(10) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES (-123),(100),(100);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-123	1
100	2
SELECT * FROM t;
a	b
-123	-123
100	100
100	100
DROP TABLE t;
## tinytext, varchar
CREATE TABLE t (a tinytext, b varchar(255) as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES ('aaa'),('bbb'),('aaa');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
aaa	2
bbb	1
SELECT * FROM t;
a	b
aaa	aaa
bbb	bbb
aaa	aaa
DROP TABLE t;
## varchar(10) latin1, varchar(10) utf8
CREATE TABLE t
(a varchar(10) collate latin1_swedish_ci,
b varchar(10) collate utf8mb4_unicode_ci as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('12345-aaa'),('12345-bbb'),('12345-aaa');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
12345-aaa	2
12345-bbb	1
SELECT * FROM t;
a	b
12345-aaa	12345-aaa
12345-bbb	12345-bbb
12345-aaa	12345-aaa
DROP TABLE t;
## varchar(10), varchar(5)
CREATE TABLE t (a varchar(10), b varchar(5) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('12345-aaa'),('12345-bbb');
Warnings:
Warning	1265	Data truncated for column 'b' at row 1
Warning	1265	Data truncated for column 'b' at row 2
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
12345-aaa	1
12345-bbb	1
SELECT * FROM t;
a	b
12345-aaa	12345
12345-bbb	12345
DROP TABLE t;
## varbinary(1024), varbinary(8) (MDEV-38685)
CREATE TABLE t (c VARBINARY(1024), vc VARBINARY(1024) AS (c) VIRTUAL, KEY(vc(8)));
INSERT INTO t (c) VALUES ('012345678'),('123456789'),('012345678');
explain
select COUNT(*) cnt FROM t GROUP BY c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`c` -> vc due to failed supertype check
select COUNT(*) cnt FROM t GROUP BY c;
cnt
2
1
DROP TABLE t;
## varchar(5) < varchar(10)
CREATE TABLE t (a varchar(5), b varchar(10) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('12345'),('12345'),('abcde');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	43	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
12345	2
abcde	1
SELECT * FROM t;
a	b
12345	12345
12345	12345
abcde	abcde
DROP TABLE t;
# Type_handler_blob_common
## tinyint, tinytext
CREATE TABLE t (a tinyint, b varchar(10) as (a), key(b));
INSERT IGNORE INTO t (a) VALUES (-123),(100),(100);
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
-123	1
100	2
SELECT * FROM t;
a	b
-123	-123
100	100
100	100
DROP TABLE t;
## varchar, tinytext
CREATE TABLE t (a varchar(10), b tinytext as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES ('aaa'),('bbb'),('aaa');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
aaa	2
bbb	1
SELECT * FROM t;
a	b
aaa	aaa
bbb	bbb
aaa	aaa
DROP TABLE t;
## tinytext latin1, tinytext utf8
CREATE TABLE t
(a tinytext collate latin1_swedish_ci,
b tinytext collate utf8mb4_unicode_ci as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES ('12345-aaa'),('12345-bbb'),('12345-aaa');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
12345-aaa	2
12345-bbb	1
SELECT * FROM t;
a	b
12345-aaa	12345-aaa
12345-bbb	12345-bbb
12345-aaa	12345-aaa
DROP TABLE t;
## text, tinytext
CREATE TABLE t (a text, b tinytext as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES ('12345-aaa'),('12345-bbb');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
12345-aaa	1
12345-bbb	1
SELECT * FROM t;
a	b
12345-aaa	12345-aaa
12345-bbb	12345-bbb
DROP TABLE t;
## tinytext < text
### TODO: somehow the key was not chosen. why?
CREATE TABLE t (a tinytext, b text as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES ('12345'),('12345'),('abcde');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
12345	2
abcde	1
SELECT * FROM t;
a	b
12345	12345
12345	12345
abcde	abcde
DROP TABLE t;
## tinytext, text(10)
CREATE TABLE t (a tinytext, b text as (a), key(b(10)));
INSERT IGNORE INTO t (a) VALUES ('12345'),('12345'),('abcde');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
12345	2
abcde	1
SELECT * FROM t;
a	b
12345	12345
12345	12345
abcde	abcde
DROP TABLE t;
## tinytext, text(8) (like MDEV-38685)
CREATE TABLE t (c tinytext, vc text AS (c) VIRTUAL, KEY(vc(8)));
INSERT INTO t (c) VALUES ('012345678'),('012345679');
explain
select COUNT(*) cnt FROM t GROUP BY c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
select COUNT(*) cnt FROM t GROUP BY c;
cnt
1
1
DROP TABLE t;
# Type_handler_typelib
## enum, enum
CREATE TABLE t (a enum('e1', 'e2', 'e3'), b enum('e1', 'e2', 'e3') as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('e1'),('e2'),('e1');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
e1	2
e2	1
SELECT * FROM t;
a	b
e1	e1
e2	e2
e1	e1
DROP TABLE t;
## enum, enum
CREATE TABLE t (a set('e1', 'e2', 'e3'), b set('e1', 'e2', 'e3') as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('e1,e2'),('e3'),('e1,e2');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
e1,e2	2
e3	1
SELECT * FROM t;
a	b
e1,e2	e1,e2
e3	e3
e1,e2	e1,e2
DROP TABLE t;
# Type_handler_geometry
## varchar, point
CREATE TABLE t (a varchar(20), b point as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('point(0,0)'),('point(1,0)'),('point(0,0)');
ERROR 22003: Cannot get geometry object from data you send to the GEOMETRY field
DROP TABLE t;
## point, polygon
CREATE TABLE t (a point, b polygon as (a), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES (point(0,0)),(point(1,0)),(point(0,0));
ERROR 22007: Incorrect POLYGON value: 'POINT(0 0)' for column `test`.`t`.`b` at row 1
DROP TABLE t;
## boundary(linestring), multipoint
## the type of boundary(linestring) is geometry
CREATE TABLE t (a linestring, b multipoint as (boundary(a)), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES
(LineStringFromText('LINESTRING(10 10,20 10,10 10)')),
(LineStringFromText('LINESTRING(10 10,5 10,10 10)')),
(LineStringFromText('LINESTRING(10 10,20 10,5 10)'));
explain
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY boundary(a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression st_boundary(`t`.`a`) -> b due to failed supertype check
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY boundary(a);
ST_AsWKT(a)	COUNT(*)
LINESTRING(10 10,20 10,5 10)	1
LINESTRING(10 10,20 10,10 10)	2
SELECT ST_AsWKT(a), ST_AsWKT(boundary(a)), ST_AsWKT(b) FROM t;
ST_AsWKT(a)	ST_AsWKT(boundary(a))	ST_AsWKT(b)
LINESTRING(10 10,20 10,10 10)	MULTIPOINT(10 10,10 10)	MULTIPOINT(10 10,10 10)
LINESTRING(10 10,5 10,10 10)	MULTIPOINT(10 10,10 10)	MULTIPOINT(10 10,10 10)
LINESTRING(10 10,20 10,5 10)	MULTIPOINT(10 10,5 10)	MULTIPOINT(10 10,5 10)
DROP TABLE t;
## point < point
CREATE TABLE t (a point, b point as (a), key(b));
INSERT IGNORE INTO t (a) VALUES (point(0,0)),(point(1,0)),(point(0,0));
explain
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY a;
ST_AsWKT(a)	COUNT(*)
POINT(0 0)	2
POINT(1 0)	1
SELECT ST_AsWKT(a), ST_AsWKT(b) FROM t;
ST_AsWKT(a)	ST_AsWKT(b)
POINT(0 0)	POINT(0 0)
POINT(1 0)	POINT(1 0)
POINT(0 0)	POINT(0 0)
DROP TABLE t;
## boundary(linestring), geometry
CREATE TABLE t (a linestring, b geometry as (boundary(a)), key(b));
Warnings:
Note	1071	Specified key was too long; max key length is 1000 bytes
INSERT IGNORE INTO t (a) VALUES
(LineStringFromText('LINESTRING(10 10,20 10,10 10)')),
(LineStringFromText('LINESTRING(10 10,5 10,10 10)')),
(LineStringFromText('LINESTRING(10 10,20 10,5 10)'));
explain
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY boundary(a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
SELECT ST_AsWKT(a), COUNT(*) FROM t GROUP BY boundary(a);
ST_AsWKT(a)	COUNT(*)
LINESTRING(10 10,20 10,5 10)	1
LINESTRING(10 10,20 10,10 10)	2
SELECT ST_AsWKT(a), ST_AsWKT(boundary(a)), ST_AsWKT(b) FROM t;
ST_AsWKT(a)	ST_AsWKT(boundary(a))	ST_AsWKT(b)
LINESTRING(10 10,20 10,10 10)	MULTIPOINT(10 10,10 10)	MULTIPOINT(10 10,10 10)
LINESTRING(10 10,5 10,10 10)	MULTIPOINT(10 10,10 10)	MULTIPOINT(10 10,10 10)
LINESTRING(10 10,20 10,5 10)	MULTIPOINT(10 10,5 10)	MULTIPOINT(10 10,5 10)
DROP TABLE t;
# Type_handler_fbt
## varchar, uuid
CREATE TABLE t (a varchar(36), b uuid as (a), key(b));
INSERT IGNORE INTO t (a) VALUES
('ffffffff-ffff-ffff-ffff-fffffffffffe'),
('ffffffff-ffff-ffff-ffff-ffffffffffff'),
('ffffffff-ffff-ffff-ffff-fffffffffffe');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
ffffffff-ffff-ffff-ffff-fffffffffffe	2
ffffffff-ffff-ffff-ffff-ffffffffffff	1
SELECT * FROM t;
a	b
ffffffff-ffff-ffff-ffff-fffffffffffe	ffffffff-ffff-ffff-ffff-fffffffffffe
ffffffff-ffff-ffff-ffff-ffffffffffff	ffffffff-ffff-ffff-ffff-ffffffffffff
ffffffff-ffff-ffff-ffff-fffffffffffe	ffffffff-ffff-ffff-ffff-fffffffffffe
DROP TABLE t;
## uuid < uuid
CREATE TABLE t (a uuid, b uuid as (a), key(b));
INSERT IGNORE INTO t (a) VALUES
(x'fffffffffffffffffffffffffffffffe'),
(x'ffffffffffffffffffffffffffffffff'),
(x'fffffffffffffffffffffffffffffffe');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	17	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
ffffffff-ffff-ffff-ffff-fffffffffffe	2
ffffffff-ffff-ffff-ffff-ffffffffffff	1
SELECT * FROM t;
a	b
ffffffff-ffff-ffff-ffff-fffffffffffe	ffffffff-ffff-ffff-ffff-fffffffffffe
ffffffff-ffff-ffff-ffff-ffffffffffff	ffffffff-ffff-ffff-ffff-ffffffffffff
ffffffff-ffff-ffff-ffff-fffffffffffe	ffffffff-ffff-ffff-ffff-fffffffffffe
DROP TABLE t;
## inet4, inet6
CREATE TABLE t (a inet4, b inet6 as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('0.0.0.0'), ('255.10.0.0'), ('0.0.0.0');
explain
SELECT a, COUNT(*) FROM t GROUP BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	3	Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> b due to failed supertype check
SELECT a, COUNT(*) FROM t GROUP BY a;
a	COUNT(*)
0.0.0.0	2
255.10.0.0	1
SELECT * FROM t;
a	b
0.0.0.0	::ffff:0.0.0.0
255.10.0.0	::ffff:255.10.0.0
0.0.0.0	::ffff:0.0.0.0
DROP TABLE t;
## inet4 < inet4
CREATE TABLE t (a inet4, b inet4 as (a), key(b));
INSERT IGNORE INTO t (a) VALUES ('0.0.0.0'), ('255.10.0.0'), ('0.0.0.0');
explain
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	b	5	NULL	2	
SELECT a, COUNT(*) FROM t GROUP BY a LIMIT 2;
a	COUNT(*)
0.0.0.0	2
255.10.0.0	1
SELECT * FROM t;
a	b
0.0.0.0	0.0.0.0
255.10.0.0	255.10.0.0
0.0.0.0	0.0.0.0
DROP TABLE t;
# Misc. tests
## insert then add index
CREATE TABLE t (a INT, va TINYINT AS (a));
SET sql_mode=STRICT_ALL_TABLES;
INSERT INTO t (a) VALUES (100),(150),(200);
SET sql_mode=DEFAULT;
ALTER TABLE t add index i(a);
ERROR 22003: Out of range value for column 'va' at row 2
drop table t;
## insert ignore overrides STRICT_ALL_TABLES
CREATE TABLE t (a INT, b TINYINT AS (a), key (b));
SET sql_mode=STRICT_ALL_TABLES;
INSERT INTO t (a) VALUES (100),(150),(200);
ERROR 22003: Out of range value for column 'b' at row 2
INSERT IGNORE INTO t (a) VALUES (100),(150),(200);
Warnings:
Warning	1264	Out of range value for column 'b' at row 2
Warning	1264	Out of range value for column 'b' at row 3
SET sql_mode=DEFAULT;
drop table t;
## order by also affected
CREATE TABLE t (a INT, va TINYINT AS (a), key (va));
INSERT IGNORE INTO t (a) VALUES (100),(150),(200),(180);
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
Warning	1264	Out of range value for column 'va' at row 4
explain
select * from t order by a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	ALL	NULL	NULL	NULL	NULL	4	Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`a` -> va due to failed supertype check
select * from t order by a;
a	va
100	100
150	127
180	127
200	127
drop table t;
## vcol on vcol
create table t (c tinyint, key (c));
alter table t
add column vc1 tinyint as (c + 10),
add index(vc1);
alter table t
add column vc2 int as (vc1 * 2),
add index(vc2);
insert ignore into t (c) values (121), (122), (120);
Warnings:
Warning	1264	Out of range value for column 'vc1' at row 1
Warning	1264	Out of range value for column 'vc1' at row 2
Warning	1264	Out of range value for column 'vc1' at row 3
explain
select * from t;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	c	2	NULL	3	Using index
select * from t;
c	vc1	vc2
120	127	254
121	127	254
122	127	254
explain
select c from t group by vc2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	c	2	NULL	3	Using index; Using temporary; Using filesort
select c from t group by vc2;
c
120
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	range	NULL	vc2	5	NULL	3	Using index for group-by
select vc2 from t group by vc1 * 2;
vc2
254
explain
select * from t group by (c + 10) * 2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	c	2	NULL	3	Using index; Using temporary; Using filesort
select * from t group by (c + 10) * 2;
c	vc1	vc2
120	127	254
121	127	254
122	127	254
explain
select * from t group by c + 10;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t	index	NULL	c	2	NULL	3	Using index; Using temporary; Using filesort
Warnings:
Note	1105	Cannot substitute virtual column expression `t`.`c` + 10 -> vc1 due to failed supertype check
select * from t group by c + 10;
c	vc1	vc2
120	127	254
121	127	254
122	127	254
drop table t;
