3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix window1
18 ifcapable !windowfunc {
24 CREATE TABLE t1(a, b, c, d);
25 INSERT INTO t1 VALUES(1, 2, 3, 4);
26 INSERT INTO t1 VALUES(5, 6, 7, 8);
27 INSERT INTO t1 VALUES(9, 10, 11, 12);
31 SELECT sum(b) OVER () FROM t1
35 SELECT a, sum(b) OVER () FROM t1
39 SELECT a, 4 + sum(b) OVER () FROM t1
43 SELECT a + 4 + sum(b) OVER () FROM t1
47 SELECT a, sum(b) OVER (PARTITION BY c) FROM t1
51 1 "SELECT sum(b) OVER () FROM t1"
52 2 "SELECT sum(b) OVER (PARTITION BY c) FROM t1"
53 3 "SELECT sum(b) OVER (ORDER BY c) FROM t1"
54 4 "SELECT sum(b) OVER (PARTITION BY d ORDER BY c) FROM t1"
55 5 "SELECT sum(b) FILTER (WHERE a>0) OVER (PARTITION BY d ORDER BY c) FROM t1"
56 6 "SELECT sum(b) OVER (ORDER BY c RANGE UNBOUNDED PRECEDING) FROM t1"
57 7 "SELECT sum(b) OVER (ORDER BY c ROWS 45 PRECEDING) FROM t1"
58 8 "SELECT sum(b) OVER (ORDER BY c RANGE CURRENT ROW) FROM t1"
59 9 "SELECT sum(b) OVER (ORDER BY c RANGE BETWEEN UNBOUNDED PRECEDING
60 AND CURRENT ROW) FROM t1"
61 10 "SELECT sum(b) OVER (ORDER BY c ROWS BETWEEN UNBOUNDED PRECEDING
62 AND UNBOUNDED FOLLOWING) FROM t1"
64 do_test 2.$tn { lindex [catchsql $sql] 0 } 0
68 1 "SELECT * FROM t1 WHERE sum(b) OVER ()"
69 2 "SELECT * FROM t1 GROUP BY sum(b) OVER ()"
70 3 "SELECT * FROM t1 GROUP BY a HAVING sum(b) OVER ()"
72 do_catchsql_test 3.$tn $sql {1 {misuse of window function sum()}}
76 CREATE TABLE t2(a, b, c);
77 INSERT INTO t2 VALUES(0, 0, 0);
78 INSERT INTO t2 VALUES(1, 1, 1);
79 INSERT INTO t2 VALUES(2, 0, 2);
80 INSERT INTO t2 VALUES(3, 1, 0);
81 INSERT INTO t2 VALUES(4, 0, 1);
82 INSERT INTO t2 VALUES(5, 1, 2);
83 INSERT INTO t2 VALUES(6, 0, 0);
87 SELECT a, sum(a) OVER (PARTITION BY b) FROM t2;
89 0 12 2 12 4 12 6 12 1 9 3 9 5 9
93 SELECT a, sum(a) OVER (PARTITION BY b) FROM t2 ORDER BY a;
95 0 12 1 9 2 12 3 9 4 12 5 9 6 12
99 SELECT a, sum(a) OVER () FROM t2 ORDER BY a;
101 0 21 1 21 2 21 3 21 4 21 5 21 6 21
104 do_execsql_test 4.4 {
105 SELECT a, sum(a) OVER (ORDER BY a) FROM t2;
107 0 0 1 1 2 3 3 6 4 10 5 15 6 21
110 do_execsql_test 4.5 {
111 SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a) FROM t2 ORDER BY a
113 0 0 1 1 2 2 3 4 4 6 5 9 6 12
116 do_execsql_test 4.6 {
117 SELECT a, sum(a) OVER (PARTITION BY c ORDER BY a) FROM t2 ORDER BY a
119 0 0 1 1 2 2 3 3 4 5 5 7 6 9
122 do_execsql_test 4.7 {
123 SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a DESC) FROM t2 ORDER BY a
125 0 12 1 9 2 12 3 8 4 10 5 5 6 6
128 do_execsql_test 4.8 {
130 sum(a) OVER (PARTITION BY b ORDER BY a DESC),
131 sum(a) OVER (PARTITION BY c ORDER BY a)
143 do_execsql_test 4.9 {
145 sum(a) OVER (ORDER BY a),
146 avg(a) OVER (ORDER BY a)
158 do_execsql_test 4.10.1 {
160 count() OVER (ORDER BY a DESC),
161 string_agg(a, '.') OVER (ORDER BY a DESC)
162 FROM t2 ORDER BY a DESC
173 do_execsql_test 4.10.2 {
175 count(*) OVER (ORDER BY a DESC),
176 group_concat(a, '.') OVER (ORDER BY a DESC)
177 FROM t2 ORDER BY a DESC
188 do_catchsql_test 5.1 {
189 SELECT ntile(0) OVER (ORDER BY a) FROM t2;
190 } {1 {argument of ntile must be a positive integer}}
191 do_catchsql_test 5.2 {
192 SELECT ntile(-1) OVER (ORDER BY a) FROM t2;
193 } {1 {argument of ntile must be a positive integer}}
194 do_catchsql_test 5.3 {
195 SELECT ntile('zbc') OVER (ORDER BY a) FROM t2;
196 } {1 {argument of ntile must be a positive integer}}
197 do_execsql_test 5.4 {
198 CREATE TABLE t4(a, b);
199 SELECT ntile(1) OVER (ORDER BY a) FROM t4;
202 #-------------------------------------------------------------------------
204 do_execsql_test 6.1 {
206 INSERT INTO t1 VALUES(7), (6), (5), (4), (3), (2), (1);
209 INSERT INTO t2 VALUES('b'), ('a');
211 SELECT x, count(*) OVER (ORDER BY x) FROM t1;
212 } {1 1 2 2 3 3 4 4 5 5 6 6 7 7}
214 do_execsql_test 6.2 {
215 SELECT * FROM t2, (SELECT x, count(*) OVER (ORDER BY x) FROM t1)
218 a 1 1 a 2 2 a 3 3 a 4 4 a 5 5 a 6 6 a 7 7
219 b 1 1 b 2 2 b 3 3 b 4 4 b 5 5 b 6 6 b 7 7
222 do_catchsql_test 6.3 {
223 SELECT x, lag(x) FILTER (WHERE (x%2)=0) OVER w FROM t1
224 WINDOW w AS (ORDER BY x)
225 } {1 {FILTER clause may only be used with aggregate window functions}}
227 #-------------------------------------------------------------------------
228 # Attempt to use a window function as an aggregate. And other errors.
231 do_execsql_test 7.0 {
232 CREATE TABLE t1(x, y);
233 INSERT INTO t1 VALUES(1, 2);
234 INSERT INTO t1 VALUES(3, 4);
235 INSERT INTO t1 VALUES(5, 6);
236 INSERT INTO t1 VALUES(7, 8);
237 INSERT INTO t1 VALUES(9, 10);
240 do_catchsql_test 7.1.1 {
241 SELECT nth_value(x, 1) FROM t1;
242 } {1 {misuse of window function nth_value()}}
243 do_catchsql_test 7.1.2 {
244 SELECT * FROM t1 WHERE nth_value(x, 1) OVER (ORDER BY y);
245 } {1 {misuse of window function nth_value()}}
246 do_catchsql_test 7.1.3 {
247 SELECT count(*) FROM t1 GROUP BY y HAVING nth_value(x, 1) OVER (ORDER BY y);
248 } {1 {misuse of window function nth_value()}}
249 do_catchsql_test 7.1.4 {
250 SELECT count(*) FROM t1 GROUP BY nth_value(x, 1) OVER (ORDER BY y);
251 } {1 {misuse of window function nth_value()}}
252 do_catchsql_test 7.1.5 {
253 SELECT count(*) FROM t1 LIMIT nth_value(x, 1) OVER ();
254 } {1 {no such column: x}}
255 do_catchsql_test 7.1.6 {
256 SELECT trim(x) OVER (ORDER BY y) FROM t1;
257 } {1 {trim() may not be used as a window function}}
258 do_catchsql_test 7.1.7 {
259 SELECT max(x) OVER abc FROM t1 WINDOW def AS (ORDER BY y);
260 } {1 {no such window: abc}}
261 do_catchsql_test 7.1.8 {
262 SELECT row_number(x) OVER () FROM t1
263 } {1 {wrong number of arguments to function row_number()}}
265 do_execsql_test 7.2 {
269 lead(y, 3, 'default') OVER win
271 WINDOW win AS (ORDER BY x)
273 4 6 8 6 8 10 8 10 default 10 {} default {} {} default
276 do_execsql_test 7.3 {
277 SELECT row_number() OVER (ORDER BY x) FROM t1
280 do_execsql_test 7.4 {
282 row_number() OVER win,
285 WINDOW win AS (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
286 } {1 3 2 5 3 7 4 9 5 {}}
288 #-------------------------------------------------------------------------
289 # Attempt to use a window function in a view.
291 do_execsql_test 8.0 {
292 CREATE TABLE t3(a, b, c);
294 WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<6 )
295 INSERT INTO t3 SELECT i, i, i FROM s;
297 CREATE VIEW v1 AS SELECT
298 sum(b) OVER (ORDER BY c),
299 min(b) OVER (ORDER BY c),
300 max(b) OVER (ORDER BY c)
303 CREATE VIEW v2 AS SELECT
308 WINDOW win AS (ORDER BY c);
311 do_execsql_test 8.1.1 {
313 } {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6}
314 do_execsql_test 8.1.2 {
316 } {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6}
320 do_execsql_test 8.2.1 {
322 } {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6}
323 do_execsql_test 8.2.2 {
325 } {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6}
327 #-------------------------------------------------------------------------
328 # Attempt to use a window function in a trigger.
330 do_execsql_test 9.0 {
331 CREATE TABLE t4(x, y);
332 INSERT INTO t4 VALUES(1, 'g');
333 INSERT INTO t4 VALUES(2, 'i');
334 INSERT INTO t4 VALUES(3, 'l');
335 INSERT INTO t4 VALUES(4, 'g');
336 INSERT INTO t4 VALUES(5, 'a');
338 CREATE TABLE t5(x, y, m);
339 CREATE TRIGGER t4i AFTER INSERT ON t4 BEGIN
342 SELECT x, y, max(y) OVER xyz FROM t4
343 WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x);
347 do_execsql_test 9.1.1 {
348 SELECT x, y, max(y) OVER xyz FROM t4
349 WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1
350 } {1 g g 2 i i 3 l l 4 g i 5 a l}
352 do_execsql_test 9.1.2 {
353 INSERT INTO t4 VALUES(6, 'm');
354 SELECT x, y, max(y) OVER xyz FROM t4
355 WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1
356 } {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m}
358 do_execsql_test 9.1.3 {
359 SELECT * FROM t5 ORDER BY 1
360 } {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m}
362 do_execsql_test 9.2 {
363 WITH aaa(x, y, z) AS (
364 SELECT x, y, max(y) OVER xyz FROM t4
365 WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x)
367 SELECT * FROM aaa ORDER BY 1;
368 } {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m}
370 do_execsql_test 9.3 {
371 WITH aaa(x, y, z) AS (
372 SELECT x, y, max(y) OVER xyz FROM t4
373 WINDOW xyz AS (ORDER BY x)
375 SELECT *, min(z) OVER (ORDER BY x) FROM aaa ORDER BY 1;
376 } {1 g g g 2 i i g 3 l l g 4 g l g 5 a l g 6 m m g}
378 do_catchsql_test 9.4 {
379 -- 2021-04-17 dbsqlfuzz d9cf66100064952b66951845dfab41de1c124611
380 DROP TABLE IF EXISTS t1;
381 CREATE TABLE t1(a,b,c,d);
382 DROP TABLE IF EXISTS t2;
383 CREATE TABLE t2(x,y);
384 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
386 SELECT a, max(d) OVER w1 FROM t1
387 WINDOW w1 AS (PARTITION BY EXISTS(SELECT 1 FROM t1 WHERE c=?1) );
389 } {1 {trigger cannot use variables}}
391 do_catchsql_test 9.4.2 {
392 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
394 SELECT a, max(d) OVER w1 FROM t1
396 ORDER BY a ROWS BETWEEN ? PRECEDING AND UNBOUNDED FOLLOWING
399 } {1 {trigger cannot use variables}}
400 do_catchsql_test 9.4.3 {
401 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
403 SELECT a, max(d) OVER w1 FROM t1
405 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND ? FOLLOWING
408 } {1 {trigger cannot use variables}}
410 #-------------------------------------------------------------------------
412 do_execsql_test 10.0 {
413 CREATE TABLE sales(emp TEXT PRIMARY KEY, region, total);
414 INSERT INTO sales VALUES
415 ('Alice', 'North', 34),
416 ('Frank', 'South', 22),
417 ('Charles', 'North', 45),
418 ('Darrell', 'South', 8),
419 ('Grant', 'South', 23),
420 ('Brad' , 'North', 22),
421 ('Elizabeth', 'South', 99),
422 ('Horace', 'East', 1);
425 # Best two salespeople from each region
427 do_execsql_test 10.1 {
428 SELECT emp, region, total FROM (
431 row_number() OVER (PARTITION BY region ORDER BY total DESC) AS rank
433 ) WHERE rank<=2 ORDER BY region, total DESC
442 do_execsql_test 10.2 {
443 SELECT emp, region, sum(total) OVER win FROM sales
444 WINDOW win AS (PARTITION BY region ORDER BY total)
456 do_execsql_test 10.3 {
457 SELECT emp, region, sum(total) OVER win FROM sales
458 WINDOW win AS (PARTITION BY region ORDER BY total)
468 do_execsql_test 10.4 {
469 SELECT emp, region, sum(total) OVER win FROM sales
470 WINDOW win AS (PARTITION BY region ORDER BY total)
480 do_execsql_test 10.5 {
481 SELECT emp, region, sum(total) OVER win FROM sales
483 PARTITION BY region ORDER BY total
484 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
497 do_execsql_test 10.6 {
498 SELECT emp, region, sum(total) OVER win FROM sales
500 PARTITION BY region ORDER BY total
501 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
511 do_execsql_test 10.7 {
512 SELECT emp, region, (
513 SELECT sum(total) OVER (
514 ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
515 ) || outer.emp FROM sales
516 ) FROM sales AS outer;
520 Charles North 254Charles
521 Darrell South 254Darrell
524 Elizabeth South 254Elizabeth
525 Horace East 254Horace
528 do_execsql_test 10.8 {
529 SELECT emp, region, (
530 SELECT sum(total) FILTER (WHERE sales.emp!=outer.emp) OVER (
531 ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
533 ) FROM sales AS outer;
545 #-------------------------------------------------------------------------
546 # Check that it is not possible to use a window function in a CREATE INDEX
549 do_execsql_test 11.0 { CREATE TABLE t6(a, b, c); }
551 do_catchsql_test 11.1 {
552 CREATE INDEX t6i ON t6(a) WHERE sum(b) OVER ();
553 } {1 {misuse of window function sum()}}
554 do_catchsql_test 11.2 {
555 CREATE INDEX t6i ON t6(a) WHERE lead(b) OVER ();
556 } {1 {misuse of window function lead()}}
558 do_catchsql_test 11.3 {
559 CREATE INDEX t6i ON t6(sum(b) OVER ());
560 } {1 {misuse of window function sum()}}
561 do_catchsql_test 11.4 {
562 CREATE INDEX t6i ON t6(lead(b) OVER ());
563 } {1 {misuse of window function lead()}}
565 # 2018-09-17 ticket 510cde277783b5fb5de628393959849dff377eb3
566 # Endless loop on a query with window functions and a limit
568 do_execsql_test 12.100 {
569 DROP TABLE IF EXISTS t1;
570 CREATE TABLE t1(id INT, b VARCHAR, c VARCHAR);
571 INSERT INTO t1 VALUES(1, 'A', 'one');
572 INSERT INTO t1 VALUES(2, 'B', 'two');
573 INSERT INTO t1 VALUES(3, 'C', 'three');
574 INSERT INTO t1 VALUES(4, 'D', 'one');
575 INSERT INTO t1 VALUES(5, 'E', 'two');
576 SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x
580 do_execsql_test 12.110 {
581 INSERT INTO t1 VALUES(6, 'F', 'three');
582 INSERT INTO t1 VALUES(7, 'G', 'one');
583 SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x
586 } {2 B two 3 C three}
588 #-------------------------------------------------------------------------
590 do_execsql_test 13.1 {
591 DROP TABLE IF EXISTS t1;
592 CREATE TABLE t1(a int, b int);
593 INSERT INTO t1 VALUES(1,11);
594 INSERT INTO t1 VALUES(2,12);
597 do_execsql_test 13.2.1 {
598 SELECT a, rank() OVER(ORDER BY b) FROM t1;
599 SELECT a, rank() OVER(ORDER BY b DESC) FROM t1;
603 do_execsql_test 13.2.2 {
604 SELECT a, rank() OVER(ORDER BY b) FROM t1
606 SELECT a, rank() OVER(ORDER BY b DESC) FROM t1;
610 do_execsql_test 13.3 {
611 SELECT a, rank() OVER(ORDER BY b) FROM t1
613 SELECT a, rank() OVER(ORDER BY b DESC) FROM t1;
618 do_execsql_test 13.4 {
619 SELECT a, rank() OVER(ORDER BY b) FROM t1
621 SELECT a, rank() OVER(ORDER BY b DESC) FROM t1;
626 do_execsql_test 13.5 {
627 SELECT a, rank() OVER(ORDER BY b) FROM t1
629 SELECT a, rank() OVER(ORDER BY b DESC) FROM t1;
633 # https://www.sqlite.org/src/info/f09fcd17810f65f7
634 # Assertion fault when window functions are used.
636 # Root cause is the query flattener invoking sqlite3ExprDup() on
637 # expressions that contain subqueries with window functions. The
638 # sqlite3ExprDup() routine is not making correctly initializing
639 # Select.pWin field of the subqueries.
642 do_execsql_test 14.0 {
644 SELECT * FROM (SELECT 1 AS c) WHERE c IN (
645 SELECT (row_number() OVER()) FROM (VALUES (0))
649 do_execsql_test 14.1 {
650 CREATE TABLE t1(x); INSERT INTO t1(x) VALUES(12345);
651 CREATE TABLE t2(c); INSERT INTO t2(c) VALUES(1);
652 SELECT y, y+1, y+2 FROM (
654 SELECT (row_number() OVER()) FROM t1
660 # https://www.sqlite.org/src/info/d0866b26f83e9c55
661 # Window function in correlated subquery causes assertion fault
663 do_catchsql_test 15.0 {
664 WITH t(id, parent) AS (
665 SELECT CAST(1 AS INT), CAST(NULL AS INT)
677 SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn
681 SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn
688 } {1 {cannot use window functions in recursive queries}}
689 do_execsql_test 15.1 {
690 DROP TABLE IF EXISTS t1;
691 DROP TABLE IF EXISTS t2;
693 INSERT INTO t1 VALUES('a'), ('b'), ('c');
694 CREATE TABLE t2(a, b);
695 INSERT INTO t2 VALUES('X', 1), ('X', 2), ('Y', 2), ('Y', 3);
698 OVER (PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING
699 AND UNBOUNDED FOLLOWING)
704 do_execsql_test 15.2 {
712 #-------------------------------------------------------------------------
714 do_execsql_test 16.0 {
715 CREATE TABLE t7(a,b);
716 INSERT INTO t7(rowid, a, b) VALUES
722 do_execsql_test 16.1 {
723 SELECT rowid, sum(a) OVER (PARTITION BY b IN (SELECT rowid FROM t7)) FROM t7;
730 do_execsql_test 16.2 {
731 SELECT rowid, sum(a) OVER w1 FROM t7
732 WINDOW w1 AS (PARTITION BY b IN (SELECT rowid FROM t7));
739 #-------------------------------------------------------------------------
740 do_execsql_test 17.0 {
742 INSERT INTO t8 VALUES(1), (2), (3);
745 do_execsql_test 17.1 {
746 SELECT +sum(0) OVER () ORDER BY +sum(0) OVER ();
749 do_execsql_test 17.2 {
750 select +sum(a) OVER () FROM t8 ORDER BY +sum(a) OVER () DESC;
753 do_execsql_test 17.3 {
754 SELECT 10+sum(a) OVER (ORDER BY a)
756 ORDER BY 10+sum(a) OVER (ORDER BY a) DESC;
760 #-------------------------------------------------------------------------
761 # Test error cases from chaining window definitions.
764 do_execsql_test 18.0 {
765 DROP TABLE IF EXISTS t1;
766 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER);
767 INSERT INTO t1 VALUES(1, 'odd', 'one', 1);
768 INSERT INTO t1 VALUES(2, 'even', 'two', 2);
769 INSERT INTO t1 VALUES(3, 'odd', 'three', 3);
770 INSERT INTO t1 VALUES(4, 'even', 'four', 4);
771 INSERT INTO t1 VALUES(5, 'odd', 'five', 5);
772 INSERT INTO t1 VALUES(6, 'even', 'six', 6);
775 foreach {tn sql error} {
777 SELECT c, sum(d) OVER win2 FROM t1
778 WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
779 win2 AS (win1 ORDER BY b)
780 } {cannot override frame specification of window: win1}
783 SELECT c, sum(d) OVER win2 FROM t1
785 win2 AS (win4 ORDER BY b)
786 } {no such window: win4}
789 SELECT c, sum(d) OVER win2 FROM t1
791 win2 AS (win1 PARTITION BY d)
792 } {cannot override PARTITION clause of window: win1}
795 SELECT c, sum(d) OVER win2 FROM t1
796 WINDOW win1 AS (ORDER BY b),
797 win2 AS (win1 ORDER BY d)
798 } {cannot override ORDER BY clause of window: win1}
800 do_catchsql_test 18.1.$tn $sql [list 1 $error]
803 foreach {tn sql error} {
805 SELECT c, sum(d) OVER (win1 ORDER BY b) FROM t1
806 WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
807 } {cannot override frame specification of window: win1}
810 SELECT c, sum(d) OVER (win4 ORDER BY b) FROM t1
812 } {no such window: win4}
815 SELECT c, sum(d) OVER (win1 PARTITION BY d) FROM t1
817 } {cannot override PARTITION clause of window: win1}
820 SELECT c, sum(d) OVER (win1 ORDER BY d) FROM t1
821 WINDOW win1 AS (ORDER BY b)
822 } {cannot override ORDER BY clause of window: win1}
824 do_catchsql_test 18.2.$tn $sql [list 1 $error]
827 do_execsql_test 18.3.1 {
828 SELECT string_agg(c, '.') OVER (PARTITION BY b ORDER BY c)
830 } {four four.six four.six.two five five.one five.one.three}
832 do_execsql_test 18.3.2 {
833 SELECT group_concat(c, '.') OVER (win1 ORDER BY c)
835 WINDOW win1 AS (PARTITION BY b)
836 } {four four.six four.six.two five five.one five.one.three}
838 do_execsql_test 18.3.3 {
839 SELECT string_agg(c, '.') OVER win2
841 WINDOW win1 AS (PARTITION BY b),
842 win2 AS (win1 ORDER BY c)
843 } {four four.six four.six.two five five.one five.one.three}
845 do_execsql_test 18.3.4 {
846 SELECT group_concat(c, '.') OVER (win2)
848 WINDOW win1 AS (PARTITION BY b),
849 win2 AS (win1 ORDER BY c)
850 } {four four.six four.six.two five five.one five.one.three}
852 do_execsql_test 18.3.5 {
853 SELECT string_agg(c, '.') OVER win5
855 WINDOW win1 AS (PARTITION BY b),
859 win5 AS (win4 ORDER BY c)
860 } {four four.six four.six.two five five.one five.one.three}
862 #-------------------------------------------------------------------------
863 # Test RANGE <expr> PRECEDING/FOLLOWING when there are string, blob
864 # and NULL values in the dataset.
867 do_execsql_test 19.0 {
868 CREATE TABLE t1(a, b);
869 INSERT INTO t1 VALUES
870 (1, 1), (2, 2), (3, 3), (4, 4), (5, 5),
871 ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10);
873 do_execsql_test 19.1 {
874 SELECT a, sum(b) OVER (ORDER BY a) FROM t1;
875 } {1 1 2 3 3 6 4 10 5 15 a 21 b 28 c 36 d 45 e 55}
877 do_execsql_test 19.2.1 {
878 SELECT a, sum(b) OVER (
879 ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
881 } {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10}
882 do_execsql_test 19.2.2 {
883 SELECT a, sum(b) OVER (
884 ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
885 ) FROM t1 ORDER BY a ASC;
886 } {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10}
888 do_execsql_test 19.3.1 {
889 SELECT a, sum(b) OVER (
890 ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING
892 } {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10}
893 do_execsql_test 19.3.2 {
894 SELECT a, sum(b) OVER (
895 ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING
896 ) FROM t1 ORDER BY a ASC;
897 } {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10}
901 do_execsql_test 20.0 {
902 CREATE TABLE t1(a, b);
903 INSERT INTO t1 VALUES
904 (NULL, 100), (NULL, 100),
905 (1, 1), (2, 2), (3, 3), (4, 4), (5, 5),
906 ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10);
908 do_execsql_test 20.1 {
909 SELECT a, sum(b) OVER (ORDER BY a) FROM t1;
911 {} 200 {} 200 1 201 2 203 3 206 4 210 5 215
912 a 221 b 228 c 236 d 245 e 255
915 do_execsql_test 20.2.1 {
916 SELECT a, sum(b) OVER (
917 ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
919 } {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10}
920 do_execsql_test 20.2.2 {
921 SELECT a, sum(b) OVER (
922 ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING
923 ) FROM t1 ORDER BY a ASC;
924 } {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10}
926 do_execsql_test 20.3.1 {
927 SELECT a, sum(b) OVER (
928 ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING
930 } {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10}
931 do_execsql_test 20.3.2 {
932 SELECT a, sum(b) OVER (
933 ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING
934 ) FROM t1 ORDER BY a ASC;
935 } {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10}
937 #-------------------------------------------------------------------------
938 do_execsql_test 21.0 {
939 CREATE TABLE keyword_tab(
940 current, exclude, filter, following, groups, no, others, over,
941 partition, preceding, range, ties, unbounded, window
944 do_execsql_test 21.1 {
946 current, exclude, filter, following, groups, no, others, over,
947 partition, preceding, range, ties, unbounded, window
951 #-------------------------------------------------------------------------
952 foreach {tn expr err} {
965 if {$err} {set res {1 {frame starting offset must be a non-negative number}} }
966 do_catchsql_test 22.$tn.1 "
967 WITH a(x, y) AS ( VALUES(1, 2) )
969 ORDER BY y RANGE BETWEEN $expr PRECEDING AND UNBOUNDED FOLLOWING
974 if {$err} {set res {1 {frame ending offset must be a non-negative number}} }
975 do_catchsql_test 22.$tn.2 "
976 WITH a(x, y) AS ( VALUES(1, 2) )
978 ORDER BY y RANGE BETWEEN UNBOUNDED PRECEDING AND $expr FOLLOWING
983 #-------------------------------------------------------------------------
985 do_execsql_test 23.0 {
986 CREATE TABLE t5(a, b, c);
987 CREATE INDEX t5ab ON t5(a, b);
990 proc do_ordercount_test {tn sql nOrderBy} {
991 set plan [execsql "EXPLAIN QUERY PLAN $sql"]
992 uplevel [list do_test $tn [list regexp -all ORDER $plan] $nOrderBy]
995 do_ordercount_test 23.1 {
997 sum(c) OVER (ORDER BY a, b),
998 sum(c) OVER (PARTITION BY a ORDER BY b)
1002 do_ordercount_test 23.2 {
1004 sum(c) OVER (ORDER BY b, a),
1005 sum(c) OVER (PARTITION BY b ORDER BY a)
1009 do_ordercount_test 23.3 {
1011 sum(c) OVER (ORDER BY b, a),
1012 sum(c) OVER (ORDER BY c, b)
1016 do_ordercount_test 23.4 {
1018 sum(c) OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
1019 sum(c) OVER (ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
1020 sum(c) OVER (ORDER BY b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
1024 do_ordercount_test 23.5 {
1026 sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING),
1027 sum(c) OVER (ORDER BY b+1 RANGE UNBOUNDED PRECEDING),
1028 sum(c) OVER (ORDER BY b+1 GROUPS UNBOUNDED PRECEDING)
1032 do_ordercount_test 23.6 {
1034 sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING),
1035 sum(c) OVER (ORDER BY b+2 RANGE UNBOUNDED PRECEDING),
1036 sum(c) OVER (ORDER BY b+3 GROUPS UNBOUNDED PRECEDING)
1040 do_execsql_test 24.1 {
1041 SELECT sum(44) OVER ()
1044 do_execsql_test 24.2 {
1045 SELECT lead(44) OVER ()
1048 #-------------------------------------------------------------------------
1051 do_execsql_test 25.0 {
1052 CREATE TABLE t1 ( t1_id INTEGER PRIMARY KEY );
1053 CREATE TABLE t2 ( t2_id INTEGER PRIMARY KEY );
1054 CREATE TABLE t3 ( t3_id INTEGER PRIMARY KEY );
1056 INSERT INTO t1 VALUES(1), (3), (5);
1057 INSERT INTO t2 VALUES (3), (5);
1058 INSERT INTO t3 VALUES(10), (11), (12);
1061 do_execsql_test 25.1 {
1062 SELECT t1.* FROM t1, t2 WHERE
1063 t1_id=t2_id AND t1_id IN (
1064 SELECT t1_id + row_number() OVER ( ORDER BY t1_id ) FROM t3
1068 do_execsql_test 25.2 {
1069 SELECT t1.* FROM t1, t2 WHERE
1070 t1_id=t2_id AND t1_id IN (
1071 SELECT row_number() OVER ( ORDER BY t1_id ) FROM t3
1075 #-------------------------------------------------------------------------
1077 do_execsql_test 26.0 {
1082 do_execsql_test 26.1 {
1083 SELECT ( SELECT row_number() OVER () FROM ( SELECT c FROM t1 ) ) FROM t2
1086 do_execsql_test 26.2 {
1087 INSERT INTO t1 VALUES(1), (2), (3), (4);
1088 INSERT INTO t2 VALUES(2), (6), (8), (4);
1090 SELECT row_number() OVER () FROM ( SELECT c FROM t1 )
1094 do_execsql_test 26.3 {
1098 INSERT INTO t2 VALUES(1), (2), (3), (4);
1099 INSERT INTO t1 VALUES(1), (1), (2), (3), (3), (3), (3), (4), (4);
1102 SELECT row_number() OVER () FROM ( SELECT 1 FROM t1 WHERE x=c )
1106 #-------------------------------------------------------------------------
1108 do_execsql_test 27.0 {
1110 INSERT INTO t1 VALUES(NULL), (1), (2), (3), (4), (5);
1112 do_execsql_test 27.1 {
1113 SELECT min(x) FROM t1;
1115 do_execsql_test 27.2 {
1116 SELECT min(x) OVER win FROM t1
1117 WINDOW win AS (ORDER BY rowid ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
1120 #-------------------------------------------------------------------------
1123 do_execsql_test 28.1.1 {
1124 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY);
1125 INSERT INTO t1 VALUES (3, 'C', 'cc', 1.0);
1126 INSERT INTO t1 VALUES (13,'M', 'cc', NULL);
1129 do_execsql_test 28.1.2 {
1130 SELECT string_agg(b,'') OVER w1 FROM t1
1131 WINDOW w1 AS (ORDER BY a RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING)
1136 do_execsql_test 28.2.1 {
1137 CREATE TABLE t2(a TEXT, b INTEGER);
1138 INSERT INTO t2 VALUES('A', NULL);
1139 INSERT INTO t2 VALUES('B', NULL);
1142 do_execsql_test 28.2.1 {
1143 DROP TABLE IF EXISTS t1;
1144 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY);
1145 INSERT INTO t1 VALUES
1146 (10,'J', 'cc', NULL),
1147 (11,'K', 'cc', 'xyz'),
1148 (13,'M', 'cc', NULL);
1151 do_execsql_test 28.2.2 {
1152 SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1
1154 (ORDER BY d DESC RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING)
1162 #-------------------------------------------------------------------------
1165 do_execsql_test 29.1 {
1166 DROP TABLE IF EXISTS t1;
1167 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY);
1168 INSERT INTO t1 VALUES
1169 (1, 'A', 'aa', 2.5),
1170 (2, 'B', 'bb', 3.75),
1171 (3, 'C', 'cc', 1.0),
1172 (4, 'D', 'cc', 8.25),
1173 (5, 'E', 'bb', 6.5),
1174 (6, 'F', 'aa', 6.5),
1175 (7, 'G', 'aa', 6.0),
1176 (8, 'H', 'bb', 9.0),
1177 (9, 'I', 'aa', 3.75),
1178 (10,'J', 'cc', NULL),
1179 (11,'K', 'cc', 'xyz'),
1180 (12,'L', 'cc', 'xyZ'),
1181 (13,'M', 'cc', NULL);
1184 do_execsql_test 29.2 {
1185 SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1
1187 (PARTITION BY c ORDER BY d DESC
1188 RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING)
1207 # Check-in [7ef7b23cbb1b9ace] (which was itself a fix for ticket
1208 # https://www.sqlite.org/src/info/1be72aab9) introduced a new problem
1209 # if the LHS of a BETWEEN operator is a WINDOW function. The problem
1210 # was found by (the recently enhanced) dbsqlfuzz.
1212 do_execsql_test 30.0 {
1213 DROP TABLE IF EXISTS t1;
1214 CREATE TABLE t1(a, b, c);
1215 INSERT INTO t1 VALUES('BB','aa',399);
1217 count () OVER win1 NOT BETWEEN 'a' AND 'mmm',
1220 WINDOW win1 AS (ORDER BY a GROUPS BETWEEN 4 PRECEDING AND 1 FOLLOWING
1221 EXCLUDE CURRENT ROW),
1222 win2 AS (PARTITION BY b ORDER BY a),
1223 win3 AS (win2 RANGE BETWEEN 5.2 PRECEDING AND true PRECEDING );
1226 #-------------------------------------------------------------------------
1228 do_execsql_test 31.1 {
1229 CREATE TABLE t1(a, b);
1230 CREATE TABLE t2(c, d);
1231 CREATE TABLE t3(e, f);
1233 INSERT INTO t1 VALUES(1, 1);
1234 INSERT INTO t2 VALUES(1, 1);
1235 INSERT INTO t3 VALUES(1, 1);
1238 do_execsql_test 31.2 {
1239 SELECT d IN (SELECT sum(c) OVER (ORDER BY e+c) FROM t3) FROM (
1244 do_execsql_test 31.3 {
1245 SELECT d IN (SELECT sum(c) OVER (PARTITION BY d ORDER BY e+c) FROM t3) FROM (
1250 do_catchsql_test 31.3 {
1252 SELECT sum(c) OVER ( ROWS BETWEEN d FOLLOWING AND UNBOUNDED FOLLOWING)
1258 } {1 {frame starting offset must be a non-negative integer}}
1260 do_catchsql_test 31.3 {
1262 SELECT sum(c) OVER ( ROWS BETWEEN CURRENT ROW AND c FOLLOWING)
1268 } {1 {frame ending offset must be a non-negative integer}}
1270 # 2019-11-16 chromium issue 1025467
1271 ifcapable altertable {
1274 do_catchsql_test 32.10 {
1275 CREATE VIEW a AS SELECT NULL INTERSECT SELECT NULL ORDER BY s() OVER R;
1276 CREATE TABLE a0 AS SELECT 0;
1277 ALTER TABLE a0 RENAME TO S;
1278 } {1 {error in view a: 1st ORDER BY term does not match any column in the result set}}
1282 do_execsql_test 33.1 {
1283 CREATE TABLE t1(aa, bb);
1284 INSERT INTO t1 VALUES(1, 2);
1285 INSERT INTO t1 VALUES(5, 6);
1287 INSERT INTO t2 VALUES(1);
1289 do_execsql_test 33.2 {
1290 SELECT (SELECT DISTINCT sum(aa) OVER() FROM t1 ORDER BY 1), x FROM t2
1295 do_execsql_test 34.1 {
1296 CREATE TABLE t1(a,b,c);
1298 do_execsql_test 34.2 {
1299 SELECT avg(a) OVER (
1300 ORDER BY (SELECT sum(b) OVER ()
1302 SELECT total(d) OVER (ORDER BY c)
1303 FROM (SELECT 1 AS d) ORDER BY 1
1310 #-------------------------------------------------------------------------
1312 do_catchsql_test 35.0 {
1313 SELECT * WINDOW f AS () ORDER BY name COLLATE nocase;
1314 } {1 {no tables specified}}
1316 do_catchsql_test 35.1 {
1317 VALUES(1) INTERSECT SELECT * WINDOW f AS () ORDER BY x COLLATE nocase;
1318 } {1 {no tables specified}}
1320 do_execsql_test 35.2 {
1322 INSERT INTO t1 VALUES(1), (2), (3);
1324 SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1;
1327 do_execsql_test 35.3 {
1329 SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1;
1332 do_execsql_test 35.4 {
1334 SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1;
1337 # 2019-12-07 gramfuzz find
1339 do_execsql_test 36.10 {
1340 VALUES(count(*)OVER());
1342 do_execsql_test 36.20 {
1343 VALUES(count(*)OVER()),(2);
1345 do_execsql_test 36.30 {
1346 VALUES(2),(count(*)OVER());
1348 do_execsql_test 36.40 {
1349 VALUES(2),(3),(count(*)OVER()),(4),(5);
1352 # 2019-12-17 crash test case found by Yongheng and Rui
1353 # See check-in 1ca0bd982ab1183b
1356 do_execsql_test 37.10 {
1357 CREATE TABLE t0(a UNIQUE, b PRIMARY KEY);
1358 CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1))) FROM t0;
1359 SELECT c FROM v0 WHERE c BETWEEN 10 AND 20;
1361 do_execsql_test 37.20 {
1363 CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1234))) FROM t0;
1364 SELECT c FROM v0 WHERE c BETWEEN -10 AND 20;
1367 # 2019-12-20 mrigger reported problem with a FILTER clause on an aggregate
1371 do_catchsql_test 38.10 {
1372 CREATE TABLE t0(c0);
1373 CREATE TABLE t1(c0, c1 UNIQUE);
1374 INSERT INTO t0(c0) VALUES(1);
1375 INSERT INTO t1(c0,c1) VALUES(2,3);
1376 SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(0) FILTER(WHERE t1.c1));
1377 } {1 {misuse of aggregate: AVG()}}
1378 do_execsql_test 38.20 {
1379 SELECT COUNT(*), AVG(1) FILTER(WHERE t1.c1) FROM t0, t1;
1381 do_catchsql_test 38.30 {
1382 SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(1) FILTER(WHERE t1.c1));
1383 } {1 {misuse of aggregate: AVG()}}
1386 do_execsql_test 39.1 {
1387 CREATE TABLE t0(c0 UNIQUE);
1389 do_execsql_test 39.2 {
1390 SELECT FIRST_VALUE(0) OVER();
1392 do_execsql_test 39.3 {
1393 SELECT * FROM t0 WHERE(c0, 0) IN(SELECT FIRST_VALUE(0) OVER(), 0);
1395 do_execsql_test 39.4 {
1396 SELECT * FROM t0 WHERE (t0.c0, 1) IN(SELECT NTILE(1) OVER(), 0 FROM t0);
1400 # 2019-12-25 ticket d87336c81c7d0873
1403 do_catchsql_test 40.1 {
1404 CREATE VIRTUAL TABLE t0 USING rtree(c0, c1, c2);
1406 WHERE ((0,0) IN (SELECT COUNT(*),LAG(5)OVER(PARTITION BY 0) FROM t0),0)<=(c1,0);
1410 #-------------------------------------------------------------------------
1412 do_execsql_test 41.1 {
1413 CREATE TABLE t1(a, b, c);
1414 INSERT INTO t1 VALUES(NULL,'bb',355);
1415 INSERT INTO t1 VALUES('CC','aa',158);
1416 INSERT INTO t1 VALUES('GG','bb',929);
1417 INSERT INTO t1 VALUES('FF','Rb',574);
1420 do_execsql_test 41.2 {
1421 SELECT min(c) OVER (
1422 ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING
1426 do_execsql_test 41.2 {
1427 SELECT min(c) OVER (
1428 ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING
1432 do_execsql_test 41.3 {
1434 min(c) OVER win3 << first_value(c) OVER win3,
1435 min(c) OVER win3 << first_value(c) OVER win3
1438 PARTITION BY 6 ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING
1442 #-------------------------------------------------------------------------
1444 do_execsql_test 42.1 {
1445 CREATE TABLE t1(a, b, c);
1446 INSERT INTO t1 VALUES(1, 1, 1);
1447 INSERT INTO t1 VALUES(2, 2, 2);
1449 do_execsql_test 42.2 {
1450 SELECT * FROM t1 WHERE (0, 0) IN ( SELECT count(*), 0 FROM t1 )
1452 do_execsql_test 42.3 {
1453 SELECT * FROM t1 WHERE (2, 0) IN ( SELECT count(*), 0 FROM t1 )
1456 do_execsql_test 42.3 {
1457 SELECT count(*), max(a) OVER () FROM t1 GROUP BY c;
1460 do_execsql_test 42.4 {
1461 SELECT sum(a), max(b) OVER () FROM t1;
1464 do_execsql_test 42.5 {
1465 CREATE TABLE t2(a, b);
1466 INSERT INTO t2 VALUES('a', 1);
1467 INSERT INTO t2 VALUES('a', 2);
1468 INSERT INTO t2 VALUES('a', 3);
1469 INSERT INTO t2 VALUES('b', 4);
1470 INSERT INTO t2 VALUES('b', 5);
1471 INSERT INTO t2 VALUES('b', 6);
1474 do_execsql_test 42.6 {
1475 SELECT a, sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2 GROUP BY a;
1478 do_execsql_test 42.7 {
1479 SELECT sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2;
1482 #-------------------------------------------------------------------------
1484 do_execsql_test 43.1.1 {
1485 CREATE TABLE t1(x INTEGER PRIMARY KEY);
1486 INSERT INTO t1 VALUES (10);
1488 do_catchsql_test 43.1.2 {
1489 SELECT count() OVER() AS m FROM t1 ORDER BY (SELECT m);
1490 } {1 {misuse of aliased window function m}}
1493 do_execsql_test 43.2.1 {
1494 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
1495 INSERT INTO t1(a, b) VALUES(1, 10); -- 10
1496 INSERT INTO t1(a, b) VALUES(2, 15); -- 25
1497 INSERT INTO t1(a, b) VALUES(3, -5); -- 20
1498 INSERT INTO t1(a, b) VALUES(4, -5); -- 15
1499 INSERT INTO t1(a, b) VALUES(5, 20); -- 35
1500 INSERT INTO t1(a, b) VALUES(6, -11); -- 24
1503 do_execsql_test 43.2.2 {
1504 SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY 2
1506 1 10 4 15 3 20 6 24 2 25 5 35
1509 do_execsql_test 43.2.3 {
1510 SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc
1512 1 10 4 15 3 20 6 24 2 25 5 35
1515 do_execsql_test 43.2.4 {
1516 SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc+5
1518 1 10 4 15 3 20 6 24 2 25 5 35
1521 do_catchsql_test 43.2.5 {
1522 SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc)
1523 } {1 {misuse of aliased window function abc}}
1525 do_catchsql_test 43.2.6 {
1526 SELECT a, 1+sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc)
1527 } {1 {misuse of aliased window function abc}}
1529 #-------------------------------------------------------------------------
1531 do_execsql_test 44.1 {
1532 CREATE TABLE t0(c0);
1535 do_catchsql_test 44.2.1 {
1536 SELECT ntile(0) OVER ();
1537 } {1 {argument of ntile must be a positive integer}}
1538 do_catchsql_test 44.2.2 {
1539 SELECT (0, 0) IN(SELECT MIN(c0), NTILE(0) OVER()) FROM t0;
1540 } {1 {argument of ntile must be a positive integer}}
1542 do_execsql_test 44.3.1 {
1543 SELECT ntile(1) OVER ();
1545 do_execsql_test 44.3.2 {
1546 SELECT (0, 0) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0;
1549 do_execsql_test 44.4.2 {
1550 INSERT INTO t0 VALUES(2), (1), (0);
1551 SELECT (0, 1) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0;
1554 #-------------------------------------------------------------------------
1556 do_execsql_test 45.1 {
1559 INSERT INTO t1 VALUES(1000);
1560 INSERT INTO t1 VALUES(1000);
1561 INSERT INTO t0 VALUES(10000);
1563 do_execsql_test 45.2 {
1565 SELECT sum (a) OVER() FROM t1 UNION ALL SELECT x FROM t0
1569 #-------------------------------------------------------------------------
1571 do_execsql_test 46.1 {
1572 CREATE TABLE t1 (a);
1573 CREATE INDEX i1 ON t1(a);
1575 INSERT INTO t1 VALUES (10);
1578 do_execsql_test 46.2 {
1579 SELECT (SELECT sum(a) OVER(ORDER BY a)) FROM t1
1582 do_execsql_test 46.3 {
1583 SELECT * FROM t1 WHERE (SELECT sum(a) OVER(ORDER BY a));
1586 do_execsql_test 46.4 {
1587 SELECT * FROM t1 NATURAL JOIN t1
1589 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)
1592 #-------------------------------------------------------------------------
1594 do_execsql_test 47.0 {
1604 do_execsql_test 47.1 {
1605 CREATE VIEW t2(k) AS
1606 SELECT e FROM t1 WHERE g = 'abc' OR h BETWEEN 10 AND f;
1609 do_catchsql_test 47.2 {
1612 OR (SELECT k FROM t2 WHERE (SELECT sum(a) OVER() FROM t1 GROUP BY 1));
1613 } {1 {misuse of window function sum()}}
1615 #-------------------------------------------------------------------------
1617 do_execsql_test 48.0 {
1619 INSERT INTO t1 VALUES(1);
1620 INSERT INTO t1 VALUES(2);
1621 INSERT INTO t1 VALUES(3);
1622 SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x))
1623 FROM (SELECT (SELECT sum(a) FROM t1) AS x FROM t1);
1626 do_execsql_test 48.1 {
1627 SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x))
1628 FROM (SELECT (SELECT sum(a) FROM t1 GROUP BY a) AS x FROM t1);
1631 #-------------------------------------------------------------------------
1633 do_execsql_test 49.1 {
1634 CREATE TABLE t1 (a PRIMARY KEY);
1635 INSERT INTO t1 VALUES(1);
1638 do_execsql_test 49.2 {
1639 SELECT b AS c FROM (
1640 SELECT a AS b FROM (
1641 SELECT a FROM t1 WHERE a=1 OR (SELECT sum(a) OVER ())
1649 #-------------------------------------------------------------------------
1651 do_execsql_test 50.0 {
1652 CREATE TABLE t1 (a DOUBLE PRIMARY KEY);
1653 INSERT INTO t1 VALUES(10.0);
1656 do_execsql_test 50.1 {
1657 SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2))
1660 do_execsql_test 50.2 {
1662 SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2))
1664 WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 )
1667 do_execsql_test 50.3 {
1670 SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2))
1672 WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 )
1677 do_execsql_test 50.4 {
1680 SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2))
1682 WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 )
1684 WHERE a=1 OR ((SELECT sum(a) OVER(ORDER BY a%8)) AND 10<=a)
1687 do_execsql_test 50.5 {
1688 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 (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);
1691 # 2020-04-03 ticket af4556bb5c285c08
1694 do_catchsql_test 51.1 {
1695 CREATE TABLE a(b, c);
1696 SELECT c FROM a GROUP BY c
1697 HAVING(SELECT(sum(b) OVER(ORDER BY b),
1698 sum(b) OVER(PARTITION BY min(DISTINCT c), c ORDER BY b)));
1699 } {1 {row value misused}}
1701 #-------------------------------------------------------------------------
1703 do_execsql_test 52.1 {
1704 CREATE TABLE t1(a, b, c);
1705 INSERT INTO t1 VALUES('AA','bb',356);
1706 INSERT INTO t1 VALUES('CC','aa',158);
1707 INSERT INTO t1 VALUES('BB','aa',399);
1708 INSERT INTO t1 VALUES('FF','bb',938);
1711 do_execsql_test 52.2 {
1715 first_value(c) OVER win2,
1716 count(a) OVER (ORDER BY b)
1719 win1 AS (ORDER BY a),
1720 win2 AS (PARTITION BY 6 ORDER BY a
1721 RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING );
1729 do_execsql_test 52.3 {
1733 first_value(c) OVER win2,
1734 count(a) OVER (ORDER BY b)
1737 win1 AS (ORDER BY a),
1738 win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a
1739 RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING );
1747 do_execsql_test 52.4 {
1751 first_value(c) OVER win2,
1752 count(a) OVER (ORDER BY b)
1755 win1 AS (ORDER BY a),
1756 win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a
1757 RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING );
1766 # ticket 7a5279a25c57adf1
1769 do_execsql_test 53.0 {
1770 CREATE TABLE a(c UNIQUE);
1771 INSERT INTO a VALUES(4),(0),(9),(-9);
1774 JOIN a AS b ON a.c=4
1775 JOIN a AS e ON a.c=e.c
1776 WHERE a.c=(SELECT (SELECT coalesce(lead(2) OVER(),0) + sum(d.c))
1781 #-------------------------------------------------------------------------
1783 do_execsql_test 54.1 {
1784 CREATE TABLE t1(a VARCHAR(20), b FLOAT);
1785 INSERT INTO t1 VALUES('1',10.0);
1788 do_catchsql_test 54.2 {
1790 SELECT sum(b) OVER() AS c FROM t1
1792 SELECT b AS c FROM t1
1796 do_execsql_test 54.3 {
1797 INSERT INTO t1 VALUES('2',5.0);
1798 INSERT INTO t1 VALUES('3',15.0);
1801 do_catchsql_test 54.4 {
1803 SELECT sum(b) OVER() AS c FROM t1
1805 SELECT b AS c FROM t1
1809 # 2020-06-05 ticket c8d3b9f0a750a529
1811 do_execsql_test 55.1 {
1816 HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
1824 #------------------------------------------------------------------------
1826 do_execsql_test 56.1 {
1827 CREATE TABLE t1(a, b INTEGER);
1828 CREATE TABLE t2(c, d);
1830 do_catchsql_test 56.2 {
1831 SELECT avg(b) FROM t1
1833 SELECT min(c) OVER () FROM t2
1834 ORDER BY nosuchcolumn;
1835 } {1 {1st ORDER BY term does not match any column in the result set}}
1838 do_execsql_test 57.1 {
1839 CREATE TABLE t4(a, b, c, d, e);
1842 do_catchsql_test 57.2 {
1847 SELECT sum(x) OVER() FROM (
1848 SELECT c AS x FROM t4
1851 ORDER BY (SELECT e FROM t4)
1854 } {1 {1st ORDER BY term does not match any column in the result set}}
1856 # 2020-06-06 various dbsqlfuzz finds and
1857 # ticket 0899cf62f597d7e7
1860 do_execsql_test 57.1 {
1861 CREATE TABLE t1(a, b, c);
1862 INSERT INTO t1 VALUES(NULL,NULL,NULL);
1866 count(c) OVER (ORDER BY b)
1869 do_execsql_test 57.2 {
1870 CREATE TABLE v0 ( v1 INTEGER PRIMARY KEY ) ;
1871 INSERT INTO v0 VALUES ( 10 ) ;
1872 SELECT DISTINCT v1, lead(v1) OVER() FROM v0 GROUP BY v1 ORDER BY 2;
1874 do_catchsql_test 57.3 {
1877 INSERT INTO t1(a) VALUES(22);
1879 INSERT INTO t3(y) VALUES(5),(11),(-9);
1881 SELECT max(y) OVER( ORDER BY (SELECT x FROM (SELECT sum(y) AS x FROM t1)))
1886 # 2020-06-06 ticket 1f6f353b684fc708
1888 do_execsql_test 58.1 {
1889 CREATE TABLE a(a, b, c);
1890 INSERT INTO a VALUES(1, 2, 3);
1891 INSERT INTO a VALUES(4, 5, 6);
1892 SELECT sum(345+b) OVER (ORDER BY b),
1893 sum(avg(678)) OVER (ORDER BY c) FROM a;
1896 # 2020-06-06 ticket e5504e987e419fb0
1897 do_catchsql_test 59.1 {
1898 DROP TABLE IF EXISTS t1;
1899 CREATE TABLE t1(x INTEGER PRIMARY KEY);
1900 INSERT INTO t1 VALUES (123);
1902 ntile( (SELECT sum(x)) ) OVER(ORDER BY x),
1903 min(x) OVER(ORDER BY x)
1905 } {1 {misuse of aggregate: sum()}}
1907 # 2020-06-07 ticket f7d890858f361402
1908 do_execsql_test 60.1 {
1909 DROP TABLE IF EXISTS t1;
1910 CREATE TABLE t1 (x INTEGER PRIMARY KEY);
1911 INSERT INTO t1 VALUES (99);
1912 SELECT EXISTS(SELECT count(*) OVER() FROM t1 ORDER BY sum(x) OVER());
1915 # 2020-06-07 test case generated by dbsqlfuzz showing how an AggInfo
1916 # object might be referenced after the sqlite3Select() call that created
1917 # it returns. This proves the need to persist all AggInfo objects until
1918 # the Parse object is destroyed.
1921 do_catchsql_test 61.1 {
1923 INSERT INTO t1 VALUES(5),(NULL),('seventeen');
1924 SELECT (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);
1928 if {$tn==2} { optimization_control db query-flattener 0 }
1929 do_catchsql_test 61.2.$tn {
1931 (SELECT max(x)OVER(ORDER BY x) / min(x) OVER() )
1933 SELECT (SELECT sum(a) FROM t1 ) AS x FROM t1
1940 optimization_control db all 0
1941 do_execsql_test 61.3.0 {
1946 do_execsql_test 61.3.1 {
1948 SELECT count(a) OVER ( ORDER BY (SELECT sum(y) FROM t2) )
1953 do_execsql_test 61.4.2 {
1955 SELECT count(a) OVER ( ORDER BY sum(a) )
1961 do_catchsql_test 61.4.3 {
1963 sum(a) OVER ( ORDER BY a )
1965 ORDER BY (SELECT sum(a) FROM t2)
1966 } {1 {misuse of aggregate: sum()}}
1967 do_execsql_test 61.4.4 {
1969 sum(a) OVER ( ORDER BY a )
1971 ORDER BY (SELECT sum(y) FROM t2)
1975 #-------------------------------------------------------------------------
1977 do_execsql_test 62.1 {
1978 CREATE TABLE t1(a VARCHAR(20), b FLOAT);
1979 INSERT INTO t1 VALUES('1',10.0);
1982 do_execsql_test 62.2 {
1984 SELECT sum(b) OVER() AS c FROM t1
1986 SELECT b AS c FROM t1
1990 do_execsql_test 62.3 {
1991 INSERT INTO t1 VALUES('2',5.0);
1992 INSERT INTO t1 VALUES('3',15.0);
1995 do_execsql_test 62.4 {
1997 SELECT sum(b) OVER() AS c FROM t1
1999 SELECT b AS c FROM t1
2003 #-------------------------------------------------------------------------
2005 do_execsql_test 63.1 {
2006 CREATE TABLE t1(b, x);
2007 CREATE TABLE t2(c, d);
2008 CREATE TABLE t3(e, f);
2011 do_execsql_test 63.2 {
2014 (SELECT c FROM t2 UNION SELECT x ORDER BY c)
2019 do_execsql_test 63.3 {
2024 (SELECT x AS c UNION SELECT 1234 ORDER BY c)
2033 #-------------------------------------------------------------------------
2035 do_execsql_test 64.1 {
2036 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
2037 INSERT INTO t1 VALUES(1, 'abcd');
2038 INSERT INTO t1 VALUES(2, 'BCDE');
2039 INSERT INTO t1 VALUES(3, 'cdef');
2040 INSERT INTO t1 VALUES(4, 'DEFG');
2043 do_execsql_test 64.2 {
2044 SELECT rowid, max(b COLLATE nocase)||''
2047 ORDER BY max(b COLLATE nocase)||'';
2048 } {1 abcd 2 BCDE 3 cdef 4 DEFG}
2050 do_execsql_test 64.3 {
2051 SELECT count() OVER (), rowid, max(b COLLATE nocase)||''
2054 ORDER BY max(b COLLATE nocase)||'';
2055 } {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG}
2057 do_execsql_test 64.4 {
2058 SELECT count() OVER (), rowid, max(b COLLATE nocase)
2061 ORDER BY max(b COLLATE nocase);
2062 } {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG}
2064 #-------------------------------------------------------------------------
2066 do_execsql_test 65.1 {
2067 CREATE TABLE t1(c1);
2068 INSERT INTO t1 VALUES('abcd');
2070 do_execsql_test 65.2 {
2071 SELECT max(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1;
2074 do_execsql_test 65.3 {
2077 group_concat(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1;
2080 do_execsql_test 65.4 {
2081 SELECT COUNT() OVER () LIKE lead(102030) OVER(
2082 ORDER BY sum('abcdef' COLLATE nocase) IN (SELECT 54321)
2087 #-------------------------------------------------------------------------
2090 do_execsql_test 66.1 {
2091 CREATE TABLE t1(a INTEGER);
2092 INSERT INTO t1 VALUES(3578824042033200656);
2093 INSERT INTO t1 VALUES(3029012920382354029);
2097 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING"
2098 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING"
2099 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING"
2100 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING"
2101 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING"
2102 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING"
2104 do_execsql_test 66.2.$tn "
2105 SELECT total(a) OVER ( $spec ) FROM t1 ORDER BY a
2107 3.02901292038235e+18 3.5788240420332e+18
2112 do_execsql_test 66.3 {
2113 CREATE TABLE t2(a INTEGER);
2114 INSERT INTO t2 VALUES(45);
2115 INSERT INTO t2 VALUES(30);
2118 foreach {tn spec res} {
2119 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0}
2120 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING" {0.0 0.0}
2121 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING" {0.0 0.0}
2122 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0}
2123 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0}
2124 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING" {0.0 0.0}
2126 do_execsql_test 66.2.$tn "
2127 SELECT total(a) OVER ( $spec ) FROM t2 ORDER BY a
2132 #-------------------------------------------------------------------------
2134 do_execsql_test 67.0 {
2135 CREATE TABLE t1(a, b, c);
2136 CREATE TABLE t2(a, b, c);
2139 do_catchsql_test 67.1 {
2140 SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY (
2141 SELECT nth_value(a,2) OVER w1
2142 WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM v1)) )
2144 } {1 {no such table: v1}}
2146 do_catchsql_test 67.2 {
2147 SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY (
2148 SELECT nth_value(a,2) OVER w1
2149 WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM t2)) )
2151 } {1 {1st ORDER BY term does not match any column in the result set}}
2154 # Do not allow aggregate functions in the ORDER BY clause even if
2155 # there are window functions in the result set.
2156 # Forum: /forumpost/540fdfef77
2159 do_catchsql_test 68.0 {
2160 CREATE TABLE t1(a,b);
2161 INSERT INTO t1(a,b) VALUES(0,0),(1,1),(2,4),(3,9),(4,99);
2162 SELECT rowid, a, b, sum(a)OVER() FROM t1 ORDER BY count(b);
2163 } {1 {misuse of aggregate: count()}}
2166 # Forum https://sqlite.org/forum/forumpost/7e484e225c
2169 do_catchsql_test 69.0 {
2170 CREATE TABLE t1(a,b);
2171 CREATE INDEX t1ba ON t1(b,a);
2172 SELECT * FROM t1 WHERE b = (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a));
2173 } {1 {misuse of aggregate: sum()}}
2174 do_catchsql_test 69.1 {
2175 SELECT * FROM t1 WHERE b >= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a));
2176 } {1 {misuse of aggregate: sum()}}
2177 do_catchsql_test 69.2 {
2178 SELECT * FROM t1 WHERE b <= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a));
2179 } {1 {misuse of aggregate: sum()}}
2182 # Forum https://sqlite.org/forum/forumpost/31e0432608
2185 do_execsql_test 70.0 {
2188 do_execsql_test 70.1 {
2189 SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY 2)) AS ca0 FROM t1 ORDER BY ca0;
2191 do_execsql_test 70.2 {
2192 SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY likely(2))) AS ca0 FROM t1 ORDER BY ca0;
2196 # Bug report from Wang Ke
2197 # https://sqlite.org/forum/forumpost/9ba4f60ff8
2199 do_catchsql_test 71.0 {
2201 SELECT a FROM t0, (SELECT a AS b FROM t0)
2202 WHERE (a,1)=(SELECT 2,2 UNION SELECT sum(b),max(b) OVER(ORDER BY b) ORDER BY 2)
2207 do_execsql_test 72.1 {
2208 CREATE TABLE dual(dummy); INSERT INTO dual VALUES('X');
2209 CREATE VIEW v1(x,y) AS SELECT RANK() OVER (PARTITION BY 0), SUM(0) FROM dual;
2210 SELECT * FROM v1 WHERE true;
2213 #-------------------------------------------------------------------------
2216 do_execsql_test 72.0 {
2217 CREATE TABLE t0(c0);
2218 INSERT INTO t0(c0) VALUES (0);
2219 CREATE VIEW v0(c0) AS SELECT TOTAL(0) OVER (PARTITION BY t0.c0) FROM t0;
2221 do_execsql_test 72.1 {
2222 SELECT COUNT(*) FROM (
2223 SELECT TOTAL(0) OVER (PARTITION BY t0.c0) FROM t0
2225 WHERE ('1' IS NOT ('abcde' NOTNULL));
2228 # 2023-03-28 https://sqlite.org/forum/forumpost/dc3b92cfa0 (Song Liu)
2231 do_execsql_test 73.0 {
2232 CREATE TABLE t1(a INT);
2233 INSERT INTO t1(a) VALUES(1),(2),(4);
2234 CREATE VIEW t2(b,c) AS SELECT * FROM t1 JOIN t1 A ORDER BY sum(0) OVER(PARTITION BY 0);
2235 CREATE TRIGGER x1 INSTEAD OF UPDATE ON t2 BEGIN SELECT true; END;
2237 do_execsql_test 73.1 {
2239 } {1 1 1 2 1 4 2 1 2 2 2 4 4 1 4 2 4 4}
2240 do_execsql_test 73.2 {
2241 UPDATE t2 SET c=99 WHERE b=4 RETURNING *;
2243 do_execsql_test 73.3 {
2244 SELECT *, nth_value(15,2) OVER() FROM t2, t1 WHERE b=4;
2256 do_execsql_test 73.4 {
2257 UPDATE t2 SET c=nth_value(15,2) OVER() FROM (SELECT * FROM t1) WHERE b=4 RETURNING *;
2269 do_execsql_test 73.5 {
2272 do_catchsql_test 73.6 {
2273 UPDATE t2 SET c=99 WHERE b=4 RETURNING *;
2274 } {1 {cannot modify t2 because it is a view}}
2275 do_catchsql_test 73.7 {
2276 UPDATE t2 SET c=nth_value(15,2) OVER() FROM (SELECT * FROM t1) WHERE b=4 RETURNING *;
2277 } {1 {cannot modify t2 because it is a view}}
2279 # 2023-03-28 https://sqlite.org/forum/forumpost/bad532820c
2282 do_execsql_test 74.0 {
2283 CREATE TABLE t1 (a INT, b INT);
2284 CREATE TABLE t2 (c INT, d INT);
2285 CREATE INDEX idx ON t1(abs(a));
2286 INSERT INTO t1 VALUES(1,2),(3,4);
2287 INSERT INTO t2 VALUES(5,6),(7,8);
2289 do_execsql_test 74.1 {
2291 SELECT count( a ) FROM t2 LIMIT 1
2294 } {2} ;# Verified using PG 14.2
2295 do_execsql_test 74.2 {
2297 SELECT count( a+c ) FROM t2 LIMIT 1
2300 } {2 2} ;# verified on PG 14.2. Crashes PG 9.6!
2301 do_execsql_test 74.3 {
2303 SELECT count( ( SELECT(sum(0) OVER(ORDER BY c, abs(a))) ) )
2304 FROM t2 GROUP BY c LIMIT 1
2307 } {1 1} ;# verified on PG 14.2
2308 do_execsql_test 74.4 {
2309 /* Original test case reported in https://sqlite.org/forum/forumpost/bad532820c
2310 CREATE TABLE v0 (c1);
2311 CREATE INDEX i ON v0 (c1, c1=1);
2312 SELECT 0 FROM v0 AS a1
2313 WHERE (SELECT count((SELECT(sum(0) OVER(PARTITION BY(c1), (a1.c1=1) ))))
2319 # 2023-04-11 https://sqlite.org/forum/forumpost/6c5678e3da
2320 # An ALWAYS() turns out to be sometimes false.
2322 do_execsql_test 75.0 {
2324 CREATE TABLE t1(a INT, b INT);
2325 CREATE INDEX t1x ON t1(a+b);
2327 do_catchsql_test 75.1 {
2328 SELECT count((SELECT count(a0.a+a0.b) ORDER BY sum(0) OVER (PARTITION BY 0)))
2329 FROM t1 AS a0 JOIN t1 AS a1
2331 } {1 {misuse of aggregate: count()}}
2333 # 2023-04-13 https://sqlite.org/forum/forumpost/0d48347967
2335 do_execsql_test 76.0 {
2336 CREATE TABLE t1(a INT, b INT);
2337 INSERT INTO t1(a,b) VALUES (111,222),(111,223),(118,229);
2338 CREATE INDEX t1a ON t1(a);
2339 CREATE TABLE t2(x INT);
2340 INSERT INTO t2 VALUES (333),(444),(555);
2342 do_execsql_test 76.1 {
2343 SELECT c, (SELECT c + sum(1) OVER ()) AS "res"
2344 FROM t2 LEFT JOIN (SELECT +a AS c FROM t1) AS v1 ON true
2348 # ^^^^^^^^^^^^^^^^^-- results verified against PG 14.2
2350 do_execsql_test 76.2 {
2353 INSERT INTO t3 VALUES(100), (200), (400);
2354 INSERT INTO t4 VALUES(100), (300), (400);
2356 do_execsql_test 76.3 {
2357 SELECT (SELECT y+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y;
2359 do_execsql_test 76.4 {
2360 SELECT (SELECT y+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y GROUP BY x;
2362 do_execsql_test 76.5 {
2363 SELECT (SELECT max(y)+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y GROUP BY x;
2366 # 2023-05-23 https://sqlite.org/forum/forumpost/fbfe330a20
2369 do_execsql_test 77.1 {
2370 CREATE TABLE t1(x INT);
2371 CREATE INDEX t1x ON t1(likely(x));
2372 INSERT INTO t1 VALUES(1),(2),(4),(8);
2374 do_execsql_test 77.2 {
2375 SELECT max(~likely(x)) FILTER (WHERE true) FROM t1 INDEXED BY t1x GROUP BY x;