summaryrefslogtreecommitdiffstats
path: root/test/bestindex8.test
blob: e95c3c6dc207d068892a94ecb2c8543c0a206468 (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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# 2020-01-29
#
# 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.
#
#***********************************************************************
# 

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

ifcapable !vtab {
  finish_test
  return
}

register_tcl_module db

proc vtab_command {src method args} {
  switch -- $method {
    xConnect {
      return "CREATE TABLE xxx(a, b)"
    }

    xBestIndex {
      set hdl [lindex $args 0]
      set clist [$hdl constraints]
      set orderby [$hdl orderby]
      lappend ::lBestIndexDistinct [$hdl distinct]

      #puts "ORDERBY: $orderby" 
      set iCons 0
      set ret [list]
      foreach cons $clist {
        catch { array unset C }
        array set C $cons
        if {$C(usable)} {
          lappend ret use $iCons
        }
        incr iCons
      }
      if {$orderby=="{column 0 desc 0} {column 1 desc 0}"
       || $orderby=="{column 0 desc 0}"
      } {
        lappend ret orderby 1
        lappend ret idxnum 1
        set ::lOrderByConsumed 1
      }
      return $ret
    }

    xFilter {
      set idxnum [lindex $args 0]
      if {$idxnum} {
        return [list sql "SELECT rowid, a, b FROM $src order by 2, 3"]
      }
      return [list sql "SELECT rowid, a, b FROM $src"]
    }

  }

  return {}
}

do_execsql_test 1.0 {
  CREATE TABLE t1(a, b);
  CREATE INDEX i1 ON t1(a, b);
  INSERT INTO t1 VALUES('a', 'b'), ('c', 'd');
  INSERT INTO t1 VALUES('a', 'b'), ('c', 'd');
  CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);

  CREATE TABLE t0(c0);
  INSERT INTO t0(c0) VALUES (1), (0);
}

foreach {tn sql bDistinct idxinsert bConsumed res} {
  1 "SELECT a, b FROM vt1"                              0 0 0 {a b c d a b c d}
  2 "SELECT DISTINCT a, b FROM vt1"                     2 1 1 {a b c d}
  3 "SELECT DISTINCT a FROM vt1"                        2 1 1 {a c}
  4 "SELECT DISTINCT b FROM vt1"                        2 1 0 {b d}
  5 "SELECT DISTINCT b FROM vt1 ORDER BY a"             0 1 1 {b d}
  6 "SELECT DISTINCT t0.c0 FROM vt1, t0 ORDER BY vt1.a" 0 1 1 {1 0}
  7 "SELECT DISTINCT a, b FROM vt1 ORDER BY a, b"       3 0 1 {a b c d}
  8 "SELECT DISTINCT a, b FROM vt1 ORDER BY a"          0 1 1 {a b c d}
  9 "SELECT DISTINCT a FROM vt1 ORDER BY a, b"          0 1 1 {a c}

 10 "SELECT DISTINCT a, b FROM vt1 WHERE b='b'"         2 1 1 {a b}
 11 "SELECT DISTINCT a, b FROM vt1 WHERE +b='b'"        2 1 1 {a b}
} {
  set ::lBestIndexDistinct ""
  set ::lOrderByConsumed 0
  do_execsql_test 1.$tn.1 $sql $res
  do_test 1.$tn.2 {
    set ::lBestIndexDistinct
  } $bDistinct
  do_test 1.$tn.3 {
    expr {[lsearch [execsql "explain $sql"] IdxInsert]>=0}
  } $idxinsert
  do_test 1.$tn.4 {
    set ::lOrderByConsumed
  } $bConsumed
}

#-------------------------------------------------------------------------
reset_db
register_tcl_module db

proc vtab_command {src method args} {
  switch -- $method {
    xConnect {
      return "CREATE TABLE xxx(a, b)"
    }

    xBestIndex {
      set hdl [lindex $args 0]
      set ret [list]

      set iCons 0
      foreach cons [$hdl constraints] {
        array set C $cons
        if {($C(op)=="limit" || $C(op)=="offset") && $C(usable)} {
          lappend ret use $iCons
        }
        incr iCons
      }

      return $ret
    }

    xFilter {
      lappend ::lFilterArgs [lindex $args 2]
      return [list sql "SELECT rowid, a, b FROM $src"]
    }

  }

  return {}
}

do_execsql_test 2.0 {
  CREATE TABLE t1(a, b);
  CREATE INDEX i1 ON t1(a, b);
  CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
}

do_test 2.1 {
  set ::lFilterArgs [list]
  execsql { SELECT * FROM vt1 LIMIT 10 }
  set ::lFilterArgs
} {10}

do_test 2.2 {
  set ::lFilterArgs [list]
  execsql { SELECT * FROM vt1 LIMIT 5 OFFSET 50 }
  set ::lFilterArgs
} {{5 50}}

do_test 2.3 {
  set ::lFilterArgs [list]
  execsql { SELECT * FROM vt1 ORDER BY a, b LIMIT 1 OFFSET 1 }
  set ::lFilterArgs
} {{1 1}}

do_test 2.4 {
  set ::lFilterArgs [list]
  execsql { SELECT * FROM vt1 ORDER BY a, +b LIMIT 1 OFFSET 1 }
  set ::lFilterArgs
} {{}}

#-------------------------------------------------------------------------
reset_db
register_tcl_module db

proc vtab_command {src method args} {
  switch -- $method {
    xConnect {
      return "CREATE TABLE xxx(a, b)"
    }

    xBestIndex {
      set hdl [lindex $args 0]
      set lCons [$hdl constraints]

      set ret [list]
      for {set i 0} {$i < [llength $lCons]} {incr i} {
        array set C [lindex $lCons $i]
        if {$C(usable)} {
          lappend ret use $i
          $hdl in $i 1
        }
      }
      return $ret
    }

    xFilter {
      set lArg [lindex $args 2]
      lappend ::lFilterArg {*}$lArg
      return [list sql "SELECT rowid, a, b FROM $src"]
    }

  }

  return {}
}

do_execsql_test 3.0 {
  CREATE TABLE t1(a, b);
  CREATE INDEX i1 ON t1(a, b);
  CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
}

foreach {tn sql lfa} {
  1 "SELECT * FROM vt1 WHERE b IN (10, 20, 30)" {{10 20 30}}
  2 "SELECT * FROM vt1 WHERE b IN ('abc', 'def')" {{abc def}}
  3 "SELECT * FROM vt1 WHERE a IS NULL AND b IN ('abc', 'def')" {{} {abc def}}
  4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b IN ('abc', 'def')" 
     {{1 2 3} {abc def}}

  5 "SELECT * FROM vt1 
     WHERE a IN (SELECT 1 UNION SELECT 2) AND b IN ('abc', 'def')"
     {{1 2} {abc def}}

  6 "SELECT * FROM vt1 
     WHERE b IN ('abc', 'def') AND a IN (SELECT 1 UNION SELECT 2)"
     {{abc def} {1 2}}
} {
  do_test 3.$tn {
    set ::lFilterArg [list]
    execsql $sql
    set ::lFilterArg
  } $lfa
}

#explain_i { SELECT * FROM vt1 WHERE b IN (10, 20, 30) }

#-------------------------------------------------------------------------
reset_db
register_tcl_module db

proc vtab_command {src method args} {
  switch -- $method {
    xConnect {
      return "CREATE TABLE xxx(a, b, c)"
    }

    xBestIndex {
      set hdl [lindex $args 0]
      set lCons [$hdl constraints]

      set ret [list]
      for {set i 0} {$i < [llength $lCons]} {incr i} {
        lappend ::lBestIndexRhs [$hdl rhs_value $i -]
      }
      return $ret
    }

    xFilter {
      return [list sql "SELECT rowid, a, b, c FROM $src"]
    }

  }

  return {}
}

do_execsql_test 4.0 {
  CREATE TABLE t1(a, b, c);
  CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
}

foreach {tn sql lbir} {
  1 "SELECT * FROM vt1 WHERE b = 10" {10}
  2 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30" {abc 30}
  3 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30+2" {abc -}
  4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b < 30+2" {- -}
  5 "SELECT * FROM vt1 WHERE a IS 111 AND b < 30+2" {111 -}
} {
  do_test 4.$tn {
    set ::lBestIndexRhs [list]
    execsql $sql
    set ::lBestIndexRhs
  } $lbir
}

#-------------------------------------------------------------------------
reset_db
db cache size 0
register_tcl_module db

set ::vtab_handle_in 1
proc vtab_command {src method args} {
  switch -- $method {
    xConnect {
      return "CREATE TABLE xxx(a, b, c)"
    }

    xBestIndex {
      set lCols [list a b c]

      set hdl [lindex $args 0]
      set lCons [$hdl constraints]
      set lOrder [$hdl order]

      set L ""
      set O ""
      set W [list]
      set a 0
      for {set i 0} {$i < [llength $lCons]} {incr i} {
        array set C [lindex $lCons $i]
        if {$C(usable)} {
          if { $C(op)=="eq" } {
            set bIn 0
            if {$::vtab_handle_in} { set bIn [$hdl in $i 1] }
            if {$bIn} {
              lappend W "[lindex $lCols $C(column)] IN (%I$a%)"
            } else {
              lappend W "[lindex $lCols $C(column)] = %$a%"
            }
            lappend ret omit $i
          }
          if { $C(op)=="limit"  } { set L " LIMIT %$a%"  ; lappend ret use $i }
          if { $C(op)=="offset" } { set O " OFFSET %$a%" ; lappend ret use $i }
          incr a
        }
      }

      set order ""
      set selectlist "rowid, a, b, c"
      if {[llength $lOrder]} {
        array set sl [list]
        set lO [list]
        foreach s $lOrder {
          array set C $s
          set ad ""
          if {$C(desc)} { set ad " DESC" }
          lappend lO "[lindex $lCols $C(column)]$ad"
          set sl($C(column)) 1
        }
        if {[$hdl distinct]==2} {
          set selectlist "DISTINCT 0"
          foreach i {0 1 2} {
            if {[info exists sl($i)]} {
              append selectlist ", [lindex $lCols $i]"
            } else {
              append selectlist ", 0"
            }
          }
        } else {
          set order " ORDER BY [join $lO ,]"
        }
      }

      set where ""
      if {[llength $W]} { set where " WHERE [join $W { AND }]" }
      set sql "SELECT $selectlist FROM $src$where$order$L$O"

      lappend ret idxStr $sql
      return $ret
    }

    xFilter {
      foreach {idxnum idxstr lArg} $args {}
      set ii 0
      set sql $idxstr
      foreach a $lArg {
        set sql [string map [list %$ii% $a] $sql]
        set sql [string map [list %I$ii% [join $a ,]] $sql]
        incr ii
      }
      lappend ::lFilterSql $sql

      if {[regexp {OFFSET (.*)$} $sql -> off]} {
        set real_sql "
          WITH c(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<$off )
          SELECT 0,0,0,0 FROM c
          UNION ALL SELECT * FROM (
            $sql
          )
        "
      } else {
        set real_sql $sql
      }

      return [list sql $real_sql]
    }

  }

  return {}
}

do_execsql_test 5.0 {
  CREATE TABLE t1(a, b, c);
  CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
  INSERT INTO t1 VALUES(1, 2, 3);
  INSERT INTO t1 VALUES(2, 3, 4);
  INSERT INTO t1 VALUES(3, 4, 5);
  INSERT INTO t1 VALUES(1, 5, 6);
  INSERT INTO t1 VALUES(2, 6, 7);
  INSERT INTO t1 VALUES(3, 7, 8);
  INSERT INTO t1 VALUES(1, 8, 9);
  INSERT INTO t1 VALUES(2, 9, 0);
}

proc do_vtab_test {tn sql vtsql {res {}}} {
  set ::lFilterSql [list]
  uplevel [list do_execsql_test $tn.1 $sql $res]
  uplevel [list do_test $tn.2 {set ::lFilterSql} [list {*}$vtsql]]
}

do_vtab_test 5.1.1 {
  SELECT DISTINCT a FROM vt1
} {
  {SELECT DISTINCT 0, a, 0, 0 FROM t1}
} {1 2 3}

do_vtab_test 5.1.2 {
  SELECT DISTINCT a FROM vt1 ORDER BY a
} {
  {SELECT rowid, a, b, c FROM t1 ORDER BY a}
} {1 2 3}

do_vtab_test 5.1.3 {
  SELECT DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8)
} {
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c IN (4,5,6,7,8)}
} {2 3 1}

set ::vtab_handle_in 0
do_vtab_test 5.1.4 {
  SELECT  DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8)
} {
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 4}
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 5}
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 6}
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 7}
  {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 8}
} {2 3 1}

set ::vtab_handle_in 1
do_vtab_test 5.1.5a {
  SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2
} {
  {SELECT rowid, a, b, c FROM t1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2}
} {1 5 6 2 6 7}

set ::vtab_handle_in 0
do_vtab_test 5.1.5b {
  SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2
} {
  {SELECT rowid, a, b, c FROM t1 WHERE c = 4}
  {SELECT rowid, a, b, c FROM t1 WHERE c = 5}
  {SELECT rowid, a, b, c FROM t1 WHERE c = 6}
  {SELECT rowid, a, b, c FROM t1 WHERE c = 7}
} {1 5 6 2 6 7}
set ::vtab_handle_in 1

finish_test