| 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_array_field.h" |
| 32 | #include "plugin/x/src/query_string_builder.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/assert_error_code.h" |
| 34 | |
| 35 | namespace xpl { |
| 36 | namespace test { |
| 37 | |
| 38 | using ::testing::_; |
| 39 | using ::testing::DoAll; |
| 40 | using ::testing::Eq; |
| 41 | using ::testing::Return; |
| 42 | |
| 43 | namespace { |
| 44 | #define PATH "$.path" |
| 45 | #define NOT_REQUIRED false |
| 46 | #define REQUIRED true |
| 47 | #define OPTIONS 42u |
| 48 | #define SRID 666u |
| 49 | #define DEFAULT_VALUE std::numeric_limits<uint64_t>::max() |
| 50 | #define CAST_TO_ARRAY(type) \ |
| 51 | "(CAST(JSON_EXTRACT(`doc`,'" PATH "') AS " type " ARRAY))" |
| 52 | |
| 53 | struct Array_field_info : public Admin_command_index::Index_field_info { |
| 54 | Array_field_info( |
| 55 | const std::string &path, const std::string &type, |
| 56 | const bool is_required = false, |
| 57 | const uint64_t options = std::numeric_limits<uint64_t>::max(), |
| 58 | const uint64_t srid = std::numeric_limits<uint64_t>::max()) { |
| 59 | m_path = path; |
| 60 | m_type = type; |
| 61 | m_is_required = is_required; |
| 62 | m_options = options; |
| 63 | m_srid = srid; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | class Index_array_field_create_test |
| 70 | : public ::testing::TestWithParam<Array_field_info> {}; |
| 71 | |
| 72 | TEST_P(Index_array_field_create_test, fail_on_create) { |
| 73 | const auto ¶m = GetParam(); |
| 74 | ngs::Error_code error; |
| 75 | std::unique_ptr<const Index_array_field> field( |
| 76 | Index_array_field::create(param, &error)); |
| 77 | ASSERT_ERROR_CODE(ER_X_CMD_ARGUMENT_VALUE, error); |
| 78 | ASSERT_EQ(nullptr, field.get()); |
| 79 | } |
| 80 | |
| 81 | Array_field_info fail_on_create_array_field_param[] = { |
| 82 | {"", "DECIMAL"}, |
| 83 | {PATH, ""}, |
| 84 | {PATH, "DECIMAL", REQUIRED, DEFAULT_VALUE, DEFAULT_VALUE}, |
| 85 | {PATH, "DECIMAL", NOT_REQUIRED, OPTIONS, DEFAULT_VALUE}, |
| 86 | {PATH, "DECIMAL", NOT_REQUIRED, DEFAULT_VALUE, SRID}, |
| 87 | {PATH, "DECIMAL SIGNED"}, |
| 88 | {PATH, "DECIMAL UNSIGNED"}, |
| 89 | {PATH, "tinyint"}, |
| 90 | {PATH, "smallint"}, |
| 91 | {PATH, "mediumint"}, |
| 92 | {PATH, "int"}, |
| 93 | {PATH, "integer"}, |
| 94 | {PATH, "bigint"}, |
| 95 | {PATH, "real"}, |
| 96 | {PATH, "float"}, |
| 97 | {PATH, "double"}, |
| 98 | {PATH, "numeric"}, |
| 99 | {PATH, "date(10)"}, |
| 100 | {PATH, "date(10,2)"}, |
| 101 | {PATH, "date unsigned"}, |
| 102 | {PATH, "time(10)"}, |
| 103 | {PATH, "time(10,2)"}, |
| 104 | {PATH, "time unsigned"}, |
| 105 | {PATH, "timestamp"}, |
| 106 | {PATH, "datetime(10)"}, |
| 107 | {PATH, "datetime(10,2)"}, |
| 108 | {PATH, "datetime unsigned"}, |
| 109 | {PATH, "year"}, |
| 110 | {PATH, "bit"}, |
| 111 | {PATH, "blob"}, |
| 112 | {PATH, "text"}, |
| 113 | {PATH, "geojson"}, |
| 114 | {PATH, "fulltext"}, |
| 115 | }; |
| 116 | |
| 117 | INSTANTIATE_TEST_SUITE_P(fail_on_create_field, Index_array_field_create_test, |
| 118 | ::testing::ValuesIn(fail_on_create_array_field_param)); |
| 119 | |
| 120 | struct Param_index_array_field_add_field { |
| 121 | std::string expect; |
| 122 | Array_field_info info; |
| 123 | }; |
| 124 | |
| 125 | class Index_array_field_add_field_test |
| 126 | : public ::testing::TestWithParam<Param_index_array_field_add_field> {}; |
| 127 | |
| 128 | TEST_P(Index_array_field_add_field_test, add_field) { |
| 129 | const auto ¶m = GetParam(); |
| 130 | Query_string_builder qb; |
| 131 | ngs::Error_code error; |
| 132 | std::unique_ptr<const Index_array_field> field( |
| 133 | Index_array_field::create(param.info, &error)); |
| 134 | ASSERT_NE(nullptr, field.get()); |
| 135 | field->add_field(&qb); |
| 136 | ASSERT_STREQ(param.expect.c_str(), qb.get().c_str()); |
| 137 | } |
| 138 | |
| 139 | Param_index_array_field_add_field add_array_field_param[] = { |
| 140 | {CAST_TO_ARRAY("BINARY"), {PATH, "BINARY"}}, |
| 141 | {CAST_TO_ARRAY("binary(20)"), {PATH, "binary(20)"}}, |
| 142 | {CAST_TO_ARRAY("date"), {PATH, "date"}}, |
| 143 | {CAST_TO_ARRAY("time"), {PATH, "time"}}, |
| 144 | {CAST_TO_ARRAY("datetime"), {PATH, "datetime"}}, |
| 145 | {CAST_TO_ARRAY("char"), {PATH, "char"}}, |
| 146 | {CAST_TO_ARRAY("char(20)"), {PATH, "char(20)"}}, |
| 147 | // {CAST_TO_ARRAY("char(30) character set latin1"), |
| 148 | // {PATH, "char(30) character set latin1"}}, |
| 149 | // {CAST_TO_ARRAY("char(40) collate latin1_bin"), |
| 150 | // {PATH, "char(40) collate latin1_bin"}}, |
| 151 | // {CAST_TO_ARRAY("char(50) character set latin1 collate latin1_bin"), |
| 152 | // {PATH, "char(50) character set latin1 collate latin1_bin"}}, |
| 153 | {CAST_TO_ARRAY("decimal"), {PATH, "decimal"}}, |
| 154 | {CAST_TO_ARRAY("DEcimAL"), {PATH, "DEcimAL"}}, |
| 155 | {CAST_TO_ARRAY("DECIMAL(32)"), {PATH, "DECIMAL(32)"}}, |
| 156 | {CAST_TO_ARRAY("DECIMAL(32,16)"), {PATH, "DECIMAL(32,16)"}}, |
| 157 | {CAST_TO_ARRAY("DECIMAL(0,16)"), {PATH, "DECIMAL(0,16)"}}, |
| 158 | {CAST_TO_ARRAY("unsigned"), {PATH, "unsigned"}}, |
| 159 | {CAST_TO_ARRAY("unsigned integer"), {PATH, "unsigned integer"}}, |
| 160 | {CAST_TO_ARRAY("signed"), {PATH, "signed"}}, |
| 161 | {CAST_TO_ARRAY("signed integer"), {PATH, "signed integer"}}, |
| 162 | }; |
| 163 | |
| 164 | INSTANTIATE_TEST_SUITE_P(get_index_field_name, Index_array_field_add_field_test, |
| 165 | ::testing::ValuesIn(add_array_field_param)); |
| 166 | |
| 167 | } // namespace test |
| 168 | } // namespace xpl |