CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
aggorderby.test175 linesDownload Raw Back to test
1# 2023-10-182#3# The author disclaims copyright to this source code.  In place of4# 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 14set testdir [file dirname $argv0]15source $testdir/tester.tcl16 17do_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);23}24do_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()}}27do_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()}}30 31do_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}34do_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}37do_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}40do_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}43do_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}46 47do_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}50do_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}53 54do_execsql_test aggorderby-4.0 {55  SELECT count(ORDER BY a) FROM t1;56} 2057do_execsql_test aggorderby-4.1 {58  SELECT c, max(a ORDER BY a) FROM t1;59} {7 9}60 61 62do_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}69do_execsql_test aggorderby-5.1 {70  SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1;71} {aaa/aaa bbb/bbb}72do_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}75do_execsql_test aggorderby-5.3 {76  SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1;77} {aaa#aaa bbb#bbb}78 79# COLLATE works on the ORDER BY.80#81do_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}86do_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}91 92# NULLS FIRST and NULLS LAST work on the ORDER BY93#94do_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]}99do_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]}104 105# The DISTINCT only applies to the function arguments, not to the106# ORDER BY arguments.107#108do_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}112do_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}116do_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} 6120 121# Subtype information is transfered through the sorter for aggregates122# that make use of subtype info.123#124do_execsql_test aggorderby-9.0 {125  WITH c(x,y) AS (VALUES126    ('{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]]}}132do_execsql_test aggorderby-9.1 {133  WITH c(x,y) AS (VALUES134    ('[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]]}}142do_execsql_test aggorderby-9.2 {143  WITH c(x,y) AS (VALUES144    ('{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}]}}150do_execsql_test aggorderby-9.3 {151  WITH c(x,y) AS (VALUES152    ('[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}]}}160 161#-------------------------------------------------------------------------162reset_db163do_execsql_test aggorderby-10.0 {164  CREATE TABLE t1(w, x);165  INSERT INTO t1 VALUES(1, 2);166}167 168for {set i 0} {$i < 70000} {incr i} { lappend lExpr x }169do_catchsql_test aggorderby-10.1 "170  SELECT group_concat(w ORDER BY [join $lExpr ,]) FROM t1171" {1 {too many terms in ORDER BY clause}}172 173 174finish_test175