Skip to content

Commit

Permalink
Save xlsx files beside sql files
Browse files Browse the repository at this point in the history
  • Loading branch information
pgoslatara committed May 2, 2023
1 parent 61747df commit 01cb727
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dbt/adapters/excel/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class ExcelAdapter(DuckDBAdapter):
def output_excel(self, location):
import pandas as pd

pd.read_parquet(location + ".parquet").to_excel(location)
pd.read_parquet(location.replace(".xlsx", ".parquet")).to_excel(location)
2 changes: 1 addition & 1 deletion dbt/include/excel/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

{% elif format == 'xlsx' %}
{% set copy_to %}
copy {{ relation }} to '{{ location }}.parquet' (FORMAT 'parquet');
copy {{ relation }} to '{{ location.replace(".xlsx", ".parquet") }}' (FORMAT 'parquet');
{% endset %}

{% else %}
Expand Down
14 changes: 12 additions & 2 deletions dbt/include/excel/macros/materializations/external.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{% materialization external, adapter="excel", supported_languages=['sql', 'python'] %}

{%- set format = render(config.get('format', default='parquet')) -%}
{%- set location = render(config.get('location', default=external_location(this, format))) -%}
{%- set delimiter = render(config.get('delimiter', default=',')) -%}
{%- set glue_register = config.get('glue_register', default=false) -%}
{%- set glue_database = render(config.get('glue_database', default='default')) -%}

{#
For xlsx formats the default location is the same directory as the .sql file.
If a `location` is passed in the config then this takes priority.
For all other formats the `location` parameter is prioritised.
#}
{% if format == 'xlsx' and config.get('location', 'not passed') == 'not passed' %}
{%- set location = render(model.original_file_path.split('.')[0] + '.' + format) -%}
{% else %}
{%- set location = render(config.get('location', default=external_location(this, format))) -%}
{% endif %}

-- set language - python or sql
{%- set language = model['language'] -%}

Expand Down Expand Up @@ -53,7 +63,7 @@
{% endif %}
{% call statement('main', language='sql') -%}
create or replace view {{ intermediate_relation.include(database=adapter.use_database()) }} as (
select * from '{{ location }}'
select * from '{{ location.replace(".xlsx", "") }}'
);
{%- endcall %}

Expand Down
1 change: 0 additions & 1 deletion docs/jaffle_shop_with_dbt_excel/models/customers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

config(
materialized='external',
location='./customers.xlsx',
format="xlsx"
)

Expand Down

0 comments on commit 01cb727

Please sign in to comment.