fix(sql): resolve 'Invalid column' error in WINDOW JOIN with aliased columns#6701
fix(sql): resolve 'Invalid column' error in WINDOW JOIN with aliased columns#6701bluestreak01 merged 2 commits intomasterfrom
Conversation
WalkthroughChanges to SqlOptimiser modify alias resolution and aggregate/literal emission logic for window join scenarios. When a literal references a column, the code now short-circuits for certain window join cases or preserves group-by key tracking. Additionally, the translatingModel parameter for emitAggregatesAndLiterals is conditionally set based on whether a window join is present. Test cases are added for self-join window join scenarios with aggregates. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~35 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ast-grep (0.40.5)core/src/main/java/io/questdb/griffin/SqlOptimiser.javacore/src/test/java/io/questdb/test/griffin/SqlParserTest.javaThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@CodeRabbit pls review, find bugs |
|
Oops, something went wrong! Please try again later. 🐰 💔 |
|
@CodeRabbit whenever you can, review my bugs |
|
🧠 Learnings used✅ Actions performedReview triggered.
|
|
[PR Coverage check]😍 pass : 13 / 13 (100.00%) file detail
|
|
@CodeRabbit what is the story? |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain
🏁 Script executed: Length of output: 10965 🏁 Script executed: Length of output: 8502 🏁 Script executed: Length of output: 36
✅ Implementation AnalysisThe fix correctly addresses the root cause you described. The logic is sound:
|
Summary
Invalid column: <column>error at position 0t.price AS fill_price) and used in an expression with aggregates (e.g.,(t.price - sum(...)))Problem
When processing expressions containing aggregates in WINDOW JOIN context, the
SqlOptimiser.replaceIfAggregateOrLiteral()method failed to correctly resolve column references because:translatingModelto get its aliasgroupByModelusinggroupByModel.getColumnNameToAliasMap().get(translatingAlias)windowJoinModel), and the lookup failed because the map storestoken -> alias, notalias -> somethingThis caused the column to be re-added with a bare name instead of using the existing alias, leading to the "Invalid column" error at code generation time.
Fix
windowJoinModelas the translating model toemitAggregatesAndLiteralswhenisWindowJoinis truereplaceIfAggregateOrLiteralfor whentranslatingModel == groupByModel(window join case) to directly return the found aliasTest plan
SqlParserTest.testWindowJoin()- verifies correct query model is generatedWindowJoinTest.testWindowJoinSelfJoinWithAggregatesInSelectAndWhere()- verifies query executes correctlyWindowJoinTesttests pass (184 tests)Example query that was failing
🤖 Generated with Claude Code