Skip to content
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

Revert "Fix: redshift leverages postgres adapter dispatch implementations for common functionality" #2836

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions sqlmesh/dbt/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,15 @@ def quote(self, identifier: str) -> str:

def dispatch(self, name: str, package: t.Optional[str] = None) -> t.Callable:
"""Returns a dialect-specific version of a macro with the given name."""
package = package or "dbt"
target_type = self.jinja_globals["target"]["type"]
macro_suffix = f"__{name}"

def _relevance(macro: MacroReference) -> int:
"""Lower scores more relevant."""
if macro.name.startswith("default"):
return 2
elif macro.name.startswith(target_type):
return 0
return 1

macros = [
MacroReference(package=macro_package, name=macro_name)
for macro_package, macros in self.jinja_macros.packages.items()
if (macro_package == package) or (package == "dbt" and macro_package.startswith("dbt_"))
for macro_name in macros.keys()
if macro_name.endswith(macro_suffix)
references_to_try = [
MacroReference(package=f"{package}_{target_type}", name=f"{target_type}__{name}"),
MacroReference(package=package, name=f"{target_type}__{name}"),
MacroReference(package=package, name=f"default__{name}"),
]
for macro in sorted(macros, key=_relevance):
macro_callable = self.jinja_macros.build_macro(macro, **self.jinja_globals)

for reference in references_to_try:
macro_callable = self.jinja_macros.build_macro(reference, **self.jinja_globals)
if macro_callable is not None:
return macro_callable

Expand Down
4 changes: 0 additions & 4 deletions tests/dbt/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,9 @@ def test_adapter_dispatch(sushi_test_project: Project, runtime_renderer: t.Calla
context = sushi_test_project.context
renderer = runtime_renderer(context)
assert renderer("{{ adapter.dispatch('current_engine', 'customers')() }}") == "duckdb"
assert renderer("{{ adapter.dispatch('current_timestamp')() }}") == "now()"
assert renderer("{{ adapter.dispatch('current_timestamp', 'dbt')() }}") == "now()"

with pytest.raises(ConfigError, match=r"Macro 'current_engine'.*was not found."):
renderer("{{ adapter.dispatch('current_engine')() }}")
with pytest.raises(ConfigError, match=r"Macro 'current_timestamp'.*was not found."):
assert renderer("{{ adapter.dispatch('current_timestamp', 'customers')() }}")


def test_adapter_map_snapshot_tables(
Expand Down