ClickHouse
48 строк · 1.7 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionsVisitParam.h>
3#include <Functions/FunctionsStringSearch.h>
4
5
6namespace DB
7{
8
9struct NameSimpleJSONExtractUInt { static constexpr auto name = "simpleJSONExtractUInt"; };
10using FunctionSimpleJSONExtractUInt = FunctionsStringSearch<ExtractParamImpl<NameSimpleJSONExtractUInt, ExtractNumericType<UInt64>>>;
11
12
13REGISTER_FUNCTION(VisitParamExtractUInt)
14{
15factory.registerFunction<FunctionSimpleJSONExtractUInt>(FunctionDocumentation{
16.description
17= "Parses UInt64 from the value of the field named field_name. If this is a string field, it tries to parse a number from the "
18"beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0.",
19.syntax = "simpleJSONExtractUInt(json, field_name)",
20.arguments
21= {{"json", "The JSON in which the field is searched for. String."},
22{"field_name", "The name of the field to search for. String literal."}},
23.returned_value = "It returns the number parsed from the field if the field exists and contains a number, 0 otherwise.",
24.examples
25= {{.name = "simple",
26.query = R"(CREATE TABLE jsons
27(
28json String
29)
30ENGINE = Memory;
31
32INSERT INTO jsons VALUES ('{"foo":"4e3"}');
33INSERT INTO jsons VALUES ('{"foo":3.4}');
34INSERT INTO jsons VALUES ('{"foo":5}');
35INSERT INTO jsons VALUES ('{"foo":"not1number"}');
36INSERT INTO jsons VALUES ('{"baz":2}');
37
38SELECT simpleJSONExtractUInt(json, 'foo') FROM jsons ORDER BY json;)",
39.result = R"(0
404
410
423
435)"}},
44.categories{"JSON"}});
45factory.registerAlias("visitParamExtractUInt", "simpleJSONExtractUInt");
46}
47
48}
49