blob: 47af39606e5c9d3d46628a95a11ab03425856eeb [file] [log] [blame]
drh398f8722015-08-19 13:54:20 +00001# 2015-08-19
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# This file implements tests for table-valued-functions implemented using
13# eponymous virtual tables.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix tabfunc01
19
20ifcapable !vtab {
21 finish_test
22 return
23}
24load_static_extension db series
25
26do_execsql_test tabfunc01-1.1 {
27 SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2;
28} {1 | 3 | 5 | 7 | 9 |}
29do_execsql_test tabfunc01-1.2 {
30 SELECT *, '|' FROM generate_series LIMIT 5;
31} {0 | 1 | 2 | 3 | 4 |}
32do_catchsql_test tabfunc01-1.3 {
33 CREATE VIRTUAL TABLE t1 USING generate_series;
drh8a48b9c2015-08-19 15:20:00 +000034} {1 {no such module: generate_series}}
drh01d230c2015-08-19 17:11:37 +000035do_execsql_test tabfunc01-1.4 {
36 SELECT * FROM generate_series(1,9,2);
37} {1 3 5 7 9}
38do_execsql_test tabfunc01-1.5 {
39 SELECT * FROM generate_series(1,9);
40} {1 2 3 4 5 6 7 8 9}
41do_execsql_test tabfunc01-1.6 {
42 SELECT * FROM generate_series(1,10) WHERE step=3;
43} {1 4 7 10}
44do_catchsql_test tabfunc01-1.7 {
45 SELECT * FROM generate_series(1,9,2,11);
drhd8b1bfc2015-08-20 23:21:34 +000046} {1 {too many arguments on generate_series() - max 3}}
drh01d230c2015-08-19 17:11:37 +000047
drhbc550df2015-08-19 18:19:49 +000048do_execsql_test tabfunc01-1.8 {
49 SELECT * FROM generate_series(0,32,5) ORDER BY rowid DESC;
50} {30 25 20 15 10 5 0}
drh509c3fc2015-08-19 19:01:28 +000051do_execsql_test tabfunc01-1.9 {
52 SELECT rowid, * FROM generate_series(0,32,5) ORDER BY value DESC;
53} {1 30 2 25 3 20 4 15 5 10 6 5 7 0}
54do_execsql_test tabfunc01-1.10 {
55 SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC;
56} {7 30 6 25 5 20 4 15 3 10 2 5 1 0}
drhbc550df2015-08-19 18:19:49 +000057
drh62302122015-09-19 20:27:08 +000058do_execsql_test tabfunc01-1.20 {
59 CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4);
60 SELECT * FROM v1;
61} {1 2 3 4}
62do_catchsql_test tabfunc01-1.21 {
63 SELECT * FROM v1(55);
64} {1 {'v1' is not a function}}
65do_execsql_test tabfunc01-1.22 {
66 CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5);
67 SELECT * FROM v2;
68} {1 2 3 4 5}
69do_catchsql_test tabfunc01-1.23 {
70 SELECT * FROM v2(55);
71} {1 {'v2' is not a function}}
72
drh01d230c2015-08-19 17:11:37 +000073do_execsql_test tabfunc01-2.1 {
74 CREATE TABLE t1(x);
75 INSERT INTO t1(x) VALUES(2),(3);
76 SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2
drh01d230c2015-08-19 17:11:37 +000077} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
drhd10dbad2015-08-20 20:13:37 +000078do_execsql_test tabfunc01-2.2 {
drhd12b6362015-10-11 19:46:59 +000079 SELECT *, '|' FROM (SELECT x FROM t1) AS y, generate_series(1,y.x)
80 ORDER BY 1, 2;
81} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
82
83do_execsql_test tabfunc01-2.50 {
drhd10dbad2015-08-20 20:13:37 +000084 SELECT * FROM generate_series() LIMIT 5;
85} {0 1 2 3 4}
86
drh1f2fc282015-08-21 17:14:48 +000087do_execsql_test tabfunc01-3.1 {
88 SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1;
89} {1 2 3}
90
drhb4d472f2015-09-08 20:26:09 +000091# Eponymous virtual table exists in the "main" schema only
92#
93do_execsql_test tabfunc01-4.1 {
94 SELECT * FROM main.generate_series(1,4)
95} {1 2 3 4}
96do_catchsql_test tabfunc01-4.2 {
97 SELECT * FROM temp.generate_series(1,4)
98} {1 {no such table: temp.generate_series}}
99do_catchsql_test tabfunc01-4.3 {
100 ATTACH ':memory:' AS aux1;
101 CREATE TABLE aux1.t1(a,b,c);
102 SELECT * FROM aux1.generate_series(1,4)
103} {1 {no such table: aux1.generate_series}}
drhd10dbad2015-08-20 20:13:37 +0000104
drh398f8722015-08-19 13:54:20 +0000105finish_test