-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathselect_index
1322 lines (1195 loc) · 46.2 KB
/
select_index
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# LogicTest: local-opt
statement ok
CREATE TABLE t (
a INT,
b CHAR,
c INT,
d CHAR,
PRIMARY KEY (a, b),
INDEX bc (b, c),
INDEX dc (d, c),
INDEX a_desc (a DESC),
FAMILY (a, b),
FAMILY (c),
FAMILY (d)
)
statement ok
INSERT INTO t VALUES
(1, 'one', 11, 'foo'),
(2, 'two', 22, 'bar'),
(3, 'three', 33, 'blah')
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a = 2]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/2/'two' -> NULL
fetched: /t/primary/2/'two'/c -> 22
fetched: /t/primary/2/'two'/d -> 'bar'
output row: [2 'two' 22 'bar']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a IN (1, 3)]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'one' -> NULL
fetched: /t/primary/1/'one'/c -> 11
fetched: /t/primary/1/'one'/d -> 'foo'
output row: [1 'one' 11 'foo']
fetched: /t/primary/3/'three' -> NULL
fetched: /t/primary/3/'three'/c -> 33
fetched: /t/primary/3/'three'/d -> 'blah'
output row: [3 'three' 33 'blah']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE d = 'foo' OR d = 'bar']
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/dc/'bar'/22/2/'two' -> NULL
output row: [2 'two' 22 'bar']
fetched: /t/dc/'foo'/11/1/'one' -> NULL
output row: [1 'one' 11 'foo']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE (d, c) IN (('foo', 11), ('bar', 22))]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/dc/'bar'/22/2/'two' -> NULL
output row: [2 'two' 22 'bar']
fetched: /t/dc/'foo'/11/1/'one' -> NULL
output row: [1 'one' 11 'foo']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE (d, c) = ('foo', 11)]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/dc/'foo'/11/1/'one' -> NULL
output row: [1 'one' 11 'foo']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a < 2]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'one' -> NULL
fetched: /t/primary/1/'one'/c -> 11
fetched: /t/primary/1/'one'/d -> 'foo'
output row: [1 'one' 11 'foo']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a <= (1 + 1)]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'one' -> NULL
fetched: /t/primary/1/'one'/c -> 11
fetched: /t/primary/1/'one'/d -> 'foo'
output row: [1 'one' 11 'foo']
fetched: /t/primary/2/'two' -> NULL
fetched: /t/primary/2/'two'/c -> 22
fetched: /t/primary/2/'two'/d -> 'bar'
output row: [2 'two' 22 'bar']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT a, b FROM t WHERE b > 't']
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/bc/'three'/33/3 -> NULL
output row: [3 'three']
fetched: /t/bc/'two'/22/2 -> NULL
output row: [2 'two']
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE d < ('b' || 'l')]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/dc/'bar'/22/2/'two' -> NULL
output row: [2 'two' 22 'bar']
# TODO(radu): Need to select "dc" index
# The where-clause does not contain columns matching a prefix of any
# index. Note that the index "dc" was chosen because it contains fewer
# keys per row than the primary key index while still containing all
# of the needed columns.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE c = 22]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'one' -> NULL
fetched: /t/primary/1/'one'/c -> 11
fetched: /t/primary/1/'one'/d -> 'foo'
fetched: /t/primary/2/'two' -> NULL
fetched: /t/primary/2/'two'/c -> 22
fetched: /t/primary/2/'two'/d -> 'bar'
output row: [2 'two' 22 'bar']
fetched: /t/primary/3/'three' -> NULL
fetched: /t/primary/3/'three'/c -> 33
fetched: /t/primary/3/'three'/d -> 'blah'
# Use the descending index
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT a FROM t ORDER BY a DESC]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/a_desc/3/'three' -> NULL
output row: [3]
fetched: /t/a_desc/2/'two' -> NULL
output row: [2]
fetched: /t/a_desc/1/'one' -> NULL
output row: [1]
# Use the descending index with multiple spans.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT a FROM t WHERE a in (2, 3) ORDER BY a DESC]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/a_desc/3/'three' -> NULL
output row: [3]
fetched: /t/a_desc/2/'two' -> NULL
output row: [2]
# Index selection occurs in direct join operands too.
# TODO(radu): except that it doesn't in this case with new optimizer
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM t x JOIN t y USING(b) WHERE x.b < '3'
----
render · · (b, a, c, d, a, c, d) ·
│ render 0 b · ·
│ render 1 a · ·
│ render 2 c · ·
│ render 3 d · ·
│ render 4 a · ·
│ render 5 c · ·
│ render 6 d · ·
└── join · · (a, b, c, d, a, b, c, d) ·
│ type inner · ·
│ equality (b) = (b) · ·
├── scan · · (a, b, c, d) ·
│ table t@primary · ·
│ spans ALL · ·
│ filter b < '3' · ·
└── scan · · (a, b, c, d) ·
· table t@primary · ·
· spans ALL · ·
statement ok
TRUNCATE TABLE t
statement ok
INSERT INTO t VALUES
(1, 'a', NULL, NULL),
(1, 'b', NULL, NULL),
(1, 'c', NULL, NULL)
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a = 1 AND b > 'b']
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'c' -> NULL
output row: [1 'c' NULL NULL]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a > 0 AND b > 'b']
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'c' -> NULL
output row: [1 'c' NULL NULL]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a > 1 AND b > 'b']
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
query TTT
EXPLAIN SELECT * FROM t WHERE a > 1 AND a < 2
----
norows · ·
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t WHERE a = 1 AND 'a' < b AND 'c' > b]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/primary/1/'b' -> NULL
output row: [1 'b' NULL NULL]
statement ok
DROP TABLE t
statement ok
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
INDEX ab (a, b)
)
statement ok
INSERT INTO t VALUES (1, 2), (3, 4), (5, 6)
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a >= 3 AND a < 5]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a BETWEEN 3 AND 4]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a BETWEEN 3 AND 5]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
fetched: /t/ab/5/6 -> NULL
output row: [5 6]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a < 2 OR a < 4]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/1/2 -> NULL
output row: [1 2]
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a < 3 OR a <= 3]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/1/2 -> NULL
output row: [1 2]
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a <= 3 OR a < 3]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/1/2 -> NULL
output row: [1 2]
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a > 3 OR a >= 3]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
fetched: /t/ab/5/6 -> NULL
output row: [5 6]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a >= 3 OR a > 3]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
fetched: /t/ab/5/6 -> NULL
output row: [5 6]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a = 3 OR a = 5]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
fetched: /t/ab/5/6 -> NULL
output row: [5 6]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a < 3 OR a > 3]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/1/2 -> NULL
output row: [1 2]
fetched: /t/ab/5/6 -> NULL
output row: [5 6]
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM t@ab WHERE a + 1 = 4]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /t/ab/3/4 -> NULL
output row: [3 4]
query TTT
EXPLAIN SELECT * FROM t WHERE a = 1 AND false
----
norows · ·
query TTT
EXPLAIN SELECT * FROM t WHERE a = 1 AND NULL
----
norows · ·
query TTT
EXPLAIN SELECT * FROM t WHERE a = NULL AND a != NULL
----
norows · ·
# Make sure that mixed type comparison operations are not used
# for selecting indexes.
statement ok
DROP TABLE t
statement ok
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
c INT,
INDEX b_desc (b DESC),
INDEX bc (b, c)
)
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE c > 1
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, c) ·
· table t@primary · ·
· spans ALL · ·
· filter c > 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE c < 1 AND b < 5
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /!NULL-/4/1 · ·
· filter c < 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE c > 1.0
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, c) ·
· table t@primary · ·
· spans ALL · ·
· filter c > 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE c < 1.0
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, c) ·
· table t@primary · ·
· spans ALL · ·
· filter c < 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE c > 1.0 AND b < 5
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /!NULL-/5 · ·
· filter c > 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE b < 5.0 AND c < 1
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /!NULL-/4/1 · ·
· filter c < 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE (b, c) = (5, 1)
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /5/1-/5/2 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE (b, c) = (5.0, 1)
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /5/1-/5/2 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE (b, c) = (5.1, 1)
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b, c) ·
· table t@bc · ·
· spans /!NULL- · ·
· filter (b = 5.1) AND (c = 1) · ·
# Note the span is reversed because of #20203.
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM t WHERE b IN (5.0, 1)
----
render · · (a) ·
│ render 0 a · ·
└── scan · · (a, b) ·
· table t@b_desc · ·
· spans /5-/4 /1-/0 · ·
statement ok
CREATE TABLE abcd (
a INT,
b INT,
c INT,
d INT,
INDEX adb (a, d, b),
INDEX abcd (a, b, c, d)
)
# Verify that we prefer the index where more columns are constrained, even if it
# has more keys per row.
query TTTTT
EXPLAIN (VERBOSE) SELECT b FROM abcd WHERE (a, b) = (1, 4)
----
render · · (b) ·
│ render 0 b · ·
└── scan · · (a, b) ·
· table abcd@abcd · ·
· spans /1/4-/1/5 · ·
# TODO(radu): in this case, we're not preferring abcd, unlike 2.0
query TTTTT
EXPLAIN (VERBOSE) SELECT b FROM abcd WHERE (a, b) IN ((1, 4), (2, 9))
----
render · · (b) ·
│ render 0 b · ·
└── scan · · (a, b) ·
· table abcd@abcd · ·
· spans /1/4-/1/5 /2/9-/2/10 · ·
statement ok
CREATE TABLE ab (
s STRING,
i INT
);
query TTTTT
EXPLAIN (VERBOSE) SELECT i, s FROM ab WHERE (i, s) < (1, 'c')
----
render · · (i, s) ·
│ render 0 i · ·
│ render 1 s · ·
└── scan · · (s, i) ·
· table ab@primary · ·
· spans ALL · ·
· filter (i, s) < (1, 'c') · ·
statement ok
CREATE INDEX baz ON ab (i, s)
query TTTTT
EXPLAIN (VERBOSE) SELECT i, s FROM ab@baz WHERE (i, s) < (1, 'c')
----
render · · (i, s) ·
│ render 0 test.public.ab.i · ·
│ render 1 test.public.ab.s · ·
└── scan · · (s, i, rowid[hidden,omitted]) rowid!=NULL; weak-key(s,i,rowid)
· table ab@baz · ·
· spans /!NULL-/1/"c" · ·
· filter (i, s) < (1, 'c') · ·
# Check that primary key definitions can indicate index ordering,
# and this information is subsequently used during index selection
# and span generation. #13882
query TTBITTBB
CREATE TABLE abz(a INT, b INT, c INT, PRIMARY KEY (a DESC, b ASC), UNIQUE(c DESC, b ASC)); SHOW INDEX FROM abz
----
abz primary true 1 a DESC false false
abz primary true 2 b ASC false false
abz abz_c_b_key true 1 c DESC false false
abz abz_c_b_key true 2 b ASC false false
abz abz_c_b_key true 3 a ASC false true
query TTTTT
EXPLAIN (VERBOSE) SELECT a FROM abz ORDER BY a DESC LIMIT 1
----
scan · · (a) -a
· table abz@primary · ·
· spans ALL · ·
· limit 1 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT c FROM abz ORDER BY c DESC LIMIT 1
----
scan · · (c) -c
· table abz@abz_c_b_key · ·
· spans ALL · ·
· limit 1 · ·
# Issue #14426: verify we don't have an internal filter that contains "a IN ()"
# (which causes an error in DistSQL due to expression serialization).
statement ok
CREATE TABLE tab0(
k INT PRIMARY KEY,
a INT,
b INT
)
query TTTTT
EXPLAIN (VERBOSE) SELECT k FROM tab0 WHERE (a IN (6) AND a > 6) OR b >= 4
----
render · · (k) ·
│ render 0 k · ·
└── scan · · (k, a, b) ·
· table tab0@primary · ·
· spans ALL · ·
· filter ((a IN (6)) AND (a > 6)) OR (b >= 4) · ·
# Check that no extraneous rows are fetched due to excessive batching (#15910)
# The test is composed of three parts: populate a table, check
# that the problematic plan is properly derived from the test query,
# then test the results.
statement ok
CREATE TABLE test2 (id BIGSERIAL PRIMARY KEY, k TEXT UNIQUE, v INT DEFAULT 42);
INSERT INTO test2(k)
VALUES ('001'),('002'),('003'),('004'),('005'),('006'),('007'),('008'),('009'),('010'),
('011'),('012'),('013'),('014'),('015'),('016'),('017'),('018'),('019'),('020'),
('021'),('022'),('023'),('024'),('025'),('026'),('027'),('028'),('029'),('030')
# Plan check:
# The query is using an index-join and the limit is propagated to the scan.
# TODO(radu): need revscan, index-join, and limit pushdown
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM test2 WHERE k <= '100' ORDER BY k DESC LIMIT 20
----
limit · · (id, k, v) -k
│ count 20 · ·
└── sort · · (id, k, v) -k
│ order -k · ·
└── scan · · (id, k, v) ·
· table test2@primary · ·
· spans ALL · ·
· filter k <= '100' · ·
# Result check: The following query must not issue more than the
# requested LIMIT K/V reads, even though an index join batches 100
# rows at a time -- the limit should be enforced by the scan. We are
# reading from the end (ORDER BY k DESC) so we should see 20 values
# from 030 to 011 (thus not 001-010).
query T
SELECT regexp_replace(message, '\d\d\d\d\d+', '...PK...')
FROM [SHOW KV TRACE FOR SELECT * FROM test2 WHERE k <= '100' ORDER BY k DESC LIMIT 20]
WHERE message LIKE 'fetched:%'
----
fetched: /test2/primary/...PK.../k/v -> /'001'/42
fetched: /test2/primary/...PK.../k/v -> /'002'/42
fetched: /test2/primary/...PK.../k/v -> /'003'/42
fetched: /test2/primary/...PK.../k/v -> /'004'/42
fetched: /test2/primary/...PK.../k/v -> /'005'/42
fetched: /test2/primary/...PK.../k/v -> /'006'/42
fetched: /test2/primary/...PK.../k/v -> /'007'/42
fetched: /test2/primary/...PK.../k/v -> /'008'/42
fetched: /test2/primary/...PK.../k/v -> /'009'/42
fetched: /test2/primary/...PK.../k/v -> /'010'/42
fetched: /test2/primary/...PK.../k/v -> /'011'/42
fetched: /test2/primary/...PK.../k/v -> /'012'/42
fetched: /test2/primary/...PK.../k/v -> /'013'/42
fetched: /test2/primary/...PK.../k/v -> /'014'/42
fetched: /test2/primary/...PK.../k/v -> /'015'/42
fetched: /test2/primary/...PK.../k/v -> /'016'/42
fetched: /test2/primary/...PK.../k/v -> /'017'/42
fetched: /test2/primary/...PK.../k/v -> /'018'/42
fetched: /test2/primary/...PK.../k/v -> /'019'/42
fetched: /test2/primary/...PK.../k/v -> /'020'/42
fetched: /test2/primary/...PK.../k/v -> /'021'/42
fetched: /test2/primary/...PK.../k/v -> /'022'/42
fetched: /test2/primary/...PK.../k/v -> /'023'/42
fetched: /test2/primary/...PK.../k/v -> /'024'/42
fetched: /test2/primary/...PK.../k/v -> /'025'/42
fetched: /test2/primary/...PK.../k/v -> /'026'/42
fetched: /test2/primary/...PK.../k/v -> /'027'/42
fetched: /test2/primary/...PK.../k/v -> /'028'/42
fetched: /test2/primary/...PK.../k/v -> /'029'/42
fetched: /test2/primary/...PK.../k/v -> /'030'/42
# Regression test for #20035.
statement ok
CREATE TABLE favorites (
id INT NOT NULL DEFAULT unique_rowid(),
resource_type STRING(30) NOT NULL,
resource_key STRING(255) NOT NULL,
device_group STRING(30) NOT NULL,
customerid INT NOT NULL,
jurisdiction STRING(2) NOT NULL,
brand STRING(255) NOT NULL,
created_ts TIMESTAMP NULL,
guid_id STRING(100) NOT NULL,
locale STRING(10) NOT NULL DEFAULT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX favorites_idx (resource_type ASC, device_group ASC, resource_key ASC, customerid ASC),
INDEX favorites_guid_idx (guid_id ASC),
INDEX favorites_glob_fav_idx (resource_type ASC, device_group ASC, jurisdiction ASC, brand ASC, locale ASC, resource_key ASC),
FAMILY "primary" (id, resource_type, resource_key, device_group, customerid, jurisdiction, brand, created_ts, guid_id, locale)
)
statement ok
INSERT INTO favorites (customerid, guid_id, resource_type, device_group, jurisdiction, brand, locale, resource_key)
VALUES (1, '1', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'tp'),
(2, '2', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'ts'),
(3, '3', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'ts1'),
(4, '4', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'ts2'),
(5, '5', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'ts3'),
(6, '6', 'GAME', 'web', 'MT', 'xxx', 'en_GB', 'ts4')
query TTT
EXPLAIN SELECT
resource_key,
count(resource_key) total
FROM favorites f1
WHERE f1.jurisdiction = 'MT'
AND f1.brand = 'xxx'
AND f1.resource_type = 'GAME'
AND f1.device_group = 'web'
AND f1.locale = 'en_GB'
AND f1.resource_key IN ('ts', 'ts2', 'ts3')
GROUP BY resource_key
ORDER BY total DESC
----
sort · ·
│ order -total
└── group · ·
│ aggregate 0 resource_key
│ aggregate 1 count(resource_key)
│ group by @2
└── scan · ·
· table favorites@favorites_glob_fav_idx
· spans /"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts"-/"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts"/PrefixEnd /"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts2"-/"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts2"/PrefixEnd /"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts3"-/"GAME"/"web"/"MT"/"xxx"/"en_GB"/"ts3"/PrefixEnd
query TI rowsort
SELECT
resource_key,
count(resource_key) total
FROM favorites f1
WHERE f1.jurisdiction = 'MT'
AND f1.brand = 'xxx'
AND f1.resource_type = 'GAME'
AND f1.device_group = 'web'
AND f1.locale = 'en_GB'
AND f1.resource_key IN ('ts', 'ts2', 'ts3')
GROUP BY resource_key
ORDER BY total DESC
----
ts 1
ts2 1
ts3 1
# Regression tests for #20362 (IS NULL handling).
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM abcd@abcd WHERE a IS NULL AND b > 5
----
render · · (a, b, c, d) a=CONST; b!=NULL
│ render 0 test.public.abcd.a · ·
│ render 1 test.public.abcd.b · ·
│ render 2 test.public.abcd.c · ·
│ render 3 test.public.abcd.d · ·
└── scan · · (a, b, c, d, rowid[hidden,omitted]) a=CONST; b!=NULL; rowid!=NULL; weak-key(b,c,d,rowid)
· table abcd@abcd · ·
· spans /NULL/6-/!NULL · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM abcd@abcd WHERE a IS NULL AND b < 5
----
render · · (a, b, c, d) a=CONST; b!=NULL
│ render 0 test.public.abcd.a · ·
│ render 1 test.public.abcd.b · ·
│ render 2 test.public.abcd.c · ·
│ render 3 test.public.abcd.d · ·
└── scan · · (a, b, c, d, rowid[hidden,omitted]) a=CONST; b!=NULL; rowid!=NULL; weak-key(b,c,d,rowid)
· table abcd@abcd · ·
· spans /NULL/!NULL-/NULL/5 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM abcd@abcd WHERE a IS NULL ORDER BY b
----
render · · (a, b, c, d) a=CONST; +b
│ render 0 test.public.abcd.a · ·
│ render 1 test.public.abcd.b · ·
│ render 2 test.public.abcd.c · ·
│ render 3 test.public.abcd.d · ·
└── scan · · (a, b, c, d, rowid[hidden,omitted]) a=CONST; rowid!=NULL; weak-key(b,c,d,rowid); +b
· table abcd@abcd · ·
· spans /NULL-/!NULL · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM abcd@abcd WHERE a = 1 AND b IS NULL AND c > 0 AND c < 10 ORDER BY c
----
render · · (a, b, c, d) a=CONST; b=CONST; c!=NULL; +c
│ render 0 test.public.abcd.a · ·
│ render 1 test.public.abcd.b · ·
│ render 2 test.public.abcd.c · ·
│ render 3 test.public.abcd.d · ·
└── scan · · (a, b, c, d, rowid[hidden,omitted]) a=CONST; b=CONST; c!=NULL; rowid!=NULL; weak-key(c,d,rowid); +c
· table abcd@abcd · ·
· spans /1/NULL/1-/1/NULL/10 · ·
# Regression test for #3548: verify we create constraints on implicit columns
# when they are part of the key (non-unique index).
statement ok
CREATE TABLE abc (a INT, b INT, c INT, PRIMARY KEY(a,b), INDEX(c))
query TTTTT
EXPLAIN (VERBOSE) SELECT c FROM abc WHERE c = 1 and a = 3
----
render · · (c) ·
│ render 0 c · ·
└── scan · · (a, c) ·
· table abc@abc_c_idx · ·
· spans /1/3-/1/4 · ·
# Verify we don't create constraints on implicit columns when they are not part
# of the key (unique index).
statement ok
CREATE TABLE def (d INT, e INT, f INT, PRIMARY KEY(d,e), UNIQUE INDEX(f))
query TTTTT
EXPLAIN (VERBOSE) SELECT f FROM def WHERE f = 1 and d = 3
----
render · · (f) ·
│ render 0 f · ·
└── scan · · (d, f) ·
· table def@primary · ·
· spans /3-/4 · ·
· filter f = 1 · ·
# Regression test for #20504.
query TTTTT
EXPLAIN (VERBOSE) SELECT a, b FROM abc WHERE (a, b) BETWEEN (1, 2) AND (3, 4)
----
scan · · (a, b) ·
· table abc@primary · ·
· spans /1/2-/3/4/# · ·
# Regression test for #21831.
statement ok
CREATE TABLE str (k INT PRIMARY KEY, v STRING, INDEX(v))
query TTTTT
EXPLAIN (VERBOSE) SELECT k, v FROM str WHERE v LIKE 'ABC%'
----
scan · · (k, v) ·
· table str@str_v_idx · ·
· spans /"ABC"-/"ABD" · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT k, v FROM str WHERE v LIKE 'ABC%Z'
----
scan · · (k, v) ·
· table str@str_v_idx · ·
· spans /"ABC"-/"ABD" · ·
· filter v LIKE 'ABC%Z' · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT k, v FROM str WHERE v SIMILAR TO 'ABC_*'
----
scan · · (k, v) ·
· table str@str_v_idx · ·
· spans /"ABC"-/"ABD" · ·
· filter v SIMILAR TO 'ABC_*' · ·
# Test that we generate spans for IS (NOT) DISTINCT FROM.
statement ok
CREATE TABLE xy (x INT, y INT, INDEX (y))
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE y IS NOT DISTINCT FROM NULL
----
render · · (x, y) ·
│ render 0 x · ·
│ render 1 y · ·
└── index-join · · (x, y, rowid[hidden]) ·
├── scan · · (y, rowid[hidden]) ·
│ table xy@xy_y_idx · ·
│ spans /NULL-/!NULL · ·
└── scan · · (x, y, rowid[hidden]) ·
· table xy@primary · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE y IS NOT DISTINCT FROM 4
----
render · · (x, y) ·
│ render 0 x · ·
│ render 1 y · ·
└── index-join · · (x, y, rowid[hidden]) ·
├── scan · · (y, rowid[hidden]) ·
│ table xy@xy_y_idx · ·
│ spans /4-/5 · ·
└── scan · · (x, y, rowid[hidden]) ·
· table xy@primary · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT x FROM xy WHERE y > 0 AND y < 2 ORDER BY y
----
render · · (x) ·
│ render 0 x · ·
└── index-join · · (x, y, rowid[hidden]) +y
├── scan · · (y, rowid[hidden]) +y
│ table xy@xy_y_idx · ·
│ spans /1-/2 · ·
└── scan · · (x, y, rowid[hidden]) ·
· table xy@primary · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE y IS DISTINCT FROM NULL
----
scan · · (x, y) ·
· table xy@primary · ·
· spans ALL · ·
· filter y IS NOT NULL · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE y IS DISTINCT FROM 4
----
scan · · (x, y) ·
· table xy@primary · ·
· spans ALL · ·
· filter y IS DISTINCT FROM 4 · ·
# Regression tests for #22670.
statement ok
CREATE INDEX xy_idx ON xy (x, y)
statement ok
INSERT INTO xy VALUES (NULL, NULL), (1, NULL), (NULL, 1), (1, 1)
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE x IN (NULL, 1, 2)
----
scan · · (x, y) ·
· table xy@xy_idx · ·
· spans /1-/3 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM xy WHERE (x, y) IN ((NULL, NULL), (1, NULL), (NULL, 1), (1, 1), (1, 2))
----
scan · · (x, y) ·
· table xy@xy_idx · ·
· spans /1/1-/1/3 · ·
# ------------------------------------------------------------------------------
# Non-covering index
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE noncover (
a INT PRIMARY KEY,
b INT,
c INT,
d INT,
INDEX b (b),
UNIQUE INDEX c (c),
FAMILY (a),
FAMILY (b),
FAMILY (c),
FAMILY (d)
)
statement ok
INSERT INTO noncover VALUES (1, 2, 3, 4), (5, 6, 7, 8)
query TTT
EXPLAIN SELECT * FROM noncover WHERE b = 2
----
index-join · ·
├── scan · ·
│ table noncover@b
│ spans /2-/3
└── scan · ·
· table noncover@primary
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM noncover WHERE b = 2]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /noncover/b/2/1 -> NULL
fetched: /noncover/primary/1 -> NULL
fetched: /noncover/primary/1/b -> 2
fetched: /noncover/primary/1/c -> 3
fetched: /noncover/primary/1/d -> 4
output row: [1 2 3 4]
query TTT
EXPLAIN SELECT * FROM noncover WHERE c = 6
----
index-join · ·
├── scan · ·
│ table noncover@c
│ spans /6-/7
└── scan · ·
· table noncover@primary
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT * FROM noncover WHERE c = 7]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /noncover/c/7 -> /5
fetched: /noncover/primary/5 -> NULL
fetched: /noncover/primary/5/b -> 6
fetched: /noncover/primary/5/c -> 7
fetched: /noncover/primary/5/d -> 8
output row: [5 6 7 8]
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM noncover WHERE c > 0 ORDER BY c DESC
----
sort · · (a, b, c, d) -c
│ order -c · ·
└── scan · · (a, b, c, d) ·
· table noncover@primary · ·
· spans ALL · ·
· filter c > 0 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM noncover WHERE c > 0 ORDER BY c
----
sort · · (a, b, c, d) +c
│ order +c · ·
└── scan · · (a, b, c, d) ·
· table noncover@primary · ·
· spans ALL · ·
· filter c > 0 · ·
query TTTTT
EXPLAIN (VERBOSE) SELECT * FROM noncover WHERE c > 0 AND d = 8
----
scan · · (a, b, c, d) ·
· table noncover@primary · ·
· spans ALL · ·
· filter (c > 0) AND (d = 8) · ·
# The following testcases verify that when we have a small limit, we prefer an
# order-matching index.
query TTT
EXPLAIN SELECT * FROM noncover ORDER BY c
----
sort · ·
│ order +c
└── scan · ·
· table noncover@primary
· spans ALL
# TODO(radu): need to prefer the order-matching index
query TTT
EXPLAIN SELECT * FROM noncover ORDER BY c LIMIT 5
----
limit · ·
└── sort · ·
│ order +c
└── scan · ·