Skip to content

Commit 2cdac75

Browse files
author
Sarah Morgan
authored
Add duckdb support to create_json_object macro (#890)
add macro for duckdb
1 parent eb6f995 commit 2cdac75

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

macros/cross_database_utils/create_json_object.sql

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
/* default */
4545
{% macro default__create_json_object(table_ref, group_by_col, object_col_name, object_col_list) %}
46-
{{ exceptions.raise_compiler_error("The macro create_json_object is not implemented for this adapter.") }}
46+
{{ exceptions.warn("The macro create_json_object is not implemented for this adapter.") }}
4747
{% endmacro %}
4848

4949
/* snowflake */
@@ -115,7 +115,6 @@ from (
115115
group by {{ group_by_col }}
116116
{% endmacro %}
117117

118-
119118
/* fabric */
120119
{% macro fabric__create_json_object(table_ref, group_by_col, object_col_name, object_col_list) %}
121120
select
@@ -138,3 +137,26 @@ select
138137
from {{ table_ref }}
139138
group by {{ group_by_col }}
140139
{% endmacro %}
140+
141+
/* duckdb */
142+
{% macro duckdb__create_json_object(table_ref, group_by_col, object_col_name, object_col_list) %}
143+
select
144+
{{ group_by_col }}
145+
, to_json(
146+
list(
147+
{
148+
{%- for col in object_col_list %}
149+
{% if not loop.first %}, {% endif -%}
150+
'{{ the_tuva_project.snake_to_camel(col) }}':
151+
{%- if 'list' in col | lower -%}
152+
{{ col }}::JSON /* Cast to JSON to handle lists properly */
153+
{%- else -%}
154+
{{ col }}
155+
{%- endif -%}
156+
{%- endfor %}
157+
}
158+
)
159+
) as {{ object_col_name }}
160+
from {{ table_ref }}
161+
group by {{ group_by_col }}
162+
{% endmacro %}

0 commit comments

Comments
 (0)