Skip to content

Commit 8e1fc0b

Browse files
fix: handle dbt deferral in get_elementary_relation (#1006)
* fix: handle dbt deferral in get_elementary_relation When --favor-state or --defer is active, Elementary models may not be built in the CI target schema. adapter.get_relation() returns None in this case, causing anomaly tests to fail with 'relation "none" does not exist'. Fall back to constructing a Relation from graph node coordinates when the physical catalog lookup returns None but the node exists in the dbt graph. This allows the generated SQL to reference the correct (deferred) schema. Co-Authored-By: Noy Arie <noyarie1992@gmail.com> * fix: gate deferral fallback on --defer/--favor-state flags Only construct a synthetic Relation when dbt deferral is actually active (invocation_args_dict.defer or favor_state). Without this guard, the fallback would fire for any installed Elementary model whose table doesn't exist yet (e.g. partial dbt run), breaking callers that rely on a None return to detect missing tables. Co-Authored-By: Noy Arie <noyarie1992@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 91d04fd commit 8e1fc0b

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

macros/utils/graph/get_elementary_relation.sql

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,32 @@
2020
{% if this and this.database == elementary_database and this.schema == elementary_schema and this.identifier == identifier_alias %}
2121
{% do return(this) %}
2222
{% endif %}
23-
{% do return(
24-
adapter.get_relation(
25-
elementary_database, elementary_schema, identifier_alias
23+
{% set relation = adapter.get_relation(
24+
elementary_database, elementary_schema, identifier_alias
25+
) %}
26+
{% if relation is not none %} {% do return(relation) %} {% endif %}
27+
{# Relation not found in the target schema. Under dbt deferral
28+
(--favor-state / --defer) the Elementary models may exist only
29+
in the deferred (e.g. prod) schema and not in the current
30+
target. Construct a relation from the graph node coordinates
31+
so the generated SQL references the correct schema instead of
32+
rendering "from None". #}
33+
{% set is_defer = (
34+
(
35+
invocation_args_dict.get("defer", false)
36+
or invocation_args_dict.get("favor_state", false)
2637
)
38+
if invocation_args_dict
39+
else false
2740
) %}
41+
{% if identifier_node and is_defer %}
42+
{% do return(
43+
api.Relation.create(
44+
database=elementary_database,
45+
schema=elementary_schema,
46+
identifier=identifier_alias,
47+
)
48+
) %}
49+
{% endif %}
2850
{%- endif %}
2951
{% endmacro %}

0 commit comments

Comments
 (0)