Area: Ingestion
Sub-Area: SQL Parsing
Applies to: OSS + Cloud
Issue
Lineage is missing or incomplete for some queries from your data warehouse. The SQL parser could not parse those queries.
You Might Be Asking
- Why isn't my SQL query producing lineage?
- Is there a recipe setting to change the SQL parser or dialect?
- How do I test a query against the parser?
- How do I add lineage for a query the parser can't handle?
Cause
DataHub parses query text with its own SQL parser to build lineage. When the parser fails at the table level, DataHub drops that query and it gives no lineage. When only column-level parsing fails, DataHub keeps the table lineage and loses the column lineage for that query.
The ingestion run report records every parse failure at any log level:
-
observed_query_parse_failureslists each failure as<error> on query: <first 100 characters of the query>. -
num_observed_queries_failedcounts table-level failures. -
num_observed_queries_column_failedcounts column-level failures. -
views_parse_failuresandnum_views_failedrecord view definitions that failed to parse.
Only the log line Error parsing query <query>: <error> needs DEBUG logging. It is written only when the logger level is DEBUG or lower.
Recipe configs reject unknown keys, so a recipe with keys such as sql_parser, sql_parser_config, parse_queries_with_ctes, sql_dialect or sql_parsing_fallback fails validation. Remove them. A few sources have their own parsing keys:
- The SQL Queries source has
override_dialect, which overrides automatic dialect detection. - BigQuery has
lineage_use_sql_parser(default true). - dbt has
prefer_sql_parser_lineage(default false). - PowerBI has
native_query_parsing(default true).
Solution
-
Remove invented keys from your recipe. Snowflake table and column lineage are on by default. Column lineage requires
include_table_lineage. -
Find the failing query in the ingestion report. Open the run report and check
observed_query_parse_failuresandviews_parse_failures. For the full error log line, set logging to DEBUG and look forError parsing query. -
Test the query with the CLI. Replace the placeholders with your values:
datahub check sql-lineage --platform snowflake --sql "<query>"Use
--sql-file <file>for a long query.--platformis required. Other options are--platform-instance,--env,--default-dband--default-schema. The command runs online by default. Online mode uses schemas from DataHub to produce column-level lineage. Use--offlineto skip DataHub. Offline results, and results for tables DataHub doesn't know, are less accurate. -
Test from Python if you prefer.
DataHubGraph.parse_sql_lineage(sql, *, platform, platform_instance=None, env=DEFAULT_ENV, default_db=None, default_schema=None, override_dialect=None)returns aSqlParsingResult. Don't importdatahub.utilities.sql_parser.SQLParser. It is not part of this workflow. See the SQL parser page.from datahub.ingestion.graph.client import DataHubGraph from datahub.ingestion.graph.config import DatahubClientConfig graph = DataHubGraph(DatahubClientConfig(server="http://localhost:8080", token="<token>")) result = graph.parse_sql_lineage("<query>", platform="snowflake") print(result) -
Add lineage by hand for queries that still don't parse. Prefer patches. They add edges to the existing lineage. Use the SDK lineage client's
add_lineage(*, upstream, downstream, column_lineage=False, transformation_text=None). It builds a patch for the downstream dataset. You can also useDatasetPatchBuilder(urn).add_upstream_lineage(upstream), andadd_fine_grained_upstream_lineage(fine_grained_lineage)for column lineage.add_upstream_lineageadds or updates one edge and leaves the others alone.set_upstream_lineagesreplaces all upstream edges, so avoid it here. To add lineage by parsing a SQL query, use the SDK lineage client'sinfer_lineage_from_sql(*, query_text, platform, ...). See the Lineage SDK how-to.A source with
incremental_lineageset to false (the default for most warehouse sources) re-states its lineage on each run. Read the lineage feature guide before you rely on manual edges.If you must write the whole
upstreamLineageaspect through the REST emitter,emit_mcptakes a single proposal object. It has noentity_urn,aspect_nameoraspectarguments.UpstreamClasstakesdatasetandtype. The type values areCOPY,TRANSFORMEDandVIEW. This writes the full aspect for the downstream dataset, unlike the patch methods above.from datahub.emitter.mce_builder import make_dataset_urn from datahub.emitter.mcp import MetadataChangeProposalWrapper from datahub.emitter.rest_emitter import DatahubRestEmitter from datahub.metadata.schema_classes import UpstreamClass, UpstreamLineageClass emitter = DatahubRestEmitter(gms_server="http://localhost:8080", token="<token>") downstream_urn = make_dataset_urn("snowflake", "db.schema.target") upstream_lineage = UpstreamLineageClass( upstreams=[ UpstreamClass(dataset=make_dataset_urn("snowflake", "db.schema.source1"), type="TRANSFORMED"), UpstreamClass(dataset=make_dataset_urn("snowflake", "db.schema.source2"), type="TRANSFORMED"), ] ) emitter.emit_mcp(MetadataChangeProposalWrapper(entityUrn=downstream_urn, aspect=upstream_lineage))See the Python emitter guide for the server URL and token.