Repository navigation
fix(#6401): a hash code is a property of the value, and an expression's value is every field that carries one - #6407
Conversation
…'s value is every field that carries one Four follow-ups from #6359, and two of them turned out to be each other's cover. `SimpleNode.hashCode()` hashed the ARRAY that `getIdentityElements()` returns instead of its contents: there is no `Objects.hashCode(Object...)` overload, so an `Object[]` binds to `Objects.hashCode(Object)` and the answer was the array's identity hash - of an array allocated fresh on every call. So a node did not agree with itself twice in a row, and two nodes `equals()` to each other hashed differently, which is the `Object.hashCode` contract broken outright. `Arrays.hashCode` is what the code reads as if it says. The other half is that identity left out fields that carry a value. `BaseExpression` omitted `number` and `expression`, so `SELECT 1` and `SELECT 2` parsed to nodes that were `equals()`; `Expression` omitted `whereCondition` and the inherited `value` slot, and `value` is where `IMPORT`, `EXPORT` and `BACKUP DATABASE` park a setting NAME - so every key of a `WITH` clause was equal to every other key. `PNumber` gained an identity of its own for the same reason `PInteger` has always had one: without it, listing `number` would compare two literals by object identity and say "not equal" for two parses of the same statement. Neither fix is safe alone. `equals()` said yes where it should have said no, the hash codes said no to everything, and a `HashMap` keyed on a parsed node came out almost right by accident: fixing the hash first collapses the five `Map<Expression, Expression> settings` maps to one entry, which is exactly what `ImportDatabaseStatementTestParserTest` caught. Also here, from the same batch: - the external-bucket arm of `addSuperType`'s rollback gets its regression test. It needs no fault injection: `ensureExternalBucketFor` refuses to adopt a bucket that is already some user type's primary bucket, so naming a primary bucket after the one the subtype's pairing would want reaches the refusal from step one, before an index is propagated or a strategy inherited. The link has to go back on both sides, and nothing may be half-adopted; - `addSuperTypeInternal` is split along the two seams four review rounds on #6364 asked for once the logic stopped moving: `linkSuperType` (symmetric with `unlinkSuperType`), `applyPostLinkageSteps`, `propagateSuperTypeIndexes` and `createSubIndexesFor`. No behaviour change; the ordering constraints that were only in comments are now the method's javadoc checklist.
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 13 |
🟢 Coverage 96.55% diff coverage · -6.58% coverage variation
Metric Results Coverage variation ✅ -6.58% coverage variation Diff coverage ✅ 96.55% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (90b0959) 160706 127762 79.50% Head commit (f62d2f5) 193301 (+32595) 140962 (+13200) 72.92% (-6.58%) 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 (#6407) 58 56 96.55% 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%
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.
ReviewOverall this is a well-scoped, well-tested fix for a genuinely nasty pair of bugs (broken Note on verification: I wasn't able to run Correctness
Traced the extracted methods ( New tests
Minor/style
Nothing here blocks merging; the identified item is an observation for future awareness, not a defect in this PR. |
…o it inherits the identity that now reads it Answers the one substantive point of the review on #6407: ValueExpression extends Expression without an identity of its own, so adding 'value' to Expression's identity changes how it compares. That is the right answer for it - it stores a pre-calculated value in exactly that slot, and before the change every ValueExpression was equals() to every other one - but the class said nothing about it. The caveat worth remembering is now written down: the identity is only as well-behaved as the wrapped object's own equals/hashCode.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6407 +/- ##
==========================================
+ Coverage 70.81% 70.83% +0.01%
==========================================
Files 1852 1853 +1
Lines 160706 160914 +208
Branches 33763 33820 +57
==========================================
+ Hits 113806 113980 +174
- Misses 32983 32998 +15
- Partials 13917 13936 +19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…, fix node identity for CASE/ORDER BY/INSERT SET (#6413) * fix(#6409): a WITH setting key means one shape everywhere, a duplicate always refuses, and identity holds for CASE, ORDER BY and INSERT SET Follow-ups from #6401/#6407 collected while asking, for each parser node, "does identity actually work here now": - Item 1: IMPORT, EXPORT and BACKUP DATABASE built their WITH setting keys as a raw value parked in the node's inherited value slot, while REBUILD INDEX and REBUILD TYPE already built a proper identifier expression. All five now build the key the same way (new Expression(Identifier)), and every reader recovers it the same way (toString()), removing the defensive raw-value read in BackupDatabaseStatement that existed only because the old shape was fragile. - Item 2: only REBUILD TYPE refused a duplicate WITH setting; the other four silently accepted one, last one wins. A shared SQLASTBuilder#putSetting helper now refuses a repeat - keyed on the lowercased name, matching the equalsIgnoreCase every reader already uses - in all five statements. - Item 3: a sweep for parser nodes whose identity does not work, widening the corpus test #6401 left as a starting point. Confirmed and fixed: ArrayRangeSelector (a[1..3] under-matched, comparing unequal to itself), OrderByItem and InsertSetExpression (same under-match, breaking identity for any statement carrying an ORDER BY or an INSERT ... SET), and CaseExpression (the opposite failure: no identity override of its own meant it inherited MathExpression's, which reads fields CaseExpression never populates, so two structurally different CASE WHEN expressions over-matched as equal). - Item 4: SimpleNode.equals() indexed the other node's identity array by this node's length with no guard; harmless today because every override returns a fixed-length array, but one built conditionally would throw ArrayIndexOutOfBoundsException out of equals(). Added the length guard. - Item 5 (hashCode is now O(subtree)) is a documented cost, not a defect - no code change. * fix(#6409): correct the putSetting javadoc's claim about case-insensitive readers Review on #6413 caught it: the comment said every reader recognises a setting with equalsIgnoreCase, which is only true for REBUILD INDEX and REBUILD TYPE. BackupDatabaseStatement's setting switch is exact-case, and Import/ExportDatabaseStatement hand the key through as-is to the integration-module setter, so for those three the lowercased dedup is a defensive choice rather than a fix for an existing case-insensitive read. No behavior change. * fix(#6409): IMPORT/EXPORT DATABASE identity ignored the WITH settings Code review on this PR caught it: ImportDatabaseStatement#equals() only compared url, and ExportDatabaseStatement#getIdentityElements() only listed url, so two statements with the same URL but different WITH settings compared equal. For IMPORT DATABASE this was the sharper case: a CSV import has no URL at all, so the settings are the statement. BackupDatabaseStatement already included settings in its identity; the other two now match it. Same over-match direction as the CaseExpression fix in this PR, on a statement node instead of an expression node.
Closes #6401.
Four follow-ups collected while fixing #6359. Items 3 and 4 turned out to be each other's cover, so they are fixed together.
Item 4 -
SimpleNode.hashCode()hashed the ARRAYThere is no
Objects.hashCode(Object...)overload, so theObject[]returned bygetIdentityElements()bound toObjects.hashCode(Object)and the answer was the array's IDENTITY hash - of an array allocated fresh on every call. So:node.hashCode() != node.hashCode()across two calls on the same object;a.equals(b)true whilea.hashCode() != b.hashCode(), which is theObject.hashCodecontract broken outright.Arrays.hashCode(elements)is what the code reads as if it says, and it walks the elements exactly asequals()a fewlines above does.
Item 3 - the identity of an expression left out fields that carry its value
BaseExpression:numberandexpressionwere missing, so for a bare numeric literal every listed field isnull/falseand the parsed forms ofSELECT 1andSELECT 2compared EQUAL. Same for two different wrappedsub-expressions.
Expression:whereConditionand the inheritedvalueslot were missing, and both are read byexecute().valueis where the threeWITH ...settings builders (IMPORT,EXPORT,BACKUP DATABASE) park a setting NAME,so every key of a
WITHclause was a node whose every other field is null - equal to every other key.PNumbergained an identity of its own, for the same reasonPIntegerhas always had one: without it, listingnumberonBaseExpressionwould compare two literals by object identity and answer "not equal" for two parses ofthe very same statement.
parenthesizedis deliberately left out ofBaseExpression's identity: it is rendering-only, the parentheses arealready in the tree SHAPE, so two nodes differing in it alone mean the same thing.
Neither fix is safe alone.
equals()said yes where it should have said no, the hash codes said no to everything,and a
HashMapkeyed on a parsed node came out almost right by accident. Fixing the hash first collapses the fiveMap<Expression, Expression> settingsmaps to a single entry - which is precisely whatImportDatabaseStatementTestParserTest.copyKeepsTheWithSettingscaught on the first full engine run.Item 1 - the external-bucket arm of
addSuperType's rollback now has a regression testIt needs no fault injection, contrary to what PR #6364 assumed:
ensureExternalBucketForalready refuses to adopt abucket that is a primary bucket of another user type. Naming a primary bucket after the one the subtype's pairing would
want (
<subtypePrimaryBucket>_ext) reaches the refusal from step ONE, before an index is propagated or a strategyinherited. The test pins that the link goes back on both sides, that nothing is half-adopted, and that with the
collision gone the same link is made and the pairing creates the bucket it could not adopt.
Verified non-vacuous: removing the
unlinkSuperTypecall from the guard makes it red.Item 2 -
addSuperTypeInternalextracted along its seams~170 lines with three nesting levels of try/catch, split into
linkSuperType(symmetric withunlinkSuperType),applyPostLinkageSteps(the three steps, numbered),propagateSuperTypeIndexesandcreateSubIndexesFor. No behaviourchange. The ordering constraints that were only in comments are now the method's javadoc checklist: the quiescence
outside
recordFileChanges, the share predicate asked before any transaction opens,createddeclared outside thepropagation, both lists cleared on entry to the retrying lambda.
Verification
Issue6401NodeIdentityTest(10 cases): hash stability across calls, equal nodes hash equally, different literals/ wrapped sub-expressions / collection literals / inline conditions are different nodes, an
Expressionused as ahash key behaves like a key, every setting key of a
WITHclause is its own key,PNumbercompares by value, and anode with no identity elements still honours the contract. Mutation-checked: reverting either
Expressionfieldturns two of them red.
Issue6359SuperTypeIndexPropagationTest#aRefusalFromTheExternalBucketPairingHandsTheLinkBackToo.engine: 12764 tests, 0 failures.server: 794, 0 failures.gremlin/graphql/postgresw/mongodbw/integration/console: green.