Skip to content

Commit 7844c4e

Browse files
MCOL-6088 add tests
1 parent 5ae2e21 commit 7844c4e

File tree

2 files changed

+142
-59
lines changed

2 files changed

+142
-59
lines changed
Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,90 @@
11
DROP DATABASE IF EXISTS group_by_ambiguous_test;
22
CREATE DATABASE group_by_ambiguous_test;
33
USE group_by_ambiguous_test;
4-
CREATE TABLE `test2` (
4+
CREATE TABLE `t1` (
55
`id` INT UNSIGNED NOT NULL,
66
`GBP` DOUBLE UNSIGNED NOT NULL,
77
`cat_id` INT UNSIGNED NOT NULL,
88
`cat_id_level_0` INT UNSIGNED NOT NULL
99
) ENGINE=Columnstore;
10-
INSERT INTO test2 VALUES (1, 100.5, 10, 20);
11-
INSERT INTO test2 VALUES (2, 200.5, 10, 30);
12-
INSERT INTO test2 VALUES (3, 300.5, 20, 20);
13-
#
14-
# Test case: GROUP BY with ambiguous column reference
15-
# This should not throw a ColumnStore error, but resolve to the table column (SQL standard)
16-
#
10+
INSERT INTO t1 VALUES (1, 100.5, 10, 20);
11+
INSERT INTO t1 VALUES (2, 200.5, 10, 30);
12+
INSERT INTO t1 VALUES (3, 300.5, 20, 20);
1713
SELECT cat_id_level_0 AS entity_id, SUM(GBP) AS spend, cat_id_level_0 AS cat_id
18-
FROM test2
19-
GROUP BY cat_id;
14+
FROM t1
15+
GROUP BY cat_id
16+
ORDER BY entity_id;
2017
entity_id spend cat_id
2118
20 300.5 20
2219
30 301 30
2320
Warnings:
2421
Warning 1052 Column 'cat_id' in GROUP BY is ambiguous
25-
DROP TABLE test2;
26-
#
27-
# Test case: GROUP BY with alias that matches a column but points to different column
28-
#
29-
CREATE TABLE t1 (col1 int, col2 varchar(5), col_t1 int) ENGINE=Columnstore;
30-
INSERT INTO t1 VALUES(10,'hello',10);
31-
INSERT INTO t1 VALUES(20,'hello',20);
32-
INSERT INTO t1 VALUES(30,'hello',30);
33-
INSERT INTO t1 VALUES(10,'bye',10);
34-
INSERT INTO t1 VALUES(10,'sam',10);
35-
INSERT INTO t1 VALUES(10,'bob',10);
36-
# GROUP BY col2 is ambiguous: matches both table column and SELECT alias
37-
# SQL standard: prefers table column col2 (varchar) over alias
38-
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2 ORDER BY col2;
22+
CREATE TABLE t2 (col1 int, col2 varchar(5), col_t1 int) ENGINE=Columnstore;
23+
INSERT INTO t2 VALUES(10,'hello',10);
24+
INSERT INTO t2 VALUES(20,'hello',20);
25+
INSERT INTO t2 VALUES(30,'hello',30);
26+
INSERT INTO t2 VALUES(10,'bye',10);
27+
INSERT INTO t2 VALUES(10,'sam',10);
28+
INSERT INTO t2 VALUES(10,'bob',10);
29+
SELECT SUM(col1) AS col2 FROM t2 GROUP BY col2 ORDER BY col2;
3930
col2
4031
10
4132
10
4233
10
4334
60
4435
Warnings:
4536
Warning 1052 Column 'col2' in GROUP BY is ambiguous
46-
#
47-
# Cleanup
48-
#
37+
CREATE TABLE t3 (a int, b int, c int) ENGINE=Columnstore;
38+
INSERT INTO t3 VALUES(1,10,100),(1,20,200),(2,10,100),(2,20,200);
39+
SELECT c AS a, c AS b, SUM(a) AS sum_val FROM t3 GROUP BY a, b ORDER BY a, b, sum_val;
40+
a b sum_val
41+
100 100 1
42+
100 100 2
43+
200 200 1
44+
200 200 2
45+
Warnings:
46+
Warning 1052 Column 'a' in GROUP BY is ambiguous
47+
Warning 1052 Column 'b' in GROUP BY is ambiguous
48+
CREATE TABLE t4 (id int, val int, name varchar(50)) ENGINE=Columnstore;
49+
CREATE TABLE t4b (id int, amount int) ENGINE=Columnstore;
50+
INSERT INTO t4 VALUES(1, 100, 'A'), (2, 200, 'B');
51+
INSERT INTO t4b VALUES(1, 50), (2, 75);
52+
CREATE VIEW v4 AS SELECT id, val FROM t4;
53+
SELECT v4.id, SUM(t4b.amount) AS total FROM v4 JOIN t4b ON v4.id = t4b.id GROUP BY id ORDER BY id;
54+
id total
55+
1 50
56+
2 75
57+
DROP VIEW v4;
58+
CREATE TABLE t5 (id int, amount decimal(10,2), name varchar(50)) ENGINE=Columnstore;
59+
INSERT INTO t5 VALUES(1, 100.50, 'A'),(1, 200.75, 'B'),(2, 150.25, 'A');
60+
SELECT id AS name, SUM(amount) AS total FROM t5 GROUP BY name ORDER BY name;
61+
name total
62+
1 200.75
63+
2 250.75
64+
Warnings:
65+
Warning 1052 Column 'name' in GROUP BY is ambiguous
66+
CREATE TABLE t6a (id int, val int) ENGINE=Columnstore;
67+
CREATE TABLE t6b (id int, name varchar(10)) ENGINE=Columnstore;
68+
INSERT INTO t6a VALUES(1,100),(2,200),(3,300);
69+
INSERT INTO t6b VALUES(1,'A'),(2,'B'),(3,'C');
70+
SELECT t6a.val AS id, COUNT(*) AS cnt FROM t6a JOIN t6b ON t6a.id = t6b.id GROUP BY id ORDER BY id;
71+
id cnt
72+
100 1
73+
200 1
74+
300 1
75+
CREATE TABLE t7 (x int, y int, z int) ENGINE=Columnstore;
76+
INSERT INTO t7 VALUES(1,10,100),(2,20,200),(1,30,100),(2,40,200);
77+
SELECT z AS x, SUM(y) AS sum_y FROM t7 GROUP BY x ORDER BY x;
78+
x sum_y
79+
100 40
80+
200 60
81+
Warnings:
82+
Warning 1052 Column 'x' in GROUP BY is ambiguous
4983
DROP TABLE t1;
84+
DROP TABLE t2;
85+
DROP TABLE t3;
86+
DROP TABLE t4, t4b;
87+
DROP TABLE t5;
88+
DROP TABLE t6a, t6b;
89+
DROP TABLE t7;
5090
DROP DATABASE group_by_ambiguous_test;

mysql-test/columnstore/basic/t/group_by_ambiguous.test

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,88 @@ DROP DATABASE IF EXISTS group_by_ambiguous_test;
77
CREATE DATABASE group_by_ambiguous_test;
88
USE group_by_ambiguous_test;
99

10-
CREATE TABLE `test2` (
10+
CREATE TABLE `t1` (
1111
`id` INT UNSIGNED NOT NULL,
1212
`GBP` DOUBLE UNSIGNED NOT NULL,
1313
`cat_id` INT UNSIGNED NOT NULL,
1414
`cat_id_level_0` INT UNSIGNED NOT NULL
1515
) ENGINE=Columnstore;
1616

17-
INSERT INTO test2 VALUES (1, 100.5, 10, 20);
18-
INSERT INTO test2 VALUES (2, 200.5, 10, 30);
19-
INSERT INTO test2 VALUES (3, 300.5, 20, 20);
20-
21-
--echo #
22-
--echo # Test case: GROUP BY with ambiguous column reference
23-
--echo # This should not throw a ColumnStore error, but resolve to the table column (SQL standard)
24-
--echo #
17+
INSERT INTO t1 VALUES (1, 100.5, 10, 20);
18+
INSERT INTO t1 VALUES (2, 200.5, 10, 30);
19+
INSERT INTO t1 VALUES (3, 300.5, 20, 20);
2520

21+
#
22+
# Test case: GROUP BY with ambiguous column reference
23+
# This should not throw a ColumnStore error, but resolve to the table column (SQL standard)
24+
#
2625
SELECT cat_id_level_0 AS entity_id, SUM(GBP) AS spend, cat_id_level_0 AS cat_id
27-
FROM test2
28-
GROUP BY cat_id;
29-
DROP TABLE test2;
30-
31-
--echo #
32-
--echo # Test case: GROUP BY with alias that matches a column but points to different column
33-
--echo #
34-
35-
CREATE TABLE t1 (col1 int, col2 varchar(5), col_t1 int) ENGINE=Columnstore;
36-
INSERT INTO t1 VALUES(10,'hello',10);
37-
INSERT INTO t1 VALUES(20,'hello',20);
38-
INSERT INTO t1 VALUES(30,'hello',30);
39-
INSERT INTO t1 VALUES(10,'bye',10);
40-
INSERT INTO t1 VALUES(10,'sam',10);
41-
INSERT INTO t1 VALUES(10,'bob',10);
42-
43-
--echo # GROUP BY col2 is ambiguous: matches both table column and SELECT alias
44-
--echo # SQL standard: prefers table column col2 (varchar) over alias
45-
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2 ORDER BY col2;
46-
47-
--echo #
48-
--echo # Cleanup
49-
--echo #
26+
FROM t1
27+
GROUP BY cat_id
28+
ORDER BY entity_id;
29+
30+
CREATE TABLE t2 (col1 int, col2 varchar(5), col_t1 int) ENGINE=Columnstore;
31+
INSERT INTO t2 VALUES(10,'hello',10);
32+
INSERT INTO t2 VALUES(20,'hello',20);
33+
INSERT INTO t2 VALUES(30,'hello',30);
34+
INSERT INTO t2 VALUES(10,'bye',10);
35+
INSERT INTO t2 VALUES(10,'sam',10);
36+
INSERT INTO t2 VALUES(10,'bob',10);
37+
38+
#
39+
# Test case: GROUP BY with alias that matches a column but points to different column
40+
#
41+
42+
# GROUP BY col2 is ambiguous: matches both table column and SELECT alias
43+
# SQL standard: prefers table column col2 (varchar) over alias
44+
SELECT SUM(col1) AS col2 FROM t2 GROUP BY col2 ORDER BY col2;
45+
46+
CREATE TABLE t3 (a int, b int, c int) ENGINE=Columnstore;
47+
INSERT INTO t3 VALUES(1,10,100),(1,20,200),(2,10,100),(2,20,200);
48+
49+
# Multiple columns in GROUP BY
50+
SELECT c AS a, c AS b, SUM(a) AS sum_val FROM t3 GROUP BY a, b ORDER BY a, b, sum_val;
51+
52+
CREATE TABLE t4 (id int, val int, name varchar(50)) ENGINE=Columnstore;
53+
CREATE TABLE t4b (id int, amount int) ENGINE=Columnstore;
54+
INSERT INTO t4 VALUES(1, 100, 'A'), (2, 200, 'B');
55+
INSERT INTO t4b VALUES(1, 50), (2, 75);
56+
57+
# VIEW with ambiguous column in GROUP BY
58+
CREATE VIEW v4 AS SELECT id, val FROM t4;
59+
SELECT v4.id, SUM(t4b.amount) AS total FROM v4 JOIN t4b ON v4.id = t4b.id GROUP BY id ORDER BY id;
60+
DROP VIEW v4;
61+
62+
CREATE TABLE t5 (id int, amount decimal(10,2), name varchar(50)) ENGINE=Columnstore;
63+
INSERT INTO t5 VALUES(1, 100.50, 'A'),(1, 200.75, 'B'),(2, 150.25, 'A');
64+
65+
# Different data types (int aliased to varchar column name)
66+
SELECT id AS name, SUM(amount) AS total FROM t5 GROUP BY name ORDER BY name;
67+
68+
CREATE TABLE t6a (id int, val int) ENGINE=Columnstore;
69+
CREATE TABLE t6b (id int, name varchar(10)) ENGINE=Columnstore;
70+
INSERT INTO t6a VALUES(1,100),(2,200),(3,300);
71+
INSERT INTO t6b VALUES(1,'A'),(2,'B'),(3,'C');
72+
73+
# JOIN with aliased column matching table column
74+
SELECT t6a.val AS id, COUNT(*) AS cnt FROM t6a JOIN t6b ON t6a.id = t6b.id GROUP BY id ORDER BY id;
75+
76+
CREATE TABLE t7 (x int, y int, z int) ENGINE=Columnstore;
77+
INSERT INTO t7 VALUES(1,10,100),(2,20,200),(1,30,100),(2,40,200);
78+
79+
# Column reference in both GROUP BY and ORDER BY
80+
SELECT z AS x, SUM(y) AS sum_y FROM t7 GROUP BY x ORDER BY x;
81+
82+
5083
DROP TABLE t1;
84+
DROP TABLE t2;
85+
DROP TABLE t3;
86+
DROP TABLE t4, t4b;
87+
DROP TABLE t5;
88+
DROP TABLE t6a, t6b;
89+
DROP TABLE t7;
90+
91+
#
92+
# Cleanup
93+
#
5194
DROP DATABASE group_by_ambiguous_test;

0 commit comments

Comments
 (0)