mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Merge pull request #7293 from Vdimir/feature/isvalidjson-5910
Add function isValidJSON
This commit is contained in:
commit
f611fd4dfa
@ -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"}; };
|
||||
@ -311,6 +312,36 @@ public:
|
||||
};
|
||||
|
||||
|
||||
template <typename JSONParser>
|
||||
class IsValidJSONImpl
|
||||
{
|
||||
public:
|
||||
static DataTypePtr getType(const char * function_name, const ColumnsWithTypeAndName & arguments)
|
||||
{
|
||||
if (arguments.size() != 1)
|
||||
{
|
||||
/// IsValidJSON() shouldn't get parameters other than JSON.
|
||||
throw Exception{"Function " + String(function_name) + " needs exactly one argument",
|
||||
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
||||
}
|
||||
return std::make_shared<DataTypeUInt8>();
|
||||
}
|
||||
|
||||
using Iterator = typename JSONParser::Iterator;
|
||||
static bool addValueToColumn(IColumn & dest, const Iterator &)
|
||||
{
|
||||
/// This function is called only if JSON is valid.
|
||||
/// If JSON isn't valid then `FunctionJSON::Executor::run()` adds default value (=zero) to `dest` without calling this function.
|
||||
ColumnVector<UInt8> & col_vec = assert_cast<ColumnVector<UInt8> &>(dest);
|
||||
col_vec.insertValue(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr size_t num_extra_arguments = 0;
|
||||
static void prepare(const char *, const Block &, const ColumnNumbers &, size_t) {}
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
|
@ -57,6 +57,17 @@ There is currently no support for code points in the format `\uXXXX\uYYYY` that
|
||||
|
||||
The following functions are based on [simdjson](https://github.com/lemire/simdjson) designed for more complex JSON parsing requirements. The assumption 2 mentioned above still applies.
|
||||
|
||||
## isValidJSON(json)
|
||||
|
||||
Checks that passed string is a valid json.
|
||||
|
||||
Examples:
|
||||
|
||||
```sql
|
||||
SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}') = 1
|
||||
SELECT isValidJSON('not a json') = 0
|
||||
```
|
||||
|
||||
## JSONHas(json[, indices_or_keys]...)
|
||||
|
||||
If the value exists in the JSON document, `1` will be returned.
|
||||
|
@ -57,6 +57,17 @@ visitParamExtractString('{"abc":"hello}', 'abc') = ''
|
||||
|
||||
Следующие функции используют [simdjson](https://github.com/lemire/simdjson) который разработан по более сложны требования для разбора JSON. Упомянутое выше предположение 2 по-прежнему применимо.
|
||||
|
||||
## isValidJSON(json)
|
||||
|
||||
Проверяет, является ли переданная строка валидным json значением.
|
||||
|
||||
Примеры:
|
||||
|
||||
```sql
|
||||
SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}') = 1
|
||||
SELECT isValidJSON('not a json') = 0
|
||||
```
|
||||
|
||||
## JSONHas(json[, indices_or_keys]...)
|
||||
|
||||
Если значение существует в документе JSON, то возвращается `1`.
|
||||
|
Loading…
Reference in New Issue
Block a user