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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
# 2008 July 15
#
# 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 how SQLite generates the names
# of columns in a result set.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Rules (applied in order):
#
# (1) If there is an AS clause, use it.
#
# (2) A non-trival expression (not a table column name) then the name is
# a copy of the expression text.
#
# (3) If short_column_names=ON, then just the abbreviated column name without
# the table name.
#
# (4) When short_column_names=OFF and full_column_names=OFF then
# use case (2) for simple queries and case (5) for joins.
#
# (5) When short_column_names=OFF and full_column_names=ON then
# use the form: TABLE.COLUMN
#
# Verify the default settings for short_column_name and full_column_name
#
do_test colname-1.1 {
db eval {PRAGMA short_column_names}
} {1}
do_test colname-1.2 {
db eval {PRAGMA full_column_names}
} {0}
# Tests for then short=ON and full=any
#
do_test colname-2.1 {
db eval {
CREATE TABLE tabc(a,b,c);
INSERT INTO tabc VALUES(1,2,3);
CREATE TABLE txyz(x,y,z);
INSERT INTO txyz VALUES(4,5,6);
CREATE TABLE tboth(a,b,c,x,y,z);
INSERT INTO tboth VALUES(11,12,13,14,15,16);
CREATE VIEW v1 AS SELECT tabC.a, txyZ.x, *
FROM tabc, txyz ORDER BY 1 LIMIT 1;
CREATE VIEW v2 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
}
execsql2 {
SELECT * FROM tabc;
}
} {a 1 b 2 c 3}
do_test colname-2.2 {
execsql2 {
SELECT Tabc.a, tAbc.b, taBc.c, * FROM tabc
}
} {a 1 b 2 c 3 a 1 b 2 c 3}
do_test colname-2.3 {
execsql2 {
SELECT +tabc.a, -tabc.b, tabc.c, * FROM tabc
}
} {+tabc.a 1 -tabc.b -2 c 3 a 1 b 2 c 3}
do_test colname-2.4 {
execsql2 {
SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC, * FROM tabc
}
} {AAA 1 BBB -2 CCC 3 a 1 b 2 c 3}
do_test colname-2.5 {
execsql2 {
SELECT tabc.a, txyz.x, * FROM tabc, txyz;
}
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-2.6 {
execsql2 {
SELECT tabc.a, txyz.x, tabc.*, txyz.* FROM tabc, txyz;
}
} {a 1 x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-2.7 {
execsql2 {
SELECT tabc.a, txyz.x, tboth.a, tboth.x, * FROM tabc, txyz, tboth;
}
} {a 11 x 14 a 11 x 14 a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
do_test colname-2.8 {
execsql2 {
SELECT * FROM v1 ORDER BY 2;
}
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-2.9 {
execsql2 {
SELECT * FROM v2 ORDER BY 2;
}
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
# Tests for short=OFF and full=OFF
#
do_test colname-3.1 {
db eval {
PRAGMA short_column_names=OFF;
PRAGMA full_column_names=OFF;
CREATE VIEW v3 AS SELECT tabC.a, txyZ.x, *
FROM tabc, txyz ORDER BY 1 LIMIT 1;
CREATE VIEW v4 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
}
execsql2 {
SELECT * FROM tabc;
}
} {a 1 b 2 c 3}
do_test colname-3.2 {
execsql2 {
SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
}
} {Tabc.a 1 tAbc.b 2 taBc.c 3}
do_test colname-3.3 {
execsql2 {
SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
}
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
do_test colname-3.4 {
execsql2 {
SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
}
} {AAA 1 BBB -2 CCC 3}
do_test colname-3.5 {
execsql2 {
SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
}
} {Tabc.a 1 Txyz.x 4 a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-3.6 {
execsql2 {
SELECT tabc.*, txyz.* FROM tabc, txyz;
}
} {a 1 b 2 c 3 x 4 y 5 z 6}
do_test colname-3.7 {
execsql2 {
SELECT * FROM tabc, txyz, tboth;
}
} {a 11 b 12 c 13 x 14 y 15 z 16 a 11 b 12 c 13 x 14 y 15 z 16}
do_test colname-3.8 {
execsql2 {
SELECT v1.a, * FROM v1 ORDER BY 2;
}
} {v1.a 1 a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-3.9 {
execsql2 {
SELECT * FROM v2 ORDER BY 2;
}
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
do_test colname-3.10 {
execsql2 {
SELECT * FROM v3 ORDER BY 2;
}
} {a 1 x 4 a:1 1 b 2 c 3 x:1 4 y 5 z 6}
do_test colname-3.11 {
execsql2 {
SELECT * FROM v4 ORDER BY 2;
}
} {a 1 x 4 a:1 11 x:1 14 a:2 1 b 2 c 3 x:2 4 y 5 z 6 a:3 11 b:1 12 c:1 13 x:3 14 y:1 15 z:1 16}
# Test for short=OFF and full=ON
#
do_test colname-4.1 {
db eval {
PRAGMA short_column_names=OFF;
PRAGMA full_column_names=ON;
CREATE VIEW v5 AS SELECT tabC.a, txyZ.x, *
FROM tabc, txyz ORDER BY 1 LIMIT 1;
CREATE VIEW v6 AS SELECT tabC.a, txyZ.x, tboTh.a, tbotH.x, *
FROM tabc, txyz, tboth ORDER BY 1 LIMIT 1;
}
execsql2 {
SELECT * FROM tabc;
}
} {tabc.a 1 tabc.b 2 tabc.c 3}
do_test colname-4.2 {
execsql2 {
SELECT Tabc.a, tAbc.b, taBc.c FROM tabc
}
} {tabc.a 1 tabc.b 2 tabc.c 3}
do_test colname-4.3 {
execsql2 {
SELECT +tabc.a, -tabc.b, tabc.c FROM tabc
}
} {+tabc.a 1 -tabc.b -2 tabc.c 3}
do_test colname-4.4 {
execsql2 {
SELECT +tabc.a AS AAA, -tabc.b AS BBB, tabc.c CCC FROM tabc
}
} {AAA 1 BBB -2 CCC 3}
do_test colname-4.5 {
execsql2 {
SELECT Tabc.a, Txyz.x, * FROM tabc, txyz;
}
} {tabc.a 1 txyz.x 4 tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
do_test colname-4.6 {
execsql2 {
SELECT tabc.*, txyz.* FROM tabc, txyz;
}
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6}
do_test colname-4.7 {
execsql2 {
SELECT * FROM tabc, txyz, tboth;
}
} {tabc.a 1 tabc.b 2 tabc.c 3 txyz.x 4 txyz.y 5 txyz.z 6 tboth.a 11 tboth.b 12 tboth.c 13 tboth.x 14 tboth.y 15 tboth.z 16}
do_test colname-4.8 {
execsql2 {
SELECT * FROM v1 ORDER BY 2;
}
} {v1.a 1 v1.x 4 v1.a:1 1 v1.b 2 v1.c 3 v1.x:1 4 v1.y 5 v1.z 6}
do_test colname-4.9 {
execsql2 {
SELECT * FROM v2 ORDER BY 2;
}
} {v2.a 1 v2.x 4 v2.a:1 11 v2.x:1 14 v2.a:2 1 v2.b 2 v2.c 3 v2.x:2 4 v2.y 5 v2.z 6 v2.a:3 11 v2.b:1 12 v2.c:1 13 v2.x:3 14 v2.y:1 15 v2.z:1 16}
do_test colname-4.10 {
execsql2 {
SELECT * FROM v3 ORDER BY 2;
}
} {v3.a 1 v3.x 4 v3.a:1 1 v3.b 2 v3.c 3 v3.x:1 4 v3.y 5 v3.z 6}
do_test colname-4.11 {
execsql2 {
SELECT * FROM v4 ORDER BY 2;
}
} {v4.a 1 v4.x 4 v4.a:1 11 v4.x:1 14 v4.a:2 1 v4.b 2 v4.c 3 v4.x:2 4 v4.y 5 v4.z 6 v4.a:3 11 v4.b:1 12 v4.c:1 13 v4.x:3 14 v4.y:1 15 v4.z:1 16}
do_test colname-4.12 {
execsql2 {
SELECT * FROM v5 ORDER BY 2;
}
} {v5.a 1 v5.x 4 v5.a:1 1 v5.b 2 v5.c 3 v5.x:1 4 v5.y 5 v5.z 6}
do_test colname-4.13 {
execsql2 {
SELECT * FROM v6 ORDER BY 2;
}
} {v6.a 1 v6.x 4 v6.a:1 11 v6.x:1 14 v6.a:2 1 v6.b 2 v6.c 3 v6.x:2 4 v6.y 5 v6.z 6 v6.a:3 11 v6.b:1 12 v6.c:1 13 v6.x:3 14 v6.y:1 15 v6.z:1 16}
# ticket #3229
do_test colname-5.1 {
lreplace [db eval {
SELECT x.* FROM sqlite_master X LIMIT 1;
}] 3 3 x
} {table tabc tabc x {CREATE TABLE tabc(a,b,c)}}
# ticket #3370, #3371, #3372
#
do_test colname-6.1 {
db close
sqlite3 db test.db
db eval {
CREATE TABLE t6(a, ['a'], ["a"], "[a]", [`a`]);
INSERT INTO t6 VALUES(1,2,3,4,5);
}
execsql2 {SELECT * FROM t6}
} {a 1 'a' 2 {"a"} 3 {[a]} 4 `a` 5}
do_test colname-6.2 {
execsql2 {SELECT ['a'], [`a`], "[a]", [a], ["a"] FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.3 {
execsql2 {SELECT "'a'", "`a`", "[a]", "a", """a""" FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.4 {
execsql2 {SELECT `'a'`, ```a```, `[a]`, `a`, `"a"` FROM t6}
} {'a' 2 `a` 5 {[a]} 4 a 1 {"a"} 3}
do_test colname-6.11 {
execsql2 {SELECT a, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.12 {
execsql2 {SELECT `a`, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.13 {
execsql2 {SELECT "a", max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.14 {
execsql2 {SELECT [a], max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.15 {
execsql2 {SELECT t6.a, max(a) AS m FROM t6}
} {a 1 m 1}
do_test colname-6.16 {
execsql2 {SELECT ['a'], max(['a']) AS m FROM t6}
} {'a' 2 m 2}
do_test colname-6.17 {
execsql2 {SELECT ["a"], max(["a"]) AS m FROM t6}
} {{"a"} 3 m 3}
do_test colname-6.18 {
execsql2 {SELECT "[a]", max("[a]") AS m FROM t6}
} {{[a]} 4 m 4}
do_test colname-6.19 {
execsql2 {SELECT "`a`", max([`a`]) AS m FROM t6}
} {`a` 5 m 5}
# Ticket #3429
# We cannot find anything wrong, but it never hurts to add another
# test case.
#
do_test colname-7.1 {
db eval {
CREATE TABLE t7(x INTEGER PRIMARY KEY, y);
INSERT INTO t7 VALUES(1,2);
}
execsql2 {SELECT rowid, * FROM t7}
} {x 1 x 1 y 2}
# Tickets #3893 and #3984. (Same problem; independently reported)
#
do_test colname-8.1 {
db eval {
CREATE TABLE "t3893"("x");
INSERT INTO t3893 VALUES(123);
SELECT "y"."x" FROM (SELECT "x" FROM "t3893") AS "y";
}
} {123}
# 2017-07-29: Interaction between column naming and query flattening.
# For years now, the query flattener has inserted AS clauses on the
# outer query that were the original SQL text of the column. This caused
# column-name shifts when the query flattener was enhanced, breaking
# legacy applications. See https://sqlite.org/src/info/41c27bc0ff1d3135
# for details.
#
# To fix this, the column naming logic was moved ahead of the query
# flattener so that column names are assigned before the query flattener
# runs.
#
db close
sqlite3 db :memory:
do_test colname-9.100 {
db eval {
CREATE TABLE t1(a,b);
INSERT INTO t1 VALUES(1,2);
CREATE VIEW v1(x,y) AS SELECT a,b FROM t1;
}
execsql2 {SELECT v1.x, (Y) FROM v1}
# Prior to the fix, this would return: "v1.x 1 (Y) 2"
} {x 1 y 2}
do_test colname-9.110 {
execsql2 {SELECT * FROM v1}
} {x 1 y 2}
do_test colname-9.120 {
db eval {
CREATE VIEW v2(x,y) AS SELECT a,b FROM t1 LIMIT 10;
}
execsql2 {SELECT * FROM v2 WHERE 1}
} {x 1 y 2}
do_test colname-9.130 {
execsql2 {SELECT v2.x, [v2].[y] FROM v2 WHERE 1}
} {x 1 y 2}
do_test colname-9.140 {
execsql2 {SELECT +x, +y FROM v2 WHERE 1}
} {+x 1 +y 2}
do_test colname-9.200 {
db eval {
CREATE TABLE t2(c,d);
INSERT INTO t2 VALUES(3,4);
CREATE VIEW v3 AS SELECT c AS a, d AS b FROM t2;
}
execsql2 {SELECT t1.a, v3.a AS n FROM t1 LEFT JOIN v3}
} {a 1 n 3}
do_test colname-9.211 {
execsql2 {SELECT t1.a AS n, v3.a FROM t1 JOIN v3}
} {n 1 a 3}
do_test colname-9.210 {
execsql2 {SELECT t1.a, v3.a AS n FROM t1 JOIN v3}
} {a 1 n 3}
# 2017-12-23: Ticket https://www.sqlite.org/src/info/3b4450072511e621
# Inconsistent column names in CREATE TABLE AS
#
# Verify that the names of columns in the created table of a CREATE TABLE AS
# are the same as the names of result columns in the SELECT statement.
#
do_execsql_test colname-9.300 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(aaa INT);
INSERT INTO t1(aaa) VALUES(123);
}
do_test colname-9.310 {
execsql2 {SELECT BBb FROM (SELECT aaa AS Bbb FROM t1)}
} {Bbb 123}
ifcapable vtab {
do_execsql_test colname-9.320 {
CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1);
SELECT name FROM pragma_table_info('t2');
} {Bbb}
}
do_execsql_test colname-9.330 { -- added 2019-08-10 to invalidate
DROP TABLE IF EXISTS t1; -- a couple assert()s that were
CREATE TABLE t1(a); -- added by ticket 3b44500725
INSERT INTO t1 VALUES(17),(2),(99),(-3),(7);
SELECT (SELECT avg(a) UNION SELECT min(a) OVER()) FROM t1;
} {17}
# Issue detected by OSSFuzz on 2017-12-24 (Christmas Eve)
# caused by check-in https://sqlite.org/src/info/6b2ff26c25
#
# Prior to being fixed, the following CREATE TABLE was dereferencing
# a NULL pointer and segfaulting.
#
do_catchsql_test colname-9.400 {
CREATE TABLE t4 AS SELECT #0;
} {1 {near "#0": syntax error}}
# Issue detected by OSSFuzz on 2017-12-25 (Christmas Day)
# also caused by check-in https://sqlite.org/src/info/6b2ff26c25
#
# Prior to being fixed, the following CREATE TABLE caused an
# assertion fault.
#
do_catchsql_test colname-9.410 {
CREATE TABLE t5 AS SELECT RAISE(abort,a);
} {1 {RAISE() may only be used within a trigger-program}}
# Make sure the quotation marks get removed from the column names
# when constructing a new table from an aggregate SELECT.
# Email from Juergen Palm on 2017-07-11.
#
do_execsql_test colname-10.100 {
DROP TABLE IF EXISTS t1;
CREATE TABLE t1("with space" TEXT);
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 AS SELECT "with space" FROM t1;
PRAGMA table_info(t2);
} {0 {with space} TEXT 0 {} 0}
do_execsql_test colname-10.110 {
DROP TABLE IF EXISTS t3;
CREATE TABLE t3 AS SELECT "with space" FROM t1 GROUP BY 1;
PRAGMA table_info(t3);
} {0 {with space} TEXT 0 {} 0}
finish_test
|