# 2017-07-15 # # 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 regression tests for SQLite library. The # focus of this file is percentile.c extension # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix unionvtab load_static_extension db unionvtab #------------------------------------------------------------------------- # Warm body tests. # forcedelete test.db2 do_execsql_test 1.0 { ATTACH 'test.db2' AS aux; CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT); CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b TEXT); INSERT INTO t1 VALUES(1, 'one'), (2, 'two'), (3, 'three'); INSERT INTO t2 VALUES(10, 'ten'), (11, 'eleven'), (12, 'twelve'); INSERT INTO t3 VALUES(20, 'twenty'), (21, 'twenty-one'), (22, 'twenty-two'); } do_execsql_test 1.1 { CREATE VIRTUAL TABLE temp.uuu USING unionvtab( "VALUES(NULL, 't1', 1, 9), ('main', 't2', 10, 19), ('aux', 't3', 20, 29)" ); SELECT * FROM uuu; } { 1 one 2 two 3 three 10 ten 11 eleven 12 twelve 20 twenty 21 twenty-one 22 twenty-two } do_execsql_test 1.2 { PRAGMA table_info(uuu); } { 0 a INTEGER 0 {} 0 1 b TEXT 0 {} 0 } do_execsql_test 1.3 { SELECT * FROM uuu WHERE rowid = 3; SELECT * FROM uuu WHERE rowid = 11; } {3 three 11 eleven} do_execsql_test 1.4 { SELECT * FROM uuu WHERE rowid IN (12, 10, 2); } {2 two 10 ten 12 twelve} do_execsql_test 1.5 { SELECT * FROM uuu WHERE rowid BETWEEN 3 AND 11; } {3 three 10 ten 11 eleven} do_execsql_test 1.6 { SELECT * FROM uuu WHERE rowid BETWEEN 11 AND 15; } {11 eleven 12 twelve} do_execsql_test 1.7 { SELECT * FROM uuu WHERE rowid BETWEEN -46 AND 1500; } { 1 one 2 two 3 three 10 ten 11 eleven 12 twelve 20 twenty 21 twenty-one 22 twenty-two } #------------------------------------------------------------------------- # Error conditions. # # 2.1.*: Attempt to create a unionvtab table outside of the TEMP schema. # 2.2.*: Tables that do not exist. # 2.3.*: Non rowid tables. # 2.4.*: Tables with mismatched schemas. # do_catchsql_test 2.1.1 { CREATE VIRTUAL TABLE u1 USING unionvtab("VALUES(NULL, 't1', 1, 100)"); } {1 {unionvtab tables must be created in TEMP schema}} do_catchsql_test 2.1.2 { CREATE VIRTUAL TABLE main.u1 USING unionvtab("VALUES('', 't1', 1, 100)"); } {1 {unionvtab tables must be created in TEMP schema}} do_catchsql_test 2.1.3 { CREATE VIRTUAL TABLE aux.u1 USING unionvtab("VALUES('', 't1', 1, 100)"); } {1 {unionvtab tables must be created in TEMP schema}} do_catchsql_test 2.2.1 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 't555', 1, 100)"); } {1 {no such rowid table: t555}} do_catchsql_test 2.2.2 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('aux', 't555', 1, 100)"); } {1 {no such rowid table: aux.t555}} do_catchsql_test 2.2.3 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('xua', 't555', 1, 100)"); } {1 {no such rowid table: xua.t555}} do_execsql_test 2.3.0 { CREATE TABLE wr1(a, b, c PRIMARY KEY) WITHOUT ROWID; CREATE VIEW v1 AS SELECT * FROM t1; CREATE VIEW v2 AS SELECT _rowid_, * FROM t1; } do_catchsql_test 2.3.1 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('main', 'wr1', 1, 2)"); } {1 {no such rowid table: main.wr1}} do_catchsql_test 2.3.2 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'v1', 1, 2)"); } {1 {no such rowid table: v1}} do_catchsql_test 2.3.3 { CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'v2', 1, 2)"); } {1 {no such rowid table: v2}} do_execsql_test 2.4.0 { CREATE TABLE x1(a BLOB, b); CREATE TABLE x2(a BLOB, b); CREATE TEMP TABLE x3(a BLOB, b); CREATE TABLE aux.y1(one, two, three INTEGER PRIMARY KEY); CREATE TEMP TABLE y2(one, two, three INTEGER PRIMARY KEY); CREATE TABLE y3(one, two, three INTEGER PRIMARY KEY); } foreach {tn dbs res} { 1 {x1 x2 x3} {0 {}} 2 {y1 y2 y3} {0 {}} 3 {x1 y2 y3} {1 {source table schema mismatch}} 4 {x1 y2 x3} {1 {source table schema mismatch}} 5 {x1 x2 y3} {1 {source table schema mismatch}} } { set L [list] set iMin 0 foreach e $dbs { set E [split $e .] if {[llength $E]>1} { lappend L "('[lindex $E 0]', '[lindex $E 1]', $iMin, $iMin)" } else { lappend L "(NULL, '$e', $iMin, $iMin)" } incr iMin } set sql "CREATE VIRTUAL TABLE temp.a1 USING unionvtab(\"VALUES [join $L ,]\")" do_catchsql_test 2.4.$tn " DROP TABLE IF EXISTS temp.a1; CREATE VIRTUAL TABLE temp.a1 USING unionvtab(\"VALUES [join $L ,]\"); " $res } #------------------------------------------------------------------------- # reset_db load_static_extension db unionvtab do_execsql_test 3.0 { CREATE TABLE tbl1(a INTEGER PRIMARY KEY, b); CREATE TABLE tbl2(a INTEGER PRIMARY KEY, b); CREATE TABLE tbl3(a INTEGER PRIMARY KEY, b); WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) INSERT INTO tbl1 SELECT ii, '1.' || ii FROM ss; WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) INSERT INTO tbl2 SELECT ii, '2.' || ii FROM ss; WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) INSERT INTO tbl3 SELECT ii, '3.' || ii FROM ss; CREATE VIRTUAL TABLE temp.uu USING unionvtab( "VALUES(NULL,'tbl2', 26, 74), (NULL,'tbl3', 75, 100), (NULL,'tbl1', 1, 25)" ); } do_execsql_test 3.1 { SELECT * FROM uu WHERE rowid = 10; } {10 {1.10}} do_execsql_test 3.2 { SELECT * FROM uu WHERE rowid = 25; } {25 {1.25}} do_execsql_test 3.3 { SELECT count(*) FROM uu WHERE rowid <= 24 } {24} # The following queries get the "wrong" answers. This is because the # module assumes that each source table contains rowids from only within # the range specified. For example, (rowid <= 25) matches 100 rows. This # is because the module implements (rowid <= 25) as a full table scan # of tbl1 only. do_execsql_test 3.4.1 { SELECT count(*) FROM uu WHERE rowid <= 25 } {100} do_execsql_test 3.4.2 { SELECT count(*) FROM uu WHERE rowid <= 26 } {126} do_execsql_test 3.4.3 { SELECT count(*) FROM uu WHERE rowid <= 73 } {173} do_execsql_test 3.4.4 { SELECT count(*) FROM uu WHERE rowid <= 74 } {200} do_execsql_test 3.4.5 { SELECT count(*) FROM uu WHERE rowid <= 75 } {275} do_execsql_test 3.4.6 { SELECT count(*) FROM uu WHERE rowid <= 99 } {299} do_execsql_test 3.4.7 { SELECT count(*) FROM uu WHERE rowid <= 100 } {300} do_execsql_test 3.4.8 { SELECT count(*) FROM uu WHERE rowid <= 101 } {300} do_execsql_test 3.5.1 { SELECT count(*) FROM uu WHERE rowid < 25 } {24} do_execsql_test 3.5.2 { SELECT count(*) FROM uu WHERE rowid < 26 } {100} do_execsql_test 3.5.3 { SELECT count(*) FROM uu WHERE rowid < 27 } {126} do_execsql_test 3.5.4 { SELECT count(*) FROM uu WHERE rowid < 73 } {172} do_execsql_test 3.5.5 { SELECT count(*) FROM uu WHERE rowid < 74 } {173} do_execsql_test 3.5.6 { SELECT count(*) FROM uu WHERE rowid < 75 } {200} do_execsql_test 3.5.7 { SELECT count(*) FROM uu WHERE rowid < 76 } {275} do_execsql_test 3.5.8 { SELECT count(*) FROM uu WHERE rowid < 99 } {298} do_execsql_test 3.5.9 { SELECT count(*) FROM uu WHERE rowid < 100 } {299} do_execsql_test 3.5.10 { SELECT count(*) FROM uu WHERE rowid < 101 } {300} do_execsql_test 3.6.1 { SELECT count(*) FROM uu WHERE rowid > 24 } {276} do_execsql_test 3.6.1 { SELECT count(*) FROM uu WHERE rowid > 25 } {200} do_execsql_test 3.6.2 { SELECT count(*) FROM uu WHERE rowid > 26 } {174} do_execsql_test 3.6.3 { SELECT count(*) FROM uu WHERE rowid > 27 } {173} do_execsql_test 3.6.4 { SELECT count(*) FROM uu WHERE rowid > 73 } {127} do_execsql_test 3.6.5 { SELECT count(*) FROM uu WHERE rowid > 74 } {100} do_execsql_test 3.6.6 { SELECT count(*) FROM uu WHERE rowid > 75 } {25} do_execsql_test 3.6.7 { SELECT count(*) FROM uu WHERE rowid > 76 } {24} do_execsql_test 3.6.8 { SELECT count(*) FROM uu WHERE rowid > 99 } {1} do_execsql_test 3.6.9 { SELECT count(*) FROM uu WHERE rowid > 100 } {0} do_execsql_test 3.6.10 { SELECT count(*) FROM uu WHERE rowid > 101 } {0} do_execsql_test 3.7.1 { SELECT count(*) FROM uu WHERE rowid >= 24 } {277} do_execsql_test 3.7.1 { SELECT count(*) FROM uu WHERE rowid >= 25 } {276} do_execsql_test 3.7.2 { SELECT count(*) FROM uu WHERE rowid >= 26 } {200} do_execsql_test 3.7.3 { SELECT count(*) FROM uu WHERE rowid >= 27 } {174} do_execsql_test 3.7.4 { SELECT count(*) FROM uu WHERE rowid >= 73 } {128} do_execsql_test 3.7.5 { SELECT count(*) FROM uu WHERE rowid >= 74 } {127} do_execsql_test 3.7.6 { SELECT count(*) FROM uu WHERE rowid >= 75 } {100} do_execsql_test 3.7.7 { SELECT count(*) FROM uu WHERE rowid >= 76 } {25} do_execsql_test 3.7.8 { SELECT count(*) FROM uu WHERE rowid >= 99 } {2} do_execsql_test 3.7.9 { SELECT count(*) FROM uu WHERE rowid >= 100 } {1} do_execsql_test 3.7.10 { SELECT count(*) FROM uu WHERE rowid >= 101 } {0} set L [expr 9223372036854775807] set S [expr -9223372036854775808] do_execsql_test 3.8.1 { SELECT count(*) FROM uu WHERE rowid >= $S } {300} do_execsql_test 3.8.2 { SELECT count(*) FROM uu WHERE rowid > $S } {300} do_execsql_test 3.8.3 { SELECT count(*) FROM uu WHERE rowid <= $S } {0} do_execsql_test 3.8.4 { SELECT count(*) FROM uu WHERE rowid < $S } {0} do_execsql_test 3.9.1 { SELECT count(*) FROM uu WHERE rowid >= $L } {0} do_execsql_test 3.9.2 { SELECT count(*) FROM uu WHERE rowid > $L } {0} do_execsql_test 3.9.3 { SELECT count(*) FROM uu WHERE rowid <= $L } {300} do_execsql_test 3.9.4 { SELECT count(*) FROM uu WHERE rowid < $L } {300} do_execsql_test 3.10.1 { SELECT count(*) FROM uu WHERE a < 25 } {24} do_execsql_test 3.10.2 { SELECT count(*) FROM uu WHERE a < 26 } {100} do_execsql_test 3.10.3 { SELECT count(*) FROM uu WHERE a < 27 } {126} do_execsql_test 3.10.4 { SELECT count(*) FROM uu WHERE a < 73 } {172} do_execsql_test 3.10.5 { SELECT count(*) FROM uu WHERE a < 74 } {173} do_execsql_test 3.10.6 { SELECT count(*) FROM uu WHERE a < 75 } {200} do_execsql_test 3.10.7 { SELECT count(*) FROM uu WHERE a < 76 } {275} do_execsql_test 3.10.8 { SELECT count(*) FROM uu WHERE a < 99 } {298} do_execsql_test 3.10.9 { SELECT count(*) FROM uu WHERE a < 100 } {299} do_execsql_test 3.10.10 { SELECT count(*) FROM uu WHERE a < 101 } {300} #------------------------------------------------------------------------- # do_execsql_test 4.0 { CREATE TABLE s1(k INTEGER PRIMARY KEY, v); INSERT INTO s1 VALUES($S, 'one'); INSERT INTO s1 VALUES($S+1, 'two'); INSERT INTO s1 VALUES($S+2, 'three'); CREATE TABLE l1(k INTEGER PRIMARY KEY, v); INSERT INTO l1 VALUES($L, 'six'); INSERT INTO l1 VALUES($L-1, 'five'); INSERT INTO l1 VALUES($L-2, 'four'); CREATE VIRTUAL TABLE temp.sl USING unionvtab( "SELECT NULL, 'l1', 0, 9223372036854775807 UNION ALL SELECT NULL, 's1', -9223372036854775808, -1" ); } do_execsql_test 4.1 { SELECT * FROM sl; } { -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six } foreach {k v} { -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six } { do_execsql_test 4.2.$v { SELECT * FROM sl WHERE rowid=$k } [list $k $v] } do_execsql_test 4.3.1 { SELECT * FROM sl WHERE rowid>-9223372036854775808 } { -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six } do_execsql_test 4.3.2 { SELECT * FROM sl WHERE rowid>=-9223372036854775808 } { -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six } do_execsql_test 4.3.3 { SELECT * FROM sl WHERE rowid<=-9223372036854775808 } { -9223372036854775808 one } do_execsql_test 4.3.4 { SELECT * FROM sl WHERE rowid<-9223372036854775808 } { } do_execsql_test 4.4.1 { SELECT * FROM sl WHERE rowid<9223372036854775807 } { -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five } do_execsql_test 4.4.2 { SELECT * FROM sl WHERE rowid<=9223372036854775807 } { -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six } do_execsql_test 4.4.3 { SELECT * FROM sl WHERE rowid>=9223372036854775807 } { 9223372036854775807 six } do_execsql_test 4.4.4 { SELECT * FROM sl WHERE rowid>9223372036854775807 } { } finish_test