# 2021-02-22 # # 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 the MATERIALIZED hint to common table expressions # set testdir [file dirname $argv0] source $testdir/tester.tcl set ::testprefix with6 ifcapable {!cte} { finish_test return } do_execsql_test 100 { WITH c(x) AS (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } {000 001 010 011 100 101 110 111} do_eqp_test 101 { WITH c(x) AS (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | `--SCAN 2 CONSTANT ROWS |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } do_execsql_test 110 { WITH c(x) AS MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } {000 001 010 011 100 101 110 111} do_eqp_test 111 { WITH c(x) AS MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | `--SCAN 2 CONSTANT ROWS |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } # Even though the CTE is not materialized, the self-join optimization # kicks in and does the materialization for us. # do_execsql_test 120 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } {000 001 010 011 100 101 110 111} do_eqp_test 121 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | `--SCAN 2 CONSTANT ROWS |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } do_execsql_test 130 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 5) AS c2, (SELECT x FROM c LIMIT 5) AS c3; } {000 001 010 011 100 101 110 111} do_eqp_test 131 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 5) AS c2, (SELECT x FROM c LIMIT 5) AS c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | |--CO-ROUTINE xxxxxx | | `--SCAN 2 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | |--CO-ROUTINE xxxxxx | | `--SCAN 2 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | |--CO-ROUTINE xxxxxx | | `--SCAN 2 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } # The (SELECT x FROM c LIMIT N) subqueries get materialized once each. # Show multiple materializations are shown. But there is only one # materialization for c, shown by the "SCAN 2 CONSTANT ROWS" line. # do_execsql_test 140 { WITH c(x) AS MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 6) AS c2, (SELECT x FROM c LIMIT 7) AS c3; } {000 001 010 011 100 101 110 111} do_eqp_test 141 { WITH c(x) AS MATERIALIZED (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 6) AS c2, (SELECT x FROM c LIMIT 7) AS c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | |--MATERIALIZE xxxxxx | | `--SCAN 2 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | `--SCAN SUBQUERY xxxxxx |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } do_execsql_test 150 { WITH c(x) AS (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 6) AS c2, (SELECT x FROM c LIMIT 7) AS c3; } {000 001 010 011 100 101 110 111} do_eqp_test 151 { WITH c(x) AS (VALUES(0),(1)) SELECT c1.x||c2.x||c3.x FROM (SELECT x FROM c LIMIT 5) AS c1, (SELECT x FROM c LIMIT 6) AS c2, (SELECT x FROM c LIMIT 7) AS c3; } { QUERY PLAN |--MATERIALIZE xxxxxx | |--MATERIALIZE xxxxxx | | `--SCAN 2 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | `--SCAN SUBQUERY xxxxxx |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 `--SCAN SUBQUERY xxxxxx AS c3 } do_execsql_test 160 { WITH c(x) AS (VALUES(0),(1)) SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) FROM c AS c2 WHERE c2.x<10; } {100 301} do_eqp_test 161 { WITH c(x) AS (VALUES(0),(1)) SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) FROM c AS c2 WHERE c2.x<10; } { QUERY PLAN |--MATERIALIZE xxxxxx | `--SCAN 2 CONSTANT ROWS |--SCAN SUBQUERY xxxxxx AS c2 `--CORRELATED SCALAR SUBQUERY xxxxxx `--SCAN SUBQUERY xxxxxx } do_execsql_test 170 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) FROM c AS c2 WHERE c2.x<10; } {100 301} do_eqp_test 171 { WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) FROM c AS c2 WHERE c2.x<10; } { QUERY PLAN |--CO-ROUTINE xxxxxx | `--SCAN 2 CONSTANT ROWS |--SCAN SUBQUERY xxxxxx AS c2 `--CORRELATED SCALAR SUBQUERY xxxxxx |--CO-ROUTINE xxxxxx | `--SCAN 2 CONSTANT ROWS `--SCAN SUBQUERY xxxxxx } do_execsql_test 200 { CREATE TABLE t1(x); INSERT INTO t1(x) VALUES(4); CREATE VIEW t2(y) AS WITH c(z) AS (VALUES(4),(5),(6)) SELECT c1.z+c2.z*100+t1.x*10000 FROM t1, (SELECT z FROM c LIMIT 5) AS c1, (SELECT z FROM c LIMIT 5) AS c2; SELECT y FROM t2 ORDER BY y; } {40404 40405 40406 40504 40505 40506 40604 40605 40606} do_execsql_test 210 { DROP VIEW t2; CREATE VIEW t2(y) AS WITH c(z) AS NOT MATERIALIZED (VALUES(4),(5),(6)) SELECT c1.z+c2.z*100+t1.x*10000 FROM t1, (SELECT z FROM c LIMIT 5) AS c1, (SELECT z FROM c LIMIT 5) AS c2; SELECT y FROM t2 ORDER BY y; } {40404 40405 40406 40504 40505 40506 40604 40605 40606} do_eqp_test 211 { SELECT y FROM t2 ORDER BY y; } { QUERY PLAN |--MATERIALIZE xxxxxx | |--MATERIALIZE xxxxxx | | `--SCAN 3 CONSTANT ROWS | `--SCAN SUBQUERY xxxxxx |--MATERIALIZE xxxxxx | `--SCAN SUBQUERY xxxxxx |--SCAN SUBQUERY xxxxxx AS c1 |--SCAN SUBQUERY xxxxxx AS c2 |--SCAN TABLE t1 `--USE TEMP B-TREE FOR ORDER BY } do_execsql_test 220 { DROP VIEW t2; CREATE VIEW t2(y) AS WITH c(z) AS MATERIALIZED (VALUES(4),(5),(6)) SELECT c1.z+c2.z*100+t1.x*10000 FROM t1, (SELECT z FROM c LIMIT 5) AS c1, (SELECT z FROM c LIMIT 5) AS c2; SELECT y FROM t2 ORDER BY y; } {40404 40405 40406 40504 40505 40506 40604 40605 40606} finish_test