mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 23:21:59 +00:00
Add isValidJSON function
This commit is contained in:
parent
aeb7504570
commit
1174fc346b
@ -8,6 +8,7 @@ namespace DB
|
||||
void registerFunctionsJSON(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionJSON<NameJSONHas, JSONHasImpl>>();
|
||||
factory.registerFunction<FunctionJSON<NameIsValidJSON, isValidJSONImpl>>();
|
||||
factory.registerFunction<FunctionJSON<NameJSONLength, JSONLengthImpl>>();
|
||||
factory.registerFunction<FunctionJSON<NameJSONKey, JSONKeyImpl>>();
|
||||
factory.registerFunction<FunctionJSON<NameJSONType, JSONTypeImpl>>();
|
||||
|
@ -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 <typename JSONParser>
|
||||
class JSONHasImpl
|
||||
template <typename JSONParser, bool support_key_lookup>
|
||||
class JSONCheckImpl
|
||||
{
|
||||
public:
|
||||
static DataTypePtr getType(const char *, const ColumnsWithTypeAndName &) { return std::make_shared<DataTypeUInt8>(); }
|
||||
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<DataTypeUInt8>();
|
||||
}
|
||||
|
||||
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 <typename JSONParser>
|
||||
using JSONHasImpl = JSONCheckImpl<JSONParser, true>;
|
||||
template <typename JSONParser>
|
||||
using isValidJSONImpl = JSONCheckImpl<JSONParser, false>;
|
||||
|
||||
template <typename JSONParser>
|
||||
class JSONLengthImpl
|
||||
|
@ -7,6 +7,10 @@
|
||||
1
|
||||
1
|
||||
0
|
||||
--isValidJSON--
|
||||
1
|
||||
0
|
||||
0
|
||||
--JSONKey--
|
||||
a
|
||||
b
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user