forked from MahletNigusse/write-after-read-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt_range-t.cc
2066 lines (1741 loc) · 63.3 KB
/
opt_range-t.cc
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
/* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include <gtest/gtest.h>
#include "mock_field_long.h"
#include <vector>
#include <sstream>
#include <string>
#include "handler-t.h"
#include "fake_table.h"
#include "fake_range_opt_param.h"
#include "test_utils.h"
#include "opt_range.cc"
#include "parse_tree_helpers.h"
namespace opt_range_unittest {
using std::vector;
using std::string;
/**
Helper class to print which line a failing test was called from.
*/
class TestFailLinePrinter
{
public:
explicit TestFailLinePrinter(int line) : m_line(line) {}
int m_line;
};
std::ostream &operator<< (std::ostream &s, const TestFailLinePrinter &v)
{
return s << "called from line " << v.m_line;
}
/**
Keep in mind the following boolean algebra definitions and rules
when reading the tests in this file:
Operators:
& (and)
| (or)
! (negation)
DeMorgans laws:
DM1: !(X & Y) <==> !X | !Y
DM2: !(X | Y) <==> !X & !Y
Boolean axioms:
A1 (associativity): X & (Y & Z) <==> (X & Y) & Z
X | (Y | Z) <==> (X | Y) | Z
A2 (commutativity): X & Y <==> Y & X
X | Y <==> Y | X
A3 (identity): X | false <==> X
X | true <==> true
X & false <==> false
X & true <==> X
A4 (distributivity): X | (Y & Z) <==> (X | Y) & (X | Z)
X & (Y | Z) <==> (X & Y) | (X & Z)
A5 (complements): X | !X <==> true
X & !X <==> false
A6 (idempotence of |): X | X <==> X
A7 (idempotence of &): X & X <==> X
Also note that the range optimizer follows a relaxed boolean algebra
where the result may be bigger than boolean algebra rules dictate.
@See get_mm_tree() for explanation.
*/
using my_testing::Server_initializer;
class OptRangeTest : public ::testing::Test
{
protected:
OptRangeTest() : m_opt_param(NULL) {}
virtual void SetUp()
{
initializer.SetUp();
init_sql_alloc(PSI_NOT_INSTRUMENTED,
&m_alloc, thd()->variables.range_alloc_block_size, 0);
}
virtual void TearDown()
{
delete m_opt_param;
initializer.TearDown();
free_root(&m_alloc, MYF(0));
}
THD *thd() { return initializer.thd(); }
/**
Create a table with the requested number of fields. All fields are
indexed.
@param nbr_fields The number of fields in the table
*/
void create_table_singlecol_idx(int nbr_fields)
{
create_table(nbr_fields);
for (int i= 0; i < nbr_fields; i++)
m_opt_param->add_key(m_opt_param->table->field[i]);
m_field= m_opt_param->table->field;
}
/**
Create a table with the requested number of fields without
creating indexes.
@param nbr_fields The number of fields in the table
*/
void create_table(int nbr_fields, bool columns_nullable)
{
m_opt_param=
new Fake_RANGE_OPT_PARAM(thd(), &m_alloc, nbr_fields, columns_nullable);
m_field= m_opt_param->table->field;
}
void create_table(int nbr_fields) { create_table(nbr_fields, false); }
/*
The new_item_xxx are convenience functions for creating Item_func
descendents from Field's and ints without having to wrap them in
Item's and resolving them.
*/
template<typename T> T *new_item(Field *field, int value)
{
T *item= new T(new Item_field(field), new Item_int(value));
Item *item_base= item;
item->fix_fields(thd(), &item_base);
return item;
}
Item_func_lt *new_item_lt(Field *field, int value)
{
return new_item<Item_func_lt>(field, value);
}
Item_func_gt *new_item_gt(Field *field, int value)
{
return new_item<Item_func_gt>(field, value);
}
Item_func_equal *new_item_equal(Field *field, int value)
{
return new_item<Item_func_equal>(field, value);
}
Item_func_xor *new_item_xor(Field *field, int value)
{
return new_item<Item_func_xor>(field, value);
}
Item_cond_and *new_item_between(Field *fld, int val1, int val2)
{
return new Item_cond_and(new_item_gt(fld, val1), new_item_lt(fld, val2));
}
Item_cond_or *new_item_ne(Field *fld, int val1)
{
return new Item_cond_or(new_item_lt(fld, val1), new_item_gt(fld, val1));
}
/**
Utility funtion used to simplify creation of SEL_TREEs with
specified range predicate operators and values. Also verifies that
the created SEL_TREE has the expected range conditions.
@param type The type of range predicate operator requested
@param fld The field used in the range predicate
@param val1 The first value used in the range predicate
@param val2 The second value used in the range predicate.
Only used for range predicates that takes two
values (BETWEEN).
@param expected_result The range conditions the created SEL_TREE
is expected to consist of. The format of this
string is what opt_range.cc print_tree() produces.
@return SEL_TREE that has been verified to have expected range conditions.
*/
// Undefined at end of this file
#define create_tree(i, er) \
do_create_tree(i, er, TestFailLinePrinter(__LINE__))
SEL_TREE *do_create_tree(Item *item, const char* expected_result,
TestFailLinePrinter called_from_line)
{
SEL_TREE *result= get_mm_tree(m_opt_param, item);
SCOPED_TRACE(called_from_line);
check_tree_result(result, SEL_TREE::KEY, expected_result);
return result;
}
/**
Utility funtion used to simplify creation of func items used as
range predicates.
@param type The type of range predicate operator requested
@param fld The field used in the range predicate
@param val1 The value used in the range predicate
@return Item for the specified range predicate
*/
Item_func *create_item(Item_func::Functype type, Field *fld, int value);
/**
Create instance of Xor Item_func.
@param item1 first item for xor condition.
@param item2 second item for xor condition.
@return pointer to newly created instance of Xor Item.
*/
Item_func_xor *create_xor_item(Item *item1, Item *item2);
/**
Check that the use_count of all SEL_ARGs in the SEL_TREE are
correct.
@param tree The SEL_TREE to check
*/
void check_use_count(SEL_TREE *tree);
/**
Verify that a SEL_TREE has the type and conditions we expect it to
have.
@param tree The SEL_TREE to check
@param expected_type The type 'tree' is expected to have
@param expected_result The range conditions 'tree' is expected
to consist of. The format of this string
is what opt_range.cc print_tree() produces.
*/
void check_tree_result(SEL_TREE *tree,
const SEL_TREE::Type expected_type,
const char* expected_result);
/**
Perform OR between two SEL_TREEs and verify that the result of the
OR operation is as expected.
@param tree1 First SEL_TREE that will be ORed
@param tree2 Second SEL_TREE that will be ORed
@param expected_type The type the ORed result is expected to have
@param expected_result The range conditions the ORed result is expected
to consist of. The format of this string
is what opt_range.cc print_tree() produces.
@return SEL_TREE result of the OR operation between tree1 and
tree2 that has been verified to have expected range
conditions.
*/
// Undefined at end of this file
#define create_and_check_tree_or(t1, t2, et, er) \
do_create_and_check_tree_or(t1, t2, et, er, TestFailLinePrinter(__LINE__))
SEL_TREE *do_create_and_check_tree_or(SEL_TREE *tree1, SEL_TREE *tree2,
const SEL_TREE::Type expected_type,
const char* expected_result,
TestFailLinePrinter called_from_line);
/**
Perform AND between two SEL_TREEs and verify that the result of the
AND operation is as expected.
@param tree1 First SEL_TREE that will be ANDed
@param tree2 Second SEL_TREE that will be ANDed
@param expected_type The type the ANDed result is expected to have
@param expected_result The range conditions the ANDed result is expected
to consist of. The format of this string
is what opt_range.cc print_tree() produces.
@return SEL_TREE result of the AND operation between tree1 and
tree2 that has been verified to have expected range
conditions.
*/
// Undefined at end of this file
#define create_and_check_tree_and(t1, t2, et, er) \
do_create_and_check_tree_and(t1, t2, et, er, TestFailLinePrinter(__LINE__))
SEL_TREE *do_create_and_check_tree_and(SEL_TREE *tree1, SEL_TREE *tree2,
const SEL_TREE::Type expected_type,
const char* expected_result,
TestFailLinePrinter called_from_line);
Server_initializer initializer;
MEM_ROOT m_alloc;
Fake_RANGE_OPT_PARAM *m_opt_param;
/*
Pointer to m_opt_param->table->field. Only valid if the table was
created by calling one of OptRangeTest::create_table*()
*/
Field **m_field;
};
Item_func* OptRangeTest::create_item(Item_func::Functype type,
Field *fld, int value)
{
Item_func *result;
switch (type)
{
case Item_func::GT_FUNC:
result= new Item_func_gt(new Item_field(fld), new Item_int(value));
break;
case Item_func::LT_FUNC:
result= new Item_func_lt(new Item_field(fld), new Item_int(value));
break;
case Item_func::MULT_EQUAL_FUNC:
result= new Item_equal(new Item_int(value), new Item_field(fld));
break;
case Item_func::XOR_FUNC:
result= new Item_func_xor(new Item_field(fld), new Item_int(value));
break;
default:
result= NULL;
DBUG_ASSERT(false);
return result;
}
Item *itm= static_cast<Item*>(result);
result->fix_fields(thd(), &itm);
return result;
}
Item_func_xor*
OptRangeTest::create_xor_item(Item *item1, Item *item2)
{
Item_func_xor *xor_item= new Item_func_xor(item1, item2);
Item *itm= static_cast<Item*>(xor_item);
xor_item->fix_fields(thd(), &itm);
return xor_item;
}
void OptRangeTest::check_use_count(SEL_TREE *tree)
{
for (uint i= 0; i < m_opt_param->keys; i++)
{
SEL_ARG *cur_range= tree->keys[i];
if (cur_range != NULL)
{
EXPECT_FALSE(cur_range->test_use_count(cur_range));
}
}
if (!tree->merges.is_empty())
{
List_iterator<SEL_IMERGE> it(tree->merges);
SEL_IMERGE *merge= it++;
for (SEL_TREE** current= merge->trees;
current != merge->trees_next;
current++)
check_use_count(*current);
}
}
void OptRangeTest::check_tree_result(SEL_TREE *tree,
const SEL_TREE::Type expected_type,
const char* expected_result)
{
EXPECT_EQ(expected_type, tree->type);
if (expected_type != SEL_TREE::KEY)
return;
char buff[512];
String actual_result(buff, sizeof(buff), system_charset_info);
actual_result.set_charset(system_charset_info);
actual_result.length(0);
print_tree(&actual_result, "result", tree, m_opt_param, false);
EXPECT_STREQ(expected_result, actual_result.c_ptr());
SCOPED_TRACE("check_use_count");
check_use_count(tree);
}
SEL_TREE *
OptRangeTest::do_create_and_check_tree_or(SEL_TREE *tree1, SEL_TREE *tree2,
const SEL_TREE::Type expected_type,
const char* expected_result,
TestFailLinePrinter called_from_line)
{
{
// Check that tree use counts are OK before OR'ing
SCOPED_TRACE(called_from_line);
check_use_count(tree1);
check_use_count(tree2);
}
SEL_TREE *result= tree_or(m_opt_param, tree1, tree2);
// Tree returned from tree_or()
SCOPED_TRACE(called_from_line);
check_tree_result(result, expected_type, expected_result);
return result;
}
SEL_TREE *
OptRangeTest::do_create_and_check_tree_and(SEL_TREE *tree1, SEL_TREE *tree2,
const SEL_TREE::Type expected_type,
const char* expected_result,
TestFailLinePrinter called_from_line)
{
{
// Check that tree use counts are OK before AND'ing
SCOPED_TRACE(called_from_line);
check_use_count(tree1);
check_use_count(tree2);
}
SEL_TREE *result= tree_and(m_opt_param, tree1, tree2);
// Tree returned from tree_and()
SCOPED_TRACE(called_from_line);
check_tree_result(result, expected_type, expected_result);
return result;
}
/*
Experiment with these to measure performance of
'new (thd->mem_root)' Foo vs. 'new Foo'.
With gcc 4.4.2 I see ~4% difference (in optimized mode).
*/
const int num_iterations= 10;
const int num_allocs= 10;
TEST_F(OptRangeTest, AllocateExplicit)
{
for (int ix= 0; ix < num_iterations; ++ix)
{
free_root(thd()->mem_root, MYF(MY_KEEP_PREALLOC));
for (int ii= 0; ii < num_allocs; ++ii)
new (thd()->mem_root) SEL_ARG;
}
}
TEST_F(OptRangeTest, AllocateImplicit)
{
for (int ix= 0; ix < num_iterations; ++ix)
{
free_root(thd()->mem_root, MYF(MY_KEEP_PREALLOC));
for (int ii= 0; ii < num_allocs; ++ii)
new SEL_ARG;
}
}
/*
We cannot do EXPECT_NE(NULL, get_mm_tree(...))
because of limits in google test.
*/
const SEL_TREE *null_tree= NULL;
const SEL_ARG *null_arg= NULL;
static void print_selarg_ranges(String *s, SEL_ARG *sel_arg,
const KEY_PART_INFO *kpi)
{
for (SEL_ARG *cur= sel_arg->first();
cur != &null_element;
cur= cur->right)
{
String current_range;
append_range(¤t_range, kpi, cur->min_value, cur->max_value,
cur->min_flag | cur->max_flag);
if (s->length() > 0)
s->append(STRING_WITH_LEN("\n"));
s->append(current_range);
}
}
TEST_F(OptRangeTest, SimpleCond)
{
Fake_RANGE_OPT_PARAM opt_param(thd(), &m_alloc, 0, false);
EXPECT_NE(null_tree, get_mm_tree(&opt_param, new Item_int(42)));
}
/*
Exercise range optimizer without adding indexes
*/
TEST_F(OptRangeTest, EqualCondNoIndexes)
{
Fake_RANGE_OPT_PARAM opt_param(thd(), &m_alloc, 1, false);
SEL_TREE *tree=
get_mm_tree(&opt_param, new_item_equal(opt_param.table->field[0], 42));
EXPECT_EQ(null_tree, tree);
}
/*
Exercise range optimizer with xor operator.
*/
TEST_F(OptRangeTest, XorCondIndexes)
{
create_table(1);
m_opt_param->add_key(m_field[0]);
/*
XOR is not range optimizible ATM and is treated as
always true. No SEL_TREE is therefore expected.
*/
SEL_TREE *tree= get_mm_tree(m_opt_param, new_item_xor(m_field[0], 42));
EXPECT_EQ(null_tree, tree);
}
/*
Exercise range optimizer with xor and different type of operator.
*/
TEST_F(OptRangeTest, XorCondWithIndexes)
{
create_table(5);
m_opt_param->add_key(m_field[0]);
m_opt_param->add_key(m_field[1]);
m_opt_param->add_key(m_field[2]);
m_opt_param->add_key(m_field[3]);
m_opt_param->add_key(m_field[4]);
/*
Create SEL_TREE from "field1=7 AND (field1 XOR 42)". Since XOR is
not range optimizible (treated as always true), we get a tree for
"field1=7" only.
*/
const char expected1[]= "result keys[0]: (7 <= field_1 <= 7)\n";
SEL_TREE *tree=
get_mm_tree(m_opt_param,
new Item_cond_and (new_item_xor(m_field[0], 42),
new_item_equal(m_field[0],7)));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected1);
/*
Create SEL_TREE from "(field1 XOR 0) AND (field1>14)". Since XOR
is not range optimizible (treated as always true), we get a tree
for "field1>14" only.
*/
const char expected2[]= "result keys[0]: (14 < field_1)\n";
tree= get_mm_tree(m_opt_param,
new Item_cond_and (new_item_xor(m_field[0], 0),
new_item_gt(m_field[0], 14)));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected2);
/*
Create SEL_TREE from "(field1<0 AND field1>14) XOR
(field1>17)". Since XOR is not range optimizible (treated as
always true), we get a NULL tree.
*/
tree= get_mm_tree(m_opt_param,
create_xor_item(new Item_cond_and
(new_item_lt(m_field[0], 0),
new_item_gt(m_field[0], 14)),
new_item_gt(m_field[0], 17)));
SCOPED_TRACE("");
EXPECT_EQ(null_tree, tree);
/*
Create SEL_TREE from
(field1<0 AND field2>14) AND
((field3<0 and field4>14) XOR field5>17) ".
Since XOR is not range optimizible (treated as always true),
we get a tree for "field1<0 AND field2>14" only.
*/
const char expected3[]=
"result keys[0]: (field_1 < 0)\n"
"result keys[1]: (14 < field_2)\n";
tree= get_mm_tree(m_opt_param,
new Item_cond_and(
new Item_cond_and
(new_item_lt(m_field[0], 0),
new_item_gt(m_field[1], 14)),
create_xor_item(
new Item_cond_and
(new_item_lt(m_field[2], 0),
new_item_gt(m_field[3], 14)),
new_item_gt(m_field[4], 17))));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected3);
}
/*
Exercise range optimizer with single column index
*/
TEST_F(OptRangeTest, GetMMTreeSingleColIndex)
{
// Create a single-column table with index
create_table_singlecol_idx(1);
// Expected result of next test:
const char expected[]= "result keys[0]: (42 <= field_1 <= 42)\n";
create_tree(new_item_equal(m_field[0], 42), expected);
// Expected result of next test:
const char expected2[]=
"result keys[0]: (42 <= field_1 <= 42) OR (43 <= field_1 <= 43)\n";
SEL_TREE *tree=
get_mm_tree(m_opt_param, new Item_cond_or(new_item_equal(m_field[0], 42),
new_item_equal(m_field[0], 43)));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected2);
// Expected result of next test:
const char expected3[]=
"result keys[0]: "
"(1 <= field_1 <= 1) OR (2 <= field_1 <= 2) OR "
"(3 <= field_1 <= 3) OR (4 <= field_1 <= 4) OR "
"(5 <= field_1 <= 5) OR (6 <= field_1 <= 6) OR "
"(7 <= field_1 <= 7) OR (8 <= field_1 <= 8)\n";
List<Item> or_list1;
or_list1.push_back(new_item_equal(m_field[0], 1));
or_list1.push_back(new_item_equal(m_field[0], 2));
or_list1.push_back(new_item_equal(m_field[0], 3));
or_list1.push_back(new_item_equal(m_field[0], 4));
or_list1.push_back(new_item_equal(m_field[0], 5));
or_list1.push_back(new_item_equal(m_field[0], 6));
or_list1.push_back(new_item_equal(m_field[0], 7));
or_list1.push_back(new_item_equal(m_field[0], 8));
tree= get_mm_tree(m_opt_param, new Item_cond_or(or_list1));
check_tree_result(tree, SEL_TREE::KEY, expected3);
// Expected result of next test:
const char expected4[]= "result keys[0]: (7 <= field_1 <= 7)\n";
tree= get_mm_tree(m_opt_param,
new Item_cond_and (new Item_cond_or(or_list1),
new_item_equal(m_field[0], 7)));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected4);
// Expected result of next test:
const char expected5[]=
"result keys[0]: "
"(1 <= field_1 <= 1) OR (3 <= field_1 <= 3) OR "
"(5 <= field_1 <= 5) OR (7 <= field_1 <= 7)\n";
List<Item> or_list2;
or_list2.push_back(new_item_equal(m_field[0], 1));
or_list2.push_back(new_item_equal(m_field[0], 3));
or_list2.push_back(new_item_equal(m_field[0], 5));
or_list2.push_back(new_item_equal(m_field[0], 7));
or_list2.push_back(new_item_equal(m_field[0], 9));
tree= get_mm_tree(m_opt_param,
new Item_cond_and(new Item_cond_or(or_list1),
new Item_cond_or(or_list2)));
SCOPED_TRACE("");
check_tree_result(tree, SEL_TREE::KEY, expected5);
}
/*
Exercise range optimizer with multiple column index
*/
TEST_F(OptRangeTest, GetMMTreeMultipleSingleColIndex)
{
// Create a single-column table without index
create_table(1);
// Add two indexes covering the same field
m_opt_param->add_key(m_field[0]);
m_opt_param->add_key(m_field[0]);
char buff[512];
String range_string(buff, sizeof(buff), system_charset_info);
range_string.set_charset(system_charset_info);
// Expected result of next test:
const char expected[]=
"result keys[0]: (42 <= field_1 <= 42)\n"
"result keys[1]: (42 <= field_1 <= 42)\n";
create_tree(new_item_equal(m_field[0], 42), expected);
}
/*
Exercise range optimizer with multiple single column indexes
*/
TEST_F(OptRangeTest, GetMMTreeOneTwoColIndex)
{
create_table(2);
m_opt_param->add_key(m_field[0], m_field[1]);
char buff[512];
String range_string(buff, sizeof(buff), system_charset_info);
range_string.set_charset(system_charset_info);
// Expected result of next test:
const char expected[]= "result keys[0]: (42 <= field_1 <= 42)\n";
create_tree(new_item_equal(m_field[0], 42), expected);
// Expected result of next test:
const char expected2[]=
"result keys[0]: (42 <= field_1 <= 42 AND 10 <= field_2 <= 10)\n";
SEL_TREE *tree=
get_mm_tree(m_opt_param,
new Item_cond_and(new_item_equal(m_field[0], 42),
new_item_equal(m_field[1], 10)));
range_string.length(0);
print_tree(&range_string, "result",tree , m_opt_param, false);
EXPECT_STREQ(expected2, range_string.c_ptr());
}
/*
Optimizer tracing should only print ranges for applicable keyparts,
except when argument for print_tree() parameter 'print_full' is true.
*/
TEST_F(OptRangeTest, GetMMTreeNonApplicableKeypart)
{
create_table(3);
List<Field> index_list;
index_list.push_back(m_field[0]);
index_list.push_back(m_field[1]);
index_list.push_back(m_field[2]);
m_opt_param->add_key(index_list);
char buff[512];
String range_string(buff, sizeof(buff), system_charset_info);
range_string.set_charset(system_charset_info);
/*
Expected result is range only on first keypart. Third keypart is
not applicable because there are no predicates on the second
keypart.
*/
const char expected1[]=
"result keys[0]: (42 <= field_1 <= 42)\n";
SEL_TREE *tree=
get_mm_tree(m_opt_param,
new Item_cond_and (new_item_equal(m_field[0], 42),
new_item_equal(m_field[2], 10)));
range_string.length(0);
print_tree(&range_string, "result", tree , m_opt_param, false);
EXPECT_STREQ(expected1, range_string.c_ptr());
/*
Same SEL_ARG tree, but print_full argument is now true.
Non-applicable key parts are also printed in this case.
*/
const char expected1_printfull[]=
"result keys[0]: (42 <= field_1 <= 42 AND 10 <= field_3 <= 10)\n";
range_string.length(0);
print_tree(&range_string, "result", tree , m_opt_param, true);
EXPECT_STREQ(expected1_printfull, range_string.c_ptr());
/*
Expected result is range only on first keypart. Second keypart is
not applicable because the predicate on the first keypart does not
use an equality operator.
*/
const char expected2[]=
"result keys[0]: (field_1 < 42)\n";
tree=
get_mm_tree(m_opt_param,
new Item_cond_and
(new_item_lt(m_field[0], 42),
new_item_equal(m_field[1], 10)));
range_string.length(0);
print_tree(&range_string, "result", tree , m_opt_param, false);
EXPECT_STREQ(expected2, range_string.c_ptr());
/*
Same SEL_ARG tree, but print_full argument is now true.
Non-applicable key parts are also printed in this case.
*/
const char expected2_printfull[]=
"result keys[0]: (field_1 < 42 AND 10 <= field_2 <= 10)\n";
range_string.length(0);
print_tree(&range_string, "result", tree , m_opt_param, true);
EXPECT_STREQ(expected2_printfull, range_string.c_ptr());
}
/*
Exercise range optimizer with three single column indexes
*/
TEST_F(OptRangeTest, treeAndSingleColIndex1)
{
create_table_singlecol_idx(3);
// Expected outputs
// Single-field range predicates
const char expected_fld1[]= "result keys[0]: (10 < field_1 < 13)\n";
const char expected_fld2_1[]= "result keys[1]: (field_2 < 11)\n";
const char expected_fld2_2[]= "result keys[1]: (8 < field_2)\n";
const char expected_fld3[]= "result keys[2]: (20 < field_3 < 30)\n";
/*
Expected result when performing AND of:
"(field_1 BETWEEN 10 AND 13) & (field_2 < 11)"
*/
const char expected_and1[]=
"result keys[0]: (10 < field_1 < 13)\n"
"result keys[1]: (field_2 < 11)\n";
/*
Expected result when performing AND of:
"((field_1 BETWEEN 10 AND 13) & (field_2 < 11))
&
(field_3 BETWEEN 20 AND 30)"
*/
const char expected_and2[]=
"result keys[0]: (10 < field_1 < 13)\n"
"result keys[1]: (field_2 < 11)\n"
"result keys[2]: (20 < field_3 < 30)\n";
/*
Expected result when performing AND of:
"((field_1 BETWEEN 10 AND 13) &
(field_2 < 11) &
(field_3 BETWEEN 20 AND 30)
)
&
field_2 > 8"
*/
const char expected_and3[]=
"result keys[0]: (10 < field_1 < 13)\n"
"result keys[1]: (8 < field_2 < 11)\n" // <- notice lower bound
"result keys[2]: (20 < field_3 < 30)\n";
SEL_TREE *tree_and=
create_and_check_tree_and
(create_and_check_tree_and
(create_tree(new_item_between(m_field[0], 10, 13), expected_fld1),
create_tree(new_item_lt(m_field[1], 11), expected_fld2_1),
SEL_TREE::KEY, expected_and1),
create_tree(new_item_between(m_field[2], 20, 30), expected_fld3),
SEL_TREE::KEY, expected_and2
);
/*
Testing Axiom 7: AND'ing a predicate already part of a SEL_TREE
has no effect.
*/
create_and_check_tree_and(
tree_and,
create_tree(new_item_between(m_field[2], 20, 30), expected_fld3),
SEL_TREE::KEY, expected_and2 // conditions did not change
);
create_and_check_tree_and(tree_and,
create_tree(new_item_gt(m_field[1], 8),
expected_fld2_2),
SEL_TREE::KEY, expected_and3
);
}
/*
Exercise range optimizer with three single column indexes
*/
TEST_F(OptRangeTest, treeOrSingleColIndex1)
{
create_table_singlecol_idx(3);
// Expected outputs
// Single-field range predicates
const char expected_fld1[]= "result keys[0]: (10 < field_1 < 13)\n";
const char expected_fld2_1[]= "result keys[1]: (field_2 < 11)\n";
const char expected_fld2_2[]= "result keys[1]: (8 < field_2)\n";
const char expected_fld3[]= "result keys[2]: (20 < field_3 < 30)\n";
/*
Expected result when performing OR of:
"(field_1 Item_func::BETWEEN 10 AND 13) | (field_2 < 11)"
*/
const char expected_or1[]=
"result contains the following merges\n"
"--- alternative 1 ---\n"
" merge_tree keys[0]: (10 < field_1 < 13)\n"
" merge_tree keys[1]: (field_2 < 11)\n";
/*
Expected result when performing OR of:
"((field_1 BETWEEN 10 AND 13) | (field_2 < 11))
|
(field_3 BETWEEN 20 AND 30)"
*/
const char expected_or2[]=
"result contains the following merges\n"
"--- alternative 1 ---\n"
" merge_tree keys[0]: (10 < field_1 < 13)\n"
" merge_tree keys[1]: (field_2 < 11)\n"
" merge_tree keys[2]: (20 < field_3 < 30)\n";
SEL_TREE *tree_or2=
create_and_check_tree_or(
create_and_check_tree_or(
create_tree(new_item_between(m_field[0], 10, 13), expected_fld1),
create_tree(new_item_lt(m_field[1], 11), expected_fld2_1),
SEL_TREE::KEY, expected_or1),
create_tree(new_item_between(m_field[2], 20, 30), expected_fld3),
SEL_TREE::KEY, expected_or2
);
/*
Testing Axiom 6: OR'ing a predicate already part of a SEL_TREE
has no effect.
*/
SEL_TREE *tree_or3=
create_and_check_tree_or(
tree_or2,
create_tree(new_item_between(m_field[2], 20, 30), expected_fld3),
SEL_TREE::KEY, expected_or2
);
/*
Perform OR of:
"((field_1 BETWEEN 10 AND 13) |
(field_2 < 11) |
(field_3 BETWEEN 20 AND 30)
) |
(field_2 > 8)"
This is always TRUE due to
(field_2 < 11) | (field_2 > 8) <==> true
*/
create_and_check_tree_or(
tree_or3,
create_tree(new_item_gt(m_field[1], 8), expected_fld2_2),
SEL_TREE::ALWAYS, ""
);
}
/*
Exercise range optimizer with three single column indexes
*/
TEST_F(OptRangeTest, treeAndOrComboSingleColIndex1)
{
create_table_singlecol_idx(3);
// Expected outputs
// Single-field range predicates
const char exected_fld1[]= "result keys[0]: (10 < field_1 < 13)\n";
const char exected_fld2[]= "result keys[1]: (field_2 < 11)\n";
const char exected_fld3[]= "result keys[2]: (20 < field_3 < 30)\n";
// What "exected_fld1 & exected_fld2" should produce
const char expected_and[]=
"result keys[0]: (10 < field_1 < 13)\n"
"result keys[1]: (field_2 < 11)\n";
/*
What "(exected_fld1 & exected_fld2) | exected_fld3" should
produce.
By Axiom 4 (see above), we have that
X | (Y & Z) <==> (X | Y) & (X | Z)
Thus:
((field_1 BETWEEN 10 AND 13) & field_2 < 11) |
(field_3 BETWEEN 20 AND 30)
<==> (Axiom 4)
(field_1 BETWEEN ... | field_3 BETWEEN ...) &
(field_2 < ... | field_3 BETWEEN ...)
But the result above is not created. Instead the following, which
is incorrect (reads more rows than necessary), is the result:
(field_1 BETWEEN ... | field_2 < 11 | field_3 BETWEEN ...)
*/
const char expected_incorrect_or[]=
"result contains the following merges\n"