mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Add JSONExtractKeys function
This commit is contained in:
parent
5ac0d36392
commit
65c6605c2c
@ -73,6 +73,7 @@ void registerFunctionsJSON(FunctionFactory & factory)
|
||||
factory.registerFunction<JSONOverloadResolver<NameJSONExtractRaw, JSONExtractRawImpl>>();
|
||||
factory.registerFunction<JSONOverloadResolver<NameJSONExtractArrayRaw, JSONExtractArrayRawImpl>>();
|
||||
factory.registerFunction<JSONOverloadResolver<NameJSONExtractKeysAndValuesRaw, JSONExtractKeysAndValuesRawImpl>>();
|
||||
factory.registerFunction<JSONOverloadResolver<NameJSONExtractKeys, JSONExtractKeysImpl>>();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -433,6 +433,7 @@ struct NameJSONExtractKeysAndValues { static constexpr auto name{"JSONExtractKey
|
||||
struct NameJSONExtractRaw { static constexpr auto name{"JSONExtractRaw"}; };
|
||||
struct NameJSONExtractArrayRaw { static constexpr auto name{"JSONExtractArrayRaw"}; };
|
||||
struct NameJSONExtractKeysAndValuesRaw { static constexpr auto name{"JSONExtractKeysAndValuesRaw"}; };
|
||||
struct NameJSONExtractKeys { static constexpr auto name{"JSONExtractKeys"}; };
|
||||
|
||||
|
||||
template <typename JSONParser>
|
||||
@ -1353,4 +1354,38 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename JSONParser>
|
||||
class JSONExtractKeysImpl
|
||||
{
|
||||
public:
|
||||
using Element = typename JSONParser::Element;
|
||||
|
||||
static DataTypePtr getReturnType(const char *, const ColumnsWithTypeAndName &)
|
||||
{
|
||||
return std::make_unique<DataTypeString>();
|
||||
}
|
||||
|
||||
static size_t getNumberOfIndexArguments(const ColumnsWithTypeAndName & arguments) { return arguments.size() - 1; }
|
||||
|
||||
bool insertResultToColumn(IColumn & dest, const Element & element, const std::string_view &)
|
||||
{
|
||||
if (!element.isObject())
|
||||
return false;
|
||||
|
||||
auto object = element.getObject();
|
||||
|
||||
auto & col_arr = assert_cast<ColumnArray &>(dest);
|
||||
auto & col_tuple = assert_cast<ColumnTuple &>(col_arr.getData());
|
||||
auto & col_key = assert_cast<ColumnString &>(col_tuple.getColumn(0));
|
||||
|
||||
for (auto [key, value] : object)
|
||||
{
|
||||
col_key.insertData(key.data(), key.size());
|
||||
}
|
||||
|
||||
col_arr.getOffsets().push_back(col_arr.getOffsets().back() + object.size());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -650,6 +650,7 @@
|
||||
"JSONExtractInt"
|
||||
"JSONExtractKeysAndValues"
|
||||
"JSONExtractKeysAndValuesRaw"
|
||||
"JSONExtractKeys"
|
||||
"JSONExtractRaw"
|
||||
"JSONExtractString"
|
||||
"JSONExtractUInt"
|
||||
|
@ -31,6 +31,7 @@
|
||||
"toJSONString"
|
||||
"JSON_VALUE"
|
||||
"JSONExtractKeysAndValuesRaw"
|
||||
"JSONExtractKeys"
|
||||
"JSONExtractString"
|
||||
"JSONType"
|
||||
"JSONKey"
|
||||
|
@ -101,6 +101,11 @@ hello
|
||||
[('a','"hello"'),('b','[-100,200,300]')]
|
||||
[('a','"hello"'),('b','[-100,200,300]'),('c','{"d":[121,144]}')]
|
||||
[('d','[121,144]')]
|
||||
--JSONExtractKeys--
|
||||
['a','b']
|
||||
[]
|
||||
[]
|
||||
['d']
|
||||
--const/non-const mixed--
|
||||
a
|
||||
b
|
||||
|
@ -231,6 +231,12 @@ SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300]}');
|
||||
SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300], "c":{"d":[121,144]}}');
|
||||
SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300], "c":{"d":[121,144]}}', 'c');
|
||||
|
||||
SELECT '--JSONExtractKeys--';
|
||||
SELECT JSONExtractKeys('{"a": "hello", "b": [-100, 200.0, 300]}');
|
||||
SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'b');
|
||||
SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'a');
|
||||
SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300], "c":{"d":[121,144]}}', '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);
|
||||
|
Loading…
Reference in New Issue
Block a user