Skip to content

Fix issue when a query uses a CTE with a table alias and DJ fails to …#1723

Merged
shangyian merged 1 commit intoDataJunction:mainfrom
shangyian:cte-type-infer
Jan 28, 2026
Merged

Fix issue when a query uses a CTE with a table alias and DJ fails to …#1723
shangyian merged 1 commit intoDataJunction:mainfrom
shangyian:cte-type-infer

Conversation

@shangyian
Copy link
Copy Markdown
Collaborator

Summary

This fixes an issue when a query uses a CTE with a table alias and DJ fails to resolve column types:

WITH monthly_totals AS (
    SELECT region, SUM(amount) AS total FROM orders GROUP BY region
)
SELECT m.region, m.total
FROM monthly_totals m  -- aliased as 'm'

results in

Error: Cannot resolve type of column m.region

This is because when inlining a CTE as a subquery, the table alias from the FROM clause is not preserved. The CTE is inlined with its original name (monthly_totals) instead of the alias (m), causing column references like m.region to fail resolution.

The fix is to modify bake_ctes() to preserve the table alias when swapping a CTE reference.

Test Plan

  • PR has an associated issue: #
  • make check passes
  • make test shows 100% unit test coverage

Deployment Plan

…resolve column types. This is because when inlining a CTE as a subquery, the table alias from the FROM clause was not preserved. The CTE was inlined with its original name instead of the alias, causing column references to fail resolution.
@netlify
Copy link
Copy Markdown

netlify bot commented Jan 28, 2026

Deploy Preview for thriving-cassata-78ae72 canceled.

Name Link
🔨 Latest commit f265a3a
🔍 Latest deploy log https://app.netlify.com/projects/thriving-cassata-78ae72/deploys/69798f3dc6ac850008a7bbea

@shangyian shangyian marked this pull request as ready for review January 28, 2026 04:51
@shangyian shangyian merged commit 2499a07 into DataJunction:main Jan 28, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant