Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / aggorderby.test
blobeed1f83a7e00474f92d6520973a77915531f583b
1 # 2023-10-18
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;
56 } 20
57 do_execsql_test aggorderby-4.1 {
58   SELECT c, max(a ORDER BY a) FROM t1;
59 } {7 9}
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;
68 } {aaa-aaa bbb-bbb}
69 do_execsql_test aggorderby-5.1 {
70   SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1;
71 } {aaa/aaa bbb/bbb}
72 do_execsql_test aggorderby-5.2 {
73   SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1;
74 } {aaa-aaa bbb-bbb}
75 do_execsql_test aggorderby-5.3 {
76   SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1;
77 } {aaa#aaa bbb#bbb}
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;
90 } {1,2,3,4 2,4,1,3}
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;
111 } {c,b,a}
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;
115 } {c,b,a}
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;
119 } 6
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
126     ('{a:3}', 3),
127     ('[1,1]', 1),
128     ('[4,4]', 4),
129     ('{x:2}', 2))
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
134     ('[4,4]', 4),
135     ('{a:3}', 3),
136     ('[4,4]', 4),
137     ('[1,1]', 1),
138     ('[4,4]', 4),
139     ('{x:2}', 2))
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
144     ('{a:3}', 3),
145     ('[1,1]', 1),
146     ('[4,4]', 4),
147     ('{x:2}', 2))
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
152     ('[4,4]', 4),
153     ('{a:3}', 3),
154     ('[4,4]', 4),
155     ('[1,1]', 1),
156     ('[4,4]', 4),
157     ('{x:2}', 2))
158   SELECT json_group_array(DISTINCT json(x) ORDER BY json(x)) FROM c;
159 } {{[[1,1],[4,4],{"a":3},{"x":2}]}}
162 finish_test