Debian Codegraph @

unittest/gunit/opt_ref-t.cc

raw symbols SCIP
1 /* Copyright (c) 2013, 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 <gtest/gtest.h>
25 #include <stddef.h>
26 #include <sys/types.h>
27
28 #include "sql/parse_tree_helpers.h"
29 #include "sql/sql_optimizer.h"
30 #include "sql/sql_optimizer_internal.h"
31 #include "unittest/gunit/fake_table.h"
32 #include "unittest/gunit/mock_field_long.h"
33 #include "unittest/gunit/test_utils.h"
34
35 // Unit tests of the ref optimizer.
36 namespace opt_ref_unittest {
37
38 using my_testing::Server_initializer;
39
40 bool scrap_bool; // Needed by Key_field CTOR.
41
42 /*
43 Class for easy creation of an array of Key_field's. Must be the same
44 size as Key_field.
45 */
46 class Fake_key_field : public Key_field {
47 public:
48 Fake_key_field()
49 : Key_field(nullptr, nullptr, 0, 0, false, false, &scrap_bool, 0) {}
50 };
51
52 /*
53 Class that tests the ref optimizer. The class creates the fake table
54 definitions:
55
56 t1(a int, b int, key(a, b))
57 t2(a int, b int)
58 */
59 class OptRefTest : public ::testing::Test {
60 public:
61 OptRefTest() : t1_key_fields(&t1_key_field_arr[0]) {}
62
63 void SetUp() override {
64 // We do some pointer arithmetic on these
65 static_assert(sizeof(Fake_key_field) == sizeof(Key_field), "");
66 initializer.SetUp();
67
68 item_zero = new Item_int(0);
69 item_one = new Item_int(1);
70
71 field_t1_a = new Mock_field_long("field1", true, false);
72 field_t1_b = new Mock_field_long("field2", true, false);
73 field_t2_a = new Mock_field_long("field3", true, false);
74 field_t2_b = new Mock_field_long("field4", true, false);
75 t1 = new Fake_TABLE(field_t1_a, field_t1_b);
76 t2 = new Fake_TABLE(field_t2_a, field_t2_b);
77
78 index_over_t1ab_id = t1->create_index(field_t1_a, field_t1_b);
79 indexes.set_bit(index_over_t1ab_id);
80
81 t1_join_tab.set_qs(&t1_qep_shared);
82 t1_join_tab.set_table(t1);
83
84 item_field_t1_a = new Item_field(field_t1_a);
85 item_field_t1_b = new Item_field(field_t1_b);
86
87 item_field_t2_a = new Item_field(field_t2_a);
88 item_field_t2_b = new Item_field(field_t2_b);
89 }
90
91 void TearDown() override {
92 delete field_t1_a;
93 delete field_t1_b;
94 delete field_t2_a;
95 delete field_t2_b;
96 delete t1;
97 delete t2;
98 initializer.TearDown();
99 }
100
101 THD *thd() { return initializer.thd(); }
102
103 Key_map indexes;
104
105 Mock_field_long *field_t1_a, *field_t1_b;
106 Mock_field_long *field_t2_a, *field_t2_b;
107
108 Fake_TABLE *t1;
109 Fake_TABLE *t2;
110
111 JOIN_TAB t1_join_tab;
112 QEP_shared t1_qep_shared;
113 Fake_key_field t1_key_field_arr[10];
114 Key_field *t1_key_fields;
115
116 Item_int *item_zero;
117 Item_int *item_one;
118
119 Item_field *item_field_t1_a, *item_field_t1_b;
120 Item_field *item_field_t2_a, *item_field_t2_b;
121
122 int index_over_t1ab_id;
123
124 void call_add_key_fields(Item *cond) {
125 uint and_level = 0;
126 (void)add_key_fields(thd(), nullptr /* join */, &t1_key_fields, &and_level,
127 cond, ~0ULL, nullptr);
128 }
129
130 private:
131 Server_initializer initializer;
132 };
133
134 static Item_row *make_item_row(Item *a, Item *b) {
135 /*
136 The Item_row CTOR doesn't store the reference to the list, hence
137 it can live on the stack.
138 */
139 mem_root_deque<Item *> items(*THR_MALLOC);
140 items.push_back(b);
141 return new Item_row(POS(), a, items);
142 }
143
144 TEST_F(OptRefTest, addKeyFieldsFromInOneRow) {
145 /*
146 We simulate the where condition (a, b) IN ((0, 0)). Note that this
147 can't happen in practice since the parser is hacked to parse such
148 an expression in to (a, b) = (0, 0), which gets rewritten into a =
149 0 AND b = 0 before the ref optimizer runs.
150 */
151 PT_item_list *all_args = new (current_thd->mem_root) PT_item_list;
152 all_args->push_front(make_item_row(item_zero, item_zero));
153 all_args->push_front(make_item_row(item_field_t1_a, item_field_t1_b));
154 Item *cond = new Item_func_in(POS(), all_args, false);
155 Parse_context pc(thd(), thd()->lex->current_query_block());
156 EXPECT_FALSE(cond->itemize(&pc, &cond));
157
158 call_add_key_fields(cond);
159
160 // We expect the key_fields pointer not to be incremented.
161 EXPECT_EQ(0, t1_key_fields - static_cast<Key_field *>(&t1_key_field_arr[0]));
162 EXPECT_EQ(indexes, t1_join_tab.const_keys)
163 << "SARGable index not present in const_keys";
164 EXPECT_EQ(indexes, t1_join_tab.keys());
165 EXPECT_EQ(0U, t1_key_field_arr[0].level);
166 EXPECT_EQ(0U, t1_key_field_arr[1].level);
167 }
168
169 TEST_F(OptRefTest, addKeyFieldsFromInTwoRows) {
170 // We simulate the where condition (col_a, col_b) IN ((0, 0), (1, 1))
171 PT_item_list *all_args = new (current_thd->mem_root) PT_item_list;
172 all_args->push_front(make_item_row(item_one, item_one));
173 all_args->push_front(make_item_row(item_zero, item_zero));
174 all_args->push_front(make_item_row(item_field_t1_a, item_field_t1_b));
175 Item *cond = new Item_func_in(POS(), all_args, false);
176 Parse_context pc(thd(), thd()->lex->current_query_block());
177 EXPECT_FALSE(cond->itemize(&pc, &cond));
178
179 call_add_key_fields(cond);
180
181 // We expect the key_fields pointer not to be incremented.
182 EXPECT_EQ(0, t1_key_fields - static_cast<Key_field *>(&t1_key_field_arr[0]));
183 EXPECT_EQ(indexes, t1_join_tab.const_keys)
184 << "SARGable index not present in const_keys";
185 EXPECT_EQ(indexes, t1_join_tab.keys());
186 }
187
188 TEST_F(OptRefTest, addKeyFieldsFromInOneRowWithCols) {
189 // We simulate the where condition (t1.a, t1.b) IN ((t2.a, t2.b))
190 PT_item_list *all_args = new (current_thd->mem_root) PT_item_list;
191 all_args->push_front(make_item_row(item_field_t2_a, item_field_t2_b));
192 all_args->push_front(make_item_row(item_field_t1_a, item_field_t1_b));
193 Item *cond = new Item_func_in(POS(), all_args, false);
194 Parse_context pc(thd(), thd()->lex->current_query_block());
195 EXPECT_FALSE(cond->itemize(&pc, &cond));
196
197 call_add_key_fields(cond);
198
199 // We expect the key_fields pointer not to be incremented.
200 EXPECT_EQ(0, t1_key_fields - static_cast<Key_field *>(&t1_key_field_arr[0]));
201 EXPECT_EQ(Key_map(0), t1_join_tab.const_keys);
202 EXPECT_EQ(indexes, t1_join_tab.keys());
203
204 EXPECT_EQ(t2->pos_in_table_list->map(), t1_join_tab.key_dependent);
205 }
206
207 TEST_F(OptRefTest, addKeyFieldsFromEq) {
208 // We simulate the where condition a = 0 AND b = 0
209 Item_func_eq *eq1 = new Item_func_eq(item_field_t1_a, item_zero);
210 Item_func_eq *eq2 = new Item_func_eq(item_field_t1_b, item_zero);
211 Item_cond_and *cond = new Item_cond_and(eq1, eq2);
212
213 call_add_key_fields(cond);
214
215 /*
216 We expect 2 Key_field's to be written. Actually they're always
217 written, but we expect the pointer to be incremented.
218 */
219 EXPECT_EQ(2, t1_key_fields - static_cast<Key_field *>(&t1_key_field_arr[0]));
220 EXPECT_EQ(indexes, t1_join_tab.const_keys)
221 << "SARGable index not present in const_keys";
222 EXPECT_EQ(indexes, t1_join_tab.keys());
223
224 EXPECT_EQ(0U, t1_join_tab.key_dependent);
225
226 EXPECT_EQ(0U, t1_key_field_arr[0].level);
227 EXPECT_EQ(0U, t1_key_field_arr[1].level);
228 }
229
230 } // namespace opt_ref_unittest