-
Notifications
You must be signed in to change notification settings - Fork 294
feat: Add watcher mode support for dbt test node states #2318
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
tatiana
merged 5 commits into
astronomer:main
from
michal-mrazek:feat/watcher-test-operator-support
Mar 3, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da0ac02
feat: Add watcher mode support for dbt test node states
michal-mrazek bda4941
rename all dbt node variables for cleaner code
michal-mrazek 9f5332f
handle the dbt test aggregation
michal-mrazek 5f01621
comments
michal-mrazek 0826b5b
tests
michal-mrazek 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
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
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 |
|---|---|---|
| @@ -1,6 +1,22 @@ | ||
| from __future__ import annotations | ||
|
|
||
| __all__ = ["get_xcom_val", "safe_xcom_push", "build_producer_state_fetcher", "WatcherTrigger", "_parse_compressed_xcom"] | ||
| __all__ = [ | ||
| "get_xcom_val", | ||
| "safe_xcom_push", | ||
| "build_producer_state_fetcher", | ||
| "is_dbt_node_status_success", | ||
| "is_dbt_node_status_failed", | ||
| "is_dbt_node_status_terminal", | ||
| "WatcherTrigger", | ||
| "_parse_compressed_xcom", | ||
| ] | ||
|
|
||
| from cosmos.operators._watcher.state import build_producer_state_fetcher, get_xcom_val, safe_xcom_push | ||
| from cosmos.operators._watcher.state import ( | ||
| build_producer_state_fetcher, | ||
| get_xcom_val, | ||
| is_dbt_node_status_failed, | ||
| is_dbt_node_status_success, | ||
| is_dbt_node_status_terminal, | ||
| safe_xcom_push, | ||
| ) | ||
| from cosmos.operators._watcher.triggerer import WatcherTrigger, _parse_compressed_xcom |
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,93 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from threading import Lock | ||
| from typing import Any | ||
|
|
||
| from cosmos.log import get_logger | ||
| from cosmos.operators._watcher.state import DbtTestStatus, is_dbt_node_status_success, safe_xcom_push | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # Protects all mutations of ``test_results_per_model`` so that concurrent | ||
| # dbt threads cannot interleave ``setdefault`` / ``append`` / ``len`` checks. | ||
| _test_results_lock = Lock() | ||
|
|
||
|
|
||
| def get_tests_status_xcom_key(model_uid: str) -> str: | ||
| """Return the XCom key used to store the aggregated test status for a model.""" | ||
| return f"{model_uid.replace('.', '__')}_tests_status" | ||
|
|
||
|
|
||
| def accumulate_test_result( | ||
| test_unique_id: str, | ||
| status: str, | ||
| tests_per_model: dict[str, list[str]], | ||
| test_results_per_model: dict[str, list[str]], | ||
| ) -> str | None: | ||
| """Accumulate a test's terminal status into test_results_per_model for its parent model. | ||
|
|
||
| Returns the parent model's unique_id if found, else None. | ||
| """ | ||
| for model_uid, test_uids in tests_per_model.items(): | ||
| if test_unique_id in test_uids: | ||
| test_results_per_model.setdefault(model_uid, []).append(status) | ||
| return model_uid | ||
|
michal-mrazek marked this conversation as resolved.
|
||
| return None | ||
|
tatiana marked this conversation as resolved.
tatiana marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def get_aggregated_test_status( | ||
| model_uid: str, | ||
| tests_per_model: dict[str, list[str]], | ||
| test_results_per_model: dict[str, list[str]], | ||
| ) -> str | None: | ||
| """ | ||
| Check if all tests for a model have finished and return aggregated status. | ||
|
|
||
| Returns: | ||
| "pass" if all tests passed, "fail" if any test failed, | ||
| or None if not all tests have reported yet. | ||
| """ | ||
| expected = tests_per_model.get(model_uid) | ||
| if not expected: | ||
| return None | ||
| collected = test_results_per_model.get(model_uid, []) | ||
| if len(collected) < len(expected): | ||
| logger.debug( | ||
| "Model '%s' has %s tests, but only %s have reported results so far.", | ||
| model_uid, | ||
| len(expected), | ||
| len(collected), | ||
| ) | ||
|
michal-mrazek marked this conversation as resolved.
|
||
| return None | ||
| aggregated_test_result = ( | ||
| DbtTestStatus.PASS if all(is_dbt_node_status_success(s) for s in collected) else DbtTestStatus.FAIL | ||
| ) | ||
|
michal-mrazek marked this conversation as resolved.
|
||
| logger.debug("Model '%s' has all tests reported. Aggregated result: %s", model_uid, aggregated_test_result) | ||
| return aggregated_test_result | ||
|
|
||
|
|
||
| def push_test_result_or_aggregate( | ||
| test_unique_id: str, | ||
| status: str, | ||
| tests_per_model: dict[str, list[str]], | ||
| test_results_per_model: dict[str, list[str]], | ||
| task_instance: Any, | ||
| ) -> None: | ||
| """Accumulate a test result and, when all tests for the parent model have reported, push aggregated XCom. | ||
|
|
||
| :param test_unique_id: The unique_id of the finished test node. | ||
| :param status: The terminal status of the test (e.g. "pass", "fail"). | ||
| :param tests_per_model: Mapping of model unique_id → list of test unique_ids. | ||
| :param test_results_per_model: Mutable accumulator, mutated in place. | ||
| :param task_instance: The Airflow task instance used for XCom push. | ||
| """ | ||
| with _test_results_lock: | ||
| model_uid = accumulate_test_result(test_unique_id, status, tests_per_model, test_results_per_model) | ||
| if model_uid is not None: | ||
| aggregated = get_aggregated_test_status(model_uid, tests_per_model, test_results_per_model) | ||
| if aggregated is not None: | ||
| safe_xcom_push( | ||
| task_instance=task_instance, | ||
| key=get_tests_status_xcom_key(model_uid), | ||
| value=aggregated, | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.