-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow custom schema def for tmp tables generated by incremental (…
…#659) Co-authored-by: nicor88 <[email protected]>
- Loading branch information
Showing
7 changed files
with
174 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tests/functional/adapter/test_incremental_tmp_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import pytest | ||
import yaml | ||
from tests.functional.adapter.utils.parse_dbt_run_output import ( | ||
extract_create_statement_table_names, | ||
extract_running_create_statements, | ||
) | ||
|
||
from dbt.contracts.results import RunStatus | ||
from dbt.tests.util import run_dbt | ||
|
||
models__schema_tmp_sql = """ | ||
{{ config( | ||
materialized='incremental', | ||
incremental_strategy='insert_overwrite', | ||
partitioned_by=['date_column'], | ||
temp_schema=var('temp_schema_name') | ||
) | ||
}} | ||
select | ||
random() as rnd, | ||
cast(from_iso8601_date('{{ var('logical_date') }}') as date) as date_column | ||
""" | ||
|
||
|
||
class TestIncrementalTmpSchema: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"schema_tmp.sql": models__schema_tmp_sql} | ||
|
||
def test__schema_tmp(self, project, capsys): | ||
relation_name = "schema_tmp" | ||
temp_schema_name = f"{project.test_schema}_tmp" | ||
drop_temp_schema = f"drop schema if exists `{temp_schema_name}` cascade" | ||
model_run_result_row_count_query = f"select count(*) as records from {project.test_schema}.{relation_name}" | ||
|
||
vars_dict = { | ||
"temp_schema_name": temp_schema_name, | ||
"logical_date": "2024-01-01", | ||
} | ||
|
||
first_model_run = run_dbt( | ||
[ | ||
"run", | ||
"--select", | ||
relation_name, | ||
"--vars", | ||
yaml.safe_dump(vars_dict), | ||
"--log-level", | ||
"debug", | ||
"--log-format", | ||
"json", | ||
] | ||
) | ||
|
||
first_model_run_result = first_model_run.results[0] | ||
|
||
assert first_model_run_result.status == RunStatus.Success | ||
|
||
records_count_first_run = project.run_sql(model_run_result_row_count_query, fetch="all")[0][0] | ||
|
||
assert records_count_first_run == 1 | ||
|
||
out, _ = capsys.readouterr() | ||
athena_running_create_statements = extract_running_create_statements(out, relation_name) | ||
|
||
assert len(athena_running_create_statements) == 1 | ||
|
||
incremental_model_run_result_table_name = extract_create_statement_table_names( | ||
athena_running_create_statements[0] | ||
)[0] | ||
|
||
assert temp_schema_name not in incremental_model_run_result_table_name | ||
|
||
vars_dict["logical_date"] = "2024-01-02" | ||
incremental_model_run = run_dbt( | ||
[ | ||
"run", | ||
"--select", | ||
relation_name, | ||
"--vars", | ||
yaml.safe_dump(vars_dict), | ||
"--log-level", | ||
"debug", | ||
"--log-format", | ||
"json", | ||
] | ||
) | ||
|
||
incremental_model_run_result = incremental_model_run.results[0] | ||
|
||
assert incremental_model_run_result.status == RunStatus.Success | ||
|
||
records_count_incremental_run = project.run_sql(model_run_result_row_count_query, fetch="all")[0][0] | ||
|
||
assert records_count_incremental_run == 2 | ||
|
||
out, _ = capsys.readouterr() | ||
athena_running_create_statements = extract_running_create_statements(out, relation_name) | ||
|
||
assert len(athena_running_create_statements) == 1 | ||
|
||
incremental_model_run_result_table_name = extract_create_statement_table_names( | ||
athena_running_create_statements[0] | ||
)[0] | ||
|
||
assert temp_schema_name == incremental_model_run_result_table_name.split(".")[1].strip('"') | ||
|
||
project.run_sql(drop_temp_schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import re | ||
from typing import List | ||
|
||
|
||
def extract_running_create_statements(dbt_run_capsys_output: str, relation_name: str) -> List[str]: | ||
sql_create_statements = [] | ||
# Skipping "Invoking dbt with ['run', '--select', 'unique_tmp_table_suffix'..." | ||
for events_msg in dbt_run_capsys_output.split("\n")[1:]: | ||
base_msg_data = None | ||
# Best effort solution to avoid invalid records and blank lines | ||
try: | ||
base_msg_data = json.loads(events_msg).get("data") | ||
except json.JSONDecodeError: | ||
pass | ||
"""First run will not produce data.sql object in the execution logs, only data.base_msg | ||
containing the "Running Athena query:" initial create statement. | ||
Subsequent incremental runs will only contain the insert from the tmp table into the model | ||
table destination. | ||
Since we want to compare both run create statements, we need to handle both cases""" | ||
if base_msg_data: | ||
base_msg = base_msg_data.get("base_msg") | ||
if "Running Athena query:" in str(base_msg): | ||
if "create table" in base_msg: | ||
sql_create_statements.append(base_msg) | ||
|
||
if base_msg_data.get("conn_name") == f"model.test.{relation_name}" and "sql" in base_msg_data: | ||
if "create table" in base_msg_data.get("sql"): | ||
sql_create_statements.append(base_msg_data.get("sql")) | ||
|
||
return sql_create_statements | ||
|
||
|
||
def extract_create_statement_table_names(sql_create_statement: str) -> List[str]: | ||
table_names = re.findall(r"(?s)(?<=create table ).*?(?=with)", sql_create_statement) | ||
return [table_name.rstrip() for table_name in table_names] |