sqlite/test/unionall.test
dan d131b51cbd Fix for the previous fix in the case where a UNION ALL sub-query is joined against some other compound query.
FossilOrigin-Name: 63c5cfb9ae8f4598a523bed2a60c0e69172179952961a573113fcf756c06551d
2020-12-18 18:04:44 +00:00

249 lines
5.7 KiB
Plaintext

# 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
}
do_execsql_test 2.2.9a {
SELECT * FROM t1, t2 ORDER BY +k
} {
1 one 2 ii 1 ONE 2 ii 2 two 2 ii
2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii
1 one 3 iii 1 ONE 3 iii 2 two 3 iii
2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii
1 one 4 iv 1 ONE 4 iv 2 two 4 iv
2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv
1 one 5 v 1 ONE 5 v 2 two 5 v
2 TWO 5 v 3 three 5 v 3 THREE 5 v
}
do_execsql_test 2.2.9b {
SELECT * FROM t1, t2 ORDER BY k
} {
1 one 2 ii 1 ONE 2 ii 2 two 2 ii
2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii
1 one 3 iii 1 ONE 3 iii 2 two 3 iii
2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii
1 one 4 iv 1 ONE 4 iv 2 two 4 iv
2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv
1 one 5 v 1 ONE 5 v 2 two 5 v
2 TWO 5 v 3 three 5 v 3 THREE 5 v
}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
CREATE TABLE t1(c INTEGER PRIMARY KEY, d TEXT);
INSERT INTO t1 VALUES(1,2);
CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT);
INSERT INTO t3_a VALUES(2,'ii');
CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT);
CREATE VIEW t3 AS
SELECT * FROM t3_a
UNION ALL
SELECT * FROM t3_b;
} {}
do_execsql_test 3.1 {
SELECT * FROM t1, t3 ORDER BY k;
} {1 2 2 ii}
reset_db
do_execsql_test 4.0 {
CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
INSERT INTO t1_a VALUES(123, 't1_a');
CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
CREATE VIEW t1 AS
SELECT a, b FROM t1_a
UNION ALL
SELECT c, d FROM t1_b;
CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT);
INSERT INTO t3_a VALUES(456, 't3_a');
CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT);
CREATE VIEW t3 AS
SELECT * FROM t3_a
UNION ALL
SELECT * FROM t3_b;
}
do_execsql_test 4.1 {
SELECT * FROM t1, t3 ORDER BY k;
} {123 t1_a 456 t3_a}
do_execsql_test 4.2 {
SELECT * FROM (SELECT * FROM t1, t3) ORDER BY k;
} {123 t1_a 456 t3_a}
finish_test