ClickHouse
59 строк · 2.2 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionsVisitParam.h>
3#include <Functions/FunctionsStringSearchToString.h>
4
5
6namespace DB
7{
8
9struct ExtractString
10{
11static void extract(const UInt8 * pos, const UInt8 * end, ColumnString::Chars & res_data)
12{
13size_t old_size = res_data.size();
14ReadBufferFromMemory in(pos, end - pos);
15if (!tryReadJSONStringInto(res_data, in, default_json_settings))
16res_data.resize(old_size);
17}
18
19static const FormatSettings::JSON constexpr default_json_settings;
20};
21
22struct NameSimpleJSONExtractString { static constexpr auto name = "simpleJSONExtractString"; };
23using FunctionSimpleJSONExtractString = FunctionsStringSearchToString<ExtractParamToStringImpl<ExtractString>, NameSimpleJSONExtractString>;
24
25REGISTER_FUNCTION(VisitParamExtractString)
26{
27factory.registerFunction<FunctionSimpleJSONExtractString>(FunctionDocumentation{
28.description = R"(Parses String in double quotes from the value of the field named field_name.
29
30There is currently no support for code points in the format \uXXXX\uYYYY that are not from the basic multilingual plane (they are converted to CESU-8 instead of UTF-8).)",
31.syntax = "simpleJSONExtractString(json, field_name)",
32.arguments
33= {{"json", "The JSON in which the field is searched for. String."},
34{"field_name", "The name of the field to search for. String literal."}},
35.returned_value = "It returns the value of a field as a String, including separators. The value is unescaped. It returns an empty "
36"String: if the field doesn't contain a double quoted string, if unescaping fails or if the field doesn't exist.",
37.examples
38= {{.name = "simple",
39.query = R"(CREATE TABLE jsons
40(
41json String
42)
43ENGINE = Memory;
44
45INSERT INTO jsons VALUES ('{"foo":"\\n\\u0000"}');
46INSERT INTO jsons VALUES ('{"foo":"\\u263"}');
47INSERT INTO jsons VALUES ('{"foo":"\\u263a"}');
48INSERT INTO jsons VALUES ('{"foo":"hello}');
49
50SELECT simpleJSONExtractString(json, 'foo') FROM jsons ORDER BY json;)",
51.result = R"(\n\0
52
53☺
54)"}},
55.categories{"JSON"}});
56factory.registerAlias("visitParamExtractString", "simpleJSONExtractString");
57}
58
59}
60