186fb6e17Sdan# 2018 May 8 286fb6e17Sdan# 386fb6e17Sdan# The author disclaims copyright to this source code. In place of 486fb6e17Sdan# a legal notice, here is a blessing: 586fb6e17Sdan# 686fb6e17Sdan# May you do good and not evil. 786fb6e17Sdan# May you find forgiveness for yourself and forgive others. 886fb6e17Sdan# May you share freely, never taking more than you give. 986fb6e17Sdan# 1086fb6e17Sdan#*********************************************************************** 1186fb6e17Sdan# This file implements regression tests for SQLite library. 1286fb6e17Sdan# 1386fb6e17Sdan 1486fb6e17Sdanset testdir [file dirname $argv0] 1586fb6e17Sdansource $testdir/tester.tcl 1686fb6e17Sdanset testprefix window1 1786fb6e17Sdan 1867a9b8edSdanifcapable !windowfunc { 1967a9b8edSdan finish_test 2067a9b8edSdan return 2167a9b8edSdan} 2286fb6e17Sdan 2386fb6e17Sdando_execsql_test 1.0 { 2486fb6e17Sdan CREATE TABLE t1(a, b, c, d); 2586fb6e17Sdan INSERT INTO t1 VALUES(1, 2, 3, 4); 2686fb6e17Sdan INSERT INTO t1 VALUES(5, 6, 7, 8); 2786fb6e17Sdan INSERT INTO t1 VALUES(9, 10, 11, 12); 2886fb6e17Sdan} 2986fb6e17Sdan 3086fb6e17Sdando_execsql_test 1.1 { 3186fb6e17Sdan SELECT sum(b) OVER () FROM t1 3286fb6e17Sdan} {18 18 18} 3386fb6e17Sdan 3486fb6e17Sdando_execsql_test 1.2 { 3586fb6e17Sdan SELECT a, sum(b) OVER () FROM t1 3686fb6e17Sdan} {1 18 5 18 9 18} 3786fb6e17Sdan 3886fb6e17Sdando_execsql_test 1.3 { 3986fb6e17Sdan SELECT a, 4 + sum(b) OVER () FROM t1 4086fb6e17Sdan} {1 22 5 22 9 22} 4186fb6e17Sdan 4286fb6e17Sdando_execsql_test 1.4 { 4386fb6e17Sdan SELECT a + 4 + sum(b) OVER () FROM t1 4486fb6e17Sdan} {23 27 31} 4586fb6e17Sdan 4686fb6e17Sdando_execsql_test 1.5 { 4786fb6e17Sdan SELECT a, sum(b) OVER (PARTITION BY c) FROM t1 4886fb6e17Sdan} {1 2 5 6 9 10} 4986fb6e17Sdan 5086fb6e17Sdanforeach {tn sql} { 5186fb6e17Sdan 1 "SELECT sum(b) OVER () FROM t1" 5286fb6e17Sdan 2 "SELECT sum(b) OVER (PARTITION BY c) FROM t1" 5386fb6e17Sdan 3 "SELECT sum(b) OVER (ORDER BY c) FROM t1" 5486fb6e17Sdan 4 "SELECT sum(b) OVER (PARTITION BY d ORDER BY c) FROM t1" 5586fb6e17Sdan 5 "SELECT sum(b) FILTER (WHERE a>0) OVER (PARTITION BY d ORDER BY c) FROM t1" 5686fb6e17Sdan 6 "SELECT sum(b) OVER (ORDER BY c RANGE UNBOUNDED PRECEDING) FROM t1" 5786fb6e17Sdan 7 "SELECT sum(b) OVER (ORDER BY c ROWS 45 PRECEDING) FROM t1" 5886fb6e17Sdan 8 "SELECT sum(b) OVER (ORDER BY c RANGE CURRENT ROW) FROM t1" 5986fb6e17Sdan 9 "SELECT sum(b) OVER (ORDER BY c RANGE BETWEEN UNBOUNDED PRECEDING 6086fb6e17Sdan AND CURRENT ROW) FROM t1" 6186fb6e17Sdan 10 "SELECT sum(b) OVER (ORDER BY c ROWS BETWEEN UNBOUNDED PRECEDING 6286fb6e17Sdan AND UNBOUNDED FOLLOWING) FROM t1" 6386fb6e17Sdan} { 6486fb6e17Sdan do_test 2.$tn { lindex [catchsql $sql] 0 } 0 6586fb6e17Sdan} 6686fb6e17Sdan 6786fb6e17Sdanforeach {tn sql} { 6886fb6e17Sdan 1 "SELECT * FROM t1 WHERE sum(b) OVER ()" 6986fb6e17Sdan 2 "SELECT * FROM t1 GROUP BY sum(b) OVER ()" 7086fb6e17Sdan 3 "SELECT * FROM t1 GROUP BY a HAVING sum(b) OVER ()" 7186fb6e17Sdan} { 7286fb6e17Sdan do_catchsql_test 3.$tn $sql {1 {misuse of window function sum()}} 7386fb6e17Sdan} 7486fb6e17Sdan 7586fb6e17Sdando_execsql_test 4.0 { 7686fb6e17Sdan CREATE TABLE t2(a, b, c); 7786fb6e17Sdan INSERT INTO t2 VALUES(0, 0, 0); 7886fb6e17Sdan INSERT INTO t2 VALUES(1, 1, 1); 7986fb6e17Sdan INSERT INTO t2 VALUES(2, 0, 2); 8086fb6e17Sdan INSERT INTO t2 VALUES(3, 1, 0); 8186fb6e17Sdan INSERT INTO t2 VALUES(4, 0, 1); 8286fb6e17Sdan INSERT INTO t2 VALUES(5, 1, 2); 8386fb6e17Sdan INSERT INTO t2 VALUES(6, 0, 0); 8486fb6e17Sdan} 8586fb6e17Sdan 8686fb6e17Sdando_execsql_test 4.1 { 8786fb6e17Sdan SELECT a, sum(a) OVER (PARTITION BY b) FROM t2; 8886fb6e17Sdan} { 8986fb6e17Sdan 0 12 2 12 4 12 6 12 1 9 3 9 5 9 9086fb6e17Sdan} 9186fb6e17Sdan 9286fb6e17Sdando_execsql_test 4.2 { 9386fb6e17Sdan SELECT a, sum(a) OVER (PARTITION BY b) FROM t2 ORDER BY a; 9486fb6e17Sdan} { 9586fb6e17Sdan 0 12 1 9 2 12 3 9 4 12 5 9 6 12 9686fb6e17Sdan} 9786fb6e17Sdan 9886fb6e17Sdando_execsql_test 4.3 { 9986fb6e17Sdan SELECT a, sum(a) OVER () FROM t2 ORDER BY a; 10086fb6e17Sdan} { 10186fb6e17Sdan 0 21 1 21 2 21 3 21 4 21 5 21 6 21 10286fb6e17Sdan} 10386fb6e17Sdan 10486fb6e17Sdando_execsql_test 4.4 { 10586fb6e17Sdan SELECT a, sum(a) OVER (ORDER BY a) FROM t2; 10686fb6e17Sdan} { 10786fb6e17Sdan 0 0 1 1 2 3 3 6 4 10 5 15 6 21 10886fb6e17Sdan} 10986fb6e17Sdan 11086fb6e17Sdando_execsql_test 4.5 { 11186fb6e17Sdan SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a) FROM t2 ORDER BY a 11286fb6e17Sdan} { 11386fb6e17Sdan 0 0 1 1 2 2 3 4 4 6 5 9 6 12 11486fb6e17Sdan} 11586fb6e17Sdan 1162e362f97Sdando_execsql_test 4.6 { 1172e362f97Sdan SELECT a, sum(a) OVER (PARTITION BY c ORDER BY a) FROM t2 ORDER BY a 1182e362f97Sdan} { 1192e362f97Sdan 0 0 1 1 2 2 3 3 4 5 5 7 6 9 1202e362f97Sdan} 1212e362f97Sdan 1222e362f97Sdando_execsql_test 4.7 { 1232e362f97Sdan SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a DESC) FROM t2 ORDER BY a 1242e362f97Sdan} { 1252e362f97Sdan 0 12 1 9 2 12 3 8 4 10 5 5 6 6 1262e362f97Sdan} 1272e362f97Sdan 1282e362f97Sdando_execsql_test 4.8 { 1292e362f97Sdan SELECT a, 1302e362f97Sdan sum(a) OVER (PARTITION BY b ORDER BY a DESC), 1312e362f97Sdan sum(a) OVER (PARTITION BY c ORDER BY a) 1322e362f97Sdan FROM t2 ORDER BY a 1332e362f97Sdan} { 1342e362f97Sdan 0 12 0 1352e362f97Sdan 1 9 1 1362e362f97Sdan 2 12 2 1372e362f97Sdan 3 8 3 1382e362f97Sdan 4 10 5 1392e362f97Sdan 5 5 7 1402e362f97Sdan 6 6 9 1412e362f97Sdan} 1422e362f97Sdan 143e2f781b9Sdando_execsql_test 4.9 { 144e2f781b9Sdan SELECT a, 145e2f781b9Sdan sum(a) OVER (ORDER BY a), 146e2f781b9Sdan avg(a) OVER (ORDER BY a) 147e2f781b9Sdan FROM t2 ORDER BY a 148e2f781b9Sdan} { 149e2f781b9Sdan 0 0 0.0 150e2f781b9Sdan 1 1 0.5 151e2f781b9Sdan 2 3 1.0 152e2f781b9Sdan 3 6 1.5 153e2f781b9Sdan 4 10 2.0 154e2f781b9Sdan 5 15 2.5 155e2f781b9Sdan 6 21 3.0 156e2f781b9Sdan} 157e2f781b9Sdan 158b6e9f7a4Sdando_execsql_test 4.10.1 { 159e2f781b9Sdan SELECT a, 160e2f781b9Sdan count() OVER (ORDER BY a DESC), 161e2f781b9Sdan group_concat(a, '.') OVER (ORDER BY a DESC) 162e2f781b9Sdan FROM t2 ORDER BY a DESC 163e2f781b9Sdan} { 164e2f781b9Sdan 6 1 6 165e2f781b9Sdan 5 2 6.5 166e2f781b9Sdan 4 3 6.5.4 167e2f781b9Sdan 3 4 6.5.4.3 168e2f781b9Sdan 2 5 6.5.4.3.2 169e2f781b9Sdan 1 6 6.5.4.3.2.1 170e2f781b9Sdan 0 7 6.5.4.3.2.1.0 171e2f781b9Sdan} 172e2f781b9Sdan 173b6e9f7a4Sdando_execsql_test 4.10.2 { 174b6e9f7a4Sdan SELECT a, 175b6e9f7a4Sdan count(*) OVER (ORDER BY a DESC), 176b6e9f7a4Sdan group_concat(a, '.') OVER (ORDER BY a DESC) 177b6e9f7a4Sdan FROM t2 ORDER BY a DESC 178b6e9f7a4Sdan} { 179b6e9f7a4Sdan 6 1 6 180b6e9f7a4Sdan 5 2 6.5 181b6e9f7a4Sdan 4 3 6.5.4 182b6e9f7a4Sdan 3 4 6.5.4.3 183b6e9f7a4Sdan 2 5 6.5.4.3.2 184b6e9f7a4Sdan 1 6 6.5.4.3.2.1 185b6e9f7a4Sdan 0 7 6.5.4.3.2.1.0 186b6e9f7a4Sdan} 187b6e9f7a4Sdan 1886bc5c9e7Sdando_catchsql_test 5.1 { 1896bc5c9e7Sdan SELECT ntile(0) OVER (ORDER BY a) FROM t2; 1906bc5c9e7Sdan} {1 {argument of ntile must be a positive integer}} 1916bc5c9e7Sdando_catchsql_test 5.2 { 1926bc5c9e7Sdan SELECT ntile(-1) OVER (ORDER BY a) FROM t2; 1936bc5c9e7Sdan} {1 {argument of ntile must be a positive integer}} 1946bc5c9e7Sdando_catchsql_test 5.3 { 1956bc5c9e7Sdan SELECT ntile('zbc') OVER (ORDER BY a) FROM t2; 1966bc5c9e7Sdan} {1 {argument of ntile must be a positive integer}} 1976bc5c9e7Sdando_execsql_test 5.4 { 1986bc5c9e7Sdan CREATE TABLE t4(a, b); 1996bc5c9e7Sdan SELECT ntile(1) OVER (ORDER BY a) FROM t4; 2006bc5c9e7Sdan} {} 2016bc5c9e7Sdan 202dacf1de9Sdan#------------------------------------------------------------------------- 203dacf1de9Sdanreset_db 204dacf1de9Sdando_execsql_test 6.1 { 205dacf1de9Sdan CREATE TABLE t1(x); 206dacf1de9Sdan INSERT INTO t1 VALUES(7), (6), (5), (4), (3), (2), (1); 207dacf1de9Sdan 208dacf1de9Sdan CREATE TABLE t2(x); 209dacf1de9Sdan INSERT INTO t2 VALUES('b'), ('a'); 210dacf1de9Sdan 211dacf1de9Sdan SELECT x, count(*) OVER (ORDER BY x) FROM t1; 212dacf1de9Sdan} {1 1 2 2 3 3 4 4 5 5 6 6 7 7} 213dacf1de9Sdan 214dacf1de9Sdando_execsql_test 6.2 { 215*a3fc683cSdrh SELECT * FROM t2, (SELECT x, count(*) OVER (ORDER BY x) FROM t1) 216*a3fc683cSdrh ORDER BY 1, 2; 217dacf1de9Sdan} { 218dacf1de9Sdan a 1 1 a 2 2 a 3 3 a 4 4 a 5 5 a 6 6 a 7 7 219*a3fc683cSdrh b 1 1 b 2 2 b 3 3 b 4 4 b 5 5 b 6 6 b 7 7 220dacf1de9Sdan} 221dacf1de9Sdan 22226522d1cSdando_catchsql_test 6.3 { 2238b98560dSdan SELECT x, lag(x) FILTER (WHERE (x%2)=0) OVER w FROM t1 2248b98560dSdan WINDOW w AS (ORDER BY x) 2258b98560dSdan} {1 {FILTER clause may only be used with aggregate window functions}} 226e2f781b9Sdan 22726522d1cSdan#------------------------------------------------------------------------- 22826522d1cSdan# Attempt to use a window function as an aggregate. And other errors. 22926522d1cSdan# 23026522d1cSdanreset_db 23126522d1cSdando_execsql_test 7.0 { 23226522d1cSdan CREATE TABLE t1(x, y); 23326522d1cSdan INSERT INTO t1 VALUES(1, 2); 23426522d1cSdan INSERT INTO t1 VALUES(3, 4); 23526522d1cSdan INSERT INTO t1 VALUES(5, 6); 23626522d1cSdan INSERT INTO t1 VALUES(7, 8); 23726522d1cSdan INSERT INTO t1 VALUES(9, 10); 23826522d1cSdan} 23926522d1cSdan 24026522d1cSdando_catchsql_test 7.1.1 { 24126522d1cSdan SELECT nth_value(x, 1) FROM t1; 24226522d1cSdan} {1 {misuse of window function nth_value()}} 24326522d1cSdando_catchsql_test 7.1.2 { 24426522d1cSdan SELECT * FROM t1 WHERE nth_value(x, 1) OVER (ORDER BY y); 24526522d1cSdan} {1 {misuse of window function nth_value()}} 24626522d1cSdando_catchsql_test 7.1.3 { 24726522d1cSdan SELECT count(*) FROM t1 GROUP BY y HAVING nth_value(x, 1) OVER (ORDER BY y); 24826522d1cSdan} {1 {misuse of window function nth_value()}} 24926522d1cSdando_catchsql_test 7.1.4 { 25026522d1cSdan SELECT count(*) FROM t1 GROUP BY nth_value(x, 1) OVER (ORDER BY y); 25126522d1cSdan} {1 {misuse of window function nth_value()}} 25226522d1cSdando_catchsql_test 7.1.5 { 253c3163073Sdan SELECT count(*) FROM t1 LIMIT nth_value(x, 1) OVER (); 25426522d1cSdan} {1 {no such column: x}} 25526522d1cSdando_catchsql_test 7.1.6 { 25626522d1cSdan SELECT trim(x) OVER (ORDER BY y) FROM t1; 25726522d1cSdan} {1 {trim() may not be used as a window function}} 2589a94722dSdando_catchsql_test 7.1.7 { 2599a94722dSdan SELECT max(x) OVER abc FROM t1 WINDOW def AS (ORDER BY y); 2609a94722dSdan} {1 {no such window: abc}} 2615e61c1b7Sdando_catchsql_test 7.1.8 { 2625e61c1b7Sdan SELECT row_number(x) OVER () FROM t1 2638f245174Sdan} {1 {wrong number of arguments to function row_number()}} 2649a94722dSdan 265e0a5e20fSdando_execsql_test 7.2 { 266e0a5e20fSdan SELECT 267e0a5e20fSdan lead(y) OVER win, 268e0a5e20fSdan lead(y, 2) OVER win, 269e0a5e20fSdan lead(y, 3, 'default') OVER win 270e0a5e20fSdan FROM t1 271e0a5e20fSdan WINDOW win AS (ORDER BY x) 272e0a5e20fSdan} { 273e0a5e20fSdan 4 6 8 6 8 10 8 10 default 10 {} default {} {} default 274e0a5e20fSdan} 275e0a5e20fSdan 27613b08bb6Sdando_execsql_test 7.3 { 27713b08bb6Sdan SELECT row_number() OVER (ORDER BY x) FROM t1 27813b08bb6Sdan} {1 2 3 4 5} 27913b08bb6Sdan 280660af939Sdando_execsql_test 7.4 { 281660af939Sdan SELECT 282660af939Sdan row_number() OVER win, 283660af939Sdan lead(x) OVER win 284660af939Sdan FROM t1 285660af939Sdan WINDOW win AS (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 286660af939Sdan} {1 3 2 5 3 7 4 9 5 {}} 28726522d1cSdan 288c95f38d4Sdan#------------------------------------------------------------------------- 289c95f38d4Sdan# Attempt to use a window function in a view. 290c95f38d4Sdan# 291c95f38d4Sdando_execsql_test 8.0 { 292c95f38d4Sdan CREATE TABLE t3(a, b, c); 293c95f38d4Sdan 294c95f38d4Sdan WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<6 ) 295c95f38d4Sdan INSERT INTO t3 SELECT i, i, i FROM s; 296c95f38d4Sdan 297c95f38d4Sdan CREATE VIEW v1 AS SELECT 298c95f38d4Sdan sum(b) OVER (ORDER BY c), 299c95f38d4Sdan min(b) OVER (ORDER BY c), 300c95f38d4Sdan max(b) OVER (ORDER BY c) 301c95f38d4Sdan FROM t3; 302c95f38d4Sdan 303c95f38d4Sdan CREATE VIEW v2 AS SELECT 304c95f38d4Sdan sum(b) OVER win, 305c95f38d4Sdan min(b) OVER win, 306c95f38d4Sdan max(b) OVER win 307c95f38d4Sdan FROM t3 308c95f38d4Sdan WINDOW win AS (ORDER BY c); 309c95f38d4Sdan} 310c95f38d4Sdan 311c95f38d4Sdando_execsql_test 8.1.1 { 312c95f38d4Sdan SELECT * FROM v1 313c95f38d4Sdan} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} 314c95f38d4Sdando_execsql_test 8.1.2 { 315c95f38d4Sdan SELECT * FROM v2 316c95f38d4Sdan} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} 317c95f38d4Sdan 318c95f38d4Sdandb close 319c95f38d4Sdansqlite3 db test.db 320c95f38d4Sdando_execsql_test 8.2.1 { 321c95f38d4Sdan SELECT * FROM v1 322c95f38d4Sdan} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} 323c95f38d4Sdando_execsql_test 8.2.2 { 324c95f38d4Sdan SELECT * FROM v2 325c95f38d4Sdan} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} 326c95f38d4Sdan 3276fb2b54cSdan#------------------------------------------------------------------------- 3286fb2b54cSdan# Attempt to use a window function in a trigger. 3296fb2b54cSdan# 3306fb2b54cSdando_execsql_test 9.0 { 3316fb2b54cSdan CREATE TABLE t4(x, y); 3326fb2b54cSdan INSERT INTO t4 VALUES(1, 'g'); 3336fb2b54cSdan INSERT INTO t4 VALUES(2, 'i'); 3346fb2b54cSdan INSERT INTO t4 VALUES(3, 'l'); 3356fb2b54cSdan INSERT INTO t4 VALUES(4, 'g'); 3366fb2b54cSdan INSERT INTO t4 VALUES(5, 'a'); 3376fb2b54cSdan 3386fb2b54cSdan CREATE TABLE t5(x, y, m); 3396fb2b54cSdan CREATE TRIGGER t4i AFTER INSERT ON t4 BEGIN 3406fb2b54cSdan DELETE FROM t5; 3416fb2b54cSdan INSERT INTO t5 3426fb2b54cSdan SELECT x, y, max(y) OVER xyz FROM t4 3436fb2b54cSdan WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x); 3446fb2b54cSdan END; 3456fb2b54cSdan} 3466fb2b54cSdan 3476fb2b54cSdando_execsql_test 9.1.1 { 3486fb2b54cSdan SELECT x, y, max(y) OVER xyz FROM t4 3496fb2b54cSdan WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1 3506fb2b54cSdan} {1 g g 2 i i 3 l l 4 g i 5 a l} 3516fb2b54cSdan 3526fb2b54cSdando_execsql_test 9.1.2 { 3536fb2b54cSdan INSERT INTO t4 VALUES(6, 'm'); 3546fb2b54cSdan SELECT x, y, max(y) OVER xyz FROM t4 3556fb2b54cSdan WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1 3566fb2b54cSdan} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} 3576fb2b54cSdan 3586fb2b54cSdando_execsql_test 9.1.3 { 3596fb2b54cSdan SELECT * FROM t5 ORDER BY 1 3606fb2b54cSdan} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} 361c95f38d4Sdan 362cc464418Sdando_execsql_test 9.2 { 363cc464418Sdan WITH aaa(x, y, z) AS ( 364cc464418Sdan SELECT x, y, max(y) OVER xyz FROM t4 365cc464418Sdan WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) 366cc464418Sdan ) 367cc464418Sdan SELECT * FROM aaa ORDER BY 1; 368cc464418Sdan} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} 369cc464418Sdan 370cc464418Sdando_execsql_test 9.3 { 371cc464418Sdan WITH aaa(x, y, z) AS ( 372cc464418Sdan SELECT x, y, max(y) OVER xyz FROM t4 373cc464418Sdan WINDOW xyz AS (ORDER BY x) 374cc464418Sdan ) 375cc464418Sdan SELECT *, min(z) OVER (ORDER BY x) FROM aaa ORDER BY 1; 376cc464418Sdan} {1 g g g 2 i i g 3 l l g 4 g l g 5 a l g 6 m m g} 377c95f38d4Sdan 3787b0d34feSdrhdo_catchsql_test 9.4 { 3797b0d34feSdrh -- 2021-04-17 dbsqlfuzz d9cf66100064952b66951845dfab41de1c124611 3807b0d34feSdrh DROP TABLE IF EXISTS t1; 3817b0d34feSdrh CREATE TABLE t1(a,b,c,d); 3827b0d34feSdrh DROP TABLE IF EXISTS t2; 3837b0d34feSdrh CREATE TABLE t2(x,y); 3847b0d34feSdrh CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN 3857b0d34feSdrh INSERT INTO t2(x,y) 3867b0d34feSdrh SELECT a, max(d) OVER w1 FROM t1 3877b0d34feSdrh WINDOW w1 AS (PARTITION BY EXISTS(SELECT 1 FROM t1 WHERE c=?1) ); 3887b0d34feSdrh END; 3897b0d34feSdrh} {1 {trigger cannot use variables}} 3907b0d34feSdrh 3913c6fbd6dSdando_catchsql_test 9.4.2 { 3923c6fbd6dSdan CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN 3933c6fbd6dSdan INSERT INTO t1(a,b) 3943c6fbd6dSdan SELECT a, max(d) OVER w1 FROM t1 3953c6fbd6dSdan WINDOW w1 AS ( 3963c6fbd6dSdan ORDER BY a ROWS BETWEEN ? PRECEDING AND UNBOUNDED FOLLOWING 3973c6fbd6dSdan ); 3983c6fbd6dSdan END; 3993c6fbd6dSdan} {1 {trigger cannot use variables}} 4003c6fbd6dSdando_catchsql_test 9.4.3 { 4013c6fbd6dSdan CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN 4023c6fbd6dSdan INSERT INTO t1(a,b) 4033c6fbd6dSdan SELECT a, max(d) OVER w1 FROM t1 4043c6fbd6dSdan WINDOW w1 AS ( 4053c6fbd6dSdan ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND ? FOLLOWING 4063c6fbd6dSdan ); 4073c6fbd6dSdan END; 4083c6fbd6dSdan} {1 {trigger cannot use variables}} 4093c6fbd6dSdan 410ce103735Sdan#------------------------------------------------------------------------- 411ce103735Sdan# 412ce103735Sdando_execsql_test 10.0 { 413ce103735Sdan CREATE TABLE sales(emp TEXT PRIMARY KEY, region, total); 414ce103735Sdan INSERT INTO sales VALUES 415ce103735Sdan ('Alice', 'North', 34), 416ce103735Sdan ('Frank', 'South', 22), 417ce103735Sdan ('Charles', 'North', 45), 418ce103735Sdan ('Darrell', 'South', 8), 419ce103735Sdan ('Grant', 'South', 23), 420ce103735Sdan ('Brad' , 'North', 22), 421ce103735Sdan ('Elizabeth', 'South', 99), 422ce103735Sdan ('Horace', 'East', 1); 423ce103735Sdan} 424ce103735Sdan 425ce103735Sdan# Best two salespeople from each region 426ce103735Sdan# 427ce103735Sdando_execsql_test 10.1 { 428ce103735Sdan SELECT emp, region, total FROM ( 429ce103735Sdan SELECT 430ce103735Sdan emp, region, total, 431ce103735Sdan row_number() OVER (PARTITION BY region ORDER BY total DESC) AS rank 432ce103735Sdan FROM sales 433ce103735Sdan ) WHERE rank<=2 ORDER BY region, total DESC 434ce103735Sdan} { 435ce103735Sdan Horace East 1 436ce103735Sdan Charles North 45 437ce103735Sdan Alice North 34 438ce103735Sdan Elizabeth South 99 439ce103735Sdan Grant South 23 440ce103735Sdan} 441ce103735Sdan 442efa3a3c9Sdando_execsql_test 10.2 { 443efa3a3c9Sdan SELECT emp, region, sum(total) OVER win FROM sales 444efa3a3c9Sdan WINDOW win AS (PARTITION BY region ORDER BY total) 445efa3a3c9Sdan} { 446efa3a3c9Sdan Horace East 1 447efa3a3c9Sdan Brad North 22 448efa3a3c9Sdan Alice North 56 449efa3a3c9Sdan Charles North 101 450efa3a3c9Sdan Darrell South 8 451efa3a3c9Sdan Frank South 30 452efa3a3c9Sdan Grant South 53 453efa3a3c9Sdan Elizabeth South 152 454efa3a3c9Sdan} 455efa3a3c9Sdan 456efa3a3c9Sdando_execsql_test 10.3 { 457efa3a3c9Sdan SELECT emp, region, sum(total) OVER win FROM sales 458efa3a3c9Sdan WINDOW win AS (PARTITION BY region ORDER BY total) 459efa3a3c9Sdan LIMIT 5 460efa3a3c9Sdan} { 461efa3a3c9Sdan Horace East 1 462efa3a3c9Sdan Brad North 22 463efa3a3c9Sdan Alice North 56 464efa3a3c9Sdan Charles North 101 465efa3a3c9Sdan Darrell South 8 466efa3a3c9Sdan} 467efa3a3c9Sdan 468efa3a3c9Sdando_execsql_test 10.4 { 469efa3a3c9Sdan SELECT emp, region, sum(total) OVER win FROM sales 470efa3a3c9Sdan WINDOW win AS (PARTITION BY region ORDER BY total) 471efa3a3c9Sdan LIMIT 5 OFFSET 2 472efa3a3c9Sdan} { 473efa3a3c9Sdan Alice North 56 474efa3a3c9Sdan Charles North 101 475efa3a3c9Sdan Darrell South 8 476efa3a3c9Sdan Frank South 30 477efa3a3c9Sdan Grant South 53 478efa3a3c9Sdan} 479efa3a3c9Sdan 480c3163073Sdando_execsql_test 10.5 { 481c3163073Sdan SELECT emp, region, sum(total) OVER win FROM sales 482c3163073Sdan WINDOW win AS ( 483c3163073Sdan PARTITION BY region ORDER BY total 484c3163073Sdan ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING 485c3163073Sdan ) 486c3163073Sdan} { 487c3163073Sdan Horace East 1 488c3163073Sdan Brad North 101 489c3163073Sdan Alice North 79 490c3163073Sdan Charles North 45 491c3163073Sdan Darrell South 152 492c3163073Sdan Frank South 144 493c3163073Sdan Grant South 122 494c3163073Sdan Elizabeth South 99 495c3163073Sdan} 496c3163073Sdan 497c3163073Sdando_execsql_test 10.6 { 498c3163073Sdan SELECT emp, region, sum(total) OVER win FROM sales 499c3163073Sdan WINDOW win AS ( 500c3163073Sdan PARTITION BY region ORDER BY total 501c3163073Sdan ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING 502c3163073Sdan ) LIMIT 5 OFFSET 2 503c3163073Sdan} { 504c3163073Sdan Alice North 79 505c3163073Sdan Charles North 45 506c3163073Sdan Darrell South 152 507c3163073Sdan Frank South 144 508c3163073Sdan Grant South 122 509c3163073Sdan} 510c3163073Sdan 511c3163073Sdando_execsql_test 10.7 { 512c3163073Sdan SELECT emp, region, ( 513c3163073Sdan SELECT sum(total) OVER ( 514c3163073Sdan ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING 515c3163073Sdan ) || outer.emp FROM sales 516c3163073Sdan ) FROM sales AS outer; 517c3163073Sdan} { 518c3163073Sdan Alice North 254Alice 519c3163073Sdan Frank South 254Frank 520c3163073Sdan Charles North 254Charles 521c3163073Sdan Darrell South 254Darrell 522c3163073Sdan Grant South 254Grant 523c3163073Sdan Brad North 254Brad 524c3163073Sdan Elizabeth South 254Elizabeth 525c3163073Sdan Horace East 254Horace 526c3163073Sdan} 527c3163073Sdan 528c3163073Sdando_execsql_test 10.8 { 529c3163073Sdan SELECT emp, region, ( 530c3163073Sdan SELECT sum(total) FILTER (WHERE sales.emp!=outer.emp) OVER ( 531c3163073Sdan ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING 532c3163073Sdan ) FROM sales 533c3163073Sdan ) FROM sales AS outer; 534c3163073Sdan} { 535c3163073Sdan Alice North 220 536c3163073Sdan Frank South 232 537c3163073Sdan Charles North 209 538c3163073Sdan Darrell South 246 539c3163073Sdan Grant South 231 540c3163073Sdan Brad North 232 541c3163073Sdan Elizabeth South 155 542c3163073Sdan Horace East 253 543c3163073Sdan} 544c3163073Sdan 545867be212Sdan#------------------------------------------------------------------------- 546867be212Sdan# Check that it is not possible to use a window function in a CREATE INDEX 547867be212Sdan# statement. 548867be212Sdan# 549867be212Sdando_execsql_test 11.0 { CREATE TABLE t6(a, b, c); } 550867be212Sdan 551867be212Sdando_catchsql_test 11.1 { 552867be212Sdan CREATE INDEX t6i ON t6(a) WHERE sum(b) OVER (); 553867be212Sdan} {1 {misuse of window function sum()}} 554867be212Sdando_catchsql_test 11.2 { 555867be212Sdan CREATE INDEX t6i ON t6(a) WHERE lead(b) OVER (); 556867be212Sdan} {1 {misuse of window function lead()}} 557867be212Sdan 558867be212Sdando_catchsql_test 11.3 { 559867be212Sdan CREATE INDEX t6i ON t6(sum(b) OVER ()); 560867be212Sdan} {1 {misuse of window function sum()}} 561867be212Sdando_catchsql_test 11.4 { 562867be212Sdan CREATE INDEX t6i ON t6(lead(b) OVER ()); 563867be212Sdan} {1 {misuse of window function lead()}} 564c3163073Sdan 565d4cb09e3Sdrh# 2018-09-17 ticket 510cde277783b5fb5de628393959849dff377eb3 566d4cb09e3Sdrh# Endless loop on a query with window functions and a limit 567d4cb09e3Sdrh# 568d4cb09e3Sdrhdo_execsql_test 12.100 { 569d4cb09e3Sdrh DROP TABLE IF EXISTS t1; 570d4cb09e3Sdrh CREATE TABLE t1(id INT, b VARCHAR, c VARCHAR); 571d4cb09e3Sdrh INSERT INTO t1 VALUES(1, 'A', 'one'); 572d4cb09e3Sdrh INSERT INTO t1 VALUES(2, 'B', 'two'); 573d4cb09e3Sdrh INSERT INTO t1 VALUES(3, 'C', 'three'); 574d4cb09e3Sdrh INSERT INTO t1 VALUES(4, 'D', 'one'); 575d4cb09e3Sdrh INSERT INTO t1 VALUES(5, 'E', 'two'); 576d4cb09e3Sdrh SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x 577d4cb09e3Sdrh FROM t1 WHERE id>1 578d4cb09e3Sdrh ORDER BY b LIMIT 1; 579d4cb09e3Sdrh} {2 B two} 580d4cb09e3Sdrhdo_execsql_test 12.110 { 581d4cb09e3Sdrh INSERT INTO t1 VALUES(6, 'F', 'three'); 582d4cb09e3Sdrh INSERT INTO t1 VALUES(7, 'G', 'one'); 583d4cb09e3Sdrh SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x 584d4cb09e3Sdrh FROM t1 WHERE id>1 585d4cb09e3Sdrh ORDER BY b LIMIT 2; 586d4cb09e3Sdrh} {2 B two 3 C three} 58726522d1cSdan 5880f5f5406Sdan#------------------------------------------------------------------------- 5890f5f5406Sdan 5900f5f5406Sdando_execsql_test 13.1 { 5910f5f5406Sdan DROP TABLE IF EXISTS t1; 5920f5f5406Sdan CREATE TABLE t1(a int, b int); 5930f5f5406Sdan INSERT INTO t1 VALUES(1,11); 5940f5f5406Sdan INSERT INTO t1 VALUES(2,12); 5950f5f5406Sdan} 5960f5f5406Sdan 5970f5f5406Sdando_execsql_test 13.2.1 { 5980f5f5406Sdan SELECT a, rank() OVER(ORDER BY b) FROM t1; 5990f5f5406Sdan SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; 6000f5f5406Sdan} { 6010f5f5406Sdan 1 1 2 2 2 1 1 2 6020f5f5406Sdan} 6030f5f5406Sdando_execsql_test 13.2.2 { 6040f5f5406Sdan SELECT a, rank() OVER(ORDER BY b) FROM t1 6050f5f5406Sdan UNION ALL 6060f5f5406Sdan SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; 6070f5f5406Sdan} { 6080f5f5406Sdan 1 1 2 2 2 1 1 2 6090f5f5406Sdan} 6100f5f5406Sdando_execsql_test 13.3 { 6110f5f5406Sdan SELECT a, rank() OVER(ORDER BY b) FROM t1 6120f5f5406Sdan UNION 6130f5f5406Sdan SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; 6140f5f5406Sdan} { 6150f5f5406Sdan 1 1 1 2 2 1 2 2 6160f5f5406Sdan} 6170f5f5406Sdan 6180f5f5406Sdando_execsql_test 13.4 { 6190f5f5406Sdan SELECT a, rank() OVER(ORDER BY b) FROM t1 6200f5f5406Sdan EXCEPT 6210f5f5406Sdan SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; 6220f5f5406Sdan} { 6230f5f5406Sdan 1 1 2 2 6240f5f5406Sdan} 6250f5f5406Sdan 6260f5f5406Sdando_execsql_test 13.5 { 6270f5f5406Sdan SELECT a, rank() OVER(ORDER BY b) FROM t1 6280f5f5406Sdan INTERSECT 6290f5f5406Sdan SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; 6303a07548bSdrh} {} 6310f5f5406Sdan 63211df7d28Sdrh# 2018-12-06 63311df7d28Sdrh# https://www.sqlite.org/src/info/f09fcd17810f65f7 634bb383df7Sdrh# Assertion fault when window functions are used. 635bb383df7Sdrh# 636bb383df7Sdrh# Root cause is the query flattener invoking sqlite3ExprDup() on 637bb383df7Sdrh# expressions that contain subqueries with window functions. The 638bb383df7Sdrh# sqlite3ExprDup() routine is not making correctly initializing 639bb383df7Sdrh# Select.pWin field of the subqueries. 64011df7d28Sdrh# 64111df7d28Sdrhsqlite3 db :memory: 64211df7d28Sdrhdo_execsql_test 14.0 { 64311df7d28Sdrh SELECT * FROM( 64411df7d28Sdrh SELECT * FROM (SELECT 1 AS c) WHERE c IN ( 64511df7d28Sdrh SELECT (row_number() OVER()) FROM (VALUES (0)) 64611df7d28Sdrh ) 64711df7d28Sdrh ); 64811df7d28Sdrh} {1} 649bb383df7Sdrhdo_execsql_test 14.1 { 650bb383df7Sdrh CREATE TABLE t1(x); INSERT INTO t1(x) VALUES(12345); 651bb383df7Sdrh CREATE TABLE t2(c); INSERT INTO t2(c) VALUES(1); 652bb383df7Sdrh SELECT y, y+1, y+2 FROM ( 653bb383df7Sdrh SELECT c IN ( 654bb383df7Sdrh SELECT (row_number() OVER()) FROM t1 655bb383df7Sdrh ) AS y FROM t2 656bb383df7Sdrh ); 657bb383df7Sdrh} {1 2 3} 65811df7d28Sdrh 6594afdfa19Sdrh# 2018-12-31 6604afdfa19Sdrh# https://www.sqlite.org/src/info/d0866b26f83e9c55 6614afdfa19Sdrh# Window function in correlated subquery causes assertion fault 6624afdfa19Sdrh# 6634afdfa19Sdrhdo_catchsql_test 15.0 { 6644afdfa19Sdrh WITH t(id, parent) AS ( 6654afdfa19Sdrh SELECT CAST(1 AS INT), CAST(NULL AS INT) 6664afdfa19Sdrh UNION ALL 6674afdfa19Sdrh SELECT 2, NULL 6684afdfa19Sdrh UNION ALL 6694afdfa19Sdrh SELECT 3, 1 6704afdfa19Sdrh UNION ALL 6714afdfa19Sdrh SELECT 4, 1 6724afdfa19Sdrh UNION ALL 6734afdfa19Sdrh SELECT 5, 2 6744afdfa19Sdrh UNION ALL 6754afdfa19Sdrh SELECT 6, 2 6764afdfa19Sdrh ), q AS ( 6774afdfa19Sdrh SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn 6784afdfa19Sdrh FROM t 6794afdfa19Sdrh WHERE parent IS NULL 6804afdfa19Sdrh UNION ALL 6814afdfa19Sdrh SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn 6824afdfa19Sdrh FROM q 6834afdfa19Sdrh JOIN t 6844afdfa19Sdrh ON t.parent = q.id 6854afdfa19Sdrh ) 6864afdfa19Sdrh SELECT * 6874afdfa19Sdrh FROM q; 6884afdfa19Sdrh} {1 {cannot use window functions in recursive queries}} 6894afdfa19Sdrhdo_execsql_test 15.1 { 6904afdfa19Sdrh DROP TABLE IF EXISTS t1; 6914afdfa19Sdrh DROP TABLE IF EXISTS t2; 6924afdfa19Sdrh CREATE TABLE t1(x); 6934afdfa19Sdrh INSERT INTO t1 VALUES('a'), ('b'), ('c'); 6944afdfa19Sdrh CREATE TABLE t2(a, b); 6954afdfa19Sdrh INSERT INTO t2 VALUES('X', 1), ('X', 2), ('Y', 2), ('Y', 3); 6964afdfa19Sdrh SELECT x, ( 6974afdfa19Sdrh SELECT sum(b) 6984afdfa19Sdrh OVER (PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING 6994afdfa19Sdrh AND UNBOUNDED FOLLOWING) 7004afdfa19Sdrh FROM t2 WHERE b<x 7014afdfa19Sdrh ) FROM t1; 7024afdfa19Sdrh} {a 3 b 3 c 3} 7034afdfa19Sdrh 70497c8cb3eSdando_execsql_test 15.2 { 70597c8cb3eSdan SELECT( 70697c8cb3eSdan WITH c AS( 70797c8cb3eSdan VALUES(1) 70897c8cb3eSdan ) SELECT '' FROM c,c 70997c8cb3eSdan ) x WHERE x+x; 71097c8cb3eSdan} {} 71197c8cb3eSdan 712d9995031Sdan#------------------------------------------------------------------------- 713d9995031Sdan 714d9995031Sdando_execsql_test 16.0 { 715d9995031Sdan CREATE TABLE t7(a,b); 716d9995031Sdan INSERT INTO t7(rowid, a, b) VALUES 717d9995031Sdan (1, 1, 3), 718d9995031Sdan (2, 10, 4), 719d9995031Sdan (3, 100, 2); 720d9995031Sdan} 721d9995031Sdan 722d9995031Sdando_execsql_test 16.1 { 723d9995031Sdan SELECT rowid, sum(a) OVER (PARTITION BY b IN (SELECT rowid FROM t7)) FROM t7; 724d9995031Sdan} { 725d9995031Sdan 2 10 726d9995031Sdan 1 101 727d9995031Sdan 3 101 728d9995031Sdan} 729d9995031Sdan 730d9995031Sdando_execsql_test 16.2 { 731d9995031Sdan SELECT rowid, sum(a) OVER w1 FROM t7 732d9995031Sdan WINDOW w1 AS (PARTITION BY b IN (SELECT rowid FROM t7)); 733d9995031Sdan} { 734d9995031Sdan 2 10 735d9995031Sdan 1 101 736d9995031Sdan 3 101 737d9995031Sdan} 738d9995031Sdan 739e7c9ca41Sdan#------------------------------------------------------------------------- 740f030b376Sdando_execsql_test 17.0 { 741f030b376Sdan CREATE TABLE t8(a); 742f030b376Sdan INSERT INTO t8 VALUES(1), (2), (3); 743f030b376Sdan} 744f030b376Sdan 745f030b376Sdando_execsql_test 17.1 { 746f030b376Sdan SELECT +sum(0) OVER () ORDER BY +sum(0) OVER (); 747f030b376Sdan} {0} 748f030b376Sdan 749f030b376Sdando_execsql_test 17.2 { 750f030b376Sdan select +sum(a) OVER () FROM t8 ORDER BY +sum(a) OVER () DESC; 751f030b376Sdan} {6 6 6} 752f030b376Sdan 753f030b376Sdando_execsql_test 17.3 { 754f030b376Sdan SELECT 10+sum(a) OVER (ORDER BY a) 755f030b376Sdan FROM t8 756f030b376Sdan ORDER BY 10+sum(a) OVER (ORDER BY a) DESC; 757f030b376Sdan} {16 13 11} 758f030b376Sdan 7599e24439cSdan 760db7d895eSdan#------------------------------------------------------------------------- 761e7c9ca41Sdan# Test error cases from chaining window definitions. 762e7c9ca41Sdan# 763e7c9ca41Sdanreset_db 7644e72e62fSdando_execsql_test 18.0 { 765e7c9ca41Sdan DROP TABLE IF EXISTS t1; 766e7c9ca41Sdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); 767e7c9ca41Sdan INSERT INTO t1 VALUES(1, 'odd', 'one', 1); 768e7c9ca41Sdan INSERT INTO t1 VALUES(2, 'even', 'two', 2); 769e7c9ca41Sdan INSERT INTO t1 VALUES(3, 'odd', 'three', 3); 770e7c9ca41Sdan INSERT INTO t1 VALUES(4, 'even', 'four', 4); 771e7c9ca41Sdan INSERT INTO t1 VALUES(5, 'odd', 'five', 5); 772e7c9ca41Sdan INSERT INTO t1 VALUES(6, 'even', 'six', 6); 773e7c9ca41Sdan} 774e7c9ca41Sdan 775e7c9ca41Sdanforeach {tn sql error} { 776e7c9ca41Sdan 1 { 777e7c9ca41Sdan SELECT c, sum(d) OVER win2 FROM t1 778e7c9ca41Sdan WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), 779e7c9ca41Sdan win2 AS (win1 ORDER BY b) 780e7c9ca41Sdan } {cannot override frame specification of window: win1} 781e7c9ca41Sdan 782e7c9ca41Sdan 2 { 783e7c9ca41Sdan SELECT c, sum(d) OVER win2 FROM t1 784e7c9ca41Sdan WINDOW win1 AS (), 785e7c9ca41Sdan win2 AS (win4 ORDER BY b) 786e7c9ca41Sdan } {no such window: win4} 787e7c9ca41Sdan 788e7c9ca41Sdan 3 { 789e7c9ca41Sdan SELECT c, sum(d) OVER win2 FROM t1 790e7c9ca41Sdan WINDOW win1 AS (), 791e7c9ca41Sdan win2 AS (win1 PARTITION BY d) 792e7c9ca41Sdan } {cannot override PARTITION clause of window: win1} 793e7c9ca41Sdan 794e7c9ca41Sdan 4 { 795e7c9ca41Sdan SELECT c, sum(d) OVER win2 FROM t1 796e7c9ca41Sdan WINDOW win1 AS (ORDER BY b), 797e7c9ca41Sdan win2 AS (win1 ORDER BY d) 798e7c9ca41Sdan } {cannot override ORDER BY clause of window: win1} 799e7c9ca41Sdan} { 8004e72e62fSdan do_catchsql_test 18.1.$tn $sql [list 1 $error] 801e7c9ca41Sdan} 802e7c9ca41Sdan 803e7c9ca41Sdanforeach {tn sql error} { 804e7c9ca41Sdan 1 { 805e7c9ca41Sdan SELECT c, sum(d) OVER (win1 ORDER BY b) FROM t1 806e7c9ca41Sdan WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) 807e7c9ca41Sdan } {cannot override frame specification of window: win1} 808e7c9ca41Sdan 809e7c9ca41Sdan 2 { 810e7c9ca41Sdan SELECT c, sum(d) OVER (win4 ORDER BY b) FROM t1 811e7c9ca41Sdan WINDOW win1 AS () 812e7c9ca41Sdan } {no such window: win4} 813e7c9ca41Sdan 814e7c9ca41Sdan 3 { 815e7c9ca41Sdan SELECT c, sum(d) OVER (win1 PARTITION BY d) FROM t1 816e7c9ca41Sdan WINDOW win1 AS () 817e7c9ca41Sdan } {cannot override PARTITION clause of window: win1} 818e7c9ca41Sdan 819e7c9ca41Sdan 4 { 820e7c9ca41Sdan SELECT c, sum(d) OVER (win1 ORDER BY d) FROM t1 821e7c9ca41Sdan WINDOW win1 AS (ORDER BY b) 822e7c9ca41Sdan } {cannot override ORDER BY clause of window: win1} 823e7c9ca41Sdan} { 8244e72e62fSdan do_catchsql_test 18.2.$tn $sql [list 1 $error] 825e7c9ca41Sdan} 826e7c9ca41Sdan 8274e72e62fSdando_execsql_test 18.3.1 { 828e7c9ca41Sdan SELECT group_concat(c, '.') OVER (PARTITION BY b ORDER BY c) 829e7c9ca41Sdan FROM t1 830e7c9ca41Sdan} {four four.six four.six.two five five.one five.one.three} 831e7c9ca41Sdan 8324e72e62fSdando_execsql_test 18.3.2 { 833e7c9ca41Sdan SELECT group_concat(c, '.') OVER (win1 ORDER BY c) 834e7c9ca41Sdan FROM t1 835e7c9ca41Sdan WINDOW win1 AS (PARTITION BY b) 836e7c9ca41Sdan} {four four.six four.six.two five five.one five.one.three} 837e7c9ca41Sdan 8384e72e62fSdando_execsql_test 18.3.3 { 839e7c9ca41Sdan SELECT group_concat(c, '.') OVER win2 840e7c9ca41Sdan FROM t1 841e7c9ca41Sdan WINDOW win1 AS (PARTITION BY b), 842e7c9ca41Sdan win2 AS (win1 ORDER BY c) 843e7c9ca41Sdan} {four four.six four.six.two five five.one five.one.three} 844e7c9ca41Sdan 8454e72e62fSdando_execsql_test 18.3.4 { 846e7c9ca41Sdan SELECT group_concat(c, '.') OVER (win2) 847e7c9ca41Sdan FROM t1 848e7c9ca41Sdan WINDOW win1 AS (PARTITION BY b), 849e7c9ca41Sdan win2 AS (win1 ORDER BY c) 850e7c9ca41Sdan} {four four.six four.six.two five five.one five.one.three} 851e7c9ca41Sdan 8524e72e62fSdando_execsql_test 18.3.5 { 853e7c9ca41Sdan SELECT group_concat(c, '.') OVER win5 854e7c9ca41Sdan FROM t1 855e7c9ca41Sdan WINDOW win1 AS (PARTITION BY b), 856e7c9ca41Sdan win2 AS (win1), 857e7c9ca41Sdan win3 AS (win2), 858e7c9ca41Sdan win4 AS (win3), 859e7c9ca41Sdan win5 AS (win4 ORDER BY c) 860e7c9ca41Sdan} {four four.six four.six.two five five.one five.one.three} 861d9995031Sdan 8621e7cb19bSdan#------------------------------------------------------------------------- 8631e7cb19bSdan# Test RANGE <expr> PRECEDING/FOLLOWING when there are string, blob 8641e7cb19bSdan# and NULL values in the dataset. 8651e7cb19bSdan# 8661e7cb19bSdanreset_db 8671e7cb19bSdando_execsql_test 19.0 { 8681e7cb19bSdan CREATE TABLE t1(a, b); 8691e7cb19bSdan INSERT INTO t1 VALUES 8701e7cb19bSdan (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), 8711e7cb19bSdan ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10); 8721e7cb19bSdan} 8731e7cb19bSdando_execsql_test 19.1 { 8741e7cb19bSdan SELECT a, sum(b) OVER (ORDER BY a) FROM t1; 8751e7cb19bSdan} {1 1 2 3 3 6 4 10 5 15 a 21 b 28 c 36 d 45 e 55} 8761e7cb19bSdan 8771e7cb19bSdando_execsql_test 19.2.1 { 8781e7cb19bSdan SELECT a, sum(b) OVER ( 8791e7cb19bSdan ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING 8801e7cb19bSdan ) FROM t1; 8811e7cb19bSdan} {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} 8821e7cb19bSdando_execsql_test 19.2.2 { 8831e7cb19bSdan SELECT a, sum(b) OVER ( 8841e7cb19bSdan ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING 8851e7cb19bSdan ) FROM t1 ORDER BY a ASC; 8861e7cb19bSdan} {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} 8871e7cb19bSdan 8881e7cb19bSdando_execsql_test 19.3.1 { 8891e7cb19bSdan SELECT a, sum(b) OVER ( 8901e7cb19bSdan ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING 8911e7cb19bSdan ) FROM t1; 8921e7cb19bSdan} {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} 8931e7cb19bSdando_execsql_test 19.3.2 { 8941e7cb19bSdan SELECT a, sum(b) OVER ( 8951e7cb19bSdan ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING 8961e7cb19bSdan ) FROM t1 ORDER BY a ASC; 8971e7cb19bSdan} {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} 8981e7cb19bSdan 8991e7cb19bSdan 9001e7cb19bSdanreset_db 9011e7cb19bSdando_execsql_test 20.0 { 9021e7cb19bSdan CREATE TABLE t1(a, b); 9031e7cb19bSdan INSERT INTO t1 VALUES 9041e7cb19bSdan (NULL, 100), (NULL, 100), 9051e7cb19bSdan (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), 9061e7cb19bSdan ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10); 9071e7cb19bSdan} 9081e7cb19bSdando_execsql_test 20.1 { 9091e7cb19bSdan SELECT a, sum(b) OVER (ORDER BY a) FROM t1; 9101e7cb19bSdan} { 9111e7cb19bSdan {} 200 {} 200 1 201 2 203 3 206 4 210 5 215 9121e7cb19bSdan a 221 b 228 c 236 d 245 e 255 9131e7cb19bSdan} 9141e7cb19bSdan 9151e7cb19bSdando_execsql_test 20.2.1 { 9161e7cb19bSdan SELECT a, sum(b) OVER ( 9171e7cb19bSdan ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING 9181e7cb19bSdan ) FROM t1; 9191e7cb19bSdan} {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} 9201e7cb19bSdando_execsql_test 20.2.2 { 9211e7cb19bSdan SELECT a, sum(b) OVER ( 9221e7cb19bSdan ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING 9231e7cb19bSdan ) FROM t1 ORDER BY a ASC; 9241e7cb19bSdan} {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} 9251e7cb19bSdan 9261e7cb19bSdando_execsql_test 20.3.1 { 9271e7cb19bSdan SELECT a, sum(b) OVER ( 9281e7cb19bSdan ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING 9291e7cb19bSdan ) FROM t1; 9301e7cb19bSdan} {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} 9311e7cb19bSdando_execsql_test 20.3.2 { 9321e7cb19bSdan SELECT a, sum(b) OVER ( 9331e7cb19bSdan ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING 9341e7cb19bSdan ) FROM t1 ORDER BY a ASC; 9351e7cb19bSdan} {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} 9361e7cb19bSdan 937ced89124Sdan#------------------------------------------------------------------------- 938ced89124Sdando_execsql_test 21.0 { 939ced89124Sdan CREATE TABLE keyword_tab( 940ced89124Sdan current, exclude, filter, following, groups, no, others, over, 941ced89124Sdan partition, preceding, range, ties, unbounded, window 942ced89124Sdan ); 943ced89124Sdan} 944ced89124Sdando_execsql_test 21.1 { 945ced89124Sdan SELECT 946ced89124Sdan current, exclude, filter, following, groups, no, others, over, 947ced89124Sdan partition, preceding, range, ties, unbounded, window 948ced89124Sdan FROM keyword_tab 949ced89124Sdan} 950ced89124Sdan 951e5166e07Sdan#------------------------------------------------------------------------- 952e5166e07Sdanforeach {tn expr err} { 953e5166e07Sdan 1 4.5 0 954e5166e07Sdan 2 NULL 1 955e5166e07Sdan 3 0.0 0 956e5166e07Sdan 4 0.1 0 957e5166e07Sdan 5 -0.1 1 958e5166e07Sdan 6 '' 1 959e5166e07Sdan 7 '2.0' 0 960e5166e07Sdan 8 '2.0x' 1 961e5166e07Sdan 9 x'1234' 1 962e5166e07Sdan 10 '1.2' 0 963e5166e07Sdan} { 964e5166e07Sdan set res {0 1} 965e5166e07Sdan if {$err} {set res {1 {frame starting offset must be a non-negative number}} } 966e5166e07Sdan do_catchsql_test 22.$tn.1 " 967e5166e07Sdan WITH a(x, y) AS ( VALUES(1, 2) ) 968e5166e07Sdan SELECT sum(x) OVER ( 969e5166e07Sdan ORDER BY y RANGE BETWEEN $expr PRECEDING AND UNBOUNDED FOLLOWING 970e5166e07Sdan ) FROM a 971e5166e07Sdan " $res 972e5166e07Sdan 973e5166e07Sdan set res {0 1} 974e5166e07Sdan if {$err} {set res {1 {frame ending offset must be a non-negative number}} } 975e5166e07Sdan do_catchsql_test 22.$tn.2 " 976e5166e07Sdan WITH a(x, y) AS ( VALUES(1, 2) ) 977e5166e07Sdan SELECT sum(x) OVER ( 978e5166e07Sdan ORDER BY y RANGE BETWEEN UNBOUNDED PRECEDING AND $expr FOLLOWING 979e5166e07Sdan ) FROM a 980e5166e07Sdan " $res 981e5166e07Sdan} 982e5166e07Sdan 9838eff0cc3Sdan#------------------------------------------------------------------------- 9848eff0cc3Sdanreset_db 9858eff0cc3Sdando_execsql_test 23.0 { 9868eff0cc3Sdan CREATE TABLE t5(a, b, c); 9878eff0cc3Sdan CREATE INDEX t5ab ON t5(a, b); 9888eff0cc3Sdan} 9898eff0cc3Sdan 9908eff0cc3Sdanproc do_ordercount_test {tn sql nOrderBy} { 9918eff0cc3Sdan set plan [execsql "EXPLAIN QUERY PLAN $sql"] 9928eff0cc3Sdan uplevel [list do_test $tn [list regexp -all ORDER $plan] $nOrderBy] 9938eff0cc3Sdan} 9948eff0cc3Sdan 9958eff0cc3Sdando_ordercount_test 23.1 { 9968eff0cc3Sdan SELECT 9978eff0cc3Sdan sum(c) OVER (ORDER BY a, b), 9988eff0cc3Sdan sum(c) OVER (PARTITION BY a ORDER BY b) 9998eff0cc3Sdan FROM t5 10008eff0cc3Sdan} 0 10018eff0cc3Sdan 10028eff0cc3Sdando_ordercount_test 23.2 { 10038eff0cc3Sdan SELECT 10048eff0cc3Sdan sum(c) OVER (ORDER BY b, a), 10058eff0cc3Sdan sum(c) OVER (PARTITION BY b ORDER BY a) 10068eff0cc3Sdan FROM t5 10078eff0cc3Sdan} 1 10088eff0cc3Sdan 10098eff0cc3Sdando_ordercount_test 23.3 { 10108eff0cc3Sdan SELECT 10118eff0cc3Sdan sum(c) OVER (ORDER BY b, a), 10128eff0cc3Sdan sum(c) OVER (ORDER BY c, b) 10138eff0cc3Sdan FROM t5 10148eff0cc3Sdan} 2 10158eff0cc3Sdan 10168eff0cc3Sdando_ordercount_test 23.4 { 10178eff0cc3Sdan SELECT 10188eff0cc3Sdan sum(c) OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 10198eff0cc3Sdan sum(c) OVER (ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 10208eff0cc3Sdan sum(c) OVER (ORDER BY b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 10218eff0cc3Sdan FROM t5 10228eff0cc3Sdan} 1 10238eff0cc3Sdan 10248eff0cc3Sdando_ordercount_test 23.5 { 10258eff0cc3Sdan SELECT 10268eff0cc3Sdan sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING), 10278eff0cc3Sdan sum(c) OVER (ORDER BY b+1 RANGE UNBOUNDED PRECEDING), 10288eff0cc3Sdan sum(c) OVER (ORDER BY b+1 GROUPS UNBOUNDED PRECEDING) 10298eff0cc3Sdan FROM t5 10308eff0cc3Sdan} 1 10318eff0cc3Sdan 10328eff0cc3Sdando_ordercount_test 23.6 { 10338eff0cc3Sdan SELECT 10348eff0cc3Sdan sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING), 10358eff0cc3Sdan sum(c) OVER (ORDER BY b+2 RANGE UNBOUNDED PRECEDING), 10368eff0cc3Sdan sum(c) OVER (ORDER BY b+3 GROUPS UNBOUNDED PRECEDING) 10378eff0cc3Sdan FROM t5 10388eff0cc3Sdan} 3 10398eff0cc3Sdan 1040c7694a6dSdando_execsql_test 24.1 { 1041c7694a6dSdan SELECT sum(44) OVER () 1042c7694a6dSdan} {44} 1043c7694a6dSdan 1044c7694a6dSdando_execsql_test 24.2 { 1045c7694a6dSdan SELECT lead(44) OVER () 1046c7694a6dSdan} {{}} 1047c7694a6dSdan 1048781b7ac3Sdan#------------------------------------------------------------------------- 1049781b7ac3Sdan# 1050781b7ac3Sdanreset_db 1051781b7ac3Sdando_execsql_test 25.0 { 1052781b7ac3Sdan CREATE TABLE t1 ( t1_id INTEGER PRIMARY KEY ); 1053781b7ac3Sdan CREATE TABLE t2 ( t2_id INTEGER PRIMARY KEY ); 1054781b7ac3Sdan CREATE TABLE t3 ( t3_id INTEGER PRIMARY KEY ); 1055781b7ac3Sdan 1056781b7ac3Sdan INSERT INTO t1 VALUES(1), (3), (5); 1057781b7ac3Sdan INSERT INTO t2 VALUES (3), (5); 1058781b7ac3Sdan INSERT INTO t3 VALUES(10), (11), (12); 1059781b7ac3Sdan} 1060781b7ac3Sdan 1061781b7ac3Sdando_execsql_test 25.1 { 1062781b7ac3Sdan SELECT t1.* FROM t1, t2 WHERE 1063781b7ac3Sdan t1_id=t2_id AND t1_id IN ( 1064781b7ac3Sdan SELECT t1_id + row_number() OVER ( ORDER BY t1_id ) FROM t3 1065781b7ac3Sdan ) 1066781b7ac3Sdan} 1067781b7ac3Sdan 1068781b7ac3Sdando_execsql_test 25.2 { 1069781b7ac3Sdan SELECT t1.* FROM t1, t2 WHERE 1070781b7ac3Sdan t1_id=t2_id AND t1_id IN ( 1071781b7ac3Sdan SELECT row_number() OVER ( ORDER BY t1_id ) FROM t3 1072781b7ac3Sdan ) 1073781b7ac3Sdan} {3} 1074781b7ac3Sdan 1075781b7ac3Sdan#------------------------------------------------------------------------- 1076781b7ac3Sdanreset_db 1077781b7ac3Sdando_execsql_test 26.0 { 1078781b7ac3Sdan CREATE TABLE t1(x); 1079781b7ac3Sdan CREATE TABLE t2(c); 1080781b7ac3Sdan} 1081781b7ac3Sdan 1082781b7ac3Sdando_execsql_test 26.1 { 1083781b7ac3Sdan SELECT ( SELECT row_number() OVER () FROM ( SELECT c FROM t1 ) ) FROM t2 1084781b7ac3Sdan} {} 1085781b7ac3Sdan 1086781b7ac3Sdando_execsql_test 26.2 { 1087781b7ac3Sdan INSERT INTO t1 VALUES(1), (2), (3), (4); 1088781b7ac3Sdan INSERT INTO t2 VALUES(2), (6), (8), (4); 1089781b7ac3Sdan SELECT c, c IN ( 1090781b7ac3Sdan SELECT row_number() OVER () FROM ( SELECT c FROM t1 ) 1091781b7ac3Sdan ) FROM t2 1092781b7ac3Sdan} {2 1 6 0 8 0 4 1} 1093781b7ac3Sdan 1094781b7ac3Sdando_execsql_test 26.3 { 1095781b7ac3Sdan DELETE FROM t1; 1096781b7ac3Sdan DELETE FROM t2; 1097781b7ac3Sdan 1098781b7ac3Sdan INSERT INTO t2 VALUES(1), (2), (3), (4); 1099781b7ac3Sdan INSERT INTO t1 VALUES(1), (1), (2), (3), (3), (3), (3), (4), (4); 1100781b7ac3Sdan 1101781b7ac3Sdan SELECT c, c IN ( 1102781b7ac3Sdan SELECT row_number() OVER () FROM ( SELECT 1 FROM t1 WHERE x=c ) 1103781b7ac3Sdan ) FROM t2 1104781b7ac3Sdan} {1 1 2 0 3 1 4 0} 1105781b7ac3Sdan 1106afb3f3c7Sdan#------------------------------------------------------------------------- 1107afb3f3c7Sdanreset_db 1108afb3f3c7Sdando_execsql_test 27.0 { 1109afb3f3c7Sdan CREATE TABLE t1(x); 1110afb3f3c7Sdan INSERT INTO t1 VALUES(NULL), (1), (2), (3), (4), (5); 1111afb3f3c7Sdan} 1112afb3f3c7Sdando_execsql_test 27.1 { 1113afb3f3c7Sdan SELECT min(x) FROM t1; 1114afb3f3c7Sdan} {1} 1115afb3f3c7Sdando_execsql_test 27.2 { 1116afb3f3c7Sdan SELECT min(x) OVER win FROM t1 1117afb3f3c7Sdan WINDOW win AS (ORDER BY rowid ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) 1118afb3f3c7Sdan} {1 1 1 2 3 4} 1119781b7ac3Sdan 11203f49c321Sdan#------------------------------------------------------------------------- 11213f49c321Sdan 11223f49c321Sdanreset_db 11233f49c321Sdando_execsql_test 28.1.1 { 11243f49c321Sdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); 11253f49c321Sdan INSERT INTO t1 VALUES (3, 'C', 'cc', 1.0); 11263f49c321Sdan INSERT INTO t1 VALUES (13,'M', 'cc', NULL); 11273f49c321Sdan} 11283f49c321Sdan 11293f49c321Sdando_execsql_test 28.1.2 { 11303f49c321Sdan SELECT group_concat(b,'') OVER w1 FROM t1 11313f49c321Sdan WINDOW w1 AS (ORDER BY a RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING) 11323f49c321Sdan} { 11333f49c321Sdan {} {} 11343f49c321Sdan} 11353f49c321Sdan 11363f49c321Sdando_execsql_test 28.2.1 { 11373f49c321Sdan CREATE TABLE t2(a TEXT, b INTEGER); 11383f49c321Sdan INSERT INTO t2 VALUES('A', NULL); 11393f49c321Sdan INSERT INTO t2 VALUES('B', NULL); 11403f49c321Sdan} 11413f49c321Sdan 11423f49c321Sdando_execsql_test 28.2.1 { 11433f49c321Sdan DROP TABLE IF EXISTS t1; 11443f49c321Sdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); 11453f49c321Sdan INSERT INTO t1 VALUES 11463f49c321Sdan (10,'J', 'cc', NULL), 11473f49c321Sdan (11,'K', 'cc', 'xyz'), 11483f49c321Sdan (13,'M', 'cc', NULL); 11493f49c321Sdan} 11503f49c321Sdan 11513f49c321Sdando_execsql_test 28.2.2 { 11523f49c321Sdan SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1 11533f49c321Sdan WINDOW w1 AS 11543f49c321Sdan (ORDER BY d DESC RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING) 11553f49c321Sdan ORDER BY c, d, a; 11563f49c321Sdan} { 11573f49c321Sdan 10 J cc NULL JM | 11583f49c321Sdan 13 M cc NULL JM | 11593f49c321Sdan 11 K cc 'xyz' K | 11603f49c321Sdan} 11613f49c321Sdan 11623f49c321Sdan#------------------------------------------------------------------------- 11633f49c321Sdanreset_db 11643f49c321Sdan 11653f49c321Sdando_execsql_test 29.1 { 11663f49c321Sdan DROP TABLE IF EXISTS t1; 11673f49c321Sdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); 11683f49c321Sdan INSERT INTO t1 VALUES 11693f49c321Sdan (1, 'A', 'aa', 2.5), 11703f49c321Sdan (2, 'B', 'bb', 3.75), 11713f49c321Sdan (3, 'C', 'cc', 1.0), 11723f49c321Sdan (4, 'D', 'cc', 8.25), 11733f49c321Sdan (5, 'E', 'bb', 6.5), 11743f49c321Sdan (6, 'F', 'aa', 6.5), 11753f49c321Sdan (7, 'G', 'aa', 6.0), 11763f49c321Sdan (8, 'H', 'bb', 9.0), 11773f49c321Sdan (9, 'I', 'aa', 3.75), 11783f49c321Sdan (10,'J', 'cc', NULL), 11793f49c321Sdan (11,'K', 'cc', 'xyz'), 11803f49c321Sdan (12,'L', 'cc', 'xyZ'), 11813f49c321Sdan (13,'M', 'cc', NULL); 11823f49c321Sdan} 11833f49c321Sdan 11843f49c321Sdando_execsql_test 29.2 { 11853f49c321Sdan SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1 11863f49c321Sdan WINDOW w1 AS 11873f49c321Sdan (PARTITION BY c ORDER BY d DESC 11883f49c321Sdan RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING) 11893f49c321Sdan ORDER BY c, d, a; 11903f49c321Sdan} { 11913f49c321Sdan 1 A aa 2.5 FG | 11923f49c321Sdan 9 I aa 3.75 F | 11933f49c321Sdan 7 G aa 6 {} | 11943f49c321Sdan 6 F aa 6.5 {} | 11953f49c321Sdan 2 B bb 3.75 HE | 11963f49c321Sdan 5 E bb 6.5 H | 11973f49c321Sdan 8 H bb 9 {} | 11983f49c321Sdan 10 J cc NULL JM | 11993f49c321Sdan 13 M cc NULL JM | 12003f49c321Sdan 3 C cc 1 {} | 12013f49c321Sdan 4 D cc 8.25 {} | 12023f49c321Sdan 12 L cc 'xyZ' L | 12033f49c321Sdan 11 K cc 'xyz' K | 12043f49c321Sdan} 1205c7694a6dSdan 1206b555b080Sdrh# 2019-07-18 1207b555b080Sdrh# Check-in [7ef7b23cbb1b9ace] (which was itself a fix for ticket 1208b555b080Sdrh# https://www.sqlite.org/src/info/1be72aab9) introduced a new problem 1209b555b080Sdrh# if the LHS of a BETWEEN operator is a WINDOW function. The problem 1210b555b080Sdrh# was found by (the recently enhanced) dbsqlfuzz. 1211b555b080Sdrh# 1212b555b080Sdrhdo_execsql_test 30.0 { 1213b555b080Sdrh DROP TABLE IF EXISTS t1; 1214b555b080Sdrh CREATE TABLE t1(a, b, c); 1215b555b080Sdrh INSERT INTO t1 VALUES('BB','aa',399); 1216b555b080Sdrh SELECT 1217b555b080Sdrh count () OVER win1 NOT BETWEEN 'a' AND 'mmm', 1218b555b080Sdrh count () OVER win3 1219b555b080Sdrh FROM t1 1220b555b080Sdrh WINDOW win1 AS (ORDER BY a GROUPS BETWEEN 4 PRECEDING AND 1 FOLLOWING 1221b555b080Sdrh EXCLUDE CURRENT ROW), 1222b555b080Sdrh win2 AS (PARTITION BY b ORDER BY a), 1223b555b080Sdrh win3 AS (win2 RANGE BETWEEN 5.2 PRECEDING AND true PRECEDING ); 1224b555b080Sdrh} {1 1} 1225b555b080Sdrh 12263703edf1Sdan#------------------------------------------------------------------------- 12273703edf1Sdanreset_db 12283703edf1Sdando_execsql_test 31.1 { 12293703edf1Sdan CREATE TABLE t1(a, b); 12303703edf1Sdan CREATE TABLE t2(c, d); 12313703edf1Sdan CREATE TABLE t3(e, f); 12323703edf1Sdan 12333703edf1Sdan INSERT INTO t1 VALUES(1, 1); 12343703edf1Sdan INSERT INTO t2 VALUES(1, 1); 12353703edf1Sdan INSERT INTO t3 VALUES(1, 1); 12363703edf1Sdan} 12373703edf1Sdan 12383703edf1Sdando_execsql_test 31.2 { 12393703edf1Sdan SELECT d IN (SELECT sum(c) OVER (ORDER BY e+c) FROM t3) FROM ( 12403703edf1Sdan SELECT * FROM t2 12413703edf1Sdan ); 12423703edf1Sdan} {1} 12433703edf1Sdan 12443703edf1Sdando_execsql_test 31.3 { 12453703edf1Sdan SELECT d IN (SELECT sum(c) OVER (PARTITION BY d ORDER BY e+c) FROM t3) FROM ( 12463703edf1Sdan SELECT * FROM t2 12473703edf1Sdan ); 12483703edf1Sdan} {1} 12493703edf1Sdan 12503703edf1Sdando_catchsql_test 31.3 { 12513703edf1Sdan SELECT d IN ( 12523703edf1Sdan SELECT sum(c) OVER ( ROWS BETWEEN d FOLLOWING AND UNBOUNDED FOLLOWING) 12533703edf1Sdan FROM t3 12543703edf1Sdan ) 12553703edf1Sdan FROM ( 12563703edf1Sdan SELECT * FROM t2 12573703edf1Sdan ); 12583703edf1Sdan} {1 {frame starting offset must be a non-negative integer}} 12593703edf1Sdan 12603703edf1Sdando_catchsql_test 31.3 { 12613703edf1Sdan SELECT d IN ( 12623703edf1Sdan SELECT sum(c) OVER ( ROWS BETWEEN CURRENT ROW AND c FOLLOWING) 12633703edf1Sdan FROM t3 12643703edf1Sdan ) 12653703edf1Sdan FROM ( 12663703edf1Sdan SELECT * FROM t2 12673703edf1Sdan ); 12683703edf1Sdan} {1 {frame ending offset must be a non-negative integer}} 12693703edf1Sdan 127047bcc342Sdrh# 2019-11-16 chromium issue 1025467 127137f3ac8fSdanifcapable altertable { 127247bcc342Sdrh db close 127347bcc342Sdrh sqlite3 db :memory: 127447bcc342Sdrh do_catchsql_test 32.10 { 127547bcc342Sdrh CREATE VIEW a AS SELECT NULL INTERSECT SELECT NULL ORDER BY s() OVER R; 127647bcc342Sdrh CREATE TABLE a0 AS SELECT 0; 127747bcc342Sdrh ALTER TABLE a0 RENAME TO S; 127847bcc342Sdrh } {1 {error in view a: 1st ORDER BY term does not match any column in the result set}} 127937f3ac8fSdan} 128047bcc342Sdrh 1281e59c562bSdanreset_db 1282e59c562bSdando_execsql_test 33.1 { 1283e59c562bSdan CREATE TABLE t1(aa, bb); 1284e59c562bSdan INSERT INTO t1 VALUES(1, 2); 1285e59c562bSdan INSERT INTO t1 VALUES(5, 6); 1286e59c562bSdan CREATE TABLE t2(x); 1287e59c562bSdan INSERT INTO t2 VALUES(1); 1288e59c562bSdan} 1289e59c562bSdando_execsql_test 33.2 { 1290e59c562bSdan SELECT (SELECT DISTINCT sum(aa) OVER() FROM t1 ORDER BY 1), x FROM t2 1291e59c562bSdan ORDER BY 1; 1292e59c562bSdan} {6 1} 1293e59c562bSdan 129462be2dc7Sdanreset_db 129562be2dc7Sdando_execsql_test 34.1 { 129662be2dc7Sdan CREATE TABLE t1(a,b,c); 129762be2dc7Sdan} 129862be2dc7Sdando_execsql_test 34.2 { 129962be2dc7Sdan SELECT avg(a) OVER ( 130062be2dc7Sdan ORDER BY (SELECT sum(b) OVER () 130162be2dc7Sdan FROM t1 ORDER BY ( 130262be2dc7Sdan SELECT total(d) OVER (ORDER BY c) 130362be2dc7Sdan FROM (SELECT 1 AS d) ORDER BY 1 130462be2dc7Sdan ) 130562be2dc7Sdan ) 130662be2dc7Sdan ) 130762be2dc7Sdan FROM t1; 130862be2dc7Sdan} 130962be2dc7Sdan 1310fcc057dbSdan#------------------------------------------------------------------------- 1311fcc057dbSdanreset_db 1312fcc057dbSdando_catchsql_test 35.0 { 1313fcc057dbSdan SELECT * WINDOW f AS () ORDER BY name COLLATE nocase; 1314fcc057dbSdan} {1 {no tables specified}} 1315fcc057dbSdan 1316fcc057dbSdando_catchsql_test 35.1 { 1317fcc057dbSdan VALUES(1) INTERSECT SELECT * WINDOW f AS () ORDER BY x COLLATE nocase; 1318fcc057dbSdan} {1 {no tables specified}} 1319fcc057dbSdan 1320fcc057dbSdando_execsql_test 35.2 { 1321fcc057dbSdan CREATE TABLE t1(x); 1322fcc057dbSdan INSERT INTO t1 VALUES(1), (2), (3); 1323fcc057dbSdan VALUES(1) INTERSECT 1324fcc057dbSdan SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; 1325fcc057dbSdan} {1} 1326fcc057dbSdan 1327fcc057dbSdando_execsql_test 35.3 { 1328fcc057dbSdan VALUES(8) EXCEPT 1329fcc057dbSdan SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; 1330fcc057dbSdan} {8} 1331fcc057dbSdan 1332fcc057dbSdando_execsql_test 35.4 { 1333fcc057dbSdan VALUES(1) UNION 1334fcc057dbSdan SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; 1335fcc057dbSdan} {1 3 6} 1336fcc057dbSdan 133729cdbadfSdrh# 2019-12-07 gramfuzz find 133829cdbadfSdrh# 133929cdbadfSdrhdo_execsql_test 36.10 { 134029cdbadfSdrh VALUES(count(*)OVER()); 134129cdbadfSdrh} {1} 134229cdbadfSdrhdo_execsql_test 36.20 { 134329cdbadfSdrh VALUES(count(*)OVER()),(2); 134429cdbadfSdrh} {1 2} 134529cdbadfSdrhdo_execsql_test 36.30 { 134629cdbadfSdrh VALUES(2),(count(*)OVER()); 134729cdbadfSdrh} {2 1} 134829cdbadfSdrhdo_execsql_test 36.40 { 134929cdbadfSdrh VALUES(2),(3),(count(*)OVER()),(4),(5); 135029cdbadfSdrh} {2 3 1 4 5} 135129cdbadfSdrh 13528c72afafSdrh# 2019-12-17 crash test case found by Yongheng and Rui 13538c72afafSdrh# See check-in 1ca0bd982ab1183b 13548c72afafSdrh# 13558c72afafSdrhreset_db 13568c72afafSdrhdo_execsql_test 37.10 { 13578c72afafSdrh CREATE TABLE t0(a UNIQUE, b PRIMARY KEY); 13588c72afafSdrh CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1))) FROM t0; 13598c72afafSdrh SELECT c FROM v0 WHERE c BETWEEN 10 AND 20; 13608c72afafSdrh} {} 13618c72afafSdrhdo_execsql_test 37.20 { 13628c72afafSdrh DROP VIEW v0; 13638c72afafSdrh CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1234))) FROM t0; 13648c72afafSdrh SELECT c FROM v0 WHERE c BETWEEN -10 AND 20; 13658c72afafSdrh} {} 13668c72afafSdrh 1367a9ebfe20Sdrh# 2019-12-20 mrigger reported problem with a FILTER clause on an aggregate 13688cc8feaeSdrh# in a join. 13698cc8feaeSdrh# 13708cc8feaeSdrhreset_db 13715e484cb3Sdando_catchsql_test 38.10 { 13728cc8feaeSdrh CREATE TABLE t0(c0); 13738cc8feaeSdrh CREATE TABLE t1(c0, c1 UNIQUE); 13748cc8feaeSdrh INSERT INTO t0(c0) VALUES(1); 13758cc8feaeSdrh INSERT INTO t1(c0,c1) VALUES(2,3); 13768cc8feaeSdrh SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(0) FILTER(WHERE t1.c1)); 13775e484cb3Sdan} {1 {misuse of aggregate: AVG()}} 13788cc8feaeSdrhdo_execsql_test 38.20 { 13798cc8feaeSdrh SELECT COUNT(*), AVG(1) FILTER(WHERE t1.c1) FROM t0, t1; 13808cc8feaeSdrh} {1 1.0} 13815e484cb3Sdando_catchsql_test 38.30 { 13828cc8feaeSdrh SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(1) FILTER(WHERE t1.c1)); 13835e484cb3Sdan} {1 {misuse of aggregate: AVG()}} 138429cdbadfSdrh 13852811ea6bSdanreset_db 13862811ea6bSdando_execsql_test 39.1 { 13872811ea6bSdan CREATE TABLE t0(c0 UNIQUE); 13882811ea6bSdan} 13892811ea6bSdando_execsql_test 39.2 { 13902811ea6bSdan SELECT FIRST_VALUE(0) OVER(); 13912811ea6bSdan} {0} 13922811ea6bSdando_execsql_test 39.3 { 13932811ea6bSdan SELECT * FROM t0 WHERE(c0, 0) IN(SELECT FIRST_VALUE(0) OVER(), 0); 13942811ea6bSdan} 13951d24a531Sdando_execsql_test 39.4 { 13961d24a531Sdan SELECT * FROM t0 WHERE (t0.c0, 1) IN(SELECT NTILE(1) OVER(), 0 FROM t0); 13971d24a531Sdan} 13981d24a531Sdan 13996473ba95Sdrhifcapable rtree { 1400a9ebfe20Sdrh # 2019-12-25 ticket d87336c81c7d0873 1401a9ebfe20Sdrh # 140243170437Sdan reset_db 140343170437Sdan do_catchsql_test 40.1 { 140443170437Sdan CREATE VIRTUAL TABLE t0 USING rtree(c0, c1, c2); 140543170437Sdan SELECT * FROM t0 140643170437Sdan WHERE ((0,0) IN (SELECT COUNT(*),LAG(5)OVER(PARTITION BY 0) FROM t0),0)<=(c1,0); 140743170437Sdan } {0 {}} 14086473ba95Sdrh} 1409a9ebfe20Sdrh 1410d8d2fb92Sdan#------------------------------------------------------------------------- 1411d8d2fb92Sdanreset_db 1412d8d2fb92Sdando_execsql_test 41.1 { 1413d8d2fb92Sdan CREATE TABLE t1(a, b, c); 1414d8d2fb92Sdan INSERT INTO t1 VALUES(NULL,'bb',355); 1415d8d2fb92Sdan INSERT INTO t1 VALUES('CC','aa',158); 1416d8d2fb92Sdan INSERT INTO t1 VALUES('GG','bb',929); 1417d8d2fb92Sdan INSERT INTO t1 VALUES('FF','Rb',574); 1418d8d2fb92Sdan} 1419d8d2fb92Sdan 1420d8d2fb92Sdando_execsql_test 41.2 { 1421d8d2fb92Sdan SELECT min(c) OVER ( 1422d8d2fb92Sdan ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING 1423d8d2fb92Sdan ) FROM t1 1424d8d2fb92Sdan} {355 158 574 929} 1425d8d2fb92Sdan 1426d8d2fb92Sdando_execsql_test 41.2 { 1427d8d2fb92Sdan SELECT min(c) OVER ( 1428d8d2fb92Sdan ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING 1429d8d2fb92Sdan ) << 100 FROM t1 1430d8d2fb92Sdan} {0 0 0 0} 1431d8d2fb92Sdan 1432d8d2fb92Sdando_execsql_test 41.3 { 1433d8d2fb92Sdan SELECT 1434d8d2fb92Sdan min(c) OVER win3 << first_value(c) OVER win3, 1435d8d2fb92Sdan min(c) OVER win3 << first_value(c) OVER win3 1436d8d2fb92Sdan FROM t1 1437d8d2fb92Sdan WINDOW win3 AS ( 1438d8d2fb92Sdan PARTITION BY 6 ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING 1439d8d2fb92Sdan ); 1440d8d2fb92Sdan} {0 0 0 0 0 0 0 0} 14412811ea6bSdan 144243170437Sdan#------------------------------------------------------------------------- 144343170437Sdanreset_db 144443170437Sdando_execsql_test 42.1 { 144543170437Sdan CREATE TABLE t1(a, b, c); 144643170437Sdan INSERT INTO t1 VALUES(1, 1, 1); 144743170437Sdan INSERT INTO t1 VALUES(2, 2, 2); 144843170437Sdan} 144943170437Sdando_execsql_test 42.2 { 145043170437Sdan SELECT * FROM t1 WHERE (0, 0) IN ( SELECT count(*), 0 FROM t1 ) 145143170437Sdan} {} 145243170437Sdando_execsql_test 42.3 { 145343170437Sdan SELECT * FROM t1 WHERE (2, 0) IN ( SELECT count(*), 0 FROM t1 ) 145443170437Sdan} {1 1 1 2 2 2} 145543170437Sdan 145643170437Sdando_execsql_test 42.3 { 145743170437Sdan SELECT count(*), max(a) OVER () FROM t1 GROUP BY c; 145843170437Sdan} {1 2 1 2} 145943170437Sdan 146043170437Sdando_execsql_test 42.4 { 146143170437Sdan SELECT sum(a), max(b) OVER () FROM t1; 146243170437Sdan} {3 1} 146343170437Sdan 146443170437Sdando_execsql_test 42.5 { 146543170437Sdan CREATE TABLE t2(a, b); 146643170437Sdan INSERT INTO t2 VALUES('a', 1); 146743170437Sdan INSERT INTO t2 VALUES('a', 2); 146843170437Sdan INSERT INTO t2 VALUES('a', 3); 146943170437Sdan INSERT INTO t2 VALUES('b', 4); 147043170437Sdan INSERT INTO t2 VALUES('b', 5); 147143170437Sdan INSERT INTO t2 VALUES('b', 6); 147243170437Sdan} 147343170437Sdan 147443170437Sdando_execsql_test 42.6 { 147543170437Sdan SELECT a, sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2 GROUP BY a; 147643170437Sdan} {a 6 6 b 15 21} 147743170437Sdan 147843170437Sdando_execsql_test 42.7 { 147943170437Sdan SELECT sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2; 148043170437Sdan} {21 21} 148143170437Sdan 1482e3735bf4Sdan#------------------------------------------------------------------------- 1483e3735bf4Sdanreset_db 1484e3735bf4Sdando_execsql_test 43.1.1 { 1485e3735bf4Sdan CREATE TABLE t1(x INTEGER PRIMARY KEY); 1486e3735bf4Sdan INSERT INTO t1 VALUES (10); 1487e3735bf4Sdan} 1488e3735bf4Sdando_catchsql_test 43.1.2 { 1489e3735bf4Sdan SELECT count() OVER() AS m FROM t1 ORDER BY (SELECT m); 1490e3735bf4Sdan} {1 {misuse of aliased window function m}} 1491e3735bf4Sdan 1492e3735bf4Sdanreset_db 1493e3735bf4Sdando_execsql_test 43.2.1 { 1494e3735bf4Sdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); 1495e3735bf4Sdan INSERT INTO t1(a, b) VALUES(1, 10); -- 10 1496e3735bf4Sdan INSERT INTO t1(a, b) VALUES(2, 15); -- 25 1497e3735bf4Sdan INSERT INTO t1(a, b) VALUES(3, -5); -- 20 1498e3735bf4Sdan INSERT INTO t1(a, b) VALUES(4, -5); -- 15 1499e3735bf4Sdan INSERT INTO t1(a, b) VALUES(5, 20); -- 35 1500e3735bf4Sdan INSERT INTO t1(a, b) VALUES(6, -11); -- 24 1501e3735bf4Sdan} 1502e3735bf4Sdan 1503e3735bf4Sdando_execsql_test 43.2.2 { 1504e3735bf4Sdan SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY 2 1505e3735bf4Sdan} { 1506e3735bf4Sdan 1 10 4 15 3 20 6 24 2 25 5 35 1507e3735bf4Sdan} 1508e3735bf4Sdan 1509e3735bf4Sdando_execsql_test 43.2.3 { 1510e3735bf4Sdan SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc 1511e3735bf4Sdan} { 1512e3735bf4Sdan 1 10 4 15 3 20 6 24 2 25 5 35 1513e3735bf4Sdan} 1514e3735bf4Sdan 1515e3735bf4Sdando_execsql_test 43.2.4 { 1516e3735bf4Sdan SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc+5 1517e3735bf4Sdan} { 1518e3735bf4Sdan 1 10 4 15 3 20 6 24 2 25 5 35 1519e3735bf4Sdan} 1520e3735bf4Sdan 1521e3735bf4Sdando_catchsql_test 43.2.5 { 1522e3735bf4Sdan SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc) 1523e3735bf4Sdan} {1 {misuse of aliased window function abc}} 1524e3735bf4Sdan 1525e3735bf4Sdando_catchsql_test 43.2.6 { 1526e3735bf4Sdan SELECT a, 1+sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc) 1527e3735bf4Sdan} {1 {misuse of aliased window function abc}} 1528e3735bf4Sdan 1529b4b36306Sdan#------------------------------------------------------------------------- 1530b4b36306Sdanreset_db 1531b4b36306Sdando_execsql_test 44.1 { 1532b4b36306Sdan CREATE TABLE t0(c0); 1533b4b36306Sdan} 1534b4b36306Sdan 1535b4b36306Sdando_catchsql_test 44.2.1 { 1536b4b36306Sdan SELECT ntile(0) OVER (); 1537b4b36306Sdan} {1 {argument of ntile must be a positive integer}} 1538b4b36306Sdando_catchsql_test 44.2.2 { 1539b4b36306Sdan SELECT (0, 0) IN(SELECT MIN(c0), NTILE(0) OVER()) FROM t0; 1540b4b36306Sdan} {1 {argument of ntile must be a positive integer}} 1541b4b36306Sdan 1542b4b36306Sdando_execsql_test 44.3.1 { 1543b4b36306Sdan SELECT ntile(1) OVER (); 1544b4b36306Sdan} {1} 1545b4b36306Sdando_execsql_test 44.3.2 { 1546b4b36306Sdan SELECT (0, 0) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0; 1547b4b36306Sdan} {0} 1548b4b36306Sdan 1549b4b36306Sdando_execsql_test 44.4.2 { 1550b4b36306Sdan INSERT INTO t0 VALUES(2), (1), (0); 1551b4b36306Sdan SELECT (0, 1) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0; 1552b4b36306Sdan} {1} 1553b4b36306Sdan 1554997d7434Sdan#------------------------------------------------------------------------- 1555997d7434Sdanreset_db 1556997d7434Sdando_execsql_test 45.1 { 1557997d7434Sdan CREATE TABLE t0(x); 1558997d7434Sdan CREATE TABLE t1(a); 1559997d7434Sdan INSERT INTO t1 VALUES(1000); 1560997d7434Sdan INSERT INTO t1 VALUES(1000); 1561997d7434Sdan INSERT INTO t0 VALUES(10000); 1562997d7434Sdan} 1563997d7434Sdando_execsql_test 45.2 { 1564997d7434Sdan SELECT * FROM ( 1565997d7434Sdan SELECT sum (a) OVER() FROM t1 UNION ALL SELECT x FROM t0 1566997d7434Sdan ); 1567997d7434Sdan} {2000 2000 10000} 1568997d7434Sdan 15694ea562eeSdan#------------------------------------------------------------------------- 15704ea562eeSdanreset_db 15714ea562eeSdando_execsql_test 46.1 { 15724ea562eeSdan CREATE TABLE t1 (a); 15734ea562eeSdan CREATE INDEX i1 ON t1(a); 15744ea562eeSdan 15754ea562eeSdan INSERT INTO t1 VALUES (10); 15764ea562eeSdan} 15774ea562eeSdan 15784ea562eeSdando_execsql_test 46.2 { 15794ea562eeSdan SELECT (SELECT sum(a) OVER(ORDER BY a)) FROM t1 15804ea562eeSdan} 10 15814ea562eeSdan 15824ea562eeSdando_execsql_test 46.3 { 15834ea562eeSdan SELECT * FROM t1 WHERE (SELECT sum(a) OVER(ORDER BY a)); 15844ea562eeSdan} 10 15854ea562eeSdan 15864ea562eeSdando_execsql_test 46.4 { 15874ea562eeSdan SELECT * FROM t1 NATURAL JOIN t1 15884ea562eeSdan WHERE a=1 15894ea562eeSdan OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10) 15904ea562eeSdan} 10 15914ea562eeSdan 1592aa328b6aSdan#------------------------------------------------------------------------- 1593aa328b6aSdanreset_db 1594aa328b6aSdando_execsql_test 47.0 { 1595aa328b6aSdan CREATE TABLE t1( 1596aa328b6aSdan a, 1597aa328b6aSdan e, 1598aa328b6aSdan f, 1599aa328b6aSdan g UNIQUE, 1600aa328b6aSdan h UNIQUE 1601aa328b6aSdan ); 1602aa328b6aSdan} 1603aa328b6aSdan 1604aa328b6aSdando_execsql_test 47.1 { 1605aa328b6aSdan CREATE VIEW t2(k) AS 1606aa328b6aSdan SELECT e FROM t1 WHERE g = 'abc' OR h BETWEEN 10 AND f; 1607aa328b6aSdan} 1608aa328b6aSdan 1609aa328b6aSdando_catchsql_test 47.2 { 1610aa328b6aSdan SELECT 234 FROM t2 1611aa328b6aSdan WHERE k=1 1612aa328b6aSdan OR (SELECT k FROM t2 WHERE (SELECT sum(a) OVER() FROM t1 GROUP BY 1)); 1613aa328b6aSdan} {1 {misuse of window function sum()}} 1614aa328b6aSdan 1615fbb6e9ffSdan#------------------------------------------------------------------------- 1616fbb6e9ffSdanreset_db 1617fbb6e9ffSdando_execsql_test 48.0 { 1618fbb6e9ffSdan CREATE TABLE t1(a); 1619fbb6e9ffSdan INSERT INTO t1 VALUES(1); 1620fbb6e9ffSdan INSERT INTO t1 VALUES(2); 1621fbb6e9ffSdan INSERT INTO t1 VALUES(3); 1622fbb6e9ffSdan SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x)) 1623fbb6e9ffSdan FROM (SELECT (SELECT sum(a) FROM t1) AS x FROM t1); 1624fbb6e9ffSdan} {12 12 12} 1625fbb6e9ffSdan 1626fbb6e9ffSdando_execsql_test 48.1 { 1627fbb6e9ffSdan SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x)) 1628fbb6e9ffSdan FROM (SELECT (SELECT sum(a) FROM t1 GROUP BY a) AS x FROM t1); 1629fbb6e9ffSdan} {2 2 2} 1630fbb6e9ffSdan 16310a21ea99Sdan#------------------------------------------------------------------------- 16320a21ea99Sdanreset_db 16330a21ea99Sdando_execsql_test 49.1 { 16340a21ea99Sdan CREATE TABLE t1 (a PRIMARY KEY); 16350a21ea99Sdan INSERT INTO t1 VALUES(1); 16360a21ea99Sdan} 16370a21ea99Sdan 16380a21ea99Sdando_execsql_test 49.2 { 16390a21ea99Sdan SELECT b AS c FROM ( 16400a21ea99Sdan SELECT a AS b FROM ( 16410a21ea99Sdan SELECT a FROM t1 WHERE a=1 OR (SELECT sum(a) OVER ()) 16420a21ea99Sdan ) 16430a21ea99Sdan WHERE b=1 OR b<10 16440a21ea99Sdan ) 16450a21ea99Sdan WHERE c=1 OR c>=10; 16460a21ea99Sdan} {1} 16470a21ea99Sdan 16480a21ea99Sdan 16490a21ea99Sdan#------------------------------------------------------------------------- 16500a21ea99Sdanreset_db 16510a21ea99Sdando_execsql_test 50.0 { 16520a21ea99Sdan CREATE TABLE t1 (a DOUBLE PRIMARY KEY); 16530a21ea99Sdan INSERT INTO t1 VALUES(10.0); 16540a21ea99Sdan} 16550a21ea99Sdan 16560a21ea99Sdando_execsql_test 50.1 { 16570a21ea99Sdan SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) 16580a21ea99Sdan} {10.0} 16590a21ea99Sdan 16600a21ea99Sdando_execsql_test 50.2 { 16610a21ea99Sdan SELECT * FROM ( 16620a21ea99Sdan SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) 16630a21ea99Sdan ) 16640a21ea99Sdan WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) 16650a21ea99Sdan} {10.0} 16660a21ea99Sdan 16670a21ea99Sdando_execsql_test 50.3 { 16680a21ea99Sdan SELECT a FROM ( 16690a21ea99Sdan SELECT * FROM ( 16700a21ea99Sdan SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) 16710a21ea99Sdan ) 16720a21ea99Sdan WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) 16730a21ea99Sdan ) 16740a21ea99Sdan WHERE a=1 OR a=10.0 16750a21ea99Sdan} {10.0} 16760a21ea99Sdan 16770a21ea99Sdando_execsql_test 50.4 { 16780a21ea99Sdan SELECT a FROM ( 16790a21ea99Sdan SELECT * FROM ( 16800a21ea99Sdan SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) 16810a21ea99Sdan ) 16820a21ea99Sdan WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) 16830a21ea99Sdan ) 16840a21ea99Sdan WHERE a=1 OR ((SELECT sum(a) OVER(ORDER BY a%8)) AND 10<=a) 16850a21ea99Sdan} {10.0} 16860a21ea99Sdan 16870a21ea99Sdando_execsql_test 50.5 { 16880a21ea99SdanSELECT * FROM (SELECT * FROM t1 NATURAL JOIN t1 WHERE a%1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum((SELECT * FROM (SELECT * FROM (SELECT * FROM t1 NATURAL JOIN t1 WHERE a%1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum((SELECT * FROM t1 NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)))OVER(ORDER BY a% 1 )) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND 10<=a)))OVER(ORDER BY a%5)) AND a<=10); 16890a21ea99Sdan} {10.0} 1690fbb6e9ffSdan 1691c415d910Sdrh# 2020-04-03 ticket af4556bb5c285c08 1692c415d910Sdrh# 1693c415d910Sdrhreset_db 1694c415d910Sdrhdo_catchsql_test 51.1 { 1695c415d910Sdrh CREATE TABLE a(b, c); 1696c415d910Sdrh SELECT c FROM a GROUP BY c 1697c415d910Sdrh HAVING(SELECT(sum(b) OVER(ORDER BY b), 1698c415d910Sdrh sum(b) OVER(PARTITION BY min(DISTINCT c), c ORDER BY b))); 1699c415d910Sdrh} {1 {row value misused}} 1700c415d910Sdrh 1701efa78884Sdan#------------------------------------------------------------------------- 1702efa78884Sdanreset_db 1703efa78884Sdando_execsql_test 52.1 { 1704efa78884Sdan CREATE TABLE t1(a, b, c); 1705efa78884Sdan INSERT INTO t1 VALUES('AA','bb',356); 1706efa78884Sdan INSERT INTO t1 VALUES('CC','aa',158); 1707efa78884Sdan INSERT INTO t1 VALUES('BB','aa',399); 1708efa78884Sdan INSERT INTO t1 VALUES('FF','bb',938); 1709efa78884Sdan} 1710efa78884Sdan 1711efa78884Sdando_execsql_test 52.2 { 1712efa78884Sdan SELECT 1713efa78884Sdan count() OVER win1, 1714efa78884Sdan sum(c) OVER win2, 1715efa78884Sdan first_value(c) OVER win2, 1716efa78884Sdan count(a) OVER (ORDER BY b) 1717efa78884Sdan FROM t1 1718efa78884Sdan WINDOW 1719efa78884Sdan win1 AS (ORDER BY a), 1720efa78884Sdan win2 AS (PARTITION BY 6 ORDER BY a 1721efa78884Sdan RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); 1722efa78884Sdan} { 1723efa78884Sdan 1 356 356 4 1724efa78884Sdan 2 399 399 2 1725efa78884Sdan 3 158 158 2 1726efa78884Sdan 4 938 938 4 1727efa78884Sdan} 1728efa78884Sdan 1729efa78884Sdando_execsql_test 52.3 { 1730efa78884SdanSELECT 1731efa78884Sdan count() OVER (), 1732efa78884Sdan sum(c) OVER win2, 1733efa78884Sdan first_value(c) OVER win2, 1734efa78884Sdan count(a) OVER (ORDER BY b) 1735efa78884SdanFROM t1 1736efa78884SdanWINDOW 1737efa78884Sdan win1 AS (ORDER BY a), 1738efa78884Sdan win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a 1739efa78884Sdan RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); 1740efa78884Sdan} { 1741efa78884Sdan 4 356 356 4 1742efa78884Sdan 4 399 399 2 1743efa78884Sdan 4 158 158 2 1744efa78884Sdan 4 938 938 4 1745efa78884Sdan} 1746efa78884Sdan 1747efa78884Sdando_execsql_test 52.4 { 1748efa78884Sdan SELECT 1749efa78884Sdan count() OVER win1, 1750efa78884Sdan sum(c) OVER win2, 1751efa78884Sdan first_value(c) OVER win2, 1752efa78884Sdan count(a) OVER (ORDER BY b) 1753efa78884Sdan FROM t1 1754efa78884Sdan WINDOW 1755efa78884Sdan win1 AS (ORDER BY a), 1756efa78884Sdan win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a 1757efa78884Sdan RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); 1758efa78884Sdan} { 1759efa78884Sdan 1 356 356 4 1760efa78884Sdan 2 399 399 2 1761efa78884Sdan 3 158 158 2 1762efa78884Sdan 4 938 938 4 1763efa78884Sdan} 1764efa78884Sdan 1765c37577bbSdrh# 2020-05-23 1766c37577bbSdrh# ticket 7a5279a25c57adf1 1767c37577bbSdrh# 1768c37577bbSdrhreset_db 1769c37577bbSdrhdo_execsql_test 53.0 { 1770c37577bbSdrh CREATE TABLE a(c UNIQUE); 1771c37577bbSdrh INSERT INTO a VALUES(4),(0),(9),(-9); 1772c37577bbSdrh SELECT a.c 1773c37577bbSdrh FROM a 1774c37577bbSdrh JOIN a AS b ON a.c=4 1775c37577bbSdrh JOIN a AS e ON a.c=e.c 1776c37577bbSdrh WHERE a.c=(SELECT (SELECT coalesce(lead(2) OVER(),0) + sum(d.c)) 1777c37577bbSdrh FROM a AS d 1778c37577bbSdrh WHERE a.c); 1779c37577bbSdrh} {4 4 4 4} 1780c37577bbSdrh 178189636628Sdrh#------------------------------------------------------------------------- 178289636628Sdrhreset_db 178389636628Sdrhdo_execsql_test 54.1 { 178489636628Sdrh CREATE TABLE t1(a VARCHAR(20), b FLOAT); 178589636628Sdrh INSERT INTO t1 VALUES('1',10.0); 178689636628Sdrh} 178789636628Sdrh 178889636628Sdrhdo_catchsql_test 54.2 { 178989636628Sdrh SELECT * FROM ( 179089636628Sdrh SELECT sum(b) OVER() AS c FROM t1 179189636628Sdrh UNION 179289636628Sdrh SELECT b AS c FROM t1 179389636628Sdrh ) WHERE c>10; 1794f65e3799Sdan} {0 {}} 179589636628Sdrh 179689636628Sdrhdo_execsql_test 54.3 { 179789636628Sdrh INSERT INTO t1 VALUES('2',5.0); 179889636628Sdrh INSERT INTO t1 VALUES('3',15.0); 179989636628Sdrh} 180089636628Sdrh 180189636628Sdrhdo_catchsql_test 54.4 { 180289636628Sdrh SELECT * FROM ( 180389636628Sdrh SELECT sum(b) OVER() AS c FROM t1 180489636628Sdrh UNION 180589636628Sdrh SELECT b AS c FROM t1 180689636628Sdrh ) WHERE c>10; 1807f65e3799Sdan} {0 {15.0 30.0}} 180889636628Sdrh 180989636628Sdrh# 2020-06-05 ticket c8d3b9f0a750a529 181089636628Sdrhreset_db 181189636628Sdrhdo_execsql_test 55.1 { 181289636628Sdrh CREATE TABLE a(b); 181389636628Sdrh SELECT 181489636628Sdrh (SELECT b FROM a 181589636628Sdrh GROUP BY b 181689636628Sdrh HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b)) 181789636628Sdrh ) 181889636628Sdrh FROM a 181989636628Sdrh UNION 182089636628Sdrh SELECT 99 182189636628Sdrh ORDER BY 1; 182289636628Sdrh} {99} 182389636628Sdrh 182489636628Sdrh#------------------------------------------------------------------------ 182589636628Sdrhreset_db 182689636628Sdrhdo_execsql_test 56.1 { 182789636628Sdrh CREATE TABLE t1(a, b INTEGER); 182889636628Sdrh CREATE TABLE t2(c, d); 182989636628Sdrh} 183089636628Sdrhdo_catchsql_test 56.2 { 183189636628Sdrh SELECT avg(b) FROM t1 183289636628Sdrh UNION ALL 183389636628Sdrh SELECT min(c) OVER () FROM t2 183489636628Sdrh ORDER BY nosuchcolumn; 183589636628Sdrh} {1 {1st ORDER BY term does not match any column in the result set}} 183689636628Sdrh 183789636628Sdrhreset_db 183889636628Sdrhdo_execsql_test 57.1 { 183989636628Sdrh CREATE TABLE t4(a, b, c, d, e); 184089636628Sdrh} 184189636628Sdrh 184289636628Sdrhdo_catchsql_test 57.2 { 184389636628Sdrh SELECT b FROM t4 184489636628Sdrh UNION 184589636628Sdrh SELECT a FROM t4 184689636628Sdrh ORDER BY ( 184789636628Sdrh SELECT sum(x) OVER() FROM ( 184889636628Sdrh SELECT c AS x FROM t4 184989636628Sdrh UNION 185089636628Sdrh SELECT d FROM t4 185189636628Sdrh ORDER BY (SELECT e FROM t4) 185289636628Sdrh ) 185389636628Sdrh ); 185489636628Sdrh} {1 {1st ORDER BY term does not match any column in the result set}} 185589636628Sdrh 185689636628Sdrh# 2020-06-06 various dbsqlfuzz finds and 185789636628Sdrh# ticket 0899cf62f597d7e7 185889636628Sdrh# 185989636628Sdrhreset_db 186089636628Sdrhdo_execsql_test 57.1 { 186189636628Sdrh CREATE TABLE t1(a, b, c); 186289636628Sdrh INSERT INTO t1 VALUES(NULL,NULL,NULL); 186389636628Sdrh SELECT 186489636628Sdrh sum(a), 186589636628Sdrh min(b) OVER (), 186689636628Sdrh count(c) OVER (ORDER BY b) 186789636628Sdrh FROM t1; 186889636628Sdrh} {{} {} 0} 186989636628Sdrhdo_execsql_test 57.2 { 187089636628Sdrh CREATE TABLE v0 ( v1 INTEGER PRIMARY KEY ) ; 187189636628Sdrh INSERT INTO v0 VALUES ( 10 ) ; 187289636628Sdrh SELECT DISTINCT v1, lead(v1) OVER() FROM v0 GROUP BY v1 ORDER BY 2; 187389636628Sdrh} {10 {}} 187489636628Sdrhdo_catchsql_test 57.3 { 187589636628Sdrh DROP TABLE t1; 187689636628Sdrh CREATE TABLE t1(a); 187789636628Sdrh INSERT INTO t1(a) VALUES(22); 187889636628Sdrh CREATE TABLE t3(y); 187989636628Sdrh INSERT INTO t3(y) VALUES(5),(11),(-9); 188089636628Sdrh SELECT ( 188189636628Sdrh SELECT max(y) OVER( ORDER BY (SELECT x FROM (SELECT sum(y) AS x FROM t1))) 188289636628Sdrh ) 188389636628Sdrh FROM t3; 188489636628Sdrh} {1 {misuse of aggregate: sum()}} 188589636628Sdrh 188689636628Sdrh# 2020-06-06 ticket 1f6f353b684fc708 188789636628Sdrhreset_db 188889636628Sdrhdo_execsql_test 58.1 { 188989636628Sdrh CREATE TABLE a(a, b, c); 189089636628Sdrh INSERT INTO a VALUES(1, 2, 3); 189189636628Sdrh INSERT INTO a VALUES(4, 5, 6); 189289636628Sdrh SELECT sum(345+b) OVER (ORDER BY b), 189389636628Sdrh sum(avg(678)) OVER (ORDER BY c) FROM a; 189489636628Sdrh} {347 678.0} 189589636628Sdrh 189689636628Sdrh# 2020-06-06 ticket e5504e987e419fb0 189789636628Sdrhdo_catchsql_test 59.1 { 189889636628Sdrh DROP TABLE IF EXISTS t1; 189989636628Sdrh CREATE TABLE t1(x INTEGER PRIMARY KEY); 190089636628Sdrh INSERT INTO t1 VALUES (123); 190189636628Sdrh SELECT 190289636628Sdrh ntile( (SELECT sum(x)) ) OVER(ORDER BY x), 190389636628Sdrh min(x) OVER(ORDER BY x) 190489636628Sdrh FROM t1; 190589636628Sdrh} {1 {misuse of aggregate: sum()}} 190689636628Sdrh 190789636628Sdrh# 2020-06-07 ticket f7d890858f361402 190889636628Sdrhdo_execsql_test 60.1 { 190989636628Sdrh DROP TABLE IF EXISTS t1; 191089636628Sdrh CREATE TABLE t1 (x INTEGER PRIMARY KEY); 191189636628Sdrh INSERT INTO t1 VALUES (99); 191289636628Sdrh SELECT EXISTS(SELECT count(*) OVER() FROM t1 ORDER BY sum(x) OVER()); 191389636628Sdrh} {1} 191489636628Sdrh 1915bf790973Sdrh# 2020-06-07 test case generated by dbsqlfuzz showing how an AggInfo 1916bf790973Sdrh# object might be referenced after the sqlite3Select() call that created 1917bf790973Sdrh# it returns. This proves the need to persist all AggInfo objects until 1918bf790973Sdrh# the Parse object is destroyed. 1919bf790973Sdrh# 192089636628Sdrhreset_db 19214752bbd8Sdrhdo_catchsql_test 61.1 { 1922bf790973SdrhCREATE TABLE t1(a); 1923bf790973SdrhINSERT INTO t1 VALUES(5),(NULL),('seventeen'); 1924bf790973SdrhSELECT (SELECT max(x)OVER(ORDER BY x) % min(x)OVER(ORDER BY CASE x WHEN 889 THEN x WHEN x THEN x END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST((SELECT (SELECT max(x)OVER(ORDER BY x) / min(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN -true THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x) & sum ( a )OVER(ORDER BY CASE x WHEN -8 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a AS )) FROM t1) AS x FROM t1)) AS t1 )) FROM t1) AS x FROM t1)) AS x )) FROM t1) AS x FROM t1)) AS real)) FROM t1) AS x FROM t1); 19253d691fd9Sdan} {0 {{} {} {}}} 19263d691fd9Sdan 19273d691fd9Sdanforeach tn {1 2} { 19283d691fd9Sdan if {$tn==2} { optimization_control db query-flattener 0 } 19293d691fd9Sdan do_catchsql_test 61.2.$tn { 19303d691fd9Sdan SELECT 19313d691fd9Sdan (SELECT max(x)OVER(ORDER BY x) / min(x) OVER() ) 19323d691fd9Sdan FROM ( 19333d691fd9Sdan SELECT (SELECT sum(a) FROM t1 ) AS x FROM t1 19343d691fd9Sdan ) 19353d691fd9Sdan 19363d691fd9Sdan } {0 {1.0 1.0 1.0}} 19373d691fd9Sdan} 19383d691fd9Sdan 19393d691fd9Sdanreset_db 19403d691fd9Sdanoptimization_control db all 0 19413d691fd9Sdando_execsql_test 61.3.0 { 19423d691fd9Sdan CREATE TABLE t1(a); 19433d691fd9Sdan CREATE TABLE t2(y); 19443d691fd9Sdan} 19453d691fd9Sdan 19463d691fd9Sdando_execsql_test 61.3.1 { 19473d691fd9Sdan SELECT ( 19483d691fd9Sdan SELECT count(a) OVER ( ORDER BY (SELECT sum(y) FROM t2) ) 19493d691fd9Sdan + total(a) OVER() 19503d691fd9Sdan ) 19513d691fd9Sdan FROM t1 19523d691fd9Sdan} {} 19533d691fd9Sdando_execsql_test 61.4.2 { 19543d691fd9Sdan SELECT ( 19553d691fd9Sdan SELECT count(a) OVER ( ORDER BY sum(a) ) 19563d691fd9Sdan + total(a) OVER() 19573d691fd9Sdan ) 19583d691fd9Sdan FROM t1 19593d691fd9Sdan} {0.0} 19603d691fd9Sdan 19613d691fd9Sdando_catchsql_test 61.4.3 { 19623d691fd9Sdan SELECT 19633d691fd9Sdan sum(a) OVER ( ORDER BY a ) 19643d691fd9Sdan FROM t1 19653d691fd9Sdan ORDER BY (SELECT sum(a) FROM t2) 19664752bbd8Sdrh} {1 {misuse of aggregate: sum()}} 19673d691fd9Sdando_execsql_test 61.4.4 { 19683d691fd9Sdan SELECT 19693d691fd9Sdan sum(a) OVER ( ORDER BY a ) 19703d691fd9Sdan FROM t1 19713d691fd9Sdan ORDER BY (SELECT sum(y) FROM t2) 19723d691fd9Sdan} 19733d691fd9Sdan 1974efa78884Sdan 1975f65e3799Sdan#------------------------------------------------------------------------- 1976f65e3799Sdanreset_db 1977f65e3799Sdando_execsql_test 62.1 { 1978f65e3799Sdan CREATE TABLE t1(a VARCHAR(20), b FLOAT); 1979f65e3799Sdan INSERT INTO t1 VALUES('1',10.0); 1980f65e3799Sdan} 1981f65e3799Sdan 1982f65e3799Sdando_execsql_test 62.2 { 1983f65e3799Sdan SELECT * FROM ( 1984f65e3799Sdan SELECT sum(b) OVER() AS c FROM t1 1985f65e3799Sdan UNION 1986f65e3799Sdan SELECT b AS c FROM t1 1987f65e3799Sdan ) WHERE c>10; 1988f65e3799Sdan} 1989f65e3799Sdan 1990f65e3799Sdando_execsql_test 62.3 { 1991f65e3799Sdan INSERT INTO t1 VALUES('2',5.0); 1992f65e3799Sdan INSERT INTO t1 VALUES('3',15.0); 1993f65e3799Sdan} 1994f65e3799Sdan 1995f65e3799Sdando_execsql_test 62.4 { 1996f65e3799Sdan SELECT * FROM ( 1997f65e3799Sdan SELECT sum(b) OVER() AS c FROM t1 1998f65e3799Sdan UNION 1999f65e3799Sdan SELECT b AS c FROM t1 2000f65e3799Sdan ) WHERE c>10; 2001f65e3799Sdan} {15.0 30.0} 2002f65e3799Sdan 200346daa99bSdan#------------------------------------------------------------------------- 200446daa99bSdanreset_db 200546daa99bSdando_execsql_test 63.1 { 200646daa99bSdan CREATE TABLE t1(b, x); 200746daa99bSdan CREATE TABLE t2(c, d); 200846daa99bSdan CREATE TABLE t3(e, f); 200946daa99bSdan} 201046daa99bSdan 201146daa99bSdando_execsql_test 63.2 { 201246daa99bSdan SELECT max(b) OVER( 201346daa99bSdan ORDER BY SUM( 201446daa99bSdan (SELECT c FROM t2 UNION SELECT x ORDER BY c) 201546daa99bSdan ) 201646daa99bSdan ) FROM t1; 201746daa99bSdan} {{}} 201846daa99bSdan 201946daa99bSdando_execsql_test 63.3 { 202046daa99bSdan SELECT sum(b) over( 202146daa99bSdan ORDER BY ( 202246daa99bSdan SELECT max(b) OVER( 202346daa99bSdan ORDER BY sum( 202446daa99bSdan (SELECT x AS c UNION SELECT 1234 ORDER BY c) 202546daa99bSdan ) 202646daa99bSdan ) AS e 202746daa99bSdan ORDER BY e 202846daa99bSdan ) 202946daa99bSdan ) 203046daa99bSdan FROM t1; 203146daa99bSdan} {{}} 203246daa99bSdan 203327da907fSdan#------------------------------------------------------------------------- 203427da907fSdanreset_db 203527da907fSdando_execsql_test 64.1 { 203627da907fSdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 203727da907fSdan INSERT INTO t1 VALUES(1, 'abcd'); 203827da907fSdan INSERT INTO t1 VALUES(2, 'BCDE'); 203927da907fSdan INSERT INTO t1 VALUES(3, 'cdef'); 204027da907fSdan INSERT INTO t1 VALUES(4, 'DEFG'); 204127da907fSdan} 204227da907fSdan 204327da907fSdando_execsql_test 64.2 { 204427da907fSdan SELECT rowid, max(b COLLATE nocase)||'' 204527da907fSdan FROM t1 204627da907fSdan GROUP BY rowid 204727da907fSdan ORDER BY max(b COLLATE nocase)||''; 204827da907fSdan} {1 abcd 2 BCDE 3 cdef 4 DEFG} 204927da907fSdan 205027da907fSdando_execsql_test 64.3 { 205127da907fSdan SELECT count() OVER (), rowid, max(b COLLATE nocase)||'' 205227da907fSdan FROM t1 205327da907fSdan GROUP BY rowid 205427da907fSdan ORDER BY max(b COLLATE nocase)||''; 205527da907fSdan} {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG} 205627da907fSdan 205727da907fSdando_execsql_test 64.4 { 205827da907fSdan SELECT count() OVER (), rowid, max(b COLLATE nocase) 205927da907fSdan FROM t1 206027da907fSdan GROUP BY rowid 206127da907fSdan ORDER BY max(b COLLATE nocase); 206227da907fSdan} {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG} 206327da907fSdan 206427da907fSdan#------------------------------------------------------------------------- 206527da907fSdanreset_db 206627da907fSdando_execsql_test 65.1 { 206727da907fSdan CREATE TABLE t1(c1); 206827da907fSdan INSERT INTO t1 VALUES('abcd'); 206927da907fSdan} 207027da907fSdando_execsql_test 65.2 { 207127da907fSdan SELECT max(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1; 207227da907fSdan} {1} 207327da907fSdan 207427da907fSdando_execsql_test 65.3 { 207527da907fSdan SELECT 207627da907fSdan count() OVER (), 207727da907fSdan group_concat(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1; 207827da907fSdan} {1 1} 207927da907fSdan 208027da907fSdando_execsql_test 65.4 { 208127da907fSdan SELECT COUNT() OVER () LIKE lead(102030) OVER( 208227da907fSdan ORDER BY sum('abcdef' COLLATE nocase) IN (SELECT 54321) 208327da907fSdan ) 208427da907fSdan FROM t1; 208527da907fSdan} {{}} 208627da907fSdan 20876b6ec407Sdan#------------------------------------------------------------------------- 20886b6ec407Sdanreset_db 20896b6ec407Sdan 20906b6ec407Sdando_execsql_test 66.1 { 20916b6ec407Sdan CREATE TABLE t1(a INTEGER); 20926b6ec407Sdan INSERT INTO t1 VALUES(3578824042033200656); 20936b6ec407Sdan INSERT INTO t1 VALUES(3029012920382354029); 20946b6ec407Sdan} 20956b6ec407Sdan 20966b6ec407Sdanforeach {tn spec} { 20976b6ec407Sdan 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" 20986b6ec407Sdan 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING" 20996b6ec407Sdan 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING" 21006b6ec407Sdan 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" 21016b6ec407Sdan 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" 21026b6ec407Sdan 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING" 21036b6ec407Sdan} { 21046b6ec407Sdan do_execsql_test 66.2.$tn " 21056b6ec407Sdan SELECT total(a) OVER ( $spec ) FROM t1 ORDER BY a 21066b6ec407Sdan " { 21076b6ec407Sdan 3.02901292038235e+18 3.5788240420332e+18 21086b6ec407Sdan } 21096b6ec407Sdan} 21106b6ec407Sdan 21116b6ec407Sdan 21126b6ec407Sdando_execsql_test 66.3 { 21136b6ec407Sdan CREATE TABLE t2(a INTEGER); 21146b6ec407Sdan INSERT INTO t2 VALUES(45); 21156b6ec407Sdan INSERT INTO t2 VALUES(30); 21166b6ec407Sdan} 21176b6ec407Sdan 21186b6ec407Sdanforeach {tn spec res} { 21196b6ec407Sdan 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} 21206b6ec407Sdan 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING" {0.0 0.0} 21216b6ec407Sdan 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING" {0.0 0.0} 21226b6ec407Sdan 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} 21236b6ec407Sdan 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} 21246b6ec407Sdan 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING" {0.0 0.0} 21256b6ec407Sdan} { 21266b6ec407Sdan do_execsql_test 66.2.$tn " 21276b6ec407Sdan SELECT total(a) OVER ( $spec ) FROM t2 ORDER BY a 21286b6ec407Sdan " $res 21296b6ec407Sdan} 21306b6ec407Sdan 2131d487e373Sdan 2132d487e373Sdan#------------------------------------------------------------------------- 2133d487e373Sdanreset_db 2134d487e373Sdando_execsql_test 67.0 { 2135d487e373Sdan CREATE TABLE t1(a, b, c); 2136d487e373Sdan CREATE TABLE t2(a, b, c); 2137d487e373Sdan} 2138d487e373Sdan 2139d487e373Sdando_catchsql_test 67.1 { 2140d487e373Sdan SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY ( 2141d487e373Sdan SELECT nth_value(a,2) OVER w1 2142d487e373Sdan WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM v1)) ) 2143d487e373Sdan ) 2144be12083bSdan} {1 {no such table: v1}} 2145be12083bSdan 2146be12083bSdando_catchsql_test 67.2 { 2147be12083bSdan SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY ( 2148be12083bSdan SELECT nth_value(a,2) OVER w1 2149be12083bSdan WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM t2)) ) 2150be12083bSdan ) 2151d487e373Sdan} {1 {1st ORDER BY term does not match any column in the result set}} 2152d487e373Sdan 21534752bbd8Sdrh# 2021-05-07 21544752bbd8Sdrh# Do not allow aggregate functions in the ORDER BY clause even if 21554752bbd8Sdrh# there are window functions in the result set. 21564752bbd8Sdrh# Forum: /forumpost/540fdfef77 21574752bbd8Sdrh# 21584752bbd8Sdrhreset_db 21594752bbd8Sdrhdo_catchsql_test 68.0 { 21604752bbd8Sdrh CREATE TABLE t1(a,b); 21614752bbd8Sdrh INSERT INTO t1(a,b) VALUES(0,0),(1,1),(2,4),(3,9),(4,99); 21624752bbd8Sdrh SELECT rowid, a, b, sum(a)OVER() FROM t1 ORDER BY count(b); 21634752bbd8Sdrh} {1 {misuse of aggregate: count()}} 21644752bbd8Sdrh 21655e1a7dedSdrh# 2021-05-22 21665e1a7dedSdrh# Forum https://sqlite.org/forum/forumpost/7e484e225c 21675e1a7dedSdrh# 21685e1a7dedSdrhreset_db 21695e1a7dedSdrhdo_catchsql_test 69.0 { 21705e1a7dedSdrh CREATE TABLE t1(a,b); 21715e1a7dedSdrh CREATE INDEX t1ba ON t1(b,a); 217205428127Sdrh SELECT * FROM t1 WHERE b = (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); 217305428127Sdrh} {1 {misuse of aggregate: sum()}} 217405428127Sdrhdo_catchsql_test 69.1 { 217505428127Sdrh SELECT * FROM t1 WHERE b >= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); 217605428127Sdrh} {1 {misuse of aggregate: sum()}} 217705428127Sdrhdo_catchsql_test 69.2 { 217805428127Sdrh SELECT * FROM t1 WHERE b <= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); 217905428127Sdrh} {1 {misuse of aggregate: sum()}} 218005428127Sdrh 2181a261c02dSdan# 2021-06-23 2182a261c02dSdan# Forum https://sqlite.org/forum/forumpost/31e0432608 2183a261c02dSdan# 2184a261c02dSdanreset_db 2185a261c02dSdando_execsql_test 70.0 { 2186a261c02dSdan CREATE TABLE t1(a); 2187a261c02dSdan} 2188a261c02dSdando_execsql_test 70.1 { 2189a261c02dSdan SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY 2)) AS ca0 FROM t1 ORDER BY ca0; 2190a261c02dSdan} 2191a261c02dSdando_execsql_test 70.2 { 2192a261c02dSdan SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY likely(2))) AS ca0 FROM t1 ORDER BY ca0; 2193a261c02dSdan} 2194a261c02dSdan 21956d64b4a0Sdrh# 2021-11-07 21966d64b4a0Sdrh# Bug report from Wang Ke 21976d64b4a0Sdrh# https://sqlite.org/forum/forumpost/9ba4f60ff8 21986d64b4a0Sdrhreset_db 21996d64b4a0Sdrhdo_catchsql_test 71.0 { 22006d64b4a0Sdrh CREATE TABLE t0(a); 22016d64b4a0Sdrh SELECT a FROM t0, (SELECT a AS b FROM t0) 22026d64b4a0Sdrh WHERE (a,1)=(SELECT 2,2 UNION SELECT sum(b),max(b) OVER(ORDER BY b) ORDER BY 2) 22036d64b4a0Sdrh AND b=4 22046d64b4a0Sdrh ORDER BY b; 22056d64b4a0Sdrh} {/1 {.*}/} 22066d64b4a0Sdrh 2207b9294de1Sdrhdo_execsql_test 72.1 { 2208b9294de1Sdrh CREATE TABLE dual(dummy); INSERT INTO dual VALUES('X'); 2209b9294de1Sdrh CREATE VIEW v1(x,y) AS SELECT RANK() OVER (PARTITION BY 0), SUM(0) FROM dual; 2210b9294de1Sdrh SELECT * FROM v1 WHERE true; 2211b9294de1Sdrh} {1 0} 22125e1a7dedSdrh 2213d4cb09e3Sdrhfinish_test 2214