2018-06-29 20:44:52 +03:00
|
|
|
# 2018 May 8
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library. Specifically,
|
|
|
|
# it tests the sqlite3_create_window_function() API.
|
|
|
|
#
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
set testprefix window6
|
|
|
|
|
|
|
|
ifcapable !windowfunc {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set setup {
|
2018-06-30 21:54:56 +03:00
|
|
|
CREATE TABLE %t1(%x, %y %typename);
|
2018-06-29 20:44:52 +03:00
|
|
|
INSERT INTO %t1 VALUES(1, 'a');
|
|
|
|
INSERT INTO %t1 VALUES(2, 'b');
|
|
|
|
INSERT INTO %t1 VALUES(3, 'c');
|
|
|
|
INSERT INTO %t1 VALUES(4, 'd');
|
|
|
|
INSERT INTO %t1 VALUES(5, 'e');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach {tn vars} {
|
|
|
|
1 {}
|
|
|
|
2 { set A(%t1) over }
|
2018-06-30 21:54:56 +03:00
|
|
|
3 { set A(%x) over }
|
2018-06-29 20:44:52 +03:00
|
|
|
4 {
|
|
|
|
set A(%alias) over
|
2018-06-30 21:54:56 +03:00
|
|
|
set A(%x) following
|
|
|
|
set A(%y) over
|
2018-06-29 20:44:52 +03:00
|
|
|
}
|
|
|
|
5 {
|
2018-06-30 21:54:56 +03:00
|
|
|
set A(%t1) over
|
|
|
|
set A(%x) following
|
|
|
|
set A(%y) preceding
|
2018-06-29 20:44:52 +03:00
|
|
|
set A(%w) current
|
2018-06-30 21:54:56 +03:00
|
|
|
set A(%alias) filter
|
|
|
|
set A(%typename) window
|
2018-06-29 20:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
6 {
|
2018-06-30 21:54:56 +03:00
|
|
|
set A(%x) window
|
2018-06-29 20:44:52 +03:00
|
|
|
}
|
|
|
|
} {
|
|
|
|
set A(%t1) t1
|
2018-06-30 21:54:56 +03:00
|
|
|
set A(%x) x
|
|
|
|
set A(%y) y
|
2018-06-29 20:44:52 +03:00
|
|
|
set A(%w) w
|
|
|
|
set A(%alias) alias
|
|
|
|
set A(%typename) integer
|
|
|
|
eval $vars
|
|
|
|
|
|
|
|
set MAP [array get A]
|
|
|
|
set setup_sql [string map $MAP $setup]
|
|
|
|
reset_db
|
|
|
|
execsql $setup_sql
|
|
|
|
|
|
|
|
do_execsql_test 1.$tn.1 [string map $MAP {
|
2018-06-30 21:54:56 +03:00
|
|
|
SELECT group_concat(%x, '.') OVER (ORDER BY %y) FROM %t1
|
2018-06-29 20:44:52 +03:00
|
|
|
}] {1 1.2 1.2.3 1.2.3.4 1.2.3.4.5}
|
|
|
|
|
|
|
|
do_execsql_test 1.$tn.2 [string map $MAP {
|
2018-06-30 21:54:56 +03:00
|
|
|
SELECT sum(%x) OVER %w FROM %t1 WINDOW %w AS (ORDER BY %y)
|
2018-06-29 20:44:52 +03:00
|
|
|
}] {1 3 6 10 15}
|
|
|
|
|
|
|
|
do_execsql_test 1.$tn.3 [string map $MAP {
|
2018-06-30 21:54:56 +03:00
|
|
|
SELECT sum(%alias.%x) OVER %w FROM %t1 %alias WINDOW %w AS (ORDER BY %y)
|
2018-06-29 20:44:52 +03:00
|
|
|
}] {1 3 6 10 15}
|
|
|
|
|
|
|
|
do_execsql_test 1.$tn.4 [string map $MAP {
|
2018-06-30 21:54:56 +03:00
|
|
|
SELECT sum(%x) %alias FROM %t1
|
2018-06-29 20:44:52 +03:00
|
|
|
}] {15}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
proc winproc {args} { return "window: $args" }
|
|
|
|
db func window winproc
|
|
|
|
do_execsql_test 2.0 {
|
|
|
|
SELECT window('hello world');
|
|
|
|
} {{window: {hello world}}}
|
|
|
|
|
|
|
|
proc wincmp {a b} { string compare $b $a }
|
|
|
|
db collate window wincmp
|
|
|
|
do_execsql_test 3.0 {
|
|
|
|
CREATE TABLE window(x COLLATE window);
|
|
|
|
INSERT INTO window VALUES('bob'), ('alice'), ('cate');
|
|
|
|
SELECT * FROM window ORDER BY x COLLATE window;
|
|
|
|
} {cate bob alice}
|
|
|
|
do_execsql_test 3.1 {
|
|
|
|
DROP TABLE window;
|
|
|
|
CREATE TABLE x1(x);
|
|
|
|
INSERT INTO x1 VALUES('bob'), ('alice'), ('cate');
|
|
|
|
CREATE INDEX window ON x1(x COLLATE window);
|
|
|
|
SELECT * FROM x1 ORDER BY x COLLATE window;
|
|
|
|
} {cate bob alice}
|
|
|
|
|
|
|
|
|
|
|
|
do_execsql_test 4.0 { CREATE TABLE t4(x, y); }
|
|
|
|
|
|
|
|
# do_execsql_test 4.1 { PRAGMA parser_trace = 1 }
|
|
|
|
do_execsql_test 4.1 {
|
|
|
|
SELECT * FROM t4 window, t4;
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:54:56 +03:00
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
reset_db
|
|
|
|
|
|
|
|
do_execsql_test 5.0 {
|
|
|
|
CREATE TABLE over(x, over);
|
|
|
|
CREATE TABLE window(x, window);
|
|
|
|
INSERT INTO over VALUES(1, 2), (3, 4), (5, 6);
|
|
|
|
INSERT INTO window VALUES(1, 2), (3, 4), (5, 6);
|
|
|
|
SELECT sum(x) over FROM over
|
|
|
|
} {9}
|
|
|
|
|
|
|
|
do_execsql_test 5.1 {
|
|
|
|
SELECT sum(x) over over FROM over WINDOW over AS ()
|
|
|
|
} {9 9 9}
|
|
|
|
|
|
|
|
do_execsql_test 5.2 {
|
|
|
|
SELECT sum(over) over over over FROM over over WINDOW over AS (ORDER BY over)
|
|
|
|
} {2 6 12}
|
|
|
|
|
|
|
|
do_execsql_test 5.3 {
|
|
|
|
SELECT sum(over) over over over FROM over over WINDOW over AS (ORDER BY over);
|
|
|
|
} {2 6 12}
|
|
|
|
|
|
|
|
do_execsql_test 5.4 {
|
|
|
|
SELECT sum(window) OVER window window FROM window window window window AS (ORDER BY window);
|
|
|
|
} {2 6 12}
|
2018-06-29 20:44:52 +03:00
|
|
|
|
2018-07-02 15:07:32 +03:00
|
|
|
do_execsql_test 5.5 {
|
|
|
|
SELECT count(*) OVER win FROM over
|
|
|
|
WINDOW win AS (ORDER BY x ROWS BETWEEN +2 FOLLOWING AND +3 FOLLOWING)
|
|
|
|
} {1 0 0}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2018-07-02 18:03:50 +03:00
|
|
|
|
2018-07-02 15:07:32 +03:00
|
|
|
do_execsql_test 6.0 {
|
2018-07-02 18:03:50 +03:00
|
|
|
SELECT LIKE('!', '', '!') x WHERE x;
|
|
|
|
} {}
|
|
|
|
|
|
|
|
do_execsql_test 6.1 {
|
2018-07-02 15:07:32 +03:00
|
|
|
SELECT LIKE("!","","!")""WHeRE"";
|
2018-07-02 18:03:50 +03:00
|
|
|
} {}
|
|
|
|
|
|
|
|
do_catchsql_test 6.2 {
|
2018-07-02 15:07:32 +03:00
|
|
|
SELECT LIKE("!","","!")""window"";
|
|
|
|
} {1 {near "window": syntax error}}
|
|
|
|
|
2018-07-02 18:03:50 +03:00
|
|
|
reset_db
|
|
|
|
do_execsql_test 7.0 {
|
|
|
|
CREATE TABLE t1(x TEXT);
|
|
|
|
CREATE INDEX i1 ON t1(x COLLATE nocase);
|
|
|
|
INSERT INTO t1 VALUES('');
|
|
|
|
}
|
|
|
|
|
|
|
|
do_execsql_test 7.1 {
|
|
|
|
SELECT count(*) FROM t1 WHERE x LIKE '!' ESCAPE '!';
|
|
|
|
} {0}
|
|
|
|
|
|
|
|
|
2018-06-29 20:44:52 +03:00
|
|
|
finish_test
|
|
|
|
|