/ Hex Artifact Content
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Artifact f391776423a7c0d0949edfce375708bfb0f3141e:


0000: 23 20 32 30 30 37 20 44 65 63 20 34 0a 23 0a 23  # 2007 Dec 4.#.#
0010: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
0020: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
0030: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
0040: 6f 64 65 2e 20 49 6e 20 70 6c 61 63 65 20 6f 66  ode. In place of
0050: 0a 23 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  .# a legal notic
0060: 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
0070: 73 73 69 6e 67 3a 0a 23 0a 23 20 20 20 20 4d 61  ssing:.#.#    Ma
0080: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
0090: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 23 20 20 20  d not evil..#   
00a0: 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
00b0: 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
00c0: 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
00d0: 76 65 20 6f 74 68 65 72 73 2e 0a 23 20 20 20 20  ve others..#    
00e0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
00f0: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
0100: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
0110: 20 67 69 76 65 2e 0a 23 0a 23 2a 2a 2a 2a 2a 2a   give..#.#******
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 0a 23 0a 23 20 54 68 69 73 20 66 69 6c 65 20  *.#.# This file 
0170: 69 73 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  is to test that 
0180: 74 68 65 20 69 73 73 75 65 73 20 73 75 72 72 6f  the issues surro
0190: 75 6e 64 69 6e 67 20 65 78 70 72 65 73 73 69 6f  unding expressio
01a0: 6e 73 20 69 6e 0a 23 20 4f 52 44 45 52 20 42 59  ns in.# ORDER BY
01b0: 20 63 6c 61 75 73 65 73 20 6f 6e 20 63 6f 6d 70   clauses on comp
01c0: 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74  ound SELECT stat
01d0: 65 6d 65 6e 74 73 20 72 61 69 73 65 64 20 62 79  ements raised by
01e0: 20 74 69 63 6b 65 74 0a 23 20 23 32 38 32 32 20   ticket.# #2822 
01f0: 68 61 76 65 20 62 65 65 6e 20 64 65 61 6c 74 20  have been dealt 
0200: 77 69 74 68 2e 0a 23 0a 23 20 24 49 64 3a 20 74  with..#.# $Id: t
0210: 6b 74 32 38 32 32 2e 74 65 73 74 2c 76 20 31 2e  kt2822.test,v 1.
0220: 36 20 32 30 30 38 2f 30 38 2f 32 30 20 31 36 3a  6 2008/08/20 16:
0230: 33 35 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a  35:10 drh Exp $.
0240: 23 0a 0a 73 65 74 20 74 65 73 74 64 69 72 20 5b  #..set testdir [
0250: 66 69 6c 65 20 64 69 72 6e 61 6d 65 20 24 61 72  file dirname $ar
0260: 67 76 30 5d 0a 73 6f 75 72 63 65 20 24 74 65 73  gv0].source $tes
0270: 74 64 69 72 2f 74 65 73 74 65 72 2e 74 63 6c 0a  tdir/tester.tcl.
0280: 0a 69 66 63 61 70 61 62 6c 65 20 21 63 6f 6d 70  .ifcapable !comp
0290: 6f 75 6e 64 20 7b 0a 20 20 66 69 6e 69 73 68 5f  ound {.  finish_
02a0: 74 65 73 74 0a 20 20 72 65 74 75 72 6e 0a 7d 0a  test.  return.}.
02b0: 0a 23 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  .# The ORDER BY 
02c0: 6d 61 74 63 68 69 6e 67 20 61 6c 67 6f 72 69 74  matching algorit
02d0: 68 6d 20 69 73 20 74 68 72 65 65 20 73 74 65 70  hm is three step
02e0: 73 3a 0a 23 20 0a 23 20 20 20 28 31 29 20 20 49  s:.# .#   (1)  I
02f0: 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74  f the ORDER BY t
0300: 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65  erm is an intege
0310: 72 20 63 6f 6e 73 74 61 6e 74 20 69 2c 20 74 68  r constant i, th
0320: 65 6e 0a 23 20 20 20 20 20 20 20 20 73 6f 72 74  en.#        sort
0330: 20 62 79 20 74 68 65 20 69 2d 74 68 20 63 6f 6c   by the i-th col
0340: 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
0350: 74 20 73 65 74 2e 0a 23 20 0a 23 20 20 20 28 32  t set..# .#   (2
0360: 29 20 20 49 66 20 74 68 65 20 4f 52 44 45 52 20  )  If the ORDER 
0370: 42 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 64  BY term is an id
0380: 65 6e 74 69 66 69 65 72 20 28 6e 6f 74 20 78 2e  entifier (not x.
0390: 79 20 6f 72 20 78 2e 79 2e 7a 0a 23 20 20 20 20  y or x.y.z.#    
03a0: 20 20 20 20 62 75 74 20 73 69 6d 70 6c 79 20 78      but simply x
03b0: 29 20 74 68 65 6e 20 6c 6f 6f 6b 20 66 6f 72 20  ) then look for 
03c0: 61 20 63 6f 6c 75 6d 6e 20 61 6c 69 61 73 20 77  a column alias w
03d0: 69 74 68 20 74 68 65 20 73 61 6d 65 0a 23 20 20  ith the same.#  
03e0: 20 20 20 20 20 20 6e 61 6d 65 2e 20 20 49 66 20        name.  If 
03f0: 66 6f 75 6e 64 2c 20 74 68 65 6e 20 73 6f 72 74  found, then sort
0400: 20 62 79 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e   by that column.
0410: 0a 23 20 0a 23 20 20 20 28 33 29 20 20 45 76 61  .# .#   (3)  Eva
0420: 6c 75 61 74 65 20 74 68 65 20 74 65 72 6d 20 61  luate the term a
0430: 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
0440: 61 6e 64 20 73 6f 72 74 20 62 79 20 74 68 65 0a  and sort by the.
0450: 23 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f  #        value o
0460: 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
0470: 2e 0a 23 20 0a 23 20 46 6f 72 20 61 20 63 6f 6d  ..# .# For a com
0480: 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 74 68 65  pound SELECT the
0490: 20 72 75 6c 65 73 20 61 72 65 20 6d 6f 64 69 66   rules are modif
04a0: 69 65 64 20 73 6c 69 67 68 74 6c 79 2e 0a 23 20  ied slightly..# 
04b0: 49 6e 20 74 68 65 20 74 68 69 72 64 20 72 75 6c  In the third rul
04c0: 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  e, the expressio
04d0: 6e 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20 6d  n must exactly m
04e0: 61 74 63 68 20 6f 6e 65 0a 23 20 6f 66 20 74 68  atch one.# of th
04f0: 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  e result columns
0500: 2e 20 20 54 68 65 20 73 65 71 75 65 6e 63 65 73  .  The sequences
0510: 20 6f 66 20 74 68 72 65 65 20 72 75 6c 65 73 20   of three rules 
0520: 69 73 0a 23 20 61 74 74 65 6d 70 74 65 64 20 66  is.# attempted f
0530: 69 72 73 74 20 6f 6e 20 74 68 65 20 6c 65 66 74  irst on the left
0540: 2d 6d 6f 73 74 20 53 45 4c 45 43 54 2e 20 20 49  -most SELECT.  I
0550: 66 20 74 68 61 74 20 64 6f 65 73 6e 27 74 0a 23  f that doesn't.#
0560: 20 77 6f 72 6b 2c 20 77 65 20 6d 6f 76 65 20 74   work, we move t
0570: 6f 20 74 68 65 20 72 69 67 68 74 2c 20 6f 6e 65  o the right, one
0580: 20 62 79 20 6f 6e 65 2e 0a 23 0a 23 20 52 75 6c   by one..#.# Rul
0590: 65 20 28 33 29 20 69 73 20 6e 6f 74 20 69 6e 20  e (3) is not in 
05a0: 73 74 61 6e 64 61 72 64 20 53 51 4c 20 2d 20 69  standard SQL - i
05b0: 74 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65  t is an SQLite e
05c0: 78 74 65 6e 73 69 6f 6e 2c 0a 23 20 74 68 6f 75  xtension,.# thou
05d0: 67 68 20 6f 6e 65 20 63 6f 70 69 65 64 20 66 72  gh one copied fr
05e0: 6f 6d 20 50 6f 73 74 67 72 65 53 51 4c 2e 20 20  om PostgreSQL.  
05f0: 54 68 65 20 72 75 6c 65 20 66 6f 72 20 63 6f 6d  The rule for com
0600: 70 6f 75 6e 64 0a 23 20 71 75 65 72 69 65 73 20  pound.# queries 
0610: 77 68 65 72 65 20 61 20 73 65 61 72 63 68 20 69  where a search i
0620: 73 20 6d 61 64 65 20 6f 66 20 53 45 4c 45 43 54  s made of SELECT
0630: 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 0a 23  s to the right.#
0640: 20 69 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73   if the left-mos
0650: 74 20 53 45 4c 45 43 54 20 64 6f 65 73 20 6e 6f  t SELECT does no
0660: 74 20 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 61  t match is not a
0670: 20 70 61 72 74 20 6f 66 0a 23 20 73 74 61 6e 64   part of.# stand
0680: 61 72 64 20 53 51 4c 20 65 69 74 68 65 72 2e 20  ard SQL either. 
0690: 20 54 68 69 73 20 65 78 74 65 6e 73 69 6f 6e 20   This extension 
06a0: 69 73 20 75 6e 69 71 75 65 20 74 6f 20 53 51 4c  is unique to SQL
06b0: 69 74 65 0a 23 20 61 73 20 66 61 72 20 61 73 20  ite.# as far as 
06c0: 77 65 20 6b 6e 6f 77 2e 0a 23 0a 23 20 52 75 6c  we know..#.# Rul
06d0: 65 20 28 32 29 20 77 61 73 20 61 64 64 65 64 20  e (2) was added 
06e0: 62 79 20 74 68 65 20 63 68 61 6e 67 65 73 20 74  by the changes t
06f0: 69 63 6b 65 74 20 23 32 38 32 32 2e 20 20 50 72  icket #2822.  Pr
0700: 69 6f 72 20 74 6f 0a 23 20 74 68 61 74 20 63 68  ior to.# that ch
0710: 61 6e 67 65 73 2c 20 53 51 4c 69 74 65 20 64 69  anges, SQLite di
0720: 64 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 75  d not support ru
0730: 6c 65 20 28 32 29 2c 20 6d 61 6b 69 6e 67 20 69  le (2), making i
0740: 74 0a 23 20 74 65 63 68 6e 69 63 61 6c 6c 79 20  t.# technically 
0750: 69 6e 20 76 69 6f 6c 61 74 69 6f 6e 20 6f 66 20  in violation of 
0760: 73 74 61 6e 64 61 72 64 20 53 51 4c 20 73 65 6d  standard SQL sem
0770: 61 6e 74 69 63 73 2e 20 20 0a 23 20 4e 6f 20 62  antics.  .# No b
0780: 6f 64 79 20 6e 6f 74 69 63 65 64 20 62 65 63 61  ody noticed beca
0790: 75 73 65 20 72 75 6c 65 20 28 33 29 20 68 61 73  use rule (3) has
07a0: 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74   the same effect
07b0: 20 61 73 0a 23 20 72 75 6c 65 20 28 32 29 20 65   as.# rule (2) e
07c0: 78 63 65 70 74 20 69 6e 20 73 6f 6d 65 20 6f 62  xcept in some ob
07d0: 73 63 75 72 65 20 63 61 73 65 73 2e 0a 23 0a 0a  scure cases..#..
07e0: 0a 23 20 54 65 73 74 20 70 6c 61 6e 3a 0a 23 0a  .# Test plan:.#.
07f0: 23 20 20 20 74 6b 74 32 38 32 32 2d 31 2e 2a 20  #   tkt2822-1.* 
0800: 2d 20 53 69 6d 70 6c 65 20 69 64 65 6e 74 69 66  - Simple identif
0810: 69 65 72 20 61 73 20 4f 52 44 45 52 20 42 59 20  ier as ORDER BY 
0820: 65 78 70 72 65 73 73 69 6f 6e 2e 0a 23 20 20 20  expression..#   
0830: 74 6b 74 32 38 32 32 2d 32 2e 2a 20 2d 20 4d 6f  tkt2822-2.* - Mo
0840: 72 65 20 63 6f 6d 70 6c 65 78 20 4f 52 44 45 52  re complex ORDER
0850: 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 73 2e   BY expressions.
0860: 0a 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32  ..do_test tkt282
0870: 32 2d 30 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  2-0.1 {.  execsq
0880: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
0890: 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20 63 29  ABLE t1(a, b, c)
08a0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
08b0: 4c 45 20 74 32 28 61 2c 20 62 2c 20 63 29 3b 0a  LE t2(a, b, c);.
08c0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
08d0: 20 74 31 20 56 41 4c 55 45 53 28 31 2c 20 33 2c   t1 VALUES(1, 3,
08e0: 20 39 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   9);.    INSERT 
08f0: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 33  INTO t1 VALUES(3
0900: 2c 20 39 2c 20 32 37 29 3b 0a 20 20 20 20 49 4e  , 9, 27);.    IN
0910: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
0920: 55 45 53 28 35 2c 20 31 35 2c 20 34 35 29 3b 0a  UES(5, 15, 45);.
0930: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
0940: 20 74 32 20 56 41 4c 55 45 53 28 32 2c 20 36 2c   t2 VALUES(2, 6,
0950: 20 31 38 29 3b 0a 20 20 20 20 49 4e 53 45 52 54   18);.    INSERT
0960: 20 49 4e 54 4f 20 74 32 20 56 41 4c 55 45 53 28   INTO t2 VALUES(
0970: 34 2c 20 31 32 2c 20 33 36 29 3b 0a 20 20 20 20  4, 12, 36);.    
0980: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56  INSERT INTO t2 V
0990: 41 4c 55 45 53 28 36 2c 20 31 38 2c 20 35 34 29  ALUES(6, 18, 54)
09a0: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 20 54 65  ;.  }.} {}..# Te
09b0: 73 74 20 74 68 65 20 22 4f 52 44 45 52 20 42 59  st the "ORDER BY
09c0: 20 3c 69 6e 74 65 67 65 72 3e 22 20 73 79 6e 74   <integer>" synt
09d0: 61 78 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 74 6b  ax..#.do_test tk
09e0: 74 32 38 32 32 2d 31 2e 31 20 7b 0a 20 20 65 78  t2822-1.1 {.  ex
09f0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
0a00: 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20  CT a, b, c FROM 
0a10: 74 31 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c  t1 UNION ALL SEL
0a20: 45 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d  ECT a, b, c FROM
0a30: 20 74 32 20 4f 52 44 45 52 20 42 59 20 31 3b 0a   t2 ORDER BY 1;.
0a40: 20 20 7d 0a 7d 20 7b 31 20 33 20 39 20 32 20 36    }.} {1 3 9 2 6
0a50: 20 31 38 20 33 20 39 20 32 37 20 34 20 31 32 20   18 3 9 27 4 12 
0a60: 33 36 20 35 20 31 35 20 34 35 20 36 20 31 38 20  36 5 15 45 6 18 
0a70: 35 34 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32  54}.do_test tkt2
0a80: 38 32 32 2d 31 2e 32 20 7b 0a 20 20 65 78 65 63  822-1.2 {.  exec
0a90: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
0aa0: 20 61 2c 20 43 41 53 54 20 28 62 20 41 53 20 54   a, CAST (b AS T
0ab0: 45 58 54 29 2c 20 63 20 46 52 4f 4d 20 74 31 20  EXT), c FROM t1 
0ac0: 0a 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c  .      UNION ALL
0ad0: 20 0a 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20   .    SELECT a, 
0ae0: 62 2c 20 63 20 46 52 4f 4d 20 74 32 20 0a 20 20  b, c FROM t2 .  
0af0: 20 20 20 20 4f 52 44 45 52 20 42 59 20 32 3b 0a      ORDER BY 2;.
0b00: 20 20 7d 0a 7d 20 7b 32 20 36 20 31 38 20 34 20    }.} {2 6 18 4 
0b10: 31 32 20 33 36 20 36 20 31 38 20 35 34 20 35 20  12 36 6 18 54 5 
0b20: 31 35 20 34 35 20 31 20 33 20 39 20 33 20 39 20  15 45 1 3 9 3 9 
0b30: 32 37 7d 0a 0a 23 20 54 65 73 74 20 74 68 65 20  27}..# Test the 
0b40: 22 4f 52 44 45 52 20 42 59 20 3c 69 64 65 6e 74  "ORDER BY <ident
0b50: 69 66 69 65 72 3e 22 20 73 79 6e 74 61 78 2e 0a  ifier>" syntax..
0b60: 23 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32  #.do_test tkt282
0b70: 32 2d 32 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  2-2.1 {.  execsq
0b80: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 61  l {.    SELECT a
0b90: 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 31 20 55  , b, c FROM t1 U
0ba0: 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20  NION ALL SELECT 
0bb0: 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 32 20  a, b, c FROM t2 
0bc0: 4f 52 44 45 52 20 42 59 20 61 3b 0a 20 20 7d 0a  ORDER BY a;.  }.
0bd0: 7d 20 7b 31 20 33 20 39 20 32 20 36 20 31 38 20  } {1 3 9 2 6 18 
0be0: 33 20 39 20 32 37 20 34 20 31 32 20 33 36 20 35  3 9 27 4 12 36 5
0bf0: 20 31 35 20 34 35 20 36 20 31 38 20 35 34 7d 0a   15 45 6 18 54}.
0c00: 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32 32  .do_test tkt2822
0c10: 2d 32 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  -2.2 {.  execsql
0c20: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 61 2c   {.    SELECT a,
0c30: 20 43 41 53 54 20 28 62 20 41 53 20 54 45 58 54   CAST (b AS TEXT
0c40: 29 20 41 53 20 78 2c 20 63 20 46 52 4f 4d 20 74  ) AS x, c FROM t
0c50: 31 20 0a 20 20 20 20 20 20 55 4e 49 4f 4e 20 41  1 .      UNION A
0c60: 4c 4c 20 0a 20 20 20 20 53 45 4c 45 43 54 20 61  LL .    SELECT a
0c70: 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 32 20 0a  , b, c FROM t2 .
0c80: 20 20 20 20 20 20 4f 52 44 45 52 20 42 59 20 78        ORDER BY x
0c90: 3b 0a 20 20 7d 0a 7d 20 7b 32 20 36 20 31 38 20  ;.  }.} {2 6 18 
0ca0: 34 20 31 32 20 33 36 20 36 20 31 38 20 35 34 20  4 12 36 6 18 54 
0cb0: 35 20 31 35 20 34 35 20 31 20 33 20 39 20 33 20  5 15 45 1 3 9 3 
0cc0: 39 20 32 37 7d 0a 64 6f 5f 74 65 73 74 20 74 6b  9 27}.do_test tk
0cd0: 74 32 38 32 32 2d 32 2e 33 20 7b 0a 20 20 65 78  t2822-2.3 {.  ex
0ce0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
0cf0: 43 54 20 74 31 2e 61 2c 20 62 2c 20 63 20 46 52  CT t1.a, b, c FR
0d00: 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 41 4c 4c 20  OM t1 UNION ALL 
0d10: 53 45 4c 45 43 54 20 74 32 2e 61 2c 20 62 2c 20  SELECT t2.a, b, 
0d20: 63 20 46 52 4f 4d 20 74 32 20 4f 52 44 45 52 20  c FROM t2 ORDER 
0d30: 42 59 20 61 3b 0a 20 20 7d 0a 7d 20 7b 31 20 33  BY a;.  }.} {1 3
0d40: 20 39 20 32 20 36 20 31 38 20 33 20 39 20 32 37   9 2 6 18 3 9 27
0d50: 20 34 20 31 32 20 33 36 20 35 20 31 35 20 34 35   4 12 36 5 15 45
0d60: 20 36 20 31 38 20 35 34 7d 0a 0a 23 20 54 65 73   6 18 54}..# Tes
0d70: 74 20 74 68 65 20 22 4f 52 44 45 52 20 42 59 20  t the "ORDER BY 
0d80: 3c 65 78 70 72 65 73 73 69 6f 6e 3e 22 20 73 79  <expression>" sy
0d90: 6e 74 61 78 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  ntax..#.do_test 
0da0: 74 6b 74 32 38 32 32 2d 33 2e 31 20 7b 0a 20 20  tkt2822-3.1 {.  
0db0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45  execsql {.    SE
0dc0: 4c 45 43 54 20 61 2c 20 43 41 53 54 20 28 62 20  LECT a, CAST (b 
0dd0: 41 53 20 54 45 58 54 29 20 41 53 20 78 2c 20 63  AS TEXT) AS x, c
0de0: 20 46 52 4f 4d 20 74 31 20 0a 20 20 20 20 20 20   FROM t1 .      
0df0: 55 4e 49 4f 4e 20 41 4c 4c 20 0a 20 20 20 20 53  UNION ALL .    S
0e00: 45 4c 45 43 54 20 61 2c 20 62 2c 20 63 20 46 52  ELECT a, b, c FR
0e10: 4f 4d 20 74 32 20 0a 20 20 20 20 20 20 4f 52 44  OM t2 .      ORD
0e20: 45 52 20 42 59 20 43 41 53 54 20 28 62 20 41 53  ER BY CAST (b AS
0e30: 20 54 45 58 54 29 3b 0a 20 20 7d 0a 7d 20 7b 32   TEXT);.  }.} {2
0e40: 20 36 20 31 38 20 34 20 31 32 20 33 36 20 36 20   6 18 4 12 36 6 
0e50: 31 38 20 35 34 20 35 20 31 35 20 34 35 20 31 20  18 54 5 15 45 1 
0e60: 33 20 39 20 33 20 39 20 32 37 7d 0a 64 6f 5f 74  3 9 3 9 27}.do_t
0e70: 65 73 74 20 74 6b 74 32 38 32 32 2d 33 2e 32 20  est tkt2822-3.2 
0e80: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
0e90: 20 20 53 45 4c 45 43 54 20 74 31 2e 61 2c 20 62    SELECT t1.a, b
0ea0: 2c 20 63 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f  , c FROM t1 UNIO
0eb0: 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20 74 32 2e  N ALL SELECT t2.
0ec0: 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 32 20  a, b, c FROM t2 
0ed0: 4f 52 44 45 52 20 42 59 20 74 31 2e 61 3b 0a 20  ORDER BY t1.a;. 
0ee0: 20 7d 0a 7d 20 7b 31 20 33 20 39 20 32 20 36 20   }.} {1 3 9 2 6 
0ef0: 31 38 20 33 20 39 20 32 37 20 34 20 31 32 20 33  18 3 9 27 4 12 3
0f00: 36 20 35 20 31 35 20 34 35 20 36 20 31 38 20 35  6 5 15 45 6 18 5
0f10: 34 7d 0a 0a 23 20 54 65 73 74 20 74 68 61 74 20  4}..# Test that 
0f20: 69 66 20 61 20 6d 61 74 63 68 20 63 61 6e 6e 6f  if a match canno
0f30: 74 20 62 65 20 66 6f 75 6e 64 20 69 6e 20 74 68  t be found in th
0f40: 65 20 6c 65 66 74 6d 6f 73 74 20 53 45 4c 45 43  e leftmost SELEC
0f50: 54 2c 20 61 6e 0a 23 20 61 74 74 65 6d 70 74 20  T, an.# attempt 
0f60: 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 20  is made to find 
0f70: 61 20 6d 61 74 63 68 20 69 6e 20 73 75 62 73 65  a match in subse
0f80: 71 75 65 6e 74 20 53 45 4c 45 43 54 20 73 74 61  quent SELECT sta
0f90: 74 65 6d 65 6e 74 73 2e 0a 23 0a 64 6f 5f 74 65  tements..#.do_te
0fa0: 73 74 20 74 6b 74 32 38 32 32 2d 33 2e 33 20 7b  st tkt2822-3.3 {
0fb0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
0fc0: 20 53 45 4c 45 43 54 20 61 2c 20 62 2c 20 63 20   SELECT a, b, c 
0fd0: 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 41 4c  FROM t1 UNION AL
0fe0: 4c 20 53 45 4c 45 43 54 20 61 20 41 53 20 78 2c  L SELECT a AS x,
0ff0: 20 62 2c 20 63 20 46 52 4f 4d 20 74 32 20 4f 52   b, c FROM t2 OR
1000: 44 45 52 20 42 59 20 78 3b 0a 20 20 7d 0a 7d 20  DER BY x;.  }.} 
1010: 7b 31 20 33 20 39 20 32 20 36 20 31 38 20 33 20  {1 3 9 2 6 18 3 
1020: 39 20 32 37 20 34 20 31 32 20 33 36 20 35 20 31  9 27 4 12 36 5 1
1030: 35 20 34 35 20 36 20 31 38 20 35 34 7d 0a 64 6f  5 45 6 18 54}.do
1040: 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d 33 2e  _test tkt2822-3.
1050: 34 20 7b 0a 20 20 23 20 42 75 74 20 74 68 65 20  4 {.  # But the 
1060: 6c 65 66 74 6d 6f 73 74 20 53 45 4c 45 43 54 20  leftmost SELECT 
1070: 74 61 6b 65 73 20 70 72 65 63 65 64 65 6e 63 65  takes precedence
1080: 2e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  ..  execsql {.  
1090: 20 20 53 45 4c 45 43 54 20 61 20 41 53 20 62 2c    SELECT a AS b,
10a0: 20 43 41 53 54 20 28 62 20 41 53 20 54 45 58 54   CAST (b AS TEXT
10b0: 29 20 41 53 20 61 2c 20 63 20 46 52 4f 4d 20 74  ) AS a, c FROM t
10c0: 31 20 0a 20 20 20 20 20 20 55 4e 49 4f 4e 20 41  1 .      UNION A
10d0: 4c 4c 20 0a 20 20 20 20 53 45 4c 45 43 54 20 61  LL .    SELECT a
10e0: 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 32 20 0a  , b, c FROM t2 .
10f0: 20 20 20 20 20 20 4f 52 44 45 52 20 42 59 20 61        ORDER BY a
1100: 3b 0a 20 20 7d 0a 7d 20 7b 32 20 36 20 31 38 20  ;.  }.} {2 6 18 
1110: 34 20 31 32 20 33 36 20 36 20 31 38 20 35 34 20  4 12 36 6 18 54 
1120: 35 20 31 35 20 34 35 20 31 20 33 20 39 20 33 20  5 15 45 1 3 9 3 
1130: 39 20 32 37 7d 0a 64 6f 5f 74 65 73 74 20 74 6b  9 27}.do_test tk
1140: 74 32 38 32 32 2d 33 2e 35 20 7b 0a 20 20 65 78  t2822-3.5 {.  ex
1150: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
1160: 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20  CT a, b, c FROM 
1170: 74 32 20 0a 20 20 20 20 20 20 55 4e 49 4f 4e 20  t2 .      UNION 
1180: 41 4c 4c 20 0a 20 20 20 20 53 45 4c 45 43 54 20  ALL .    SELECT 
1190: 61 20 41 53 20 62 2c 20 43 41 53 54 20 28 62 20  a AS b, CAST (b 
11a0: 41 53 20 54 45 58 54 29 20 41 53 20 61 2c 20 63  AS TEXT) AS a, c
11b0: 20 46 52 4f 4d 20 74 31 20 0a 20 20 20 20 20 20   FROM t1 .      
11c0: 4f 52 44 45 52 20 42 59 20 61 3b 0a 20 20 7d 0a  ORDER BY a;.  }.
11d0: 7d 20 7b 31 20 33 20 39 20 32 20 36 20 31 38 20  } {1 3 9 2 6 18 
11e0: 33 20 39 20 32 37 20 34 20 31 32 20 33 36 20 35  3 9 27 4 12 36 5
11f0: 20 31 35 20 34 35 20 36 20 31 38 20 35 34 7d 0a   15 45 6 18 54}.
1200: 0a 23 20 54 65 73 74 20 73 6f 6d 65 20 65 72 72  .# Test some err
1210: 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 28 4f  or conditions (O
1220: 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 20  RDER BY clauses 
1230: 74 68 61 74 20 6d 61 74 63 68 20 6e 6f 20 63 6f  that match no co
1240: 6c 75 6d 6e 29 2e 0a 23 0a 64 6f 5f 74 65 73 74  lumn)..#.do_test
1250: 20 74 6b 74 32 38 32 32 2d 34 2e 31 20 7b 0a 20   tkt2822-4.1 {. 
1260: 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20   catchsql {.    
1270: 53 45 4c 45 43 54 20 61 2c 20 62 2c 20 63 20 46  SELECT a, b, c F
1280: 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 41 4c 4c  ROM t1 UNION ALL
1290: 20 53 45 4c 45 43 54 20 61 2c 20 62 2c 20 63 20   SELECT a, b, c 
12a0: 46 52 4f 4d 20 74 32 20 4f 52 44 45 52 20 42 59  FROM t2 ORDER BY
12b0: 20 78 0a 20 20 7d 0a 7d 20 7b 31 20 7b 31 73 74   x.  }.} {1 {1st
12c0: 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 64   ORDER BY term d
12d0: 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e  oes not match an
12e0: 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  y column in the 
12f0: 72 65 73 75 6c 74 20 73 65 74 7d 7d 0a 64 6f 5f  result set}}.do_
1300: 74 65 73 74 20 74 6b 74 32 38 32 32 2d 34 2e 32  test tkt2822-4.2
1310: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a   {.  catchsql {.
1320: 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 43 41      SELECT a, CA
1330: 53 54 20 28 62 20 41 53 20 54 45 58 54 29 20 41  ST (b AS TEXT) A
1340: 53 20 78 2c 20 63 20 46 52 4f 4d 20 74 31 20 0a  S x, c FROM t1 .
1350: 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20        UNION ALL 
1360: 0a 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62  .    SELECT a, b
1370: 2c 20 63 20 46 52 4f 4d 20 74 32 20 0a 20 20 20  , c FROM t2 .   
1380: 20 20 20 4f 52 44 45 52 20 42 59 20 43 41 53 54     ORDER BY CAST
1390: 20 28 62 20 41 53 20 49 4e 54 45 47 45 52 29 3b   (b AS INTEGER);
13a0: 0a 20 20 7d 0a 7d 20 7b 31 20 7b 31 73 74 20 4f  .  }.} {1 {1st O
13b0: 52 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f 65  RDER BY term doe
13c0: 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20  s not match any 
13d0: 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
13e0: 73 75 6c 74 20 73 65 74 7d 7d 0a 0a 23 20 54 65  sult set}}..# Te
13f0: 73 74 73 20 66 6f 72 20 72 75 6c 65 20 28 32 29  sts for rule (2)
1400: 2e 0a 23 0a 23 20 54 68 65 20 22 4f 52 44 45 52  ..#.# The "ORDER
1410: 20 42 59 20 62 22 20 73 68 6f 75 6c 64 20 6d 61   BY b" should ma
1420: 74 63 68 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  tch the column a
1430: 6c 69 61 73 20 28 72 75 6c 65 20 32 29 2c 20 6e  lias (rule 2), n
1440: 6f 74 20 74 68 65 0a 23 20 74 68 65 20 74 33 2e  ot the.# the t3.
1450: 62 20 76 61 6c 75 65 20 28 72 75 6c 65 20 33 29  b value (rule 3)
1460: 2e 20 20 0a 23 0a 64 6f 5f 74 65 73 74 20 74 6b  .  .#.do_test tk
1470: 74 32 38 32 32 2d 35 2e 31 20 7b 0a 20 20 65 78  t2822-5.1 {.  ex
1480: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
1490: 54 45 20 54 41 42 4c 45 20 74 33 28 61 2c 62 29  TE TABLE t3(a,b)
14a0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
14b0: 4f 20 74 33 20 56 41 4c 55 45 53 28 31 2c 38 29  O t3 VALUES(1,8)
14c0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
14d0: 4f 20 74 33 20 56 41 4c 55 45 53 28 39 2c 32 29  O t3 VALUES(9,2)
14e0: 3b 0a 0a 20 20 20 20 53 45 4c 45 43 54 20 61 20  ;..    SELECT a 
14f0: 41 53 20 62 20 46 52 4f 4d 20 74 33 20 4f 52 44  AS b FROM t3 ORD
1500: 45 52 20 42 59 20 62 3b 0a 20 20 7d 0a 7d 20 7b  ER BY b;.  }.} {
1510: 31 20 39 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74  1 9}.do_test tkt
1520: 32 38 32 32 2d 35 2e 32 20 7b 0a 20 20 23 20 43  2822-5.2 {.  # C
1530: 61 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  ase does not mat
1540: 74 65 72 2e 20 20 62 20 73 68 6f 75 6c 64 20 6d  ter.  b should m
1550: 61 74 63 68 20 42 0a 20 20 65 78 65 63 73 71 6c  atch B.  execsql
1560: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 61 20   {.    SELECT a 
1570: 41 53 20 62 20 46 52 4f 4d 20 74 33 20 4f 52 44  AS b FROM t3 ORD
1580: 45 52 20 42 59 20 42 3b 0a 20 20 7d 0a 7d 20 7b  ER BY B;.  }.} {
1590: 31 20 39 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74  1 9}.do_test tkt
15a0: 32 38 32 32 2d 35 2e 33 20 7b 0a 20 20 23 20 51  2822-5.3 {.  # Q
15b0: 75 6f 74 69 6e 67 20 73 68 6f 75 6c 64 20 6e 6f  uoting should no
15c0: 74 20 6d 61 74 74 65 72 0a 20 20 65 78 65 63 73  t matter.  execs
15d0: 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20  ql {.    SELECT 
15e0: 61 20 41 53 20 27 62 27 20 46 52 4f 4d 20 74 33  a AS 'b' FROM t3
15f0: 20 4f 52 44 45 52 20 42 59 20 22 42 22 3b 0a 20   ORDER BY "B";. 
1600: 20 7d 0a 7d 20 7b 31 20 39 7d 0a 64 6f 5f 74 65   }.} {1 9}.do_te
1610: 73 74 20 74 6b 74 32 38 32 32 2d 35 2e 34 20 7b  st tkt2822-5.4 {
1620: 0a 20 20 23 20 51 75 6f 74 69 6e 67 20 73 68 6f  .  # Quoting sho
1630: 75 6c 64 20 6e 6f 74 20 6d 61 74 74 65 72 0a 20  uld not matter. 
1640: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53   execsql {.    S
1650: 45 4c 45 43 54 20 61 20 41 53 20 22 62 22 20 46  ELECT a AS "b" F
1660: 52 4f 4d 20 74 33 20 4f 52 44 45 52 20 42 59 20  ROM t3 ORDER BY 
1670: 5b 42 5d 3b 0a 20 20 7d 0a 7d 20 7b 31 20 39 7d  [B];.  }.} {1 9}
1680: 0a 0a 23 20 49 6e 20 22 4f 52 44 45 52 20 42 59  ..# In "ORDER BY
1690: 20 2b 62 22 20 74 68 65 20 74 65 72 6d 20 69 73   +b" the term is
16a0: 20 6e 6f 77 20 61 6e 20 65 78 70 72 65 73 73 69   now an expressi
16b0: 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a 23  on rather than.#
16c0: 20 61 20 6c 61 62 65 6c 2e 20 20 49 74 20 74 68   a label.  It th
16d0: 65 72 65 66 6f 72 65 20 6d 61 74 63 68 65 73 20  erefore matches 
16e0: 62 79 20 72 75 6c 65 20 28 33 29 20 69 6e 73 74  by rule (3) inst
16f0: 65 61 64 20 6f 66 20 72 75 6c 65 20 28 32 29 2e  ead of rule (2).
1700: 0a 23 20 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32  .# .do_test tkt2
1710: 38 32 32 2d 35 2e 35 20 7b 0a 20 20 65 78 65 63  822-5.5 {.  exec
1720: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
1730: 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 33 20   a AS b FROM t3 
1740: 4f 52 44 45 52 20 42 59 20 2b 62 3b 0a 20 20 7d  ORDER BY +b;.  }
1750: 0a 7d 20 7b 39 20 31 7d 0a 0a 23 20 54 65 73 74  .} {9 1}..# Test
1760: 73 20 66 6f 72 20 72 75 6c 65 20 32 20 69 6e 20  s for rule 2 in 
1770: 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69 65 73  compound queries
1780: 0a 23 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32 38  .#.do_test tkt28
1790: 32 32 2d 36 2e 31 20 7b 0a 20 20 65 78 65 63 73  22-6.1 {.  execs
17a0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
17b0: 54 41 42 4c 45 20 74 36 61 28 70 2c 71 29 3b 0a  TABLE t6a(p,q);.
17c0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
17d0: 74 36 61 20 56 41 4c 55 45 53 28 31 2c 38 29 3b  t6a VALUES(1,8);
17e0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
17f0: 20 74 36 61 20 56 41 4c 55 45 53 28 39 2c 32 29   t6a VALUES(9,2)
1800: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
1810: 4c 45 20 74 36 62 28 78 2c 79 29 3b 0a 20 20 20  LE t6b(x,y);.   
1820: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 36 62   INSERT INTO t6b
1830: 20 56 41 4c 55 45 53 28 31 2c 37 29 3b 0a 20 20   VALUES(1,7);.  
1840: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 36    INSERT INTO t6
1850: 62 20 56 41 4c 55 45 53 28 37 2c 32 29 3b 0a 0a  b VALUES(7,2);..
1860: 20 20 20 20 53 45 4c 45 43 54 20 70 2c 20 71 20      SELECT p, q 
1870: 46 52 4f 4d 20 74 36 61 20 55 4e 49 4f 4e 20 41  FROM t6a UNION A
1880: 4c 4c 20 53 45 4c 45 43 54 20 78 2c 20 79 20 46  LL SELECT x, y F
1890: 52 4f 4d 20 74 36 62 20 4f 52 44 45 52 20 42 59  ROM t6b ORDER BY
18a0: 20 31 2c 20 32 0a 20 20 7d 0a 7d 20 7b 31 20 37   1, 2.  }.} {1 7
18b0: 20 31 20 38 20 37 20 32 20 39 20 32 7d 0a 64 6f   1 8 7 2 9 2}.do
18c0: 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d 36 2e  _test tkt2822-6.
18d0: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
18e0: 20 20 20 20 53 45 4c 45 43 54 20 70 20 50 58 2c      SELECT p PX,
18f0: 20 71 20 51 58 20 46 52 4f 4d 20 74 36 61 20 55   q QX FROM t6a U
1900: 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20  NION ALL SELECT 
1910: 78 20 58 58 2c 20 79 20 59 58 20 46 52 4f 4d 20  x XX, y YX FROM 
1920: 74 36 62 0a 20 20 20 20 4f 52 44 45 52 20 42 59  t6b.    ORDER BY
1930: 20 50 58 2c 20 59 58 0a 20 20 7d 0a 7d 20 7b 31   PX, YX.  }.} {1
1940: 20 37 20 31 20 38 20 37 20 32 20 39 20 32 7d 0a   7 1 8 7 2 9 2}.
1950: 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d  do_test tkt2822-
1960: 36 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6.3 {.  execsql 
1970: 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 70 20 50  {.    SELECT p P
1980: 58 2c 20 71 20 51 58 20 46 52 4f 4d 20 74 36 61  X, q QX FROM t6a
1990: 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43   UNION ALL SELEC
19a0: 54 20 78 20 58 58 2c 20 79 20 59 58 20 46 52 4f  T x XX, y YX FRO
19b0: 4d 20 74 36 62 0a 20 20 20 20 4f 52 44 45 52 20  M t6b.    ORDER 
19c0: 42 59 20 58 58 2c 20 51 58 0a 20 20 7d 0a 7d 20  BY XX, QX.  }.} 
19d0: 7b 31 20 37 20 31 20 38 20 37 20 32 20 39 20 32  {1 7 1 8 7 2 9 2
19e0: 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32  }.do_test tkt282
19f0: 32 2d 36 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  2-6.4 {.  execsq
1a00: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 70  l {.    SELECT p
1a10: 20 50 58 2c 20 71 20 51 58 20 46 52 4f 4d 20 74   PX, q QX FROM t
1a20: 36 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c  6a UNION ALL SEL
1a30: 45 43 54 20 78 20 58 58 2c 20 79 20 59 58 20 46  ECT x XX, y YX F
1a40: 52 4f 4d 20 74 36 62 0a 20 20 20 20 4f 52 44 45  ROM t6b.    ORDE
1a50: 52 20 42 59 20 51 58 2c 20 58 58 0a 20 20 7d 0a  R BY QX, XX.  }.
1a60: 7d 20 7b 37 20 32 20 39 20 32 20 31 20 37 20 31  } {7 2 9 2 1 7 1
1a70: 20 38 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32   8}.do_test tkt2
1a80: 38 32 32 2d 36 2e 35 20 7b 0a 20 20 65 78 65 63  822-6.5 {.  exec
1a90: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
1aa0: 20 70 20 50 58 2c 20 71 20 51 58 20 46 52 4f 4d   p PX, q QX FROM
1ab0: 20 74 36 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 53   t6a UNION ALL S
1ac0: 45 4c 45 43 54 20 78 20 58 58 2c 20 79 20 59 58  ELECT x XX, y YX
1ad0: 20 46 52 4f 4d 20 74 36 62 0a 20 20 20 20 4f 52   FROM t6b.    OR
1ae0: 44 45 52 20 42 59 20 74 36 62 2e 78 2c 20 51 58  DER BY t6b.x, QX
1af0: 0a 20 20 7d 0a 7d 20 7b 31 20 37 20 31 20 38 20  .  }.} {1 7 1 8 
1b00: 37 20 32 20 39 20 32 7d 0a 64 6f 5f 74 65 73 74  7 2 9 2}.do_test
1b10: 20 74 6b 74 32 38 32 32 2d 36 2e 36 20 7b 0a 20   tkt2822-6.6 {. 
1b20: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53   execsql {.    S
1b30: 45 4c 45 43 54 20 70 20 50 58 2c 20 71 20 51 58  ELECT p PX, q QX
1b40: 20 46 52 4f 4d 20 74 36 61 20 55 4e 49 4f 4e 20   FROM t6a UNION 
1b50: 41 4c 4c 20 53 45 4c 45 43 54 20 78 20 58 58 2c  ALL SELECT x XX,
1b60: 20 79 20 59 58 20 46 52 4f 4d 20 74 36 62 0a 20   y YX FROM t6b. 
1b70: 20 20 20 4f 52 44 45 52 20 42 59 20 74 36 61 2e     ORDER BY t6a.
1b80: 71 2c 20 58 58 0a 20 20 7d 0a 7d 20 7b 37 20 32  q, XX.  }.} {7 2
1b90: 20 39 20 32 20 31 20 37 20 31 20 38 7d 0a 0a 23   9 2 1 7 1 8}..#
1ba0: 20 4d 6f 72 65 20 65 72 72 6f 72 20 6d 65 73 73   More error mess
1bb0: 61 67 65 20 74 65 73 74 73 2e 20 20 54 68 69 73  age tests.  This
1bc0: 20 69 73 20 72 65 61 6c 6c 79 20 6d 6f 72 65 20   is really more 
1bd0: 6f 66 20 61 20 74 65 73 74 20 6f 66 20 74 68 65  of a test of the
1be0: 0a 23 20 25 72 20 6f 72 64 69 6e 61 6c 20 76 61  .# %r ordinal va
1bf0: 6c 75 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 63  lue formatting c
1c00: 61 70 61 62 6c 69 74 79 20 61 64 64 65 64 20 74  apablity added t
1c10: 6f 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e  o sqlite3_snprin
1c20: 74 66 28 29 0a 23 20 62 79 20 74 69 63 6b 65 74  tf().# by ticket
1c30: 20 23 32 38 32 32 2e 0a 23 0a 64 6f 5f 74 65 73   #2822..#.do_tes
1c40: 74 20 74 6b 74 32 38 32 32 2d 37 2e 31 20 7b 0a  t tkt2822-7.1 {.
1c50: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
1c60: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 37 28  CREATE TABLE t7(
1c70: 61 31 2c 61 32 2c 61 33 2c 61 34 2c 61 35 2c 61  a1,a2,a3,a4,a5,a
1c80: 36 2c 61 37 2c 61 38 2c 61 39 2c 61 31 30 2c 61  6,a7,a8,a9,a10,a
1c90: 31 31 2c 61 31 32 2c 61 31 33 2c 61 31 34 2c 0a  11,a12,a13,a14,.
1ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cb0: 20 20 20 20 61 31 35 2c 61 31 36 2c 61 31 37 2c      a15,a16,a17,
1cc0: 61 31 38 2c 61 31 39 2c 61 32 30 2c 61 32 31 2c  a18,a19,a20,a21,
1cd0: 61 32 32 2c 61 32 33 2c 61 32 34 2c 61 32 35 29  a22,a23,a24,a25)
1ce0: 3b 0a 20 20 7d 0a 20 20 63 61 74 63 68 73 71 6c  ;.  }.  catchsql
1cf0: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20   {.    SELECT * 
1d00: 46 52 4f 4d 20 74 37 20 4f 52 44 45 52 20 42 59  FROM t7 ORDER BY
1d10: 20 30 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 31 73   0;.  }.} {1 {1s
1d20: 74 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20  t ORDER BY term 
1d30: 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73  out of range - s
1d40: 68 6f 75 6c 64 20 62 65 20 62 65 74 77 65 65 6e  hould be between
1d50: 20 31 20 61 6e 64 20 32 35 7d 7d 0a 64 6f 5f 74   1 and 25}}.do_t
1d60: 65 73 74 20 74 6b 74 32 38 32 32 2d 37 2e 32 2e  est tkt2822-7.2.
1d70: 31 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  1 {.  catchsql {
1d80: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
1d90: 4f 4d 20 74 37 20 4f 52 44 45 52 20 42 59 20 31  OM t7 ORDER BY 1
1da0: 2c 20 30 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 32  , 0;.  }.} {1 {2
1db0: 6e 64 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  nd ORDER BY term
1dc0: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20   out of range - 
1dd0: 73 68 6f 75 6c 64 20 62 65 20 62 65 74 77 65 65  should be betwee
1de0: 6e 20 31 20 61 6e 64 20 32 35 7d 7d 0a 64 6f 5f  n 1 and 25}}.do_
1df0: 74 65 73 74 20 74 6b 74 32 38 32 32 2d 37 2e 32  test tkt2822-7.2
1e00: 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .2 {.  catchsql 
1e10: 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  {.    SELECT * F
1e20: 52 4f 4d 20 74 37 20 4f 52 44 45 52 20 42 59 20  ROM t7 ORDER BY 
1e30: 31 2c 20 32 36 3b 0a 20 20 7d 0a 7d 20 7b 31 20  1, 26;.  }.} {1 
1e40: 7b 32 6e 64 20 4f 52 44 45 52 20 42 59 20 74 65  {2nd ORDER BY te
1e50: 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  rm out of range 
1e60: 2d 20 73 68 6f 75 6c 64 20 62 65 20 62 65 74 77  - should be betw
1e70: 65 65 6e 20 31 20 61 6e 64 20 32 35 7d 7d 0a 64  een 1 and 25}}.d
1e80: 6f 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d 37  o_test tkt2822-7
1e90: 2e 32 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71  .2.3 {.  catchsq
1ea0: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  l {.    SELECT *
1eb0: 20 46 52 4f 4d 20 74 37 20 4f 52 44 45 52 20 42   FROM t7 ORDER B
1ec0: 59 20 31 2c 20 36 35 35 33 36 3b 0a 20 20 7d 0a  Y 1, 65536;.  }.
1ed0: 7d 20 7b 31 20 7b 32 6e 64 20 4f 52 44 45 52 20  } {1 {2nd ORDER 
1ee0: 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72  BY term out of r
1ef0: 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65  ange - should be
1f00: 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 32   between 1 and 2
1f10: 35 7d 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74 32  5}}.do_test tkt2
1f20: 38 32 32 2d 37 2e 33 20 7b 0a 20 20 63 61 74 63  822-7.3 {.  catc
1f30: 68 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43  hsql {.    SELEC
1f40: 54 20 2a 20 46 52 4f 4d 20 74 37 20 4f 52 44 45  T * FROM t7 ORDE
1f50: 52 20 42 59 20 31 2c 20 32 2c 20 30 3b 0a 20 20  R BY 1, 2, 0;.  
1f60: 7d 0a 7d 20 7b 31 20 7b 33 72 64 20 4f 52 44 45  }.} {1 {3rd ORDE
1f70: 52 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66  R BY term out of
1f80: 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20   range - should 
1f90: 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
1fa0: 20 32 35 7d 7d 0a 64 6f 5f 74 65 73 74 20 74 6b   25}}.do_test tk
1fb0: 74 32 38 32 32 2d 37 2e 34 20 7b 0a 20 20 63 61  t2822-7.4 {.  ca
1fc0: 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c  tchsql {.    SEL
1fd0: 45 43 54 20 2a 20 46 52 4f 4d 20 74 37 20 4f 52  ECT * FROM t7 OR
1fe0: 44 45 52 20 42 59 20 31 2c 20 32 2c 20 33 2c 20  DER BY 1, 2, 3, 
1ff0: 30 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 34 74 68  0;.  }.} {1 {4th
2000: 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 6f   ORDER BY term o
2010: 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68  ut of range - sh
2020: 6f 75 6c 64 20 62 65 20 62 65 74 77 65 65 6e 20  ould be between 
2030: 31 20 61 6e 64 20 32 35 7d 7d 0a 64 6f 5f 74 65  1 and 25}}.do_te
2040: 73 74 20 74 6b 74 32 38 32 32 2d 37 2e 39 20 7b  st tkt2822-7.9 {
2050: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20  .  catchsql {.  
2060: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2070: 74 37 20 4f 52 44 45 52 20 42 59 20 31 2c 20 32  t7 ORDER BY 1, 2
2080: 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 37 2c  , 3, 4, 5, 6, 7,
2090: 20 38 2c 20 30 3b 0a 20 20 7d 0a 7d 20 7b 31 20   8, 0;.  }.} {1 
20a0: 7b 39 74 68 20 4f 52 44 45 52 20 42 59 20 74 65  {9th ORDER BY te
20b0: 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  rm out of range 
20c0: 2d 20 73 68 6f 75 6c 64 20 62 65 20 62 65 74 77  - should be betw
20d0: 65 65 6e 20 31 20 61 6e 64 20 32 35 7d 7d 0a 64  een 1 and 25}}.d
20e0: 6f 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d 37  o_test tkt2822-7
20f0: 2e 31 30 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  .10 {.  catchsql
2100: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20   {.    SELECT * 
2110: 46 52 4f 4d 20 74 37 20 4f 52 44 45 52 20 42 59  FROM t7 ORDER BY
2120: 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20   1, 2, 3, 4, 5, 
2130: 36 2c 20 37 2c 20 38 2c 20 39 2c 20 30 3b 0a 20  6, 7, 8, 9, 0;. 
2140: 20 7d 0a 7d 20 7b 31 20 7b 31 30 74 68 20 4f 52   }.} {1 {10th OR
2150: 44 45 52 20 42 59 20 74 65 72 6d 20 6f 75 74 20  DER BY term out 
2160: 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c  of range - shoul
2170: 64 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  d be between 1 a
2180: 6e 64 20 32 35 7d 7d 0a 64 6f 5f 74 65 73 74 20  nd 25}}.do_test 
2190: 74 6b 74 32 38 32 32 2d 37 2e 31 31 20 7b 0a 20  tkt2822-7.11 {. 
21a0: 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20   catchsql {.    
21b0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 37  SELECT * FROM t7
21c0: 20 4f 52 44 45 52 20 42 59 20 31 2c 20 32 2c 20   ORDER BY 1, 2, 
21d0: 33 2c 20 34 2c 20 35 2c 20 36 2c 20 37 2c 20 38  3, 4, 5, 6, 7, 8
21e0: 2c 20 39 2c 20 31 30 2c 20 30 3b 0a 20 20 7d 0a  , 9, 10, 0;.  }.
21f0: 7d 20 7b 31 20 7b 31 31 74 68 20 4f 52 44 45 52  } {1 {11th ORDER
2200: 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20   BY term out of 
2210: 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62  range - should b
2220: 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
2230: 32 35 7d 7d 0a 64 6f 5f 74 65 73 74 20 74 6b 74  25}}.do_test tkt
2240: 32 38 32 32 2d 37 2e 31 32 20 7b 0a 20 20 63 61  2822-7.12 {.  ca
2250: 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c  tchsql {.    SEL
2260: 45 43 54 20 2a 20 46 52 4f 4d 20 74 37 20 4f 52  ECT * FROM t7 OR
2270: 44 45 52 20 42 59 20 31 2c 20 32 2c 20 33 2c 20  DER BY 1, 2, 3, 
2280: 34 2c 20 35 2c 20 36 2c 20 37 2c 20 38 2c 20 39  4, 5, 6, 7, 8, 9
2290: 2c 20 31 30 2c 20 31 32 2c 20 30 3b 0a 20 20 7d  , 10, 12, 0;.  }
22a0: 0a 7d 20 7b 31 20 7b 31 32 74 68 20 4f 52 44 45  .} {1 {12th ORDE
22b0: 52 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66  R BY term out of
22c0: 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20   range - should 
22d0: 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
22e0: 20 32 35 7d 7d 0a 64 6f 5f 74 65 73 74 20 74 6b   25}}.do_test tk
22f0: 74 32 38 32 32 2d 37 2e 31 33 20 7b 0a 20 20 63  t2822-7.13 {.  c
2300: 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 53 45  atchsql {.    SE
2310: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 37 20 4f  LECT * FROM t7 O
2320: 52 44 45 52 20 42 59 20 31 2c 20 32 2c 20 33 2c  RDER BY 1, 2, 3,
2330: 20 34 2c 20 35 2c 20 36 2c 20 37 2c 20 38 2c 20   4, 5, 6, 7, 8, 
2340: 39 2c 20 31 30 2c 20 31 32 2c 20 31 33 2c 20 30  9, 10, 12, 13, 0
2350: 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 31 33 74 68  ;.  }.} {1 {13th
2360: 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 6f   ORDER BY term o
2370: 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68  ut of range - sh
2380: 6f 75 6c 64 20 62 65 20 62 65 74 77 65 65 6e 20  ould be between 
2390: 31 20 61 6e 64 20 32 35 7d 7d 0a 64 6f 5f 74 65  1 and 25}}.do_te
23a0: 73 74 20 74 6b 74 32 38 32 32 2d 37 2e 32 30 20  st tkt2822-7.20 
23b0: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20  {.  catchsql {. 
23c0: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
23d0: 20 74 37 20 4f 52 44 45 52 20 42 59 20 31 2c 20   t7 ORDER BY 1, 
23e0: 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 37  2, 3, 4, 5, 6, 7
23f0: 2c 20 38 2c 20 39 2c 20 31 30 2c 0a 20 20 20 20  , 8, 9, 10,.    
2400: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2410: 20 20 20 20 20 20 20 20 20 31 31 2c 31 32 2c 31           11,12,1
2420: 33 2c 31 34 2c 31 35 2c 31 36 2c 31 37 2c 31 38  3,14,15,16,17,18
2430: 2c 31 39 2c 20 30 0a 20 20 7d 0a 7d 20 7b 31 20  ,19, 0.  }.} {1 
2440: 7b 32 30 74 68 20 4f 52 44 45 52 20 42 59 20 74  {20th ORDER BY t
2450: 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 65  erm out of range
2460: 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 62 65 74   - should be bet
2470: 77 65 65 6e 20 31 20 61 6e 64 20 32 35 7d 7d 0a  ween 1 and 25}}.
2480: 64 6f 5f 74 65 73 74 20 74 6b 74 32 38 32 32 2d  do_test tkt2822-
2490: 37 2e 32 31 20 7b 0a 20 20 63 61 74 63 68 73 71  7.21 {.  catchsq
24a0: 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  l {.    SELECT *
24b0: 20 46 52 4f 4d 20 74 37 20 4f 52 44 45 52 20 42   FROM t7 ORDER B
24c0: 59 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c  Y 1, 2, 3, 4, 5,
24d0: 20 36 2c 20 37 2c 20 38 2c 20 39 2c 20 31 30 2c   6, 7, 8, 9, 10,
24e0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
2500: 2c 31 32 2c 31 33 2c 31 34 2c 31 35 2c 31 36 2c  ,12,13,14,15,16,
2510: 31 37 2c 31 38 2c 31 39 2c 20 32 30 2c 20 30 0a  17,18,19, 20, 0.
2520: 20 20 7d 0a 7d 20 7b 31 20 7b 32 31 73 74 20 4f    }.} {1 {21st O
2530: 52 44 45 52 20 42 59 20 74 65 72 6d 20 6f 75 74  RDER BY term out
2540: 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f 75   of range - shou
2550: 6c 64 20 62 65 20 62 65 74 77 65 65 6e 20 31 20  ld be between 1 
2560: 61 6e 64 20 32 35 7d 7d 0a 64 6f 5f 74 65 73 74  and 25}}.do_test
2570: 20 74 6b 74 32 38 32 32 2d 37 2e 32 32 20 7b 0a   tkt2822-7.22 {.
2580: 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20    catchsql {.   
2590: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
25a0: 37 20 4f 52 44 45 52 20 42 59 20 31 2c 20 32 2c  7 ORDER BY 1, 2,
25b0: 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 37 2c 20   3, 4, 5, 6, 7, 
25c0: 38 2c 20 39 2c 20 31 30 2c 0a 20 20 20 20 20 20  8, 9, 10,.      
25d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e0: 20 20 20 20 20 20 20 31 31 2c 31 32 2c 31 33 2c         11,12,13,
25f0: 31 34 2c 31 35 2c 31 36 2c 31 37 2c 31 38 2c 31  14,15,16,17,18,1
2600: 39 2c 20 32 30 2c 20 32 31 2c 20 30 0a 20 20 7d  9, 20, 21, 0.  }
2610: 0a 7d 20 7b 31 20 7b 32 32 6e 64 20 4f 52 44 45  .} {1 {22nd ORDE
2620: 52 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66  R BY term out of
2630: 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20   range - should 
2640: 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
2650: 20 32 35 7d 7d 0a 0a 0a 66 69 6e 69 73 68 5f 74   25}}...finish_t
2660: 65 73 74 0a                                      est.