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
104
105class JSONExtract(Expression, Binary, Func):
106    arg_types = {
107        "this": True,
108        "expression": True,
109        "only_json_types": False,
110        "expressions": False,
111        "variant_extract": False,
112        "json_query": False,
113        "option": False,
114        "quote": False,
115        "on_condition": False,
116        "requires_json": False,
117        "emits": False,
118    }
119    _sql_names = ["JSON_EXTRACT"]
120    is_var_len_args = True
121
122    @property
123    def output_name(self) -> str:
124        return self.expression.output_name if not self.expressions else ""
125
126
127class JSONExtractArray(Expression, Func):
128    arg_types = {"this": True, "expression": False}
129    _sql_names = ["JSON_EXTRACT_ARRAY"]
130
131
132class JSONExtractScalar(Expression, Binary, Func):
133    arg_types = {
134        "this": True,
135        "expression": True,
136        "only_json_types": False,
137        "expressions": False,
138        "json_type": False,
139        "scalar_only": False,
140        "json_subtype": False,
141    }
142    _sql_names = ["JSON_EXTRACT_SCALAR"]
143    is_var_len_args = True
144
145    @property
146    def output_name(self) -> str:
147        return self.expression.output_name
148
149
150class JSONFormat(Expression, Func):
151    arg_types = {"this": False, "options": False, "is_json": False, "to_json": False}
152    _sql_names = ["JSON_FORMAT"]
153
154
155class JSONKeys(Expression, Func):
156    arg_types = {"this": True, "expression": False, "expressions": False}
157    is_var_len_args = True
158    _sql_names = ["JSON_KEYS"]
159
160
161class JSONKeysAtDepth(Expression, Func):
162    arg_types = {"this": True, "expression": False, "mode": False}
163
164
165class JSONObject(Expression, Func):
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 JSONObjectAgg(Expression, AggFunc):
176    arg_types = {
177        "expressions": False,
178        "null_handling": False,
179        "unique_keys": False,
180        "return_type": False,
181        "encoding": False,
182    }
183
184
185class JSONRemove(Expression, Func):
186    arg_types = {"this": True, "expressions": True}
187    is_var_len_args = True
188    _sql_names = ["JSON_REMOVE"]
189
190
191class JSONSet(Expression, Func):
192    arg_types = {"this": True, "expressions": True}
193    is_var_len_args = True
194    _sql_names = ["JSON_SET"]
195
196
197class JSONStripNulls(Expression, Func):
198    arg_types = {
199        "this": True,
200        "expression": False,
201        "include_arrays": False,
202        "remove_empty": False,
203    }
204    _sql_names = ["JSON_STRIP_NULLS"]
205
206
207class StripNullValue(Expression, Func):
208    pass
209
210
211class JSONTable(Expression, Func):
212    arg_types = {
213        "this": True,
214        "schema": True,
215        "path": False,
216        "error_handling": False,
217        "empty_handling": False,
218    }
219
220
221class JSONType(Expression, Func):
222    arg_types = {"this": True, "expression": False}
223    _sql_names = ["JSON_TYPE"]
224
225
226class ObjectId(Expression, Func):
227    arg_types = {"this": True, "expression": False}
228
229
230class ObjectInsert(Expression, Func):
231    arg_types = {
232        "this": True,
233        "key": True,
234        "value": True,
235        "update_flag": False,
236    }
237
238
239class OpenJSON(Expression, Func):
240    arg_types = {"this": True, "path": False, "expressions": False}
241
242
243class ParseJSON(Expression, Func):
244    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
245    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
246    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
247    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    }
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]]' = {'this', 'expressions'}
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]]' = {'this', 'expressions'}
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    }
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'}
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 ""
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
123    @property
124    def output_name(self) -> str:
125        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'}
128class JSONExtractArray(Expression, Func):
129    arg_types = {"this": True, "expression": False}
130    _sql_names = ["JSON_EXTRACT_ARRAY"]
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'jsonextractarray'
required_args: 't.ClassVar[set[str]]' = {'this'}
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
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
146    @property
147    def output_name(self) -> str:
148        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'}
151class JSONFormat(Expression, Func):
152    arg_types = {"this": False, "options": False, "is_json": False, "to_json": False}
153    _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()
156class JSONKeys(Expression, Func):
157    arg_types = {"this": True, "expression": False, "expressions": False}
158    is_var_len_args = True
159    _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'}
162class JSONKeysAtDepth(Expression, Func):
163    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'}
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    }
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()
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    }
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()
186class JSONRemove(Expression, Func):
187    arg_types = {"this": True, "expressions": True}
188    is_var_len_args = True
189    _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]]' = {'this', 'expressions'}
192class JSONSet(Expression, Func):
193    arg_types = {"this": True, "expressions": True}
194    is_var_len_args = True
195    _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]]' = {'this', 'expressions'}
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"]
arg_types = {'this': True, 'expression': False, 'include_arrays': False, 'remove_empty': False}
key: ClassVar[str] = 'jsonstripnulls'
required_args: 't.ClassVar[set[str]]' = {'this'}
208class StripNullValue(Expression, Func):
209    pass
key: ClassVar[str] = 'stripnullvalue'
required_args: 't.ClassVar[set[str]]' = {'this'}
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    }
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'}
222class JSONType(Expression, Func):
223    arg_types = {"this": True, "expression": False}
224    _sql_names = ["JSON_TYPE"]
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'jsontype'
required_args: 't.ClassVar[set[str]]' = {'this'}
227class ObjectId(Expression, Func):
228    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'objectid'
required_args: 't.ClassVar[set[str]]' = {'this'}
231class ObjectInsert(Expression, Func):
232    arg_types = {
233        "this": True,
234        "key": True,
235        "value": True,
236        "update_flag": False,
237    }
arg_types = {'this': True, 'key': True, 'value': True, 'update_flag': False}
key: ClassVar[str] = 'objectinsert'
required_args: 't.ClassVar[set[str]]' = {'key', 'this', 'value'}
240class OpenJSON(Expression, Func):
241    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'}
244class ParseJSON(Expression, Func):
245    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
246    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
247    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
248    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'}