From 6e9f90bc6ea47e17866dd48ba6b9bcf069c41ac3 Mon Sep 17 00:00:00 2001 From: Josh Wills Date: Mon, 9 Sep 2024 10:42:06 -0700 Subject: [PATCH 1/2] Add explicit register/unregister operations for python models --- dbt/include/duckdb/macros/adapters.sql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dbt/include/duckdb/macros/adapters.sql b/dbt/include/duckdb/macros/adapters.sql index 34af4346..73231c6c 100644 --- a/dbt/include/duckdb/macros/adapters.sql +++ b/dbt/include/duckdb/macros/adapters.sql @@ -101,7 +101,10 @@ def materialize(df, con): if pyarrow_available and isinstance(df, pyarrow.Table): # https://github.com/duckdb/duckdb/issues/6584 import pyarrow.dataset - con.execute('create table {{ relation }} as select * from df') + tmp_name = '__dbt_python_model_df_' + '{{ relation.identifier }}' + con.register(tmp_name, df) + con.execute('create table {{ relation }} as select * from ' + tmp_name) + con.unregister(tmp_name) {% endmacro %} {% macro duckdb__create_view_as(relation, sql) -%} From 5082d28d0703739074a8a0da41b0b532f241d514 Mon Sep 17 00:00:00 2001 From: Josh Wills Date: Mon, 9 Sep 2024 11:13:59 -0700 Subject: [PATCH 2/2] A version-independent fix for the datediff week handling in duckdb --- dbt/include/duckdb/macros/utils/datediff.sql | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dbt/include/duckdb/macros/utils/datediff.sql b/dbt/include/duckdb/macros/utils/datediff.sql index ccf3151e..10273ae7 100644 --- a/dbt/include/duckdb/macros/utils/datediff.sql +++ b/dbt/include/duckdb/macros/utils/datediff.sql @@ -1,3 +1,12 @@ {% macro duckdb__datediff(first_date, second_date, datepart) -%} - date_diff('{{ datepart }}', {{ first_date }}::timestamp, {{ second_date}}::timestamp ) + {% if datepart == 'week' %} + ({{ datediff(first_date, second_date, 'day') }} // 7 + case + when date_part('dow', ({{first_date}})::timestamp) <= date_part('dow', ({{second_date}})::timestamp) then + case when {{first_date}} <= {{second_date}} then 0 else -1 end + else + case when {{first_date}} <= {{second_date}} then 1 else 0 end + end) + {% else %} + (date_diff('{{ datepart }}', {{ first_date }}::timestamp, {{ second_date}}::timestamp )) + {% endif %} {%- endmacro %}