summaryrefslogtreecommitdiff
path: root/test/strict1.test
blob: 7655df7d3f9b69fed7294272a0bdfa69e9c2180a (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
397
398
399
400
401
402
403
404
405
406
407
# 2021-08-18
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements regression tests for SQLite library.  The
# focus of this file is testing STRICT tables.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix strict1

# STRICT tables have on a limited number of allowed datatypes.
#
do_catchsql_test strict1-1.1 {
  CREATE TABLE t1(a) STRICT;
} {1 {missing datatype for t1.a}}
do_catchsql_test strict1-1.2 {
  CREATE TABLE t1(a PRIMARY KEY) STRICT, WITHOUT ROWID;
} {1 {missing datatype for t1.a}}
do_catchsql_test strict1-1.3 {
  CREATE TABLE t1(a PRIMARY KEY) WITHOUT ROWID, STRICT;
} {1 {missing datatype for t1.a}}
do_catchsql_test strict1-1.4 {
  CREATE TABLE t1(a BANJO PRIMARY KEY) WITHOUT ROWID, STRICT;
} {1 {unknown datatype for t1.a: "BANJO"}}
do_catchsql_test strict1-1.5 {
  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f DATE) strict;
} {1 {unknown datatype for t1.f: "DATE"}}
do_catchsql_test strict1-1.6 {
  CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f TEXT(50)) WITHOUT ROWID, STRICT;
} {1 {unknown datatype for t1.f: "TEXT(50)"}}

do_execsql_test strict1-2.0 {
  CREATE TABLE t1(
    a INT,
    b INTEGER,
    c BLOB,
    d TEXT,
    e REAL
  ) STRICT;
} {}
ifcapable vtab {
  do_execsql_test strict1-2.0a {
    SELECT strict FROM pragma_table_list('t1');
  } {1}
}
do_catchsql_test strict1-2.1 {
  INSERT INTO t1(a) VALUES('xyz');
} {1 {cannot store TEXT value in INT column t1.a}}
do_catchsql_test strict1-2.2 {
  INSERT INTO t1(b) VALUES('xyz');
} {1 {cannot store TEXT value in INTEGER column t1.b}}
do_catchsql_test strict1-2.3 {
  INSERT INTO t1(c) VALUES('xyz');
} {1 {cannot store TEXT value in BLOB column t1.c}}
do_catchsql_test strict1-2.4 {
  INSERT INTO t1(d) VALUES(x'3142536475');
} {1 {cannot store BLOB value in TEXT column t1.d}}
do_catchsql_test strict1-2.5 {
  INSERT INTO t1(e) VALUES('xyz');
} {1 {cannot store TEXT value in REAL column t1.e}}


do_execsql_test strict1-3.1 {
  INSERT INTO t1(a, b) VALUES(1,2),('3','4'),(5.0, 6.0),(null,null);
  SELECT a, b, '|' FROM t1;
} {1 2 | 3 4 | 5 6 | {} {} |}
do_catchsql_test strict1-3.2 {
  INSERT INTO t1(a) VALUES(1.2);
} {1 {cannot store REAL value in INT column t1.a}}
do_catchsql_test strict1-3.3 {
  INSERT INTO t1(a) VALUES(x'313233');
} {1 {cannot store BLOB value in INT column t1.a}}
do_catchsql_test strict1-3.4 {
  INSERT INTO t1(b) VALUES(1.2);
} {1 {cannot store REAL value in INTEGER column t1.b}}
do_catchsql_test strict1-3.5 {
  INSERT INTO t1(b) VALUES(x'313233');
} {1 {cannot store BLOB value in INTEGER column t1.b}}

do_execsql_test strict1-4.1 {
  DELETE FROM t1;
  INSERT INTO t1(c) VALUES(x'313233'), (NULL);
  SELECT typeof(c), c FROM t1;
} {blob 123 null {}}
do_catchsql_test strict1-4.2 {
  INSERT INTO t1(c) VALUES('456');
} {1 {cannot store TEXT value in BLOB column t1.c}}

do_execsql_test strict1-5.1 {
  DELETE FROM t1;
  INSERT INTO t1(d) VALUES('xyz'),(4),(5.5),(NULL);
  SELECT typeof(d), d FROM t1;
} {text xyz text 4 text 5.5 null {}}
do_catchsql_test strict1-5.2 {
  INSERT INTO t1(d) VALUES(x'4567');
} {1 {cannot store BLOB value in TEXT column t1.d}}

do_execsql_test strict1-6.1 {
  DELETE FROM t1;
  INSERT INTO t1(e) VALUES(1),(2.5),('3'),('4.5'),(6.0),(NULL);
  SELECT typeof(e), e FROM t1;
} {real 1.0 real 2.5 real 3.0 real 4.5 real 6.0 null {}}
do_catchsql_test strict1-6.2 {
  INSERT INTO t1(e) VALUES('xyz');
} {1 {cannot store TEXT value in REAL column t1.e}}
do_catchsql_test strict1-6.3 {
  INSERT INTO t1(e) VALUES(x'3456');
} {1 {cannot store BLOB value in REAL column t1.e}}

ifcapable altertable {
  do_execsql_test strict1-7.1 {
    DROP TABLE IF EXISTS t4;
    CREATE TABLE t4(
      a INT AS (b*2) VIRTUAL,
      b INT AS (c*2) STORED,
      c INT PRIMARY KEY
    ) STRICT;
    INSERT INTO t4(c) VALUES(1);
    SELECT * FROM t4;
  } {4 2 1}
  do_catchsql_test strict1-7.2 {
    ALTER TABLE t4 ADD COLUMN d VARCHAR;
  } {1 {error in table t4 after add column: unknown datatype for t4.d: "VARCHAR"}}
  do_catchsql_test strict1-7.3 {
    ALTER TABLE t4 ADD COLUMN d;
  } {1 {error in table t4 after add column: missing datatype for t4.d}}
}

# 2022-01-17 https://sqlite.org/forum/forumpost/fa012c77796d9399
# 
reset_db
do_execsql_test strict1-8.1 {
  CREATE TABLE csv_import_table (
    "debit" TEXT,
    "credit" TEXT
  );
  INSERT INTO csv_import_table VALUES ('', '250.00');
  CREATE TABLE IF NOT EXISTS transactions (
      debit REAL,
      credit REAL,
      amount REAL GENERATED ALWAYS AS (ifnull(credit, 0.0) - ifnull(debit, 0.0))
  ) STRICT;
  INSERT INTO transactions
  SELECT
      nullif(debit, '') AS debit,
      nullif(credit, '') AS credit
  FROM csv_import_table;
  SELECT * FROM transactions;
} {{} 250.0 250.0}
do_execsql_test strict1-8.2 {
  CREATE TABLE t1(x REAL, y REAL AS (x)) STRICT;
  INSERT INTO t1 VALUES(5),(4611686018427387904);
  SELECT *, '|' FROM t1;
} {/5.0 5.0 4.6116\d*e\+18 4.6116\d+e\+18 |/}

# 2025-06-18 https://sqlite.org/forum/forumpost/6caf195248a849e4
#
# Enforce STRICT table type constraints on STORED generated columns
#
do_execsql_test strict1-9.1 {
  CREATE TABLE strict (
    k INTEGER PRIMARY KEY,
    c1 REAL AS(if(k=11,1.5, k=12,2, k=13,'x', k=14,x'34', 0.0))   STORED,
    c2 INT  AS(if(k=21,1.5, k=22,2, k=23,'x', k=24,x'34', 0))     STORED,
    c3 TEXT AS(if(k=31,1.5, k=32,2, k=33,'x', k=34,x'34', 'x'))   STORED,
    c4 BLOB AS(if(k=41,1.5, k=42,2, k=43,'x', k=44,x'34', x'00')) STORED,
    c5 ANY  AS(if(k=51,1.5, k=52,2, k=53,'x', k=54,x'34', 0))     STORED
  ) STRICT;
  INSERT INTO strict(k) VALUES(11);
  INSERT INTO strict(k) VALUES(12);
  INSERT INTO strict(k) VALUES(22);
  INSERT INTO strict(k) VALUES(31);
  INSERT INTO strict(k) VALUES(32);
  INSERT INTO strict(k) VALUES(33);
  INSERT INTO strict(k) VALUES(44);
  PRAGMA integrity_check;
} {ok}
do_catchsql_test strict1-9.2.13 {
  INSERT INTO strict(k) VALUES(13);
} {1 {cannot store TEXT value in REAL column strict.c1}}
do_catchsql_test strict1-9.2.14 {
  INSERT INTO strict(k) VALUES(14);
} {1 {cannot store BLOB value in REAL column strict.c1}}
do_catchsql_test strict1-9.2.21 {
  INSERT INTO strict(k) VALUES(21);
} {1 {cannot store REAL value in INT column strict.c2}}
do_catchsql_test strict1-9.2.23 {
  INSERT INTO strict(k) VALUES(23);
} {1 {cannot store TEXT value in INT column strict.c2}}
do_catchsql_test strict1-9.2.24 {
  INSERT INTO strict(k) VALUES(24);
} {1 {cannot store BLOB value in INT column strict.c2}}
do_catchsql_test strict1-9.2.34 {
  INSERT INTO strict(k) VALUES(34);
} {1 {cannot store BLOB value in TEXT column strict.c3}}
do_catchsql_test strict1-9.2.41 {
  INSERT INTO strict(k) VALUES(41);
} {1 {cannot store REAL value in BLOB column strict.c4}}
do_catchsql_test strict1-9.2.42 {
  INSERT INTO strict(k) VALUES(42);
} {1 {cannot store INT value in BLOB column strict.c4}}
do_catchsql_test strict1-9.2.43 {
  INSERT INTO strict(k) VALUES(43);
} {1 {cannot store TEXT value in BLOB column strict.c4}}

do_execsql_test strict1-9.3 {
  DROP TABLE strict;
  CREATE TABLE strict (
    k INTEGER PRIMARY KEY,
    c1 REAL AS(if(k=11,1.5, k=12,2, k=13,'x', k=14,x'34', 0.0))   VIRTUAL,
    c2 INT  AS(if(k=21,1.5, k=22,2, k=23,'x', k=24,x'34', 0))     VIRTUAL,
    c3 TEXT AS(if(k=31,1.5, k=32,2, k=33,'x', k=34,x'34', 'x'))   VIRTUAL,
    c4 BLOB AS(if(k=41,1.5, k=42,2, k=43,'x', k=44,x'34', x'00')) VIRTUAL,
    c5 ANY  AS(if(k=51,1.5, k=52,2, k=53,'x', k=54,x'34', 0))     VIRTUAL
  ) STRICT;
  INSERT INTO strict(k) VALUES(11);
  INSERT INTO strict(k) VALUES(12);
  INSERT INTO strict(k) VALUES(22);
  INSERT INTO strict(k) VALUES(31);
  INSERT INTO strict(k) VALUES(32);
  INSERT INTO strict(k) VALUES(33);
  INSERT INTO strict(k) VALUES(44);
  PRAGMA integrity_check;
} {ok}
do_catchsql_test strict1-9.4.13 {
  INSERT INTO strict(k) VALUES(13);
} {1 {cannot store TEXT value in REAL column strict.c1}}
do_catchsql_test strict1-9.4.14 {
  INSERT INTO strict(k) VALUES(14);
} {1 {cannot store BLOB value in REAL column strict.c1}}
do_catchsql_test strict1-9.4.21 {
  INSERT INTO strict(k) VALUES(21);
} {1 {cannot store REAL value in INT column strict.c2}}
do_catchsql_test strict1-9.4.23 {
  INSERT INTO strict(k) VALUES(23);
} {1 {cannot store TEXT value in INT column strict.c2}}
do_catchsql_test strict1-9.4.24 {
  INSERT INTO strict(k) VALUES(24);
} {1 {cannot store BLOB value in INT column strict.c2}}
do_catchsql_test strict1-9.4.34 {
  INSERT INTO strict(k) VALUES(34);
} {1 {cannot store BLOB value in TEXT column strict.c3}}
do_catchsql_test strict1-9.4.41 {
  INSERT INTO strict(k) VALUES(41);
} {1 {cannot store REAL value in BLOB column strict.c4}}
do_catchsql_test strict1-9.4.42 {
  INSERT INTO strict(k) VALUES(42);
} {1 {cannot store INT value in BLOB column strict.c4}}
do_catchsql_test strict1-9.4.43 {
  INSERT INTO strict(k) VALUES(43);
} {1 {cannot store TEXT value in BLOB column strict.c4}}

##########################################################################
####         Remaining tests in the file require ALTER TABLE         #####
####                                                                 #####
ifcapable !altertable {
  finish_test
  return
}
##########################################################################

#-------------------------------------------------------------------------
# ALTER TABLE ADD COLUMN on a STRICT table should not need to scan the
# entire table when the new column has no DEFAULT (or a NULL DEFAULT),
# just as it does not for an ordinary table.
#
#   https://sqlite.org/bugs/forumpost/1ffb2dfd73
#
reset_db
do_execsql_test strict1-10.0 {
  CREATE TABLE t1(a TEXT, b TEXT) STRICT;
  CREATE TABLE t2(a TEXT, b TEXT);
  WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5000)
    INSERT INTO t1 SELECT hex(randomblob(50)), i FROM c;
  INSERT INTO t2 SELECT * FROM t1;
}
proc alter_page_fetches {sql} {
  sqlite3_db_status db CACHE_HIT 1
  sqlite3_db_status db CACHE_MISS 1
  db eval $sql
  expr {[lindex [sqlite3_db_status db CACHE_HIT 0] 1]
      + [lindex [sqlite3_db_status db CACHE_MISS 0] 1]}
}
do_test strict1-10.1 {
  expr {[alter_page_fetches {ALTER TABLE t2 ADD COLUMN c TEXT}] < 50}
} 1
do_test strict1-10.2 {
  expr {[alter_page_fetches {ALTER TABLE t1 ADD COLUMN c TEXT}] < 50}
} 1
do_test strict1-10.3 {
  expr {[alter_page_fetches {ALTER TABLE t1 ADD COLUMN d TEXT DEFAULT NULL}] < 50}
} 1
do_catchsql_test strict1-10.4 {
  ALTER TABLE t1 ADD COLUMN e INT DEFAULT 'abc';
} {1 {type mismatch on DEFAULT}}

# Defaults that are converted losslessly by the column affinity are
# acceptable. In particular, an integer default on a REAL column.
#
do_test strict1-10.4.1 {
  expr {[alter_page_fetches {ALTER TABLE t1 ADD COLUMN r REAL DEFAULT 5}] < 50}
} 1
do_execsql_test strict1-10.4.2 {
  ALTER TABLE t1 ADD COLUMN i INT DEFAULT '12';
  ALTER TABLE t1 ADD COLUMN s TEXT DEFAULT 5;
  SELECT r, typeof(r), i, typeof(i), s, typeof(s) FROM t1 LIMIT 1;
  PRAGMA quick_check;
} {5.0 real 12 integer 5 text ok}
do_catchsql_test strict1-10.4.3 {
  ALTER TABLE t1 ADD COLUMN x BLOB DEFAULT 'abc';
} {1 {type mismatch on DEFAULT}}
do_catchsql_test strict1-10.4.4 {
  ALTER TABLE t1 ADD COLUMN x REAL DEFAULT 'abc';
} {1 {type mismatch on DEFAULT}}

# Adding a generated column to a STRICT table must check the computed
# value of the new column against its declared type for every existing
# row. This requires a full scan.
#
do_execsql_test strict1-10.5 {
  CREATE TABLE t3(a ANY, b INT) STRICT;
  INSERT INTO t3 VALUES(1, 1), (2, 2), ('x', 3);
}
do_test strict1-10.6 {
  lindex [catchsql { ALTER TABLE t3 ADD COLUMN g1 INT AS (a) }] 0
} 1
do_execsql_test strict1-10.7 {
  ALTER TABLE t3 ADD COLUMN g2 INT AS (b+1);
  ALTER TABLE t3 ADD COLUMN g3 ANY AS (a);
  SELECT a, g2, g3 FROM t3;
} {1 2 1 2 3 2 x 4 x}
do_catchsql_test strict1-10.8 {
  INSERT INTO t3(a, b) VALUES(4, 'y');
} {1 {cannot store TEXT value in INT column t3.b}}

# A CHECK constraint added by ADD COLUMN must be verified against the
# existing rows, for both STRICT and ordinary tables.
#
do_execsql_test strict1-10.9 {
  CREATE TABLE t4(a INT, b TEXT) STRICT;
  CREATE TABLE t5(a INT, b TEXT);
  INSERT INTO t4 VALUES(1, 'one'), (2, 'two');
  INSERT INTO t5 SELECT * FROM t4;
}
do_catchsql_test strict1-10.10 {
  ALTER TABLE t4 ADD COLUMN c INT CHECK( c IS NOT NULL );
} {1 {CHECK constraint failed}}
do_catchsql_test strict1-10.11 {
  ALTER TABLE t5 ADD COLUMN c INT CHECK( c IS NOT NULL );
} {1 {CHECK constraint failed}}
do_catchsql_test strict1-10.12 {
  ALTER TABLE t4 ADD COLUMN c INT DEFAULT 5 CHECK( c>a+3 );
} {1 {CHECK constraint failed}}
do_execsql_test strict1-10.13 {
  ALTER TABLE t4 ADD COLUMN c INT DEFAULT 5 CHECK( c>a );
  ALTER TABLE t5 ADD COLUMN c INT DEFAULT 5 CHECK( c>a );
  SELECT * FROM t4 UNION ALL SELECT * FROM t5;
} {1 one 5 2 two 5 1 one 5 2 two 5}

# A CHECK constraint that is already on the table, and not added by the
# ADD COLUMN, does not require a scan.
#
do_execsql_test strict1-10.14 {
  CREATE TABLE t6(a TEXT, b INT CHECK(b>0), CHECK(a!=b));
  WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5000)
    INSERT INTO t6 SELECT hex(randomblob(50)), i FROM c;
}
do_test strict1-10.15 {
  expr {[alter_page_fetches {ALTER TABLE t6 ADD COLUMN c TEXT}] < 50}
} 1

# The single-row check of the DEFAULT type on a STRICT table is not
# subject to the authorizer. Denying reads of the table does not
# prevent the column from being added, or the type check from working.
#
ifcapable auth {
  proc strict1_auth {code arg1 args} {
    if {$code=="SQLITE_READ" && $arg1=="t7"} { return SQLITE_DENY }
    return SQLITE_OK
  }
  do_execsql_test strict1-10.16 {
    CREATE TABLE t7(a INT) STRICT;
    INSERT INTO t7 VALUES(1);
  }
  db auth strict1_auth
  do_execsql_test strict1-10.17 {
    ALTER TABLE t7 ADD COLUMN b REAL DEFAULT 5;
  }
  do_catchsql_test strict1-10.18 {
    ALTER TABLE t7 ADD COLUMN c INT DEFAULT 'x';
  } {1 {type mismatch on DEFAULT}}
  db auth {}
  do_execsql_test strict1-10.19 {
    SELECT a, b, typeof(b) FROM t7;
  } {1 5.0 real}
}

finish_test