Debian Codegraph @

unittest/gunit/fake_range_opt_param.h

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 #ifndef FAKE_RANGE_OPT_PARAM
25 #define FAKE_RANGE_OPT_PARAM
26
27 #include <gmock/gmock.h>
28
29 #include "sql/range_optimizer/internal.h"
30 #include "sql/range_optimizer/range_optimizer.h"
31 #include "unittest/gunit/fake_table.h"
32
33 using ::testing::_;
34 using ::testing::Return;
35
36 class Fake_RANGE_OPT_PARAM : public RANGE_OPT_PARAM {
37 KEY_PART m_key_parts[64];
38 Mem_root_array<KEY_PART_INFO> m_kpis;
39 Fake_TABLE fake_table;
40
41 public:
42 /**
43 Creates a Fake_RANGE_OPT_PARAM and optionally a Fake_TABLE.
44
45 @note The Fake_TABLE is always created, but with zero columns if
46 number_columns is zero. However, it won't be used since
47 Fake_RANGE_OPT_PARAM::table is NULL.
48
49 @param number_columns If non-zero, a Fake_TABLE is created with
50 this many columns.
51
52 @param columns_nullable Creates nullable columns, if applicable.
53 */
54 Fake_RANGE_OPT_PARAM(THD *thd_arg, MEM_ROOT *alloc_arg, int number_columns,
55 bool columns_nullable)
56 : m_kpis(alloc_arg), fake_table(number_columns, columns_nullable) {
57 m_kpis.reserve(64);
58
59 real_keynr = alloc_arg->ArrayAlloc<uint>(MAX_KEY);
60 key = alloc_arg->ArrayAlloc<KEY_PART *>(MAX_KEY);
61
62 return_mem_root = alloc_arg;
63 temp_mem_root = alloc_arg;
64 query_block = thd_arg->lex->current_query_block();
65
66 if (number_columns != 0) {
67 table = &fake_table;
68 } else {
69 table = nullptr;
70 }
71
72 using_real_indexes = true;
73 key_parts = m_key_parts;
74 key_parts_end = m_key_parts;
75 keys = 0;
76
77 const Mock_HANDLER *mock_handler = &fake_table.mock_handler;
78
79 ON_CALL(*mock_handler, index_flags(_, _, true))
80 .WillByDefault(Return(HA_READ_RANGE));
81 }
82
83 void add_key(List<Field> fields_in_index) {
84 List_iterator<Field> it(fields_in_index);
85 int cur_kp = 0;
86
87 table->key_info[keys].actual_key_parts = 0;
88 for (Field *cur_field = it++; cur_field; cur_field = it++, cur_kp++) {
89 KEY_PART_INFO *kpi = m_kpis.end(); // Points past the end.
90 m_kpis.push_back(KEY_PART_INFO()); // kpi now points to a new element
91 kpi->init_from_field(cur_field);
92
93 key_parts_end->key = keys;
94 key_parts_end->part = cur_kp;
95 key_parts_end->length = kpi->store_length;
96 key_parts_end->store_length = kpi->store_length;
97 key_parts_end->field = kpi->field;
98 key_parts_end->null_bit = kpi->null_bit;
99 key_parts_end->flag = static_cast<uint8>(kpi->key_part_flag);
100 key_parts_end->image_type = Field::itRAW;
101
102 key_parts_end++;
103 table->key_info[keys].key_part[cur_kp] = *kpi;
104 table->key_info[keys].actual_key_parts++;
105 }
106 table->key_info[keys].user_defined_key_parts =
107 table->key_info[keys].actual_key_parts;
108 real_keynr[keys] = keys;
109 keys++;
110 }
111
112 void add_key(Field *field_to_index) {
113 List<Field> index_list;
114 index_list.push_back(field_to_index);
115 add_key(index_list);
116 }
117
118 void add_key(Field *field_to_index1, Field *field_to_index2) {
119 List<Field> index_list;
120 index_list.push_back(field_to_index1);
121 index_list.push_back(field_to_index2);
122 add_key(index_list);
123 }
124
125 /// Creates an index over all columns in the RANGE_OPT_PARAM's table.
126 void add_key() {
127 List<Field> index_list;
128 for (uint i = 0; i < table->s->fields; ++i)
129 index_list.push_back(table->field[i]);
130 add_key(index_list);
131 }
132
133 ~Fake_RANGE_OPT_PARAM() {
134 for (uint i = 0; i < keys; i++) {
135 table->key_info[i].actual_key_parts = 0;
136 table->key_info[i].user_defined_key_parts = 0;
137 }
138 }
139 };
140
141 #endif // FAKE_RANGE_OPT_PARAM