Skip to content

Commit 282488a

Browse files
committed
Tests for group_concat with an order by containing a subquery
1 parent f382747 commit 282488a

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

enginetest/queries/script_queries.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,167 @@ CREATE TABLE tab3 (
27602760
},
27612761
},
27622762
},
2763+
{
2764+
Name: "Group Concat with Subquery in ORDER BY",
2765+
Dialect: "mysql",
2766+
SetUpScript: []string{
2767+
"CREATE TABLE test_data (id INT PRIMARY KEY, name VARCHAR(50), age INT, category VARCHAR(10))",
2768+
"INSERT INTO test_data VALUES (1, 'Alice', 25, 'A'), (2, 'Bob', 30, 'B'), (3, 'Charlie', 22, 'A'), (4, 'Diana', 28, 'C'), (5, 'Eve', 35, 'B'), (6, 'Frank', 26, 'A')",
2769+
},
2770+
Assertions: []ScriptTestAssertion{
2771+
{
2772+
Query: "SELECT category, group_concat(name ORDER BY (SELECT COUNT(*) FROM test_data t2 WHERE t2.category = test_data.category AND t2.age < test_data.age)) FROM test_data GROUP BY category ORDER BY category",
2773+
Expected: []sql.Row{{"A", "Charlie,Alice,Frank"}, {"B", "Bob,Eve"}, {"C", "Diana"}},
2774+
},
2775+
{
2776+
Query: "SELECT group_concat(name ORDER BY (SELECT AVG(age) FROM test_data t2 WHERE t2.category = test_data.category)) FROM test_data",
2777+
Expected: []sql.Row{{"Charlie,Alice,Frank,Bob,Eve,Diana"}},
2778+
},
2779+
{
2780+
Query: "SELECT category, group_concat(name ORDER BY (SELECT MAX(age) FROM test_data t2 WHERE t2.id <= test_data.id)) FROM test_data GROUP BY category ORDER BY category",
2781+
Expected: []sql.Row{{"A", "Alice,Charlie,Frank"}, {"B", "Bob,Eve"}, {"C", "Diana"}},
2782+
},
2783+
},
2784+
},
2785+
{
2786+
Name: "Group Concat with Subquery in ORDER BY",
2787+
Dialect: "mysql",
2788+
SetUpScript: []string{
2789+
"CREATE TABLE test_data (id INT PRIMARY KEY, name VARCHAR(50), age INT, category VARCHAR(10))",
2790+
"INSERT INTO test_data VALUES (1, 'Alice', 25, 'A'), (2, 'Bob', 30, 'B'), (3, 'Charlie', 22, 'A'), (4, 'Diana', 28, 'C'), (5, 'Eve', 35, 'B'), (6, 'Frank', 26, 'A')",
2791+
},
2792+
Assertions: []ScriptTestAssertion{
2793+
{
2794+
Query: "SELECT category, group_concat(name ORDER BY (SELECT COUNT(*) FROM test_data t2 WHERE t2.category = test_data.category AND t2.age < test_data.age)) FROM test_data GROUP BY category ORDER BY category",
2795+
Expected: []sql.Row{{"A", "Charlie,Alice,Frank"}, {"B", "Bob,Eve"}, {"C", "Diana"}},
2796+
},
2797+
{
2798+
Query: "SELECT group_concat(name ORDER BY (SELECT AVG(age) FROM test_data t2 WHERE t2.category = test_data.category)) FROM test_data",
2799+
Expected: []sql.Row{{"Charlie,Alice,Frank,Bob,Eve,Diana"}},
2800+
},
2801+
{
2802+
Query: "SELECT category, group_concat(name ORDER BY (SELECT MAX(age) FROM test_data t2 WHERE t2.id <= test_data.id)) FROM test_data GROUP BY category ORDER BY category",
2803+
Expected: []sql.Row{{"A", "Alice,Charlie,Frank"}, {"B", "Bob,Eve"}, {"C", "Diana"}},
2804+
},
2805+
},
2806+
},
2807+
{
2808+
Name: "Group Concat with Subquery in ORDER BY - Additional Edge Cases",
2809+
Dialect: "mysql",
2810+
SetUpScript: []string{
2811+
"CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(50), price DECIMAL(10,2), category_id INT, supplier_id INT)",
2812+
"CREATE TABLE categories (id INT PRIMARY KEY, name VARCHAR(50), priority INT)",
2813+
"CREATE TABLE suppliers (id INT PRIMARY KEY, name VARCHAR(50), rating INT)",
2814+
"INSERT INTO products VALUES (1, 'Laptop', 999.99, 1, 1), (2, 'Mouse', 25.50, 1, 2), (3, 'Keyboard', 75.00, 1, 1)",
2815+
"INSERT INTO products VALUES (4, 'Chair', 150.00, 2, 3), (5, 'Desk', 300.00, 2, 3), (6, 'Monitor', 250.00, 1, 2)",
2816+
"INSERT INTO categories VALUES (1, 'Electronics', 1), (2, 'Furniture', 2)",
2817+
"INSERT INTO suppliers VALUES (1, 'TechCorp', 5), (2, 'GadgetInc', 4), (3, 'OfficeSupply', 3)",
2818+
},
2819+
Assertions: []ScriptTestAssertion{
2820+
{
2821+
Query: "SELECT category_id, GROUP_CONCAT(name ORDER BY (SELECT rating FROM suppliers WHERE suppliers.id = products.supplier_id) DESC) FROM products GROUP BY category_id ORDER BY category_id",
2822+
Expected: []sql.Row{{1, "Laptop,Keyboard,Monitor,Mouse"}, {2, "Chair,Desk"}},
2823+
},
2824+
{
2825+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT COUNT(*) FROM products p2 WHERE p2.price < products.price)) FROM products",
2826+
Expected: []sql.Row{{"Mouse,Keyboard,Desk,Monitor,Chair,Laptop"}},
2827+
},
2828+
{
2829+
Query: "SELECT category_id, GROUP_CONCAT(DISTINCT supplier_id ORDER BY (SELECT rating FROM suppliers WHERE suppliers.id = products.supplier_id)) FROM products GROUP BY category_id",
2830+
Expected: []sql.Row{{1, "2,1"}, {2, "3"}},
2831+
},
2832+
{
2833+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT priority FROM categories WHERE categories.id = products.category_id), price) FROM products",
2834+
Expected: []sql.Row{{"Mouse,Keyboard,Monitor,Laptop,Chair,Desk"}},
2835+
},
2836+
{
2837+
Query: "SELECT category_id, GROUP_CONCAT(name ORDER BY (SELECT AVG(price) FROM products p2 WHERE p2.category_id = products.category_id) DESC, name) FROM products GROUP BY category_id ORDER BY category_id",
2838+
Expected: []sql.Row{{1, "Keyboard,Laptop,Monitor,Mouse"}, {2, "Chair,Desk"}},
2839+
},
2840+
},
2841+
},
2842+
{
2843+
Name: "Group Concat Subquery ORDER BY Error Cases",
2844+
Dialect: "mysql",
2845+
SetUpScript: []string{
2846+
"CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(50), value INT)",
2847+
"INSERT INTO test_table VALUES (1, 'A', 10), (2, 'B', 20), (3, 'C', 30)",
2848+
},
2849+
Assertions: []ScriptTestAssertion{
2850+
{
2851+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT name, value FROM test_table t2 WHERE t2.id = test_table.id)) FROM test_table",
2852+
ExpectedErr: sql.ErrInvalidOperandColumns,
2853+
},
2854+
{
2855+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT value FROM test_table)) FROM test_table",
2856+
ExpectedErr: sql.ErrExpectedSingleRow,
2857+
},
2858+
},
2859+
},
2860+
{
2861+
Name: "Group Concat Subquery ORDER BY Additional Edge Cases",
2862+
Dialect: "mysql",
2863+
SetUpScript: []string{
2864+
"CREATE TABLE complex_test (id INT PRIMARY KEY, name VARCHAR(50), value INT, category VARCHAR(10), created_at DATE)",
2865+
"INSERT INTO complex_test VALUES (1, 'Alpha', 100, 'X', '2023-01-01')",
2866+
"INSERT INTO complex_test VALUES (2, 'Beta', 50, 'Y', '2023-01-15')",
2867+
"INSERT INTO complex_test VALUES (3, 'Gamma', 75, 'X', '2023-02-01')",
2868+
"INSERT INTO complex_test VALUES (4, 'Delta', 25, 'Z', '2023-02-15')",
2869+
"INSERT INTO complex_test VALUES (5, 'Epsilon', 90, 'Y', '2023-03-01')",
2870+
},
2871+
Assertions: []ScriptTestAssertion{
2872+
{
2873+
// Test with subquery returning NULL values
2874+
Query: "SELECT category, GROUP_CONCAT(name ORDER BY (SELECT CASE WHEN complex_test.value > 80 THEN NULL ELSE complex_test.value END)) FROM complex_test GROUP BY category ORDER BY category",
2875+
Expected: []sql.Row{{"X", "Gamma,Alpha"}, {"Y", "Beta,Epsilon"}, {"Z", "Delta"}},
2876+
},
2877+
{
2878+
// Test with correlated subquery using multiple tables
2879+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT COUNT(*) FROM complex_test c2 WHERE c2.category = complex_test.category AND c2.value > complex_test.value)) FROM complex_test",
2880+
Expected: []sql.Row{{"Alpha,Gamma,Epsilon,Beta,Delta"}},
2881+
},
2882+
{
2883+
// Test with subquery using aggregate functions with HAVING
2884+
Query: "SELECT category, GROUP_CONCAT(name ORDER BY (SELECT AVG(value) FROM complex_test c2 WHERE c2.id <= complex_test.id HAVING AVG(value) > 50) DESC) FROM complex_test GROUP BY category ORDER BY category",
2885+
Expected: []sql.Row{{"X", "Alpha,Gamma"}, {"Y", "Epsilon,Beta"}, {"Z", "Delta"}},
2886+
},
2887+
{
2888+
// Test with DISTINCT and complex subquery
2889+
Query: "SELECT GROUP_CONCAT(DISTINCT category ORDER BY (SELECT SUM(value) FROM complex_test c2 WHERE c2.category = complex_test.category) DESC SEPARATOR '|') FROM complex_test",
2890+
Expected: []sql.Row{{"X|Y|Z"}},
2891+
},
2892+
{
2893+
// Test with nested subqueries
2894+
Query: "SELECT GROUP_CONCAT(name ORDER BY (SELECT COUNT(*) FROM complex_test c2 WHERE c2.value > (SELECT MIN(value) FROM complex_test c3 WHERE c3.category = complex_test.category))) FROM complex_test",
2895+
Expected: []sql.Row{{"Delta,Beta,Gamma,Epsilon,Alpha"}},
2896+
},
2897+
},
2898+
},
2899+
{
2900+
Name: "Group Concat Subquery ORDER BY Performance and Boundary Cases",
2901+
Dialect: "mysql",
2902+
SetUpScript: []string{
2903+
"CREATE TABLE perf_test (id INT PRIMARY KEY, data VARCHAR(10), weight DECIMAL(5,2))",
2904+
"INSERT INTO perf_test VALUES (1, 'A', 1.5), (2, 'B', 2.5), (3, 'C', 0.5), (4, 'D', 3.5), (5, 'E', 2.0)",
2905+
},
2906+
Assertions: []ScriptTestAssertion{
2907+
{
2908+
// Test with subquery returning same value for multiple rows (stability)
2909+
Query: "SELECT GROUP_CONCAT(data ORDER BY (SELECT 42), id) FROM perf_test",
2910+
Expected: []sql.Row{{"A,B,C,D,E"}},
2911+
},
2912+
{
2913+
// Test with subquery using LIMIT
2914+
Query: "SELECT GROUP_CONCAT(data ORDER BY (SELECT weight FROM perf_test p2 WHERE p2.id = perf_test.id LIMIT 1)) FROM perf_test",
2915+
Expected: []sql.Row{{"C,A,E,B,D"}},
2916+
},
2917+
{
2918+
// Test with very small decimal differences in ORDER BY subquery
2919+
Query: "SELECT GROUP_CONCAT(data ORDER BY (SELECT weight + 0.001 * perf_test.id FROM perf_test p2 WHERE p2.id = perf_test.id)) FROM perf_test",
2920+
Expected: []sql.Row{{"C,A,E,B,D"}},
2921+
},
2922+
},
2923+
},
27632924
{
27642925
Name: "CONVERT USING still converts between incompatible character sets",
27652926
Dialect: "mysql",

0 commit comments

Comments
 (0)