From 1174fc346bba33285a8be96e50d8c947edac46c5 Mon Sep 17 00:00:00 2001 From: VDimir Date: Sat, 12 Oct 2019 11:42:43 +0000 Subject: [PATCH] Add isValidJSON function --- dbms/src/Functions/FunctionsJSON.cpp | 1 + dbms/src/Functions/FunctionsJSON.h | 23 ++++++++++++++++--- .../00918_json_functions.reference | 4 ++++ .../0_stateless/00918_json_functions.sql | 5 ++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/dbms/src/Functions/FunctionsJSON.cpp b/dbms/src/Functions/FunctionsJSON.cpp index 62413ce58d4..eedbecf852d 100644 --- a/dbms/src/Functions/FunctionsJSON.cpp +++ b/dbms/src/Functions/FunctionsJSON.cpp @@ -8,6 +8,7 @@ namespace DB void registerFunctionsJSON(FunctionFactory & factory) { factory.registerFunction>(); + factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); diff --git a/dbms/src/Functions/FunctionsJSON.h b/dbms/src/Functions/FunctionsJSON.h index b9fddf57d39..ee556f480d7 100644 --- a/dbms/src/Functions/FunctionsJSON.h +++ b/dbms/src/Functions/FunctionsJSON.h @@ -279,6 +279,7 @@ private: struct NameJSONHas { static constexpr auto name{"JSONHas"}; }; +struct NameIsValidJSON { static constexpr auto name{"isValidJSON"}; }; struct NameJSONLength { static constexpr auto name{"JSONLength"}; }; struct NameJSONKey { static constexpr auto name{"JSONKey"}; }; struct NameJSONType { static constexpr auto name{"JSONType"}; }; @@ -292,11 +293,23 @@ struct NameJSONExtractKeysAndValues { static constexpr auto name{"JSONExtractKey struct NameJSONExtractRaw { static constexpr auto name{"JSONExtractRaw"}; }; -template -class JSONHasImpl +template +class JSONCheckImpl { public: - static DataTypePtr getType(const char *, const ColumnsWithTypeAndName &) { return std::make_shared(); } + static DataTypePtr getType(const char * function_name, const ColumnsWithTypeAndName & arguments) { + if constexpr (!support_key_lookup) { + if (arguments.size() != 1) + throw Exception{"Function " + String(function_name) + " needs exactly one argument", + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH}; + } + else + { + UNUSED(function_name); + UNUSED(arguments); + } + return std::make_shared(); + } using Iterator = typename JSONParser::Iterator; static bool addValueToColumn(IColumn & dest, const Iterator &) @@ -310,6 +323,10 @@ public: static void prepare(const char *, const Block &, const ColumnNumbers &, size_t) {} }; +template +using JSONHasImpl = JSONCheckImpl; +template +using isValidJSONImpl = JSONCheckImpl; template class JSONLengthImpl diff --git a/dbms/tests/queries/0_stateless/00918_json_functions.reference b/dbms/tests/queries/0_stateless/00918_json_functions.reference index a23b177d468..181da3dd3a0 100644 --- a/dbms/tests/queries/0_stateless/00918_json_functions.reference +++ b/dbms/tests/queries/0_stateless/00918_json_functions.reference @@ -7,6 +7,10 @@ 1 1 0 +--isValidJSON-- +1 +0 +0 --JSONKey-- a b diff --git a/dbms/tests/queries/0_stateless/00918_json_functions.sql b/dbms/tests/queries/0_stateless/00918_json_functions.sql index 38bf0a7ffec..4cb2445ca2a 100644 --- a/dbms/tests/queries/0_stateless/00918_json_functions.sql +++ b/dbms/tests/queries/0_stateless/00918_json_functions.sql @@ -11,6 +11,11 @@ SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'a'); SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'b'); SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'c'); +SELECT '--isValidJSON--'; +SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}'); +SELECT isValidJSON('not a json'); +SELECT isValidJSON('"HX-='); + SELECT '--JSONKey--'; SELECT JSONKey('{"a": "hello", "b": [-100, 200.0, 300]}', 1); SELECT JSONKey('{"a": "hello", "b": [-100, 200.0, 300]}', 2);