Edit on GitHub

sqlglot expressions - JSON functions.

  1"""sqlglot expressions - JSON functions."""
  2
  3from __future__ import annotations
  4
  5from sqlglot.expressions.core import Expression, Func, AggFunc, Binary, Predicate
  6
  7
  8class CheckJson(Expression, Func):
  9    arg_types = {"this": True}
 10
 11
 12class JSONArray(Expression, Func):
 13    arg_types = {
 14        "expressions": False,
 15        "null_handling": False,
 16        "return_type": False,
 17        "strict": False,
 18    }
 19
 20
 21class JSONArrayAgg(Expression, AggFunc):
 22    arg_types = {
 23        "this": True,
 24        "order": False,
 25        "null_handling": False,
 26        "return_type": False,
 27        "strict": False,
 28    }
 29
 30
 31class JSONArrayAppend(Expression, Func):
 32    arg_types = {"this": True, "expressions": True}
 33    is_var_len_args = True
 34    _sql_names = ["JSON_ARRAY_APPEND"]
 35
 36
 37class JSONArrayContains(Expression, Binary, Predicate, Func):
 38    arg_types = {"this": True, "expression": True, "json_type": False}
 39    _sql_names = ["JSON_ARRAY_CONTAINS"]
 40
 41
 42class JSONArrayInsert(Expression, Func):
 43    arg_types = {"this": True, "expressions": True}
 44    is_var_len_args = True
 45    _sql_names = ["JSON_ARRAY_INSERT"]
 46
 47
 48class JSONBContains(Expression, Binary, Func):
 49    _sql_names = ["JSONB_CONTAINS"]
 50
 51
 52class JSONBContainsAllTopKeys(Expression, Binary, Func):
 53    pass
 54
 55
 56class JSONBContainsAnyTopKeys(Expression, Binary, Func):
 57    pass
 58
 59
 60class JSONBDeleteAtPath(Expression, Binary, Func):
 61    pass
 62
 63
 64class JSONBExists(Expression, Func):
 65    arg_types = {"this": True, "path": True}
 66    _sql_names = ["JSONB_EXISTS"]
 67
 68
 69class JSONBExtract(Expression, Binary, Func):
 70    _sql_names = ["JSONB_EXTRACT"]
 71
 72
 73class JSONBExtractScalar(Expression, Binary, Func):
 74    arg_types = {"this": True, "expression": True, "json_type": False}
 75    _sql_names = ["JSONB_EXTRACT_SCALAR"]
 76
 77
 78class JSONBObjectAgg(Expression, AggFunc):
 79    arg_types = {"this": True, "expression": True}
 80
 81
 82class JSONBool(Expression, Func):
 83    pass
 84
 85
 86class JSONExists(Expression, Func):
 87    arg_types = {
 88        "this": True,
 89        "path": True,
 90        "passing": False,
 91        "on_condition": False,
 92        "from_dcolonqmark": False,
 93    }
 94
 95
 96class JSONExtract(Expression, Binary, Func):
 97    arg_types = {
 98        "this": True,
 99        "expression": True,
100        "only_json_types": False,
101        "expressions": False,
102        "variant_extract": False,
103        "json_query": False,
104        "option": False,
105        "quote": False,
106        "on_condition": False,
107        "requires_json": False,
108        "emits": False,
109    }
110    _sql_names = ["JSON_EXTRACT"]
111    is_var_len_args = True
112
113    @property
114    def output_name(self) -> str:
115        return self.expression.output_name if not self.expressions else ""
116
117
118class JSONExtractArray(Expression, Func):
119    arg_types = {"this": True, "expression": False}
120    _sql_names = ["JSON_EXTRACT_ARRAY"]
121
122
123class JSONExtractScalar(Expression, Binary, Func):
124    arg_types = {
125        "this": True,
126        "expression": True,
127        "only_json_types": False,
128        "expressions": False,
129        "json_type": False,
130        "scalar_only": False,
131    }
132    _sql_names = ["JSON_EXTRACT_SCALAR"]
133    is_var_len_args = True
134
135    @property
136    def output_name(self) -> str:
137        return self.expression.output_name
138
139
140class JSONFormat(Expression, Func):
141    arg_types = {"this": False, "options": False, "is_json": False, "to_json": False}
142    _sql_names = ["JSON_FORMAT"]
143
144
145class JSONKeys(Expression, Func):
146    arg_types = {"this": True, "expression": False, "expressions": False}
147    is_var_len_args = True
148    _sql_names = ["JSON_KEYS"]
149
150
151class JSONKeysAtDepth(Expression, Func):
152    arg_types = {"this": True, "expression": False, "mode": False}
153
154
155class JSONObject(Expression, Func):
156    arg_types = {
157        "expressions": False,
158        "null_handling": False,
159        "unique_keys": False,
160        "return_type": False,
161        "encoding": False,
162    }
163
164
165class JSONObjectAgg(Expression, AggFunc):
166    arg_types = {
167        "expressions": False,
168        "null_handling": False,
169        "unique_keys": False,
170        "return_type": False,
171        "encoding": False,
172    }
173
174
175class JSONRemove(Expression, Func):
176    arg_types = {"this": True, "expressions": True}
177    is_var_len_args = True
178    _sql_names = ["JSON_REMOVE"]
179
180
181class JSONSet(Expression, Func):
182    arg_types = {"this": True, "expressions": True}
183    is_var_len_args = True
184    _sql_names = ["JSON_SET"]
185
186
187class JSONStripNulls(Expression, Func):
188    arg_types = {
189        "this": True,
190        "expression": False,
191        "include_arrays": False,
192        "remove_empty": False,
193    }
194    _sql_names = ["JSON_STRIP_NULLS"]
195
196
197class StripNullValue(Expression, Func):
198    pass
199
200
201class JSONTable(Expression, Func):
202    arg_types = {
203        "this": True,
204        "schema": True,
205        "path": False,
206        "error_handling": False,
207        "empty_handling": False,
208    }
209
210
211class JSONType(Expression, Func):
212    arg_types = {"this": True, "expression": False}
213    _sql_names = ["JSON_TYPE"]
214
215
216class ObjectId(Expression, Func):
217    arg_types = {"this": True, "expression": False}
218
219
220class ObjectInsert(Expression, Func):
221    arg_types = {
222        "this": True,
223        "key": True,
224        "value": True,
225        "update_flag": False,
226    }
227
228
229class OpenJSON(Expression, Func):
230    arg_types = {"this": True, "path": False, "expressions": False}
231
232
233class ParseJSON(Expression, Func):
234    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
235    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
236    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
237    arg_types = {"this": True, "expression": False, "safe": False}
 9class CheckJson(Expression, Func):
10    arg_types = {"this": True}
arg_types = {'this': True}
key: ClassVar[str] = 'checkjson'
required_args: 't.ClassVar[set[str]]' = {'this'}
13class JSONArray(Expression, Func):
14    arg_types = {
15        "expressions": False,
16        "null_handling": False,
17        "return_type": False,
18        "strict": False,
19    }
arg_types = {'expressions': False, 'null_handling': False, 'return_type': False, 'strict': False}
key: ClassVar[str] = 'jsonarray'
required_args: 't.ClassVar[set[str]]' = set()
22class JSONArrayAgg(Expression, AggFunc):
23    arg_types = {
24        "this": True,
25        "order": False,
26        "null_handling": False,
27        "return_type": False,
28        "strict": False,
29    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key: ClassVar[str] = 'jsonarrayagg'
required_args: 't.ClassVar[set[str]]' = {'this'}
32class JSONArrayAppend(Expression, Func):
33    arg_types = {"this": True, "expressions": True}
34    is_var_len_args = True
35    _sql_names = ["JSON_ARRAY_APPEND"]
arg_types = {'this': True, 'expressions': True}
is_var_len_args = True
key: ClassVar[str] = 'jsonarrayappend'
required_args: 't.ClassVar[set[str]]' = {'expressions', 'this'}
38class JSONArrayContains(Expression, Binary, Predicate, Func):
39    arg_types = {"this": True, "expression": True, "json_type": False}
40    _sql_names = ["JSON_ARRAY_CONTAINS"]
arg_types = {'this': True, 'expression': True, 'json_type': False}
key: ClassVar[str] = 'jsonarraycontains'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
43class JSONArrayInsert(Expression, Func):
44    arg_types = {"this": True, "expressions": True}
45    is_var_len_args = True
46    _sql_names = ["JSON_ARRAY_INSERT"]
arg_types = {'this': True, 'expressions': True}
is_var_len_args = True
key: ClassVar[str] = 'jsonarrayinsert'
required_args: 't.ClassVar[set[str]]' = {'expressions', 'this'}
49class JSONBContains(Expression, Binary, Func):
50    _sql_names = ["JSONB_CONTAINS"]
key: ClassVar[str] = 'jsonbcontains'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
53class JSONBContainsAllTopKeys(Expression, Binary, Func):
54    pass
key: ClassVar[str] = 'jsonbcontainsalltopkeys'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
57class JSONBContainsAnyTopKeys(Expression, Binary, Func):
58    pass
key: ClassVar[str] = 'jsonbcontainsanytopkeys'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
61class JSONBDeleteAtPath(Expression, Binary, Func):
62    pass
key: ClassVar[str] = 'jsonbdeleteatpath'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
65class JSONBExists(Expression, Func):
66    arg_types = {"this": True, "path": True}
67    _sql_names = ["JSONB_EXISTS"]
arg_types = {'this': True, 'path': True}
key: ClassVar[str] = 'jsonbexists'
required_args: 't.ClassVar[set[str]]' = {'path', 'this'}
70class JSONBExtract(Expression, Binary, Func):
71    _sql_names = ["JSONB_EXTRACT"]
key: ClassVar[str] = 'jsonbextract'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
74class JSONBExtractScalar(Expression, Binary, Func):
75    arg_types = {"this": True, "expression": True, "json_type": False}
76    _sql_names = ["JSONB_EXTRACT_SCALAR"]
arg_types = {'this': True, 'expression': True, 'json_type': False}
key: ClassVar[str] = 'jsonbextractscalar'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
79class JSONBObjectAgg(Expression, AggFunc):
80    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'jsonbobjectagg'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
83class JSONBool(Expression, Func):
84    pass
key: ClassVar[str] = 'jsonbool'
required_args: 't.ClassVar[set[str]]' = {'this'}
87class JSONExists(Expression, Func):
88    arg_types = {
89        "this": True,
90        "path": True,
91        "passing": False,
92        "on_condition": False,
93        "from_dcolonqmark": False,
94    }
arg_types = {'this': True, 'path': True, 'passing': False, 'on_condition': False, 'from_dcolonqmark': False}
key: ClassVar[str] = 'jsonexists'
required_args: 't.ClassVar[set[str]]' = {'path', 'this'}
 97class JSONExtract(Expression, Binary, Func):
 98    arg_types = {
 99        "this": True,
100        "expression": True,
101        "only_json_types": False,
102        "expressions": False,
103        "variant_extract": False,
104        "json_query": False,
105        "option": False,
106        "quote": False,
107        "on_condition": False,
108        "requires_json": False,
109        "emits": False,
110    }
111    _sql_names = ["JSON_EXTRACT"]
112    is_var_len_args = True
113
114    @property
115    def output_name(self) -> str:
116        return self.expression.output_name if not self.expressions else ""
arg_types = {'this': True, 'expression': True, 'only_json_types': False, 'expressions': False, 'variant_extract': False, 'json_query': False, 'option': False, 'quote': False, 'on_condition': False, 'requires_json': False, 'emits': False}
is_var_len_args = True
output_name: str
114    @property
115    def output_name(self) -> str:
116        return self.expression.output_name if not self.expressions else ""

Name of the output column if this expression is a selection.

If the Expr has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a").expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c").expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2").expressions[0].output_name
''
key: ClassVar[str] = 'jsonextract'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
119class JSONExtractArray(Expression, Func):
120    arg_types = {"this": True, "expression": False}
121    _sql_names = ["JSON_EXTRACT_ARRAY"]
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'jsonextractarray'
required_args: 't.ClassVar[set[str]]' = {'this'}
124class JSONExtractScalar(Expression, Binary, Func):
125    arg_types = {
126        "this": True,
127        "expression": True,
128        "only_json_types": False,
129        "expressions": False,
130        "json_type": False,
131        "scalar_only": False,
132    }
133    _sql_names = ["JSON_EXTRACT_SCALAR"]
134    is_var_len_args = True
135
136    @property
137    def output_name(self) -> str:
138        return self.expression.output_name
arg_types = {'this': True, 'expression': True, 'only_json_types': False, 'expressions': False, 'json_type': False, 'scalar_only': False}
is_var_len_args = True
output_name: str
136    @property
137    def output_name(self) -> str:
138        return self.expression.output_name

Name of the output column if this expression is a selection.

If the Expr has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a").expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c").expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2").expressions[0].output_name
''
key: ClassVar[str] = 'jsonextractscalar'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
141class JSONFormat(Expression, Func):
142    arg_types = {"this": False, "options": False, "is_json": False, "to_json": False}
143    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False, 'is_json': False, 'to_json': False}
key: ClassVar[str] = 'jsonformat'
required_args: 't.ClassVar[set[str]]' = set()
146class JSONKeys(Expression, Func):
147    arg_types = {"this": True, "expression": False, "expressions": False}
148    is_var_len_args = True
149    _sql_names = ["JSON_KEYS"]
arg_types = {'this': True, 'expression': False, 'expressions': False}
is_var_len_args = True
key: ClassVar[str] = 'jsonkeys'
required_args: 't.ClassVar[set[str]]' = {'this'}
152class JSONKeysAtDepth(Expression, Func):
153    arg_types = {"this": True, "expression": False, "mode": False}
arg_types = {'this': True, 'expression': False, 'mode': False}
key: ClassVar[str] = 'jsonkeysatdepth'
required_args: 't.ClassVar[set[str]]' = {'this'}
156class JSONObject(Expression, Func):
157    arg_types = {
158        "expressions": False,
159        "null_handling": False,
160        "unique_keys": False,
161        "return_type": False,
162        "encoding": False,
163    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key: ClassVar[str] = 'jsonobject'
required_args: 't.ClassVar[set[str]]' = set()
166class JSONObjectAgg(Expression, AggFunc):
167    arg_types = {
168        "expressions": False,
169        "null_handling": False,
170        "unique_keys": False,
171        "return_type": False,
172        "encoding": False,
173    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key: ClassVar[str] = 'jsonobjectagg'
required_args: 't.ClassVar[set[str]]' = set()
176class JSONRemove(Expression, Func):
177    arg_types = {"this": True, "expressions": True}
178    is_var_len_args = True
179    _sql_names = ["JSON_REMOVE"]
arg_types = {'this': True, 'expressions': True}
is_var_len_args = True
key: ClassVar[str] = 'jsonremove'
required_args: 't.ClassVar[set[str]]' = {'expressions', 'this'}
182class JSONSet(Expression, Func):
183    arg_types = {"this": True, "expressions": True}
184    is_var_len_args = True
185    _sql_names = ["JSON_SET"]
arg_types = {'this': True, 'expressions': True}
is_var_len_args = True
key: ClassVar[str] = 'jsonset'
required_args: 't.ClassVar[set[str]]' = {'expressions', 'this'}
188class JSONStripNulls(Expression, Func):
189    arg_types = {
190        "this": True,
191        "expression": False,
192        "include_arrays": False,
193        "remove_empty": False,
194    }
195    _sql_names = ["JSON_STRIP_NULLS"]
arg_types = {'this': True, 'expression': False, 'include_arrays': False, 'remove_empty': False}
key: ClassVar[str] = 'jsonstripnulls'
required_args: 't.ClassVar[set[str]]' = {'this'}
198class StripNullValue(Expression, Func):
199    pass
key: ClassVar[str] = 'stripnullvalue'
required_args: 't.ClassVar[set[str]]' = {'this'}
202class JSONTable(Expression, Func):
203    arg_types = {
204        "this": True,
205        "schema": True,
206        "path": False,
207        "error_handling": False,
208        "empty_handling": False,
209    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key: ClassVar[str] = 'jsontable'
required_args: 't.ClassVar[set[str]]' = {'schema', 'this'}
212class JSONType(Expression, Func):
213    arg_types = {"this": True, "expression": False}
214    _sql_names = ["JSON_TYPE"]
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'jsontype'
required_args: 't.ClassVar[set[str]]' = {'this'}
217class ObjectId(Expression, Func):
218    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'objectid'
required_args: 't.ClassVar[set[str]]' = {'this'}
221class ObjectInsert(Expression, Func):
222    arg_types = {
223        "this": True,
224        "key": True,
225        "value": True,
226        "update_flag": False,
227    }
arg_types = {'this': True, 'key': True, 'value': True, 'update_flag': False}
key: ClassVar[str] = 'objectinsert'
required_args: 't.ClassVar[set[str]]' = {'value', 'key', 'this'}
230class OpenJSON(Expression, Func):
231    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key: ClassVar[str] = 'openjson'
required_args: 't.ClassVar[set[str]]' = {'this'}
234class ParseJSON(Expression, Func):
235    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
236    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
237    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
238    arg_types = {"this": True, "expression": False, "safe": False}
arg_types = {'this': True, 'expression': False, 'safe': False}
key: ClassVar[str] = 'parsejson'
required_args: 't.ClassVar[set[str]]' = {'this'}