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

708 lines · 19603 bytes Raw Blame History
1# 2019-10-31
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# Test cases for generated columns.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix gencol1
18
19# ticket 830277d9db6c3ba1 on 2019-10-31
20do_execsql_test 100 {
21 CREATE TABLE t0(c0 AS(TYPEOF(c1)), c1);
22 INSERT INTO t0(c1) VALUES(0);
23 CREATE TABLE t1(x AS (typeof(y)), y);
24 INSERT INTO t1 SELECT * FROM t0;
25 SELECT * FROM t1;
26} {integer 0}
27
28foreach {tn schema} {
291 {
30 CREATE TABLE t1(
31 a INT,
32 b TEXT,
33 c ANY,
34 w INT GENERATED ALWAYS AS (a*10),
35 x TEXT AS (typeof(c)),
36 y TEXT AS (substr(b,a,a+2))
37 );
38 }
392 {
40 CREATE TABLE t1(
41 w INT GENERATED ALWAYS AS (a*10),
42 x TEXT AS (typeof(c)),
43 y TEXT AS (substr(b,a,a+2)),
44 a INT,
45 b TEXT,
46 c ANY
47 );
48 }
493 {
50 CREATE TABLE t1(
51 w INT GENERATED ALWAYS AS (a*10),
52 a INT,
53 x TEXT AS (typeof(c)) STORED,
54 b TEXT,
55 y TEXT AS (substr(b,a,a+2)),
56 c ANY
57 );
58 }
594 {
60 CREATE TABLE t1(
61 a INTEGER PRIMARY KEY,
62 w INT GENERATED ALWAYS AS (a*10),
63 b TEXT,
64 x TEXT AS (typeof(c)),
65 y TEXT AS (substr(b,a,a+2)) STORED,
66 c ANY
67 );
68 }
695 {
70 CREATE TABLE t1(
71 w INT GENERATED ALWAYS AS (a*10),
72 a INT,
73 x TEXT AS (typeof(c)),
74 b TEXT,
75 y TEXT AS (substr(b,a,a+2)) STORED,
76 c ANY,
77 PRIMARY KEY(a,b)
78 ) WITHOUT ROWID;
79 }
806 {
81 CREATE TABLE t1(
82 w INT GENERATED ALWAYS AS (m*5),
83 m INT AS (a*2) STORED,
84 a INT,
85 x TEXT AS (typeof(c)),
86 b TEXT,
87 y TEXT AS (substr(b,m/2,m/2+2)) STORED,
88 c ANY,
89 PRIMARY KEY(a,b)
90 );
91 }
927 {
93 CREATE TABLE t1(
94 w INT GENERATED ALWAYS AS (m*5),
95 m INT AS (a*2) NOT NULL,
96 a INT,
97 x TEXT AS (typeof(c)) CHECK (x<>'blank'),
98 b TEXT,
99 y TEXT AS (substr(b,m/2,m/2+2)) STORED,
100 c ANY,
101 PRIMARY KEY(b,a)
102 ) WITHOUT ROWID;
103 }
104} {
105 catch {db close}
106 sqlite3 db :memory:
107 db eval $schema
108 do_execsql_test 2.$tn.100 {
109 INSERT INTO t1(a,b,c) VALUES(1,'abcdef',5.5),(3,'cantaloupe',NULL);
110 SELECT w, x, y, '|' FROM t1 ORDER BY a;
111 } {10 real abc | 30 null ntalo |}
112 do_execsql_test 2.$tn.101 {
113 SELECT w, x, y, '|' FROM t1 ORDER BY w;
114 } {10 real abc | 30 null ntalo |}
115 do_execsql_test 2.$tn.102 {
116 SELECT a FROM t1 WHERE w=30;
117 } {3}
118 do_execsql_test 2.$tn.103 {
119 SELECT a FROM t1 WHERE x='real';
120 } {1}
121 do_execsql_test 2.$tn.104 {
122 SELECT a FROM t1 WHERE y LIKE '%tal%' OR x='real' ORDER BY b;
123 } {1 3}
124 do_execsql_test 2.$tn.110 {
125 CREATE INDEX t1w ON t1(w);
126 SELECT a FROM t1 WHERE w=10;
127 } {1}
128 do_execsql_test 2.$tn.120 {
129 CREATE INDEX t1x ON t1(x) WHERE w BETWEEN 20 AND 40;
130 SELECT a FROM t1 WHERE x='null' AND w BETWEEN 20 AND 40;
131 } {3}
132 do_execsql_test 2.$tn.121 {
133 SELECT a FROM t1 WHERE x='real';
134 } {1}
135 do_execsql_test 2.$tn.130 {
136 VACUUM;
137 PRAGMA integrity_check;
138 } {ok}
139 do_execsql_test 2.$tn.140 {
140 UPDATE t1 SET a=a+100 WHERE w<20;
141 SELECT a, w, '|' FROM t1 ORDER BY w;
142 } {3 30 | 101 1010 |}
143 do_execsql_test 2.$tn.150 {
144 INSERT INTO t1 VALUES(4,'jambalaya','Chef John'),(15,87719874135,0);
145 SELECT w, x, y, '|' FROM t1 ORDER BY w;
146 } {30 null ntalo | 40 text balaya | 150 integer {} | 1010 real {} |}
147}
148
149# 2019-10-31 ticket b9befa4b83a660cc
150db close
151sqlite3 db :memory:
152do_execsql_test 3.100 {
153 PRAGMA foreign_keys = true;
154 CREATE TABLE t0(c0 PRIMARY KEY, c1, c2 AS (c0+c1-c3) REFERENCES t0, c3);
155 INSERT INTO t0 VALUES (0, 0, 0), (11, 5, 5);
156 UPDATE t0 SET c1 = c0, c3 = c0;
157 SELECT *, '|' FROM t0 ORDER BY +c0;
158} {0 0 0 0 | 11 11 11 11 |}
159do_catchsql_test 3.110 {
160 UPDATE t0 SET c1 = c0, c3 = c0+1;
161} {1 {FOREIGN KEY constraint failed}}
162
163# 2019-11-01 ticket c28a01da72f8957c
164db close
165sqlite3 db :memory:
166do_execsql_test 4.100 {
167 CREATE TABLE t0 (
168 c0,
169 c1 a UNIQUE AS (1),
170 c2,
171 c3 REFERENCES t0(c1)
172 );
173 PRAGMA foreign_keys = true;
174 INSERT INTO t0(c0,c2,c3) VALUES(0,0,1);
175} {}
176do_catchsql_test 4.110 {
177 REPLACE INTO t0(c0,c2,c3) VALUES(0,0,0),(0,0,0);
178} {1 {FOREIGN KEY constraint failed}}
179
180# 2019-11-01 Problem found while adding new foreign key test cases in TH3.
181db close
182sqlite3 db :memory:
183do_execsql_test 5.100 {
184 PRAGMA foreign_keys=ON;
185 CREATE TABLE t1(
186 gcb AS (b*1),
187 a INTEGER PRIMARY KEY,
188 gcc AS (c+0),
189 b UNIQUE,
190 gca AS (1*a+0),
191 c UNIQUE
192 ) WITHOUT ROWID;
193 INSERT INTO t1 VALUES(1,2,3);
194 INSERT INTO t1 VALUES(4,5,6);
195 INSERT INTO t1 VALUES(7,8,9);
196 CREATE TABLE t1a(
197 gcx AS (x+0) REFERENCES t1(a) ON DELETE CASCADE,
198 id,
199 x,
200 gcid AS (1*id)
201 );
202 INSERT INTO t1a VALUES(1, 1);
203 INSERT INTO t1a VALUES(2, 4);
204 INSERT INTO t1a VALUES(3, 7);
205 DELETE FROM t1 WHERE b=5;
206 SELECT id,x,'|' FROM t1a ORDER BY id;
207} {1 1 | 3 7 |}
208
209do_catchsql_test 6.10 {
210 DROP TABLE IF EXISTS t0;
211 CREATE TABLE t0(c0 NOT NULL AS(c1), c1);
212 REPLACE INTO t0(c1) VALUES(NULL);
213} {1 {NOT NULL constraint failed: t0.c0}}
214
215# 2019-11-06 ticket https://sqlite.org/src/info/2399f5986134f79c
216# 2019-12-27 ticket https://sqlite.org/src/info/5fbc159eeb092130
217# 2019-12-27 ticket https://sqlite.org/src/info/37823501c68a09f9
218#
219# All of the above tickets deal with NOT NULL ON CONFLICT REPLACE
220# constraints on tables that have generated columns.
221#
222reset_db
223do_execsql_test 7.10 {
224 CREATE TABLE t0 (c0 GENERATED ALWAYS AS (1), c1 UNIQUE, c2 UNIQUE);
225 INSERT INTO t0(c1) VALUES (1);
226 SELECT quote(0 = t0.c2 OR t0.c1 BETWEEN t0.c2 AND 1) FROM t0;
227} {NULL}
228do_execsql_test 7.11 {
229 DROP TABLE t0;
230 CREATE TABLE t0(c0 NOT NULL DEFAULT 'xyz', c1 AS(c0) NOT NULL);
231 REPLACE INTO t0(c0) VALUES(NULL);
232 SELECT * FROM t0;
233} {xyz xyz}
234do_execsql_test 7.12 {
235 DROP TABLE t0;
236 CREATE TABLE t0(c0 NOT NULL DEFAULT 'xyz', c1 AS(c0) STORED NOT NULL);
237 REPLACE INTO t0(c0) VALUES(NULL);
238 SELECT * FROM t0;
239} {xyz xyz}
240do_execsql_test 7.20 {
241 CREATE TABLE t1(
242 a NOT NULL DEFAULT 'aaa',
243 b AS(c) NOT NULL,
244 c NOT NULL DEFAULT 'ccc');
245 REPLACE INTO t1(a,c) VALUES(NULL,NULL);
246 SELECT * FROM t1;
247} {aaa ccc ccc}
248do_execsql_test 7.21 {
249 DROP TABLE t1;
250 CREATE TABLE t1(
251 a NOT NULL DEFAULT 'aaa',
252 b AS(c) STORED NOT NULL,
253 c NOT NULL DEFAULT 'ccc');
254 REPLACE INTO t1(a,c) VALUES(NULL,NULL);
255 SELECT * FROM t1;
256} {aaa ccc ccc}
257do_execsql_test 7.30 {
258 CREATE TABLE t2(
259 a NOT NULL DEFAULT 'aaa',
260 b AS(a) NOT NULL,
261 c NOT NULL DEFAULT 'ccc');
262 REPLACE INTO t2(a,c) VALUES(NULL,NULL);
263 SELECT * FROM t2;
264} {aaa aaa ccc}
265do_execsql_test 7.31 {
266 DROP TABLE t2;
267 CREATE TABLE t2(
268 a NOT NULL DEFAULT 'aaa',
269 b AS(a) STORED NOT NULL,
270 c NOT NULL DEFAULT 'ccc');
271 REPLACE INTO t2(a,c) VALUES(NULL,NULL);
272 SELECT * FROM t2;
273} {aaa aaa ccc}
274do_execsql_test 7.40 {
275 CREATE TABLE t3(a NOT NULL DEFAULT 123, b AS(a) UNIQUE);
276 REPLACE INTO t3 VALUES(NULL);
277 SELECT * FROM t3;
278} {123 123}
279do_execsql_test 7.41 {
280 SELECT * FROM t3 WHERE b=123;
281} {123 123}
282do_execsql_test 7.50 {
283 CREATE TABLE t4(a NOT NULL DEFAULT 123, b AS(a*10+4) STORED UNIQUE);
284 REPLACE INTO t4 VALUES(NULL);
285 SELECT * FROM t4;
286} {123 1234}
287do_execsql_test 7.51 {
288 SELECT * FROM t4 WHERE b=1234;
289} {123 1234}
290
291# 2019-11-06 ticket 4fc08501f4e56692
292do_execsql_test 8.10 {
293 DROP TABLE IF EXISTS t0;
294 CREATE TABLE t0(
295 c0 AS (('a', 9) < ('b', c1)),
296 c1 AS (1),
297 c2 CHECK (1 = c1)
298 );
299 INSERT INTO t0 VALUES (0),(99);
300 SELECT * FROM t0;
301} {1 1 0 1 1 99}
302do_catchsql_test 8.20 {
303 DROP TABLE IF EXISTS t0;
304 CREATE TABLE t0(
305 c0,
306 c1 AS(c0 + c2),
307 c2 AS(c1) CHECK(c2)
308 );
309 UPDATE t0 SET c0 = NULL;
310} {1 {generated column loop on "c2"}}
311
312# 2019-11-21 Problems in the new generated column logic
313# reported by Yongheng Chen and Rui Zhong
314reset_db
315do_execsql_test 9.10 {
316 PRAGMA foreign_keys=OFF;
317 CREATE TABLE t1(aa , bb AS (17) UNIQUE);
318 INSERT INTO t1 VALUES(17);
319 CREATE TABLE t2(cc);
320 INSERT INTO t2 VALUES(41);
321 SELECT * FROM t2 JOIN t1 WHERE t1.bb=t1.aa AND t1.bb=17;
322} {41 17 17}
323do_execsql_test 9.20 {
324 CREATE TABLE t3(aa INT PRIMARY KEY, bb UNIQUE AS(aa));
325 INSERT INTO t3 VALUES(1);
326 SELECT 100, * FROM t3;
327 DELETE FROM t3 WHERE (SELECT bb FROM t3);
328 SELECT 200, * FROM t3;
329} {100 1 1}
330
331# 2019-12-04 Generated column in a CREATE TABLE IF NOT EXISTS that
332# does already exist.
333#
334sqlite3 db :memory:
335do_execsql_test 10.10 {
336 CREATE TABLE t1(aa,bb);
337 CREATE TABLE IF NOT EXISTS t1(aa, bb AS (aa+1));
338 PRAGMA integrity_check;
339} {ok}
340
341# 2019-12-06 Found by mrigger
342#
343sqlite3 db :memory:
344do_execsql_test 11.10 {
345 PRAGMA foreign_keys = true;
346 CREATE TABLE t0(
347 c0,
348 c1 INTEGER PRIMARY KEY,
349 c2 BLOB UNIQUE DEFAULT x'00',
350 c3 BLOB GENERATED ALWAYS AS (1),
351 FOREIGN KEY(c1) REFERENCES t0(c2)
352 );
353}
354do_catchsql_test 11.20 {
355 INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0)
356} {1 {FOREIGN KEY constraint failed}}
357do_execsql_test 11.30 {
358 DROP TABLE t0;
359 CREATE TABLE t0(
360 c0,
361 c1 INTEGER PRIMARY KEY,
362 c3 BLOB GENERATED ALWAYS AS (1),
363 c2 BLOB UNIQUE DEFAULT x'00',
364 FOREIGN KEY(c1) REFERENCES t0(c2)
365 );
366}
367do_catchsql_test 11.40 {
368 INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0)
369} {1 {FOREIGN KEY constraint failed}}
370do_execsql_test 11.50 {
371 DROP TABLE t0;
372 CREATE TABLE t0(
373 c0,
374 c3 BLOB GENERATED ALWAYS AS (1),
375 c1 INTEGER PRIMARY KEY,
376 c2 BLOB UNIQUE DEFAULT x'00',
377 FOREIGN KEY(c1) REFERENCES t0(c2)
378 );
379}
380do_catchsql_test 11.60 {
381 INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0)
382} {1 {FOREIGN KEY constraint failed}}
383do_execsql_test 11.70 {
384 DROP TABLE t0;
385 CREATE TABLE t0(
386 c3 BLOB GENERATED ALWAYS AS (1),
387 c0,
388 c1 INTEGER PRIMARY KEY,
389 c2 BLOB UNIQUE DEFAULT x'00',
390 FOREIGN KEY(c1) REFERENCES t0(c2)
391 );
392}
393do_catchsql_test 11.80 {
394 INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0)
395} {1 {FOREIGN KEY constraint failed}}
396
397# 2019-12-09 ticket bd8c280671ba44a7
398# With generated columns, the sqlite3ExprGetColumnOfTable() routine might
399# generate a code sequence that does not end with OP_Column. So check to
400# make sure that the last instruction generated is an OP_column prior to
401# applying the OPFLAG_TYPEOFARG optimization to NOT NULL checks in the
402# PRAGMA integrity_check code.
403#
404sqlite3 db :memory:
405do_execsql_test 12.10 {
406 CREATE TABLE t0 (c0, c1 NOT NULL AS (c0==0));
407 INSERT INTO t0(c0) VALUES (0);
408 PRAGMA integrity_check;
409} {ok}
410
411# 2019-12-09 bug report from Yongheng Chen
412# Ensure that the SrcList_item.colUsed field is set correctly when a
413# generated column appears in the USING clause of a join.
414#
415do_execsql_test 13.10 {
416 CREATE TABLE t1(x, y AS(x+1));
417 INSERT INTO t1 VALUES(10);
418 SELECT y FROM t1 JOIN t1 USING (y,y);
419} {11}
420do_execsql_test 13.11 {
421 SELECT 123 FROM t1 JOIN t1 USING (x);
422} {123}
423do_execsql_test 13.11 {
424 SELECT 456 FROM t1 JOIN t1 USING (x,x);
425} {456}
426do_execsql_test 13.20 {
427 CREATE INDEX t1y ON t1(y);
428 SELECT y FROM t1 JOIN t1 USING (y,y);
429} {11}
430do_execsql_test 13.21 {
431 CREATE INDEX t1x ON t1(x);
432 SELECT 123 FROM t1 JOIN t1 USING (x);
433} {123}
434do_execsql_test 13.22 {
435 SELECT 456 FROM t1 JOIN t1 USING (x,x);
436} {456}
437
438# 2019-12-14 ticket b439bfcfb7deedc6
439#
440sqlite3 db :memory:
441do_execsql_test 14.10 {
442 CREATE TABLE t0(c0 AS(1 >= 1), c1 UNIQUE AS(TYPEOF(c0)), c2);
443 INSERT INTO t0 VALUES(0);
444 REINDEX;
445 SELECT * FROM t0;
446} {1 integer 0}
447do_catchsql_test 14.10 {
448 INSERT INTO t0 VALUES(2);
449} {1 {UNIQUE constraint failed: t0.c1}}
450
451# 2019-12-14 gramfuzz1 find
452# The schema is malformed in that it has a subquery on a generated
453# column expression. This will be loaded if writable_schema=ON. SQLite
454# must not use such an expression during code generation as the code generator
455# will add bits of content to the expression tree that might be allocated
456# from lookaside. But the schema is not tied to a particular database
457# connection, so the use of lookaside memory is prohibited. The fix
458# is to change the generated column expression to NULL before adding it
459# to the schema.
460#
461reset_db
462do_test 15.10 {
463 sqlite3 db {}
464 db deserialize [decode_hexdb {
465| size 8192 pagesize 4096 filename c27.db
466| page 1 offset 0
467| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
468| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 02 .....@ ........
469| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................
470| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................
471| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................
472| 96: 00 2e 3f d8 0d 00 00 00 01 0f ba 00 0f ba 00 00 ..?.............
473| 4016: 00 00 00 00 00 00 00 00 00 00 44 01 06 17 11 11 ..........D.....
474| 4032: 01 75 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .utablet1t1.CREA
475| 4048: 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 49 4e TE TABLE t1(a IN
476| 4064: 54 2c 20 62 20 41 53 28 28 56 41 4c 55 45 53 28 T, b AS((VALUES(
477| 4080: 31 29 29 20 49 53 20 75 6e 6b 6e 6f 77 6e 29 29 1)) IS unknown))
478| page 2 offset 4096
479| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................
480| end c27.db
481}]} {}
482do_execsql_test 15.20 {
483 PRAGMA writable_schema=ON;
484 REPLACE INTO t1 VALUES(9);
485 SELECT a, quote(b) FROM t1
486} {9 NULL}
487
488# 2019-12-16 ticket 3b84b42943644d6f
489# When a table is the right table of a LEFT JOIN and the ON clause is
490# false, make sure any generated columns evaluate to NULL.
491reset_db
492do_execsql_test 16.10 {
493 CREATE TABLE t0(c0);
494 CREATE TABLE t1(c1, c2 AS(1));
495 INSERT INTO t0 VALUES(0);
496 SELECT c0, c1, c2 FROM t0 LEFT JOIN t1;
497} {0 {} {}}
498do_execsql_test 16.20 {
499 DROP TABLE t1;
500 CREATE TABLE t1(c1, c2 AS (c1 ISNULL));
501 SELECT c0, c1, c2 FROM t0 LEFT JOIN t1;
502} {0 {} {}}
503do_execsql_test 16.30 {
504 INSERT INTO t1(c1) VALUES(1),(NULL);
505 SELECT * FROM t1;
506} {1 0 {} 1}
507do_execsql_test 16.40 {
508 SELECT c0, c1, c2 FROM t0 LEFT JOIN t1 ON c0=c1;
509} {0 {} {}}
510
511# 2019-12-20 ticket e0a8120553f4b082
512# Generated columns with REAL affinity need to have an OP_RealAffinity
513# opcode applied, even when the column value is extracted from an index.
514#
515reset_db
516do_execsql_test 17.10 {
517 CREATE TABLE t0(c0 REAL AS(1) UNIQUE, c1 INT);
518 INSERT INTO t0 VALUES('');
519 SELECT quote(c0), quote(c1) from t0;
520} {1.0 ''}
521do_execsql_test 17.20 {
522 SELECT *, (1 BETWEEN CAST(t0.c0 AS TEXT) AND t0.c0) FROM t0;
523} {1.0 {} 0}
524do_execsql_test 17.30 {
525 SELECT * FROM t0 WHERE (1 BETWEEN CAST(t0.c0 AS TEXT) AND t0.c0);
526} {}
527do_execsql_test 17.40 {
528 CREATE TABLE t1(a TEXT AS(b) COLLATE nocase, b TEXT, c INT, d DEFAULT 1);
529 INSERT INTO t1(b,c) VALUES('abc',11),('DEF',22),('ghi',33);
530 SELECT a FROM t1 WHERE b='DEF' AND a='def';
531} {DEF}
532do_execsql_test 17.50 {
533 CREATE INDEX t1bca ON t1(b,c,a);
534 SELECT a FROM t1 WHERE b='DEF' AND a='def';
535} {DEF}
536
537# 2019-12-26 ticket ec8abb025e78f40c
538# An index on a virtual column with a constant value (why would anybody
539# ever do such a thing?) can cause problems for a one-pass DELETE.
540#
541reset_db
542do_execsql_test 18.10 {
543 CREATE TABLE t0(c0 UNIQUE AS(0), c1, c2);
544 INSERT INTO t0(c1) VALUES(0);
545 SELECT * FROM t0;
546} {0 0 {}}
547do_execsql_test 18.20 {
548 UPDATE t0 SET c1=0, c2=0 WHERE c0>=0;
549 SELECT * FROM t0;
550} {0 0 0}
551
552# 2019-12-27 ticket de4b04149b9fdeae
553#
554reset_db
555do_catchsql_test 19.10 {
556 CREATE TABLE t0(
557 c0 INT AS(2) UNIQUE,
558 c1 TEXT UNIQUE,
559 FOREIGN KEY(c0) REFERENCES t0(c1)
560 );
561 INSERT INTO t0(c1) VALUES(0.16334143182538696), (0);
562} {1 {UNIQUE constraint failed: t0.c0}}
563
564# 2020-06-29 forum bug report.
565# https://sqlite.org/forum/forumpost/73b9a8ccfb
566#
567do_execsql_test 20.1 {
568 CREATE TEMPORARY TABLE tab (
569 prim DATE PRIMARY KEY,
570 a INTEGER,
571 comp INTEGER AS (a),
572 b INTEGER,
573 x INTEGER
574 );
575 -- Add some data
576 INSERT INTO tab (prim, a, b) VALUES ('2001-01-01', 0, 0);
577 -- Check that each column is 0 like I expect
578 SELECT * FROM tab;
579} {2001-01-01 0 0 0 {}}
580do_execsql_test 20.2 {
581 -- Do an UPSERT on the b column
582 INSERT INTO tab (prim, b)
583 VALUES ('2001-01-01',5)
584 ON CONFLICT(prim) DO UPDATE SET b=excluded.b;
585 -- Now b is NULL rather than 5
586 SELECT * FROM tab;
587} {2001-01-01 0 0 5 {}}
588
589# 2021-07-30 forum https://sqlite.org/forum/forumpost/ff3ffe09251c105b?t=h
590#
591ifcapable vtab {
592reset_db
593 do_execsql_test 21.1 {
594 CREATE TABLE t1(
595 a integer primary key,
596 b int generated always as (a+5),
597 c text GENERATED ALWAYS as (printf('%08x',a)),
598 d Generated
599 Always
600 AS ('xyzzy'),
601 e int Always default(5)
602 );
603 INSERT INTO t1(a) VALUES(5);
604 SELECT name, type FROM pragma_table_xinfo('t1');
605 } {a INTEGER b INT c TEXT d {} e {int Always}}
606}
607
608# 2021-09-07 forum https://sqlite.org/forum/forumpost/699b44b3ee
609#
610reset_db
611do_execsql_test 22.1 {
612 CREATE TABLE t0(a PRIMARY KEY,b TEXT AS ('2') UNIQUE);
613 INSERT INTO t0(a) VALUES(2);
614 SELECT * FROM t0 AS x JOIN t0 AS y
615 WHERE x.b='2'
616 AND (y.a=2 OR (x.b LIKE '2*' AND y.a=x.b));
617} {2 2 2 2}
618
619
620# 2023-03-02 dbsqlfuzz 65f5eb57f8859344d5f1f33e08c77ee12960ed83
621#
622set typelist {ANY INT REAL BLOB TEXT {}}
623set cnt 0
624foreach t1 $typelist {
625 foreach t2 $typelist {
626 incr cnt
627 db eval "
628 DROP TABLE IF EXISTS t1;
629 CREATE TABLE t1(
630 x $t1,
631 a $t2 AS (x) VIRTUAL,
632 b BLOB AS (x) VIRTUAL
633 );
634 CREATE INDEX x2 ON t1(a);
635 INSERT INTO t1(x) VALUES(NULL),('1'),(2),(3.5),('xyz');
636 "
637 set x1 [lsort [db eval {SELECT typeof(b) FROM t1}]]
638 do_test 23.1.$cnt {
639 lsort [db eval {SELECT typeof(b) FROM t1 INDEXED BY x2}]
640 } $x1
641 }
642}
643do_execsql_test 23.2 {
644 DROP TABLE t1;
645 CREATE TABLE t1(
646 x,
647 a INT AS (x) VIRTUAL,
648 b BLOB AS (x) VIRTUAL
649 );
650 CREATE INDEX x2 ON t1(a);
651 INSERT INTO t1(x) VALUES(NULL),('1'),('xyz'),(2),(3.5);
652 SELECT quote(a) FROM t1 INDEXED BY x2;
653} {NULL 1 2 3.5 'xyz'}
654do_execsql_test 23.3 {
655 EXPLAIN SELECT a FROM t1 INDEXED BY x2;
656} {~/Column 0/}
657# ^^^^^^^^---- verfies that x2 acts like a covering index
658do_execsql_test 23.4 {
659 EXPLAIN SELECT b FROM t1 INDEXED BY x2;
660} {/Column 0/}
661# ^^^^^^^^^^--- Must reference the original table in this case because
662# of the different datatype on column b.
663
664# 2023-03-07 https://sqlite.org/forum/forumpost/b312e075b5
665#
666do_catchsql_test 23.5 {
667 CREATE TABLE v0(c1 INT, c2 AS (RAISE(IGNORE)));
668} {1 {RAISE() may only be used within a trigger-program}}
669
670# Bug 2026-10-14T17:34:30Z
671#
672do_execsql_test 24.0 {
673 CREATE TABLE t(a REAL, b AS (a) VIRTUAL);
674 INSERT INTO t(a) VALUES(1.0);
675 CREATE INDEX i ON t(b);
676}
677do_execsql_test 24.1 {
678 SELECT b, typeof(b) FROM t NOT INDEXED;
679} {1.0 real}
680do_execsql_test 24.2 {
681 SELECT b, typeof(b) FROM t INDEXED BY i;
682} {1.0 real}
683do_execsql_test 24.3 {
684 SELECT b, typeof(b) FROM t WHERE b = 1;
685} {1.0 real}
686
687# Report https://sqlite.org/bugs/forumpost/9c7be5adb8
688#
689# An aggregate argument that matches a virtual generated column must not
690# be read from an index on that column if the column has a different
691# affinity from the expression.
692#
693do_execsql_test 25.0 {
694 CREATE TABLE t25(a INT, b INT, g TEXT AS (a+0));
695 CREATE INDEX i25 ON t25(b, g);
696 INSERT INTO t25(a,b) VALUES(9,1),(10,1);
697}
698do_execsql_test 25.1 {
699 SELECT b, max(a+0), min(a+0), group_concat(typeof(a+0))
700 FROM t25 NOT INDEXED GROUP BY b;
701} {1 10 9 integer,integer}
702do_execsql_test 25.2 {
703 SELECT b, max(a+0), min(a+0), group_concat(typeof(a+0))
704 FROM t25 INDEXED BY i25 GROUP BY b;
705} {1 10 9 integer,integer}
706
707
708finish_test