2015-08-19 16:54:20 +03:00
|
|
|
# 2015-08-19
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
|
|
|
# This file implements tests for table-valued-functions implemented using
|
|
|
|
# eponymous virtual tables.
|
|
|
|
#
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
set testprefix tabfunc01
|
|
|
|
|
|
|
|
ifcapable !vtab {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
load_static_extension db series
|
2016-07-03 05:35:47 +03:00
|
|
|
load_static_extension db carray
|
2016-08-10 00:08:42 +03:00
|
|
|
load_static_extension db remember
|
2015-08-19 16:54:20 +03:00
|
|
|
|
|
|
|
do_execsql_test tabfunc01-1.1 {
|
|
|
|
SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2;
|
|
|
|
} {1 | 3 | 5 | 7 | 9 |}
|
2018-10-02 00:36:38 +03:00
|
|
|
do_execsql_test tabfunc01-1.1b {
|
|
|
|
PRAGMA table_xinfo(generate_series);
|
|
|
|
} {0 value {} 0 {} 0 0 1 start {} 0 {} 0 1 2 stop {} 0 {} 0 1 3 step {} 0 {} 0 1}
|
2015-08-19 16:54:20 +03:00
|
|
|
do_execsql_test tabfunc01-1.2 {
|
|
|
|
SELECT *, '|' FROM generate_series LIMIT 5;
|
|
|
|
} {0 | 1 | 2 | 3 | 4 |}
|
|
|
|
do_catchsql_test tabfunc01-1.3 {
|
|
|
|
CREATE VIRTUAL TABLE t1 USING generate_series;
|
2015-08-19 18:20:00 +03:00
|
|
|
} {1 {no such module: generate_series}}
|
2015-08-19 20:11:37 +03:00
|
|
|
do_execsql_test tabfunc01-1.4 {
|
|
|
|
SELECT * FROM generate_series(1,9,2);
|
|
|
|
} {1 3 5 7 9}
|
|
|
|
do_execsql_test tabfunc01-1.5 {
|
|
|
|
SELECT * FROM generate_series(1,9);
|
|
|
|
} {1 2 3 4 5 6 7 8 9}
|
|
|
|
do_execsql_test tabfunc01-1.6 {
|
|
|
|
SELECT * FROM generate_series(1,10) WHERE step=3;
|
|
|
|
} {1 4 7 10}
|
|
|
|
do_catchsql_test tabfunc01-1.7 {
|
|
|
|
SELECT * FROM generate_series(1,9,2,11);
|
2015-08-21 02:21:34 +03:00
|
|
|
} {1 {too many arguments on generate_series() - max 3}}
|
2015-08-19 20:11:37 +03:00
|
|
|
|
2015-08-19 21:19:49 +03:00
|
|
|
do_execsql_test tabfunc01-1.8 {
|
|
|
|
SELECT * FROM generate_series(0,32,5) ORDER BY rowid DESC;
|
|
|
|
} {30 25 20 15 10 5 0}
|
2015-08-19 22:01:28 +03:00
|
|
|
do_execsql_test tabfunc01-1.9 {
|
|
|
|
SELECT rowid, * FROM generate_series(0,32,5) ORDER BY value DESC;
|
|
|
|
} {1 30 2 25 3 20 4 15 5 10 6 5 7 0}
|
|
|
|
do_execsql_test tabfunc01-1.10 {
|
|
|
|
SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC;
|
|
|
|
} {7 30 6 25 5 20 4 15 3 10 2 5 1 0}
|
2015-08-19 21:19:49 +03:00
|
|
|
|
2015-09-19 23:27:08 +03:00
|
|
|
do_execsql_test tabfunc01-1.20 {
|
|
|
|
CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4);
|
|
|
|
SELECT * FROM v1;
|
|
|
|
} {1 2 3 4}
|
2015-11-21 16:24:46 +03:00
|
|
|
do_catchsql_test tabfunc01-1.21.1 {
|
2015-09-19 23:27:08 +03:00
|
|
|
SELECT * FROM v1(55);
|
|
|
|
} {1 {'v1' is not a function}}
|
2015-11-21 16:24:46 +03:00
|
|
|
do_catchsql_test tabfunc01-1.21.2 {
|
|
|
|
SELECT * FROM v1();
|
|
|
|
} {1 {'v1' is not a function}}
|
2015-09-19 23:27:08 +03:00
|
|
|
do_execsql_test tabfunc01-1.22 {
|
|
|
|
CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5);
|
|
|
|
SELECT * FROM v2;
|
|
|
|
} {1 2 3 4 5}
|
2015-11-21 16:24:46 +03:00
|
|
|
do_catchsql_test tabfunc01-1.23.1 {
|
2015-09-19 23:27:08 +03:00
|
|
|
SELECT * FROM v2(55);
|
|
|
|
} {1 {'v2' is not a function}}
|
2015-11-21 16:24:46 +03:00
|
|
|
do_catchsql_test tabfunc01-1.23.2 {
|
|
|
|
SELECT * FROM v2();
|
|
|
|
} {1 {'v2' is not a function}}
|
|
|
|
do_execsql_test tabfunc01-1.24 {
|
|
|
|
CREATE TABLE t0(x);
|
|
|
|
INSERT INTO t0(x) VALUES(123),(456),(789);
|
|
|
|
SELECT * FROM t0 ORDER BY x;
|
|
|
|
} {123 456 789}
|
|
|
|
do_catchsql_test tabfunc01-1.25 {
|
|
|
|
SELECT * FROM t0(55) ORDER BY x;
|
|
|
|
} {1 {'t0' is not a function}}
|
|
|
|
do_catchsql_test tabfunc01-1.26 {
|
|
|
|
WITH w0 AS (SELECT * FROM t0)
|
|
|
|
INSERT INTO t0(x) SELECT * FROM w0()
|
|
|
|
} {1 {'w0' is not a function}}
|
2015-09-19 23:27:08 +03:00
|
|
|
|
2015-08-19 20:11:37 +03:00
|
|
|
do_execsql_test tabfunc01-2.1 {
|
|
|
|
CREATE TABLE t1(x);
|
|
|
|
INSERT INTO t1(x) VALUES(2),(3);
|
|
|
|
SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2
|
|
|
|
} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
|
2015-08-20 23:13:37 +03:00
|
|
|
do_execsql_test tabfunc01-2.2 {
|
2015-10-11 22:46:59 +03:00
|
|
|
SELECT *, '|' FROM (SELECT x FROM t1) AS y, generate_series(1,y.x)
|
|
|
|
ORDER BY 1, 2;
|
|
|
|
} {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
|
|
|
|
|
|
|
|
do_execsql_test tabfunc01-2.50 {
|
2015-08-20 23:13:37 +03:00
|
|
|
SELECT * FROM generate_series() LIMIT 5;
|
|
|
|
} {0 1 2 3 4}
|
|
|
|
|
2015-08-21 20:14:48 +03:00
|
|
|
do_execsql_test tabfunc01-3.1 {
|
|
|
|
SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1;
|
|
|
|
} {1 2 3}
|
|
|
|
|
2018-11-05 19:38:10 +03:00
|
|
|
# Eponymous virtual table exists in all schemas.
|
2015-09-08 23:26:09 +03:00
|
|
|
#
|
|
|
|
do_execsql_test tabfunc01-4.1 {
|
|
|
|
SELECT * FROM main.generate_series(1,4)
|
|
|
|
} {1 2 3 4}
|
2018-11-05 19:38:10 +03:00
|
|
|
do_execsql_test tabfunc01-4.2 {
|
2015-09-08 23:26:09 +03:00
|
|
|
SELECT * FROM temp.generate_series(1,4)
|
2018-11-05 19:38:10 +03:00
|
|
|
} {1 2 3 4}
|
|
|
|
do_execsql_test tabfunc01-4.3 {
|
2015-09-08 23:26:09 +03:00
|
|
|
ATTACH ':memory:' AS aux1;
|
|
|
|
CREATE TABLE aux1.t1(a,b,c);
|
|
|
|
SELECT * FROM aux1.generate_series(1,4)
|
2018-11-05 19:38:10 +03:00
|
|
|
} {1 2 3 4}
|
2015-08-20 23:13:37 +03:00
|
|
|
|
2018-12-03 04:47:41 +03:00
|
|
|
# 2018-12-03: Fix bug reported by by private email.
|
|
|
|
do_execsql_test tabfunc01-4.4 {
|
|
|
|
SELECT * FROM (generate_series(1,5,2)) AS x LIMIT 10;
|
|
|
|
} {1 3 5}
|
|
|
|
|
2016-03-02 06:28:07 +03:00
|
|
|
# The next series of tests is verifying that virtual table are able
|
|
|
|
# to optimize the IN operator, even on terms that are not marked "omit".
|
|
|
|
# When the generate_series virtual table is compiled for the testfixture,
|
|
|
|
# the special -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 option is used, which
|
|
|
|
# causes the xBestIndex method of generate_series to leave the
|
|
|
|
# sqlite3_index_constraint_usage.omit flag set to 0, which should cause
|
|
|
|
# the SQLite core to verify the start=, stop=, and step= constraints on
|
|
|
|
# each step of output. At one point, the IN operator could not be used
|
|
|
|
# by virtual tables unless omit was set.
|
|
|
|
#
|
|
|
|
do_execsql_test tabfunc01-500 {
|
|
|
|
SELECT * FROM generate_series WHERE start IN (1,7) AND stop=20 AND step=10
|
|
|
|
ORDER BY +1;
|
|
|
|
} {1 7 11 17}
|
|
|
|
|
2016-07-02 15:08:14 +03:00
|
|
|
# Table-valued functions on the RHS of an IN operator
|
|
|
|
#
|
|
|
|
do_execsql_test tabfunc01-600 {
|
|
|
|
CREATE TABLE t600(a INTEGER PRIMARY KEY, b TEXT);
|
|
|
|
WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
|
|
|
|
INSERT INTO t600(a,b) SELECT x, printf('(%03d)',x) FROM c;
|
|
|
|
SELECT b FROM t600 WHERE a IN generate_series(2,52,10);
|
|
|
|
} {(002) (012) (022) (032) (042) (052)}
|
|
|
|
|
2016-06-29 08:00:30 +03:00
|
|
|
|
2016-07-03 05:35:47 +03:00
|
|
|
do_test tabfunc01-700 {
|
2017-02-03 18:16:25 +03:00
|
|
|
set PTR1 [intarray_addr 5 7 13 17 23]
|
2016-06-29 08:08:01 +03:00
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600, carray(inttoptr($PTR1),5) WHERE a=value;
|
2016-06-29 08:08:01 +03:00
|
|
|
}
|
2016-07-03 05:35:47 +03:00
|
|
|
} {(005) (007) (013) (017) (023)}
|
|
|
|
do_test tabfunc01-701 {
|
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int32');
|
2016-07-03 05:35:47 +03:00
|
|
|
}
|
|
|
|
} {(005) (007) (013) (017) (023)}
|
|
|
|
do_test tabfunc01-702 {
|
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),4,'int32');
|
2016-07-03 05:35:47 +03:00
|
|
|
}
|
|
|
|
} {(005) (007) (013) (017)}
|
|
|
|
do_catchsql_test tabfunc01-710 {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int33');
|
2016-07-03 05:35:47 +03:00
|
|
|
} {1 {unknown datatype: 'int33'}}
|
|
|
|
|
|
|
|
do_test tabfunc01-720 {
|
2017-02-03 18:16:25 +03:00
|
|
|
set PTR2 [int64array_addr 5 7 13 17 23]
|
2016-07-03 05:35:47 +03:00
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600, carray(inttoptr($PTR2),5,'int64') WHERE a=value;
|
2016-07-03 05:35:47 +03:00
|
|
|
}
|
|
|
|
} {(005) (007) (013) (017) (023)}
|
2016-08-10 00:08:42 +03:00
|
|
|
do_test tabfunc01-721 {
|
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT remember(123,inttoptr($PTR2));
|
|
|
|
SELECT value FROM carray(inttoptr($PTR2),5,'int64');
|
2016-08-10 00:08:42 +03:00
|
|
|
}
|
|
|
|
} {123 123 7 13 17 23}
|
|
|
|
do_test tabfunc01-722 {
|
2017-02-03 18:16:25 +03:00
|
|
|
set PTR3 [expr {$PTR2+16}]
|
2016-08-10 00:08:42 +03:00
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT remember(987,inttoptr($PTR3));
|
|
|
|
SELECT value FROM carray(inttoptr($PTR2),5,'int64');
|
2016-08-10 00:08:42 +03:00
|
|
|
}
|
|
|
|
} {987 123 7 987 17 23}
|
2016-07-03 05:35:47 +03:00
|
|
|
|
|
|
|
do_test tabfunc01-730 {
|
2017-02-03 18:16:25 +03:00
|
|
|
set PTR4 [doublearray_addr 5.0 7.0 13.0 17.0 23.0]
|
2016-07-03 05:35:47 +03:00
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600, carray(inttoptr($PTR4),5,'double') WHERE a=value;
|
2016-07-03 05:35:47 +03:00
|
|
|
}
|
|
|
|
} {(005) (007) (013) (017) (023)}
|
|
|
|
|
|
|
|
do_test tabfunc01-740 {
|
2017-02-03 18:16:25 +03:00
|
|
|
set PTR5 [textarray_addr x5 x7 x13 x17 x23]
|
2016-07-03 05:35:47 +03:00
|
|
|
db eval {
|
2017-07-01 02:46:16 +03:00
|
|
|
SELECT b FROM t600, carray(inttoptr($PTR5),5,'char*')
|
|
|
|
WHERE a=trim(value,'x');
|
2016-07-03 05:35:47 +03:00
|
|
|
}
|
|
|
|
} {(005) (007) (013) (017) (023)}
|
|
|
|
|
2017-02-03 18:16:25 +03:00
|
|
|
do_test tabfunc01-750 {
|
|
|
|
db eval {
|
|
|
|
SELECT aa.value, bb.value, '|'
|
2017-07-01 02:46:16 +03:00
|
|
|
FROM carray(inttoptr($PTR4),5,'double') AS aa
|
|
|
|
JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid;
|
2017-02-03 18:16:25 +03:00
|
|
|
}
|
|
|
|
} {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |}
|
2016-07-03 05:35:47 +03:00
|
|
|
|
2019-11-15 03:52:13 +03:00
|
|
|
# ticket https://www.sqlite.org/src/info/2ae0c599b735d59e
|
|
|
|
do_test tabfunc01-751 {
|
|
|
|
db eval {
|
|
|
|
SELECT aa.value, bb.value, '|'
|
|
|
|
FROM carray(inttoptr($PTR4),5,'double') AS aa
|
|
|
|
LEFT JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid;
|
|
|
|
}
|
|
|
|
} {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |}
|
|
|
|
|
2021-04-22 21:02:48 +03:00
|
|
|
ifcapable altertable {
|
|
|
|
do_test tabfunc01-800 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE generate_series ADD COLUMN col2;
|
|
|
|
}
|
|
|
|
} {1 {virtual tables may not be altered}}
|
|
|
|
do_test tabfunc01-810 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE generate_series RENAME TO flubber;
|
|
|
|
}
|
|
|
|
} {1 {table generate_series may not be altered}}
|
|
|
|
do_test tabfunc01-820 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE generate_series RENAME start TO flubber;
|
|
|
|
}
|
|
|
|
} {1 {table generate_series may not be altered}}
|
|
|
|
do_test tabfunc01-830 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE generate_series DROP COLUMN start;
|
|
|
|
}
|
|
|
|
} {1 {table generate_series may not be altered}}
|
|
|
|
do_test tabfunc01-900 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE pragma_compile_options ADD COLUMN col2;
|
|
|
|
}
|
|
|
|
} {1 {virtual tables may not be altered}}
|
|
|
|
do_test tabfunc01-910 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE pragma_compile_options RENAME TO flubber;
|
|
|
|
}
|
|
|
|
} {1 {table pragma_compile_options may not be altered}}
|
|
|
|
do_test tabfunc01-920 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE pragma_compile_options RENAME start TO flubber;
|
|
|
|
}
|
|
|
|
} {1 {table pragma_compile_options may not be altered}}
|
|
|
|
do_test tabfunc01-930 {
|
|
|
|
catchsql {
|
|
|
|
ALTER TABLE pragma_compile_options DROP COLUMN start;
|
|
|
|
}
|
|
|
|
} {1 {table pragma_compile_options may not be altered}}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-03 18:16:25 +03:00
|
|
|
# Free up memory allocations
|
2016-07-03 05:35:47 +03:00
|
|
|
intarray_addr
|
|
|
|
int64array_addr
|
|
|
|
doublearray_addr
|
|
|
|
textarray_addr
|
2016-06-29 08:00:30 +03:00
|
|
|
|
2015-08-19 16:54:20 +03:00
|
|
|
finish_test
|