fix: Resolve unqualified columns in JOIN queries using schema info#50
Merged
fix: Resolve unqualified columns in JOIN queries using schema info#50
Conversation
When a SQL query joins multiple tables and columns are unqualified (no table prefix), the lineage builder previously defaulted to the first table, which was often incorrect. For example, in: SELECT DATE_TRUNC(order_date, MONTH) as month FROM analytics.user_metrics JOIN staging.user_orders USING (user_id) The `order_date` column would be incorrectly attributed to `user_metrics` instead of `user_orders`, causing lineage edges to be dropped. This fix: 1. Uses sqlglot's qualify_columns optimizer with upstream table schemas to add correct table prefixes before building lineage 2. Fixes _extract_select_from_query to use dialect when serializing SQL, which was causing DATE_TRUNC arguments to be reordered incorrectly The 3-layer pipeline example now correctly shows: - staging.user_orders.order_date -> reports.monthly_revenue.month - trace_column_backward returns raw.orders.order_date Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
qualify_columnsoptimizer with upstream table schemas to correctly resolve which table each column belongs to_extract_select_from_queryto use dialect when serializing SQL, preventing function argument reordering (e.g., DATE_TRUNC)Problem
When a SQL query joins multiple tables and columns are unqualified (no table prefix), the lineage builder would default to the first table, which was often incorrect. For example:
The
order_datecolumn would be incorrectly attributed touser_metricsinstead ofuser_orders, causing the lineage edgestaging.user_orders.order_date -> reports.monthly_revenue.monthto be dropped.Solution
_convert_to_nested_schema()helper to convert flat schema format to sqlglot's nested format_qualify_sql_with_schema()helper to qualify unqualified columns using schema info before parsingRecursiveLineageBuilder.__init__to qualify SQL before building lineage_extract_select_from_query()to use dialect parameter when serializing SQLTest plan
test_unqualified_column_resolution.pycovering:🤖 Generated with Claude Code