Repository navigation
fix(#6179): an expression whose modifier or CASE branch reads the record is no longer "early calculated" - #6182
Conversation
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 40 |
🟢 Coverage 100.00% diff coverage
Metric Results Coverage variation Report missing for 2abaed91 Diff coverage ✅ 100.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (2abaed9) Report Missing Report Missing Report Missing Head commit (6786e6e) 190257 137404 72.22% Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#6182) 1 1 100.00% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%1 Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
ReviewI read through the full diff plus the surrounding context (the current checkout already has this PR merged in, so I could cross-reference every override against its base class in place). SummaryThis is a solid, well-scoped fix. The root cause analysis is correct and the two bugs described really do exist in the pre-PR code:
I traced the new The new Test coverage is strong: the indexed-vs-scan differential test is exactly the right shape to catch this class of bug, the literal/early-calculated matrix test pins the two predicates against each other at the cases where they actually diverge (null, functions-over-literals, modifiers-over-literals, parameters), and the partition-pruning regression test checks the plan shape (bucket ids), not just the returned rows. Things I checked and found to be non-issues
Style / CLAUDE.md conformanceNo new dependencies, no Nothing here blocks the PR from my read. I wasn't able to execute |
ReviewI went through the diff carefully, tracing the Correctness
Test coverageGood split between a parser-level unit test ( Minor observations
No security or performance concerns — all the changes are on the planning path, not the per-row hot path. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6182 +/- ##
==========================================
+ Coverage 69.96% 70.02% +0.05%
==========================================
Files 1832 1833 +1
Lines 157831 157914 +83
Branches 33301 33312 +11
==========================================
+ Hits 110430 110578 +148
+ Misses 33588 33516 -72
- Partials 13813 13820 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@claude please resolve conflicts with main |
|
Claude finished @lvca's task in 7m 43s —— View job Resolving conflicts with main
I looked into this and want to flag a limitation before going further, plus what I found. What's actually happening: this isn't a simple rebase. The limitation: I can create and push commits on this branch, but I don't have the ability to run Recommended resolution (for whoever runs the actual merge locally):
I'm not able to push a resolved branch myself here. Let me know if you'd like me to instead prepare the fully-merged file contents as a patch/diff you can apply locally, or take any other action within what I can actually do (edit + commit + push without history rewriting). |
…ord is no longer "early calculated" isEarlyCalculated() answers "can this be computed without a record", and two nodes answered it without looking at the part that reads one: - BaseExpression judged the expression on its leftmost term alone and ignored the modifier chain, so 'Mr.'.append(surname) and ['a','b'][idx] were reported as record-free; - CaseExpression keeps its branches outside childExpressions, so the inherited walker found an empty list and said yes to a CASE that reads the record on every branch. The planner takes that answer as licence to use an index and to evaluate the expression against a null record, so the record-dependent part resolved to null and the query answered differently depending on whether an index existed: on the fixture in the new test the indexed type returned no rows where the plain scan returned two. The same classification promotes a per-record LET to a global one, with the same effect. The classification now walks the modifier chain (method call arguments, array range and index selectors; a suffix reads a property of the value it is applied to and still needs no record; a filter or nested projection is never assumed record-free) and every CASE branch. The contract is also the trap the issue is named for, so it is now written down where it is read: isEarlyCalculated says nothing about purity, determinism or constant-ness - any function over literals qualifies, uuid() included - and a caller that intends to *evaluate* an expression while planning wants a constant instead. Expression.isLiteral() is that predicate, raised to the parser from the private walk PostgresNetworkExecutor grew in #6172, which now calls it. Its optional allowInputParameters form admits a bound parameter for callers whose result does not outlive the execution. Two plan-time evaluation sites move onto it: - partitioned-bucket pruning binds a coordinate only from a literal now. The bucket it derives is baked into the plan, so the value must be fixed for the plan's life and free to compute; a function was neither, and the path was safe only because FunctionCall.isCacheable() is true for traversals alone, an accidental coupling in an unrelated file that pruning no longer leans on; - the MATCH index-cost estimate evaluates literals and bound parameters only, instead of invoking a user function once per planning pass just to sharpen an estimate.
…here the value field went Two points from the review: - CaseExpression kept the MathExpression default for isLiteral() too, so a CASE over literals alone reported false. Conservative, but inconsistent with the predicate's own definition: the extended form now qualifies when its case, when, then and else expressions all do. The simple form's WHEN is a WhereClause and carries no constant-ness, so it still cannot. - The comment on the isLiteral() fall-through now names what isEarlyCalculated() reads there and this does not: the inherited value field, where the pre-ANTLR executor substituted a parameter by hand and which nothing assigns any more. The asymmetry is unreachable rather than a gap, and a reader should not have to prove that again. The unguarded execute() the review also flagged in WhereClause.getEqualityOperations stays as it is: it now only ever runs on a literal or a bound parameter, and an expression that throws there (1/0) would throw at execution too, so failing at planning is the same outcome sooner.
b5feb2d to
6786e6e
Compare
|
Rebased onto main after #6181 (the #6174 constant-false fold) landed. That PR introduced its own
Because the no-arg form now routes through the same walker, #6181's fold also picks up the shapes this PR taught the predicate to recognise: a RID literal, a collection of literals, a parenthesised literal, an array literal, and a CASE whose branches are all literals. Each is written in the statement and cannot change between executions, which is #6181's own stated definition - its exclusion of RIDs was conservative rather than principled, and folding The postgresw change from the first push is gone: #6181 moved that walk onto the AST ( Re-verified after the rebase: engine SQL suite 2546 green (including |
ReviewThis is a careful, well-scoped fix. I read through the full diff (all 21 files) and traced the Correctness
Test coverageStrong — the two new test classes ( Minor/non-blocking observations
PerformanceNo concerns — the added walks are all bounded by the expression tree size already being walked for other purposes ( Nice fix, and the regression tests are exactly the kind that would catch this class of bug again if it recurs elsewhere in the modifier/CASE handling. |
Closes #6179.
The issue was filed as a contract/naming trap found while implementing #6172, with the note "not a live bug as far as I can tell". It is one: two nodes answer
isEarlyCalculated()- "can this be computed without a record" - without looking at the part of themselves that reads one.The bug
BaseExpressionjudged the expression on its leftmost term alone and never consulted the modifier chain, so'Mr.'.append(surname)and['a','b'][idx]were reported as record-free. (isBaseIdentifier(), twelve lines above, already checksmodifier == null.)CaseExpressionkeeps its branches outsidechildExpressions, soMathExpression's inherited walker found an empty list and returned true for a CASE that reads the record on every branch.The planner takes that answer as licence to resolve the equality through an index and to evaluate the expression against a
nullrecord, so the record-dependent part silently resolves to null. The query then answers differently depending on whether an index exists. On the fixture in the new test the indexed type returns no rows where the identical unindexed type returns two:The same classification also promotes a per-record
LETto a global one (SelectExecutionPlanner.splitLet), with the same effect.The fix walks what was skipped: the modifier chain (method-call arguments, array range and index selectors; a suffix reads a property of the value it is applied to, so it still needs no record; a filter or nested projection is never assumed record-free) and every CASE branch. A traversal method (
.out(),.in(), ...) is excluded likeFunctionCallalready excludes traversal functions.The contract, written down where it is read
isEarlyCalculatedsays nothing about purity, determinism or constant-ness - any function over literals qualifies,uuid()included - and there is no marker anywhere onSQLFunctionthat would say otherwise. That is now in its Javadoc, inFunctionCall's, and inMethodCall's.A caller that intends to evaluate an expression while planning wants a constant instead.
Expression.isLiteral()is that predicate, raised to the parser from the private walkPostgresNetworkExecutorgrew in #6172 - which now calls it and drops its copy. The optionalisLiteral(true)form admits a bound input parameter, for callers whose result does not outlive the execution.Two plan-time evaluation sites move onto it:
FunctionCall.isCacheable()is true for traversals alone - two unrelated predicates in different files happening to line up, exactly the accident the issue flagged. Pruning no longer leans on it, so wideningisCacheable()later cannot silently break it. The explicitcontainsInputParameter()guard goes with it:isLiteral()already excludes parameters.WhereClause.getEqualityOperations) evaluates literals and bound parameters only, instead of invoking a user function once per planning pass just to sharpen an estimate.Tests
Issue6179EarlyCalculatedModifierTest- indexed and unindexed types with identical rows must answer identically for a method call, a collection selector and a CASE over a record field; a modifier over a literal and a function over literals must still be resolved through the index (the contract's other half).Issue6179LiteralExpressionTest- the two predicates side by side, above all where they disagree: functions and modifiers over literals are early calculated but not literal; a parameter is a literal only for the caller that says so;nullis the one shape that is a literal but has never been early calculated (an index lookup on a null key is not the filter's question, and must not start to be).PartitionPruningPlannerTest.computedPredicateDoesNotNarrowIndexFilterBuckets- a computed value no longer prunes, and the rows still come back.Verified red before / green after for both the modifier and the CASE case. Full engine suite green (11931 tests);
PostgresWJdbcITgreen (44), including the #6172 probe tests that assertuuid() = 'nomatch'is not folded.Spotted while testing, not touched here
SELECT FROM Doc WHERE @rid > :preturns zero rows for a parameter bound either to a RID or to its string form, while the same query with a RID literal answers correctly. Pre-existing and unrelated to this change (SelectExecutionPlanner.isRidRangeevaluates the right side unguarded); will file separately.