3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to verify that the enhancement
14 # request documented by ticket 99378177930f87bd is working.
16 # The enhancement is that if an aggregate query with a GROUP BY clause
17 # uses subexpressions in the arguments to aggregate functions that are
18 # also columns of an index, then the values are pulled from the index
19 # rather than being recomputed. This has the potential to make some
20 # indexed queries works as if the index were covering.
23 set testdir [file dirname $argv0]
24 source $testdir/tester.tcl
26 do_execsql_test tkt-99378-100 {
27 CREATE TABLE t1(a INT, b TEXT, c INT, d INT);
28 INSERT INTO t1(a,b,c,d) VALUES
29 (1, '{"x":1}', 12, 3),
31 (1, '{"x":1}', 6, 11),
32 (2, '{"x":1}', 22, 3),
35 CREATE INDEX t1x ON t1(d, a, b->>'x', c);
37 do_execsql_test tkt-99378-110 {
40 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
42 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
44 WHERE d BETWEEN 0 and 10
52 # The proof that the index on the expression is being used is in the
53 # fact that the byte code contains no "Function" opcodes. In other words,
54 # the ->> operator (which is implemented by a function) is never invoked.
55 # Instead, the b->>'x' value is pulled out of the index.
57 do_execsql_test tkt-99378-120 {
61 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
63 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
65 WHERE d BETWEEN 0 and 10
70 do_execsql_test tkt-99378-130 {
73 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
75 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
77 WHERE d BETWEEN 0 and 10
84 do_execsql_test tkt-99378-140 {
88 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
90 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
92 WHERE d BETWEEN 0 and 10
96 do_execsql_test tkt-99378-200 {
98 CREATE INDEX t1x ON t1(a, d, b->>'x', c);
100 do_execsql_test tkt-99378-210 {
103 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
105 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
107 WHERE d BETWEEN 0 and 10
114 do_execsql_test tkt-99378-220 {
118 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
120 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
122 WHERE d BETWEEN 0 and 10
125 do_execsql_test tkt-99378-230 {
128 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
130 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
132 WHERE d BETWEEN 0 and 10
139 do_execsql_test tkt-99378-240 {
143 SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2,
145 SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4
147 WHERE d BETWEEN 0 and 10
151 # 2022-12-20 dbsqlfuzz a644e70d7683a7ca59c71861a153c1dccf8850b9
153 do_execsql_test tkt-99378-300 {
154 DROP TABLE IF EXISTS t1;
155 CREATE TABLE t1(a INT);
156 CREATE INDEX i1 ON t1(a,a=a);
157 INSERT INTO t1 VALUES(1),(2),(3),(4);
158 SELECT * FROM t1 NATURAL JOIN t1
162 (SELECT sum((SELECT 1 FROM t1 NATURAL RIGHT JOIN t1 WHERE a=a)))) AS xyz
167 do_execsql_test tkt-99378-310 {
169 SELECT * FROM t1 NATURAL JOIN t1
173 (SELECT sum((SELECT 1 FROM t1 NATURAL RIGHT JOIN t1 WHERE a=a)))) AS xyz
179 # 2023-03-04 https://sqlite.org/forum/forumpost/a68313d054
181 # See also indexexpr1-2200 added on 2023-03-18.
183 do_execsql_test tkt-99378-400 {
186 INSERT INTO t0(w) VALUES(1);
188 INSERT INTO t1(x) VALUES(1);
189 CREATE INDEX t1x ON t1(x > 0);
190 CREATE VIEW t2(y) AS SELECT avg(w) FROM t0 GROUP BY w>1;
191 CREATE VIEW t3(z) AS SELECT count(*) FROM t2 WHERE y BETWEEN 0 and 0;
192 SELECT count(*) FROM t1 NOT INDEXED WHERE (SELECT z FROM t3);
193 SELECT count(*) FROM t1 INDEXED BY t1x WHERE (SELECT z FROM t3);