@@ -7,45 +7,88 @@ DROP DATABASE IF EXISTS group_by_ambiguous_test;
77CREATE DATABASE group_by_ambiguous_test;
88USE 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+ #
2625SELECT 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+
5083DROP 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+ #
5194DROP DATABASE group_by_ambiguous_test;
0 commit comments