Debian Codegraph @

unittest/gunit/xplugin/xpl/index_field_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 #include <memory>
29
30 #include "plugin/x/src/admin_cmd_arguments.h"
31 #include "plugin/x/src/index_field.h"
32 #include "plugin/x/src/query_string_builder.h"
33 #include "plugin/x/src/xpl_error.h"
34 #include "unittest/gunit/xplugin/xpl/assert_error_code.h"
35 #include "unittest/gunit/xplugin/xpl/mock/session.h"
36 #include "unittest/gunit/xplugin/xpl/mock/sql_session.h"
37 #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h"
38 #include "unittest/gunit/xplugin/xpl/one_row_resultset.h"
39
40 namespace xpl {
41 namespace test {
42
43 using ::testing::_;
44 using ::testing::DoAll;
45 using ::testing::Eq;
46 using ::testing::Return;
47
48 namespace {
49 #define PATH "$.path"
50 #define PATH_HASH "6EA549FAA434CCD150A7DB5FF9C0AEC77C4F5D25"
51 #define NOT_REQUIRED false
52 #define REQUIRED true
53 #define OPTIONS 42u
54 #define SRID 666u
55 #define DEFAULT_VALUE std::numeric_limits<uint64_t>::max()
56
57 #define SHOW_COLUMNS(field) \
58 "SHOW COLUMNS FROM `schema`.`collection`" \
59 " WHERE Field = '" field "'"
60
61 struct Field_info : public Admin_command_index::Index_field_info {
62 Field_info(const std::string &path, const std::string &type,
63 const bool is_required = NOT_REQUIRED,
64 const uint64_t options = DEFAULT_VALUE,
65 const uint64_t srid = DEFAULT_VALUE) {
66 m_path = path;
67 m_type = type;
68 m_is_required = is_required;
69 m_options = options;
70 m_srid = srid;
71 }
72 };
73
74 } // namespace
75
76 class Index_field_create_test : public ::testing::TestWithParam<Field_info> {};
77
78 TEST_P(Index_field_create_test, fail_on_create) {
79 const auto &param = GetParam();
80 ngs::Error_code error;
81 std::unique_ptr<const Index_field> field(
82 Index_field::create(true, param, &error));
83 ASSERT_ERROR_CODE(ER_X_CMD_ARGUMENT_VALUE, error);
84 ASSERT_EQ(nullptr, field.get());
85 }
86
87 Field_info fail_on_create_param[] = {
88 {"", "DECIMAL"},
89 {PATH, ""},
90 {PATH, "DECIMAL SIGNED"},
91 {PATH, "tinyint(10,2)"},
92 {PATH, "tinyint", NOT_REQUIRED, OPTIONS},
93 {PATH, "tinyint", NOT_REQUIRED, DEFAULT_VALUE, SRID},
94 {PATH, "smallint(10,2)"},
95 {PATH, "smallint", NOT_REQUIRED, OPTIONS},
96 {PATH, "smallint", NOT_REQUIRED, DEFAULT_VALUE, SRID},
97 {PATH, "mediumint(10,2)"},
98 {PATH, "mediumint", NOT_REQUIRED, OPTIONS},
99 {PATH, "mediumint", NOT_REQUIRED, DEFAULT_VALUE, SRID},
100 {PATH, "int(10,2)"},
101 {PATH, "int", NOT_REQUIRED, OPTIONS},
102 {PATH, "int", NOT_REQUIRED, DEFAULT_VALUE, SRID},
103 {PATH, "integer(10,2)"},
104 {PATH, "integer", NOT_REQUIRED, OPTIONS},
105 {PATH, "integer", NOT_REQUIRED, DEFAULT_VALUE, SRID},
106 {PATH, "bigint(10,2)"},
107 {PATH, "bigint", NOT_REQUIRED, OPTIONS},
108 {PATH, "bigint", NOT_REQUIRED, DEFAULT_VALUE, SRID},
109 {PATH, "real", NOT_REQUIRED, OPTIONS},
110 {PATH, "real", NOT_REQUIRED, DEFAULT_VALUE, SRID},
111 {PATH, "float", NOT_REQUIRED, OPTIONS},
112 {PATH, "float", NOT_REQUIRED, DEFAULT_VALUE, SRID},
113 {PATH, "double", NOT_REQUIRED, OPTIONS},
114 {PATH, "double", NOT_REQUIRED, DEFAULT_VALUE, SRID},
115 {PATH, "numeric", NOT_REQUIRED, OPTIONS},
116 {PATH, "numeric", NOT_REQUIRED, DEFAULT_VALUE, SRID},
117 {PATH, "date(10)"},
118 {PATH, "date(10,2)"},
119 {PATH, "date unsigned"},
120 {PATH, "date", NOT_REQUIRED, OPTIONS},
121 {PATH, "date", NOT_REQUIRED, DEFAULT_VALUE, SRID},
122 {PATH, "time(10,2)"},
123 {PATH, "time unsigned"},
124 {PATH, "time", NOT_REQUIRED, OPTIONS},
125 {PATH, "time", NOT_REQUIRED, DEFAULT_VALUE, SRID},
126 {PATH, "timestamp(10,2)"},
127 {PATH, "timestamp unsigned"},
128 {PATH, "timestamp", NOT_REQUIRED, OPTIONS},
129 {PATH, "timestamp", NOT_REQUIRED, DEFAULT_VALUE, SRID},
130 {PATH, "datetime(10,2)"},
131 {PATH, "datetime (10)"},
132 {PATH, "datetime unsigned"},
133 {PATH, "datetime", NOT_REQUIRED, OPTIONS},
134 {PATH, "datetime", NOT_REQUIRED, DEFAULT_VALUE, SRID},
135 {PATH, "year(10,2)"},
136 {PATH, "year unsigned"},
137 {PATH, "year", NOT_REQUIRED, OPTIONS},
138 {PATH, "year", NOT_REQUIRED, DEFAULT_VALUE, SRID},
139 {PATH, "bit(10,2)"},
140 {PATH, "bit (10)"},
141 {PATH, "bit unsigned "},
142 {PATH, "bit", NOT_REQUIRED, OPTIONS},
143 {PATH, "bit", NOT_REQUIRED, DEFAULT_VALUE, SRID},
144 {PATH, "blob(10,2)"},
145 {PATH, "blob unsigned"},
146 {PATH, "blob", NOT_REQUIRED, OPTIONS},
147 {PATH, "blob", NOT_REQUIRED, DEFAULT_VALUE, SRID},
148 {PATH, "text(10,2)"},
149 {PATH, "text unsigned"},
150 {PATH, "text", NOT_REQUIRED, OPTIONS},
151 {PATH, "text", NOT_REQUIRED, DEFAULT_VALUE, SRID},
152 {PATH, "geojson(10)"},
153 {PATH, "geojson(10,2)"},
154 {PATH, "geojson unsigned"},
155 {PATH, "fulltext(10)"},
156 {PATH, "fulltext unsigned"},
157 {PATH, "fulltext", NOT_REQUIRED, OPTIONS},
158 {PATH, "fulltext", NOT_REQUIRED, DEFAULT_VALUE, SRID},
159 {PATH, "char(10,2)"},
160 };
161
162 INSTANTIATE_TEST_SUITE_P(fail_on_create_field, Index_field_create_test,
163 ::testing::ValuesIn(fail_on_create_param));
164
165 struct Param_index_field_add_field {
166 std::string expect;
167 Field_info info;
168 };
169
170 class Index_field_add_field_test
171 : public ::testing::TestWithParam<Param_index_field_add_field> {};
172
173 TEST_P(Index_field_add_field_test, add_field) {
174 const auto &param = GetParam();
175 Query_string_builder qb;
176 ngs::Error_code error;
177 std::unique_ptr<const Index_field> field(
178 Index_field::create(true, param.info, &error));
179 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
180 field->add_field(&qb);
181 ASSERT_STREQ(std::string(param.expect).c_str(), qb.get().c_str());
182 }
183
184 Param_index_field_add_field add_field_param[] = {
185 {"`$ix_xd_" PATH_HASH "`", {PATH, "DECIMAL", NOT_REQUIRED}},
186 {"`$ix_xd_" PATH_HASH "`", {PATH, "decimal", NOT_REQUIRED}},
187 {"`$ix_xd_" PATH_HASH "`", {PATH, "DEcimAL", NOT_REQUIRED}},
188 {"`$ix_xd32_" PATH_HASH "`", {PATH, "DECIMAL(32)", NOT_REQUIRED}},
189 {"`$ix_xd32_16_" PATH_HASH "`", {PATH, "DECIMAL(32,16)", NOT_REQUIRED}},
190 {"`$ix_xd_16_" PATH_HASH "`", {PATH, "DECIMAL(0,16)", NOT_REQUIRED}},
191 {"`$ix_xd32_16_u_" PATH_HASH "`",
192 {PATH, "DECIMAL(32,16) UNSIGNED", NOT_REQUIRED}},
193 {"`$ix_xd32_16_ur_" PATH_HASH "`",
194 {PATH, "DECIMAL(32,16) UNSIGNED", REQUIRED}},
195 {"`$ix_xd32_16_ur_" PATH_HASH "`",
196 {PATH, "DECIMAL(32,16) UNSIGNED", REQUIRED}},
197 {"`$ix_xd32_16_r_" PATH_HASH "`", {PATH, "DECIMAL(32,16)", REQUIRED}},
198 {"`$ix_xd_ur_" PATH_HASH "`", {PATH, "DECIMAL UNSIGNED", REQUIRED}},
199 {"`$ix_xd_ur_" PATH_HASH "`", {PATH, "DECIMAL unsigned", REQUIRED}},
200 {"`$ix_xd_ur_" PATH_HASH "`", {PATH, "DECIMAL UNsignED", REQUIRED}},
201 {"`$ix_xd_ur_" PATH_HASH "`", {PATH, "DECIMAL UNSIGNED", REQUIRED}},
202 {"`$ix_it_" PATH_HASH "`", {PATH, "tinyint", NOT_REQUIRED}},
203 {"`$ix_is_" PATH_HASH "`", {PATH, "smallint", NOT_REQUIRED}},
204 {"`$ix_im_" PATH_HASH "`", {PATH, "mediumint", NOT_REQUIRED}},
205 {"`$ix_i_" PATH_HASH "`", {PATH, "int", NOT_REQUIRED}},
206 {"`$ix_i_" PATH_HASH "`", {PATH, "integer", NOT_REQUIRED}},
207 {"`$ix_ib_" PATH_HASH "`", {PATH, "bigint", NOT_REQUIRED}},
208 {"`$ix_fr_" PATH_HASH "`", {PATH, "real", NOT_REQUIRED}},
209 {"`$ix_f_" PATH_HASH "`", {PATH, "float", NOT_REQUIRED}},
210 {"`$ix_fd_" PATH_HASH "`", {PATH, "double", NOT_REQUIRED}},
211 {"`$ix_fd32_16_" PATH_HASH "`", {PATH, "double(32, 16)", NOT_REQUIRED}},
212 {"`$ix_fd32_16_" PATH_HASH "`", {PATH, "double(32 ,16)", NOT_REQUIRED}},
213 {"`$ix_fd32_16_" PATH_HASH "`", {PATH, "double(32 , 16)", NOT_REQUIRED}},
214 {"`$ix_xn_" PATH_HASH "`", {PATH, "numeric", NOT_REQUIRED}},
215 {"`$ix_d_" PATH_HASH "`", {PATH, "date", NOT_REQUIRED}},
216 {"`$ix_dt_" PATH_HASH "`", {PATH, "time", NOT_REQUIRED}},
217 {"`$ix_ds_" PATH_HASH "`", {PATH, "timestamp", NOT_REQUIRED}},
218 {"`$ix_dd_" PATH_HASH "`", {PATH, "datetime", NOT_REQUIRED}},
219 {"`$ix_dy_" PATH_HASH "`", {PATH, "year", NOT_REQUIRED}},
220 {"`$ix_t_" PATH_HASH "`", {PATH, "bit", NOT_REQUIRED}},
221 {"`$ix_bt_" PATH_HASH "`", {PATH, "blob", NOT_REQUIRED}},
222 {"`$ix_t_" PATH_HASH "`", {PATH, "text", NOT_REQUIRED}},
223 {"`$ix_gj_" PATH_HASH "`", {PATH, "geojson", NOT_REQUIRED}},
224 {"`$ix_ft_" PATH_HASH "`", {PATH, "fulltext", NOT_REQUIRED}},
225 {"`$ix_t32_" PATH_HASH "`(32)", {PATH, "text(32)", NOT_REQUIRED}},
226 {"`$ix_c_" PATH_HASH "`", {PATH, "char", NOT_REQUIRED}},
227 {"`$ix_c10_" PATH_HASH "`", {PATH, "char(10)", NOT_REQUIRED}},
228 {"`$ix_c20_" PATH_HASH "`",
229 {PATH, "char(20) charset latin1", NOT_REQUIRED}},
230 {"`$ix_c30_" PATH_HASH "`",
231 {PATH, "char(30) collate latin1_bin", NOT_REQUIRED}},
232 {"`$ix_c40_" PATH_HASH "`",
233 {PATH, "char(40) character set latin1 collate latin1_bin", NOT_REQUIRED}},
234 {"`$ix_t128_" PATH_HASH "`(128)",
235 {PATH, "text(128) character set latin1 collate latin1_bin", NOT_REQUIRED}},
236 {"`$ix_c_" PATH_HASH "`",
237 {PATH, "char character set latin1", NOT_REQUIRED}},
238 };
239
240 INSTANTIATE_TEST_SUITE_P(get_index_field_name, Index_field_add_field_test,
241 ::testing::ValuesIn(add_field_param));
242
243 struct Param_index_field_add_column {
244 std::string expect;
245 bool virtual_supported;
246 Field_info info;
247 };
248
249 class Index_field_add_column_test
250 : public ::testing::TestWithParam<Param_index_field_add_column> {};
251
252 TEST_P(Index_field_add_column_test, add_column) {
253 const Param_index_field_add_column &param = GetParam();
254 Query_string_builder qb;
255 ngs::Error_code error;
256 std::unique_ptr<const Index_field> field(
257 Index_field::create(param.virtual_supported, param.info, &error));
258 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
259 field->add_column(&qb);
260 ASSERT_STREQ(param.expect.c_str(), qb.get().c_str());
261 }
262
263 Param_index_field_add_column add_column_param[] = {
264 {" ADD COLUMN `$ix_xd_" PATH_HASH
265 "` DECIMAL GENERATED ALWAYS AS (JSON_EXTRACT(doc, '$.path')) VIRTUAL",
266 true,
267 {PATH, "DECIMAL", NOT_REQUIRED}},
268 {" ADD COLUMN `$ix_xd_" PATH_HASH
269 "` DECIMAL GENERATED ALWAYS AS (JSON_EXTRACT(doc, '$.path')) STORED",
270 false,
271 {PATH, "DECIMAL", NOT_REQUIRED}},
272 {" ADD COLUMN `$ix_t32_" PATH_HASH
273 "` TEXT GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(doc, "
274 "'$.path'))) VIRTUAL",
275 true,
276 {PATH, "TEXT(32)", NOT_REQUIRED}},
277 {" ADD COLUMN `$ix_t32_r_" PATH_HASH
278 "` TEXT GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(doc, "
279 "'$.path'))) VIRTUAL NOT NULL",
280 true,
281 {PATH, "TEXT(32)", REQUIRED}},
282 {" ADD COLUMN `$ix_gj_r_" PATH_HASH
283 "` GEOMETRY GENERATED ALWAYS AS (ST_GEOMFROMGEOJSON"
284 "(JSON_EXTRACT(doc, '$.path'),1,4326)) STORED NOT NULL SRID 4326",
285 true,
286 {PATH, "GEOJSON", REQUIRED}},
287 {" ADD COLUMN `$ix_gj_" PATH_HASH
288 "` GEOMETRY GENERATED ALWAYS AS (ST_GEOMFROMGEOJSON("
289 "JSON_EXTRACT(doc, '$.path'),42,4326)) STORED SRID 4326",
290 true,
291 {PATH, "GEOJSON", NOT_REQUIRED, OPTIONS}},
292 {" ADD COLUMN `$ix_gj_" PATH_HASH
293 "` GEOMETRY GENERATED ALWAYS AS (ST_GEOMFROMGEOJSON("
294 "JSON_EXTRACT(doc, '$.path'),1,666)) STORED SRID 666",
295 false,
296 {PATH, "GEOJSON", NOT_REQUIRED, DEFAULT_VALUE, SRID}},
297 {" ADD COLUMN `$ix_ft_" PATH_HASH
298 "` TEXT GENERATED ALWAYS AS (JSON_UNQUOTE("
299 "JSON_EXTRACT(doc, '$.path'))) STORED",
300 false,
301 {PATH, "FULLTEXT", NOT_REQUIRED}},
302 {" ADD COLUMN `$ix_c20_" PATH_HASH
303 "` CHAR(20) GENERATED ALWAYS AS (JSON_UNQUOTE("
304 "JSON_EXTRACT(doc, '$.path'))) STORED",
305 false,
306 {PATH, "CHAR(20)", NOT_REQUIRED}},
307 {" ADD COLUMN `$ix_t64_" PATH_HASH
308 "` TEXT CHARACTER SET latin1 COLLATE latin1_bin"
309 " GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(doc, '$.path'))) STORED",
310 false,
311 {PATH, "TEXT(64) CHARACTER SET latin1 COLLATE latin1_bin", NOT_REQUIRED}},
312 };
313
314 INSTANTIATE_TEST_SUITE_P(add_column, Index_field_add_column_test,
315 ::testing::ValuesIn(add_column_param));
316
317 class Index_field_is_column_exists_test : public ::testing::Test {
318 public:
319 void SetUp() override {
320 ngs::Error_code error;
321 field.reset(
322 Index_field::create(true, Field_info(PATH, "int", REQUIRED), &error));
323 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
324 }
325
326 using Sql = ngs::PFS_string;
327 ::testing::StrictMock<mock::Sql_session> data_context;
328 std::unique_ptr<const Index_field> field;
329 };
330
331 TEST_F(Index_field_is_column_exists_test, column_is_not_exist) {
332 EXPECT_CALL(data_context,
333 execute(Eq(Sql(SHOW_COLUMNS("$ix_i_r_" PATH_HASH))), _, _))
334 .WillOnce(Return(ngs::Success()));
335 ngs::Error_code error;
336 ASSERT_FALSE(
337 field->is_column_exists(&data_context, "schema", "collection", &error));
338 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
339 }
340
341 TEST_F(Index_field_is_column_exists_test, column_is_not_exist_error) {
342 EXPECT_CALL(data_context,
343 execute(Eq(Sql(SHOW_COLUMNS("$ix_i_r_" PATH_HASH))), _, _))
344 .WillOnce(Return(ngs::Error(ER_X_ARTIFICIAL1, "internal error")));
345 ngs::Error_code error;
346 ASSERT_FALSE(
347 field->is_column_exists(&data_context, "schema", "collection", &error));
348 ASSERT_ERROR_CODE(ER_X_ARTIFICIAL1, error);
349 }
350
351 TEST_F(Index_field_is_column_exists_test, column_is_exist) {
352 One_row_resultset data{"anything"};
353 EXPECT_CALL(data_context,
354 execute(Eq(Sql(SHOW_COLUMNS("$ix_i_r_" PATH_HASH))), _, _))
355 .WillOnce(DoAll(SetUpResultset(data), Return(ngs::Success())));
356 ngs::Error_code error;
357 ASSERT_TRUE(
358 field->is_column_exists(&data_context, "schema", "collection", &error));
359 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
360 }
361 } // namespace test
362 } // namespace xpl