Skip to content

Commit

Permalink
fix(ingest/powerbi): Allow old parser to parse [db].[schema].[table] …
Browse files Browse the repository at this point in the history
…table references (datahub-project#9360)
  • Loading branch information
asikowitz authored Dec 4, 2023
1 parent 6a18010 commit c0ef728
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class PowerBiDashboardSourceConfig(
description="Configure how is ownership ingested",
)
modified_since: Optional[str] = pydantic.Field(
default=None,
description="Get only recently modified workspaces based on modified_since datetime '2023-02-10T00:00:00.0000000Z', excludePersonalWorkspaces and excludeInActiveWorkspaces limit to last 30 days",
)
extract_dashboards: bool = pydantic.Field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,24 +617,32 @@ def create_urn_using_old_parser(

tables: List[str] = native_sql_parser.get_tables(query)

for table in tables:
schema_and_table: List[str] = table.split(".")
if len(schema_and_table) == 1:
# schema name is not present. set default schema
schema_and_table.insert(0, MSSqlDataPlatformTableCreator.DEFAULT_SCHEMA)

qualified_table_name = (
f"{db_name}.{schema_and_table[0]}.{schema_and_table[1]}"
)
for parsed_table in tables:
# components: List[str] = [v.strip("[]") for v in parsed_table.split(".")]
components = [v.strip("[]") for v in parsed_table.split(".")]
if len(components) == 3:
database, schema, table = components
elif len(components) == 2:
schema, table = components
database = db_name
elif len(components) == 1:
(table,) = components
database = db_name
schema = MSSqlDataPlatformTableCreator.DEFAULT_SCHEMA
else:
logger.warning(
f"Unsupported table format found {parsed_table} in query {query}"
)
continue

qualified_table_name = f"{database}.{schema}.{table}"
urn = urn_creator(
config=self.config,
platform_instance_resolver=self.platform_instance_resolver,
data_platform_pair=self.get_platform_pair(),
server=server,
qualified_table_name=qualified_table_name,
)

dataplatform_tables.append(
DataPlatformTable(
data_platform_pair=self.get_platform_pair(),
Expand Down
65 changes: 65 additions & 0 deletions metadata-ingestion/tests/unit/test_powerbi_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest

from datahub.ingestion.api.common import PipelineContext
from datahub.ingestion.source.powerbi.config import PowerBiDashboardSourceConfig
from datahub.ingestion.source.powerbi.dataplatform_instance_resolver import (
ResolvePlatformInstanceFromDatasetTypeMapping,
)
from datahub.ingestion.source.powerbi.m_query.resolver import (
MSSqlDataPlatformTableCreator,
)


@pytest.fixture
def creator():
config = PowerBiDashboardSourceConfig(
tenant_id="test-tenant-id",
client_id="test-client-id",
client_secret="test-client-secret",
)
return MSSqlDataPlatformTableCreator(
ctx=PipelineContext(run_id="test-run-id"),
config=config,
platform_instance_resolver=ResolvePlatformInstanceFromDatasetTypeMapping(
config
),
)


def test_parse_three_part_table_reference(creator):
v = creator.create_urn_using_old_parser(
"SELECT * FROM [dwhdbt].[dbo2].[my_table] where oper_day_date > getdate() - 5",
db_name="default_db",
server="server",
)
assert len(v) == 1
assert (
v[0].urn
== "urn:li:dataset:(urn:li:dataPlatform:mssql,dwhdbt.dbo2.my_table,PROD)"
)


def test_parse_two_part_table_reference(creator):
v = creator.create_urn_using_old_parser(
"SELECT * FROM my_schema.my_table",
db_name="default_db",
server="server",
)
assert len(v) == 1
assert (
v[0].urn
== "urn:li:dataset:(urn:li:dataPlatform:mssql,default_db.my_schema.my_table,PROD)"
)


def test_parse_one_part_table_reference(creator):
v = creator.create_urn_using_old_parser(
"SELECT * FROM my_table",
db_name="default_db",
server="server",
)
assert len(v) == 1
assert (
v[0].urn
== "urn:li:dataset:(urn:li:dataPlatform:mssql,default_db.dbo.my_table,PROD)"
)

0 comments on commit c0ef728

Please sign in to comment.