fix(sql): allow aggregate functions inside CASE expressions#6550
fix(sql): allow aggregate functions inside CASE expressions#6550bluestreak01 merged 3 commits intomasterfrom
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe PR refactors the aggregate detection traversal logic in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks 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 |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
core/src/test/java/io/questdb/test/griffin/engine/groupby/SampleByTest.java (1)
15947-15947: Consider keeping exception output on stderr.The default
printStackTrace()writes toSystem.err, which is the conventional stream for error output. Redirecting toSystem.outmay cause exception traces to interleave with normal output in unexpected ways. If this change was intentional for specific test output capture needs, please disregard.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
core/src/main/java/io/questdb/griffin/SqlOptimiser.java(1 hunks)core/src/test/java/io/questdb/test/griffin/engine/groupby/SampleByTest.java(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (35)
- GitHub Check: New pull request (Coverage Report Coverage Report)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-other)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-pgwire)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-sub)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-root)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz2)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz1)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-sub)
- GitHub Check: New pull request (Rust Test and Lint on linux-jdk17)
- GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-root)
- GitHub Check: New pull request (Hosted Running tests on windows-other-2)
- GitHub Check: New pull request (Hosted Running tests on windows-other-1)
- GitHub Check: New pull request (Hosted Running tests on windows-pgwire)
- GitHub Check: New pull request (Hosted Running tests on windows-cairo-2)
- GitHub Check: New pull request (Hosted Running tests on windows-cairo-1)
- GitHub Check: New pull request (Hosted Running tests on windows-fuzz2)
- GitHub Check: New pull request (Hosted Running tests on windows-fuzz1)
- GitHub Check: New pull request (Hosted Running tests on windows-griffin-sub)
- GitHub Check: New pull request (Hosted Running tests on windows-griffin-base)
- GitHub Check: New pull request (Hosted Running tests on mac-other)
- GitHub Check: New pull request (Hosted Running tests on mac-pgwire)
- GitHub Check: New pull request (Hosted Running tests on mac-cairo-fuzz)
- GitHub Check: New pull request (Hosted Running tests on mac-cairo)
- GitHub Check: New pull request (Hosted Running tests on mac-griffin)
- GitHub Check: New pull request (SelfHosted Other tests on linux-arm64)
- GitHub Check: New pull request (SelfHosted Other tests on linux-x64-zfs)
- GitHub Check: New pull request (SelfHosted Other tests on linux-x86-graal)
- GitHub Check: New pull request (SelfHosted Cairo tests on linux-x64-zfs)
- GitHub Check: New pull request (SelfHosted Griffin tests on linux-x86-graal)
- GitHub Check: New pull request (Trigger Enterprise CI Trigger Enterprise Pipeline)
- GitHub Check: New pull request (SelfHosted Griffin tests on linux-x64-zfs)
- GitHub Check: New pull request (SelfHosted Griffin tests on linux-arm64)
- GitHub Check: New pull request (SelfHosted Cairo tests on linux-x86-graal)
- GitHub Check: New pull request (SelfHosted Cairo tests on linux-arm64)
- GitHub Check: New pull request (Check Changes Check changes)
🔇 Additional comments (4)
core/src/test/java/io/questdb/test/griffin/engine/groupby/SampleByTest.java (3)
160-196: LGTM! Well-structured regression test for issue #6549.The test correctly validates that aggregate functions (
first(),last()) work inside CASE expressions with SAMPLE BY. The data setup (8 records per symbol at 15-minute intervals) properly exercises hourly sampling, and the expected results correctly reflect the CASE logic:first(price)for BTC-USD andlast(price)for ETH-USD.
201-206: Formatting improvement using text blocks.
237-242: Consistent text block formatting.core/src/main/java/io/questdb/griffin/SqlOptimiser.java (1)
1217-1257: Aggregate detection now correctly traverses multi‑argument expressions (e.g., CASE) to find child group‑by functionsThe revised
checkForChildAggregatesnow:
- Walks
lhs/rhsforparamCount < 3as before, and- Iterates
node.argsforparamCount >= 3, checking each arg for group‑byFUNCTIONs and pushing non‑null args ontosqlNodeStackfor full traversal.The loop invariant (
nodeis only set tonullwhensqlNodeStackis empty) ensures all children—including aggregates nested inside CASE and other multi‑arg nodes—are visited. This aligns with howemitAggregatesAndLiteralstraverses the tree and should resolve the missing‑aggregate issue described in the PR without introducing regressions.
[PR Coverage check]😍 pass : 18 / 18 (100.00%) file detail
|
Summary
Fixes #6549
Queries with aggregate functions inside
CASEexpressions were failing with:For example, this query now works:
Root Cause
checkForChildAggregates()inSqlOptimiseronly traversedlhsandrhschildren of expression nodes. However,CASEexpressions (and other functions with 3+ arguments) store their arguments innode.args, notlhs/rhs. This caused the optimizer to miss aggregate functions nested inside these expressions.Fix
Modified
checkForChildAggregates()to also iterate throughnode.argswhennode.paramCount >= 3, following the same pattern already used inemitAggregatesAndLiterals().