Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/aggorderby.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / aggorderby.test` 192 lines ยท 6196 bytes Raw Blame History 1 # 2023-10-18 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 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. 9 # 10 #*********************************************************************** 11 # This file implements tests for ORDER BY on aggregate functions. 12 # 13 14 set testdir [file dirname $argv0] 15 source $testdir/tester.tcl 16 set testprefix aggorderby 17 18 do_execsql_test aggorderby-1.1 { 19 CREATE TABLE t1(a TEXT,b INT,c INT,d INT); 20 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<9) 21 INSERT INTO t1(a,b,c,d) SELECT printf('%d',(x*7)%10),1,x,10-x FROM c; 22 INSERT INTO t1(a,b,c,d) SELECT a, 2, c, 10-d FROM t1; 23 CREATE INDEX t1b ON t1(b); 24 } 25 do_catchsql_test aggorderby-1.2 { 26 SELECT b, group_concat(a ORDER BY max(d)) FROM t1 GROUP BY b; 27 } {1 {misuse of aggregate function max()}} 28 do_catchsql_test aggorderby-1.3 { 29 SELECT abs(a ORDER BY max(d)) FROM t1; 30 } {1 {ORDER BY may not be used with non-aggregate abs()}} 31 32 do_execsql_test aggorderby-2.0 { 33 SELECT group_concat(a ORDER BY a) FROM t1 WHERE b=1; 34 } {0,1,2,3,4,5,6,7,8,9} 35 do_execsql_test aggorderby-2.1 { 36 SELECT group_concat(a ORDER BY c) FROM t1 WHERE b=1; 37 } {0,7,4,1,8,5,2,9,6,3} 38 do_execsql_test aggorderby-2.2 { 39 SELECT group_concat(a ORDER BY b, d) FROM t1; 40 } {3,6,9,2,5,8,1,4,7,0,0,7,4,1,8,5,2,9,6,3} 41 do_execsql_test aggorderby-2.3 { 42 SELECT string_agg(a, ',' ORDER BY b DESC, d) FROM t1; 43 } {0,7,4,1,8,5,2,9,6,3,3,6,9,2,5,8,1,4,7,0} 44 do_execsql_test aggorderby-2.4 { 45 SELECT b, group_concat(a ORDER BY d) FROM t1 GROUP BY b ORDER BY b; 46 } {1 3,6,9,2,5,8,1,4,7,0 2 0,7,4,1,8,5,2,9,6,3} 47 48 do_execsql_test aggorderby-3.0 { 49 SELECT group_concat(DISTINCT a ORDER BY a) FROM t1; 50 } {0,1,2,3,4,5,6,7,8,9} 51 do_execsql_test aggorderby-3.1 { 52 SELECT group_concat(DISTINCT a ORDER BY c) FROM t1; 53 } {0,7,4,1,8,5,2,9,6,3} 54 55 do_execsql_test aggorderby-4.0 { 56 SELECT count(ORDER BY a) FROM t1; 57 } 20 58 do_execsql_test aggorderby-4.1 { 59 SELECT c, max(a ORDER BY a) FROM t1; 60 } {7 9} 61 62 63 do_execsql_test aggorderby-5.0 { 64 DROP TABLE IF EXISTS t1; 65 DROP TABLE IF EXISTS t3; 66 CREATE TABLE t1(a TEXT); INSERT INTO t1 VALUES('aaa'),('bbb'); 67 CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('/'),('-'); 68 SELECT (SELECT string_agg(a,d) FROM t3) FROM t1; 69 } {aaa-aaa bbb-bbb} 70 do_execsql_test aggorderby-5.1 { 71 SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1; 72 } {aaa/aaa bbb/bbb} 73 do_execsql_test aggorderby-5.2 { 74 SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1; 75 } {aaa-aaa bbb-bbb} 76 do_execsql_test aggorderby-5.3 { 77 SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1; 78 } {aaa#aaa bbb#bbb} 79 80 # COLLATE works on the ORDER BY. 81 # 82 do_execsql_test aggorderby-6.0 { 83 WITH c(x) AS (VALUES('abc'),('DEF'),('xyz'),('ABC'),('XYZ')) 84 SELECT string_agg(x,',' ORDER BY x COLLATE nocase), 85 string_agg(x,',' ORDER BY x) FROM c; 86 } {abc,ABC,DEF,xyz,XYZ ABC,DEF,XYZ,abc,xyz} 87 do_execsql_test aggorderby-6.1 { 88 WITH c(x,y) AS (VALUES(1,'a'),(2,'B'),(3,'c'),(4,'D')) 89 SELECT group_concat(x ORDER BY y COLLATE nocase), 90 group_concat(x ORDER BY y COLLATE binary) FROM c; 91 } {1,2,3,4 2,4,1,3} 92 93 # NULLS FIRST and NULLS LAST work on the ORDER BY 94 # 95 do_execsql_test aggorderby-7.0 { 96 WITH c(x) AS (VALUES(1),(NULL),(2.5),(NULL),('three')) 97 SELECT json_group_array(x ORDER BY x NULLS FIRST), 98 json_group_array(x ORDER BY x NULLS LAST) FROM c; 99 } {[null,null,1,2.5,"three"] [1,2.5,"three",null,null]} 100 do_execsql_test aggorderby-7.1 { 101 WITH c(x,y) AS (VALUES(1,9),(2,null),(3,5),(4,null),(5,1)) 102 SELECT json_group_array(x ORDER BY y NULLS FIRST, x), 103 json_group_array(x ORDER BY y NULLS LAST, x) FROM c; 104 } {[2,4,5,3,1] [5,3,1,2,4]} 105 106 # The DISTINCT only applies to the function arguments, not to the 107 # ORDER BY arguments. 108 # 109 do_execsql_test aggorderby-8.0 { 110 WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('c',2,7),('c',1,8)) 111 SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; 112 } {c,b,a} 113 do_execsql_test aggorderby-8.1 { 114 WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('b',2,7),('c',1,8)) 115 SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; 116 } {c,b,a} 117 do_execsql_test aggorderby-8.2 { 118 WITH c(x,y) AS (VALUES(1,1),(2,2),(3,3),(3,4),(3,5),(3,6)) 119 SELECT sum(DISTINCT x ORDER BY y) FROM c; 120 } 6 121 122 # Subtype information is transfered through the sorter for aggregates 123 # that make use of subtype info. 124 # 125 do_execsql_test aggorderby-9.0 { 126 WITH c(x,y) AS (VALUES 127 ('{a:3}', 3), 128 ('[1,1]', 1), 129 ('[4,4]', 4), 130 ('{x:2}', 2)) 131 SELECT json_group_array(json(x) ORDER BY y) FROM c; 132 } {{[[1,1],{"x":2},{"a":3},[4,4]]}} 133 do_execsql_test aggorderby-9.1 { 134 WITH c(x,y) AS (VALUES 135 ('[4,4]', 4), 136 ('{a:3}', 3), 137 ('[4,4]', 4), 138 ('[1,1]', 1), 139 ('[4,4]', 4), 140 ('{x:2}', 2)) 141 SELECT json_group_array(DISTINCT json(x) ORDER BY y) FROM c; 142 } {{[[1,1],{"x":2},{"a":3},[4,4]]}} 143 do_execsql_test aggorderby-9.2 { 144 WITH c(x,y) AS (VALUES 145 ('{a:3}', 3), 146 ('[1,1]', 1), 147 ('[4,4]', 4), 148 ('{x:2}', 2)) 149 SELECT json_group_array(json(x) ORDER BY json(x)) FROM c; 150 } {{[[1,1],[4,4],{"a":3},{"x":2}]}} 151 do_execsql_test aggorderby-9.3 { 152 WITH c(x,y) AS (VALUES 153 ('[4,4]', 4), 154 ('{a:3}', 3), 155 ('[4,4]', 4), 156 ('[1,1]', 1), 157 ('[4,4]', 4), 158 ('{x:2}', 2)) 159 SELECT json_group_array(DISTINCT json(x) ORDER BY json(x)) FROM c; 160 } {{[[1,1],[4,4],{"a":3},{"x":2}]}} 161 162 #------------------------------------------------------------------------- 163 reset_db 164 do_execsql_test aggorderby-10.0 { 165 CREATE TABLE t1(w, x); 166 INSERT INTO t1 VALUES(1, 2); 167 } 168 169 for {set i 0} {$i < 70000} {incr i} { lappend lExpr x } 170 do_catchsql_test aggorderby-10.1 " 171 SELECT group_concat(w ORDER BY [join $lExpr ,]) FROM t1 172 " {1 {too many terms in ORDER BY clause}} 173 174 #------------------------------------------------------------------------- 175 reset_db 176 177 do_execsql_test 11.0 { 178 CREATE TABLE t1(a); 179 CREATE TABLE t2(b); 180 INSERT INTO t1 VALUES(42); 181 INSERT INTO t2 VALUES(2); 182 } 183 184 do_execsql_test 11.1 { 185 SELECT sum(b ORDER BY a) FROM t1, t2; 186 } {2} 187 188 do_execsql_test 11.2 { 189 SELECT group_concat(b ORDER BY a) FROM t1, t2; 190 } {2} 191 192 finish_test