Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enable Pandas and Pandas-on-Spark DataFrames for dbt python models ([dbt-labs/dbt-spark#469](https://github.com/dbt-labs/dbt-spark/pull/469), [#181](https://github.com/databricks/dbt-databricks/pull/181))
- Implement testing for a test for various Python models ([#189](https://github.com/databricks/dbt-databricks/pull/189))
- Implement testing for `type_boolean` in Databricks ([dbt-labs/dbt-spark#471](https://github.com/dbt-labs/dbt-spark/pull/471), [#188](https://github.com/databricks/dbt-databricks/pull/188))
- Add a macro to support [COPY INTO](https://docs.databricks.com/spark/latest/spark-sql/language-manual/delta-copy-into.html) ([#190](https://github.com/databricks/dbt-databricks/pull/190))

### Under the hood
- Apply "Initial refactoring of incremental materialization" ([#148](https://github.com/databricks/dbt-databricks/pull/148))
Expand Down
76 changes: 76 additions & 0 deletions dbt/include/databricks/macros/copy_into.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{% macro databricks_copy_into(
target_table,
source,
file_format,
expression_list=none,
source_credential=none,
source_encryption=none,
validate=none,
files=none,
format_options=none,
copy_options=none) -%}

{% set target_relation_exists, target_relation = get_or_create_relation(
database=target.database,
schema=target.schema,
identifier=target_table,
type='table') -%}

{%- set source_clause -%}
{%- if expression_list -%}
select {{ expression_list }} from '{{ source }}'
{%- else -%}
'{{ source }}'
{%- endif -%}
{%- if source_credential or source_encryption %}
WITH (
{% if source_credential -%}
credential (
{%- for name in source_credential -%}
'{{ name }}' = '{{ source_credential[name] }}' {%- if not loop.last %}, {% endif -%}
{%- endfor -%}
)
{%- endif %}
{% if source_encryption -%}
encryption (
{%- for name in source_encryption -%}
'{{ name }}' = '{{ source_encryption[name] }}' {%- if not loop.last %}, {% endif -%}
{%- endfor -%}
)
{%- endif %}
)
{%- endif -%}
{%- endset -%}

{% set query %}
copy into {{ target_relation }}
from {{ source_clause }}
fileformat = {{ file_format }}
{% if validate -%} validate {{ validate }} {%- endif %}
{% if files -%}
files = (
{%- for file in files -%}
'{{ file }}' {%- if not loop.last %}, {% endif -%}
{%- endfor -%}
)
{%- endif %}
{% if format_options -%}
format_options (
{%- for key in format_options -%}
'{{ key }}' = '{{ format_options[key] }}' {%- if not loop.last %}, {% endif -%}
{%- endfor -%}
)
{%- endif %}
{% if copy_options -%}
copy_options (
{%- for key in copy_options -%}
'{{ key }}' = '{{ copy_options[key] }}' {%- if not loop.last %}, {% endif -%}
{%- endfor -%}
)
{%- endif %}
{% endset %}

{% do log("Running COPY INTO" ~ query, info=True) %}
{% do run_query(query) %}

{% endmacro %}
7 changes: 7 additions & 0 deletions tests/integration/copy_into/models/expected_target.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{config(materialized='table')}}

select * from values
(0, 'Zero', '2022-01-01'),
(1, 'Alice', '2022-01-01'),
(2, 'Bob', '2022-01-02')
as t(id, name, date)
15 changes: 15 additions & 0 deletions tests/integration/copy_into/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

seeds:
- name: source
config:
column_types:
id: int
name: string
date: string
- name: target
config:
column_types:
id: int
name: string
date: string
6 changes: 6 additions & 0 deletions tests/integration/copy_into/models/source.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{config(materialized='table', file_format='parquet')}}

select * from values
(1, 'Alice', '2022-01-01'),
(2, 'Bob', '2022-01-02')
t(id, name, date)
4 changes: 4 additions & 0 deletions tests/integration/copy_into/models/target.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{config(materialized='table')}}

select * from values
(0, 'Zero', '2022-01-01') as t(id, name, date)
50 changes: 50 additions & 0 deletions tests/integration/copy_into/test_copy_into.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.integration.base import DBTIntegrationTest, use_profile


args = """
target_table: target
source: {source_path}
file_format: parquet
format_options:
mergeSchema: 'true'
copy_options:
mergeSchema: 'true'
"""


class TestCopyInto(DBTIntegrationTest):
@property
def schema(self):
return "copy_into"

@property
def models(self):
return "models"

def test_copy_into(self):
self.run_dbt(["run"])
# Get the location of the source table.
rows = self.run_sql("describe table extended {database_schema}.source", fetch="all")
path = None
for row in rows:
if row.col_name == "Location":
path = row.data_type
if path is None:
raise Exception("No location found for the source table")
self.run_dbt(
[
"run-operation",
"databricks_copy_into",
"--args",
args.format(source_path=path),
]
)
self.assertTablesEqual("target", "expected_target")

@use_profile("databricks_cluster")
def test_databricks_cluster(self):
self.test_copy_into()

@use_profile("databricks_sql_endpoint")
def test_databricks_sql_endpoint(self):
self.test_copy_into()
Comment on lines +44 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this feature not support UC?

Copy link
Collaborator Author

@allisonwang-db allisonwang-db Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested against a UC cluster and I don't think UC can support this test case:

  1. UC does not allow the creation of parquet tables (only Delta is supported)
  2. UC does not allow access to the underlying path of a managed Delta table (so we can't get the source in the test)

But UC should support COPY INTO: https://docs.databricks.com/ingestion/copy-into/unity-catalog.html