2019-08-04 14:15:47 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionsVisitParam.h>
|
|
|
|
#include <Functions/FunctionsStringSearch.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-08-27 22:14:38 +00:00
|
|
|
struct NameSimpleJSONExtractInt { static constexpr auto name = "simpleJSONExtractInt"; };
|
2021-09-21 16:43:46 +00:00
|
|
|
using FunctionSimpleJSONExtractInt = FunctionsStringSearch<ExtractParamImpl<NameSimpleJSONExtractInt, ExtractNumericType<Int64>>>;
|
2019-08-04 14:15:47 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(VisitParamExtractInt)
|
2019-08-04 14:15:47 +00:00
|
|
|
{
|
2024-03-06 11:10:02 +00:00
|
|
|
factory.registerFunction<FunctionSimpleJSONExtractInt>(FunctionDocumentation{
|
|
|
|
.description
|
|
|
|
= "Parses Int64 from the value of the field named field_name. If this is a string field, it tries to parse a number from the "
|
|
|
|
"beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0.",
|
|
|
|
.syntax = "simpleJSONExtractInt(json, field_name)",
|
|
|
|
.arguments
|
|
|
|
= {{"json", "The JSON in which the field is searched for. String."},
|
|
|
|
{"field_name", "The name of the field to search for. String literal."}},
|
|
|
|
.returned_value = "It returns the number parsed from the field if the field exists and contains a number, 0 otherwise.",
|
|
|
|
.examples
|
|
|
|
= {{.name = "simple",
|
|
|
|
.query = R"(CREATE TABLE jsons
|
|
|
|
(
|
|
|
|
json String
|
|
|
|
)
|
|
|
|
ENGINE = Memory;
|
|
|
|
|
|
|
|
INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
|
|
|
|
INSERT INTO jsons VALUES ('{"foo":-3.4}');
|
|
|
|
INSERT INTO jsons VALUES ('{"foo":5}');
|
|
|
|
INSERT INTO jsons VALUES ('{"foo":"not1number"}');
|
|
|
|
INSERT INTO jsons VALUES ('{"baz":2}');
|
|
|
|
|
|
|
|
SELECT simpleJSONExtractInt(json, 'foo') FROM jsons ORDER BY json;)",
|
|
|
|
.result = R"(0
|
|
|
|
-4
|
|
|
|
0
|
|
|
|
-3
|
|
|
|
5)"}},
|
|
|
|
.categories{"JSON"}});
|
2022-08-27 22:14:38 +00:00
|
|
|
factory.registerAlias("visitParamExtractInt", "simpleJSONExtractInt");
|
2019-08-04 14:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|