Debian Codegraph @

unittest/gunit/item_filter-t.cc

raw symbols SCIP
1 /* Copyright (c) 2014, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <assert.h>
25 #include <gtest/gtest.h>
26 #include <stddef.h>
27
28 #include <sstream>
29 #include <string>
30 #include <vector>
31
32 #include "my_inttypes.h"
33 #include "my_table_map.h"
34 #include "sql/item_cmpfunc.h"
35 #include "sql/parse_tree_helpers.h"
36 #include "sql/sql_class.h"
37 #include "sql/sql_lex.h"
38 #include "unittest/gunit/fake_table.h"
39 #include "unittest/gunit/test_utils.h"
40
41 namespace item_filter_unittest {
42
43 using my_testing::Server_initializer;
44
45 /**
46 Helper class to print which line a failing test was called from.
47 */
48 class TestFailLinePrinter {
49 public:
50 explicit TestFailLinePrinter(int line) : m_line(line) {}
51 int m_line;
52 };
53 std::ostream &operator<<(std::ostream &s, const TestFailLinePrinter &v) {
54 return s << "called from line " << v.m_line;
55 }
56
57 class ItemFilterTest : public ::testing::Test {
58 protected:
59 ItemFilterTest() : rows_in_table(200) {}
60
61 void SetUp() override {
62 initializer.SetUp();
63 init_sql_alloc(PSI_NOT_INSTRUMENTED, &m_alloc,
64 thd()->variables.range_alloc_block_size);
65 }
66
67 void TearDown() override {
68 delete m_table;
69
70 initializer.TearDown();
71 }
72
73 THD *thd() { return initializer.thd(); }
74
75 /**
76 Create a table with the requested number of columns without
77 creating indexes.
78
79 @param nbr_columns The number of columns in the table
80 @param columns_nullable Whether or not columns in the table can be NULL
81 */
82 void create_table(int nbr_columns, bool columns_nullable) {
83 m_table = new Fake_TABLE(nbr_columns, columns_nullable);
84 m_field = m_table->field;
85 m_table_list = m_table->pos_in_table_list;
86 }
87
88 /**
89 Create a table with the requested number of non-nullable columns
90 without creating indexes.
91
92 @param nbr_columns The number of columns in the table
93 */
94 void create_table(int nbr_columns) { create_table(nbr_columns, false); }
95
96 /**
97 Utility funtion used to simplify creation of func items used as
98 range predicates.
99
100 @param type The type of range predicate operator requested
101 @param fld The field used in the range predicate
102 @param val1 Value used in the range predicate (if the predicate
103 takes two or more arguments)
104 @param val2 Value used in the range predicate (if the predicate
105 takes three or more arguments)
106
107 @return Item for the specified range predicate
108 */
109 Item_func *create_item(Item_func::Functype type, Field *fld, int val1,
110 int val2);
111
112 // Undefined at end of this file
113 #define create_item_check_filter(er, op, f1, v1, v2, ut, fti) \
114 do_create_item_check_filter(er, op, f1, v1, v2, ut, fti, \
115 TestFailLinePrinter(__LINE__))
116 Item *do_create_item_check_filter(const float expected_result,
117 const Item_func::Functype type, Field *fld,
118 const int val1, const int val2,
119 const table_map used_tables,
120 MY_BITMAP *fields_to_ignore,
121 TestFailLinePrinter called_from_line) {
122 SCOPED_TRACE(called_from_line);
123 Item *item = create_item(type, fld, val1, val2);
124
125 const float filter =
126 item->get_filtering_effect(thd(), m_table_list->map(), used_tables,
127 fields_to_ignore, rows_in_table);
128 EXPECT_FLOAT_EQ(expected_result, filter);
129 return item;
130 }
131
132 #define create_anditem_check_filter(er, lst, ut, fti) \
133 do_create_anditem_check_filter(er, lst, ut, fti, \
134 TestFailLinePrinter(__LINE__))
135 Item_cond_and *do_create_anditem_check_filter(
136 const float expected_result, List<Item> &lst, const table_map used_tables,
137 MY_BITMAP *fields_to_ignore, TestFailLinePrinter called_from_line) {
138 SCOPED_TRACE(called_from_line);
139
140 Item_cond_and *and_item = new Item_cond_and(lst);
141
142 Item *itm = static_cast<Item *>(and_item);
143 and_item->fix_fields(thd(), &itm);
144
145 const float filter =
146 and_item->get_filtering_effect(thd(), m_table_list->map(), used_tables,
147 fields_to_ignore, rows_in_table);
148 EXPECT_FLOAT_EQ(expected_result, filter);
149
150 return and_item;
151 }
152
153 #define create_oritem_check_filter(er, lst, ut, fti) \
154 do_create_oritem_check_filter(er, lst, ut, fti, TestFailLinePrinter(__LINE__))
155 Item_cond_or *do_create_oritem_check_filter(
156 const float expected_result, List<Item> &lst, const table_map used_tables,
157 MY_BITMAP *fields_to_ignore, TestFailLinePrinter called_from_line) {
158 SCOPED_TRACE(called_from_line);
159
160 Item_cond_or *or_item = new Item_cond_or(lst);
161
162 Item *itm = static_cast<Item *>(or_item);
163 or_item->fix_fields(thd(), &itm);
164
165 const float filter =
166 or_item->get_filtering_effect(thd(), m_table_list->map(), used_tables,
167 fields_to_ignore, rows_in_table);
168 EXPECT_FLOAT_EQ(expected_result, filter);
169
170 return or_item;
171 }
172
173 #define create_initem_check_filter(er, lst, ut, fti) \
174 do_create_initem_check_filter(er, lst, ut, fti, TestFailLinePrinter(__LINE__))
175 Item_func_in *do_create_initem_check_filter(
176 const float expected_result, const mem_root_deque<Item *> &lst,
177 const table_map used_tables, MY_BITMAP *fields_to_ignore,
178 TestFailLinePrinter called_from_line) {
179 SCOPED_TRACE(called_from_line);
180
181 PT_item_list *list = new (thd()->mem_root) PT_item_list;
182 list->value = lst;
183 Item_func_in *in_item = new Item_func_in(POS(), list, false);
184 Parse_context pc(thd(), thd()->lex->current_query_block());
185 EXPECT_FALSE(in_item->itemize(&pc, (Item **)&in_item));
186
187 Item *itm = static_cast<Item *>(in_item);
188 in_item->fix_fields(thd(), &itm);
189
190 const float filter =
191 in_item->get_filtering_effect(thd(), m_table_list->map(), used_tables,
192 fields_to_ignore, rows_in_table);
193 EXPECT_FLOAT_EQ(expected_result, filter);
194
195 return in_item;
196 }
197
198 /*
199 Many filtering estimates depends on the number of rows in the
200 table. For example, P("col = <const>") = max(0.005, 1/rows_in_table)
201
202 This variable determines the number of rows in the table.
203 */
204 double rows_in_table;
205 Server_initializer initializer;
206 MEM_ROOT m_alloc;
207
208 Fake_TABLE *m_table;
209 Table_ref *m_table_list;
210 /*
211 Pointer to m_table->field. Only valid if the table was
212 created by calling one of ItemFilterTest::create_table*()
213 */
214 Field **m_field;
215 };
216
217 Item_func *ItemFilterTest::create_item(Item_func::Functype type, Field *fld,
218 int val1, int val2) {
219 Item_func *result;
220 switch (type) {
221 case Item_func::GT_FUNC:
222 result = new Item_func_gt(new Item_field(fld), new Item_int(val1));
223 break;
224 case Item_func::GE_FUNC:
225 result = new Item_func_ge(new Item_field(fld), new Item_int(val1));
226 break;
227 case Item_func::LT_FUNC:
228 result = new Item_func_lt(new Item_field(fld), new Item_int(val1));
229 break;
230 case Item_func::LE_FUNC:
231 result = new Item_func_le(new Item_field(fld), new Item_int(val1));
232 break;
233 case Item_func::MULT_EQUAL_FUNC:
234 result = new Item_equal(new Item_int(val1), new Item_field(fld));
235 break;
236 case Item_func::XOR_FUNC:
237 result = new Item_func_xor(new Item_field(fld), new Item_int(val1));
238 break;
239 case Item_func::ISNULL_FUNC:
240 result = new Item_func_isnull(new Item_field(fld));
241 break;
242 case Item_func::ISNOTNULL_FUNC:
243 result = new Item_func_isnotnull(new Item_field(fld));
244 break;
245 case Item_func::BETWEEN: {
246 Parse_context pc(thd(), thd()->lex->current_query_block());
247 result =
248 new Item_func_between(POS(), new Item_field(fld), new Item_int(val1),
249 new Item_int(val2), false);
250 EXPECT_FALSE(result->itemize(&pc, (Item **)&result));
251 break;
252 }
253 default:
254 result = nullptr;
255 assert(false);
256 return result;
257 }
258 Item *itm = static_cast<Item *>(result);
259 result->fix_fields(thd(), &itm);
260 return result;
261 }
262
263 /*
264 Basic single predicate filter tests
265 */
266 TEST_F(ItemFilterTest, BasicDefaultRows) {
267 create_table(1, true);
268
269 const int unused_int = 0;
270 const table_map used_tables = 0;
271
272 MY_BITMAP no_ignore_flds;
273 bitmap_init(&no_ignore_flds, nullptr, m_table->s->fields);
274
275 // Check filtering for predicate: field0 = 10
276 create_item_check_filter(COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC,
277 m_field[0], 10, unused_int, used_tables,
278 &no_ignore_flds);
279 // Check filtering for predicate: field0 > 10
280 create_item_check_filter(COND_FILTER_INEQUALITY, Item_func::GT_FUNC,
281 m_field[0], 10, unused_int, used_tables,
282 &no_ignore_flds);
283 // Check filtering for predicate: field0 >= 10
284 create_item_check_filter(COND_FILTER_INEQUALITY, Item_func::GE_FUNC,
285 m_field[0], 10, unused_int, used_tables,
286 &no_ignore_flds);
287 // Check filtering for predicate: field0 < 10
288 create_item_check_filter(COND_FILTER_INEQUALITY, Item_func::LT_FUNC,
289 m_field[0], 10, unused_int, used_tables,
290 &no_ignore_flds);
291 // Check filtering for predicate: field0 <= 10
292 create_item_check_filter(COND_FILTER_INEQUALITY, Item_func::LE_FUNC,
293 m_field[0], 10, unused_int, used_tables,
294 &no_ignore_flds);
295 // Check filtering for predicate: field0 IS NULL
296 create_item_check_filter(COND_FILTER_EQUALITY, Item_func::ISNULL_FUNC,
297 m_field[0], unused_int, unused_int, used_tables,
298 &no_ignore_flds);
299 // Check filtering for predicate: field0 IS NOT NULL
300 create_item_check_filter(1.0F - COND_FILTER_EQUALITY,
301 Item_func::ISNOTNULL_FUNC, m_field[0], unused_int,
302 unused_int, used_tables, &no_ignore_flds);
303 // Check filtering for predicate: field0 BETWEEN 10 AND 12
304 create_item_check_filter(COND_FILTER_BETWEEN, Item_func::BETWEEN, m_field[0],
305 10, 12, used_tables, &no_ignore_flds);
306
307 bitmap_free(&no_ignore_flds);
308 }
309
310 /*
311 Basic single predicate filter tests when field should be ignored
312 */
313 TEST_F(ItemFilterTest, BasicIgnoreField) {
314 create_table(2, true);
315
316 const int unused_int = 0;
317 const table_map used_tables = 0;
318
319 // Predicates on m_field[0] should be ignored
320 MY_BITMAP ignore_fld0;
321 bitmap_init(&ignore_fld0, nullptr, m_table->s->fields);
322 bitmap_set_bit(&ignore_fld0, m_field[0]->field_index());
323
324 // Check filtering for predicate on ignored field: field0 = 10
325 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::MULT_EQUAL_FUNC,
326 m_field[0], 10, unused_int, used_tables,
327 &ignore_fld0);
328 // Check filtering for predicate on ignored field: field0 > 10
329 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::GT_FUNC, m_field[0],
330 10, unused_int, used_tables, &ignore_fld0);
331 // Check filtering for predicate on ignored field: field0 >= 10
332 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::GE_FUNC, m_field[0],
333 10, unused_int, used_tables, &ignore_fld0);
334 // Check filtering for predicate on ignored field: field0 < 10
335 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::LT_FUNC, m_field[0],
336 10, unused_int, used_tables, &ignore_fld0);
337 // Check filtering for predicate on ignored field: field0 <= 10
338 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::LE_FUNC, m_field[0],
339 10, unused_int, used_tables, &ignore_fld0);
340 // Check filtering for predicate on ignored field: field0 IS NULL
341 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::ISNULL_FUNC,
342 m_field[0], unused_int, unused_int, used_tables,
343 &ignore_fld0);
344 // Check filtering for predicate on ignored field: field0 IS NOT NULL
345 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::ISNOTNULL_FUNC,
346 m_field[0], unused_int, unused_int, used_tables,
347 &ignore_fld0);
348 // Check filtering for predicate on ignored field: field0 BETWEEN 10 AND 12
349 create_item_check_filter(COND_FILTER_ALLPASS, Item_func::BETWEEN, m_field[0],
350 10, 12, used_tables, &ignore_fld0);
351
352 // Verifying that predicates on other fields are not ignored
353 create_item_check_filter(COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC,
354 m_field[1], 10, unused_int, used_tables,
355 &ignore_fld0);
356
357 bitmap_free(&ignore_fld0);
358 }
359
360 /*
361 Basic condition test: AND of const predicates
362 */
363 TEST_F(ItemFilterTest, BasicConstAnd) {
364 create_table(3, true);
365
366 const int unused_int = 0;
367 const table_map used_tables = 0;
368
369 // Do not ignore predicates on any fields
370 MY_BITMAP ignore_flds;
371 bitmap_init(&ignore_flds, nullptr, m_table->s->fields);
372
373 // Create predicate: field0 = 10
374 Item *eq_item1 = create_item_check_filter(
375 COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC, m_field[0], 10,
376 unused_int, used_tables, &ignore_flds);
377 // Create predicate: field1 = 10
378 Item *eq_item2 = create_item_check_filter(
379 COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC, m_field[1], 10,
380 unused_int, used_tables, &ignore_flds);
381
382 // Create predicate: field2 < 99
383 Item *lt_item = create_item_check_filter(
384 COND_FILTER_INEQUALITY, Item_func::LT_FUNC, m_field[2], 99, unused_int,
385 used_tables, &ignore_flds);
386
387 List<Item> and_lst;
388 and_lst.push_back(eq_item1);
389 and_lst.push_back(eq_item2);
390
391 // Calculate filtering of (field0=10 AND field1=10)
392 create_anditem_check_filter(COND_FILTER_EQUALITY * COND_FILTER_EQUALITY,
393 and_lst, used_tables, &ignore_flds);
394
395 and_lst.push_back(lt_item);
396
397 // Calculate filtering of (field0=10 AND field1=10 AND field2<99)
398 Item *and_it = create_anditem_check_filter(
399 COND_FILTER_EQUALITY * COND_FILTER_EQUALITY * COND_FILTER_INEQUALITY,
400 and_lst, used_tables, &ignore_flds);
401
402 /*
403 Calculate filtering of (field0=10 AND field1=10 AND field2<99)
404 while ignoring predicates on field0
405 */
406 bitmap_set_bit(&ignore_flds, m_field[0]->field_index());
407
408 float filter = and_it->get_filtering_effect(
409 thd(), m_table_list->map(), used_tables, &ignore_flds, rows_in_table);
410 EXPECT_FLOAT_EQ(COND_FILTER_EQUALITY * COND_FILTER_INEQUALITY, filter);
411
412 /*
413 Calculate filtering of (field0=10 AND field1=10 AND field2<99)
414 while ignoring predicates on field0 and field1
415 */
416 bitmap_set_bit(&ignore_flds, m_field[1]->field_index());
417 filter = and_it->get_filtering_effect(thd(), m_table_list->map(), used_tables,
418 &ignore_flds, rows_in_table);
419 EXPECT_FLOAT_EQ(COND_FILTER_INEQUALITY, filter);
420
421 /*
422 Calculate filtering of (field0=10 AND field1=10 AND field2<99)
423 while ignoring predicates on field0, field1 and field2
424 */
425 bitmap_set_bit(&ignore_flds, m_field[2]->field_index());
426 filter = and_it->get_filtering_effect(thd(), m_table_list->map(), used_tables,
427 &ignore_flds, rows_in_table);
428 EXPECT_FLOAT_EQ(COND_FILTER_ALLPASS, filter);
429
430 bitmap_free(&ignore_flds);
431 }
432
433 /*
434 Basic condition test: OR of const predicates
435 */
436 TEST_F(ItemFilterTest, BasicConstOr) {
437 create_table(3, true);
438
439 const int unused_int = 0;
440 const table_map used_tables = 0;
441
442 // Do not ignore predicates on any fields
443 MY_BITMAP ignore_flds;
444 bitmap_init(&ignore_flds, nullptr, m_table->s->fields);
445
446 // Create predicate: field0 = 10
447 Item *eq_item1 = create_item_check_filter(
448 COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC, m_field[0], 10,
449 unused_int, used_tables, &ignore_flds);
450 // Create predicate: field1 = 10
451 Item *eq_item2 = create_item_check_filter(
452 COND_FILTER_EQUALITY, Item_func::MULT_EQUAL_FUNC, m_field[1], 10,
453 unused_int, used_tables, &ignore_flds);
454
455 // Create predicate: field2 < 99
456 Item *lt_item = create_item_check_filter(
457 COND_FILTER_INEQUALITY, Item_func::LT_FUNC, m_field[2], 99, unused_int,
458 used_tables, &ignore_flds);
459
460 List<Item> or_lst;
461 or_lst.push_back(eq_item1);
462 or_lst.push_back(eq_item2);
463
464 // Calculate filtering of (field0=10 OR field1=10)
465 const float expected1 =
466 2 * COND_FILTER_EQUALITY - (COND_FILTER_EQUALITY * COND_FILTER_EQUALITY);
467 create_oritem_check_filter(expected1, or_lst, used_tables, &ignore_flds);
468
469 or_lst.push_back(lt_item);
470
471 // Calculate filtering of (field0=10 OR field1=10 OR field2<99)
472 const float expected2 =
473 expected1 + COND_FILTER_INEQUALITY - (expected1 * COND_FILTER_INEQUALITY);
474 Item *or_it =
475 create_oritem_check_filter(expected2, or_lst, used_tables, &ignore_flds);
476
477 /*
478 Calculate filtering of (field0=10 OR field1=10 OR field2<99) while
479 ignoring predicates on m_field[0]
480
481 A predicate on an ignored field has filtering effect 1.0 (no
482 filtering). The filtering effect for "A or B" is calculated as
483 "Disjunction of independent events":
484
485 P(A or B) = P(A) + P(B) - P(A and B)
486
487 The result is always 1.0 if any of the ORed predicates is 1.0.
488
489 */
490 bitmap_set_bit(&ignore_flds, m_field[0]->field_index());
491 const float filt_ign0 = or_it->get_filtering_effect(
492 thd(), m_table_list->map(), used_tables, &ignore_flds, rows_in_table);
493 EXPECT_FLOAT_EQ(COND_FILTER_ALLPASS, filt_ign0);
494
495 /*
496 Calculate filtering of (field0=10 OR field1=10 OR field2<99) while
497 ignoring predicates on m_field[0] and m_field[1]
498 */
499 bitmap_set_bit(&ignore_flds, m_field[1]->field_index());
500 const float filt_ign1 = or_it->get_filtering_effect(
501 thd(), m_table_list->map(), used_tables, &ignore_flds, rows_in_table);
502 EXPECT_FLOAT_EQ(COND_FILTER_ALLPASS, filt_ign1);
503
504 /*
505 Calculate filtering of (field0=10 OR field1=10 OR field2<99) while
506 ignoring predicates on m_field[2]
507 */
508 bitmap_clear_all(&ignore_flds);
509 bitmap_set_bit(&ignore_flds, m_field[2]->field_index());
510 const float filt_ign2 = or_it->get_filtering_effect(
511 thd(), m_table_list->map(), used_tables, &ignore_flds, rows_in_table);
512 EXPECT_FLOAT_EQ(COND_FILTER_ALLPASS, filt_ign2);
513
514 bitmap_free(&ignore_flds);
515 }
516
517 /*
518 Test filtering for IN predicates
519 */
520 TEST_F(ItemFilterTest, InPredicate) {
521 create_table(1, true);
522
523 const table_map used_tables = 0;
524
525 // Do not ignore predicates on any fields
526 MY_BITMAP ignore_flds;
527 bitmap_init(&ignore_flds, nullptr, m_table->s->fields);
528
529 // Calculate filtering effect of "col IN (1)"
530 mem_root_deque<Item *> in_lst1(*THR_MALLOC);
531 in_lst1.push_back(new Item_field(m_field[0]));
532 in_lst1.push_back(new Item_int(1));
533
534 create_initem_check_filter(COND_FILTER_EQUALITY, in_lst1, used_tables,
535 &ignore_flds);
536
537 // Calculate filtering effect of "col IN (1, ..., 4)"
538 mem_root_deque<Item *> in_lst2(*THR_MALLOC);
539 in_lst2.push_back(new Item_field(m_field[0]));
540 in_lst2.push_back(new Item_int(1));
541 in_lst2.push_back(new Item_int(2));
542 in_lst2.push_back(new Item_int(3));
543 in_lst2.push_back(new Item_int(4));
544
545 const float filter = 4 * COND_FILTER_EQUALITY;
546 Item_func_in *in_it =
547 create_initem_check_filter(filter, in_lst2, used_tables, &ignore_flds);
548
549 /*
550 Calculate filtering effect of "col IN (1, ..., 110)"
551
552 "col IN (<110 values>)" would mean a filtering effect of
553 110 * COND_FILTER_EQUALITY = 0.6, but the filtering effect
554 of IN has an upper limit of 0.5.
555 */
556 mem_root_deque<Item *> in_lst3(*THR_MALLOC);
557 in_lst3.push_back(new Item_field(m_field[0]));
558 for (int i = 1; i <= 110; i++) in_lst3.push_back(new Item_int(i));
559
560 const float capped_filter = 0.5;
561 create_initem_check_filter(capped_filter, in_lst3, used_tables, &ignore_flds);
562
563 /*
564 There is no filtering effect for "col IN (1,..,6)" if 'col' is
565 ignored.
566 */
567 bitmap_set_bit(&ignore_flds, m_field[0]->field_index());
568 const float filt_ign = in_it->get_filtering_effect(
569 thd(), m_table_list->map(), used_tables, &ignore_flds, rows_in_table);
570 EXPECT_FLOAT_EQ(COND_FILTER_ALLPASS, filt_ign);
571 bitmap_free(&ignore_flds);
572 }
573
574 } // namespace item_filter_unittest
575 #undef create_item_check_filter
576 #undef create_anditem_check_filter
577 #undef create_oritem_check_filter
578 #undef create_initem_check_filter