Skip to content

Commit

Permalink
fix(metadata-ingestion): remove/replace filter expressions in native …
Browse files Browse the repository at this point in the history
…query

fix #9767
  • Loading branch information
pulsar256 committed Apr 30, 2024
1 parent a152f05 commit ff7d6e0
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions metadata-ingestion/src/datahub/ingestion/source/metabase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import re
from datetime import datetime, timezone
from functools import lru_cache
from typing import Dict, Iterable, List, Optional, Tuple, Union
Expand Down Expand Up @@ -576,11 +577,12 @@ def get_datasource_urn(
)
]
else:
raw_query = (
raw_query_stripped = self.strip_template_expressions(
card_details.get("dataset_query", {}).get("native", {}).get("query", "")
)

result = create_lineage_sql_parsed_result(
query=raw_query,
query=raw_query_stripped,
default_db=database_name,
default_schema=database_schema or self.config.default_schema,
platform=platform,
Expand All @@ -601,6 +603,24 @@ def get_datasource_urn(

return None

@staticmethod
def strip_template_expressions(raw_query) -> str:
"""
Workarounds for metabase raw queries containing most commonly used template expressions:
- strip conditional expressions "[[ .... ]]"
- replace all {{ filter expressions }} with "1"
reference: https://www.metabase.com/docs/latest/questions/native-editor/sql-parameters
"""

# drop [[ WHERE {{FILTER}} ]]
query_patched = re.sub(r"\[\[.+\]\]", r" ", raw_query)

# replace {{FILTER}} with 1
query_patched = re.sub(r"\{\{.+\}\}", r"1", query_patched)
return query_patched

@lru_cache(maxsize=None)
def get_source_table_from_id(
self, table_id: Union[int, str]
Expand Down

0 comments on commit ff7d6e0

Please sign in to comment.