ClickHouse

Форк
0
/
visitParamExtractBool.cpp 
56 строк · 1.9 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/FunctionsVisitParam.h>
3
#include <Functions/FunctionsStringSearch.h>
4

5

6
namespace DB
7
{
8

9
struct ExtractBool
10
{
11
    using ResultType = UInt8;
12

13
    static UInt8 extract(const UInt8 * begin, const UInt8 * end)
14
    {
15
        return begin + 4 <= end && 0 == strncmp(reinterpret_cast<const char *>(begin), "true", 4);
16
    }
17
};
18

19
struct NameSimpleJSONExtractBool { static constexpr auto name = "simpleJSONExtractBool"; };
20
using FunctionSimpleJSONExtractBool = FunctionsStringSearch<ExtractParamImpl<NameSimpleJSONExtractBool, ExtractBool>>;
21

22
REGISTER_FUNCTION(VisitParamExtractBool)
23
{
24
    factory.registerFunction<FunctionSimpleJSONExtractBool>(FunctionDocumentation{
25
        .description = "Parses a true/false value from the value of the field named field_name. The result is UInt8.",
26
        .syntax = "simpleJSONExtractBool(json, field_name)",
27
        .arguments
28
        = {{"json", "The JSON in which the field is searched for. String."},
29
           {"field_name", "The name of the field to search for. String literal."}},
30
        .returned_value
31
        = R"(It returns 1 if the value of the field is true, 0 otherwise. This means this function will return 0 including (and not only) in the following cases:
32
 - If the field doesn't exists.
33
 - If the field contains true as a string, e.g.: {"field":"true"}.
34
 - If the field contains 1 as a numerical value.)",
35
        .examples
36
        = {{.name = "simple",
37
            .query = R"(CREATE TABLE jsons
38
(
39
    json String
40
)
41
ENGINE = Memory;
42

43
INSERT INTO jsons VALUES ('{"foo":false,"bar":true}');
44
INSERT INTO jsons VALUES ('{"foo":"true","qux":1}');
45

46
SELECT simpleJSONExtractBool(json, 'bar') FROM jsons ORDER BY json;
47
SELECT simpleJSONExtractBool(json, 'foo') FROM jsons ORDER BY json;)",
48
            .result = R"(0
49
1
50
0
51
0)"}},
52
        .categories{"JSON"}});
53
    factory.registerAlias("visitParamExtractBool", "simpleJSONExtractBool");
54
}
55

56
}
57

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

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

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

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