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

Exposure dependency resolution by fully-qualified names #287

Closed
wants to merge 3 commits into from
Closed
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
52 changes: 46 additions & 6 deletions dbtmetabase/_exposures.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from .errors import ArgumentError
from .format import Filter, dump_yaml, safe_description, safe_name
from .manifest import Manifest
from .manifest import DEFAULT_SCHEMA, Manifest

_RESOURCE_VERSION = 2

Expand Down Expand Up @@ -79,12 +79,26 @@ def extract_exposures(
models = self.manifest.read_models()

ctx = self.__Context(
model_refs={m.alias.lower(): m.ref for m in models if m.ref},
table_names={t["id"]: t["name"] for t in self.metabase.get_tables()},
model_refs={m.alias_path.lower(): m.ref for m in models if m.ref},
database_names={
d["id"]: d["details"].get("dbname", d["name"])
for d in self.metabase.get_databases()
},
table_names={
t["id"]: ".".join(
[
t["db"]["details"].get("dbname", t["db"]["name"]),
t["schema"] or DEFAULT_SCHEMA,
t["name"],
]
).lower()
for t in self.metabase.get_tables()
},
)

exposures = []
counts: MutableMapping[str, int] = {}
all_depends = set()

for collection in self.metabase.get_collections(
exclude_personal=not allow_personal_collections
Expand Down Expand Up @@ -184,6 +198,9 @@ def extract_exposures(
count = counts.get(name, 0)
counts[name] = count + 1

for depend in depends:
all_depends.add(depend)

exposures.append(
{
"id": item["id"],
Expand Down Expand Up @@ -216,6 +233,15 @@ def extract_exposures(

self.__write_exposures(exposures, output_path, output_grouping)

with open(Path(output_path) / "depends.yaml", "w", encoding="utf-8") as f:
dump_yaml(
data={
"models": ctx.model_refs,
"depends": list(all_depends),
},
stream=f,
)

return exposures

def __extract_card_exposures(
Expand Down Expand Up @@ -288,13 +314,26 @@ def __extract_card_exposures(

# Parse SQL for exposures through FROM or JOIN clauses
for sql_ref in re.findall(_EXPOSURE_PARSER, native_query):
# Grab just the table / model name
parsed_model = sql_ref.split(".")[-1].strip('"').lower()
# DATABASE.schema.table -> [database, schema, table]
parsed_model_path = [
s.strip('"').lower() for s in sql_ref.split(".")
]

# Scrub CTEs (qualified sql_refs can not reference CTEs)
if parsed_model in ctes and "." not in sql_ref:
if parsed_model_path[-1] in ctes and "." not in sql_ref:
continue

# Missing schema -> use default schema
if len(parsed_model_path) < 2:
parsed_model_path.insert(0, DEFAULT_SCHEMA.lower())
# Missing database -> use query's database
if len(parsed_model_path) < 3:
database_name = ctx.database_names.get(query["database"], "")
parsed_model_path.insert(0, database_name.lower())

# Fully-qualified database.schema.table
parsed_model = ".".join(parsed_model_path)

# Verify this is one of our parsed refable models so exposures dont break the DAG
if not ctx.model_refs.get(parsed_model):
continue
Expand Down Expand Up @@ -429,4 +468,5 @@ def __write_exposures(
@dc.dataclass
class __Context:
model_refs: Mapping[str, str]
database_names: Mapping[str, str]
table_names: Mapping[str, str]
4 changes: 4 additions & 0 deletions dbtmetabase/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ def ref(self) -> Optional[str]:
return f"source('{self.source}', '{self.name}')"
return None

@property
def alias_path(self) -> str:
return ".".join([self.database, self.schema or DEFAULT_SCHEMA, self.alias])

def format_description(
self,
append_tags: bool = False,
Expand Down
6 changes: 5 additions & 1 deletion dbtmetabase/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ def _api(

return response_json

def get_databases(self) -> Sequence[Mapping]:
"""Retrieves all databases."""
return list(self._api("get", "/api/database"))

def find_database(self, name: str) -> Optional[Mapping]:
"""Finds database by name attribute or returns none."""
for api_database in list(self._api("get", "/api/database")):
for api_database in self.get_databases():
if api_database["name"].upper() == name.upper():
return api_database
return None
Expand Down