3
# The author disclaims copyright to this source code. In place of
4
# a legal notice, here is a blessing:
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.
10
#***********************************************************************
12
# This file is to test that the issues surrounding expressions in
13
# ORDER BY clauses on compound SELECT statements raised by ticket
14
# #2822 have been dealt with.
16
# $Id: tkt2822.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
19
set testdir [file dirname $argv0]
20
source $testdir/tester.tcl
27
# The ORDER BY matching algorithm is three steps:
29
# (1) If the ORDER BY term is an integer constant i, then
30
# sort by the i-th column of the result set.
32
# (2) If the ORDER BY term is an identifier (not x.y or x.y.z
33
# but simply x) then look for a column alias with the same
34
# name. If found, then sort by that column.
36
# (3) Evaluate the term as an expression and sort by the
37
# value of the expression.
39
# For a compound SELECT the rules are modified slightly.
40
# In the third rule, the expression must exactly match one
41
# of the result columns. The sequences of three rules is
42
# attempted first on the left-most SELECT. If that doesn't
43
# work, we move to the right, one by one.
45
# Rule (3) is not in standard SQL - it is an SQLite extension,
46
# though one copied from PostgreSQL. The rule for compound
47
# queries where a search is made of SELECTs to the right
48
# if the left-most SELECT does not match is not a part of
49
# standard SQL either. This extension is unique to SQLite
52
# Rule (2) was added by the changes ticket #2822. Prior to
53
# that changes, SQLite did not support rule (2), making it
54
# technically in violation of standard SQL semantics.
55
# No body noticed because rule (3) has the same effect as
56
# rule (2) except in some obscure cases.
62
# tkt2822-1.* - Simple identifier as ORDER BY expression.
63
# tkt2822-2.* - More complex ORDER BY expressions.
67
CREATE TABLE t1(a, b, c);
68
CREATE TABLE t2(a, b, c);
70
INSERT INTO t1 VALUES(1, 3, 9);
71
INSERT INTO t1 VALUES(3, 9, 27);
72
INSERT INTO t1 VALUES(5, 15, 45);
74
INSERT INTO t2 VALUES(2, 6, 18);
75
INSERT INTO t2 VALUES(4, 12, 36);
76
INSERT INTO t2 VALUES(6, 18, 54);
80
# Test the "ORDER BY <integer>" syntax.
84
SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY 1;
86
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
89
SELECT a, CAST (b AS TEXT), c FROM t1
91
SELECT a, b, c FROM t2
94
} {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
96
# Test the "ORDER BY <identifier>" syntax.
100
SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY a;
102
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
104
do_test tkt2822-2.2 {
106
SELECT a, CAST (b AS TEXT) AS x, c FROM t1
108
SELECT a, b, c FROM t2
111
} {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
112
do_test tkt2822-2.3 {
114
SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY a;
116
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
118
# Test the "ORDER BY <expression>" syntax.
120
do_test tkt2822-3.1 {
122
SELECT a, CAST (b AS TEXT) AS x, c FROM t1
124
SELECT a, b, c FROM t2
125
ORDER BY CAST (b AS TEXT);
127
} {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
128
do_test tkt2822-3.2 {
130
SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY t1.a;
132
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
134
# Test that if a match cannot be found in the leftmost SELECT, an
135
# attempt is made to find a match in subsequent SELECT statements.
137
do_test tkt2822-3.1 {
139
SELECT a, b, c FROM t1 UNION ALL SELECT a AS x, b, c FROM t2 ORDER BY x;
141
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
142
do_test tkt2822-3.2 {
143
# But the leftmost SELECT takes precedence.
145
SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1
147
SELECT a, b, c FROM t2
150
} {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
151
do_test tkt2822-3.3 {
153
SELECT a, b, c FROM t2
155
SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1
158
} {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
160
# Test some error conditions (ORDER BY clauses that match no column).
162
do_test tkt2822-4.1 {
164
SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY x
166
} {1 {1st ORDER BY term does not match any column in the result set}}
167
do_test tkt2822-4.2 {
169
SELECT a, CAST (b AS TEXT) AS x, c FROM t1
171
SELECT a, b, c FROM t2
172
ORDER BY CAST (b AS INTEGER);
174
} {1 {1st ORDER BY term does not match any column in the result set}}
176
# Tests for rule (2).
178
# The "ORDER BY b" should match the column alias (rule 2), not the
179
# the t3.b value (rule 3).
181
do_test tkt2822-5.1 {
183
CREATE TABLE t3(a,b);
184
INSERT INTO t3 VALUES(1,8);
185
INSERT INTO t3 VALUES(9,2);
187
SELECT a AS b FROM t3 ORDER BY b;
190
do_test tkt2822-5.2 {
191
# Case does not matter. b should match B
193
SELECT a AS b FROM t3 ORDER BY B;
196
do_test tkt2822-5.3 {
197
# Quoting should not matter
199
SELECT a AS 'b' FROM t3 ORDER BY "B";
202
do_test tkt2822-5.4 {
203
# Quoting should not matter
205
SELECT a AS "b" FROM t3 ORDER BY [B];
209
# In "ORDER BY +b" the term is now an expression rather than
210
# a label. It therefore matches by rule (3) instead of rule (2).
212
do_test tkt2822-5.5 {
214
SELECT a AS b FROM t3 ORDER BY +b;
218
# Tests for rule 2 in compound queries
220
do_test tkt2822-6.1 {
222
CREATE TABLE t6a(p,q);
223
INSERT INTO t6a VALUES(1,8);
224
INSERT INTO t6a VALUES(9,2);
225
CREATE TABLE t6b(x,y);
226
INSERT INTO t6b VALUES(1,7);
227
INSERT INTO t6b VALUES(7,2);
229
SELECT p, q FROM t6a UNION ALL SELECT x, y FROM t6b ORDER BY 1, 2
232
do_test tkt2822-6.2 {
234
SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
238
do_test tkt2822-6.3 {
240
SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
244
do_test tkt2822-6.4 {
246
SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
250
do_test tkt2822-6.5 {
252
SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
256
do_test tkt2822-6.6 {
258
SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
263
# More error message tests. This is really more of a test of the
264
# %r ordinal value formatting capablity added to sqlite3_snprintf()
267
do_test tkt2822-7.1 {
269
CREATE TABLE t7(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,
270
a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25);
273
SELECT * FROM t7 ORDER BY 0;
275
} {1 {1st ORDER BY term out of range - should be between 1 and 25}}
276
do_test tkt2822-7.2 {
278
SELECT * FROM t7 ORDER BY 1, 0;
280
} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
281
do_test tkt2822-7.3 {
283
SELECT * FROM t7 ORDER BY 1, 2, 0;
285
} {1 {3rd ORDER BY term out of range - should be between 1 and 25}}
286
do_test tkt2822-7.4 {
288
SELECT * FROM t7 ORDER BY 1, 2, 3, 0;
290
} {1 {4th ORDER BY term out of range - should be between 1 and 25}}
291
do_test tkt2822-7.9 {
293
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 0;
295
} {1 {9th ORDER BY term out of range - should be between 1 and 25}}
296
do_test tkt2822-7.10 {
298
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 0;
300
} {1 {10th ORDER BY term out of range - should be between 1 and 25}}
301
do_test tkt2822-7.11 {
303
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0;
305
} {1 {11th ORDER BY term out of range - should be between 1 and 25}}
306
do_test tkt2822-7.12 {
308
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 0;
310
} {1 {12th ORDER BY term out of range - should be between 1 and 25}}
311
do_test tkt2822-7.13 {
313
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 0;
315
} {1 {13th ORDER BY term out of range - should be between 1 and 25}}
316
do_test tkt2822-7.20 {
318
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
319
11,12,13,14,15,16,17,18,19, 0
321
} {1 {20th ORDER BY term out of range - should be between 1 and 25}}
322
do_test tkt2822-7.21 {
324
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
325
11,12,13,14,15,16,17,18,19, 20, 0
327
} {1 {21st ORDER BY term out of range - should be between 1 and 25}}
328
do_test tkt2822-7.22 {
330
SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
331
11,12,13,14,15,16,17,18,19, 20, 21, 0
333
} {1 {22nd ORDER BY term out of range - should be between 1 and 25}}