-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathFunctionSQLJSON.cpp
More file actions
127 lines (120 loc) · 5.96 KB
/
Copy pathFunctionSQLJSON.cpp
File metadata and controls
127 lines (120 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <Functions/FunctionSQLJSON.h>
#include <Functions/FunctionFactory.h>
namespace DB
{
REGISTER_FUNCTION(SQLJSON)
{
/// JSON_EXISTS
{
FunctionDocumentation::Description description = R"(
If the value exists in the JSON document, `1` will be returned.
If the value does not exist, `0` will be returned.
)";
FunctionDocumentation::Syntax syntax = "JSON_EXISTS(json, path)";
FunctionDocumentation::Arguments arguments = {
{"json", "A string with valid JSON.", {"String"}},
{"path", "A string representing the path.", {"String"}}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns `1` if the value exists in the JSON document, otherwise `0`.", {"UInt8"}};
FunctionDocumentation::Examples examples = {
{
"Usage example",
R"(
SELECT JSON_EXISTS('{"hello":1}', '$.hello');
SELECT JSON_EXISTS('{"hello":{"world":1}}', '$.hello.world');
SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[*]');
SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[0]');
)",
R"(
┌─JSON_EXISTS('{"hello":1}', '$.hello')─┐
│ 1 │
└───────────────────────────────────────┘
┌─JSON_EXISTS('{"hello":{"world":1}}', '$.hello.world')─┐
│ 1 │
└───────────────────────────────────────────────────────┘
┌─JSON_EXISTS('{"hello":["world"]}', '$.hello[*]')─┐
│ 1 │
└──────────────────────────────────────────────────┘
┌─JSON_EXISTS('{"hello":["world"]}', '$.hello[0]')─┐
│ 1 │
└──────────────────────────────────────────────────┘
)"
}
};
FunctionDocumentation::IntroducedIn introduced_in = {21, 8};
FunctionDocumentation::Category category = FunctionDocumentation::Category::JSON;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionSQLJSON<NameJSONExists, JSONExistsImpl>>(documentation);
}
/// JSON_QUERY
{
FunctionDocumentation::Description description = R"(
Parses a JSON and extract a value as a JSON array or JSON object.
If the value does not exist, an empty string will be returned.
)";
FunctionDocumentation::Syntax syntax = "JSON_QUERY(json, path)";
FunctionDocumentation::Arguments arguments = {
{"json", "A string with valid JSON.", {"String"}},
{"path", "A string representing the path.", {"String"}}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns the extracted JSON array or JSON object as a string, or an empty string if the value does not exist.", {"String"}};
FunctionDocumentation::Examples examples = {
{
"Usage example",
R"(
SELECT JSON_QUERY('{"hello":"world"}', '$.hello');
SELECT JSON_QUERY('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]');
SELECT JSON_QUERY('{"hello":2}', '$.hello');
SELECT toTypeName(JSON_QUERY('{"hello":2}', '$.hello'));
)",
R"(
["world"]
[0, 1, 4, 0, -1, -4]
[2]
String
)"
}
};
FunctionDocumentation::IntroducedIn introduced_in = {21, 8};
FunctionDocumentation::Category category = FunctionDocumentation::Category::JSON;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionSQLJSON<NameJSONQuery, JSONQueryImpl>>(documentation);
}
/// JSON_VALUE
{
FunctionDocumentation::Description description = R"(
Parses a JSON and extract a value as a JSON scalar. If the value does not exist, an empty string will be returned by default.
This function is controlled by the following settings:
- by SET `function_json_value_return_type_allow_nullable` = `true`, `NULL` will be returned. If the value is complex type (such as: struct, array, map), an empty string will be returned by default.
- by SET `function_json_value_return_type_allow_complex` = `true`, the complex value will be returned.
)";
FunctionDocumentation::Syntax syntax = "JSON_VALUE(json, path)";
FunctionDocumentation::Arguments arguments = {
{"json", "A string with valid JSON.", {"String"}},
{"path", "A string representing the path.", {"String"}}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns the extracted JSON scalar as a string, or an empty string if the value does not exist.", {"String"}};
FunctionDocumentation::Examples examples = {
{
"Usage example",
R"(
SELECT JSON_VALUE('{"hello":"world"}', '$.hello');
SELECT JSON_VALUE('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]');
SELECT JSON_VALUE('{"hello":2}', '$.hello');
SELECT JSON_VALUE('{"hello":"world"}', '$.b') settings function_json_value_return_type_allow_nullable=true;
)",
R"(
world
0
2
\N
)"
}
};
FunctionDocumentation::IntroducedIn introduced_in = {21, 11};
FunctionDocumentation::Category category = FunctionDocumentation::Category::JSON;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionSQLJSON<NameJSONValue, JSONValueImpl>>(documentation);
}
}
}