Repository navigation
Conversation
This was referenced Nov 26, 2026
👷 Deploy request for thriving-cassata-78ae72 pending review.Visit the deploys page to approve it
|
Miskho
marked this pull request as ready for review
November 26, 2026 09:14
Contributor
[Medium impact] The PR appears safe to merge; the previous quoted-reader issue is addressed. SummaryThe PR preserves distinct same-name CTEs when their resolved SQL differs.
Miskho documents the existing direct-AST lookback corner case with an unqualified physical table named
|
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
Two metric queries can each work on their own but fail when combined because their independently optimized CTEs have the same name and different columns. This PR preserves both variants when their resolved SQL differs.
For example, a regression fixture starts with
(day, account_id, cnt)rows(1, 1, 2),(1, 1, 3),(1, 2, 0), and(2, 1, 4). A shared transform groups by day and account, computingdownloaded_any = MAX(CASE WHEN cnt > 0 THEN 1 ELSE 0 END)anddownload_cnt = SUM(cnt):The two metric branches independently remove columns they do not need:
cteidentity_download_windowSUM(downloaded_any)cteidentity_download_windowSUM(download_cnt)The names match, but the CTE bodies no longer do. Previously, deduplicating by name could keep A's version and leave B referring to a missing
download_cntcolumn. Reversing metric order can change which column is missing. Other same-name conflicts can silently substitute a different expression.The fix shares equal resolved bodies and assigns distinct names to conflicting variants, updating their consumers. Both standalone metrics and the combined query then agree:
Share CTEs only when their rendered bodies match after resolving dependencies in their original scopes. Give conflicting variants and their readers distinct names while preserving aliases, qualified bindings, nested shadowing, and physical table references. Use the same collector for combined measures and reserve generated wrapper names while preserving window-lookback lookup.
Resolve quoted and unquoted identifiers using the target dialect's SQLGlot normalization rules, preserving the producer's quoting when rebinding readers. This prevents mixed-case CTE readers from falling through to a physical table while retaining quoted-case distinctions in dialects such as PostgreSQL.
Test Plan
Local execution used Python 3.13, DuckDB, and six native PostgreSQL controls. Spark/Trino and other dialect policies received AST/rendering checks; their native execution, other Python CI versions, and optional integration stacks were not run locally. Identifier matching uses SQLGlot's dialect defaults; engine session overrides and plugin-specific matching rules are not modeled. CTE resolution covers ordered, non-recursive CTEs; SQL equality is conservative. A direct AST input with an unqualified physical table named
base_metricsremains a lookback-densification corner case; no ordinary API reproduction was found with catalog/schema-qualified sources.Deployment Plan
No schema migration. Generated SQL may retain distinct CTE variants that were previously deduplicated by name.
Companion compiler fixes, validated together: #2754 (cross-fact aggregation) and #2755 (output-dimension filters). These PRs share compiler files; later PRs may need rebasing after the first merge.