interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / scanstatus2.test

351 lines · 8812 bytes Raw Blame History
1# 2022 December 5
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix scanstatus2
16
17ifcapable !scanstatus {
18 finish_test
19 return
20}
21
22sqlite3_db_config db STMT_SCANSTATUS 1
23
24do_execsql_test 1.0 {
25 CREATE TABLE t1(a, b);
26 CREATE TABLE t2(x, y);
27 INSERT INTO t1 VALUES(1, 2);
28 INSERT INTO t1 VALUES(3, 4);
29 INSERT INTO t2 VALUES('a', 'b');
30 INSERT INTO t2 VALUES('c', 'd');
31 INSERT INTO t2 VALUES('e', 'f');
32}
33
34proc do_zexplain_test {v2 tn sql res} {
35 db eval $sql
36 set stmt [db version -last-stmt-ptr]
37 set idx 0
38 set ret [list]
39
40 set cmd sqlite3_stmt_scanstatus
41 set f [list]
42 if {$v2} { lappend f complex }
43
44 while {1} {
45 set r [sqlite3_stmt_scanstatus -flags $f $stmt $idx]
46 if {[llength $r]==0} break
47 lappend ret [dict get $r zExplain]
48 incr idx
49 }
50 uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
51}
52
53proc get_cycles {stmt} {
54 set r [sqlite3_stmt_scanstatus $stmt -1]
55 dict get $r nCycle
56}
57
58proc foreach_scan {varname stmt body {debug 0}} {
59 upvar $varname var
60 for {set ii 0} {1} {incr ii} {
61 set f "complex"
62 if {$debug} { set f "complex debug" }
63 set r [sqlite3_stmt_scanstatus -flags $f $stmt $ii]
64 if {[llength $r]==0} break
65 array set var $r
66 uplevel $body
67 }
68}
69
70proc get_eqp_graph {stmt iPar nIndent} {
71 set res ""
72 foreach_scan A $stmt {
73 if {$A(iParentId)==$iPar} {
74 set txt $A(zExplain)
75 if {$A(nCycle)>=0} {
76 append txt " (nCycle=$A(nCycle))"
77 }
78 append res "[string repeat - $nIndent]$txt\n"
79 append res [get_eqp_graph $stmt $A(iSelectId) [expr $nIndent+2]]
80 }
81 }
82 set res
83}
84
85proc get_graph {stmt} {
86 set nCycle [get_cycles $stmt]
87 set res "QUERY (nCycle=$nCycle)\n"
88 append res [get_eqp_graph $stmt 0 2]
89}
90
91proc do_graph_test {tn sql res} {
92 db eval $sql
93 set stmt [db version -last-stmt-ptr]
94 set graph [string trim [get_graph $stmt]]
95
96 set graph [regsub -all {nCycle=[0-9]+} $graph nCycle=nnn]
97 uplevel [list do_test $tn [list set {} $graph] [string trim $res]]
98}
99
100proc puts_graph {sql} {
101 db eval $sql
102 set stmt [db version -last-stmt-ptr]
103 puts [string trim [get_graph $stmt]]
104}
105
106proc puts_debug_info {sql} {
107 db eval $sql
108 set stmt [db version -last-stmt-ptr]
109 foreach_scan X $stmt {
110 puts -nonewline "$X(debug_explain) $X(zExplain): "
111 puts -nonewline "loop=$X(debug_loop) visit=$X(debug_visit) "
112 puts "csr=$X(debug_csr) range=$X(debug_range)"
113 } 1
114}
115
116do_zexplain_test 0 1.1 {
117 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
118} {
119 {SCAN t2}
120 {SCAN t1}
121}
122do_zexplain_test 1 1.2 {
123 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
124} {
125 {SCAN t2}
126 {CORRELATED SCALAR SUBQUERY 1}
127 {SCAN t1}
128}
129
130do_graph_test 1.3 {
131 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
132} {
133QUERY (nCycle=nnn)
134--SCAN t2 (nCycle=nnn)
135--CORRELATED SCALAR SUBQUERY 1 (nCycle=nnn)
136----SCAN t1 (nCycle=nnn)
137}
138
139do_graph_test 1.4 {
140 WITH v2(x,y) AS MATERIALIZED (
141 SELECT x,y FROM t2
142 )
143 SELECT * FROM t1, v2 ORDER BY y;
144} {
145QUERY (nCycle=nnn)
146--MATERIALIZE v2 (nCycle=nnn)
147----SCAN t2 (nCycle=nnn)
148--SCAN t1 (nCycle=nnn)
149--SCAN v2 (nCycle=nnn)
150--USE TEMP B-TREE FOR ORDER BY (nCycle=nnn)
151}
152
153#-------------------------------------------------------------------------
154ifcapable fts5 {
155 reset_db
156 sqlite3_db_config db STMT_SCANSTATUS 1
157 do_execsql_test 2.0 {
158 CREATE VIRTUAL TABLE ft USING fts5(a);
159 INSERT INTO ft VALUES('abc');
160 INSERT INTO ft VALUES('def');
161 INSERT INTO ft VALUES('ghi');
162 }
163
164 do_graph_test 2.1 {
165 SELECT * FROM ft('def')
166 } {
167QUERY (nCycle=nnn)
168--SCAN ft VIRTUAL TABLE INDEX 0:M1 (nCycle=nnn)
169 }
170}
171
172#-------------------------------------------------------------------------
173reset_db
174sqlite3_db_config db STMT_SCANSTATUS 1
175do_execsql_test 3.0 {
176 CREATE TABLE x1(a, b);
177 CREATE TABLE x2(c, d);
178
179 WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000)
180 INSERT INTO x1 SELECT i, i FROM s;
181 INSERT INTO x2 SELECT a, b FROM x1;
182}
183
184do_graph_test 2.1 {
185 SELECT * FROM x1, x2 WHERE c=+a;
186} {
187QUERY (nCycle=nnn)
188--SCAN x1 (nCycle=nnn)
189--CREATE AUTOMATIC INDEX ON x2(c, d) (nCycle=nnn)
190--BLOOM FILTER ON x2 (c=?)
191--SEARCH x2 USING AUTOMATIC COVERING INDEX (c=?) (nCycle=nnn)
192}
193
194#-------------------------------------------------------------------------
195reset_db
196sqlite3_db_config db STMT_SCANSTATUS 1
197do_execsql_test 4.0 {
198 CREATE TABLE rt1 (id INTEGER PRIMARY KEY, x1, x2);
199 CREATE TABLE rt2 (id, x1, x2);
200}
201
202do_graph_test 4.1 {
203 SELECT * FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
204} {
205QUERY (nCycle=nnn)
206--SCAN rt1 (nCycle=nnn)
207--CREATE AUTOMATIC INDEX ON rt2(x1, id, x2) (nCycle=nnn)
208--BLOOM FILTER ON rt2 (x1=?)
209--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
210}
211
212do_graph_test 4.2 {
213 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
214} {
215QUERY (nCycle=nnn)
216--SCAN rt1 (nCycle=nnn)
217--CREATE AUTOMATIC INDEX ON rt2(x1, id) (nCycle=nnn)
218--BLOOM FILTER ON rt2 (x1=?)
219--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
220}
221
222do_graph_test 4.3 {
223 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND (rt2.x1+1)=(rt1.x1+1);
224} {
225QUERY (nCycle=nnn)
226--SCAN rt1 (nCycle=nnn)
227--SCAN rt2 (nCycle=nnn)
228}
229
230do_graph_test 4.4 {
231 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=(rt1.x1+1) AND rt2.id>5;
232} {
233QUERY (nCycle=nnn)
234--SCAN rt1 (nCycle=nnn)
235--CREATE AUTOMATIC INDEX ON rt2(x1, id) WHERE <expr> (nCycle=nnn)
236--BLOOM FILTER ON rt2 (x1=?)
237--SEARCH rt2 USING AUTOMATIC PARTIAL COVERING INDEX (x1=?) (nCycle=nnn)
238}
239
240do_graph_test 4.5 {
241 SELECT v1.cnt FROM rt1, (
242 SELECT count(*) AS cnt, rt2.x1 AS x1 FROM rt2 GROUP BY x1
243 ) AS v1 WHERE rt1.x1=v1.x1
244} {
245QUERY (nCycle=nnn)
246--CO-ROUTINE v1
247----SCAN rt2 (nCycle=nnn)
248----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn)
249--SCAN rt1 (nCycle=nnn)
250--CREATE AUTOMATIC INDEX ON v1(x1, cnt) (nCycle=nnn)
251--BLOOM FILTER ON v1 (x1=?)
252--SEARCH v1 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
253}
254
255#-------------------------------------------------------------------------
256reset_db
257
258if {$::tcl_platform(platform) ne "windows"} {
259 # Disabled on Windows. Bug 2026-08-19T20:22:44Z
260 ifcapable trace {
261 do_execsql_test 5.0 {
262 CREATE TABLE t1(x, y);
263 CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
264 SELECT 1;
265 END;
266 INSERT INTO t1 VALUES(1, 2);
267 }
268
269 proc trace {stmt sql} {
270 set ptr [format "ptr:0x%llx" $stmt]
271 array set A [sqlite3_stmt_scanstatus -flags complex $ptr 0]
272 lappend ::trace_explain $A(zExplain)
273 }
274 db trace_v2 trace
275
276 set ::trace_explain [list]
277 set ::delstmt [sqlite3_prepare db "DELETE FROM t1 WHERE x=1" -1 TAIL]
278 do_test 5.1 {
279 sqlite3_step $::delstmt
280 sqlite3_finalize $::delstmt
281 } {SQLITE_OK}
282
283 do_test 5.2 {
284 set ::trace_explain
285 } {{SCAN t1} {SCAN t1} {SCAN t1}}
286 }
287}
288
289#-------------------------------------------------------------------------
290reset_db
291sqlite3_db_config db STMT_SCANSTATUS 1
292
293do_execsql_test 6.0 {
294 CREATE TABLE t1(a, b);
295 INSERT INTO t1 VALUES(1, 'one');
296 INSERT INTO t1 VALUES(2, 'two');
297 INSERT INTO t1 VALUES(3, 'three');
298 INSERT INTO t1 VALUES(4, 'four');
299 INSERT INTO t1 VALUES(5, 'five');
300 INSERT INTO t1 VALUES(6, 'six');
301 INSERT INTO t1 VALUES(7, 'seven');
302 INSERT INTO t1 VALUES(8, 'eight');
303}
304
305do_graph_test 6.1 {
306 SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1
307} {
308QUERY (nCycle=nnn)
309--SCAN t1 (nCycle=nnn)
310--USE TEMP B-TREE FOR GROUP BY (nCycle=nnn)
311}
312
313set sql {
314 WITH xy(x, y) AS ( SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1)
315 SELECT * FROM xy WHERE x=1
316}
317do_graph_test 6.2 $sql {
318QUERY (nCycle=nnn)
319--CO-ROUTINE xy
320----SCAN t1 (nCycle=nnn)
321----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn)
322--SCAN xy (nCycle=nnn)
323}
324
325do_graph_test 6.3 {
326 WITH xy(x, y) AS ( SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1)
327 SELECT * FROM xy, xy AS xy2
328} {
329QUERY (nCycle=nnn)
330--MATERIALIZE xy (nCycle=nnn)
331----SCAN t1 (nCycle=nnn)
332----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn)
333--SCAN xy (nCycle=nnn)
334--SCAN xy2 (nCycle=nnn)
335}
336
337#-------------------------------------------------------------------------
338reset_db
339
340# Check that an OOB parameter (45) does not cause asan or valgrind errors.
341#
342do_test 7.0 {
343 db eval {SELECT * FROM sqlite_schema}
344 set stmt [db version -last-stmt-ptr]
345 sqlite3_stmt_scanstatus -flags complex $stmt 1000000
346} {}
347
348#explain_i { SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1 }
349#puts_debug_info { SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1 }
350
351finish_test