blob: dcaafa420c2526d59a94f10b246cb0964cb90f03 [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
drh2e3f87a2016-07-03 02:35:47 +000025load_static_extension db carray
drh6bada272016-08-09 21:08:42 +000026load_static_extension db remember
drh398f8722015-08-19 13:54:20 +000027
28do_execsql_test tabfunc01-1.1 {
29 SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2;
30} {1 | 3 | 5 | 7 | 9 |}
31do_execsql_test tabfunc01-1.2 {
32 SELECT *, '|' FROM generate_series LIMIT 5;
33} {0 | 1 | 2 | 3 | 4 |}
34do_catchsql_test tabfunc01-1.3 {
35 CREATE VIRTUAL TABLE t1 USING generate_series;
drh8a48b9c2015-08-19 15:20:00 +000036} {1 {no such module: generate_series}}
drh01d230c2015-08-19 17:11:37 +000037do_execsql_test tabfunc01-1.4 {
38 SELECT * FROM generate_series(1,9,2);
39} {1 3 5 7 9}
40do_execsql_test tabfunc01-1.5 {
41 SELECT * FROM generate_series(1,9);
42} {1 2 3 4 5 6 7 8 9}
43do_execsql_test tabfunc01-1.6 {
44 SELECT * FROM generate_series(1,10) WHERE step=3;
45} {1 4 7 10}
46do_catchsql_test tabfunc01-1.7 {
47 SELECT * FROM generate_series(1,9,2,11);
drhd8b1bfc2015-08-20 23:21:34 +000048} {1 {too many arguments on generate_series() - max 3}}
drh01d230c2015-08-19 17:11:37 +000049
drhbc550df2015-08-19 18:19:49 +000050do_execsql_test tabfunc01-1.8 {
51 SELECT * FROM generate_series(0,32,5) ORDER BY rowid DESC;
52} {30 25 20 15 10 5 0}
drh509c3fc2015-08-19 19:01:28 +000053do_execsql_test tabfunc01-1.9 {
54 SELECT rowid, * FROM generate_series(0,32,5) ORDER BY value DESC;
55} {1 30 2 25 3 20 4 15 5 10 6 5 7 0}
56do_execsql_test tabfunc01-1.10 {
57 SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC;
58} {7 30 6 25 5 20 4 15 3 10 2 5 1 0}
drhbc550df2015-08-19 18:19:49 +000059
drh62302122015-09-19 20:27:08 +000060do_execsql_test tabfunc01-1.20 {
61 CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4);
62 SELECT * FROM v1;
63} {1 2 3 4}
drh20292312015-11-21 13:24:46 +000064do_catchsql_test tabfunc01-1.21.1 {
drh62302122015-09-19 20:27:08 +000065 SELECT * FROM v1(55);
66} {1 {'v1' is not a function}}
drh20292312015-11-21 13:24:46 +000067do_catchsql_test tabfunc01-1.21.2 {
68 SELECT * FROM v1();
69} {1 {'v1' is not a function}}
drh62302122015-09-19 20:27:08 +000070do_execsql_test tabfunc01-1.22 {
71 CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5);
72 SELECT * FROM v2;
73} {1 2 3 4 5}
drh20292312015-11-21 13:24:46 +000074do_catchsql_test tabfunc01-1.23.1 {
drh62302122015-09-19 20:27:08 +000075 SELECT * FROM v2(55);
76} {1 {'v2' is not a function}}
drh20292312015-11-21 13:24:46 +000077do_catchsql_test tabfunc01-1.23.2 {
78 SELECT * FROM v2();
79} {1 {'v2' is not a function}}
80do_execsql_test tabfunc01-1.24 {
81 CREATE TABLE t0(x);
82 INSERT INTO t0(x) VALUES(123),(456),(789);
83 SELECT * FROM t0 ORDER BY x;
84} {123 456 789}
85do_catchsql_test tabfunc01-1.25 {
86 SELECT * FROM t0(55) ORDER BY x;
87} {1 {'t0' is not a function}}
88do_catchsql_test tabfunc01-1.26 {
89 WITH w0 AS (SELECT * FROM t0)
90 INSERT INTO t0(x) SELECT * FROM w0()
91} {1 {'w0' is not a function}}
drh62302122015-09-19 20:27:08 +000092
drh01d230c2015-08-19 17:11:37 +000093do_execsql_test tabfunc01-2.1 {
94 CREATE TABLE t1(x);
95 INSERT INTO t1(x) VALUES(2),(3);
96 SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2
drh01d230c2015-08-19 17:11:37 +000097} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
drhd10dbad2015-08-20 20:13:37 +000098do_execsql_test tabfunc01-2.2 {
drhd12b6362015-10-11 19:46:59 +000099 SELECT *, '|' FROM (SELECT x FROM t1) AS y, generate_series(1,y.x)
100 ORDER BY 1, 2;
101} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
102
103do_execsql_test tabfunc01-2.50 {
drhd10dbad2015-08-20 20:13:37 +0000104 SELECT * FROM generate_series() LIMIT 5;
105} {0 1 2 3 4}
106
drh1f2fc282015-08-21 17:14:48 +0000107do_execsql_test tabfunc01-3.1 {
108 SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1;
109} {1 2 3}
110
drhb4d472f2015-09-08 20:26:09 +0000111# Eponymous virtual table exists in the "main" schema only
112#
113do_execsql_test tabfunc01-4.1 {
114 SELECT * FROM main.generate_series(1,4)
115} {1 2 3 4}
116do_catchsql_test tabfunc01-4.2 {
117 SELECT * FROM temp.generate_series(1,4)
118} {1 {no such table: temp.generate_series}}
119do_catchsql_test tabfunc01-4.3 {
120 ATTACH ':memory:' AS aux1;
121 CREATE TABLE aux1.t1(a,b,c);
122 SELECT * FROM aux1.generate_series(1,4)
123} {1 {no such table: aux1.generate_series}}
drhd10dbad2015-08-20 20:13:37 +0000124
drhdbc49162016-03-02 03:28:07 +0000125# The next series of tests is verifying that virtual table are able
126# to optimize the IN operator, even on terms that are not marked "omit".
127# When the generate_series virtual table is compiled for the testfixture,
128# the special -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 option is used, which
129# causes the xBestIndex method of generate_series to leave the
130# sqlite3_index_constraint_usage.omit flag set to 0, which should cause
131# the SQLite core to verify the start=, stop=, and step= constraints on
132# each step of output. At one point, the IN operator could not be used
133# by virtual tables unless omit was set.
134#
135do_execsql_test tabfunc01-500 {
136 SELECT * FROM generate_series WHERE start IN (1,7) AND stop=20 AND step=10
137 ORDER BY +1;
138} {1 7 11 17}
139
drh5fbab882016-07-02 12:08:14 +0000140# Table-valued functions on the RHS of an IN operator
141#
142do_execsql_test tabfunc01-600 {
143 CREATE TABLE t600(a INTEGER PRIMARY KEY, b TEXT);
144 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
145 INSERT INTO t600(a,b) SELECT x, printf('(%03d)',x) FROM c;
146 SELECT b FROM t600 WHERE a IN generate_series(2,52,10);
147} {(002) (012) (022) (032) (042) (052)}
148
drh48416242016-06-29 05:00:30 +0000149
drh2e3f87a2016-07-03 02:35:47 +0000150do_test tabfunc01-700 {
drh59a40db2017-02-03 15:16:25 +0000151 set PTR1 [intarray_addr 5 7 13 17 23]
drh64131c12016-06-29 05:08:01 +0000152 db eval {
drh59a40db2017-02-03 15:16:25 +0000153 SELECT b FROM t600, carray($PTR1,5) WHERE a=value;
drh64131c12016-06-29 05:08:01 +0000154 }
drh2e3f87a2016-07-03 02:35:47 +0000155} {(005) (007) (013) (017) (023)}
156do_test tabfunc01-701 {
157 db eval {
drh59a40db2017-02-03 15:16:25 +0000158 SELECT b FROM t600 WHERE a IN carray($PTR1,5,'int32');
drh2e3f87a2016-07-03 02:35:47 +0000159 }
160} {(005) (007) (013) (017) (023)}
161do_test tabfunc01-702 {
162 db eval {
drh59a40db2017-02-03 15:16:25 +0000163 SELECT b FROM t600 WHERE a IN carray($PTR1,4,'int32');
drh2e3f87a2016-07-03 02:35:47 +0000164 }
165} {(005) (007) (013) (017)}
166do_catchsql_test tabfunc01-710 {
drh59a40db2017-02-03 15:16:25 +0000167 SELECT b FROM t600 WHERE a IN carray($PTR1,5,'int33');
drh2e3f87a2016-07-03 02:35:47 +0000168} {1 {unknown datatype: 'int33'}}
drh64131c12016-06-29 05:08:01 +0000169
drh2e3f87a2016-07-03 02:35:47 +0000170do_test tabfunc01-720 {
drh59a40db2017-02-03 15:16:25 +0000171 set PTR2 [int64array_addr 5 7 13 17 23]
drh2e3f87a2016-07-03 02:35:47 +0000172 db eval {
drh59a40db2017-02-03 15:16:25 +0000173 SELECT b FROM t600, carray($PTR2,5,'int64') WHERE a=value;
drh2e3f87a2016-07-03 02:35:47 +0000174 }
175} {(005) (007) (013) (017) (023)}
drh6bada272016-08-09 21:08:42 +0000176do_test tabfunc01-721 {
177 db eval {
drh59a40db2017-02-03 15:16:25 +0000178 SELECT remember(123,$PTR2);
179 SELECT value FROM carray($PTR2,5,'int64');
drh6bada272016-08-09 21:08:42 +0000180 }
181} {123 123 7 13 17 23}
182do_test tabfunc01-722 {
drh59a40db2017-02-03 15:16:25 +0000183 set PTR3 [expr {$PTR2+16}]
drh6bada272016-08-09 21:08:42 +0000184 db eval {
drh59a40db2017-02-03 15:16:25 +0000185 SELECT remember(987,$PTR3);
186 SELECT value FROM carray($PTR2,5,'int64');
drh6bada272016-08-09 21:08:42 +0000187 }
188} {987 123 7 987 17 23}
drh2d053312016-06-29 06:19:19 +0000189
drh2e3f87a2016-07-03 02:35:47 +0000190do_test tabfunc01-730 {
drh59a40db2017-02-03 15:16:25 +0000191 set PTR4 [doublearray_addr 5.0 7.0 13.0 17.0 23.0]
drh2e3f87a2016-07-03 02:35:47 +0000192 db eval {
drh59a40db2017-02-03 15:16:25 +0000193 SELECT b FROM t600, carray($PTR4,5,'double') WHERE a=value;
drh2e3f87a2016-07-03 02:35:47 +0000194 }
195} {(005) (007) (013) (017) (023)}
196
197do_test tabfunc01-740 {
drh59a40db2017-02-03 15:16:25 +0000198 set PTR5 [textarray_addr x5 x7 x13 x17 x23]
drh2e3f87a2016-07-03 02:35:47 +0000199 db eval {
drh59a40db2017-02-03 15:16:25 +0000200 SELECT b FROM t600, carray($PTR5,5,'char*') WHERE a=trim(value,'x');
drh2e3f87a2016-07-03 02:35:47 +0000201 }
202} {(005) (007) (013) (017) (023)}
203
drh59a40db2017-02-03 15:16:25 +0000204do_test tabfunc01-750 {
205 db eval {
206 SELECT aa.value, bb.value, '|'
207 FROM carray($PTR4,5,'double') AS aa
208 JOIN carray($PTR5,5,'char*') AS bb ON aa.rowid=bb.rowid;
209 }
210} {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |}
drh2e3f87a2016-07-03 02:35:47 +0000211
drh59a40db2017-02-03 15:16:25 +0000212# Free up memory allocations
drh2e3f87a2016-07-03 02:35:47 +0000213intarray_addr
214int64array_addr
215doublearray_addr
216textarray_addr
drh48416242016-06-29 05:00:30 +0000217
drh398f8722015-08-19 13:54:20 +0000218finish_test