Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring snapshot column names #1097

Merged
merged 11 commits into from
Sep 20, 2024
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240903-161003.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Allow configuring snapshot column names
time: 2024-09-03T16:10:03.021221-04:00
custom:
Author: gshank
Issue: "1096"
16 changes: 9 additions & 7 deletions dbt/include/spark/macros/materializations/snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


{% macro spark__snapshot_merge_sql(target, source, insert_cols) -%}
{%- set columns = config.get("snapshot_table_column_names") or get_snapshot_table_column_names() -%}

merge into {{ target }} as DBT_INTERNAL_DEST
{% if target.is_iceberg %}
Expand All @@ -21,12 +22,12 @@
{% else %}
using {{ source }} as DBT_INTERNAL_SOURCE
{% endif %}
on DBT_INTERNAL_SOURCE.dbt_scd_id = DBT_INTERNAL_DEST.dbt_scd_id
on DBT_INTERNAL_SOURCE.{{ columns.dbt_scd_id }} = DBT_INTERNAL_DEST.{{ columns.dbt_scd_id }}
when matched
and DBT_INTERNAL_DEST.dbt_valid_to is null
and DBT_INTERNAL_DEST.{{ columns.dbt_valid_to }} is null
and DBT_INTERNAL_SOURCE.dbt_change_type in ('update', 'delete')
then update
set dbt_valid_to = DBT_INTERNAL_SOURCE.dbt_valid_to
set {{ columns.dbt_valid_to }} = DBT_INTERNAL_SOURCE.{{ columns.dbt_valid_to }}

when not matched
and DBT_INTERNAL_SOURCE.dbt_change_type = 'insert'
Expand Down Expand Up @@ -81,13 +82,12 @@


{% materialization snapshot, adapter='spark' %}
{%- set config = model['config'] -%}

{%- set target_table = model.get('alias', model.get('name')) -%}

{%- set strategy_name = config.get('strategy') -%}
{%- set unique_key = config.get('unique_key') %}
{%- set file_format = config.get('file_format', 'parquet') -%}
{%- set file_format = config.get('file_format') or 'parquet' -%}
gshank marked this conversation as resolved.
Show resolved Hide resolved
{%- set grant_config = config.get('grants') -%}

{% set target_relation_exists, target_relation = get_or_create_relation(
Expand Down Expand Up @@ -126,7 +126,7 @@
{{ run_hooks(pre_hooks, inside_transaction=True) }}

{% set strategy_macro = strategy_dispatch(strategy_name) %}
{% set strategy = strategy_macro(model, "snapshotted_data", "source_data", config, target_relation_exists) %}
{% set strategy = strategy_macro(model, "snapshotted_data", "source_data", model['config'], target_relation_exists) %}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these are the same config. There's config in the globals, and then there's also config.model.config. Was there a bug or an issue that required this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I'm not sure why somebody was using the passed in config to start with (I suspect it was because using defaults is different for the config object and the config dictionary that got passed in), but you can't get a "property" from the dictionary, only the config object that you get from the context.


{% if not target_relation_exists %}

Expand All @@ -135,7 +135,9 @@

{% else %}

{{ adapter.valid_snapshot_target(target_relation) }}
{% set columns = config.get("snapshot_table_column_names") or get_snapshot_table_column_names() %}

{{ adapter.valid_snapshot_target(target_relation, columns) }}

{% set staging_table = spark_build_snapshot_staging_table(strategy, sql, target_relation) %}

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _get_plugin_version_dict():
install_requires=[
"sqlparams>=3.0.0",
"dbt-common>=1.0.4,<2.0",
"dbt-adapters>=1.1.1,<2.0",
"dbt-adapters>=1.7.0,<2.0",
# add dbt-core to ensure backwards compatibility of installation, this is not a functional dependency
"dbt-core>=1.8.0",
],
Expand Down
Loading