From 3a0d358694a781bd9399d7a14ca79102bbb34ef7 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Fri, 10 Jul 2020 17:02:48 +0300 Subject: [PATCH 1/2] Allow typeid_cast() to cast nullptr to nullptr. --- src/Common/typeid_cast.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/typeid_cast.h b/src/Common/typeid_cast.h index f28271fb53b..ca145fcc3b8 100644 --- a/src/Common/typeid_cast.h +++ b/src/Common/typeid_cast.h @@ -47,7 +47,7 @@ std::enable_if_t, To> typeid_cast(From * from) { try { - if ((typeid(From) == typeid(std::remove_pointer_t)) || (typeid(*from) == typeid(std::remove_pointer_t))) + if ((typeid(From) == typeid(std::remove_pointer_t)) || (from && typeid(*from) == typeid(std::remove_pointer_t))) return static_cast(from); else return nullptr; @@ -64,7 +64,7 @@ std::enable_if_t, To> typeid_cast(const std::shared_ptr { try { - if ((typeid(From) == typeid(typename To::element_type)) || (typeid(*from) == typeid(typename To::element_type))) + if ((typeid(From) == typeid(typename To::element_type)) || (from && typeid(*from) == typeid(typename To::element_type))) return std::static_pointer_cast(from); else return nullptr; From 94c858b2dc255c69ed502869640f583979fadfb6 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Fri, 10 Jul 2020 17:04:20 +0300 Subject: [PATCH 2/2] Fix std::bad_typeid when JSON functions called with argument of wrong type. --- src/Functions/FunctionsJSON.h | 4 ++-- tests/queries/0_stateless/00918_json_functions.reference | 2 ++ tests/queries/0_stateless/00918_json_functions.sql | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Functions/FunctionsJSON.h b/src/Functions/FunctionsJSON.h index e574149f2e9..f03473fe5ec 100644 --- a/src/Functions/FunctionsJSON.h +++ b/src/Functions/FunctionsJSON.h @@ -892,7 +892,7 @@ public: auto col_type_const = typeid_cast(col.column.get()); if (!col_type_const || !isString(col.type)) throw Exception{"The last argument of function " + String(function_name) - + " should be a constant string specifying the return data type, illegal value: " + col.column->getName(), + + " should be a constant string specifying the return data type, illegal value: " + col.name, ErrorCodes::ILLEGAL_COLUMN}; return DataTypeFactory::instance().get(col_type_const->getValue()); @@ -929,7 +929,7 @@ public: auto col_type_const = typeid_cast(col.column.get()); if (!col_type_const || !isString(col.type)) throw Exception{"The last argument of function " + String(function_name) - + " should be a constant string specifying the values' data type, illegal value: " + col.column->getName(), + + " should be a constant string specifying the values' data type, illegal value: " + col.name, ErrorCodes::ILLEGAL_COLUMN}; DataTypePtr key_type = std::make_unique(); diff --git a/tests/queries/0_stateless/00918_json_functions.reference b/tests/queries/0_stateless/00918_json_functions.reference index b83cbe17a6e..a3beb2967d4 100644 --- a/tests/queries/0_stateless/00918_json_functions.reference +++ b/tests/queries/0_stateless/00918_json_functions.reference @@ -99,6 +99,7 @@ d e u v +--show error: type should be const string --allow_simdjson=0-- --JSONLength-- 2 @@ -200,3 +201,4 @@ d e u v +--show error: type should be const string diff --git a/tests/queries/0_stateless/00918_json_functions.sql b/tests/queries/0_stateless/00918_json_functions.sql index 7a030d3dab6..dbaa5b6a80a 100644 --- a/tests/queries/0_stateless/00918_json_functions.sql +++ b/tests/queries/0_stateless/00918_json_functions.sql @@ -108,6 +108,9 @@ SELECT '--const/non-const mixed--'; SELECT JSONExtractString('["a", "b", "c", "d", "e"]', idx) FROM (SELECT arrayJoin([1,2,3,4,5]) AS idx); SELECT JSONExtractString(json, 's') FROM (SELECT arrayJoin(['{"s":"u"}', '{"s":"v"}']) AS json); +SELECT '--show error: type should be const string'; +SELECT JSONExtractKeysAndValues([], JSONLength('^?V{LSwp')); -- { serverError 44 } +WITH '{"i": 1, "f": 1.2}' AS json SELECT JSONExtract(json, 'i', JSONType(json, 'i')); -- { serverError 44 } SELECT '--allow_simdjson=0--'; @@ -219,3 +222,7 @@ SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300], "c": SELECT '--const/non-const mixed--'; SELECT JSONExtractString('["a", "b", "c", "d", "e"]', idx) FROM (SELECT arrayJoin([1,2,3,4,5]) AS idx); SELECT JSONExtractString(json, 's') FROM (SELECT arrayJoin(['{"s":"u"}', '{"s":"v"}']) AS json); + +SELECT '--show error: type should be const string'; +SELECT JSONExtractKeysAndValues([], JSONLength('^?V{LSwp')); -- { serverError 44 } +WITH '{"i": 1, "f": 1.2}' AS json SELECT JSONExtract(json, 'i', JSONType(json, 'i')); -- { serverError 44 }