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 / misc01.sql

422 lines · 12337 bytes Raw Blame History
1# 7c7dc3813e784515 2026-11-05T18:09:41Z
2# OP_SeekRowid converts a key that has both a number and its text from
3# the text: assertion, and the wrong row with a short fp_d
4# Alperen Keles
5#
6.open :memory:
7.mode -reset list
8.testcase 100
9CREATE TABLE t(a);
10INSERT INTO t(rowid, a) VALUES
11 (12340,'row 12340'),
12 (12345,'row 12345'),
13 (12350,'row 12350');
14.dbconfig fp_digits 4
15.check 'fp_digits 4'
16
17.testcase 110
18SELECT a FROM t WHERE rowid = 12345.0;
19.check 'row 12345'
20
21# The sqlar_uncompress() function has the special property
22# below that the sqlite3_value it returns has both a text
23# and an equivalent numeric representation at the same time.
24#
25.testcase 120
26SELECT a FROM t WHERE rowid = sqlar_uncompress(12345.0, 9);
27.check 'row 12345'
28.testcase 121
29SELECT a FROM t WHERE rowid = dualrep(12345);
30.check 'row 12345'
31.testcase 122
32SELECT a FROM t WHERE rowid = dualrep(12345.0);
33.check 'row 12345'
34
35.testcase 130
36SELECT a FROM t WHERE +rowid = sqlar_uncompress(12345.0, 9);
37.check 'row 12345'
38.testcase 131
39SELECT a FROM t WHERE +rowid = dualrep(12345);
40.check 'row 12345'
41.testcase 132
42SELECT a FROM t WHERE +rowid = dualrep(12345.0);
43.check 'row 12345'
44
45# 45bcbf3580 2026-11-05T18:08:21Z
46# A function result that carries a number and its text keeps
47# the old text after REAL affinity.
48#
49.testcase 200
50.mode quote
51CREATE TABLE r(x REAL);
52INSERT INTO r VALUES(dualrep(5)) RETURNING x || '';
53.check "'5.0'"
54
55.testcase 300
56PRAGMA foreign_keys=ON;
57CREATE TABLE p(k TEXT UNIQUE);
58INSERT INTO p VALUES('5.0');
59CREATE TABLE c(x REAL REFERENCES p(k));
60INSERT INTO c VALUES(5);
61.check ''
62.testcase 301
63INSERT INTO c VALUES(dualrep(5));
64.check ''
65.testcase 302
66SELECT count(*) FROM c;
67.check 2
68
69# f945de7c4eb73732 2026-11-06T04:16:54Z
70# sqlite3's .recover output can smuggle shell commands into the next
71# sqlite3 run
72#
73.if capable('vtab')
74.testcase 400
75.open --hexdb
76| size 8192 pagesize 4096 filename unk.db
77| page 1 offset 0
78| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
79| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 02 .....@ ........
80| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................
81| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................
82| 96: 00 00 00 00 0d 00 00 00 01 0f ca 00 0f ca 00 00 ................
83| 4032: 00 00 00 00 00 00 00 00 00 00 34 01 06 17 11 11 ..........4.....
84| 4048: 01 55 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .Utablet1t1.CREA
85| 4064: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c TE TABLE t1(a,b,
86| 4080: 63 29 3b 0a 2e 70 72 69 6e 74 20 68 65 6c 6c 6f c);..print hello
87| page 2 offset 4096
88| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................
89| end unk.db
90.recover
91.check <<**END-TEST**
92.dbconfig defensive off
93.safe
94BEGIN;
95PRAGMA writable_schema = on;
96PRAGMA foreign_keys = off;
97PRAGMA encoding = 'UTF-8';
98PRAGMA page_size = '4096';
99PRAGMA auto_vacuum = '0';
100PRAGMA user_version = '0';
101PRAGMA application_id = '0';
102CREATE TABLE t1(a,b,c);
103PRAGMA writable_schema = off;
104COMMIT;
105**END-TEST**
106.testcase 401
107.recover --unsafe
108.check <<**END-TEST**
109.dbconfig defensive off
110BEGIN;
111PRAGMA writable_schema = on;
112PRAGMA foreign_keys = off;
113PRAGMA encoding = 'UTF-8';
114PRAGMA page_size = '4096';
115PRAGMA auto_vacuum = '0';
116PRAGMA user_version = '0';
117PRAGMA application_id = '0';
118CREATE TABLE t1(a,b,c);
119PRAGMA writable_schema = off;
120COMMIT;
121**END-TEST**
122.endif /* capable('vtab') */
123
124# Follow-up to f945de7c4eb73732. The ".recover" command must also omit
125# extraneous text at the end of the sqlite_schema.sql field for views,
126# triggers, and non-unique indexes, not just for tables.
127#
128.open :memory:
129CREATE TABLE t1(a,b);
130CREATE INDEX i1 ON t1(a);
131CREATE VIEW v1 AS SELECT * FROM t1;
132CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
133.dbconfig defensive off
134.dbconfig writable_schema on
135UPDATE sqlite_schema
136 SET sql=sql||';'||char(10)||'.print INJECTED_'||name
137 WHERE name IN ('i1','v1','r1');
138.testcase 410
139.recover
140.check --exact <<END
141.dbconfig defensive off
142.safe
143BEGIN;
144PRAGMA writable_schema = on;
145PRAGMA foreign_keys = off;
146PRAGMA encoding = 'UTF-8';
147PRAGMA page_size = '4096';
148PRAGMA auto_vacuum = '0';
149PRAGMA user_version = '0';
150PRAGMA application_id = '0';
151CREATE TABLE t1(a,b);
152CREATE INDEX i1 ON t1(a);
153CREATE VIEW v1 AS SELECT * FROM t1;
154CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
155PRAGMA writable_schema = off;
156COMMIT;
157END
158
159# The ".dump" command must not allow text from a malformed schema (which is
160# included in an error message that is output as an SQL comment) to escape
161# from the comment. In this database the name of the only schema entry is
162# "x*/\n.print NAME_INJECTED\n/*" and its SQL is malformed. (The first
163# three lines of expected output are an error message on stderr.)
164#
165.testcase 420
166.open --hexdb
167| size 1024 pagesize 512 filename unk.db
168| page 1 offset 0
169| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
170| 16: 02 00 01 01 00 40 20 20 00 00 00 02 00 00 00 02 .....@ ........
171| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................
172| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................
173| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 ................
174| 96: 00 2e 9d 98 0d 00 00 00 01 01 c3 00 01 c3 00 00 ................
175| 448: 00 00 00 3b 01 06 17 43 11 01 31 74 61 62 6c 65 ...;...C..1table
176| 464: 78 2a 2f 0a 2e 70 72 69 6e 74 20 4e 41 4d 45 5f x*/..print NAME_
177| 480: 49 4e 4a 45 43 54 45 44 0a 2f 2a 74 31 02 43 52 INJECTED./*t1.CR
178| 496: 45 41 54 45 20 54 41 42 4c 45 20 6a 75 6e 6b 28 EATE TABLE junk(
179| page 2 offset 512
180| 0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................
181| end unk.db
182.dump
183.check --exact <<END
184sql error: malformed database schema (x*/
185.print NAME_INJECTED
186/*) (11)
187PRAGMA foreign_keys=OFF;
188BEGIN TRANSACTION;
189/****** CORRUPTION ERROR *******/
190/****** malformed database schema (x* .print NAME_INJECTED /*) ******/
191/****** ERROR: near "ORDER": syntax error ******/
192/**** ERROR: (11) malformed database schema (x* .print NAME_INJECTED /*) *****/
193COMMIT;
194END
195
196
197.if NOT capable('LIKE_DOESNT_MATCH_BLOBS')
198# 959866cfe4ec0e16 2026-11-06T00:25:13Z
199# Wrong result: c0 GLOB 'ab?' OR c0 GLOB 'ab*' on an indexed column
200# omits BLOB rows
201#
202.open :memory:
203.mode -reset qbox
204.testcase 500
205CREATE TABLE t0(c0, c1);
206INSERT INTO t0 VALUES('ab',1),('abc',2),('abd',3),(x'6162',4),
207 (x'616263',5),('b',6);
208CREATE INDEX i0 ON t0(c0);
209.check ''
210
211.testcase 510
212SELECT c1, c0 FROM t0
213 WHERE c0 GLOB 'ab*'
214 ORDER BY c1;
215.check <<*END*
216╭────┬───────────╮
217│ c1 │ c0 │
218╞════╪═══════════╡
219│ 1 │ 'ab' │
220│ 2 │ 'abc' │
221│ 3 │ 'abd' │
222│ 4 │ x'6162' │
223│ 5 │ x'616263' │
224╰────┴───────────╯
225*END*
226
227.testcase 520
228SELECT c1, c0 FROM t0
229 WHERE c0 GLOB 'ab?' OR c0 GLOB 'ab*'
230 ORDER BY c1;
231.check <<*END*
232╭────┬───────────╮
233│ c1 │ c0 │
234╞════╪═══════════╡
235│ 1 │ 'ab' │
236│ 2 │ 'abc' │
237│ 3 │ 'abd' │
238│ 4 │ x'6162' │
239│ 5 │ x'616263' │
240╰────┴───────────╯
241*END*
242
243.testcase 530
244SELECT c1, c0 FROM t0
245 WHERE c0 GLOB 'ab*' OR c0 GLOB 'ab*'
246 ORDER BY c1;
247.check <<*END*
248╭────┬───────────╮
249│ c1 │ c0 │
250╞════╪═══════════╡
251│ 1 │ 'ab' │
252│ 2 │ 'abc' │
253│ 3 │ 'abd' │
254│ 4 │ x'6162' │
255│ 5 │ x'616263' │
256╰────┴───────────╯
257*END*
258
259.testcase 540
260SELECT c1, c0 FROM t0 NOT INDEXED
261 WHERE c0 GLOB 'ab?' OR c0 GLOB 'ab*'
262 ORDER BY c1;
263.check <<*END*
264╭────┬───────────╮
265│ c1 │ c0 │
266╞════╪═══════════╡
267│ 1 │ 'ab' │
268│ 2 │ 'abc' │
269│ 3 │ 'abd' │
270│ 4 │ x'6162' │
271│ 5 │ x'616263' │
272╰────┴───────────╯
273*END*
274.endif /* NOT capable('LIKE_DOESNT_MATCH_BLOBS') */
275
276# db0bb9d415ffc8f0 2026-11-05T22:31:13Z
277# generate_series(0,32,5) ORDER BY rowid DESC LIMIT 3 returns 10, 5, 0
278# instead of 30, 25, 20
279#
280.if capable('vtab');
281.open :memory:
282.mode -reset quote
283.testcase 600
284SELECT value FROM generate_series(0,32,5) ORDER BY rowid DESC;
285.check '30 25 20 15 10 5 0'
286
287.testcase 610
288SELECT value FROM generate_series(0,32,5) ORDER BY rowid DESC LIMIT 3;
289.check '30 25 20'
290
291.testcase 620
292SELECT value FROM generate_series(0,32,5) ORDER BY value DESC LIMIT 3;
293.check '30 25 20'
294
295.testcase 630
296SELECT rowid, value FROM generate_series(0,12,5);
297.check '0,0 5,5 10,10'
298.endif /* capable('vtab') */
299
300# b4a8844d427ec7ef 2026-11-04T20:30:26Z
301# Indexed GLOB with a prefix ending in U+07FF returns non-matching rows
302# in a UTF-16be
303#
304.open :memory:
305.mode -reset quote -quote off
306.testcase 700
307PRAGMA encoding='UTF-16be';
308CREATE TABLE t1(id INT, x TEXT);
309INSERT INTO t1 VALUES
310 (1, 'a'||char(0x7FF)),
311 (2, 'a'||char(0x7FF)||'z'),
312 (3, 'a'||char(0x800)),
313 (4, 'a'||char(0x4E00)),
314 (5, 'a'||char(0xFFFE)),
315 (6, 'a'||char(0xFFFE)||'z'),
316 (7, 'a'||char(0xFFFF)),
317 (8, 'a'||char(0xFFFF)||'z'),
318 (9, 'a'||char(0x10000));
319CREATE INDEX t1x ON t1(x);
320PRAGMA encoding;
321.check 'UTF-16be'
322
323.param set :p1 "'a'||char(0x7FF)||'*'"
324.param set :p2 "'a'||char(0xFFFE)||'*'"
325.param set :p3 "'a'||char(0xFFFF)||'*'"
326
327.testcase 710
328SELECT id, hex(x) FROM t1 WHERE x GLOB :p1;
329.check -glob '1,006107FF 2,006107FF007A'
330.testcase 711
331EXPLAIN QUERY PLAN
332SELECT id, hex(x) FROM t1 WHERE x GLOB :p1;
333.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'
334
335.testcase 720
336SELECT id, hex(x) FROM t1 WHERE +x GLOB :p1;
337.check '1,006107FF 2,006107FF007A'
338
339.testcase 730
340SELECT id, hex(x) FROM t1 WHERE x GLOB :p2;
341.check '5 0061FFFE 6 0061FFFE007A'
342.testcase 731
343EXPLAIN QUERY PLAN
344SELECT id, hex(x) FROM t1 WHERE x GLOB :p2;
345.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'
346
347.testcase 740
348SELECT id, hex(x) FROM t1 WHERE +x GLOB :p2;
349.check '5 0061FFFE 6 0061FFFE007A'
350
351.testcase 750
352SELECT id, hex(x) FROM t1 WHERE x GLOB :p3;
353.check '7 0061FFFF 8 0061FFFF007A'
354.testcase 751
355EXPLAIN QUERY PLAN
356SELECT id, hex(x) FROM t1 WHERE x GLOB :p3;
357.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'
358
359.testcase 760
360SELECT id, hex(x) FROM t1 WHERE +x GLOB :p3;
361.check '7 0061FFFF 8 0061FFFF007A'
362
363# Forum https://sqlite.org/forum/forumpost/c582aea277 2026-11-07T10:10:46Z
364# Incorrect FULL JOIN Result.
365# (Despite the title, the problem is actually subtype propagation through
366# CASE statements.)
367#
368.open :memory:
369.mode -reset quote -quote off
370.testcase 800
371CREATE TABLE t2(a TEXT);
372CREATE TABLE t0(a TEXT);
373INSERT INTO t2 VALUES('[]');
374INSERT INTO t0 VALUES('[]');
375CREATE INDEX i ON t2(json(a));
376CREATE VIEW v2(x) AS SELECT 1 WHERE 0;
377.check ''
378.testcase 810
379SELECT json_array(CASE WHEN 1 THEN json(t0.a) END)
380FROM t2 FULL OUTER JOIN t0
381 ON json_array(CASE WHEN 1 THEN json(t2.a) END)
382 = json_array(CASE WHEN 1 THEN json(t0.a) END);
383.check '[[]]'
384.testcase 820
385.testctrl opt -All -q
386SELECT json_array(CASE WHEN 1 THEN json(t0.a) END)
387FROM t2 FULL OUTER JOIN t0
388 ON json_array(CASE WHEN 1 THEN json(t2.a) END)
389 = json_array(CASE WHEN 1 THEN json(t0.a) END);
390.check '[[]]'
391
392# Bug thread https://sqlite.org/bugs/info/a548aa9d4d (2026-11-05T22:09:59Z)
393# was closed as NOT PLANNED. These tests merely document the current
394# behavior. The complaint from Claude was that the error message at
395# test 901 was a bit wonky.
396#
397.open :memory:
398.mode -reset quote -quote relaxed
399.testcase 900
400CREATE TABLE t1(x,y);
401CREATE INDEX t1x ON t1(x);
402CREATE VIEW t2 AS SELECT x+y FROM t1;
403CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 'hello!'; END;
404.check ''
405.testcase 901
406CREATE TABLE main.IF(a);
407.check --glob '*syntax error*'
408.testcase 902
409SELECT name FROM sqlite_schema ORDER BY name;
410.check 'r1 t1 t1x t2'
411.testcase 903
412PRAGMA integrity_check;
413.check 'ok'
414.testcase 910
415CREATE TABLE main."IF"(a);
416.check ''
417.testcase 911
418SELECT group_concat(name, ' ' ORDER BY name) FROM sqlite_schema;
419.check 'IF r1 t1 t1x t2'
420.testcase 912
421PRAGMA integrity_check;
422.check 'ok'