forked from airbytehq/airbyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_operations.sql
279 lines (228 loc) · 13.2 KB
/
json_operations.sql
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{#
Adapter Macros for the following functions:
- Bigquery: JSON_EXTRACT(json_string_expr, json_path_format) -> https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions
- Snowflake: JSON_EXTRACT_PATH_TEXT( <column_identifier> , '<path_name>' ) -> https://docs.snowflake.com/en/sql-reference/functions/json_extract_path_text.html
- Redshift: json_extract_path_text('json_string', 'path_elem' [,'path_elem'[, ...] ] [, null_if_invalid ] ) -> https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html
- Postgres: json_extract_path_text(<from_json>, 'path' [, 'path' [, ...}}) -> https://www.postgresql.org/docs/12/functions-json.html
- MySQL: JSON_EXTRACT(json_doc, 'path' [, 'path'] ...) -> https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
- ClickHouse: JSONExtractString(json_doc, 'path' [, 'path'] ...) -> https://clickhouse.com/docs/en/sql-reference/functions/json-functions/
- Databricks: get_json_object(json_txt, 'path') -> https://spark.apache.org/docs/latest/api/sql/#get_json_object
#}
{# format_json_path -------------------------------------------------- #}
{% macro format_json_path(json_path_list) -%}
{{ adapter.dispatch('format_json_path')(json_path_list) }}
{%- endmacro %}
{% macro default__format_json_path(json_path_list) -%}
{{ '.' ~ json_path_list|join('.') }}
{%- endmacro %}
{% macro oracle__format_json_path(json_path_list) -%}
{{ '\'$."' ~ json_path_list|join('."') ~ '"\'' }}
{%- endmacro %}
{% macro bigquery__format_json_path(json_path_list) -%}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace('"', '\\"')) -%} {%- endif -%}
{%- endfor -%}
{{ '"$[\'' ~ str_list|join('\'][\'') ~ '\']"' }}
{%- endmacro %}
{% macro postgres__format_json_path(json_path_list) -%}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace("'", "''")) -%} {%- endif -%}
{%- endfor -%}
{{ "'" ~ str_list|join("','") ~ "'" }}
{%- endmacro %}
{% macro mysql__format_json_path(json_path_list) -%}
{# -- '$."x"."y"."z"' #}
{{ "'$.\"" ~ json_path_list|join(".") ~ "\"'" }}
{%- endmacro %}
{% macro databricks__format_json_path(json_path_list) -%}
{# -- '$.x.y.z' #}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace("'", "\\'")) -%} {%- endif -%}
{%- endfor -%}
{{ "'$." ~ str_list|join(".") ~ "'" }}
{%- endmacro %}
{% macro redshift__format_json_path(json_path_list) -%}
{%- set quote = '"' if redshift_super_type() else "'" -%}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace(quote, quote + quote)) -%} {%- endif -%}
{%- endfor -%}
{{ quote ~ str_list|join(quote + "," + quote) ~ quote }}
{%- endmacro %}
{% macro snowflake__format_json_path(json_path_list) -%}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace("'", "''").replace('"', '""')) -%} {%- endif -%}
{%- endfor -%}
{{ "'\"" ~ str_list|join('"."') ~ "\"'" }}
{%- endmacro %}
{% macro sqlserver__format_json_path(json_path_list) -%}
{# -- '$."x"."y"."z"' #}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace("'", "''").replace('"', '\\"')) -%} {%- endif -%}
{%- endfor -%}
{{ "'$.\"" ~ str_list|join(".") ~ "\"'" }}
{%- endmacro %}
{% macro clickhouse__format_json_path(json_path_list) -%}
{%- set str_list = [] -%}
{%- for json_path in json_path_list -%}
{%- if str_list.append(json_path.replace("'", "''").replace('"', '\\"')) -%} {%- endif -%}
{%- endfor -%}
{{ "'" ~ str_list|join("','") ~ "'" }}
{%- endmacro %}
{# json_extract ------------------------------------------------- #}
{% macro json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{{ adapter.dispatch('json_extract')(from_table, json_column, json_path_list, normalized_json_path) }}
{%- endmacro %}
{% macro default__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
json_extract({{ from_table}}.{{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro databricks__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
get_json_object({{ json_column }}, {{ format_json_path(json_path_list) }})
{% else %}
get_json_object({{ from_table }}.{{ json_column }}, {{ format_json_path(json_path_list) }})
{% endif -%}
{%- endmacro %}
{% macro oracle__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
json_value({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro bigquery__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
json_extract({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{% else %}
json_extract({{ from_table}}.{{ json_column }}, {{ format_json_path(normalized_json_path) }})
{% endif -%}
{%- endmacro %}
{% macro postgres__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
jsonb_extract_path({{ json_column }}, {{ format_json_path(json_path_list) }})
{% else %}
jsonb_extract_path({{ from_table }}.{{ json_column }}, {{ format_json_path(json_path_list) }})
{% endif -%}
{%- endmacro %}
{% macro mysql__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
json_extract({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{% else %}
json_extract({{ from_table }}.{{ json_column }}, {{ format_json_path(normalized_json_path) }})
{% endif -%}
{%- endmacro %}
{% macro redshift__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() != '' -%}
{%- set json_column = from_table|string() + "." + json_column|string() -%}
{%- endif -%}
{%- if redshift_super_type() -%}
case when {{ json_column }}.{{ format_json_path(json_path_list) }} != '' then {{ json_column }}.{{ format_json_path(json_path_list) }} end
{%- else -%}
case when json_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }}, true) != '' then json_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }}, true) end
{%- endif -%}
{%- endmacro %}
{% macro snowflake__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
get_path(parse_json({{ json_column }}), {{ format_json_path(json_path_list) }})
{% else %}
get_path(parse_json({{ from_table }}.{{ json_column }}), {{ format_json_path(json_path_list) }})
{% endif -%}
{%- endmacro %}
{% macro sqlserver__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
json_query({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro clickhouse__json_extract(from_table, json_column, json_path_list, normalized_json_path) -%}
{%- if from_table|string() == '' %}
JSONExtractRaw(assumeNotNull({{ json_column }}), {{ format_json_path(json_path_list) }})
{% else %}
JSONExtractRaw(assumeNotNull({{ from_table }}.{{ json_column }}), {{ format_json_path(json_path_list) }})
{% endif -%}
{%- endmacro %}
{# json_extract_scalar ------------------------------------------------- #}
{% macro json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
{{ adapter.dispatch('json_extract_scalar')(json_column, json_path_list, normalized_json_path) }}
{%- endmacro %}
{% macro default__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
json_extract_scalar({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro oracle__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
json_value({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro bigquery__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
json_extract_scalar({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro postgres__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
jsonb_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro mysql__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
json_value({{ json_column }}, {{ format_json_path(normalized_json_path) }} RETURNING CHAR)
{%- endmacro %}
{% macro redshift__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
{%- if redshift_super_type() -%}
case when {{ json_column }}.{{ format_json_path(json_path_list) }} != '' then {{ json_column }}.{{ format_json_path(json_path_list) }} end
{%- else -%}
case when json_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }}, true) != '' then json_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }}, true) end
{%- endif -%}
{%- endmacro %}
{% macro snowflake__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
to_varchar(get_path(parse_json({{ json_column }}), {{ format_json_path(json_path_list) }}))
{%- endmacro %}
{% macro sqlserver__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
json_value({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro clickhouse__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
JSONExtractRaw(assumeNotNull({{ json_column }}), {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro databricks__json_extract_scalar(json_column, json_path_list, normalized_json_path) -%}
get_json_object({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{# json_extract_array ------------------------------------------------- #}
{% macro json_extract_array(json_column, json_path_list, normalized_json_path) -%}
{{ adapter.dispatch('json_extract_array')(json_column, json_path_list, normalized_json_path) }}
{%- endmacro %}
{% macro default__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
json_extract_array({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro oracle__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
json_value({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro bigquery__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
json_extract_array({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro postgres__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
jsonb_extract_path({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro mysql__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
json_extract({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}
{% macro redshift__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
{%- if redshift_super_type() -%}
{{ json_column }}.{{ format_json_path(json_path_list) }}
{%- else -%}
json_extract_path_text({{ json_column }}, {{ format_json_path(json_path_list) }}, true)
{%- endif -%}
{%- endmacro %}
{% macro snowflake__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
get_path(parse_json({{ json_column }}), {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro sqlserver__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
json_query({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro clickhouse__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
JSONExtractArrayRaw(assumeNotNull({{ json_column }}), {{ format_json_path(json_path_list) }})
{%- endmacro %}
{% macro databricks__json_extract_array(json_column, json_path_list, normalized_json_path) -%}
get_json_object({{ json_column }}, {{ format_json_path(json_path_list) }})
{%- endmacro %}
{# json_extract_string_array ------------------------------------------------- #}
{% macro json_extract_string_array(json_column, json_path_list, normalized_json_path) -%}
{{ adapter.dispatch('json_extract_string_array')(json_column, json_path_list, normalized_json_path) }}
{%- endmacro %}
{% macro default__json_extract_string_array(json_column, json_path_list, normalized_json_path) -%}
{{ json_extract_array(json_column, json_path_list, normalized_json_path) }}
{%- endmacro %}
# https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#json_extract_string_array
{% macro bigquery__json_extract_string_array(json_column, json_path_list, normalized_json_path) -%}
json_extract_string_array({{ json_column }}, {{ format_json_path(normalized_json_path) }})
{%- endmacro %}