OLD | NEW |
1 # 2015-08-19 | 1 # 2015-08-19 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # | 11 # |
12 # This file implements tests for table-valued-functions implemented using | 12 # This file implements tests for table-valued-functions implemented using |
13 # eponymous virtual tables. | 13 # eponymous virtual tables. |
14 # | 14 # |
15 | 15 |
16 set testdir [file dirname $argv0] | 16 set testdir [file dirname $argv0] |
17 source $testdir/tester.tcl | 17 source $testdir/tester.tcl |
18 set testprefix tabfunc01 | 18 set testprefix tabfunc01 |
19 | 19 |
20 ifcapable !vtab { | 20 ifcapable !vtab { |
21 finish_test | 21 finish_test |
22 return | 22 return |
23 } | 23 } |
24 load_static_extension db series | 24 load_static_extension db series |
| 25 load_static_extension db carray |
| 26 load_static_extension db remember |
25 | 27 |
26 do_execsql_test tabfunc01-1.1 { | 28 do_execsql_test tabfunc01-1.1 { |
27 SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2; | 29 SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2; |
28 } {1 | 3 | 5 | 7 | 9 |} | 30 } {1 | 3 | 5 | 7 | 9 |} |
29 do_execsql_test tabfunc01-1.2 { | 31 do_execsql_test tabfunc01-1.2 { |
30 SELECT *, '|' FROM generate_series LIMIT 5; | 32 SELECT *, '|' FROM generate_series LIMIT 5; |
31 } {0 | 1 | 2 | 3 | 4 |} | 33 } {0 | 1 | 2 | 3 | 4 |} |
32 do_catchsql_test tabfunc01-1.3 { | 34 do_catchsql_test tabfunc01-1.3 { |
33 CREATE VIRTUAL TABLE t1 USING generate_series; | 35 CREATE VIRTUAL TABLE t1 USING generate_series; |
34 } {1 {no such module: generate_series}} | 36 } {1 {no such module: generate_series}} |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } {1 2 3 4} | 115 } {1 2 3 4} |
114 do_catchsql_test tabfunc01-4.2 { | 116 do_catchsql_test tabfunc01-4.2 { |
115 SELECT * FROM temp.generate_series(1,4) | 117 SELECT * FROM temp.generate_series(1,4) |
116 } {1 {no such table: temp.generate_series}} | 118 } {1 {no such table: temp.generate_series}} |
117 do_catchsql_test tabfunc01-4.3 { | 119 do_catchsql_test tabfunc01-4.3 { |
118 ATTACH ':memory:' AS aux1; | 120 ATTACH ':memory:' AS aux1; |
119 CREATE TABLE aux1.t1(a,b,c); | 121 CREATE TABLE aux1.t1(a,b,c); |
120 SELECT * FROM aux1.generate_series(1,4) | 122 SELECT * FROM aux1.generate_series(1,4) |
121 } {1 {no such table: aux1.generate_series}} | 123 } {1 {no such table: aux1.generate_series}} |
122 | 124 |
| 125 # 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 # |
| 135 do_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 |
| 140 # Table-valued functions on the RHS of an IN operator |
| 141 # |
| 142 do_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 |
| 149 |
| 150 do_test tabfunc01-700 { |
| 151 set PTR1 [intarray_addr 5 7 13 17 23] |
| 152 db eval { |
| 153 SELECT b FROM t600, carray($PTR1,5) WHERE a=value; |
| 154 } |
| 155 } {(005) (007) (013) (017) (023)} |
| 156 do_test tabfunc01-701 { |
| 157 db eval { |
| 158 SELECT b FROM t600 WHERE a IN carray($PTR1,5,'int32'); |
| 159 } |
| 160 } {(005) (007) (013) (017) (023)} |
| 161 do_test tabfunc01-702 { |
| 162 db eval { |
| 163 SELECT b FROM t600 WHERE a IN carray($PTR1,4,'int32'); |
| 164 } |
| 165 } {(005) (007) (013) (017)} |
| 166 do_catchsql_test tabfunc01-710 { |
| 167 SELECT b FROM t600 WHERE a IN carray($PTR1,5,'int33'); |
| 168 } {1 {unknown datatype: 'int33'}} |
| 169 |
| 170 do_test tabfunc01-720 { |
| 171 set PTR2 [int64array_addr 5 7 13 17 23] |
| 172 db eval { |
| 173 SELECT b FROM t600, carray($PTR2,5,'int64') WHERE a=value; |
| 174 } |
| 175 } {(005) (007) (013) (017) (023)} |
| 176 do_test tabfunc01-721 { |
| 177 db eval { |
| 178 SELECT remember(123,$PTR2); |
| 179 SELECT value FROM carray($PTR2,5,'int64'); |
| 180 } |
| 181 } {123 123 7 13 17 23} |
| 182 do_test tabfunc01-722 { |
| 183 set PTR3 [expr {$PTR2+16}] |
| 184 db eval { |
| 185 SELECT remember(987,$PTR3); |
| 186 SELECT value FROM carray($PTR2,5,'int64'); |
| 187 } |
| 188 } {987 123 7 987 17 23} |
| 189 |
| 190 do_test tabfunc01-730 { |
| 191 set PTR4 [doublearray_addr 5.0 7.0 13.0 17.0 23.0] |
| 192 db eval { |
| 193 SELECT b FROM t600, carray($PTR4,5,'double') WHERE a=value; |
| 194 } |
| 195 } {(005) (007) (013) (017) (023)} |
| 196 |
| 197 do_test tabfunc01-740 { |
| 198 set PTR5 [textarray_addr x5 x7 x13 x17 x23] |
| 199 db eval { |
| 200 SELECT b FROM t600, carray($PTR5,5,'char*') WHERE a=trim(value,'x'); |
| 201 } |
| 202 } {(005) (007) (013) (017) (023)} |
| 203 |
| 204 do_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 |} |
| 211 |
| 212 # Free up memory allocations |
| 213 intarray_addr |
| 214 int64array_addr |
| 215 doublearray_addr |
| 216 textarray_addr |
| 217 |
123 finish_test | 218 finish_test |
OLD | NEW |