0
0
mirror of https://github.com/sqlite/sqlite.git synced 2024-12-01 17:23:42 +01:00
sqlite/test/unionall.test

171 lines
3.8 KiB
Plaintext
Raw Normal View History

# 2020-12-16
#
# 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 flattening UNION ALL sub-queries.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix unionall
do_execsql_test 1.0 {
CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT);
INSERT INTO t1_a VALUES(1, 'one'), (4, 'four');
INSERT INTO t1_b VALUES(2, 'two'), (5, 'five');
INSERT INTO t1_c VALUES(3, 'three'), (6, 'six');
CREATE VIEW t1 AS
SELECT a, b FROM t1_a UNION ALL
SELECT c, d FROM t1_b UNION ALL
SELECT e, f FROM t1_c;
CREATE TABLE i1(x);
INSERT INTO i1 VALUES(2), (5), (6), (1);
}
do_execsql_test 1.1 {
SELECT a, b FROM (
SELECT a, b FROM t1_a UNION ALL
SELECT c, d FROM t1_b UNION ALL
SELECT e, f FROM t1_c
) ORDER BY a
} {
1 one 2 two 3 three 4 four 5 five 6 six
}
do_execsql_test 1.2 {
SELECT a, b FROM t1 ORDER BY a
} {
1 one 2 two 3 three 4 four 5 five 6 six
}
do_execsql_test 1.3 {
SELECT a, b FROM i1, t1 WHERE a=x ORDER BY a
} {1 one 2 two 5 five 6 six}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 2.1.0 {
CREATE TABLE t1(x, y);
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(1, 'ONE');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(2, 'TWO');
INSERT INTO t1 VALUES(3, 'three');
INSERT INTO t1 VALUES(3, 'THREE');
}
do_execsql_test 2.1.1 {
WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3
)
SELECT * FROM (
SELECT 0 AS i UNION ALL SELECT i FROM s UNION ALL SELECT 0
), t1 WHERE x=i;
} {
1 1 one 1 1 ONE 2 2 two 2 2 TWO 3 3 three 3 3 THREE
}
do_catchsql_test 2.1.2 {
WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3 UNION ALL SELECT 4
)
SELECT * FROM s, t1 WHERE x=i;
} {1 {circular reference: s}}
do_execsql_test 2.2.0 {
CREATE TABLE t2_a(k INTEGER PRIMARY KEY, v TEXT);
CREATE TABLE t2_b(k INTEGER PRIMARY KEY, v TEXT);
CREATE VIEW t2 AS
SELECT * FROM t2_a
UNION ALL
SELECT * FROM t2_b;
CREATE TRIGGER t2_insert INSTEAD OF INSERT ON t2 BEGIN
INSERT INTO t2_a SELECT new.k, new.v WHERE (new.k%2)==0;
INSERT INTO t2_b SELECT new.k, new.v WHERE (new.k%2)==1;
END;
INSERT INTO t2 VALUES(5, 'v'), (4, 'iv'), (3, 'iii'), (2, 'ii');
}
do_execsql_test 2.2.1 {
SELECT * FROM t1, t2 WHERE x=k;
} {
2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii
}
do_execsql_test 2.2.2 {
SELECT * FROM t1 LEFT JOIN t2 ON (x=k);
} {
1 one {} {}
1 ONE {} {}
2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii
}
do_execsql_test 2.2.3 {
SELECT x1.*, x2.* FROM t2 AS x1, t2 AS x2 WHERE x1.k=x2.k+1
} {
4 iv 3 iii
3 iii 2 ii
5 v 4 iv
}
do_execsql_test 2.2.4 {
SELECT * FROM t1, t2 WHERE x=k ORDER BY y;
} {
3 THREE 3 iii
2 TWO 2 ii
3 three 3 iii
2 two 2 ii
}
do_execsql_test 2.2.5 {
SELECT * FROM t1, t2 WHERE x=k ORDER BY y||'';
} {
3 THREE 3 iii
2 TWO 2 ii
3 three 3 iii
2 two 2 ii
}
do_execsql_test 2.2.6 {
SELECT * FROM t1, t2 WHERE x=k ORDER BY v
} {
2 two 2 ii
2 TWO 2 ii
3 three 3 iii
3 THREE 3 iii
}
do_execsql_test 2.2.7 {
SELECT * FROM t1, t2 WHERE x=k ORDER BY v||''
} {
2 two 2 ii
2 TWO 2 ii
3 three 3 iii
3 THREE 3 iii
}
do_execsql_test 2.2.8 {
SELECT * FROM t1, t2 WHERE x=k ORDER BY k,v||''
} {
2 two 2 ii
2 TWO 2 ii
3 three 3 iii
3 THREE 3 iii
}
finish_test