ClickHouse
47 строк · 1.7 Кб
1#include <Functions/FunctionFactory.h>2#include <Functions/FunctionsVisitParam.h>3#include <Functions/FunctionsStringSearch.h>4
5
6namespace DB7{
8
9struct NameSimpleJSONExtractFloat { static constexpr auto name = "simpleJSONExtractFloat"; };10using FunctionSimpleJSONExtractFloat = FunctionsStringSearch<ExtractParamImpl<NameSimpleJSONExtractFloat, ExtractNumericType<Float64>>>;11
12REGISTER_FUNCTION(VisitParamExtractFloat)13{
14factory.registerFunction<FunctionSimpleJSONExtractFloat>(FunctionDocumentation{15.description16= "Parses Float64 from the value of the field named field_name. If this is a string field, it tries to parse a number from the "17"beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0.",18.syntax = "simpleJSONExtractFloat(json, field_name)",19.arguments20= {{"json", "The JSON in which the field is searched for. String."},21{"field_name", "The name of the field to search for. String literal."}},22.returned_value = "It returns the number parsed from the field if the field exists and contains a number, 0 otherwise.",23.examples24= {{.name = "simple",25.query = R"(CREATE TABLE jsons26(
27json String
28)
29ENGINE = Memory;
30
31INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
32INSERT INTO jsons VALUES ('{"foo":-3.4}');
33INSERT INTO jsons VALUES ('{"foo":5}');
34INSERT INTO jsons VALUES ('{"foo":"not1number"}');
35INSERT INTO jsons VALUES ('{"baz":2}');
36
37SELECT simpleJSONExtractFloat(json, 'foo') FROM jsons ORDER BY json;)",38.result = R"(039-4000
400
41-3.4
425)"}},43.categories{"JSON"}});44factory.registerAlias("visitParamExtractFloat", "simpleJSONExtractFloat");45}
46
47}
48