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. Specifically,
12 # it tests the sqlite3_create_window_function() API.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix window5
19 ifcapable !windowfunc {
24 proc m_step {ctx val} {
29 set lSort [lsort $ctx]
31 set nVal [llength $lSort]
34 if {($nVal % 2)==0 && $nVal>0} {
35 set a [lindex $lSort $n]
36 set b [lindex $lSort $n-1]
38 set ret [expr ($a+$b)/2.0]
40 set ret [expr ($a+$b)/2]
43 set ret [lindex $lSort $n]
47 proc m_inverse {ctx val} {
48 set ctx [lrange $ctx 1 end]
55 sqlite3_create_window_function db median m_step m_value m_value m_inverse
56 sqlite3_create_window_function db win m_step w_value w_value m_inverse
59 test_create_window_function_misuse db
63 CREATE TABLE t1(a, b);
64 INSERT INTO t1 VALUES(4, 'a');
65 INSERT INTO t1 VALUES(6, 'b');
66 INSERT INTO t1 VALUES(1, 'c');
67 INSERT INTO t1 VALUES(5, 'd');
68 INSERT INTO t1 VALUES(2, 'e');
69 INSERT INTO t1 VALUES(3, 'f');
73 SELECT win(a) OVER (ORDER BY b), median(a) OVER (ORDER BY b) FROM t1;
74 } {4 4 {4 6} 5 {1 4 6} 4 {1 4 5 6} 4.5 {1 2 4 5 6} 4 {1 2 3 4 5 6} 3.5}
78 SELECT sumint(a) OVER (ORDER BY rowid) FROM t1 ORDER BY rowid;
82 SELECT sumint(a) OVER (ORDER BY rowid ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t1 ORDER BY rowid;
86 do_catchsql_test 3.0 {
88 (ORDER BY b ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
90 } {1 {sum() may not be used as a window function}}
92 SELECT sum(a) FROM t1;