138 lines
2.9 KiB
Plaintext
138 lines
2.9 KiB
Plaintext
|
# 2014 November 1
|
||
|
#
|
||
|
# 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.
|
||
|
#
|
||
|
#***********************************************************************
|
||
|
#
|
||
|
|
||
|
set testdir [file dirname $argv0]
|
||
|
source $testdir/tester.tcl
|
||
|
set testprefix scanstatus2
|
||
|
|
||
|
ifcapable !scanstatus {
|
||
|
finish_test
|
||
|
return
|
||
|
}
|
||
|
|
||
|
do_execsql_test 1.0 {
|
||
|
CREATE TABLE t1(a, b);
|
||
|
CREATE TABLE t2(x, y);
|
||
|
INSERT INTO t1 VALUES(1, 2);
|
||
|
INSERT INTO t1 VALUES(3, 4);
|
||
|
INSERT INTO t2 VALUES('a', 'b');
|
||
|
INSERT INTO t2 VALUES('c', 'd');
|
||
|
INSERT INTO t2 VALUES('e', 'f');
|
||
|
}
|
||
|
|
||
|
proc do_zexplain_test {v2 tn sql res} {
|
||
|
db eval $sql
|
||
|
set stmt [db version -last-stmt-ptr]
|
||
|
set idx 0
|
||
|
set ret [list]
|
||
|
|
||
|
set cmd sqlite3_stmt_scanstatus
|
||
|
set f [list]
|
||
|
if {$v2} { lappend f complex }
|
||
|
|
||
|
while {1} {
|
||
|
set r [sqlite3_stmt_scanstatus -flags $f $stmt $idx]
|
||
|
if {[llength $r]==0} break
|
||
|
lappend ret [dict get $r zExplain]
|
||
|
incr idx
|
||
|
}
|
||
|
uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
|
||
|
}
|
||
|
|
||
|
proc get_cycles {stmt} {
|
||
|
set r [sqlite3_stmt_scanstatus $stmt -1]
|
||
|
dict get $r nCycle
|
||
|
}
|
||
|
|
||
|
proc foreach_scan {varname stmt body} {
|
||
|
upvar $varname var
|
||
|
|
||
|
for {set ii 0} {1} {incr ii} {
|
||
|
set r [sqlite3_stmt_scanstatus -flags complex $stmt $ii]
|
||
|
if {[llength $r]==0} break
|
||
|
array set var $r
|
||
|
uplevel $body
|
||
|
}
|
||
|
}
|
||
|
|
||
|
proc get_eqp_graph {stmt iPar nIndent} {
|
||
|
set res ""
|
||
|
foreach_scan A $stmt {
|
||
|
if {$A(iParentId)==$iPar} {
|
||
|
set txt $A(zExplain)
|
||
|
if {$A(nCycle)>=0} {
|
||
|
append txt " (nCycle=$A(nCycle))"
|
||
|
}
|
||
|
append res "[string repeat - $nIndent]$txt\n"
|
||
|
append res [get_eqp_graph $stmt $A(iSelectId) [expr $nIndent+2]]
|
||
|
}
|
||
|
}
|
||
|
set res
|
||
|
}
|
||
|
|
||
|
proc get_graph {stmt} {
|
||
|
set nCycle [get_cycles $stmt]
|
||
|
set res "QUERY (nCycle=$nCycle)\n"
|
||
|
append res [get_eqp_graph $stmt 0 2]
|
||
|
}
|
||
|
|
||
|
proc do_graph_test {tn sql res} {
|
||
|
db eval $sql
|
||
|
set stmt [db version -last-stmt-ptr]
|
||
|
|
||
|
set graph [string trim [get_graph $stmt]]
|
||
|
set graph [regsub -all {nCycle=[0-9]+} $graph nCycle=nnn]
|
||
|
uplevel [list do_test $tn [list set {} $graph] [string trim $res]]
|
||
|
}
|
||
|
|
||
|
|
||
|
do_zexplain_test 0 1.1 {
|
||
|
SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
|
||
|
} {
|
||
|
{SCAN t2}
|
||
|
{SCAN t1}
|
||
|
}
|
||
|
do_zexplain_test 1 1.2 {
|
||
|
SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
|
||
|
} {
|
||
|
{SCAN t2}
|
||
|
{CORRELATED SCALAR SUBQUERY 1}
|
||
|
{SCAN t1}
|
||
|
}
|
||
|
|
||
|
do_graph_test 1.3 {
|
||
|
SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
|
||
|
} {
|
||
|
QUERY (nCycle=nnn)
|
||
|
--SCAN t2
|
||
|
--CORRELATED SCALAR SUBQUERY 1 (nCycle=nnn)
|
||
|
----SCAN t1
|
||
|
}
|
||
|
|
||
|
do_graph_test 1.4 {
|
||
|
WITH v2(x,y) AS MATERIALIZED (
|
||
|
SELECT x,y FROM t2
|
||
|
)
|
||
|
SELECT * FROM t1, v2 ORDER BY y;
|
||
|
} {
|
||
|
QUERY (nCycle=nnn)
|
||
|
--MATERIALIZE v2 (nCycle=nnn)
|
||
|
----SCAN t2
|
||
|
--SCAN v2
|
||
|
--SCAN t1
|
||
|
--USE TEMP B-TREE FOR ORDER BY
|
||
|
}
|
||
|
|
||
|
finish_test
|
||
|
|
||
|
|