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