-
Notifications
You must be signed in to change notification settings - Fork 137
overriding microbatch macro for capturing the compiled code of the mi… #1000
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
36e1956
overriding microbatch macro for capturing the compiled code of the mi…
GuyEshdat bbd163d
fix test
GuyEshdat b6aac49
test fixes
GuyEshdat cecb3a6
test fixes
GuyEshdat 4271d1f
test fixes
GuyEshdat 1c26b1a
test fixes
GuyEshdat 7ae883b
test fixes
GuyEshdat 677b181
test fixes
GuyEshdat a80518a
test fixes
GuyEshdat dd629fd
test fixes
GuyEshdat 70ccd14
test fixes
GuyEshdat 736b2a1
test fixes
GuyEshdat 9c2d74f
improved comments and fixed tests for dremio and spark
GuyEshdat 20b2fae
fix spark tests
GuyEshdat 35a35ce
skip spark as it has a different microbatch implementation
GuyEshdat b4407f0
fixed compiled code formatting in redshift in microbatch models
GuyEshdat 51d3872
avoiding race condition when updating the microbatch_compiled_code_by…
GuyEshdat 6f358de
Merge branch 'master' of github.com:elementary-data/dbt-data-reliabil…
GuyEshdat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
143 changes: 143 additions & 0 deletions
143
integration_tests/tests/test_dbt_artifacts/test_microbatch_compiled_code.py
This file contains hidden or 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,143 @@ | ||
| from contextlib import contextmanager | ||
|
|
||
| import pytest | ||
|
|
||
| from dbt_project import DbtProject | ||
|
|
||
|
|
||
| def _microbatch_source_model_sql() -> str: | ||
| return """ | ||
| {{ config(event_time='order_date') }} | ||
| {% set event_time_data_type = 'datetime2' if target.type == 'sqlserver' else 'timestamp' %} | ||
|
|
||
| select | ||
| 1 as order_id, | ||
| 1 as customer_id, | ||
| 42 as amount, | ||
| cast('2024-01-01 00:00:00' as {{ event_time_data_type }}) as order_date | ||
| union all | ||
| select | ||
| 2 as order_id, | ||
| 2 as customer_id, | ||
| 84 as amount, | ||
| cast('2025-01-01 00:00:00' as {{ event_time_data_type }}) as order_date | ||
| """ | ||
|
|
||
|
|
||
| def _microbatch_model_sql(source_model_name: str) -> str: | ||
| return """ | ||
| {% set model_config = { | ||
| "materialized": "incremental", | ||
| "incremental_strategy": "microbatch", | ||
| "event_time": "order_date", | ||
| "batch_size": "year", | ||
| "begin": "2024-01-01" | ||
| } %} | ||
| {% if target.type != "duckdb" %} | ||
| {% do model_config.update({"unique_key": "order_id"}) %} | ||
| {% endif %} | ||
| {{ config(**model_config) }} | ||
|
|
||
| select | ||
| order_id, | ||
| customer_id, | ||
| amount, | ||
| order_date | ||
| from {{ ref('__MICROBATCH_SOURCE_MODEL__') }} | ||
| """.replace("__MICROBATCH_SOURCE_MODEL__", source_model_name) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _with_microbatch_test_models(dbt_project: DbtProject, model_suffix: str): | ||
| source_model_name = f"mb_src_{model_suffix}" | ||
| target_model_name = f"mb_tgt_{model_suffix}" | ||
| source_model_path = dbt_project.tmp_models_dir_path.joinpath(f"{source_model_name}.sql") | ||
| target_model_path = dbt_project.tmp_models_dir_path.joinpath(f"{target_model_name}.sql") | ||
|
|
||
| source_model_path.write_text(_microbatch_source_model_sql()) | ||
| target_model_path.write_text(_microbatch_model_sql(source_model_name)) | ||
| relative_source_model_path = source_model_path.relative_to(dbt_project.project_dir_path) | ||
| relative_target_model_path = target_model_path.relative_to(dbt_project.project_dir_path) | ||
| try: | ||
| yield relative_source_model_path, relative_target_model_path, target_model_name | ||
| finally: | ||
| if source_model_path.exists(): | ||
| source_model_path.unlink() | ||
| if target_model_path.exists(): | ||
| target_model_path.unlink() | ||
|
|
||
|
|
||
| def _run_microbatch_model_and_get_latest_success_result( | ||
| dbt_project: DbtProject, model_suffix: str | ||
| ): | ||
| with _with_microbatch_test_models(dbt_project, model_suffix) as ( | ||
| source_model_path, | ||
| model_path, | ||
| target_model_name, | ||
| ): | ||
| dbt_project.dbt_runner.run( | ||
| select=f"{source_model_path} {model_path}" | ||
| ) | ||
|
|
||
| unique_id = f"model.elementary_tests.{target_model_name}" | ||
| run_results = dbt_project.read_table( | ||
| "dbt_run_results", | ||
| where=f"unique_id = '{unique_id}' and status = 'success'", | ||
| order_by="generated_at desc", | ||
| limit=1, | ||
| ) | ||
| return run_results | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _with_microbatch_macro_file(dbt_project: DbtProject, macro_name: str): | ||
| macro_path = ( | ||
| dbt_project.project_dir_path / "macros" / "microbatch.sql" | ||
| ) | ||
| macro_sql = """ | ||
| {% macro __MACRO_NAME__(arg_dict) %} | ||
| {{ return(elementary.get_incremental_microbatch_sql(arg_dict)) }} | ||
| {% endmacro %} | ||
| """.replace("__MACRO_NAME__", macro_name) | ||
| if macro_path.exists(): | ||
| raise FileExistsError(f"Expected no macro file at {macro_path}") | ||
|
|
||
| macro_path.write_text(macro_sql) | ||
| try: | ||
| yield | ||
| finally: | ||
| if macro_path.exists(): | ||
| macro_path.unlink() | ||
|
|
||
|
|
||
| @pytest.mark.skip_targets(["spark", "vertica", "bigquery", "athena", "clickhouse", "dremio"]) | ||
| @pytest.mark.skip_for_dbt_fusion | ||
| @pytest.mark.parametrize( | ||
| "macro_name,expected_compiled_code,model_suffix", | ||
| [ | ||
| ("get_incremental_microbatch_sql", True, "with_override"), | ||
| ("get_incremental_microbatch_sql_not_used", False, "without_override"), | ||
| ], | ||
| ids=["with_override", "without_override"], | ||
| ) | ||
| def test_microbatch_run_results_compiled_code_behavior( | ||
| dbt_project: DbtProject, | ||
| macro_name: str, | ||
| expected_compiled_code: bool, | ||
| model_suffix: str, | ||
| ): | ||
| dbt_project.dbt_runner.vars["disable_run_results"] = False | ||
|
|
||
| with _with_microbatch_macro_file(dbt_project, macro_name): | ||
| run_results = _run_microbatch_model_and_get_latest_success_result( | ||
| dbt_project, model_suffix | ||
| ) | ||
| assert run_results, "Expected a successful run result row for microbatch model" | ||
| if expected_compiled_code: | ||
| assert run_results[0]["compiled_code"], ( | ||
| "Expected compiled_code to be populated when override macro is present" | ||
| ) | ||
| else: | ||
| assert not run_results[0]["compiled_code"], ( | ||
| "Expected compiled_code to stay empty when override macro is absent" | ||
| ) |
50 changes: 50 additions & 0 deletions
50
macros/edr/dbt_artifacts/microbatch/capture_microbatch_compiled_code.sql
This file contains hidden or 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,50 @@ | ||
| {#- | ||
| NOTE FOR PACKAGE CONSUMERS: | ||
| This package macro is not guaranteed to be picked up automatically by dbt's | ||
| incremental strategy resolution in all projects. | ||
| To apply this behavior, users should: | ||
| 1) Override `get_incremental_microbatch_sql` in their own project and delegate to | ||
| `elementary.get_incremental_microbatch_sql(arg_dict)`. | ||
| 2) Enable dbt behavior flag `require_batched_execution_for_custom_microbatch_strategy`. | ||
|
|
||
| This flow is currently not supported for adapters: | ||
| - spark | ||
| - bigquery | ||
| - athena | ||
| - clickhouse | ||
| - dremio | ||
| - vertica | ||
|
|
||
| This flow is currently not supported for dbt Fusion. | ||
| -#} | ||
| {% macro get_incremental_microbatch_sql(arg_dict) %} | ||
| {% if execute and model is defined %} | ||
| {% do elementary.capture_microbatch_compiled_code_for_model() %} | ||
| {% endif %} | ||
|
|
||
| {{ return(adapter.dispatch("get_incremental_microbatch_sql", "dbt")(arg_dict)) }} | ||
| {% endmacro %} | ||
|
|
||
|
|
||
| {% macro capture_microbatch_compiled_code_for_model() %} | ||
| {% set model_unique_id = ( | ||
| model.get("unique_id") if model is mapping else model.unique_id | ||
| ) | default(none, true) %} | ||
| {% set model_compiled_code = ( | ||
| model.get("compiled_code") if model is mapping else model.compiled_code | ||
| ) | default(none, true) %} | ||
| {% if model_unique_id is none %} | ||
| {{ return(none) }} | ||
| {% endif %} | ||
| {% if not model_compiled_code %} | ||
| {{ return(none) }} | ||
| {% endif %} | ||
|
|
||
| {% set compiled_code_by_unique_id = elementary.get_cache( | ||
| "microbatch_compiled_code_by_unique_id" | ||
| ) %} | ||
| {% if model_unique_id in compiled_code_by_unique_id %} | ||
| {{ return(none) }} | ||
| {% endif %} | ||
| {% do compiled_code_by_unique_id.update({model_unique_id: model_compiled_code}) %} | ||
| {% endmacro %} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Microbatch cache fallback bypasses Redshift
%→%%escapingOn Redshift,
redshift__get_compiled_code(macros/utils/graph/get_compiled_code.sql:21-25) replaces%with%%to prevent SQL formatting errors when the compiled code is inserted as a string literal. The new microbatch cache fallback path (lines 3-7) retrieves compiled code directly from the cache without going throughadapter.dispatch, so the Redshift-specific escaping is never applied. If a microbatch model's compiled SQL contains%characters, this will produce unescaped%in the INSERT intodbt_run_results, which can cause runtime SQL errors on Redshift (e.g.,incomplete formator similar adapter-level failures).Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed