ClickHouse
96 строк · 3.2 Кб
1#include <Functions/FunctionFactory.h>2#include <Functions/FunctionsVisitParam.h>3#include <Functions/FunctionsStringSearchToString.h>4
5
6namespace DB7{
8
9struct ExtractRaw10{
11using ExpectChars = PODArrayWithStackMemory<char, 64>;12
13static void extract(const UInt8 * pos, const UInt8 * end, ColumnString::Chars & res_data)14{15ExpectChars expects_end;16UInt8 current_expect_end = 0;17
18for (const auto * extract_begin = pos; pos != end; ++pos)19{20if (current_expect_end && *pos == current_expect_end)21{22expects_end.pop_back();23current_expect_end = expects_end.empty() ? 0 : expects_end.back();24}25else if (current_expect_end == '"')26{27/// skip backslash28if (*pos == '\\' && pos + 1 < end && pos[1] == '"')29++pos;30}31else32{33switch (*pos)34{35case '[':36current_expect_end = ']';37expects_end.push_back(current_expect_end);38break;39case '{':40current_expect_end = '}';41expects_end.push_back(current_expect_end);42break;43case '"' :44current_expect_end = '"';45expects_end.push_back(current_expect_end);46break;47default:48if (!current_expect_end && (*pos == ',' || *pos == '}'))49{50res_data.insert(extract_begin, pos);51return;52}53}54}55}56}57};58
59struct NameSimpleJSONExtractRaw { static constexpr auto name = "simpleJSONExtractRaw"; };60using FunctionSimpleJSONExtractRaw = FunctionsStringSearchToString<ExtractParamToStringImpl<ExtractRaw>, NameSimpleJSONExtractRaw>;61
62REGISTER_FUNCTION(VisitParamExtractRaw)63{
64factory.registerFunction<FunctionSimpleJSONExtractRaw>(FunctionDocumentation{65.description = "Returns the value of the field named field_name as a String, including separators.",66.syntax = "simpleJSONExtractRaw(json, field_name)",67.arguments68= {{"json", "The JSON in which the field is searched for. String."},69{"field_name", "The name of the field to search for. String literal."}},70.returned_value71= "It returns the value of the field as a String including separators if the field exists, or an empty String otherwise.",72.examples73= {{.name = "simple",74.query = R"(CREATE TABLE jsons75(
76json String
77)
78ENGINE = Memory;
79
80INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
81INSERT INTO jsons VALUES ('{"foo":-3.4}');
82INSERT INTO jsons VALUES ('{"foo":5}');
83INSERT INTO jsons VALUES ('{"foo":{"def":[1,2,3]}}');
84INSERT INTO jsons VALUES ('{"baz":2}');
85
86SELECT simpleJSONExtractRaw(json, 'foo') FROM jsons ORDER BY json;)",87.result = R"(88"-4e3"
89-3.4
905
91{"def":[1,2,3]})"}},92.categories{"JSON"}});93factory.registerAlias("visitParamExtractRaw", "simpleJSONExtractRaw");94}
95
96}
97