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 / like4.test

251 lines · 6637 bytes Raw Blame History
1# 2026-10-15
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# This file implements regression tests for SQLite library.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix like4
18
19ifcapable !like_match_blobs {
20 finish_test
21 return
22}
23
24do_execsql_test 1.0 {
25 CREATE TABLE t0(a, c);
26 INSERT INTO t0 VALUES(1, 'A');
27 INSERT INTO t0 VALUES(2, 'A');
28 INSERT INTO t0 VALUES(1, X'41');
29 INSERT INTO t0 VALUES(2, X'41');
30}
31
32foreach {tn idx} {
33 1 ""
34 2 "CREATE INDEX idx ON t0(a, c)"
35 3 "CREATE INDEX idx ON t0(c, a)"
36} {
37
38 execsql { DROP INDEX IF EXISTS idx }
39 execsql $idx
40
41 do_execsql_test 1.$tn.1 {
42 SELECT a, count(*) FROM t0 WHERE a IN (1,2) AND c GLOB 'A' GROUP BY a;
43 } {1 2 2 2}
44
45 do_execsql_test 1.$tn.2 {
46 SELECT a, quote(c) FROM t0 WHERE a IN (1,2) AND c GLOB 'A' ORDER BY a;
47 } {
48 1 'A'
49 1 X'41'
50 2 'A'
51 2 X'41'
52 }
53}
54
55#-------------------------------------------------------------------------
56reset_db
57
58do_execsql_test 2.0 {
59 CREATE TABLE t1(a, b);
60 CREATE INDEX t1b ON t1(b);
61
62 INSERT INTO t1 VALUES(1, 'abc');
63 INSERT INTO t1 VALUES(2, X'616263');
64 INSERT INTO t1 VALUES(3, 'abd');
65 INSERT INTO t1 VALUES(4, X'616264');
66}
67
68do_execsql_test 2.1 {
69 SELECT a FROM t1 WHERE b GLOB 'ab*' ORDER BY b
70} {1 3 2 4}
71do_execsql_test 2.2 {
72 SELECT a FROM t1 WHERE b GLOB 'ab*' ORDER BY b DESC
73} {4 2 3 1}
74
75#-------------------------------------------------------------------------
76reset_db
77
78do_execsql_test 3.0 {
79 CREATE TABLE t(a, c);
80 CREATE INDEX ti ON t(a, c);
81 WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<100)
82 INSERT INTO t SELECT i%3, 'x'||i FROM n;
83 INSERT INTO t VALUES(2, 'A'), (1, x'41');
84}
85
86do_execsql_test 3.1 {
87 SELECT a, quote(c) FROM t WHERE c GLOB 'A' ORDER BY a;
88} {
89 1 X'41'
90 2 'A'
91}
92
93do_execsql_test 3.2 {
94 ANALYZE;
95}
96
97do_execsql_test 3.3 {
98 SELECT a, quote(c) FROM t WHERE c GLOB 'A' ORDER BY a;
99} {
100 1 X'41'
101 2 'A'
102}
103
104#-------------------------------------------------------------------------
105# Skip-scan on combined with IN(...) and the LIKE optimization.
106#
107reset_db
108
109do_execsql_test 4.0 {
110 CREATE TABLE t(a, b, c);
111 CREATE INDEX ti ON t(a, b, c);
112 WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<300)
113 INSERT INTO t SELECT i%3, i%2, 'x'||i FROM n;
114 INSERT INTO t VALUES(2, 0, 'A'), (2, 1, x'41');
115 INSERT INTO t VALUES(1, 1, 'A'), (1, 0, x'41');
116 INSERT INTO t VALUES(0, 1, 'A');
117 ANALYZE;
118}
119
120do_eqp_test 4.1 {
121 SELECT a, b, quote(c) FROM t WHERE b IN (0,1) AND c GLOB 'A' ORDER BY a, b;
122} {SEARCH t USING COVERING INDEX ti (ANY(a) AND b=? AND c>? AND c<?)}
123
124do_execsql_test 4.2 {
125 SELECT a, b, quote(c) FROM t WHERE b IN (0,1) AND c GLOB 'A' ORDER BY a, b;
126} {
127 0 1 'A'
128 1 0 X'41'
129 1 1 'A'
130 2 0 'A'
131 2 1 X'41'
132}
133
134do_execsql_test 4.3 {
135 SELECT a, b, quote(c) FROM t WHERE b IN (0,1) AND c GLOB 'A'
136 ORDER BY a DESC, b DESC;
137} {
138 2 1 X'41'
139 2 0 'A'
140 1 1 'A'
141 1 0 X'41'
142 0 1 'A'
143}
144
145do_execsql_test 4.4 {
146 SELECT a, count(*) FROM t WHERE b IN (0,1) AND c GLOB 'A' GROUP BY a;
147} {0 1 1 2 2 2}
148
149# Mixed ASC and DESC.
150#
151foreach {tn idx nosort} {
152 1 "a DESC, b, c" {"a, b DESC" "a DESC, b" "a DESC, b, c"
153 "a, b DESC, c DESC"}
154 2 "a, b DESC, c" {"a, b DESC" "a DESC, b"}
155 3 "a, b, c DESC" {"a, b, c DESC" "a DESC, b DESC, c"}
156 4 "a DESC, b DESC, c" {"a, b, c DESC" "a DESC, b DESC, c"}
157} {
158 execsql "
159 DROP INDEX ti;
160 CREATE INDEX ti ON t($idx);
161 ANALYZE;
162 "
163
164 foreach {tn2 ob res} {
165 1 "a, b DESC" {0 1 'A' 1 1 'A' 1 0 X'41' 2 1 X'41' 2 0 'A'}
166 2 "a DESC, b" {2 0 'A' 2 1 X'41' 1 0 X'41' 1 1 'A' 0 1 'A'}
167 3 "a, b, c DESC" {0 1 'A' 1 0 X'41' 1 1 'A' 2 0 'A' 2 1 X'41'}
168 4 "a DESC, b, c" {2 0 'A' 2 1 X'41' 1 0 X'41' 1 1 'A' 0 1 'A'}
169 5 "a, b DESC, c DESC" {0 1 'A' 1 1 'A' 1 0 X'41' 2 1 X'41' 2 0 'A'}
170 6 "a DESC, b DESC, c" {2 1 X'41' 2 0 'A' 1 1 'A' 1 0 X'41' 0 1 'A'}
171 } {
172 do_execsql_test 4.5.$tn.$tn2 "
173 SELECT a, b, quote(c) FROM t WHERE b IN (0,1) AND c GLOB 'A'
174 ORDER BY $ob
175 " $res
176 }
177}
178
179#-------------------------------------------------------------------------
180# LIKE optimization on the right-hand table of a RIGHT JOIN.
181#
182reset_db
183
184do_execsql_test 5.0 {
185 CREATE TABLE t1(a, b);
186 CREATE TABLE t2(x, y);
187 CREATE INDEX t2xy ON t2(x, y);
188 INSERT INTO t1 VALUES(1, 'one'), (3, 'three');
189 INSERT INTO t2 VALUES(1, 'Ax'), (1, x'4179'), (1, 'Bz');
190 INSERT INTO t2 VALUES(3, x'417a'), (3, 'Az');
191 INSERT INTO t2 VALUES(4, 'Aq'), (4, x'4171');
192}
193
194do_eqp_test 5.1 {
195 SELECT b, x, quote(y) FROM t1 RIGHT JOIN t2 ON a=x AND y GLOB 'A*'
196 ORDER BY x, y
197} {
198 QUERY PLAN
199 |--SCAN t1
200 |--SEARCH t2 USING INDEX t2xy (x=? AND y>? AND y<?)
201 |--RIGHT-JOIN t2
202 | `--SCAN t2
203 `--USE TEMP B-TREE FOR ORDER BY
204}
205
206do_execsql_test 5.2 {
207 SELECT b, x, quote(y) FROM t1 RIGHT JOIN t2 ON a=x AND y GLOB 'A*'
208 ORDER BY x, y
209} {
210 one 1 'Ax' {} 1 'Bz' one 1 X'4179'
211 three 3 'Az' three 3 X'417A'
212 {} 4 'Aq' {} 4 X'4171'
213}
214
215# Followup post: https://sqlite.org/bugs/forumpost/70e3174f54
216# on 2026-10-27T15:04:02Z
217#
218do_execsql_test 6.0 {
219 DROP TABLE t1;
220 DROP TABLE t2;
221 CREATE TABLE t1(a, b);
222 CREATE INDEX t1ab ON t1(a, b);
223 CREATE TABLE t2(x);
224 INSERT INTO t2 VALUES(1);
225 SELECT 1 FROM t2 LEFT JOIN t1 ON t1.b GLOB 'a*' AND t1.a = NULL;
226} 1
227
228# Bug 2026-10-28T05:04:51Z
229# https://sqlite.org/bugs/info/e85ab73aa1483325
230#
231# LIKE/GLOB optimization on an indexed non-TEXT column misses REAL
232# values whose text starts with an incomplete exponent
233#
234do_execsql_test 7.0 {
235 CREATE TABLE t7(x REAL); CREATE INDEX t7x ON t7(x);
236 INSERT INTO t7 VALUES(1e20),(1.5e-7),(9e999),(-9e999),('text');
237 SELECT count(x) FROM t7 WHERE x GLOB '1.0e*';
238} {1}
239do_execsql_test 7.1 { SELECT count(x) FROM t7 WHERE x GLOB '1.5e-*' } {1}
240do_execsql_test 7.2 { SELECT x FROM t7 WHERE x GLOB 'In*' } {Inf}
241do_execsql_test 7.3 { SELECT x FROM t7 WHERE x GLOB '-I*' } {-Inf}
242do_execsql_test 7.4 { SELECT x FROM t7 WHERE x GLOB 'INF*' } {}
243do_execsql_test 7.5 { SELECT x FROM t7 WHERE x GLOB '+Inf*' } {}
244do_execsql_test 7.6 { SELECT x FROM t7 WHERE x GLOB '-iNf*' } {}
245do_execsql_test 7.7 {
246 CREATE TABLE t7c(x REAL COLLATE NOCASE); CREATE INDEX t7cx ON t7c(x);
247 INSERT INTO t7c SELECT x FROM t7;
248 SELECT x FROM t7c WHERE x LIKE 'in%';
249} {Inf}
250
251finish_test