ClickHouse
56 строк · 1.9 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionsVisitParam.h>
3#include <Functions/FunctionsStringSearch.h>
4
5
6namespace DB
7{
8
9struct ExtractBool
10{
11using ResultType = UInt8;
12
13static UInt8 extract(const UInt8 * begin, const UInt8 * end)
14{
15return begin + 4 <= end && 0 == strncmp(reinterpret_cast<const char *>(begin), "true", 4);
16}
17};
18
19struct NameSimpleJSONExtractBool { static constexpr auto name = "simpleJSONExtractBool"; };
20using FunctionSimpleJSONExtractBool = FunctionsStringSearch<ExtractParamImpl<NameSimpleJSONExtractBool, ExtractBool>>;
21
22REGISTER_FUNCTION(VisitParamExtractBool)
23{
24factory.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(
39json String
40)
41ENGINE = Memory;
42
43INSERT INTO jsons VALUES ('{"foo":false,"bar":true}');
44INSERT INTO jsons VALUES ('{"foo":"true","qux":1}');
45
46SELECT simpleJSONExtractBool(json, 'bar') FROM jsons ORDER BY json;
47SELECT simpleJSONExtractBool(json, 'foo') FROM jsons ORDER BY json;)",
48.result = R"(0
491
500
510)"}},
52.categories{"JSON"}});
53factory.registerAlias("visitParamExtractBool", "simpleJSONExtractBool");
54}
55
56}
57