-
Notifications
You must be signed in to change notification settings - Fork 979
/
IndexPlanTest.java
1995 lines (1784 loc) · 78.8 KB
/
IndexPlanTest.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mapr.drill.maprdb.tests.index;
import com.mapr.db.Admin;
import com.mapr.drill.maprdb.tests.MaprDBTestsSuite;
import com.mapr.drill.maprdb.tests.json.BaseJsonTest;
import com.mapr.tests.annotations.ClusterTest;
import org.apache.drill.PlanTestBase;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.apache.drill.common.config.DrillConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters;
import java.util.Properties;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Category(ClusterTest.class)
public class IndexPlanTest extends BaseJsonTest {
final static String PRIMARY_TABLE_NAME = "/tmp/index_test_primary";
final static int PRIMARY_TABLE_SIZE = 10000;
private static final String sliceTargetSmall = "alter session set `planner.slice_target` = 1";
private static final String sliceTargetDefault = "alter session reset `planner.slice_target`";
private static final String noIndexPlan = "alter session set `planner.enable_index_planning` = false";
private static final String defaultHavingIndexPlan = "alter session reset `planner.enable_index_planning`";
private static final String disableHashAgg = "alter session set `planner.enable_hashagg` = false";
private static final String enableHashAgg = "alter session set `planner.enable_hashagg` = true";
private static final String lowNonCoveringSelectivityThreshold = "alter session set `planner.index.noncovering_selectivity_threshold` = 0.00001";
private static final String defaultnonCoveringSelectivityThreshold = "alter session set `planner.index.noncovering_selectivity_threshold` = 0.025";
private static final String incrnonCoveringSelectivityThreshold = "alter session set `planner.index.noncovering_selectivity_threshold` = 0.25";
private static final String disableFTS = "alter session set `planner.disable_full_table_scan` = true";
private static final String enableFTS = "alter session reset `planner.disable_full_table_scan`";
private static final String preferIntersectPlans = "alter session set `planner.index.prefer_intersect_plans` = true";
private static final String defaultIntersectPlans = "alter session reset `planner.index.prefer_intersect_plans`";
private static final String lowRowKeyJoinBackIOFactor
= "alter session set `planner.index.rowkeyjoin_cost_factor` = 0.01";
private static final String defaultRowKeyJoinBackIOFactor
= "alter session reset `planner.index.rowkeyjoin_cost_factor`";
private static final String incrRowKeyJoinConvSelThreshold = "alter session set `planner.rowkeyjoin_conversion_selectivity_threshold` = 1.0";
private static final String defaultRowKeyConvSelThreshold = "alter session reset `planner.rowkeyjoin_conversion_selectivity_threshold`";
private static final String forceRowKeyJoinConversionUsingHashJoin = "alter session set `planner.rowkeyjoin_conversion_using_hashjoin` = true";
private static final String defaultRowKeyJoinConversionUsingHashJoin = "alter session reset `planner.rowkeyjoin_conversion_using_hashjoin`";
/**
* A sample row of this 10K table:
------------------+-----------------------------+--------+
| 1012 | {"city":"pfrrs","state":"pc"} | {"email":"KfFzKUZwNk@gmail.com","phone":"6500005471"} |
{"ssn":"100007423"} | {"fname":"KfFzK","lname":"UZwNk"} | {"age":53.0,"income":45.0} | 1012 |
*
* This test suite generate random content to fill all the rows, since the random function always start from
* the same seed for different runs, when the row count is not changed, the data in table will always be the same,
* thus the query result could be predicted and verified.
*/
@BeforeClass
public static void setupTableIndexes() throws Exception {
Properties overrideProps = new Properties();
overrideProps.setProperty("format-maprdb.json.useNumRegionsForDistribution", "true");
updateTestCluster(1, DrillConfig.create(overrideProps));
MaprDBTestsSuite.setupTests();
MaprDBTestsSuite.createPluginAndGetConf(getDrillbitContext());
test(incrnonCoveringSelectivityThreshold);
System.out.print("setupTableIndexes begins");
Admin admin = MaprDBTestsSuite.getAdmin();
if (admin != null) {
if (admin.tableExists(PRIMARY_TABLE_NAME)) {
admin.deleteTable(PRIMARY_TABLE_NAME);
}
}
LargeTableGen gen = new LargeTableGen(MaprDBTestsSuite.getAdmin());
/**
* indexDef is an array of string, LargeTableGen.generateTableWithIndex will take it as parameter to generate indexes
* for primary table.
* indexDef[3*i] defines i-th index's indexName, NOTE: IF the name begins with "hash", it is a hash index
* indexDef[3*i+1] indexed field,
* and indexDef[3*i+2] defines i-th index's non-indexed fields
*/
final String[] indexDef = //null;
{"i_ssn", "id.ssn", "contact.phone",
"i_state_city", "address.state,address.city", "name.fname,name.lname",//mainly for composite key test
"i_age", "personal.age", "",
"i_age_desc", "personal.age:desc", "",
"i_income", "personal.income", "",
"i_lic", "driverlicense", "reverseid",
"i_state_city_dl", "address.state,address.city", "driverlicense",
"i_cast_int_ssn", "$CAST(id.ssn@INT)", "contact.phone",
"i_cast_vchar_lic", "$CAST(driverlicense@STRING)","contact.email",
"i_state_age_phone", "address.state,personal.age,contact.phone", "name.fname",
"i_cast_age_income_phone", "$CAST(personal.age@INT),$CAST(personal.income@INT),contact.phone", "name.fname",
"i_age_with_fname", "personal.age", "name.fname",
"i_rowid_cast_date_birthdate", "rowid", "$CAST(personal.birthdate@DATE)",
"hash_i_reverseid", "reverseid", "",
"hash_i_cast_timestamp_firstlogin", "$CAST(activity.irs.firstlogin@TIMESTAMP)", "id.ssn"
};
gen.generateTableWithIndex(PRIMARY_TABLE_NAME, PRIMARY_TABLE_SIZE, indexDef);
}
@AfterClass
public static void cleanupTableIndexes() throws Exception {
Admin admin = MaprDBTestsSuite.getAdmin();
if (admin != null) {
if (admin.tableExists(PRIMARY_TABLE_NAME)) {
// admin.deleteTable(PRIMARY_TABLE_NAME);
}
}
test(defaultnonCoveringSelectivityThreshold);
}
@Test
public void CTASTestTable() throws Exception {
String ctasQuery = "CREATE TABLE hbase.tmp.`backup_index_test_primary` " +
"AS SELECT * FROM hbase.`index_test_primary` as t ";
test(ctasQuery);
test("DROP TABLE IF EXISTS hbase.tmp.`backup_index_test_primary`");
}
@Test
public void CoveringPlanWithNonIndexedField() throws Exception {
String query = "SELECT t.`contact`.`phone` AS `phone` FROM hbase.`index_test_primary` as t " +
" where t.id.ssn = '100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_ssn"},
new String[]{"RowKeyJoin"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500005471")
.go();
}
@Test
public void CoveringPlanWithOnlyIndexedField() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.id.ssn = '100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_ssn"},
new String[]{"RowKeyJoin"}
);
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.sqlQuery(query)
.ordered()
.baselineColumns("ssn").baselineValues("100007423")
.go();
}
@Test
public void NoIndexPlanForNonIndexField() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.contact.phone = '6500005471'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary"},
new String[]{"RowKeyJoin", "indexName="}
);
testBuilder()
.sqlQuery(query)
.unOrdered()
.baselineColumns("ssn").baselineValues("100007423")
.baselineColumns("ssn").baselineValues("100007632")
.go();
}
@Test
public void NonCoveringPlan() throws Exception {
String query = "SELECT t.`name`.`fname` AS `fname` FROM hbase.`index_test_primary` as t " +
" where t.id.ssn = '100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin", ".*RestrictedJsonTableGroupScan.*tableName=.*index_test_primary,",
".*JsonTableGroupScan.*tableName=.*index_test_primary,.*indexName=i_ssn"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("fname").baselineValues("KfFzK")
.go();
}
@Test
public void RangeConditionIndexPlan() throws Exception {
String query = "SELECT t.`name`.`lname` AS `lname` FROM hbase.`index_test_primary` as t " +
" where t.personal.age > 52 AND t.name.fname='KfFzK'";
try {
test(defaultHavingIndexPlan + ";" + lowRowKeyJoinBackIOFactor + ";");
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{"RowKeyJoin", ".*RestrictedJsonTableGroupScan.*tableName=.*index_test_primary,",
".*JsonTableGroupScan.*tableName=.*index_test_primary,.*indexName=(i_age|i_age_with_fname)"},
new String[]{}
);
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForTestQuery(lowRowKeyJoinBackIOFactor)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
testBuilder()
.optionSettingQueriesForTestQuery(sliceTargetSmall)
.optionSettingQueriesForBaseline(sliceTargetDefault)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
} finally {
test(defaultRowKeyJoinBackIOFactor);
}
}
@Test
public void CoveringWithSimpleFieldsOnly() throws Exception {
String query = "SELECT t._id AS `tid` FROM hbase.`index_test_primary` as t " +
" where t.driverlicense = 100007423";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"JsonTableGroupScan.*tableName=.*index_test_primary,.*indexName=i_lic"},
new String[]{"RowKeyJoin"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("tid").baselineValues("1012")
.go();
}
@Test
public void NonCoveringWithSimpleFieldsOnly() throws Exception {
String query = "SELECT t.rowid AS `rowid` FROM hbase.`index_test_primary` as t " +
" where t.driverlicense = 100007423";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*" +
"RestrictedJsonTableGroupScan.*tableName=.*index_test_primary(.*[\n\r])+.*" +
"JsonTableGroupScan.*tableName=.*index_test_primary,.*indexName=i_lic"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("rowid").baselineValues("1012")
.go();
}
@Test
public void NonCoveringWithExtraConditonOnPrimary() throws Exception {
String query = "SELECT t.`name`.`fname` AS `fname` FROM hbase.`index_test_primary` as t " +
" where t.personal.age = 53 AND t.name.lname='UZwNk'";
try {
test(defaultHavingIndexPlan + ";" + lowRowKeyJoinBackIOFactor + ";");
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{"RowKeyJoin", ".*RestrictedJsonTableGroupScan",
".*JsonTableGroupScan.*indexName=i_age",},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("fname").baselineValues("KfFzK")
.go();
} finally {
test(defaultRowKeyJoinBackIOFactor);
}
}
@Test
public void Intersect2indexesPlan() throws Exception {
String query = "SELECT t.`name`.`lname` AS `lname` FROM hbase.`index_test_primary` as t " +
" where t.personal.age = 53 AND t.personal.income=45";
try {
test(defaultHavingIndexPlan);
test(preferIntersectPlans + ";" + disableFTS);
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*HashJoin(.*[\n\r])+.*JsonTableGroupScan.*indexName=(i_age|i_income)(.*[\n\r])+.*JsonTableGroupScan.*indexName=(i_age|i_income)"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.unOrdered()
.baselineColumns("lname").baselineValues("UZwNk")
.baselineColumns("lname").baselineValues("foNwtze")
.baselineColumns("lname").baselineValues("qGZVfY")
.go();
testBuilder()
.optionSettingQueriesForTestQuery(sliceTargetSmall)
.optionSettingQueriesForBaseline(sliceTargetDefault)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
} finally {
test(defaultIntersectPlans + ";" + enableFTS);
}
}
@Test
public void CompositeIndexNonCoveringPlan() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'pc' AND t.address.city='pfrrs'";
try {
test(defaultHavingIndexPlan + ";" + lowRowKeyJoinBackIOFactor + ";");
//either i_state_city or i_state_age_phone will be picked depends on cost model, both is fine for testing composite index nonCovering plan
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_state_"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.unOrdered()
.baselineColumns("ssn").baselineValues("100007423")
.baselineColumns("ssn").baselineValues("100008861")
.go();
testBuilder()
.optionSettingQueriesForTestQuery(sliceTargetSmall)
.optionSettingQueriesForBaseline(sliceTargetDefault)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
} finally {
test(defaultRowKeyJoinBackIOFactor);
}
}
@Test//filter cover indexed, included and not in index at all filter
public void CompositeIndexNonCoveringFilterWithAllFieldsPlan() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'pc' AND t.address.city='pfrrs' AND t.driverlicense IN (100007423, 100007424)";
test(defaultHavingIndexPlan+";"+lowRowKeyJoinBackIOFactor+";");
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan.*condition=.*state.*city.*driverlicense.*or.*driverlicense.*(.*[\n\r])+.*JsonTableGroupScan.*indexName="},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.unOrdered()
.baselineColumns("ssn").baselineValues("100007423")
.go();
testBuilder()
.optionSettingQueriesForTestQuery(sliceTargetSmall)
.optionSettingQueriesForBaseline(sliceTargetDefault)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test
public void CompositeIndexCoveringPlan() throws Exception {
String query = "SELECT t.`address`.`city` AS `city` FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'pc' AND t.address.city='pfrrs'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*indexName=i_state_city"},
new String[]{"RowKeyJoin", "Filter"}
);
testBuilder()
.sqlQuery(query)
.unOrdered()
.baselineColumns("city").baselineValues("pfrrs")
.baselineColumns("city").baselineValues("pfrrs")
.go();
testBuilder()
.optionSettingQueriesForTestQuery(sliceTargetSmall)
.optionSettingQueriesForBaseline(sliceTargetDefault)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test
public void TestNonCoveringRangePartition_1() throws Exception {
String query = "SELECT t.`name`.`lname` AS `lname` FROM hbase.`index_test_primary` as t " +
" where t.personal.age = 53";
String[] expectedPlan = new String[] {"RowKeyJoin(.*[\n\r])+.*" +
"RestrictedJsonTableGroupScan.*tableName=.*index_test_primary(.*[\n\r])+.*" +
"RangePartitionExchange(.*[\n\r])+.*" +
"JsonTableGroupScan.*tableName=.*index_test_primary,.*indexName=(i_age|i_age_with_fname)"};
test(defaultHavingIndexPlan+";"+sliceTargetSmall+";");
PlanTestBase.testPlanMatchingPatterns(query,
expectedPlan, new String[]{});
try {
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
} finally {
test(defaultHavingIndexPlan);
test(sliceTargetDefault);
}
}
@Test
public void TestCastVarCharCoveringPlan() throws Exception {
// length 255 is to exact match the casted indexed field's length
String query = "SELECT t._id as tid, cast(t.driverlicense as varchar(255)) as driverlicense FROM hbase.`index_test_primary` as t " +
" where cast(t.driverlicense as varchar(255))='100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_cast_vchar_lic"},
new String[]{"RowKeyJoin"}
);
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.sqlQuery(query)
.ordered()
.baselineColumns("tid", "driverlicense").baselineValues("1012", "100007423")
.go();
}
@Test
public void TestCastINTCoveringPlan() throws Exception {
String query = "SELECT t._id as tid, CAST(t.id.ssn as INT) as ssn, t.contact.phone AS `phone` FROM hbase.`index_test_primary` as t " +
" where CAST(t.id.ssn as INT) = 100007423";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_cast_int_ssn"},
new String[]{"RowKeyJoin"}
);
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.sqlQuery(query)
.ordered()
.baselineColumns("tid", "ssn", "phone").baselineValues("1012", 100007423, "6500005471")
.go();
}
@Test
public void TestCastNonCoveringPlan() throws Exception {
String query = "SELECT t.id.ssn AS `ssn` FROM hbase.`index_test_primary` as t " +
" where CAST(t.id.ssn as INT) = 100007423";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_cast_int_ssn"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("ssn").baselineValues("100007423")
.go();
}
@Test
public void TestCastVarchar_ConvertToRangePlan() throws Exception {
String query = "SELECT t.id.ssn AS `ssn` FROM hbase.`index_test_primary` as t " +
" where CAST(driverlicense as VARCHAR(10)) = '100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*MATCHES \"\\^.*100007423.*E.*\\$\".*indexName=i_cast_vchar_lic"},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("ssn").baselineValues("100007423")
.go();
}
@Test // cast expression in filter is not indexed, but the same field casted to different type was indexed (CAST id.ssn as INT)
public void TestCastNoIndexPlan() throws Exception {
String query = "select t.id.ssn from hbase.`index_test_primary` t where cast(t.id.ssn as varchar(10)) = '100007423'";
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{},
new String[]{"indexName"}
);
}
@Test
public void TestLongerCastVarCharNoIndex() throws Exception {
// length 256 is to exact match the casted indexed field's length
String query = "SELECT t._id as tid, cast(t.driverlicense as varchar(500)) as driverlicense FROM hbase.`index_test_primary` as t " +
" where cast(t.driverlicense as varchar(500))='100007423'";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {},
new String[]{"RowKeyJoin", "indexName="}
);
}
@Test
public void TestCoveringPlanSortRemoved() throws Exception {
String query = "SELECT t.`contact`.`phone` as phone FROM hbase.`index_test_primary` as t " +
" where t.id.ssn <'100000003' order by t.id.ssn";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_ssn"},
new String[]{"Sort"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500008069")
.baselineColumns("phone").baselineValues("6500001411")
.baselineColumns("phone").baselineValues("6500001595")
.go();
}
@Test
public void TestCoveringPlanSortNotRemoved() throws Exception {
String query = "SELECT t.`contact`.`phone` as phone FROM hbase.`index_test_primary` as t " +
" where t.id.ssn <'100000003' order by t.contact.phone";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort", ".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_ssn"},
new String[]{"RowkeyJoin"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500001411")
.baselineColumns("phone").baselineValues("6500001595")
.baselineColumns("phone").baselineValues("6500008069")
.go();
}
@Test
public void TestCoveringPlanSortRemovedWithSimpleFields() throws Exception {
String query = "SELECT t.driverlicense as l FROM hbase.`index_test_primary` as t " +
" where t.driverlicense < 100000003 order by t.driverlicense";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_lic"},
new String[]{"Sort"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("l").baselineValues(100000000l)
.baselineColumns("l").baselineValues(100000001l)
.baselineColumns("l").baselineValues(100000002l)
.go();
}
@Test
public void TestNonCoveringPlanSortRemoved() throws Exception {
String query = "SELECT t.contact.phone as phone FROM hbase.`index_test_primary` as t " +
" where t.driverlicense < 100000003 order by t.driverlicense";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_lic"},
new String[]{"Sort"}
);
String query2 = "SELECT t.name.fname as fname FROM hbase.`index_test_primary` as t " +
" where t.id.ssn < '100000003' order by t.id.ssn";
PlanTestBase.testPlanMatchingPatterns(query2,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName="},
new String[]{"Sort"}
);
// simple field, driverlicense
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500008069")
.baselineColumns("phone").baselineValues("6500001411")
.baselineColumns("phone").baselineValues("6500001595")
.go();
// query on field of item expression(having capProject), non-simple field t.id.ssn
testBuilder()
.sqlQuery(query2)
.ordered()
.baselineColumns("fname").baselineValues("VcFahj")
.baselineColumns("fname").baselineValues("WbKVK")
.baselineColumns("fname").baselineValues("vSAEsyFN")
.go();
test(sliceTargetSmall);
try {
PlanTestBase.testPlanMatchingPatterns(query2,
new String[]{"SingleMergeExchange(.*[\n\r])+.*"
+ "RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_ssn"},
new String[]{"Sort"}
);
} finally {
test(sliceTargetDefault);
}
}
// test cases are from TestNonCoveringPlanSortRemoved. Sort was removed when force_sort_noncovering was default(false)
@Test
public void TestNonCoveringPlanWithNoRemoveSortOption() throws Exception {
try {
test("alter session set `planner.index.force_sort_noncovering`=true");
test(defaultHavingIndexPlan);
String query = "SELECT t.contact.phone as phone FROM hbase.`index_test_primary` as t " +
" where t.driverlicense < 100000003 order by t.driverlicense";
PlanTestBase.testPlanMatchingPatterns(query,
new String[]{"Sort", "RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_lic"},
new String[]{}
);
String query2 = "SELECT t.name.fname as fname FROM hbase.`index_test_primary` as t " +
" where t.id.ssn < '100000003' order by t.id.ssn";
PlanTestBase.testPlanMatchingPatterns(query2,
new String[]{"Sort", "RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName="},
new String[]{}
);
// simple field, driverlicense
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500008069")
.baselineColumns("phone").baselineValues("6500001411")
.baselineColumns("phone").baselineValues("6500001595")
.go();
// query on field of item expression(having capProject), non-simple field t.id.ssn
testBuilder()
.sqlQuery(query2)
.ordered()
.baselineColumns("fname").baselineValues("VcFahj")
.baselineColumns("fname").baselineValues("WbKVK")
.baselineColumns("fname").baselineValues("vSAEsyFN")
.go();
test(sliceTargetSmall);
try {
PlanTestBase.testPlanMatchingPatterns(query2,
new String[]{"Sort", "SingleMergeExchange(.*[\n\r])+.*"
+ "RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_ssn"},
new String[]{}
);
} finally {
test(sliceTargetDefault);
}
}
finally {
test("alter session reset `planner.index.force_sort_noncovering`");
}
}
@Test // 2 table join, each table has local predicate on top-level column
public void TestCoveringPlanJoin_1() throws Exception {
String query = "SELECT count(*) as cnt FROM hbase.`index_test_primary` as t1 " +
" inner join hbase.`index_test_primary` as t2 on t1.driverlicense = t2.driverlicense " +
" where t1.driverlicense < 100000003 and t2.driverlicense < 100000003";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=",
".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName="},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("cnt").baselineValues(3L)
.go();
}
@Test // 2 table join, each table has local predicate on nested column
public void TestCoveringPlanJoin_2() throws Exception {
String query = "SELECT count(*) as cnt FROM hbase.`index_test_primary` as t1 " +
" inner join hbase.`index_test_primary` as t2 on t1.contact.phone = t2.contact.phone " +
" where t1.id.ssn < '100000003' and t2.id.ssn < '100000003' ";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=",
".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName="},
new String[]{}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("cnt").baselineValues(3L)
.go();
}
@Test // leading prefix of index has Equality conditions and ORDER BY last column; Sort SHOULD be dropped
public void TestCoveringPlanSortPrefix_1() throws Exception {
String query = "SELECT t.contact.phone FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age = 35 and t.contact.phone < '6500003000' order by t.contact.phone";
test(defaultHavingIndexPlan);
// we should glue to index i_state_age_phone to make sure we are testing the targeted prefix construction code path
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{"Sort"}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // leading prefix of index has Non-Equality conditions and ORDER BY last column; Sort SHOULD NOT be dropped
public void TestCoveringPlanSortPrefix_2() throws Exception {
String query = "SELECT t.contact.phone FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age < 35 and t.contact.phone < '6500003000' order by t.contact.phone";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort", ".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // ORDER BY last two columns not in the indexed order; Sort SHOULD NOT be dropped
public void TestCoveringPlanSortPrefix_3() throws Exception {
String query = "SELECT CAST(t.personal.age as VARCHAR) as age, t.contact.phone FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age < 35 and t.contact.phone < '6500003000' order by t.contact.phone, t.personal.age";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort", ".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // last two index fields in non-Equality conditions, ORDER BY last two fields; Sort SHOULD be dropped
public void TestCoveringPlanSortPrefix_4() throws Exception {
String query = "SELECT t._id as tid, t.contact.phone, CAST(t.personal.age as VARCHAR) as age FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age < 35 and t.contact.phone < '6500003000' order by t.personal.age, t.contact.phone";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{"Sort"}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // index field in two or more equality conditions, it is not leading prefix, Sort SHOULD NOT be dropped
public void TestCoveringPlanSortPrefix_5() throws Exception {
String query = "SELECT t._id as tid, t.contact.phone, CAST(t.personal.age as VARCHAR) as age FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age IN (31, 32, 33, 34) and t.contact.phone < '6500003000' order by t.contact.phone";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort", ".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // last two index fields in non-Equality conditions, ORDER BY last two fields NULLS FIRST; Sort SHOULD NOT be dropped
public void TestCoveringPlanSortPrefix_6() throws Exception {
String query = "SELECT t._id as tid, t.contact.phone, CAST(t.personal.age as VARCHAR) as age FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age < 35 and t.contact.phone < '6500003000' order by t.personal.age, t.contact.phone NULLS FIRST";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort", ".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test // last two index fields in non-Equality conditions, ORDER BY last two fields NULLS LAST; Sort SHOULD be dropped
public void TestCoveringPlanSortPrefix_7() throws Exception {
String query = "SELECT t._id as tid, t.contact.phone, CAST(t.personal.age as VARCHAR) as age FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'wo' and t.personal.age < 35 and t.contact.phone < '6500003000' order by t.personal.age, t.contact.phone NULLS LAST";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName=i_state_age_phone"},
new String[]{"Sort"}
);
// compare the results of index plan with the no-index plan
testBuilder()
.optionSettingQueriesForTestQuery(defaultHavingIndexPlan)
.optionSettingQueriesForBaseline(noIndexPlan)
.unOrdered()
.sqlQuery(query)
.sqlBaselineQuery(query)
.build()
.run();
}
@Test
public void orderByCastCoveringPlan() throws Exception {
String query = "SELECT t.contact.phone as phone FROM hbase.`index_test_primary` as t " +
" where CAST(t.id.ssn as INT) < 100000003 order by CAST(t.id.ssn as INT)";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {".*JsonTableGroupScan.*tableName=.*index_test_primary.*indexName="},
new String[]{"Sort"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("phone").baselineValues("6500008069")
.baselineColumns("phone").baselineValues("6500001411")
.baselineColumns("phone").baselineValues("6500001595")
.go();
}
@Test // non-covering plan. sort by the only indexed field, sort SHOULD be removed
public void orderByNonCoveringPlan() throws Exception {
String query = "SELECT t.name.lname as lname FROM hbase.`index_test_primary` as t " +
" where t.id.ssn < '100000003' order by t.id.ssn";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName="},
new String[]{"Sort"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("lname").baselineValues("iuMG")
.baselineColumns("lname").baselineValues("KpFq")
.baselineColumns("lname").baselineValues("bkkAvz")
.go();
}
@Test // non-covering plan. order by cast indexed field, sort SHOULD be removed
public void orderByCastNonCoveringPlan() throws Exception {
String query = "SELECT t.name.lname as lname FROM hbase.`index_test_primary` as t " +
" where CAST(t.id.ssn as INT) < 100000003 order by CAST(t.id.ssn as INT)";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName="},
new String[]{"Sort"}
);
testBuilder()
.sqlQuery(query)
.ordered()
.baselineColumns("lname").baselineValues("iuMG")
.baselineColumns("lname").baselineValues("KpFq")
.baselineColumns("lname").baselineValues("bkkAvz")
.go();
}
@Ignore // in statsCache, condition state+city has rowcount 1250, but state only has 1000. so it is picking i_state_age_phone
@Test // non-covering, order by non leading field, and leading fields are not in equality condition, Sort SHOULD NOT be removed
public void NonCoveringPlan_SortPrefix_1() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.address.state > 'pc' AND t.address.city>'pfrrr' AND t.address.city<'pfrrt' order by t.adddress.city";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {"Sort",
"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_state_city"},
new String[]{}
);
}
@Test // non-covering, order by non leading field, and leading fields are in equality condition, Sort SHOULD be removed
public void NonCoveringPlan_SortPrefix_2() throws Exception {
String query = "SELECT t.`id`.`ssn` AS `ssn` FROM hbase.`index_test_primary` as t " +
" where t.address.state = 'pc' AND t.address.city>'pfrrr' AND t.address.city<'pfrrt' order by t.address.city";
test(defaultHavingIndexPlan);
PlanTestBase.testPlanMatchingPatterns(query,
new String[] {
"RowKeyJoin(.*[\n\r])+.*RestrictedJsonTableGroupScan(.*[\n\r])+.*JsonTableGroupScan.*indexName=i_state_city"},