3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements tests for ORDER BY on aggregate functions.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
17 do_execsql_test aggorderby-1.1 {
18 CREATE TABLE t1(a TEXT,b INT,c INT,d INT);
19 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<9)
20 INSERT INTO t1(a,b,c,d) SELECT printf('%d',(x*7)%10),1,x,10-x FROM c;
21 INSERT INTO t1(a,b,c,d) SELECT a, 2, c, 10-d FROM t1;
22 CREATE INDEX t1b ON t1(b);
24 do_catchsql_test aggorderby-1.2 {
25 SELECT b, group_concat(a ORDER BY max(d)) FROM t1 GROUP BY b;
26 } {1 {misuse of aggregate function max()}}
27 do_catchsql_test aggorderby-1.3 {
28 SELECT abs(a ORDER BY max(d)) FROM t1;
29 } {1 {ORDER BY may not be used with non-aggregate abs()}}
31 do_execsql_test aggorderby-2.0 {
32 SELECT group_concat(a ORDER BY a) FROM t1 WHERE b=1;
33 } {0,1,2,3,4,5,6,7,8,9}
34 do_execsql_test aggorderby-2.1 {
35 SELECT group_concat(a ORDER BY c) FROM t1 WHERE b=1;
36 } {0,7,4,1,8,5,2,9,6,3}
37 do_execsql_test aggorderby-2.2 {
38 SELECT group_concat(a ORDER BY b, d) FROM t1;
39 } {3,6,9,2,5,8,1,4,7,0,0,7,4,1,8,5,2,9,6,3}
40 do_execsql_test aggorderby-2.3 {
41 SELECT string_agg(a, ',' ORDER BY b DESC, d) FROM t1;
42 } {0,7,4,1,8,5,2,9,6,3,3,6,9,2,5,8,1,4,7,0}
43 do_execsql_test aggorderby-2.4 {
44 SELECT b, group_concat(a ORDER BY d) FROM t1 GROUP BY b ORDER BY b;
45 } {1 3,6,9,2,5,8,1,4,7,0 2 0,7,4,1,8,5,2,9,6,3}
47 do_execsql_test aggorderby-3.0 {
48 SELECT group_concat(DISTINCT a ORDER BY a) FROM t1;
49 } {0,1,2,3,4,5,6,7,8,9}
50 do_execsql_test aggorderby-3.1 {
51 SELECT group_concat(DISTINCT a ORDER BY c) FROM t1;
52 } {0,7,4,1,8,5,2,9,6,3}
54 do_execsql_test aggorderby-4.0 {
55 SELECT count(ORDER BY a) FROM t1;
57 do_execsql_test aggorderby-4.1 {
58 SELECT c, max(a ORDER BY a) FROM t1;
62 do_execsql_test aggorderby-5.0 {
63 DROP TABLE IF EXISTS t1;
64 DROP TABLE IF EXISTS t3;
65 CREATE TABLE t1(a TEXT); INSERT INTO t1 VALUES('aaa'),('bbb');
66 CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('/'),('-');
67 SELECT (SELECT string_agg(a,d) FROM t3) FROM t1;
69 do_execsql_test aggorderby-5.1 {
70 SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1;
72 do_execsql_test aggorderby-5.2 {
73 SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1;
75 do_execsql_test aggorderby-5.3 {
76 SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1;
79 # COLLATE works on the ORDER BY.
81 do_execsql_test aggorderby-6.0 {
82 WITH c(x) AS (VALUES('abc'),('DEF'),('xyz'),('ABC'),('XYZ'))
83 SELECT string_agg(x,',' ORDER BY x COLLATE nocase),
84 string_agg(x,',' ORDER BY x) FROM c;
85 } {abc,ABC,DEF,xyz,XYZ ABC,DEF,XYZ,abc,xyz}
86 do_execsql_test aggorderby-6.1 {
87 WITH c(x,y) AS (VALUES(1,'a'),(2,'B'),(3,'c'),(4,'D'))
88 SELECT group_concat(x ORDER BY y COLLATE nocase),
89 group_concat(x ORDER BY y COLLATE binary) FROM c;
92 # NULLS FIRST and NULLS LAST work on the ORDER BY
94 do_execsql_test aggorderby-7.0 {
95 WITH c(x) AS (VALUES(1),(NULL),(2.5),(NULL),('three'))
96 SELECT json_group_array(x ORDER BY x NULLS FIRST),
97 json_group_array(x ORDER BY x NULLS LAST) FROM c;
98 } {[null,null,1,2.5,"three"] [1,2.5,"three",null,null]}
99 do_execsql_test aggorderby-7.1 {
100 WITH c(x,y) AS (VALUES(1,9),(2,null),(3,5),(4,null),(5,1))
101 SELECT json_group_array(x ORDER BY y NULLS FIRST, x),
102 json_group_array(x ORDER BY y NULLS LAST, x) FROM c;
103 } {[2,4,5,3,1] [5,3,1,2,4]}
105 # The DISTINCT only applies to the function arguments, not to the
106 # ORDER BY arguments.
108 do_execsql_test aggorderby-8.0 {
109 WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('c',2,7),('c',1,8))
110 SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c;
112 do_execsql_test aggorderby-8.1 {
113 WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('b',2,7),('c',1,8))
114 SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c;
116 do_execsql_test aggorderby-8.2 {
117 WITH c(x,y) AS (VALUES(1,1),(2,2),(3,3),(3,4),(3,5),(3,6))
118 SELECT sum(DISTINCT x ORDER BY y) FROM c;
121 # Subtype information is transfered through the sorter for aggregates
122 # that make use of subtype info.
124 do_execsql_test aggorderby-9.0 {
125 WITH c(x,y) AS (VALUES
130 SELECT json_group_array(json(x) ORDER BY y) FROM c;
131 } {{[[1,1],{"x":2},{"a":3},[4,4]]}}
132 do_execsql_test aggorderby-9.1 {
133 WITH c(x,y) AS (VALUES
140 SELECT json_group_array(DISTINCT json(x) ORDER BY y) FROM c;
141 } {{[[1,1],{"x":2},{"a":3},[4,4]]}}
142 do_execsql_test aggorderby-9.2 {
143 WITH c(x,y) AS (VALUES
148 SELECT json_group_array(json(x) ORDER BY json(x)) FROM c;
149 } {{[[1,1],[4,4],{"a":3},{"x":2}]}}
150 do_execsql_test aggorderby-9.3 {
151 WITH c(x,y) AS (VALUES
158 SELECT json_group_array(DISTINCT json(x) ORDER BY json(x)) FROM c;
159 } {{[[1,1],[4,4],{"a":3},{"x":2}]}}