/ Hex Artifact Content
Login

Artifact e4098a4b8962f9596035c3b87a8928a10648acc509f1bb8d6f96413bbf79a1b3:


0000: 23 20 32 30 31 37 20 41 70 72 69 6c 20 33 30 0a  # 2017 April 30.
0010: 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20 64  #.# The author d
0020: 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
0030: 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
0040: 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
0050: 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e  e of.# a legal n
0060: 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
0070: 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20   blessing:.#.#  
0080: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
0090: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
00a0: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  #    May you fin
00b0: 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
00c0: 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
00d0: 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23  orgive others..#
00e0: 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
00f0: 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
0100: 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
0110: 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a   you 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 2a 2a 2a 2a 0a 23 0a 23 20 54 65 73 74 20 74  *****.#.# Test t
0170: 68 65 20 48 41 56 49 4e 47 2d 3e 57 48 45 52 45  he HAVING->WHERE
0180: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 23   optimization..#
0190: 0a 0a 73 65 74 20 74 65 73 74 64 69 72 20 5b 66  ..set testdir [f
01a0: 69 6c 65 20 64 69 72 6e 61 6d 65 20 24 61 72 67  ile dirname $arg
01b0: 76 30 5d 0a 73 6f 75 72 63 65 20 24 74 65 73 74  v0].source $test
01c0: 64 69 72 2f 74 65 73 74 65 72 2e 74 63 6c 0a 73  dir/tester.tcl.s
01d0: 65 74 20 74 65 73 74 70 72 65 66 69 78 20 68 61  et testprefix ha
01e0: 76 69 6e 67 0a 0a 64 6f 5f 65 78 65 63 73 71 6c  ving..do_execsql
01f0: 5f 74 65 73 74 20 31 2e 30 20 7b 0a 20 20 43 52  _test 1.0 {.  CR
0200: 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 63 2c  EATE TABLE t2(c,
0210: 20 64 29 3b 0a 0a 20 20 43 52 45 41 54 45 20 54   d);..  CREATE T
0220: 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20  ABLE t1(a, b);. 
0230: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
0240: 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20 20  VALUES(1, 1);.  
0250: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
0260: 41 4c 55 45 53 28 32 2c 20 32 29 3b 0a 20 20 49  ALUES(2, 2);.  I
0270: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
0280: 4c 55 45 53 28 31 2c 20 33 29 3b 0a 20 20 49 4e  LUES(1, 3);.  IN
0290: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
02a0: 55 45 53 28 32 2c 20 34 29 3b 0a 20 20 49 4e 53  UES(2, 4);.  INS
02b0: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
02c0: 45 53 28 31 2c 20 35 29 3b 0a 20 20 49 4e 53 45  ES(1, 5);.  INSE
02d0: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
02e0: 53 28 32 2c 20 36 29 3b 0a 7d 20 7b 7d 0a 0a 66  S(2, 6);.} {}..f
02f0: 6f 72 65 61 63 68 20 7b 74 6e 20 73 71 6c 20 72  oreach {tn sql r
0300: 65 73 7d 20 7b 0a 20 20 31 20 22 53 45 4c 45 43  es} {.  1 "SELEC
0310: 54 20 61 2c 20 73 75 6d 28 62 29 20 46 52 4f 4d  T a, sum(b) FROM
0320: 20 74 31 20 47 52 4f 55 50 20 42 59 20 61 20 48   t1 GROUP BY a H
0330: 41 56 49 4e 47 20 61 3d 32 22 20 7b 32 20 31 32  AVING a=2" {2 12
0340: 7d 0a 20 20 32 20 22 53 45 4c 45 43 54 20 61 2c  }.  2 "SELECT a,
0350: 20 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20   sum(b) FROM t1 
0360: 47 52 4f 55 50 20 42 59 20 61 20 48 41 56 49 4e  GROUP BY a HAVIN
0370: 47 20 61 3d 32 20 41 4e 44 20 73 75 6d 28 62 29  G a=2 AND sum(b)
0380: 3e 31 30 22 20 7b 32 20 31 32 7d 0a 20 20 33 20  >10" {2 12}.  3 
0390: 22 53 45 4c 45 43 54 20 61 2c 20 73 75 6d 28 62  "SELECT a, sum(b
03a0: 29 20 46 52 4f 4d 20 74 31 20 47 52 4f 55 50 20  ) FROM t1 GROUP 
03b0: 42 59 20 61 20 48 41 56 49 4e 47 20 73 75 6d 28  BY a HAVING sum(
03c0: 62 29 3e 31 32 22 20 7b 7d 0a 7d 20 7b 0a 20 20  b)>12" {}.} {.  
03d0: 64 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20  do_execsql_test 
03e0: 31 2e 24 74 6e 20 24 73 71 6c 20 24 72 65 73 0a  1.$tn $sql $res.
03f0: 7d 0a 0a 23 20 52 75 6e 20 61 6e 20 45 58 50 4c  }..# Run an EXPL
0400: 41 49 4e 20 63 6f 6d 6d 61 6e 64 20 66 6f 72 20  AIN command for 
0410: 62 6f 74 68 20 53 51 4c 20 73 74 61 74 65 6d 65  both SQL stateme
0420: 6e 74 73 2e 20 52 65 74 75 72 6e 20 74 72 75 65  nts. Return true
0430: 20 69 66 20 0a 23 20 74 68 65 20 6f 75 74 70 75   if .# the outpu
0440: 74 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  ts are identical
0450: 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
0460: 77 69 73 65 2e 0a 23 0a 70 72 6f 63 20 63 6f 6d  wise..#.proc com
0470: 70 61 72 65 5f 76 64 62 65 20 7b 73 71 6c 31 20  pare_vdbe {sql1 
0480: 73 71 6c 32 7d 20 7b 0a 20 20 73 65 74 20 72 31  sql2} {.  set r1
0490: 20 5b 6c 69 73 74 5d 0a 20 20 73 65 74 20 72 32   [list].  set r2
04a0: 20 5b 6c 69 73 74 5d 0a 20 20 64 62 20 65 76 61   [list].  db eva
04b0: 6c 20 22 65 78 70 6c 61 69 6e 20 24 73 71 6c 31  l "explain $sql1
04c0: 22 20 7b 20 6c 61 70 70 65 6e 64 20 72 31 20 24  " { lappend r1 $
04d0: 6f 70 63 6f 64 65 20 24 70 31 20 24 70 32 20 24  opcode $p1 $p2 $
04e0: 70 33 20 24 70 34 20 24 70 35 7d 0a 20 20 64 62  p3 $p4 $p5}.  db
04f0: 20 65 76 61 6c 20 22 65 78 70 6c 61 69 6e 20 24   eval "explain $
0500: 73 71 6c 32 22 20 7b 20 6c 61 70 70 65 6e 64 20  sql2" { lappend 
0510: 72 32 20 24 6f 70 63 6f 64 65 20 24 70 31 20 24  r2 $opcode $p1 $
0520: 70 32 20 24 70 33 20 24 70 34 20 24 70 35 7d 0a  p2 $p3 $p4 $p5}.
0530: 20 20 72 65 74 75 72 6e 20 5b 65 78 70 72 20 7b    return [expr {
0540: 24 72 31 3d 3d 24 72 32 7d 5d 0a 7d 0a 0a 70 72  $r1==$r2}].}..pr
0550: 6f 63 20 64 6f 5f 63 6f 6d 70 61 72 65 5f 76 64  oc do_compare_vd
0560: 62 65 5f 74 65 73 74 20 7b 74 6e 20 73 71 6c 31  be_test {tn sql1
0570: 20 73 71 6c 32 20 72 65 73 7d 20 7b 0a 20 20 75   sql2 res} {.  u
0580: 70 6c 65 76 65 6c 20 5b 6c 69 73 74 20 64 6f 5f  plevel [list do_
0590: 74 65 73 74 20 24 74 6e 20 5b 6c 69 73 74 20 63  test $tn [list c
05a0: 6f 6d 70 61 72 65 5f 76 64 62 65 20 24 73 71 6c  ompare_vdbe $sql
05b0: 31 20 24 73 71 6c 32 5d 20 24 72 65 73 5d 0a 7d  1 $sql2] $res].}
05c0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
05d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0600: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
0610: 65 73 74 20 74 68 61 74 20 76 61 72 69 6f 75 73  est that various
0620: 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74   statements that
0630: 20 61 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f   are eligible fo
0640: 72 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  r the optimizati
0650: 6f 6e 0a 23 20 70 72 6f 64 75 63 65 20 74 68 65  on.# produce the
0660: 20 73 61 6d 65 20 56 44 42 45 20 63 6f 64 65 20   same VDBE code 
0670: 61 73 20 6f 70 74 69 6d 69 7a 69 6e 67 20 62 79  as optimizing by
0680: 20 68 61 6e 64 20 64 6f 65 73 2e 0a 23 0a 66 6f   hand does..#.fo
0690: 72 65 61 63 68 20 7b 74 6e 20 73 71 6c 31 20 73  reach {tn sql1 s
06a0: 71 6c 32 7d 20 7b 0a 20 20 31 20 22 53 45 4c 45  ql2} {.  1 "SELE
06b0: 43 54 20 61 2c 20 73 75 6d 28 62 29 20 46 52 4f  CT a, sum(b) FRO
06c0: 4d 20 74 31 20 47 52 4f 55 50 20 42 59 20 61 20  M t1 GROUP BY a 
06d0: 48 41 56 49 4e 47 20 61 3d 32 22 0a 20 20 20 20  HAVING a=2".    
06e0: 22 53 45 4c 45 43 54 20 61 2c 20 73 75 6d 28 62  "SELECT a, sum(b
06f0: 29 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  ) FROM t1 WHERE 
0700: 61 3d 32 20 47 52 4f 55 50 20 42 59 20 61 22 0a  a=2 GROUP BY a".
0710: 0a 20 20 32 20 22 53 45 4c 45 43 54 20 61 2c 20  .  2 "SELECT a, 
0720: 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20 47  sum(b) FROM t1 G
0730: 52 4f 55 50 20 42 59 20 61 20 48 41 56 49 4e 47  ROUP BY a HAVING
0740: 20 73 75 6d 28 62 29 3e 35 20 41 4e 44 20 61 3d   sum(b)>5 AND a=
0750: 32 22 0a 20 20 20 20 22 53 45 4c 45 43 54 20 61  2".    "SELECT a
0760: 2c 20 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 31  , sum(b) FROM t1
0770: 20 57 48 45 52 45 20 61 3d 32 20 47 52 4f 55 50   WHERE a=2 GROUP
0780: 20 42 59 20 61 20 48 41 56 49 4e 47 20 73 75 6d   BY a HAVING sum
0790: 28 62 29 3e 35 22 0a 0a 20 20 33 20 22 53 45 4c  (b)>5"..  3 "SEL
07a0: 45 43 54 20 61 2c 20 73 75 6d 28 62 29 20 46 52  ECT a, sum(b) FR
07b0: 4f 4d 20 74 31 20 47 52 4f 55 50 20 42 59 20 61  OM t1 GROUP BY a
07c0: 20 43 4f 4c 4c 41 54 45 20 62 69 6e 61 72 79 20   COLLATE binary 
07d0: 48 41 56 49 4e 47 20 61 3d 32 22 0a 20 20 20 20  HAVING a=2".    
07e0: 22 53 45 4c 45 43 54 20 61 2c 20 73 75 6d 28 62  "SELECT a, sum(b
07f0: 29 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  ) FROM t1 WHERE 
0800: 61 3d 32 20 47 52 4f 55 50 20 42 59 20 61 20 43  a=2 GROUP BY a C
0810: 4f 4c 4c 41 54 45 20 62 69 6e 61 72 79 22 0a 0a  OLLATE binary"..
0820: 20 20 35 20 22 53 45 4c 45 43 54 20 61 2c 20 73    5 "SELECT a, s
0830: 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20 47 52  um(b) FROM t1 GR
0840: 4f 55 50 20 42 59 20 61 20 43 4f 4c 4c 41 54 45  OUP BY a COLLATE
0850: 20 62 69 6e 61 72 79 20 48 41 56 49 4e 47 20 30   binary HAVING 0
0860: 22 0a 20 20 20 20 22 53 45 4c 45 43 54 20 61 2c  ".    "SELECT a,
0870: 20 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20   sum(b) FROM t1 
0880: 57 48 45 52 45 20 30 20 47 52 4f 55 50 20 42 59  WHERE 0 GROUP BY
0890: 20 61 20 43 4f 4c 4c 41 54 45 20 62 69 6e 61 72   a COLLATE binar
08a0: 79 22 0a 0a 20 20 36 20 22 53 45 4c 45 43 54 20  y"..  6 "SELECT 
08b0: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31  count(*) FROM t1
08c0: 2c 74 32 20 57 48 45 52 45 20 61 3d 63 20 47 52  ,t2 WHERE a=c GR
08d0: 4f 55 50 20 42 59 20 62 2c 20 64 20 48 41 56 49  OUP BY b, d HAVI
08e0: 4e 47 20 62 3d 64 22 0a 20 20 20 20 22 53 45 4c  NG b=d".    "SEL
08f0: 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f  ECT count(*) FRO
0900: 4d 20 74 31 2c 74 32 20 57 48 45 52 45 20 61 3d  M t1,t2 WHERE a=
0910: 63 20 41 4e 44 20 62 3d 64 20 47 52 4f 55 50 20  c AND b=d GROUP 
0920: 42 59 20 62 2c 20 64 22 0a 0a 20 20 37 20 7b 0a  BY b, d"..  7 {.
0930: 20 20 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75        SELECT cou
0940: 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 2c 74 32  nt(*) FROM t1,t2
0950: 20 57 48 45 52 45 20 61 3d 63 20 47 52 4f 55 50   WHERE a=c GROUP
0960: 20 42 59 20 62 2c 20 64 20 0a 20 20 20 20 20 20   BY b, d .      
0970: 48 41 56 49 4e 47 20 62 3d 64 20 43 4f 4c 4c 41  HAVING b=d COLLA
0980: 54 45 20 6e 6f 63 61 73 65 0a 20 20 20 20 7d 20  TE nocase.    } 
0990: 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 63  {.      SELECT c
09a0: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 2c  ount(*) FROM t1,
09b0: 74 32 20 57 48 45 52 45 20 61 3d 63 20 41 4e 44  t2 WHERE a=c AND
09c0: 20 62 3d 64 20 43 4f 4c 4c 41 54 45 20 6e 6f 63   b=d COLLATE noc
09d0: 61 73 65 20 0a 20 20 20 20 20 20 47 52 4f 55 50  ase .      GROUP
09e0: 20 42 59 20 62 2c 20 64 0a 20 20 20 20 7d 0a 0a   BY b, d.    }..
09f0: 20 20 38 20 22 53 45 4c 45 43 54 20 61 2c 20 73    8 "SELECT a, s
0a00: 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20 47 52  um(b) FROM t1 GR
0a10: 4f 55 50 20 42 59 20 61 7c 7c 62 20 48 41 56 49  OUP BY a||b HAVI
0a20: 4e 47 20 73 75 62 73 74 72 28 61 7c 7c 62 2c 20  NG substr(a||b, 
0a30: 31 2c 20 31 29 3d 27 61 27 22 0a 20 20 20 20 22  1, 1)='a'".    "
0a40: 53 45 4c 45 43 54 20 61 2c 20 73 75 6d 28 62 29  SELECT a, sum(b)
0a50: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 73   FROM t1 WHERE s
0a60: 75 62 73 74 72 28 61 7c 7c 62 2c 20 31 2c 20 31  ubstr(a||b, 1, 1
0a70: 29 3d 27 61 27 20 47 52 4f 55 50 20 42 59 20 61  )='a' GROUP BY a
0a80: 7c 7c 62 22 0a 7d 20 7b 0a 20 20 64 6f 5f 63 6f  ||b".} {.  do_co
0a90: 6d 70 61 72 65 5f 76 64 62 65 5f 74 65 73 74 20  mpare_vdbe_test 
0aa0: 32 2e 24 74 6e 20 24 73 71 6c 31 20 24 73 71 6c  2.$tn $sql1 $sql
0ab0: 32 20 31 0a 7d 0a 0a 23 20 54 68 65 20 28 34 29  2 1.}..# The (4)
0ac0: 20 74 65 73 74 20 69 6e 20 74 68 65 20 61 62 6f   test in the abo
0ad0: 76 65 20 73 65 74 20 75 73 65 64 20 74 6f 20 67  ve set used to g
0ae0: 65 6e 65 72 61 74 65 20 69 64 65 6e 74 69 63 61  enerate identica
0af0: 6c 20 62 79 74 65 63 6f 64 65 2c 20 62 75 74 0a  l bytecode, but.
0b00: 23 20 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e  # that is no lon
0b10: 67 65 72 20 74 68 65 20 63 61 73 65 2e 20 20 54  ger the case.  T
0b20: 68 65 20 62 79 74 65 20 63 6f 64 65 20 69 73 20  he byte code is 
0b30: 65 71 75 69 76 61 6c 65 6e 74 2c 20 74 68 6f 75  equivalent, thou
0b40: 67 68 2e 0a 23 0a 64 6f 5f 65 78 65 63 73 71 6c  gh..#.do_execsql
0b50: 5f 74 65 73 74 20 32 2e 34 61 20 7b 0a 20 20 53  _test 2.4a {.  S
0b60: 45 4c 45 43 54 20 78 2c 79 20 46 52 4f 4d 20 28  ELECT x,y FROM (
0b70: 0a 20 20 20 20 53 45 4c 45 43 54 20 61 20 41 53  .    SELECT a AS
0b80: 20 78 2c 20 73 75 6d 28 62 29 20 41 53 20 79 20   x, sum(b) AS y 
0b90: 46 52 4f 4d 20 74 31 20 0a 20 20 20 20 47 52 4f  FROM t1 .    GRO
0ba0: 55 50 20 42 59 20 61 0a 20 20 29 20 57 48 45 52  UP BY a.  ) WHER
0bb0: 45 20 78 20 42 45 54 57 45 45 4e 20 32 20 41 4e  E x BETWEEN 2 AN
0bc0: 44 20 39 39 39 39 0a 7d 20 7b 32 20 31 32 7d 0a  D 9999.} {2 12}.
0bd0: 64 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20  do_execsql_test 
0be0: 32 2e 34 62 20 7b 0a 20 20 53 45 4c 45 43 54 20  2.4b {.  SELECT 
0bf0: 78 2c 79 20 46 52 4f 4d 20 28 0a 20 20 20 20 53  x,y FROM (.    S
0c00: 45 4c 45 43 54 20 61 20 41 53 20 78 2c 20 73 75  ELECT a AS x, su
0c10: 6d 28 62 29 20 41 53 20 79 20 46 52 4f 4d 20 74  m(b) AS y FROM t
0c20: 31 20 0a 20 20 20 20 57 48 45 52 45 20 78 20 42  1 .    WHERE x B
0c30: 45 54 57 45 45 4e 20 32 20 41 4e 44 20 39 39 39  ETWEEN 2 AND 999
0c40: 39 20 0a 20 20 20 20 47 52 4f 55 50 20 42 59 20  9 .    GROUP BY 
0c50: 61 0a 20 20 29 0a 7d 20 7b 32 20 31 32 7d 0a 0a  a.  ).} {2 12}..
0c60: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
0c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 31 3a  -----------.# 1:
0cb0: 20 54 65 73 74 20 74 68 61 74 20 74 68 65 20 6f   Test that the o
0cc0: 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6f  ptimization is o
0cd0: 6e 6c 79 20 61 70 70 6c 69 65 64 20 69 66 20 74  nly applied if t
0ce0: 68 65 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d  he GROUP BY term
0cf0: 0a 23 20 20 20 20 75 73 65 73 20 42 49 4e 41 52  .#    uses BINAR
0d00: 59 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 23 0a 23  Y collation..#.#
0d10: 20 32 3a 20 4e 6f 74 20 61 70 70 6c 69 65 64 20   2: Not applied 
0d20: 69 66 20 74 68 65 72 65 20 69 73 20 61 20 6e 6f  if there is a no
0d30: 6e 2d 64 65 74 65 72 6d 69 6e 69 73 74 69 63 20  n-deterministic 
0d40: 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  function in the 
0d50: 48 41 56 49 4e 47 0a 23 20 20 20 20 74 65 72 6d  HAVING.#    term
0d60: 2e 0a 23 0a 66 6f 72 65 61 63 68 20 7b 74 6e 20  ..#.foreach {tn 
0d70: 73 71 6c 31 20 73 71 6c 32 7d 20 7b 0a 20 20 31  sql1 sql2} {.  1
0d80: 20 22 53 45 4c 45 43 54 20 61 2c 20 73 75 6d 28   "SELECT a, sum(
0d90: 62 29 20 46 52 4f 4d 20 74 31 20 47 52 4f 55 50  b) FROM t1 GROUP
0da0: 20 42 59 20 61 20 43 4f 4c 4c 41 54 45 20 6e 6f   BY a COLLATE no
0db0: 63 61 73 65 20 48 41 56 49 4e 47 20 61 3d 32 22  case HAVING a=2"
0dc0: 0a 20 20 20 20 22 53 45 4c 45 43 54 20 61 2c 20  .    "SELECT a, 
0dd0: 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 31 20 57  sum(b) FROM t1 W
0de0: 48 45 52 45 20 61 3d 32 20 47 52 4f 55 50 20 42  HERE a=2 GROUP B
0df0: 59 20 61 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61  Y a COLLATE noca
0e00: 73 65 22 0a 0a 20 20 32 20 22 53 45 4c 45 43 54  se"..  2 "SELECT
0e10: 20 61 2c 20 73 75 6d 28 62 29 20 46 52 4f 4d 20   a, sum(b) FROM 
0e20: 74 31 20 47 52 4f 55 50 20 42 59 20 61 20 48 41  t1 GROUP BY a HA
0e30: 56 49 4e 47 20 72 61 6e 64 6f 6d 62 6c 6f 62 28  VING randomblob(
0e40: 61 29 3c 58 27 38 38 27 22 0a 20 20 20 20 22 53  a)<X'88'".    "S
0e50: 45 4c 45 43 54 20 61 2c 20 73 75 6d 28 62 29 20  ELECT a, sum(b) 
0e60: 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 72 61  FROM t1 WHERE ra
0e70: 6e 64 6f 6d 62 6c 6f 62 28 61 29 3c 58 27 38 38  ndomblob(a)<X'88
0e80: 27 20 47 52 4f 55 50 20 42 59 20 61 22 0a 7d 20  ' GROUP BY a".} 
0e90: 7b 0a 20 20 64 6f 5f 63 6f 6d 70 61 72 65 5f 76  {.  do_compare_v
0ea0: 64 62 65 5f 74 65 73 74 20 33 2e 24 74 6e 20 24  dbe_test 3.$tn $
0eb0: 73 71 6c 31 20 24 73 71 6c 32 20 30 0a 7d 0a 0a  sql1 $sql2 0.}..
0ec0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
0ed0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ee0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ef0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65  -----------.# Te
0f10: 73 74 20 74 68 61 74 20 6e 6f 6e 2d 64 65 74 65  st that non-dete
0f20: 72 6d 69 6e 69 73 74 69 63 20 66 75 6e 63 74 69  rministic functi
0f30: 6f 6e 73 20 64 69 73 71 75 61 6c 69 66 79 20 61  ons disqualify a
0f40: 20 74 65 72 6d 20 66 72 6f 6d 20 62 65 69 6e 67   term from being
0f50: 0a 23 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68  .# moved from th
0f60: 65 20 48 41 56 49 4e 47 20 74 6f 20 57 48 45 52  e HAVING to WHER
0f70: 45 20 63 6c 61 75 73 65 2e 0a 23 0a 64 6f 5f 65  E clause..#.do_e
0f80: 78 65 63 73 71 6c 5f 74 65 73 74 20 34 2e 31 20  xecsql_test 4.1 
0f90: 7b 0a 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  {.  CREATE TABLE
0fa0: 20 74 33 28 61 2c 20 62 29 3b 0a 20 20 49 4e 53   t3(a, b);.  INS
0fb0: 45 52 54 20 49 4e 54 4f 20 74 33 20 56 41 4c 55  ERT INTO t3 VALU
0fc0: 45 53 28 31 2c 20 31 29 3b 0a 20 20 49 4e 53 45  ES(1, 1);.  INSE
0fd0: 52 54 20 49 4e 54 4f 20 74 33 20 56 41 4c 55 45  RT INTO t3 VALUE
0fe0: 53 28 31 2c 20 32 29 3b 0a 20 20 49 4e 53 45 52  S(1, 2);.  INSER
0ff0: 54 20 49 4e 54 4f 20 74 33 20 56 41 4c 55 45 53  T INTO t3 VALUES
1000: 28 31 2c 20 33 29 3b 0a 20 20 49 4e 53 45 52 54  (1, 3);.  INSERT
1010: 20 49 4e 54 4f 20 74 33 20 56 41 4c 55 45 53 28   INTO t3 VALUES(
1020: 32 2c 20 31 29 3b 0a 20 20 49 4e 53 45 52 54 20  2, 1);.  INSERT 
1030: 49 4e 54 4f 20 74 33 20 56 41 4c 55 45 53 28 32  INTO t3 VALUES(2
1040: 2c 20 32 29 3b 0a 20 20 49 4e 53 45 52 54 20 49  , 2);.  INSERT I
1050: 4e 54 4f 20 74 33 20 56 41 4c 55 45 53 28 32 2c  NTO t3 VALUES(2,
1060: 20 33 29 3b 0a 7d 0a 0a 70 72 6f 63 20 6e 6f 6e   3);.}..proc non
1070: 64 65 74 65 72 20 7b 61 72 67 73 7d 20 7b 0a 20  deter {args} {. 
1080: 20 69 6e 63 72 20 3a 3a 6e 6f 6e 64 65 74 65 72   incr ::nondeter
1090: 5f 72 65 74 0a 20 20 65 78 70 72 20 7b 24 3a 3a  _ret.  expr {$::
10a0: 6e 6f 6e 64 65 74 65 72 5f 72 65 74 20 25 20 32  nondeter_ret % 2
10b0: 7d 0a 7d 0a 64 62 20 66 75 6e 63 20 6e 6f 6e 64  }.}.db func nond
10c0: 65 74 65 72 20 6e 6f 6e 64 65 74 65 72 0a 0a 73  eter nondeter..s
10d0: 65 74 20 3a 3a 6e 6f 6e 64 65 74 65 72 5f 72 65  et ::nondeter_re
10e0: 74 20 30 0a 64 6f 5f 65 78 65 63 73 71 6c 5f 74  t 0.do_execsql_t
10f0: 65 73 74 20 34 2e 32 20 7b 0a 20 20 53 45 4c 45  est 4.2 {.  SELE
1100: 43 54 20 61 2c 20 73 75 6d 28 62 29 20 46 52 4f  CT a, sum(b) FRO
1110: 4d 20 74 33 20 47 52 4f 55 50 20 42 59 20 61 20  M t3 GROUP BY a 
1120: 48 41 56 49 4e 47 20 6e 6f 6e 64 65 74 65 72 28  HAVING nondeter(
1130: 61 29 0a 7d 20 7b 31 20 36 7d 0a 0a 23 20 49 66  a).} {1 6}..# If
1140: 20 74 68 65 20 74 65 72 6d 20 77 68 65 72 65 20   the term where 
1150: 6d 6f 76 65 64 2c 20 74 68 65 20 71 75 65 72 79  moved, the query
1160: 20 61 62 6f 76 65 20 77 6f 75 6c 64 20 72 65 74   above would ret
1170: 75 72 6e 20 74 68 65 20 73 61 6d 65 0a 23 20 72  urn the same.# r
1180: 65 73 75 6c 74 20 61 73 20 74 68 65 20 66 6f 6c  esult as the fol
1190: 6c 6f 77 69 6e 67 2e 20 42 75 74 20 69 74 20 64  lowing. But it d
11a0: 6f 65 73 20 6e 6f 74 2e 0a 23 0a 73 65 74 20 3a  oes not..#.set :
11b0: 3a 6e 6f 6e 64 65 74 65 72 5f 72 65 74 20 30 0a  :nondeter_ret 0.
11c0: 64 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20  do_execsql_test 
11d0: 34 2e 33 20 7b 0a 20 20 53 45 4c 45 43 54 20 61  4.3 {.  SELECT a
11e0: 2c 20 73 75 6d 28 62 29 20 46 52 4f 4d 20 74 33  , sum(b) FROM t3
11f0: 20 57 48 45 52 45 20 6e 6f 6e 64 65 74 65 72 28   WHERE nondeter(
1200: 61 29 20 47 52 4f 55 50 20 42 59 20 61 0a 7d 20  a) GROUP BY a.} 
1210: 7b 31 20 34 20 32 20 32 7d 0a 0a 0a 66 69 6e 69  {1 4 2 2}...fini
1220: 73 68 5f 74 65 73 74 0a                          sh_test.