SQLite

Check-in [8f184e40ff]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Additional test cases for tkt2822. Fix a related bug in printf(). (CVS 4624)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8f184e40ff79c3c5ef98337615f58a4a6d0249dd
User & Date: drh 2007-12-13 17:50:23.000
Context
2007-12-13
18:20
Add new makefile target for "testcli" that builds a CLI using individual source files instead of the amalgamation. Ticket #2838. (CVS 4625) (check-in: b6f04e5b93 user: drh tags: trunk)
17:50
Additional test cases for tkt2822. Fix a related bug in printf(). (CVS 4624) (check-in: 8f184e40ff user: drh tags: trunk)
08:15
Return an error if the user attempts to rename a view. Related to (but not a fix for) #2831. (CVS 4623) (check-in: 19d56d997f user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/printf.c.
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
        }
        if( longvalue==0 ) flag_alternateform = 0;
        if( flag_zeropad && precision<width-(prefix!=0) ){
          precision = width-(prefix!=0);
        }
        bufpt = &buf[etBUFSIZE-1];
        if( xtype==etORDINAL ){







          bufpt -= 2;
        }
        {
          register const char *cset;      /* Use registers for speed */
          register int base;
          cset = &aDigits[infop->charset];
          base = infop->base;
          do{                                           /* Convert to ascii */
            *(--bufpt) = cset[longvalue%base];
            longvalue = longvalue/base;
          }while( longvalue>0 );
        }
        if( xtype==etORDINAL ){
          static const char zOrd[] = "thstndrd";
          int x = buf[etBUFSIZE-4] - '0';
          if( x>=4 ) x = 0;
          buf[etBUFSIZE-3] = zOrd[x*2];
          buf[etBUFSIZE-2] = zOrd[x*2+1];
        }
        length = &buf[etBUFSIZE-1]-bufpt;
        for(idx=precision-length; idx>0; idx--){
          *(--bufpt) = '0';                             /* Zero pad */
        }
        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
          const char *pre;







>
>
>
>
>
>
>












<
<
<
<
<
<
<







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
        }
        if( longvalue==0 ) flag_alternateform = 0;
        if( flag_zeropad && precision<width-(prefix!=0) ){
          precision = width-(prefix!=0);
        }
        bufpt = &buf[etBUFSIZE-1];
        if( xtype==etORDINAL ){
          static const char zOrd[] = "thstndrd";
          int x = longvalue % 10;
          if( x>=4 || (longvalue/10)%10==1 ){
            x = 0;
          }
          buf[etBUFSIZE-3] = zOrd[x*2];
          buf[etBUFSIZE-2] = zOrd[x*2+1];
          bufpt -= 2;
        }
        {
          register const char *cset;      /* Use registers for speed */
          register int base;
          cset = &aDigits[infop->charset];
          base = infop->base;
          do{                                           /* Convert to ascii */
            *(--bufpt) = cset[longvalue%base];
            longvalue = longvalue/base;
          }while( longvalue>0 );
        }







        length = &buf[etBUFSIZE-1]-bufpt;
        for(idx=precision-length; idx>0; idx--){
          *(--bufpt) = '0';                             /* Zero pad */
        }
        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
          const char *pre;
Changes to test/tkt2822.test.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#***********************************************************************
#
# This file is to test that the issues surrounding expressions in
# ORDER BY clauses on compound SELECT statements raised by ticket
# #2822 have been dealt with.
#
# $Id: tkt2822.test,v 1.3 2007/12/13 07:58:51 danielk1977 Exp $
#

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

# The ORDER BY matching algorithm is three steps:
# 







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#***********************************************************************
#
# This file is to test that the issues surrounding expressions in
# ORDER BY clauses on compound SELECT statements raised by ticket
# #2822 have been dealt with.
#
# $Id: tkt2822.test,v 1.4 2007/12/13 17:50:23 drh Exp $
#

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

# The ORDER BY matching algorithm is three steps:
# 
31
32
33
34
35
36
37
38

39











40
41
42
43
44
45
46
#   (3)  Evaluate the term as an expression and sort by the
#        value of the expression.
# 
# For a compound SELECT the rules are modified slightly.
# In the third rule, the expression must exactly match one
# of the result columns.  The sequences of three rules is
# attempted first on the left-most SELECT.  If that doesn't
# work, we move to the right, one by one. This is not standard

# SQL, it is an SQLite extension.











#


# Test plan:
#
#   tkt2822-1.* - Simple identifier as ORDER BY expression.
#   tkt2822-2.* - More complex ORDER BY expressions.







|
>
|
>
>
>
>
>
>
>
>
>
>
>







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
#   (3)  Evaluate the term as an expression and sort by the
#        value of the expression.
# 
# For a compound SELECT the rules are modified slightly.
# In the third rule, the expression must exactly match one
# of the result columns.  The sequences of three rules is
# attempted first on the left-most SELECT.  If that doesn't
# work, we move to the right, one by one.
#
# Rule (3) is not in standard SQL - it is an SQLite extension,
# though one copied from PostgreSQL.  The rule for compound
# queries where a search is made of SELECTs to the right
# if the left-most SELECT does not match is not a part of
# standard SQL either.  This extension is unique to SQLite
# as far as we know.
#
# Rule (2) was added by the changes ticket #2822.  Prior to
# that changes, SQLite did not support rule (2), making it
# technically in violation of standard SQL semantics.  
# No body noticed because rule (3) has the same effect as
# rule (2) except in some obscure cases.
#


# Test plan:
#
#   tkt2822-1.* - Simple identifier as ORDER BY expression.
#   tkt2822-2.* - More complex ORDER BY expressions.
152
153
154
155
156
157
158










159

160




















































































































































    SELECT a, CAST (b AS TEXT) AS x, c FROM t1 
      UNION ALL 
    SELECT a, b, c FROM t2 
      ORDER BY CAST (b AS INTEGER);
  }
} {1 {1st ORDER BY term does not match any column in the result set}}











finish_test





























































































































































>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
    SELECT a, CAST (b AS TEXT) AS x, c FROM t1 
      UNION ALL 
    SELECT a, b, c FROM t2 
      ORDER BY CAST (b AS INTEGER);
  }
} {1 {1st ORDER BY term does not match any column in the result set}}

# Tests for rule (2).
#
# The "ORDER BY b" should match the column alias (rule 2), not the
# the t3.b value (rule 3).  
#
do_test tkt2822-5.1 {
  execsql {
    CREATE TABLE t3(a,b);
    INSERT INTO t3 VALUES(1,8);
    INSERT INTO t3 VALUES(9,2);

    SELECT a AS b FROM t3 ORDER BY b;
  }
} {1 9}
do_test tkt2822-5.2 {
  # Case does not matter.  b should match B
  execsql {
    SELECT a AS b FROM t3 ORDER BY B;
  }
} {1 9}
do_test tkt2822-5.3 {
  # Quoting should not matter
  execsql {
    SELECT a AS 'b' FROM t3 ORDER BY "B";
  }
} {1 9}
do_test tkt2822-5.4 {
  # Quoting should not matter
  execsql {
    SELECT a AS "b" FROM t3 ORDER BY [B];
  }
} {1 9}

# In "ORDER BY +b" the term is now an expression rather than
# a label.  It therefore matches by rule (3) instead of rule (2).
# 
do_test tkt2822-5.5 {
  execsql {
    SELECT a AS b FROM t3 ORDER BY +b;
  }
} {9 1}

# Tests for rule 2 in compound queries
#
do_test tkt2822-6.1 {
  execsql {
    CREATE TABLE t6a(p,q);
    INSERT INTO t6a VALUES(1,8);
    INSERT INTO t6a VALUES(9,2);
    CREATE TABLE t6b(x,y);
    INSERT INTO t6b VALUES(1,7);
    INSERT INTO t6b VALUES(7,2);

    SELECT p, q FROM t6a UNION ALL SELECT x, y FROM t6b ORDER BY 1, 2
  }
} {1 7 1 8 7 2 9 2}
do_test tkt2822-6.2 {
  execsql {
    SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
    ORDER BY PX, YX
  }
} {1 7 1 8 7 2 9 2}
do_test tkt2822-6.3 {
  execsql {
    SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
    ORDER BY XX, QX
  }
} {1 7 1 8 7 2 9 2}
do_test tkt2822-6.4 {
  execsql {
    SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
    ORDER BY QX, XX
  }
} {7 2 9 2 1 7 1 8}
do_test tkt2822-6.5 {
  execsql {
    SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
    ORDER BY t6b.x, QX
  }
} {1 7 1 8 7 2 9 2}
do_test tkt2822-6.6 {
  execsql {
    SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
    ORDER BY t6a.q, XX
  }
} {7 2 9 2 1 7 1 8}

# More error message tests.  This is really more of a test of the
# %r ordinal value formatting capablity added to sqlite3_snprintf()
# by ticket #2822.
#
do_test tkt2822-7.1 {
  execsql {
    CREATE TABLE t7(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,
                    a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25);
  }
  catchsql {
    SELECT * FROM t7 ORDER BY 0;
  }
} {1 {1st ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.2 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 0;
  }
} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.3 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 0;
  }
} {1 {3rd ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.4 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 0;
  }
} {1 {4th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.9 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 0;
  }
} {1 {9th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.10 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 0;
  }
} {1 {10th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.11 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0;
  }
} {1 {11th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.12 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 0;
  }
} {1 {12th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.13 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 0;
  }
} {1 {13th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.20 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                             11,12,13,14,15,16,17,18,19, 0
  }
} {1 {20th ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.21 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                             11,12,13,14,15,16,17,18,19, 20, 0
  }
} {1 {21st ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.22 {
  catchsql {
    SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                             11,12,13,14,15,16,17,18,19, 20, 21, 0
  }
} {1 {22nd ORDER BY term out of range - should be between 1 and 25}}


finish_test