Skip to content

Commit

Permalink
Prototype of test materialization that allows materialization to be…
Browse files Browse the repository at this point in the history
… set in the config

skip-checks: true
  • Loading branch information
dbeatty10 committed Sep 29, 2023
1 parent 6a83646 commit 656a101
Showing 1 changed file with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@

{% set relations = [] %}

{% if should_store_failures() %}

{% set identifier = model['alias'] %}
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
{% set target_relation = api.Relation.create(
identifier=identifier, schema=schema, database=database, type='table') -%} %}

{% if old_relation %}
{% do adapter.drop_relation(old_relation) %}
{% endif %}

{% call statement(auto_begin=True) %}
{{ create_table_as(False, target_relation, sql) }}
{% endcall %}
-- default SQL to use for the relation to test; will be modified depending on materialization_type
{% set main_sql = sql %}

{% do relations.append(target_relation) %}
-- default value
{% set materialization_type = "ephemeral" %}
{{ log("materialization_type: " ~ materialization_type ~ " (default)", True) }}

{% set main_sql %}
select *
from {{ target_relation }}
{% endset %}
-- default value if storing failures
{% if should_store_failures() %}
{% set materialization_type = "table" %}
{% endif %}
{{ log("materialization_type: " ~ materialization_type ~ " (after considering should_store_failures())", True) }}

{{ adapter.commit() }}
-- override the default (but only if configured to do so)
{% set materialization_type = config.get("materialized") or materialization_type %}
{{ log("materialization_type: " ~ materialization_type ~ " (after considering test config())", True) }}

{% else %}
-- only allow certain materializations for now
{% if materialization_type not in ["test", "ephemeral", "table", "view", "materialized_view"] %}
{{ exceptions.raise_compiler_error("Invalid `materialization_type`. Got: " ~ materialization_type) }}
{% endif %}

{% set main_sql = sql %}
-- only a few of the allowed materializations actually create database objects
{% if materialization_type in ["table", "view", "materialized_view"] %}
{%- set target_relation = this.incorporate(type=materialization_type) -%}
{%- set materialization_macro = get_materialization_macro(materialization_type) -%}
{% set relations = materialization_macro() %}

{% set main_sql %}
select *
from {{ target_relation }}
{% endset %}
{% endif %}

{% set limit = config.get('limit') %}
{% set fail_calc = config.get('fail_calc') %}
{% set warn_if = config.get('warn_if') %}
{% set error_if = config.get('error_if') %}

{{ log("main_sql: " ~ main_sql, True) }}

{% call statement('main', fetch_result=True) -%}

{{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit)}}
Expand Down

0 comments on commit 656a101

Please sign in to comment.