summaryrefslogtreecommitdiff
path: root/test/misc01.sql
blob: 120591c8a753756c6a6f82df057c4dc0a7eef073 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# 2026-11-20
#
# ".dbconfig fp_digits" no longer works.  ".testctrl fp_digits" is
# available, but is read-only without the --unsafe-testing flag, which
# this invocation is not suppose to have.
#
.testcase 100
.dbconfig fp_digits
.check --glob unknown dbconfig
.testcase 110
.testctrl fp_digits
.check 17
.if NOT capable('unsafe-testing')
.testcase 120
.testctrl fp_digits 4
.check -glob fp_digits is read-only
.endif /* NOT capable('unsafe-testing') */

# 45bcbf3580 2026-11-05T18:08:21Z
# A function result that carries a number and its text keeps
# the old text after REAL affinity.
#
.testcase 200
.mode quote
CREATE TABLE r(x REAL);
INSERT INTO r VALUES(dualrep(5)) RETURNING x || '';
.check "'5.0'"

.testcase 300
PRAGMA foreign_keys=ON;
CREATE TABLE p(k TEXT UNIQUE);
INSERT INTO p VALUES('5.0');
CREATE TABLE c(x REAL REFERENCES p(k));
INSERT INTO c VALUES(5);
.check ''
.testcase 301
INSERT INTO c VALUES(dualrep(5));
.check ''
.testcase 302
SELECT count(*) FROM c;
.check 2

# f945de7c4eb73732 2026-11-06T04:16:54Z
# sqlite3's .recover output can smuggle shell commands into the next
# sqlite3 run
#
.if capable('vtab')
.testcase 400
.open --hexdb
| size 8192 pagesize 4096 filename unk.db
| page 1 offset 0
|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 02   .....@  ........
|     32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04   ................
|     48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00   ................
|     96: 00 00 00 00 0d 00 00 00 01 0f ca 00 0f ca 00 00   ................
|   4032: 00 00 00 00 00 00 00 00 00 00 34 01 06 17 11 11   ..........4.....
|   4048: 01 55 74 61 62 6c 65 74 31 74 31 02 43 52 45 41   .Utablet1t1.CREA
|   4064: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c   TE TABLE t1(a,b,
|   4080: 63 29 3b 0a 2e 70 72 69 6e 74 20 68 65 6c 6c 6f   c);..print hello
| page 2 offset 4096
|      0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00   ................
| end unk.db
.recover
.check <<**END-TEST**
.dbconfig defensive off
.safe
BEGIN;
PRAGMA writable_schema = on;
PRAGMA foreign_keys = off;
PRAGMA encoding = 'UTF-8';
PRAGMA page_size = '4096';
PRAGMA auto_vacuum = '0';
PRAGMA user_version = '0';
PRAGMA application_id = '0';
CREATE TABLE t1(a,b,c);
PRAGMA writable_schema = off;
COMMIT;
**END-TEST**
.testcase 401
.recover --unsafe
.check <<**END-TEST**
.dbconfig defensive off
BEGIN;
PRAGMA writable_schema = on;
PRAGMA foreign_keys = off;
PRAGMA encoding = 'UTF-8';
PRAGMA page_size = '4096';
PRAGMA auto_vacuum = '0';
PRAGMA user_version = '0';
PRAGMA application_id = '0';
CREATE TABLE t1(a,b,c);
PRAGMA writable_schema = off;
COMMIT;
**END-TEST**
.endif /* capable('vtab') */

# Follow-up to f945de7c4eb73732.  The ".recover" command must also omit
# extraneous text at the end of the sqlite_schema.sql field for views,
# triggers, and non-unique indexes, not just for tables.
#
.open :memory:
CREATE TABLE t1(a,b);
CREATE INDEX i1 ON t1(a);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
.dbconfig defensive off
.dbconfig writable_schema on
UPDATE sqlite_schema
   SET sql=sql||';'||char(10)||'.print INJECTED_'||name
 WHERE name IN ('i1','v1','r1');
.testcase 410
.recover
.check --glob <<END
.dbconfig defensive off
.safe
BEGIN;
PRAGMA writable_schema = on;
PRAGMA foreign_keys = off;
PRAGMA encoding = 'UTF-8';
PRAGMA page_size = '#';
PRAGMA auto_vacuum = '0';
PRAGMA user_version = '0';
PRAGMA application_id = '0';
CREATE TABLE t1(a,b);
CREATE INDEX i1 ON t1(a);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
PRAGMA writable_schema = off;
COMMIT;
END

# The ".dump" command must not allow text from a malformed schema (which is
# included in an error message that is output as an SQL comment) to escape
# from the comment.  In this database the name of the only schema entry is
# "x*/\n.print NAME_INJECTED\n/*" and its SQL is malformed.  (The first
# three lines of expected output are an error message on stderr.)
#
.testcase 420
.open --hexdb
| size 1024 pagesize 512 filename unk.db
| page 1 offset 0
|     0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
|    16: 02 00 01 01 00 40 20 20 00 00 00 02 00 00 00 02   .....@  ........
|    32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04   ................
|    48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00   ................
|    80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02   ................
|    96: 00 2e 9d 98 0d 00 00 00 01 01 c3 00 01 c3 00 00   ................
|   448: 00 00 00 3b 01 06 17 43 11 01 31 74 61 62 6c 65   ...;...C..1table
|   464: 78 2a 2f 0a 2e 70 72 69 6e 74 20 4e 41 4d 45 5f   x*/..print NAME_
|   480: 49 4e 4a 45 43 54 45 44 0a 2f 2a 74 31 02 43 52   INJECTED./*t1.CR
|   496: 45 41 54 45 20 54 41 42 4c 45 20 6a 75 6e 6b 28   EATE TABLE junk(
| page 2 offset 512
|     0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00   ................
| end unk.db
.dump
.check --exact <<END
sql error: malformed database schema (x*/
.print NAME_INJECTED
/*) (11)
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
/****** CORRUPTION ERROR *******/
/****** malformed database schema (x*  .print NAME_INJECTED /*) ******/
/****** ERROR: near "ORDER": syntax error ******/
/**** ERROR: (11) malformed database schema (x*  .print NAME_INJECTED /*) *****/
COMMIT;
END


.if NOT capable('LIKE_DOESNT_MATCH_BLOBS')
# 959866cfe4ec0e16 2026-11-06T00:25:13Z 
# Wrong result: c0 GLOB 'ab?' OR c0 GLOB 'ab*' on an indexed column
# omits BLOB rows
#
.open :memory:
.mode -reset qbox
.testcase 500
CREATE TABLE t0(c0, c1);
INSERT INTO t0 VALUES('ab',1),('abc',2),('abd',3),(x'6162',4),
                     (x'616263',5),('b',6);
CREATE INDEX i0 ON t0(c0);
.check ''

.testcase 510
SELECT c1, c0 FROM t0
 WHERE c0 GLOB 'ab*'
 ORDER BY c1;
.check <<*END*
╭────┬───────────╮
│ c1 │    c0     │
╞════╪═══════════╡
│  1 │ 'ab'      │
│  2 │ 'abc'     │
│  3 │ 'abd'     │
│  4 │ x'6162'   │
│  5 │ x'616263' │
╰────┴───────────╯
*END*

.testcase 520
SELECT c1, c0 FROM t0
 WHERE c0 GLOB 'ab?' OR c0 GLOB 'ab*'
 ORDER BY c1;
.check <<*END*
╭────┬───────────╮
│ c1 │    c0     │
╞════╪═══════════╡
│  1 │ 'ab'      │
│  2 │ 'abc'     │
│  3 │ 'abd'     │
│  4 │ x'6162'   │
│  5 │ x'616263' │
╰────┴───────────╯
*END*

.testcase 530
SELECT c1, c0 FROM t0
 WHERE c0 GLOB 'ab*' OR c0 GLOB 'ab*'
 ORDER BY c1;
.check <<*END*
╭────┬───────────╮
│ c1 │    c0     │
╞════╪═══════════╡
│  1 │ 'ab'      │
│  2 │ 'abc'     │
│  3 │ 'abd'     │
│  4 │ x'6162'   │
│  5 │ x'616263' │
╰────┴───────────╯
*END*

.testcase 540
SELECT c1, c0 FROM t0 NOT INDEXED
 WHERE c0 GLOB 'ab?' OR c0 GLOB 'ab*' 
 ORDER BY c1;
.check <<*END*
╭────┬───────────╮
│ c1 │    c0     │
╞════╪═══════════╡
│  1 │ 'ab'      │
│  2 │ 'abc'     │
│  3 │ 'abd'     │
│  4 │ x'6162'   │
│  5 │ x'616263' │
╰────┴───────────╯
*END*
.endif /* NOT capable('LIKE_DOESNT_MATCH_BLOBS') */

# db0bb9d415ffc8f0 2026-11-05T22:31:13Z
# generate_series(0,32,5) ORDER BY rowid DESC LIMIT 3 returns 10, 5, 0
# instead of 30, 25, 20
#
.if capable('vtab');
.open :memory:
.mode -reset quote
.testcase 600
SELECT value FROM generate_series(0,32,5) ORDER BY rowid DESC;
.check '30 25 20 15 10 5 0'

.testcase 610
SELECT value FROM generate_series(0,32,5) ORDER BY rowid DESC LIMIT 3;
.check '30 25 20'

.testcase 620
SELECT value FROM generate_series(0,32,5) ORDER BY value DESC LIMIT 3;
.check '30 25 20'

.testcase 630
SELECT rowid, value FROM generate_series(0,12,5);
.check '0,0 5,5 10,10'
.endif /* capable('vtab') */

# b4a8844d427ec7ef 2026-11-04T20:30:26Z
# Indexed GLOB with a prefix ending in U+07FF returns non-matching rows
# in a UTF-16be
#
.open :memory:
.mode -reset quote -quote off
.testcase 700
PRAGMA encoding='UTF-16be';
CREATE TABLE t1(id INT, x TEXT);
INSERT INTO t1 VALUES
  (1, 'a'||char(0x7FF)),
  (2, 'a'||char(0x7FF)||'z'),
  (3, 'a'||char(0x800)),
  (4, 'a'||char(0x4E00)),
  (5, 'a'||char(0xFFFE)),
  (6, 'a'||char(0xFFFE)||'z'),
  (7, 'a'||char(0xFFFF)),
  (8, 'a'||char(0xFFFF)||'z'),
  (9, 'a'||char(0x10000));
CREATE INDEX t1x ON t1(x);
PRAGMA encoding;
.check 'UTF-16be'

.param set :p1 "'a'||char(0x7FF)||'*'"
.param set :p2 "'a'||char(0xFFFE)||'*'"
.param set :p3 "'a'||char(0xFFFF)||'*'"

.testcase 710
SELECT id, hex(x) FROM t1 WHERE x GLOB :p1;
.check -glob '1,006107FF 2,006107FF007A'
.testcase 711
EXPLAIN QUERY PLAN
SELECT id, hex(x) FROM t1 WHERE x GLOB :p1;
.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'

.testcase 720
SELECT id, hex(x) FROM t1 WHERE +x GLOB :p1;
.check '1,006107FF 2,006107FF007A'

.testcase 730
SELECT id, hex(x) FROM t1 WHERE x GLOB :p2;
.check '5,0061FFFE 6,0061FFFE007A'
.testcase 731
EXPLAIN QUERY PLAN
SELECT id, hex(x) FROM t1 WHERE x GLOB :p2;
.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'

.testcase 740
SELECT id, hex(x) FROM t1 WHERE +x GLOB :p2;
.check '5,0061FFFE 6,0061FFFE007A'

.testcase 750
SELECT id, hex(x) FROM t1 WHERE x GLOB :p3;
.check '7,0061FFFF 8,0061FFFF007A'
.testcase 751
EXPLAIN QUERY PLAN
SELECT id, hex(x) FROM t1 WHERE x GLOB :p3;
.check -glob '*SEARCH t1 USING INDEX t1x (x>? AND x<?)*'

.testcase 760
SELECT id, hex(x) FROM t1 WHERE +x GLOB :p3;
.check '7,0061FFFF 8,0061FFFF007A'

# Forum https://sqlite.org/forum/forumpost/c582aea277 2026-11-07T10:10:46Z
# Incorrect FULL JOIN Result.
# (Despite the title, the problem is actually subtype propagation through
# CASE statements.)
#
.open :memory:
.mode -reset quote -quote off
.testcase 800
CREATE TABLE t2(a TEXT);
CREATE TABLE t0(a TEXT);
INSERT INTO t2 VALUES('[]');
INSERT INTO t0 VALUES('[]');
CREATE INDEX i ON t2(json(a));
CREATE VIEW v2(x) AS SELECT 1 WHERE 0;
.check ''
.testcase 810
SELECT json_array(CASE WHEN 1 THEN json(t0.a) END)
FROM t2 FULL OUTER JOIN t0
  ON json_array(CASE WHEN 1 THEN json(t2.a) END)
   = json_array(CASE WHEN 1 THEN json(t0.a) END);
.check '[[]]'
.testcase 820
.testctrl opt -All -q
SELECT json_array(CASE WHEN 1 THEN json(t0.a) END)
FROM t2 FULL OUTER JOIN t0
  ON json_array(CASE WHEN 1 THEN json(t2.a) END)
   = json_array(CASE WHEN 1 THEN json(t0.a) END);
.check '[[]]'

# Bug thread https://sqlite.org/bugs/info/a548aa9d4d (2026-11-05T22:09:59Z)
# was closed as NOT PLANNED.  These tests merely document the current
# behavior.  The complaint from Claude was that the error message at
# test 901 was a bit wonky.
#
.open :memory:
.mode -reset quote -quote relaxed
.testcase 900
CREATE TABLE t1(x,y);
CREATE INDEX t1x ON t1(x);
CREATE VIEW t2 AS SELECT x+y FROM t1;
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 'hello!'; END;
.check ''
.testcase 901
CREATE TABLE main.IF(a);
.check --glob '*syntax error*'
.testcase 902
SELECT name FROM sqlite_schema ORDER BY name;
.check 'r1 t1 t1x t2'
.testcase 903
PRAGMA integrity_check;
.check 'ok'
.testcase 910
CREATE TABLE main."IF"(a);
.check ''
.testcase 911
SELECT group_concat(name, ' ' ORDER BY name) FROM sqlite_schema;
.check 'IF r1 t1 t1x t2'
.testcase 912
PRAGMA integrity_check;
.check 'ok'