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

feat(ingest): add sql parser trace mode #12210

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
20 changes: 15 additions & 5 deletions metadata-ingestion/src/datahub/sql_parsing/sqlglot_lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"SQL_LINEAGE_TIMEOUT_ENABLED", True
)
SQL_LINEAGE_TIMEOUT_SECONDS = 10
SQL_PARSER_TRACE = get_boolean_env_variable("DATAHUB_SQL_PARSER_TRACE", False)


# These rules are a subset of the rules in sqlglot.optimizer.optimizer.RULES.
Expand Down Expand Up @@ -365,10 +366,11 @@ def _sqlglot_force_column_normalizer(

return node

# logger.debug(
# "Prior to case normalization sql %s",
# statement.sql(pretty=True, dialect=dialect),
# )
if SQL_PARSER_TRACE:
logger.debug(
"Prior to case normalization sql %s",
statement.sql(pretty=True, dialect=dialect),
)
statement = statement.transform(_sqlglot_force_column_normalizer, copy=False)
# logger.debug(
# "Sql after casing normalization %s",
Expand Down Expand Up @@ -562,7 +564,7 @@ def _select_statement_cll( # noqa: C901
)
)

# TODO: Also extract referenced columns (aka auxillary / non-SELECT lineage)
# TODO: Also extract referenced columns (aka auxiliary / non-SELECT lineage)
except (sqlglot.errors.OptimizeError, ValueError, IndexError) as e:
raise SqlUnderstandingError(
f"sqlglot failed to compute some lineage: {e}"
Expand Down Expand Up @@ -1022,6 +1024,14 @@ def _sqlglot_lineage_inner(
logger.debug(
f"Resolved {total_schemas_resolved} of {total_tables_discovered} table schemas"
)
if SQL_PARSER_TRACE:
for qualified_table, schema_info in table_name_schema_mapping.items():
logger.debug(
"Table name %s resolved to %s with schema %s",
qualified_table,
table_name_urn_mapping[qualified_table],
schema_info,
)

column_lineage: Optional[List[_ColumnLineageInfo]] = None
try:
Expand Down
Loading