| 1 | /* Copyright (c) 2021, 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 <cstddef> |
| 26 | |
| 27 | #include "plugin/x/src/expr_generator.h" |
| 28 | #include "unittest/gunit/xplugin/xpl/message_helpers.h" |
| 29 | #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h" |
| 30 | |
| 31 | namespace xpl { |
| 32 | namespace test { |
| 33 | |
| 34 | const char *const EMPTY_SCHEMA = ""; |
| 35 | enum { DM_DOCUMENT = 0, DM_TABLE = 1 }; |
| 36 | |
| 37 | using Octets = Scalar::Octets; |
| 38 | |
| 39 | struct Param_function_call { |
| 40 | std::string expect; |
| 41 | Function_call func; |
| 42 | std::string schema; |
| 43 | }; |
| 44 | |
| 45 | class Function_call_test : public testing::TestWithParam<Param_function_call> { |
| 46 | }; |
| 47 | |
| 48 | TEST_P(Function_call_test, function_call) { |
| 49 | const Param_function_call ¶m = GetParam(); |
| 50 | EXPECT_STREQ(param.expect.c_str(), |
| 51 | generate_expression(param.func, param.schema, DM_TABLE).c_str()); |
| 52 | } |
| 53 | |
| 54 | Param_function_call function_call_param[] = { |
| 55 | {"func()", Function_call("func"), EMPTY_SCHEMA}, |
| 56 | {"schema.func()", Function_call("func"), "schema"}, |
| 57 | {"schema.func(FALSE,5)", Function_call("func", false, 5), "schema"}, |
| 58 | {"concat(FALSE,5)", Function_call("concat", false, 5), "schema"}, |
| 59 | {"CONCAT(FALSE,5)", Function_call("CONCAT", false, 5), "schema"}, |
| 60 | {"CONCAT(FALSE,5)", Function_call("CONCAT", false, 5), EMPTY_SCHEMA}, |
| 61 | {"ASCII('string')", Function_call("ASCII", "string"), EMPTY_SCHEMA}, |
| 62 | {"ASCII(`column`)", Function_call("ASCII", Column_identifier("column")), |
| 63 | EMPTY_SCHEMA}, |
| 64 | {"ASCII(JSON_UNQUOTE(JSON_EXTRACT(doc,'$.path')))", |
| 65 | Function_call("ASCII", Column_identifier(Document_path{"path"})), |
| 66 | EMPTY_SCHEMA}, |
| 67 | {"ABS(42)", Function_call("ABS", 42), EMPTY_SCHEMA}, |
| 68 | {"ABS(`column`)", Function_call("ABS", Column_identifier("column")), |
| 69 | EMPTY_SCHEMA}, |
| 70 | {"ABS(JSON_UNQUOTE(JSON_EXTRACT(doc,'$.path')))", |
| 71 | Function_call("ABS", Column_identifier(Document_path{"path"})), |
| 72 | EMPTY_SCHEMA}, |
| 73 | {"JSON_TYPE(42)", Function_call("JSON_TYPE", 42), EMPTY_SCHEMA}, |
| 74 | {"JSON_TYPE(`column`)", |
| 75 | Function_call("JSON_TYPE", Column_identifier("column")), EMPTY_SCHEMA}, |
| 76 | {"JSON_TYPE(JSON_EXTRACT(doc,'$.path'))", |
| 77 | Function_call("JSON_TYPE", Column_identifier(Document_path{"path"})), |
| 78 | EMPTY_SCHEMA}, |
| 79 | {"JSON_KEYS('{\\\"a\\\":42}')", Function_call("JSON_KEYS", "{\"a\":42}"), |
| 80 | EMPTY_SCHEMA}, |
| 81 | {"JSON_KEYS(`column`)", |
| 82 | Function_call("JSON_KEYS", Column_identifier("column")), EMPTY_SCHEMA}, |
| 83 | {"JSON_KEYS(JSON_EXTRACT(doc,'$.path'))", |
| 84 | Function_call("JSON_KEYS", Column_identifier(Document_path{"path"})), |
| 85 | EMPTY_SCHEMA}}; |
| 86 | |
| 87 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_function_call, Function_call_test, |
| 88 | testing::ValuesIn(function_call_param)); |
| 89 | |
| 90 | struct Param_placeholders { |
| 91 | std::string expect; |
| 92 | Expression_generator::Prep_stmt_placeholder_list expect_ids; |
| 93 | Expression_list args; |
| 94 | Array expr; |
| 95 | }; |
| 96 | |
| 97 | class Placeholders_test : public testing::TestWithParam<Param_placeholders> {}; |
| 98 | |
| 99 | TEST_P(Placeholders_test, placeholders) { |
| 100 | const Param_placeholders ¶m = GetParam(); |
| 101 | Query_string_builder qb; |
| 102 | Expression_generator gen(&qb, param.args, EMPTY_SCHEMA, DM_TABLE); |
| 103 | Expression_generator::Prep_stmt_placeholder_list ids; |
| 104 | gen.set_prep_stmt_placeholder_list(&ids); |
| 105 | gen.feed(param.expr); |
| 106 | |
| 107 | EXPECT_STREQ(param.expect.c_str(), qb.get().c_str()); |
| 108 | EXPECT_EQ(param.expect_ids, ids); |
| 109 | } |
| 110 | |
| 111 | #define PH Placeholder |
| 112 | |
| 113 | Param_placeholders placeholders_param[] = { |
| 114 | {"JSON_ARRAY(?)", {0}, {}, {PH{0}}}, |
| 115 | {"JSON_ARRAY('a')", {}, {"a"}, {PH{0}}}, |
| 116 | {"JSON_ARRAY(?)", {0}, {"a"}, {PH{1}}}, |
| 117 | {"JSON_ARRAY(?,?)", {0, 0}, {}, {PH{0}, PH{0}}}, |
| 118 | {"JSON_ARRAY(?,?)", {1, 0}, {}, {PH{1}, PH{0}}}, |
| 119 | {"JSON_ARRAY('a',?)", {0}, {"a"}, {PH{0}, PH{1}}}, |
| 120 | {"JSON_ARRAY(?,'a')", {0}, {"a"}, {PH{1}, PH{0}}}, |
| 121 | {"JSON_ARRAY('a','b')", {}, {"a", "b"}, {PH{0}, PH{1}}}, |
| 122 | {"JSON_ARRAY('a','b','a')", {}, {"a", "b"}, {PH{0}, PH{1}, PH{0}}}, |
| 123 | {"JSON_ARRAY('a','b',?)", {0}, {"a", "b"}, {PH{0}, PH{1}, PH{2}}}, |
| 124 | {"JSON_ARRAY('a',?,'b')", {0}, {"a", "b"}, {PH{0}, PH{2}, PH{1}}}, |
| 125 | {"JSON_ARRAY(?,'a','b')", {0}, {"a", "b"}, {PH{2}, PH{0}, PH{1}}}, |
| 126 | {"JSON_ARRAY(?,'a',?,'b',?)", |
| 127 | {0, 0, 0}, |
| 128 | {"a", "b"}, |
| 129 | {PH{2}, PH{0}, PH{2}, PH{1}, PH{2}}}, |
| 130 | {"JSON_ARRAY(?,'a',?,'b',?)", |
| 131 | {0, 1, 0}, |
| 132 | {"a", "b"}, |
| 133 | {PH{2}, PH{0}, PH{3}, PH{1}, PH{2}}}, |
| 134 | }; |
| 135 | |
| 136 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_placeholders, Placeholders_test, |
| 137 | testing::ValuesIn(placeholders_param)); |
| 138 | |
| 139 | struct Param_operator_pass { |
| 140 | std::string expect; |
| 141 | Operator operator_; |
| 142 | Expression_list args; |
| 143 | }; |
| 144 | |
| 145 | class Operator_pass_test |
| 146 | : public testing::TestWithParam<std::shared_ptr<Param_operator_pass>> {}; |
| 147 | |
| 148 | TEST_P(Operator_pass_test, operator_pass) { |
| 149 | const auto ¶m = *GetParam(); |
| 150 | EXPECT_STREQ( |
| 151 | param.expect.c_str(), |
| 152 | generate_expression(param.operator_, param.args, EMPTY_SCHEMA, DM_TABLE) |
| 153 | .c_str()); |
| 154 | } |
| 155 | |
| 156 | std::shared_ptr<Param_operator_pass> make_op_pass(std::string &&expect, |
| 157 | Operator &&operator_, |
| 158 | Expression_list &&args) { |
| 159 | return std::shared_ptr<Param_operator_pass>{new Param_operator_pass{ |
| 160 | std::move(expect), std::move(operator_), std::move(args)}}; |
| 161 | } |
| 162 | std::shared_ptr<Param_operator_pass> cont_in_pass_param[] = { |
| 163 | // literals |
| 164 | make_op_pass("JSON_CONTAINS(CAST(1 AS JSON),CAST(2 AS JSON))", |
| 165 | Operator("cont_in", 2, 1), {}), |
| 166 | make_op_pass("JSON_CONTAINS(CAST(1.2 AS JSON),CAST(2.1 AS JSON))", |
| 167 | Operator("cont_in", 2.1, 1.2), {}), |
| 168 | make_op_pass("JSON_CONTAINS(CAST(FALSE AS JSON),CAST(TRUE AS JSON))", |
| 169 | Operator("cont_in", true, false), {}), |
| 170 | make_op_pass("JSON_CONTAINS(CAST('null' AS JSON),CAST('null' AS JSON))", |
| 171 | Operator("cont_in", Scalar::Null(), Scalar::Null()), {}), |
| 172 | make_op_pass( |
| 173 | "JSON_CONTAINS(JSON_QUOTE('white'),JSON_QUOTE('black'))", |
| 174 | Operator("cont_in", Scalar::String("black"), Scalar::String("white")), |
| 175 | {}), |
| 176 | make_op_pass( |
| 177 | "JSON_CONTAINS(JSON_QUOTE('white'),JSON_QUOTE('black'))", |
| 178 | Operator("cont_in", Octets("black", Octets::Content_type::k_plain), |
| 179 | Octets("white", Octets::Content_type::k_plain)), |
| 180 | {}), |
| 181 | make_op_pass( |
| 182 | "JSON_CONTAINS(CAST('{\\\"white\\\":2}' AS JSON)," |
| 183 | "CAST('{\\\"black\\\":1}' AS JSON))", |
| 184 | Operator("cont_in", |
| 185 | Octets("{\"black\":1}", Octets::Content_type::k_json), |
| 186 | Octets("{\"white\":2}", Octets::Content_type::k_json)), |
| 187 | {}), |
| 188 | make_op_pass( |
| 189 | "JSON_CONTAINS(JSON_QUOTE('<a>white</a>'),JSON_QUOTE('<a>black</a>'))", |
| 190 | Operator("cont_in", Octets("<a>black</a>", Octets::Content_type::k_xml), |
| 191 | Octets("<a>white</a>", Octets::Content_type::k_xml)), |
| 192 | {}), |
| 193 | make_op_pass( |
| 194 | "JSON_CONTAINS(JSON_QUOTE(ST_GEOMETRYFROMWKB('101'))," |
| 195 | "JSON_QUOTE(ST_GEOMETRYFROMWKB('010')))", |
| 196 | Operator("cont_in", Octets("010", Octets::Content_type::k_geometry), |
| 197 | Octets("101", Octets::Content_type::k_geometry)), |
| 198 | {}), |
| 199 | // arrays |
| 200 | make_op_pass("JSON_CONTAINS(JSON_ARRAY(3,4),JSON_ARRAY(1,2))", |
| 201 | Operator("cont_in", Array{1, 2}, Array{3, 4}), {}), |
| 202 | make_op_pass( |
| 203 | "JSON_CONTAINS(JSON_ARRAY(3,FALSE,'white'),JSON_ARRAY(1,TRUE,'black')" |
| 204 | ")", |
| 205 | Operator("cont_in", Array{1, true, "black"}, Array{3, false, "white"}), |
| 206 | {}), |
| 207 | make_op_pass( |
| 208 | "JSON_CONTAINS(JSON_ARRAY(CAST('{\\\"white\\\":2}' AS JSON))," |
| 209 | "JSON_ARRAY(CAST('{\\\"black\\\":1}' AS JSON)))", |
| 210 | Operator("cont_in", |
| 211 | Array{Octets("{\"black\":1}", Octets::Content_type::k_json)}, |
| 212 | Array{Octets("{\"white\":2}", Octets::Content_type::k_json)}), |
| 213 | {}), |
| 214 | // objects |
| 215 | make_op_pass( |
| 216 | "JSON_CONTAINS(JSON_OBJECT('second',2),JSON_OBJECT('first',1))", |
| 217 | Operator("cont_in", Object{{"first", 1}}, Object{{"second", 2}}), {}), |
| 218 | make_op_pass( |
| 219 | "JSON_CONTAINS(JSON_OBJECT('second',CAST('{\\\"white\\\":2}' AS " |
| 220 | "JSON))," |
| 221 | "JSON_OBJECT('first',CAST('{\\\"black\\\":1}' AS JSON)))", |
| 222 | Operator("cont_in", |
| 223 | Object{{"first", Octets("{\"black\":1}", |
| 224 | Octets::Content_type::k_json)}}, |
| 225 | Object{{"second", Octets("{\"white\":2}", |
| 226 | Octets::Content_type::k_json)}}), |
| 227 | {}), |
| 228 | make_op_pass( |
| 229 | "JSON_CONTAINS(CAST((2 - 1) AS JSON),CAST((1 + 2) AS JSON))", |
| 230 | Operator("cont_in", |
| 231 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 232 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 233 | {}), |
| 234 | // functions |
| 235 | make_op_pass( |
| 236 | "JSON_CONTAINS(json_quote(concat('foo','bar'))," |
| 237 | "json_quote(concat('foo','bar')))", |
| 238 | Operator( |
| 239 | "cont_in", |
| 240 | Function_call("json_quote", Function_call("concat", "foo", "bar")), |
| 241 | Function_call("json_quote", Function_call("concat", "foo", "bar"))), |
| 242 | {}), |
| 243 | // placeholders |
| 244 | make_op_pass("JSON_CONTAINS(CAST(2 AS JSON),CAST(1 AS JSON))", |
| 245 | Operator("cont_in", Placeholder(0), Placeholder(1)), {1, 2}), |
| 246 | make_op_pass("JSON_CONTAINS(JSON_QUOTE('bar'),JSON_QUOTE('foo'))", |
| 247 | Operator("cont_in", Placeholder(0), Placeholder(1)), |
| 248 | {"foo", "bar"}), |
| 249 | make_op_pass("JSON_CONTAINS(CAST('{\\\"white\\\":2}' AS JSON)," |
| 250 | "CAST('{\\\"black\\\":1}' AS JSON))", |
| 251 | Operator("cont_in", Placeholder(0), Placeholder(1)), |
| 252 | {Octets("{\"black\":1}", Octets::Content_type::k_json), |
| 253 | Octets("{\"white\":2}", Octets::Content_type::k_json)}), |
| 254 | // identifier |
| 255 | make_op_pass("JSON_CONTAINS(CAST(42 AS JSON)," |
| 256 | "JSON_EXTRACT(`schema`.`table`.`field`,'$.member'))", |
| 257 | Operator("cont_in", |
| 258 | Column_identifier(Document_path{"member"}, "field", |
| 259 | "table", "schema"), |
| 260 | 42), |
| 261 | {}), |
| 262 | make_op_pass( |
| 263 | "JSON_CONTAINS(JSON_EXTRACT(`schema`.`table`.`field`,'$.member')," |
| 264 | "CAST(42 AS JSON))", |
| 265 | Operator("cont_in", 42, |
| 266 | Column_identifier(Document_path{"member"}, "field", "table", |
| 267 | "schema")), |
| 268 | {}), |
| 269 | make_op_pass( |
| 270 | "JSON_CONTAINS(`schema`.`table`.`field`,CAST(42 AS JSON))", |
| 271 | Operator("cont_in", 42, Column_identifier("field", "table", "schema")), |
| 272 | {}), |
| 273 | }; |
| 274 | |
| 275 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_cont_in_pass, Operator_pass_test, |
| 276 | testing::ValuesIn(cont_in_pass_param)); |
| 277 | |
| 278 | struct Param_operator_fail { |
| 279 | Operator operator_; |
| 280 | Expression_list args; |
| 281 | }; |
| 282 | |
| 283 | class Operator_fail_test |
| 284 | : public testing::TestWithParam<std::shared_ptr<Param_operator_fail>> {}; |
| 285 | |
| 286 | TEST_P(Operator_fail_test, operator_fail) { |
| 287 | const auto ¶m = *GetParam(); |
| 288 | EXPECT_THROW( |
| 289 | generate_expression(param.operator_, param.args, EMPTY_SCHEMA, DM_TABLE), |
| 290 | Expression_generator::Error) |
| 291 | << "Should throw for: " << msg_to_string(param.operator_.base()); |
| 292 | } |
| 293 | |
| 294 | template <typename... Arguments> |
| 295 | std::shared_ptr<Param_operator_fail> make_op_fail(Operator &&op, |
| 296 | Expression_list &&list) { |
| 297 | return std::shared_ptr<Param_operator_fail>{ |
| 298 | new Param_operator_fail{std::move(op), std::move(list)}}; |
| 299 | } |
| 300 | |
| 301 | std::shared_ptr<Param_operator_fail> cont_in_fail_param[] = { |
| 302 | // literals |
| 303 | // arrays |
| 304 | // objects |
| 305 | // operators |
| 306 | make_op_fail(Operator("cont_in", Operator("+", 1, 2), Operator("-", 2, 1)), |
| 307 | {}), |
| 308 | make_op_fail( |
| 309 | Operator("cont_in", Operator("+", 1, 2), |
| 310 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 311 | {}), |
| 312 | make_op_fail(Operator("cont_in", |
| 313 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 314 | Operator("-", 2, 1)), |
| 315 | {}), |
| 316 | make_op_fail( |
| 317 | Operator("cont_in", |
| 318 | Operator("cast", Operator("+", 1, 2), Octets("SIGNED")), |
| 319 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 320 | {}), |
| 321 | make_op_fail( |
| 322 | Operator("cont_in", |
| 323 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 324 | Operator("cast", Operator("-", 2, 1), Octets("SIGNED"))), |
| 325 | {}), |
| 326 | // functions |
| 327 | make_op_fail(Operator("cont_in", Function_call("concat", "foo", "bar"), |
| 328 | Function_call("concat", "foo", "bar")), |
| 329 | {}), |
| 330 | make_op_fail(Operator("cont_in", Function_call("concat", "foo", "bar"), |
| 331 | Function_call("json_quote", |
| 332 | Function_call("concat", "foo", "bar"))), |
| 333 | {}), |
| 334 | make_op_fail(Operator("cont_in", |
| 335 | Function_call("json_quote", |
| 336 | Function_call("concat", "foo", "bar")), |
| 337 | Function_call("concat", "foo", "bar")), |
| 338 | {}), |
| 339 | // placeholders |
| 340 | make_op_fail(Operator("cont_in", Placeholder(0), Placeholder(1)), {}), |
| 341 | // identifier |
| 342 | }; |
| 343 | |
| 344 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_cont_in_fail, Operator_fail_test, |
| 345 | testing::ValuesIn(cont_in_fail_param)); |
| 346 | |
| 347 | std::shared_ptr<Param_operator_pass> overlaps_pass_param[] = { |
| 348 | // literals |
| 349 | make_op_pass("JSON_OVERLAPS(CAST(2 AS JSON),CAST(1 AS JSON))", |
| 350 | Operator("overlaps", 2, 1), {}), |
| 351 | make_op_pass("JSON_OVERLAPS(CAST(2.1 AS JSON),CAST(1.2 AS JSON))", |
| 352 | Operator("overlaps", 2.1, 1.2), {}), |
| 353 | make_op_pass("JSON_OVERLAPS(CAST(TRUE AS JSON),CAST(FALSE AS JSON))", |
| 354 | Operator("overlaps", true, false), {}), |
| 355 | make_op_pass("JSON_OVERLAPS(CAST('null' AS JSON),CAST('null' AS JSON))", |
| 356 | Operator("overlaps", Scalar::Null(), Scalar::Null()), {}), |
| 357 | make_op_pass( |
| 358 | "JSON_OVERLAPS(JSON_QUOTE('black'),JSON_QUOTE('white'))", |
| 359 | Operator("overlaps", Scalar::String("black"), Scalar::String("white")), |
| 360 | {}), |
| 361 | make_op_pass( |
| 362 | "JSON_OVERLAPS(JSON_QUOTE('black'),JSON_QUOTE('white'))", |
| 363 | Operator("overlaps", Octets("black", Octets::Content_type::k_plain), |
| 364 | Octets("white", Octets::Content_type::k_plain)), |
| 365 | {}), |
| 366 | make_op_pass( |
| 367 | "JSON_OVERLAPS(" |
| 368 | "CAST('{\\\"black\\\":1}' AS JSON),CAST('{\\\"white\\\":2}' AS JSON))", |
| 369 | Operator("overlaps", |
| 370 | Octets("{\"black\":1}", Octets::Content_type::k_json), |
| 371 | Octets("{\"white\":2}", Octets::Content_type::k_json)), |
| 372 | {}), |
| 373 | make_op_pass( |
| 374 | "JSON_OVERLAPS(JSON_QUOTE('<a>black</a>'),JSON_QUOTE('<a>white</a>'))", |
| 375 | Operator("overlaps", |
| 376 | Octets("<a>black</a>", Octets::Content_type::k_xml), |
| 377 | Octets("<a>white</a>", Octets::Content_type::k_xml)), |
| 378 | {}), |
| 379 | make_op_pass( |
| 380 | "JSON_OVERLAPS(" |
| 381 | "JSON_QUOTE(ST_GEOMETRYFROMWKB('010'))," |
| 382 | "JSON_QUOTE(ST_GEOMETRYFROMWKB('101')))", |
| 383 | Operator("overlaps", Octets("010", Octets::Content_type::k_geometry), |
| 384 | Octets("101", Octets::Content_type::k_geometry)), |
| 385 | {}), |
| 386 | // arrays |
| 387 | make_op_pass("JSON_OVERLAPS(JSON_ARRAY(1,2),JSON_ARRAY(3,4))", |
| 388 | Operator("overlaps", Array{1, 2}, Array{3, 4}), {}), |
| 389 | make_op_pass( |
| 390 | "JSON_OVERLAPS(JSON_ARRAY(1,TRUE,'black'),JSON_ARRAY(3,FALSE," |
| 391 | "'white'))", |
| 392 | Operator("overlaps", Array{1, true, "black"}, Array{3, false, "white"}), |
| 393 | {}), |
| 394 | make_op_pass( |
| 395 | "JSON_OVERLAPS(" |
| 396 | "JSON_ARRAY(CAST('{\\\"black\\\":1}' AS JSON))," |
| 397 | "JSON_ARRAY(CAST('{\\\"white\\\":2}' AS JSON)))", |
| 398 | Operator("overlaps", |
| 399 | Array{Octets("{\"black\":1}", Octets::Content_type::k_json)}, |
| 400 | Array{Octets("{\"white\":2}", Octets::Content_type::k_json)}), |
| 401 | {}), |
| 402 | // objects |
| 403 | make_op_pass( |
| 404 | "JSON_OVERLAPS(JSON_OBJECT('first',1),JSON_OBJECT('second',2))", |
| 405 | Operator("overlaps", Object{{"first", 1}}, Object{{"second", 2}}), {}), |
| 406 | make_op_pass( |
| 407 | "JSON_OVERLAPS(" |
| 408 | "JSON_OBJECT('first',CAST('{\\\"black\\\":1}' AS JSON))," |
| 409 | "JSON_OBJECT('second',CAST('{\\\"white\\\":2}' AS JSON)))", |
| 410 | Operator("overlaps", |
| 411 | Object{{"first", Octets("{\"black\":1}", |
| 412 | Octets::Content_type::k_json)}}, |
| 413 | Object{{"second", Octets("{\"white\":2}", |
| 414 | Octets::Content_type::k_json)}}), |
| 415 | {}), |
| 416 | make_op_pass( |
| 417 | "JSON_OVERLAPS(CAST((1 + 2) AS JSON),CAST((2 - 1) AS JSON))", |
| 418 | Operator("overlaps", |
| 419 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 420 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 421 | {}), |
| 422 | // functions |
| 423 | make_op_pass( |
| 424 | "JSON_OVERLAPS(" |
| 425 | "json_quote(concat('foo','bar'))," |
| 426 | "json_quote(concat('foo','bar')))", |
| 427 | Operator( |
| 428 | "overlaps", |
| 429 | Function_call("json_quote", Function_call("concat", "foo", "bar")), |
| 430 | Function_call("json_quote", Function_call("concat", "foo", "bar"))), |
| 431 | {}), |
| 432 | // placeholders |
| 433 | make_op_pass("JSON_OVERLAPS(CAST(1 AS JSON),CAST(2 AS JSON))", |
| 434 | Operator("overlaps", Placeholder(0), Placeholder(1)), {1, 2}), |
| 435 | make_op_pass("JSON_OVERLAPS(JSON_QUOTE('foo'),JSON_QUOTE('bar'))", |
| 436 | Operator("overlaps", Placeholder(0), Placeholder(1)), |
| 437 | {"foo", "bar"}), |
| 438 | make_op_pass("JSON_OVERLAPS(" |
| 439 | "CAST('{\\\"black\\\":1}' AS JSON)," |
| 440 | "CAST('{\\\"white\\\":2}' AS JSON))", |
| 441 | Operator("overlaps", Placeholder(0), Placeholder(1)), |
| 442 | {Octets("{\"black\":1}", Octets::Content_type::k_json), |
| 443 | Octets("{\"white\":2}", Octets::Content_type::k_json)}), |
| 444 | // identifier |
| 445 | make_op_pass("JSON_OVERLAPS(" |
| 446 | "JSON_EXTRACT(`schema`.`table`.`field`,'$.member')," |
| 447 | "CAST(42 AS JSON))", |
| 448 | Operator("overlaps", |
| 449 | Column_identifier(Document_path{"member"}, "field", |
| 450 | "table", "schema"), |
| 451 | 42), |
| 452 | {}), |
| 453 | make_op_pass("JSON_OVERLAPS(" |
| 454 | "CAST(42 AS JSON)," |
| 455 | "JSON_EXTRACT(`schema`.`table`.`field`,'$.member'))", |
| 456 | Operator("overlaps", 42, |
| 457 | Column_identifier(Document_path{"member"}, "field", |
| 458 | "table", "schema")), |
| 459 | {}), |
| 460 | make_op_pass( |
| 461 | "JSON_OVERLAPS(" |
| 462 | "CAST(42 AS JSON)," |
| 463 | "`schema`.`table`.`field`)", |
| 464 | Operator("overlaps", 42, Column_identifier("field", "table", "schema")), |
| 465 | {}), |
| 466 | }; |
| 467 | |
| 468 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_overlaps_pass, Operator_pass_test, |
| 469 | testing::ValuesIn(overlaps_pass_param)); |
| 470 | |
| 471 | std::shared_ptr<Param_operator_fail> overlaps_fail_param[] = { |
| 472 | // literals |
| 473 | // arrays |
| 474 | // objects |
| 475 | // operators |
| 476 | make_op_fail(Operator("overlaps", Operator("+", 1, 2), Operator("-", 2, 1)), |
| 477 | {}), |
| 478 | make_op_fail( |
| 479 | Operator("overlaps", Operator("+", 1, 2), |
| 480 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 481 | {}), |
| 482 | make_op_fail(Operator("overlaps", |
| 483 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 484 | Operator("-", 2, 1)), |
| 485 | {}), |
| 486 | make_op_fail( |
| 487 | Operator("overlaps", |
| 488 | Operator("cast", Operator("+", 1, 2), Octets("SIGNED")), |
| 489 | Operator("cast", Operator("-", 2, 1), Octets("JSON"))), |
| 490 | {}), |
| 491 | make_op_fail( |
| 492 | Operator("overlaps", |
| 493 | Operator("cast", Operator("+", 1, 2), Octets("JSON")), |
| 494 | Operator("cast", Operator("-", 2, 1), Octets("SIGNED"))), |
| 495 | {}), |
| 496 | // functions |
| 497 | make_op_fail(Operator("overlaps", Function_call("concat", "foo", "bar"), |
| 498 | Function_call("concat", "foo", "bar")), |
| 499 | {}), |
| 500 | make_op_fail(Operator("overlaps", Function_call("concat", "foo", "bar"), |
| 501 | Function_call("json_quote", |
| 502 | Function_call("concat", "foo", "bar"))), |
| 503 | {}), |
| 504 | make_op_fail(Operator("overlaps", |
| 505 | Function_call("json_quote", |
| 506 | Function_call("concat", "foo", "bar")), |
| 507 | Function_call("concat", "foo", "bar")), |
| 508 | {}), |
| 509 | // placeholders |
| 510 | make_op_fail(Operator("overlaps", Placeholder(0), Placeholder(1)), {}), |
| 511 | // identifier |
| 512 | }; |
| 513 | |
| 514 | INSTANTIATE_TEST_SUITE_P(xpl_expr_generator_overlaps_fail, Operator_fail_test, |
| 515 | testing::ValuesIn(overlaps_fail_param)); |
| 516 | |
| 517 | } // namespace test |
| 518 | } // namespace xpl |