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 |
| 25 | |
| 26 | do_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 |} |
| 29 | do_execsql_test tabfunc01-1.2 { |
| 30 | SELECT *, '|' FROM generate_series LIMIT 5; |
| 31 | } {0 | 1 | 2 | 3 | 4 |} |
| 32 | do_catchsql_test tabfunc01-1.3 { |
| 33 | CREATE VIRTUAL TABLE t1 USING generate_series; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 34 | } {1 {no such module: generate_series}} |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 35 | do_execsql_test tabfunc01-1.4 { |
| 36 | SELECT * FROM generate_series(1,9,2); |
| 37 | } {1 3 5 7 9} |
| 38 | do_execsql_test tabfunc01-1.5 { |
| 39 | SELECT * FROM generate_series(1,9); |
| 40 | } {1 2 3 4 5 6 7 8 9} |
| 41 | do_execsql_test tabfunc01-1.6 { |
| 42 | SELECT * FROM generate_series(1,10) WHERE step=3; |
| 43 | } {1 4 7 10} |
| 44 | do_catchsql_test tabfunc01-1.7 { |
| 45 | SELECT * FROM generate_series(1,9,2,11); |
drh | d8b1bfc | 2015-08-20 23:21:34 +0000 | [diff] [blame] | 46 | } {1 {too many arguments on generate_series() - max 3}} |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 47 | |
drh | bc550df | 2015-08-19 18:19:49 +0000 | [diff] [blame] | 48 | do_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} |
drh | 509c3fc | 2015-08-19 19:01:28 +0000 | [diff] [blame] | 51 | do_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} |
| 54 | do_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} |
drh | bc550df | 2015-08-19 18:19:49 +0000 | [diff] [blame] | 57 | |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 58 | do_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} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 62 | do_catchsql_test tabfunc01-1.21.1 { |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 63 | SELECT * FROM v1(55); |
| 64 | } {1 {'v1' is not a function}} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 65 | do_catchsql_test tabfunc01-1.21.2 { |
| 66 | SELECT * FROM v1(); |
| 67 | } {1 {'v1' is not a function}} |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 68 | do_execsql_test tabfunc01-1.22 { |
| 69 | CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5); |
| 70 | SELECT * FROM v2; |
| 71 | } {1 2 3 4 5} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 72 | do_catchsql_test tabfunc01-1.23.1 { |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 73 | SELECT * FROM v2(55); |
| 74 | } {1 {'v2' is not a function}} |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 75 | do_catchsql_test tabfunc01-1.23.2 { |
| 76 | SELECT * FROM v2(); |
| 77 | } {1 {'v2' is not a function}} |
| 78 | do_execsql_test tabfunc01-1.24 { |
| 79 | CREATE TABLE t0(x); |
| 80 | INSERT INTO t0(x) VALUES(123),(456),(789); |
| 81 | SELECT * FROM t0 ORDER BY x; |
| 82 | } {123 456 789} |
| 83 | do_catchsql_test tabfunc01-1.25 { |
| 84 | SELECT * FROM t0(55) ORDER BY x; |
| 85 | } {1 {'t0' is not a function}} |
| 86 | do_catchsql_test tabfunc01-1.26 { |
| 87 | WITH w0 AS (SELECT * FROM t0) |
| 88 | INSERT INTO t0(x) SELECT * FROM w0() |
| 89 | } {1 {'w0' is not a function}} |
drh | 6230212 | 2015-09-19 20:27:08 +0000 | [diff] [blame] | 90 | |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 91 | do_execsql_test tabfunc01-2.1 { |
| 92 | CREATE TABLE t1(x); |
| 93 | INSERT INTO t1(x) VALUES(2),(3); |
| 94 | SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2 |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 95 | } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |} |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 96 | do_execsql_test tabfunc01-2.2 { |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 97 | SELECT *, '|' FROM (SELECT x FROM t1) AS y, generate_series(1,y.x) |
| 98 | ORDER BY 1, 2; |
| 99 | } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |} |
| 100 | |
| 101 | do_execsql_test tabfunc01-2.50 { |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 102 | SELECT * FROM generate_series() LIMIT 5; |
| 103 | } {0 1 2 3 4} |
| 104 | |
drh | 1f2fc28 | 2015-08-21 17:14:48 +0000 | [diff] [blame] | 105 | do_execsql_test tabfunc01-3.1 { |
| 106 | SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1; |
| 107 | } {1 2 3} |
| 108 | |
drh | b4d472f | 2015-09-08 20:26:09 +0000 | [diff] [blame] | 109 | # Eponymous virtual table exists in the "main" schema only |
| 110 | # |
| 111 | do_execsql_test tabfunc01-4.1 { |
| 112 | SELECT * FROM main.generate_series(1,4) |
| 113 | } {1 2 3 4} |
| 114 | do_catchsql_test tabfunc01-4.2 { |
| 115 | SELECT * FROM temp.generate_series(1,4) |
| 116 | } {1 {no such table: temp.generate_series}} |
| 117 | do_catchsql_test tabfunc01-4.3 { |
| 118 | ATTACH ':memory:' AS aux1; |
| 119 | CREATE TABLE aux1.t1(a,b,c); |
| 120 | SELECT * FROM aux1.generate_series(1,4) |
| 121 | } {1 {no such table: aux1.generate_series}} |
drh | d10dbad | 2015-08-20 20:13:37 +0000 | [diff] [blame] | 122 | |
drh | dbc4916 | 2016-03-02 03:28:07 +0000 | [diff] [blame] | 123 | # The next series of tests is verifying that virtual table are able |
| 124 | # to optimize the IN operator, even on terms that are not marked "omit". |
| 125 | # When the generate_series virtual table is compiled for the testfixture, |
| 126 | # the special -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 option is used, which |
| 127 | # causes the xBestIndex method of generate_series to leave the |
| 128 | # sqlite3_index_constraint_usage.omit flag set to 0, which should cause |
| 129 | # the SQLite core to verify the start=, stop=, and step= constraints on |
| 130 | # each step of output. At one point, the IN operator could not be used |
| 131 | # by virtual tables unless omit was set. |
| 132 | # |
| 133 | do_execsql_test tabfunc01-500 { |
| 134 | SELECT * FROM generate_series WHERE start IN (1,7) AND stop=20 AND step=10 |
| 135 | ORDER BY +1; |
| 136 | } {1 7 11 17} |
| 137 | |
drh | 398f872 | 2015-08-19 13:54:20 +0000 | [diff] [blame] | 138 | finish_test |