ClickHouse

Форк
0
/
visitParamExtractRaw.cpp 
96 строк · 3.2 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionsVisitParam.h>
3
#include <Functions/FunctionsStringSearchToString.h>
4

5

6
namespace DB
7
{
8

9
struct ExtractRaw
10
{
11
    using ExpectChars = PODArrayWithStackMemory<char, 64>;
12

13
    static void extract(const UInt8 * pos, const UInt8 * end, ColumnString::Chars & res_data)
14
    {
15
        ExpectChars expects_end;
16
        UInt8 current_expect_end = 0;
17

18
        for (const auto * extract_begin = pos; pos != end; ++pos)
19
        {
20
            if (current_expect_end && *pos == current_expect_end)
21
            {
22
                expects_end.pop_back();
23
                current_expect_end = expects_end.empty() ? 0 : expects_end.back();
24
            }
25
            else if (current_expect_end == '"')
26
            {
27
                /// skip backslash
28
                if (*pos == '\\' && pos + 1 < end && pos[1] == '"')
29
                    ++pos;
30
            }
31
            else
32
            {
33
                switch (*pos)
34
                {
35
                    case '[':
36
                        current_expect_end = ']';
37
                        expects_end.push_back(current_expect_end);
38
                        break;
39
                    case '{':
40
                        current_expect_end = '}';
41
                        expects_end.push_back(current_expect_end);
42
                        break;
43
                    case '"' :
44
                        current_expect_end = '"';
45
                        expects_end.push_back(current_expect_end);
46
                        break;
47
                    default:
48
                        if (!current_expect_end && (*pos == ',' || *pos == '}'))
49
                        {
50
                            res_data.insert(extract_begin, pos);
51
                            return;
52
                        }
53
                }
54
            }
55
        }
56
    }
57
};
58

59
struct NameSimpleJSONExtractRaw    { static constexpr auto name = "simpleJSONExtractRaw"; };
60
using FunctionSimpleJSONExtractRaw = FunctionsStringSearchToString<ExtractParamToStringImpl<ExtractRaw>, NameSimpleJSONExtractRaw>;
61

62
REGISTER_FUNCTION(VisitParamExtractRaw)
63
{
64
    factory.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
        .arguments
68
        = {{"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_value
71
        = "It returns the value of the field as a String including separators if the field exists, or an empty String otherwise.",
72
        .examples
73
        = {{.name = "simple",
74
            .query = R"(CREATE TABLE jsons
75
(
76
    json String
77
)
78
ENGINE = Memory;
79

80
INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
81
INSERT INTO jsons VALUES ('{"foo":-3.4}');
82
INSERT INTO jsons VALUES ('{"foo":5}');
83
INSERT INTO jsons VALUES ('{"foo":{"def":[1,2,3]}}');
84
INSERT INTO jsons VALUES ('{"baz":2}');
85

86
SELECT simpleJSONExtractRaw(json, 'foo') FROM jsons ORDER BY json;)",
87
            .result = R"(
88
"-4e3"
89
-3.4
90
5
91
{"def":[1,2,3]})"}},
92
        .categories{"JSON"}});
93
    factory.registerAlias("visitParamExtractRaw", "simpleJSONExtractRaw");
94
}
95

96
}
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.