Skip to content

Commit

Permalink
Fix compiled_code, reimplement sql as wrapper (#9503)
Browse files Browse the repository at this point in the history
* Fix compiled_code, reimplement sql as wrapper

* Add changelog entry
  • Loading branch information
jtcohen6 authored Feb 1, 2024
1 parent 2d59a51 commit 1a5d692
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240201-124701.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Update 'compiled_code' context member logic to route based on command ('clone'
or not). Reimplement 'sql' context member as wrapper of 'compiled_code'.
time: 2024-02-01T12:47:01.488085+01:00
custom:
Author: jtcohen6
Issue: "9502"
29 changes: 10 additions & 19 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,27 +1457,10 @@ def post_hooks(self) -> List[Dict[str, Any]]:
h.to_dict(omit_none=True) for h in self.model.config.post_hook # type: ignore[union-attr] # noqa
]

@contextproperty()
def sql(self) -> Optional[str]:
# only doing this in sql model for backward compatible
if self.model.language == ModelLanguage.sql: # type: ignore[union-attr]
# If the model is deferred and the adapter doesn't support zero-copy cloning, then select * from the prod
# relation
# TODO: avoid routing on args.which if possible
if getattr(self.model, "defer_relation", None) and self.config.args.which == "clone":
# TODO https://github.com/dbt-labs/dbt-core/issues/7976
return f"select * from {self.model.defer_relation.relation_name or str(self.defer_relation)}" # type: ignore[union-attr]
elif getattr(self.model, "extra_ctes_injected", None):
# TODO CT-211
return self.model.compiled_code # type: ignore[union-attr]
else:
return None
else:
return None

@contextproperty()
def compiled_code(self) -> Optional[str]:
if getattr(self.model, "defer_relation", None):
# TODO: avoid routing on args.which if possible
if getattr(self.model, "defer_relation", None) and self.config.args.which == "clone":
# TODO https://github.com/dbt-labs/dbt-core/issues/7976
return f"select * from {self.model.defer_relation.relation_name or str(self.defer_relation)}" # type: ignore[union-attr]
elif getattr(self.model, "extra_ctes_injected", None):
Expand All @@ -1486,6 +1469,14 @@ def compiled_code(self) -> Optional[str]:
else:
return None

@contextproperty()
def sql(self) -> Optional[str]:
# only set this for sql models, for backward compatibility
if self.model.language == ModelLanguage.sql: # type: ignore[union-attr]
return self.compiled_code
else:
return None

@contextproperty()
def database(self) -> str:
return getattr(self.model, "database", self.config.credentials.database)
Expand Down

0 comments on commit 1a5d692

Please sign in to comment.