summaryrefslogtreecommitdiffstats
path: root/test/rowvalue2.test
blob: 1502e31a545a750aab8a1e0908535a3a0cf4ca03 (plain)
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
# 2016 June 17
#
# 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 the SELECT statement.
#

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

do_execsql_test 1.0 {
  CREATE TABLE t1(a, b, c);
  INSERT INTO t1 VALUES(0, 0, 0);
  INSERT INTO t1 VALUES(0, 1, 1);
  INSERT INTO t1 VALUES(1, 0, 2);
  INSERT INTO t1 VALUES(1, 1, 3);

  CREATE INDEX i1 ON t1(a, b);
}

do_execsql_test 1.1.1 { SELECT c FROM t1 WHERE (a, b) >= (1, 0) } {2 3} 
do_execsql_test 1.1.2 { SELECT c FROM t1 WHERE (a, b) > (1, 0)  } {3}

#-------------------------------------------------------------------------

do_execsql_test 2.0.1 {
  CREATE TABLE t2(a INTEGER, b INTEGER, c INTEGER, d INTEGER);
  CREATE INDEX i2 ON t2(a, b, c);
}
do_test 2.0.2 {
  foreach a {0 1 2 3} {
  foreach b {0 1 2 3} {
  foreach c {0 1 2 3} {
    execsql { INSERT INTO t2 VALUES($a, $b, $c, $c + $b*4 + $a*16); }
  }}}
} {}

do_execsql_test 2.1 {
  SELECT d FROM t2 WHERE (a, b) > (2, 2);
} [db eval { SELECT d FROM t2 WHERE a>2 OR (a=2 AND b>2) }]

do_execsql_test 2.2 {
  SELECT d FROM t2 WHERE (a, b) >= (2, 2);
} [db eval { SELECT d FROM t2 WHERE a>2 OR (a=2 AND b>=2) }]

do_execsql_test 2.3 {
  SELECT d FROM t2 WHERE a=1 AND (b, c) >= (1, 2);
} [db eval { SELECT d FROM t2 WHERE +a=1 AND (b>1 OR (b==1 AND c>=2)) }]

do_execsql_test 2.4 {
  SELECT d FROM t2 WHERE a=1 AND (b, c) > (1, 2);
} [db eval { SELECT d FROM t2 WHERE +a=1 AND (b>1 OR (b==1 AND c>2)) }]

#-------------------------------------------------------------------------

set words {
airfare airfield airfields airflow airfoil
airfoils airframe airframes airily airing
airings airless airlift airlifts airline
airliner airlines airlock airlocks airmail
airmails airman airmen airplane airplanes

arraignment arraignments arraigns arrange arranged
arrangement arrangements arranger arrangers arranges
arranging arrant array arrayed arrays
arrears arrest arrested arrester arresters
arresting arrestingly arrestor arrestors arrests

edifices edit edited editing edition
editions editor editorial editorially editorials
editors edits educable educate educated
educates educating education educational educationally
educations educator educators eel eelgrass
}

do_test 3.0 {
  execsql { CREATE TABLE t3(a, b, c, w); }
  foreach w $words {
    set a [string range $w 0 2]
    set b [string range $w 3 5]
    set c [string range $w 6 end]
    execsql { INSERT INTO t3 VALUES($a, $b, $c, $w) }
  }
} {}


foreach {tn idx} {
  IDX1 {}
  IDX2 { CREATE INDEX i3 ON t3(a, b, c); }
  IDX3 { CREATE INDEX i3 ON t3(a, b); }
  IDX4 { CREATE INDEX i3 ON t3(a); }
} {
  execsql { DROP INDEX IF EXISTS i3 }
  execsql $idx

  foreach w $words {
    set a [string range $w 0 2]
    set b [string range $w 3 5]
    set c [string range $w 6 end]

    foreach op [list > >= < <= == IS] {
      do_execsql_test 3.1.$tn.$w.$op [subst -novar {
        SELECT rowid FROM t3 WHERE (a, b, c) [set op] ($a, $b, $c) 
        ORDER BY +rowid
      }] [db eval [subst -novar {
        SELECT rowid FROM t3 WHERE w [set op] $w ORDER BY +rowid
      }]]

      do_execsql_test 3.1.$tn.$w.$op.subselect [subst -novar {
        SELECT rowid FROM t3 WHERE (a, b, c) [set op] (
          SELECT a, b, c FROM t3 WHERE w = $w
        )
        ORDER BY +rowid
      }] [db eval [subst -novar {
        SELECT rowid FROM t3 WHERE w [set op] $w ORDER BY +rowid
      }]]
    }

  }
}

#-------------------------------------------------------------------------
#

do_execsql_test 4.0 {
  CREATE TABLE t4(a, b, c);
  INSERT INTO t4 VALUES(NULL, NULL, NULL);
  INSERT INTO t4 VALUES(NULL, NULL, 0);
  INSERT INTO t4 VALUES(NULL, NULL, 1);
  INSERT INTO t4 VALUES(NULL,    0, NULL);
  INSERT INTO t4 VALUES(NULL,    0, 0);
  INSERT INTO t4 VALUES(NULL,    0, 1);
  INSERT INTO t4 VALUES(NULL,    1, NULL);
  INSERT INTO t4 VALUES(NULL,    1, 0);
  INSERT INTO t4 VALUES(NULL,    1, 1);

  INSERT INTO t4 VALUES(   0, NULL, NULL);
  INSERT INTO t4 VALUES(   0, NULL, 0);
  INSERT INTO t4 VALUES(   0, NULL, 1);
  INSERT INTO t4 VALUES(   0,    0, NULL);
  INSERT INTO t4 VALUES(   0,    0, 0);
  INSERT INTO t4 VALUES(   0,    0, 1);
  INSERT INTO t4 VALUES(   0,    1, NULL);
  INSERT INTO t4 VALUES(   0,    1, 0);
  INSERT INTO t4 VALUES(   0,    1, 1);

  INSERT INTO t4 VALUES(   1, NULL, NULL);
  INSERT INTO t4 VALUES(   1, NULL, 0);
  INSERT INTO t4 VALUES(   1, NULL, 1);
  INSERT INTO t4 VALUES(   1,    0, NULL);
  INSERT INTO t4 VALUES(   1,    0, 0);
  INSERT INTO t4 VALUES(   1,    0, 1);
  INSERT INTO t4 VALUES(   1,    1, NULL);
  INSERT INTO t4 VALUES(   1,    1, 0);
  INSERT INTO t4 VALUES(   1,    1, 1);
}

proc make_expr1 {cList vList op} {
  return "([join $cList ,]) $op ([join $vList ,])"
}

proc make_expr3 {cList vList op} {
  set n [llength $cList]

  set aList [list]
  foreach c [lrange $cList 0 end-1] v [lrange $vList 0 end-1] {
    lappend aList "$c == $v"
  }
  lappend aList "[lindex $cList end] $op [lindex $vList end]"

  return "([join $aList { AND }])"
}

proc make_expr2 {cList vList op} {
  set ret ""

  switch -- $op {
    == - IS {
      set aList [list]
      foreach c $cList v $vList { lappend aList "($c $op $v)" }
      set ret [join $aList " AND "]
    }

    < - > {
      set oList [list]
      for {set i 0} {$i < [llength $cList]} {incr i} {
        lappend oList [make_expr3 [lrange $cList 0 $i] [lrange $vList 0 $i] $op]
      }
      set ret [join $oList " OR "]
    }

    <= - >= {
      set o2 [string range $op 0 0]
      set oList [list]
      for {set i 0} {$i < [llength $cList]-1} {incr i} {
        lappend oList [make_expr3 [lrange $cList 0 $i] [lrange $vList 0 $i] $o2]
      }
      lappend oList [make_expr3 $cList $vList $op]
      set ret [join $oList " OR "]
    }


    default {
      error "Unknown op: $op"
    }
  }

  set ret
}

foreach {tn idx} {
  IDX1 {}
  IDX2 { CREATE INDEX i4 ON t4(a, b, c); }
  IDX3 { CREATE INDEX i4 ON t4(a, b); }
  IDX4 { CREATE INDEX i4 ON t4(a); }
} {
  execsql { DROP INDEX IF EXISTS i4 }
  execsql $idx

  foreach {tn2 vector} {
    1 {0 0 0}
    2 {1 1 1}
    3 {0 0 NULL}
    4 {0 NULL 0}
    5 {NULL 0 0}
    6 {1 1 NULL}
    7 {1 NULL 1}
    8 {NULL 1 1}
  } {
    foreach op { IS == < <= > >= } {
      set e1 [make_expr1 {a b c} $vector $op]
      set e2 [make_expr2 {a b c} $vector $op]

      do_execsql_test 4.$tn.$tn2.$op \
          "SELECT rowid FROM t4 WHERE $e2 ORDER BY +rowid" [
          db eval "SELECT rowid FROM t4 WHERE $e1 ORDER BY +rowid"
      ]
    }
  }
}

do_execsql_test 5.0 {
  CREATE TABLE r1(a TEXT, iB TEXT);
  CREATE TABLE r2(x TEXT, zY INTEGER);
  CREATE INDEX r1ab ON r1(a, iB);

  INSERT INTO r1 VALUES(35, 35);
  INSERT INTO r2 VALUES(35, 36);
  INSERT INTO r2 VALUES(35, 4);
  INSERT INTO r2 VALUES(35, 35);
} {}

foreach {tn lhs rhs} {
  1 {x +zY} {a iB}
  2 {x  zY} {a iB}
  3 {x  zY} {a +iB}
  4 {+x  zY} {a iB}
  5 {x  zY} {+a iB}
} {
  foreach op { IS == < <= > >= } {
    set e1 [make_expr1 $lhs $rhs $op]
    set e2 [make_expr2 $lhs $rhs $op]
    do_execsql_test 5.$tn.$op \
      "SELECT * FROM r1, r2 WHERE $e2 ORDER BY iB" [db eval \
      "SELECT * FROM r1, r2 WHERE $e1 ORDER BY iB"
    ]
  }
}


finish_test