drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 1 | # 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 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | set testprefix tabfunc01 |
| 19 | |
| 20 | ifcapable !vtab { |
| 21 | finish_test |
| 22 | return |
| 23 | } |
| 24 | load_static_extension db series |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 25 | load_static_extension db carray |
drh | 6bada27 | 2016-08-09 21:08:42 +0000 | [diff] [blame] | 26 | load_static_extension db remember |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 27 | |
| 28 | do_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 |} |
drh | bfbf7d9 | 2018-10-01 21:36:38 +0000 | [diff] [blame] | 31 | do_execsql_test tabfunc01-1.1b { |
| 32 | PRAGMA table_xinfo(generate_series); |
| 33 | } {0 value {} 0 {} 0 0 1 start {} 0 {} 0 1 2 stop {} 0 {} 0 1 3 step {} 0 {} 0 1} |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 34 | do_execsql_test tabfunc01-1.2 { |
drh | e46ec73 | 2021-07-16 17:04:17 +0000 | [diff] [blame] | 35 | SELECT *, '|' FROM generate_series(0) LIMIT 5; |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 36 | } {0 | 1 | 2 | 3 | 4 |} |
drh | e46ec73 | 2021-07-16 17:04:17 +0000 | [diff] [blame] | 37 | do_catchsql_test tabfunc01-1.2b { |
| 38 | SELECT *, '|' FROM generate_series LIMIT 5; |
| 39 | } {1 {first argument to "generate_series()" missing or unusable}} |
| 40 | do_catchsql_test tabfunc01-1.2c { |
| 41 | SELECT *, '|' FROM generate_series(value) LIMIT 5; |
| 42 | } {1 {first argument to "generate_series()" missing or unusable}} |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 43 | do_catchsql_test tabfunc01-1.3 { |
| 44 | CREATE VIRTUAL TABLE t1 USING generate_series; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 45 | } {1 {no such module: generate_series}} |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 46 | do_execsql_test tabfunc01-1.4 { |
| 47 | SELECT * FROM generate_series(1,9,2); |
| 48 | } {1 3 5 7 9} |
| 49 | do_execsql_test tabfunc01-1.5 { |
| 50 | SELECT * FROM generate_series(1,9); |
| 51 | } {1 2 3 4 5 6 7 8 9} |
| 52 | do_execsql_test tabfunc01-1.6 { |
| 53 | SELECT * FROM generate_series(1,10) WHERE step=3; |
| 54 | } {1 4 7 10} |
| 55 | do_catchsql_test tabfunc01-1.7 { |
| 56 | SELECT * FROM generate_series(1,9,2,11); |
drh | d8b1bfc | 2015-08-20 23:21:34 +0000 | [diff] [blame] | 57 | } {1 {too many arguments on generate_series() - max 3}} |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 58 | |
drh | bc550df | 2015-08-19 18:19:49 +0000 | [diff] [blame] | 59 | do_execsql_test tabfunc01-1.8 { |
| 60 | SELECT * FROM generate_series(0,32,5) ORDER BY rowid DESC; |
| 61 | } {30 25 20 15 10 5 0} |
drh | 509c3fc | 2015-08-19 19:01:28 +0000 | [diff] [blame] | 62 | do_execsql_test tabfunc01-1.9 { |
| 63 | SELECT rowid, * FROM generate_series(0,32,5) ORDER BY value DESC; |
| 64 | } {1 30 2 25 3 20 4 15 5 10 6 5 7 0} |
| 65 | do_execsql_test tabfunc01-1.10 { |
| 66 | SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC; |
| 67 | } {7 30 6 25 5 20 4 15 3 10 2 5 1 0} |
drh | bc550df | 2015-08-19 18:19:49 +0000 | [diff] [blame] | 68 | |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 69 | do_execsql_test tabfunc01-1.20 { |
| 70 | CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4); |
| 71 | SELECT * FROM v1; |
| 72 | } {1 2 3 4} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 73 | do_catchsql_test tabfunc01-1.21.1 { |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 74 | SELECT * FROM v1(55); |
| 75 | } {1 {'v1' is not a function}} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 76 | do_catchsql_test tabfunc01-1.21.2 { |
| 77 | SELECT * FROM v1(); |
| 78 | } {1 {'v1' is not a function}} |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 79 | do_execsql_test tabfunc01-1.22 { |
| 80 | CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5); |
| 81 | SELECT * FROM v2; |
| 82 | } {1 2 3 4 5} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 83 | do_catchsql_test tabfunc01-1.23.1 { |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 84 | SELECT * FROM v2(55); |
| 85 | } {1 {'v2' is not a function}} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 86 | do_catchsql_test tabfunc01-1.23.2 { |
| 87 | SELECT * FROM v2(); |
| 88 | } {1 {'v2' is not a function}} |
| 89 | do_execsql_test tabfunc01-1.24 { |
| 90 | CREATE TABLE t0(x); |
| 91 | INSERT INTO t0(x) VALUES(123),(456),(789); |
| 92 | SELECT * FROM t0 ORDER BY x; |
| 93 | } {123 456 789} |
| 94 | do_catchsql_test tabfunc01-1.25 { |
| 95 | SELECT * FROM t0(55) ORDER BY x; |
| 96 | } {1 {'t0' is not a function}} |
| 97 | do_catchsql_test tabfunc01-1.26 { |
| 98 | WITH w0 AS (SELECT * FROM t0) |
| 99 | INSERT INTO t0(x) SELECT * FROM w0() |
| 100 | } {1 {'w0' is not a function}} |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 101 | |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 102 | do_execsql_test tabfunc01-2.1 { |
| 103 | CREATE TABLE t1(x); |
| 104 | INSERT INTO t1(x) VALUES(2),(3); |
| 105 | SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2 |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 106 | } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |} |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 107 | do_execsql_test tabfunc01-2.2 { |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 108 | SELECT *, '|' FROM (SELECT x FROM t1) AS y, generate_series(1,y.x) |
| 109 | ORDER BY 1, 2; |
| 110 | } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |} |
| 111 | |
| 112 | do_execsql_test tabfunc01-2.50 { |
drh | e46ec73 | 2021-07-16 17:04:17 +0000 | [diff] [blame] | 113 | SELECT * FROM generate_series(0) LIMIT 5; |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 114 | } {0 1 2 3 4} |
| 115 | |
drh | 1f2fc28 | 2015-08-21 17:14:48 +0000 | [diff] [blame] | 116 | do_execsql_test tabfunc01-3.1 { |
| 117 | SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1; |
| 118 | } {1 2 3} |
| 119 | |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 120 | # Eponymous virtual table exists in all schemas. |
drh | b4d472f | 2015-09-08 20:26:09 +0000 | [diff] [blame] | 121 | # |
| 122 | do_execsql_test tabfunc01-4.1 { |
| 123 | SELECT * FROM main.generate_series(1,4) |
| 124 | } {1 2 3 4} |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 125 | do_execsql_test tabfunc01-4.2 { |
drh | b4d472f | 2015-09-08 20:26:09 +0000 | [diff] [blame] | 126 | SELECT * FROM temp.generate_series(1,4) |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 127 | } {1 2 3 4} |
| 128 | do_execsql_test tabfunc01-4.3 { |
drh | b4d472f | 2015-09-08 20:26:09 +0000 | [diff] [blame] | 129 | ATTACH ':memory:' AS aux1; |
| 130 | CREATE TABLE aux1.t1(a,b,c); |
| 131 | SELECT * FROM aux1.generate_series(1,4) |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 132 | } {1 2 3 4} |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 133 | |
drh | 4a5cff7 | 2018-12-03 01:47:41 +0000 | [diff] [blame] | 134 | # 2018-12-03: Fix bug reported by by private email. |
| 135 | do_execsql_test tabfunc01-4.4 { |
| 136 | SELECT * FROM (generate_series(1,5,2)) AS x LIMIT 10; |
| 137 | } {1 3 5} |
| 138 | |
drh | dbc4916 | 2016-03-02 03:28:07 +0000 | [diff] [blame] | 139 | # The next series of tests is verifying that virtual table are able |
| 140 | # to optimize the IN operator, even on terms that are not marked "omit". |
| 141 | # When the generate_series virtual table is compiled for the testfixture, |
| 142 | # the special -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 option is used, which |
| 143 | # causes the xBestIndex method of generate_series to leave the |
| 144 | # sqlite3_index_constraint_usage.omit flag set to 0, which should cause |
| 145 | # the SQLite core to verify the start=, stop=, and step= constraints on |
| 146 | # each step of output. At one point, the IN operator could not be used |
| 147 | # by virtual tables unless omit was set. |
| 148 | # |
| 149 | do_execsql_test tabfunc01-500 { |
| 150 | SELECT * FROM generate_series WHERE start IN (1,7) AND stop=20 AND step=10 |
| 151 | ORDER BY +1; |
| 152 | } {1 7 11 17} |
| 153 | |
drh | 5fbab88 | 2016-07-02 12:08:14 +0000 | [diff] [blame] | 154 | # Table-valued functions on the RHS of an IN operator |
| 155 | # |
| 156 | do_execsql_test tabfunc01-600 { |
| 157 | CREATE TABLE t600(a INTEGER PRIMARY KEY, b TEXT); |
| 158 | WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) |
| 159 | INSERT INTO t600(a,b) SELECT x, printf('(%03d)',x) FROM c; |
| 160 | SELECT b FROM t600 WHERE a IN generate_series(2,52,10); |
| 161 | } {(002) (012) (022) (032) (042) (052)} |
| 162 | |
drh | 4841624 | 2016-06-29 05:00:30 +0000 | [diff] [blame] | 163 | |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 164 | do_test tabfunc01-700 { |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 165 | set PTR1 [intarray_addr 5 7 13 17 23] |
drh | 64131c1 | 2016-06-29 05:08:01 +0000 | [diff] [blame] | 166 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 167 | SELECT b FROM t600, carray(inttoptr($PTR1),5) WHERE a=value; |
drh | 64131c1 | 2016-06-29 05:08:01 +0000 | [diff] [blame] | 168 | } |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 169 | } {(005) (007) (013) (017) (023)} |
| 170 | do_test tabfunc01-701 { |
| 171 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 172 | SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int32'); |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 173 | } |
| 174 | } {(005) (007) (013) (017) (023)} |
| 175 | do_test tabfunc01-702 { |
| 176 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 177 | SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),4,'int32'); |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 178 | } |
| 179 | } {(005) (007) (013) (017)} |
| 180 | do_catchsql_test tabfunc01-710 { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 181 | SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int33'); |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 182 | } {1 {unknown datatype: 'int33'}} |
drh | 64131c1 | 2016-06-29 05:08:01 +0000 | [diff] [blame] | 183 | |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 184 | do_test tabfunc01-720 { |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 185 | set PTR2 [int64array_addr 5 7 13 17 23] |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 186 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 187 | SELECT b FROM t600, carray(inttoptr($PTR2),5,'int64') WHERE a=value; |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 188 | } |
| 189 | } {(005) (007) (013) (017) (023)} |
drh | 6bada27 | 2016-08-09 21:08:42 +0000 | [diff] [blame] | 190 | do_test tabfunc01-721 { |
| 191 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 192 | SELECT remember(123,inttoptr($PTR2)); |
| 193 | SELECT value FROM carray(inttoptr($PTR2),5,'int64'); |
drh | 6bada27 | 2016-08-09 21:08:42 +0000 | [diff] [blame] | 194 | } |
| 195 | } {123 123 7 13 17 23} |
| 196 | do_test tabfunc01-722 { |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 197 | set PTR3 [expr {$PTR2+16}] |
drh | 6bada27 | 2016-08-09 21:08:42 +0000 | [diff] [blame] | 198 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 199 | SELECT remember(987,inttoptr($PTR3)); |
| 200 | SELECT value FROM carray(inttoptr($PTR2),5,'int64'); |
drh | 6bada27 | 2016-08-09 21:08:42 +0000 | [diff] [blame] | 201 | } |
| 202 | } {987 123 7 987 17 23} |
drh | 2d05331 | 2016-06-29 06:19:19 +0000 | [diff] [blame] | 203 | |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 204 | do_test tabfunc01-730 { |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 205 | set PTR4 [doublearray_addr 5.0 7.0 13.0 17.0 23.0] |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 206 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 207 | SELECT b FROM t600, carray(inttoptr($PTR4),5,'double') WHERE a=value; |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 208 | } |
| 209 | } {(005) (007) (013) (017) (023)} |
| 210 | |
| 211 | do_test tabfunc01-740 { |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 212 | set PTR5 [textarray_addr x5 x7 x13 x17 x23] |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 213 | db eval { |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 214 | SELECT b FROM t600, carray(inttoptr($PTR5),5,'char*') |
| 215 | WHERE a=trim(value,'x'); |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 216 | } |
| 217 | } {(005) (007) (013) (017) (023)} |
| 218 | |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 219 | do_test tabfunc01-750 { |
| 220 | db eval { |
| 221 | SELECT aa.value, bb.value, '|' |
drh | 3561dd4 | 2017-06-30 23:46:16 +0000 | [diff] [blame] | 222 | FROM carray(inttoptr($PTR4),5,'double') AS aa |
| 223 | JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid; |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 224 | } |
| 225 | } {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |} |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 226 | |
drh | 8103a03 | 2019-11-15 00:52:13 +0000 | [diff] [blame] | 227 | # ticket https://www.sqlite.org/src/info/2ae0c599b735d59e |
| 228 | do_test tabfunc01-751 { |
| 229 | db eval { |
| 230 | SELECT aa.value, bb.value, '|' |
| 231 | FROM carray(inttoptr($PTR4),5,'double') AS aa |
| 232 | LEFT JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid; |
| 233 | } |
| 234 | } {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |} |
| 235 | |
drh | 3d8c92d | 2021-04-22 18:02:48 +0000 | [diff] [blame] | 236 | ifcapable altertable { |
| 237 | do_test tabfunc01-800 { |
| 238 | catchsql { |
| 239 | ALTER TABLE generate_series ADD COLUMN col2; |
| 240 | } |
| 241 | } {1 {virtual tables may not be altered}} |
| 242 | do_test tabfunc01-810 { |
| 243 | catchsql { |
| 244 | ALTER TABLE generate_series RENAME TO flubber; |
| 245 | } |
| 246 | } {1 {table generate_series may not be altered}} |
| 247 | do_test tabfunc01-820 { |
| 248 | catchsql { |
| 249 | ALTER TABLE generate_series RENAME start TO flubber; |
| 250 | } |
| 251 | } {1 {table generate_series may not be altered}} |
| 252 | do_test tabfunc01-830 { |
| 253 | catchsql { |
| 254 | ALTER TABLE generate_series DROP COLUMN start; |
| 255 | } |
| 256 | } {1 {table generate_series may not be altered}} |
| 257 | do_test tabfunc01-900 { |
| 258 | catchsql { |
| 259 | ALTER TABLE pragma_compile_options ADD COLUMN col2; |
| 260 | } |
| 261 | } {1 {virtual tables may not be altered}} |
| 262 | do_test tabfunc01-910 { |
| 263 | catchsql { |
| 264 | ALTER TABLE pragma_compile_options RENAME TO flubber; |
| 265 | } |
| 266 | } {1 {table pragma_compile_options may not be altered}} |
| 267 | do_test tabfunc01-920 { |
| 268 | catchsql { |
| 269 | ALTER TABLE pragma_compile_options RENAME start TO flubber; |
| 270 | } |
| 271 | } {1 {table pragma_compile_options may not be altered}} |
| 272 | do_test tabfunc01-930 { |
| 273 | catchsql { |
| 274 | ALTER TABLE pragma_compile_options DROP COLUMN start; |
| 275 | } |
| 276 | } {1 {table pragma_compile_options may not be altered}} |
| 277 | } |
| 278 | |
| 279 | |
drh | 59a40db | 2017-02-03 15:16:25 +0000 | [diff] [blame] | 280 | # Free up memory allocations |
drh | 2e3f87a | 2016-07-03 02:35:47 +0000 | [diff] [blame] | 281 | intarray_addr |
| 282 | int64array_addr |
| 283 | doublearray_addr |
| 284 | textarray_addr |
drh | 4841624 | 2016-06-29 05:00:30 +0000 | [diff] [blame] | 285 | |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 286 | finish_test |