Repository navigation
fix(engine) #5675: CREATE INDEX IF NOT EXISTS answers for the index asked for, not for any index on those properties - #5740
Conversation
…sked for, not for any index on those properties The guard matched on the auto-derived index name, which is built from the indexed property set alone and says nothing about what the index does. A NOTUNIQUE index therefore reported success to a request for a UNIQUE one, and the schema migration that asked for the constraint carried on without it. An index of a different KIND on the same properties was ignored the same way. IF NOT EXISTS is now satisfied only when the existing index provides what was asked for: the same index kind, and a uniqueness constraint at least as strong. A UNIQUE index still covers a NOTUNIQUE request; a NOTUNIQUE index does not cover a UNIQUE one, and the mismatch is reported naming both definitions (HTTP 400). Underneath, TypeIndexBuilder.create() with withIgnoreIfExists(true) used to DROP the existing index and rebuild it on a uniqueness mismatch. When the stored data cannot satisfy the new definition the rebuild fails and the type is left with no index at all; on an inherited index it took away the parent type's index (#4083). That implicit rebuild is gone. A caller that genuinely means "make this the definition" opts in with the new withReplaceIfIncompatible(), and even then the previous index is restored if the new one cannot be built. Cypher's CREATE CONSTRAINT ... IS UNIQUE / IS NODE KEY are the callers that opt in, so a Neo4j migration script that creates indexes and then constraints keeps working. Cypher's own CREATE INDEX does not: it finds a unique index sufficient rather than replacing it with a plain one. Also removes a dead null-strategy condition in the same branch (`x != null && x == null`).
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 15 |
🟢 Coverage 90.63% diff coverage · -6.90% coverage variation
Metric Results Coverage variation ✅ -6.90% coverage variation Diff coverage ✅ 90.63% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (1449cf1) 150880 115409 76.49% Head commit (e21743f) 182961 (+32081) 127330 (+11921) 69.59% (-6.90%) 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 (#5740) 64 58 90.63% 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.
Review:
|
…x name to its type Review follow-ups on #5740: - The restore after a failed replacement left the page size behind, so an index with a custom one came back at the implementation default. Captured through getPageSizeForNewFile(), the same accessor a rebuild uses, so a page size the current file carries but creation would refuse cannot turn the restore into a second failure (#5713). Mutation-checked: without it the test index returns as 262144 instead of 16384. - Index names are global, so a manual one can already name an index on ANOTHER type. The IF NOT EXISTS branch compared the properties but not the type, and would have answered "already exists" pointing at a different type's index while creating nothing. - Guard test for the indexType null-check moving ahead of the existing-index lookup (a builder with no withType and ignoreIfExists used to hand back the existing index), plus a changelog note naming the shape. - Documented why the replaced definition is templated off a bucket-level IndexMetadata: only its concrete subclass carries the index-type settings, and LocalSchema.createBucketIndex reads it as a template, so the bucket id it carries never reaches the restored index. Resetting that field instead breaks the drop path. - Reworded the null-strategy note on satisfiesRequest: it is excluded for being non-structural, and a satisfied request stays a plain no-op rather than reconciling the strategy.
|
Thanks — pushed 63f8ffd for the review points. 1. Page size not restored — fixed. Captured in 1b. Metadata templated off a live bucket object — kept, documented. I tried the suggested reset of 2. Manual name reused across types — fixed. The branch now compares 3. 4. Null-strategy wording — reworded. It is excluded for being non-structural (settable through Crash window — agreed, left as-is and now a known property rather than an oversight. Verification: 1414 engine tests in the schema + index packages and the server index/HTTP suites, all green except |
|
Review: CREATE INDEX IF NOT EXISTS conflict handling (issue 5675) Solid fix. The core insight, that a name/property match says nothing about whether the existing index actually provides what the statement asked for, is correct, and the directional A few observations, none blocking: 1. The restore loses a custom index name. 2. Public embedded-API behavior change worth a release note. 3. Durability window in drop then create then restore. The design explicitly accepts that the replacement cannot be perfectly atomic, and the exception path is handled (restore, addSuppressed, SEVERE log) with a passing test. The residual gap is a hard crash between 4. Minor test-coverage gaps.
Nits.
Logic, imports, and the branch table in Reviewed by Claude Code. |
…, and the non-atomic replacement window Round-two review follow-ups on #5740: - Test that a failed replacement restores a MANUAL index name along with the page size. The name was already restored - it travels on the captured IndexMetadata.typeIndexName, which LocalSchema.createBucketIndex copies onto each bucket index and addIndexInternal honours when minting the TypeIndex (#4139), the same route a manual name takes on the normal creation path - but nothing pinned it. Mutation-checked: dropping the metadata pass-through fails the test. - Test that withReplaceIfIncompatible does NOT reach an index the type only inherits, which was handled by the own-vs-inherited guard but untested (#4083). - The replacement is not atomic: LocalSchema.dropIndex commits its own recordFileChanges and so does the build, so a process that dies between them leaves the type without an index. Recorded at the drop site, with why the alternatives do not work: a schema transaction held across a full index build is the wrong trade, and building the new index alongside the old one is not representable while indexesByProperties keeps one index per property set. - Release note for the embedded getOrCreateTypeIndex contract: it sets withIgnoreIfExists(true), so a uniqueness mismatch now raises instead of rebuilding, with the migration path.
|
Round two addressed in 727b853. 1. Restore loses a custom index name — it doesn't, and now a test says so. The name never travelled through 2. 3. Transactionality — confirmed NOT atomic, and now documented. 4. Test gaps — both filled. Added Nits. Verification: 1305 tests across the engine |
Review:
|
…int an unguarded repeat at IF NOT EXISTS Round-three review follow-ups on #5740, both documentation-level: - satisfiesRequest compares the structural definition only, so two same-kind indexes configured differently (vector dimensions, full-text analyzers, geospatial precision) still satisfy each other. Not a regression - the old code compared isUnique alone - but it is the next question a reader asks, so the Javadoc now says it is deliberate and why: those settings live in per-index-type IndexMetadata subclasses with no common notion of "provides at least what was asked for", which needs a per-type rule rather than this comparison. - The unguarded "Found the existent index" error now ends with the guard that makes the statement idempotent when the existing index already satisfies the request, and with the drop hint when it does not. The long-standing sentence is kept verbatim as the prefix so anything matching on it still matches.
|
Round three addressed in 7cbd6ab — both documentation-level, since nothing here was blocking. 1. Sub-configuration not compared — noted in the Javadoc, as suggested. 4. Generic message in the satisfied-but-unguarded case — improved. It now ends with 2 and 3 — no change; both are already covered in the release notes (the Verification: 1356 tests across the engine |
Review:
|
It is a runtime semantic change for embedded callers using it as an ensure/upgrade helper, so it belongs under the same BREAKING heading convention the rest of the file uses rather than a plain one.
|
Thanks — LGTM noted. One last change in e21743f, documentation only. 1. 2. Non-atomic window — agreed on all counts, no change; it is documented at the drop site with the rejected alternatives, and only the explicit opt-in reaches it. Nit: the pre-existing This closes out every point raised across the four rounds. Ready for your review. |
Review:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5740 +/- ##
==========================================
- Coverage 67.67% 67.65% -0.02%
==========================================
Files 1776 1776
Lines 150880 150937 +57
Branches 32016 32027 +11
==========================================
+ Hits 102103 102114 +11
- Misses 35512 35565 +53
+ Partials 13265 13258 -7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…sked for, not for any index on those properties (#5740) * fix(engine) #5675: CREATE INDEX IF NOT EXISTS answers for the index asked for, not for any index on those properties The guard matched on the auto-derived index name, which is built from the indexed property set alone and says nothing about what the index does. A NOTUNIQUE index therefore reported success to a request for a UNIQUE one, and the schema migration that asked for the constraint carried on without it. An index of a different KIND on the same properties was ignored the same way. IF NOT EXISTS is now satisfied only when the existing index provides what was asked for: the same index kind, and a uniqueness constraint at least as strong. A UNIQUE index still covers a NOTUNIQUE request; a NOTUNIQUE index does not cover a UNIQUE one, and the mismatch is reported naming both definitions (HTTP 400). Underneath, TypeIndexBuilder.create() with withIgnoreIfExists(true) used to DROP the existing index and rebuild it on a uniqueness mismatch. When the stored data cannot satisfy the new definition the rebuild fails and the type is left with no index at all; on an inherited index it took away the parent type's index (#4083). That implicit rebuild is gone. A caller that genuinely means "make this the definition" opts in with the new withReplaceIfIncompatible(), and even then the previous index is restored if the new one cannot be built. Cypher's CREATE CONSTRAINT ... IS UNIQUE / IS NODE KEY are the callers that opt in, so a Neo4j migration script that creates indexes and then constraints keeps working. Cypher's own CREATE INDEX does not: it finds a unique index sufficient rather than replacing it with a plain one. Also removes a dead null-strategy condition in the same branch (`x != null && x == null`). * fix(engine) #5675: restore the page size too, and scope a manual index name to its type Review follow-ups on #5740: - The restore after a failed replacement left the page size behind, so an index with a custom one came back at the implementation default. Captured through getPageSizeForNewFile(), the same accessor a rebuild uses, so a page size the current file carries but creation would refuse cannot turn the restore into a second failure (#5713). Mutation-checked: without it the test index returns as 262144 instead of 16384. - Index names are global, so a manual one can already name an index on ANOTHER type. The IF NOT EXISTS branch compared the properties but not the type, and would have answered "already exists" pointing at a different type's index while creating nothing. - Guard test for the indexType null-check moving ahead of the existing-index lookup (a builder with no withType and ignoreIfExists used to hand back the existing index), plus a changelog note naming the shape. - Documented why the replaced definition is templated off a bucket-level IndexMetadata: only its concrete subclass carries the index-type settings, and LocalSchema.createBucketIndex reads it as a template, so the bucket id it carries never reaches the restored index. Resetting that field instead breaks the drop path. - Reworded the null-strategy note on satisfiesRequest: it is excluded for being non-structural, and a satisfied request stays a plain no-op rather than reconciling the strategy. * fix(engine) #5675: pin the manual-name restore, the inherited refusal, and the non-atomic replacement window Round-two review follow-ups on #5740: - Test that a failed replacement restores a MANUAL index name along with the page size. The name was already restored - it travels on the captured IndexMetadata.typeIndexName, which LocalSchema.createBucketIndex copies onto each bucket index and addIndexInternal honours when minting the TypeIndex (#4139), the same route a manual name takes on the normal creation path - but nothing pinned it. Mutation-checked: dropping the metadata pass-through fails the test. - Test that withReplaceIfIncompatible does NOT reach an index the type only inherits, which was handled by the own-vs-inherited guard but untested (#4083). - The replacement is not atomic: LocalSchema.dropIndex commits its own recordFileChanges and so does the build, so a process that dies between them leaves the type without an index. Recorded at the drop site, with why the alternatives do not work: a schema transaction held across a full index build is the wrong trade, and building the new index alongside the old one is not representable while indexesByProperties keeps one index per property set. - Release note for the embedded getOrCreateTypeIndex contract: it sets withIgnoreIfExists(true), so a uniqueness mismatch now raises instead of rebuilding, with the migration path. * fix(engine) #5675: say what satisfiesRequest does not compare, and point an unguarded repeat at IF NOT EXISTS Round-three review follow-ups on #5740, both documentation-level: - satisfiesRequest compares the structural definition only, so two same-kind indexes configured differently (vector dimensions, full-text analyzers, geospatial precision) still satisfy each other. Not a regression - the old code compared isUnique alone - but it is the next question a reader asks, so the Javadoc now says it is deliberate and why: those settings live in per-index-type IndexMetadata subclasses with no common notion of "provides at least what was asked for", which needs a per-type rule rather than this comparison. - The unguarded "Found the existent index" error now ends with the guard that makes the statement idempotent when the existing index already satisfies the request, and with the drop hint when it does not. The long-standing sentence is kept verbatim as the prefix so anything matching on it still matches. * docs(engine) #5675: mark the getOrCreateTypeIndex change BREAKING It is a runtime semantic change for embedded callers using it as an ensure/upgrade helper, so it belongs under the same BREAKING heading convention the rest of the file uses rather than a plain one. (cherry picked from commit 4966b4b)
Fixes #5675.
The bug
IF NOT EXISTSmatched a pre-existing index on the auto-derived index name. That name is built from the indexed property set alone, so it says nothing about what the index does:Migration DDL uses
IF NOT EXISTSprecisely so it can be re-applied, so tightening an index fromNOTUNIQUEtoUNIQUEdid nothing while the application carried on believing a uniqueness constraint protected its data.Two more problems found underneath
withIgnoreIfExists(true)dropped the caller's index.TypeIndexBuilder.create()reacted to a uniqueness mismatch by dropping the existing index and rebuilding it. That is not recoverable: when the stored data has duplicates the rebuild fails and the type is left with no index at all.builderIgnoreIfExistsNeverDropsAConflictingIndexin the new test reproduced exactly that before the fix. On an inherited index the same code took away the parent type's index, which is issue Multiple warning over lost indexes on massive insert with gRPC #4083.FULL_TEXTindex satisfied a request for a range index on the same properties, silently. And the null-strategy half of the condition was dead code:existingTypeIndex.getNullStrategy() != null && existingTypeIndex.getNullStrategy() == nullis never true.The rule now
IF NOT EXISTSis satisfied only when the existing index provides what was asked for:So a
UNIQUEindex still satisfies aNOTUNIQUErequest (it indexes the same keys, and is never weakened into a plain index), while aNOTUNIQUEindex does not satisfy aUNIQUErequest. Anything the existing index does not provide is reported, naming both definitions, as anIllegalArgumentException-> HTTP 400.Nothing is dropped implicitly
Refusal is the answer even where the request could technically be granted by rebuilding, because the rebuild is the unsafe part. A caller that genuinely means "make this the definition" opts in:
and even then the drop happens as late as possible (after every remaining validation) and the previous definition — kind, uniqueness, null strategy, properties, metadata — is restored if the new index cannot be built. An inherited index is never replaced.
Cypher stays Neo4j-compatible
CREATE CONSTRAINT ... IS UNIQUEand... IS NODE KEYare statements about the data rather than "create it if missing", so they are the callers that opt into the replacement. Neo4j keeps the range index and the constraint as two objects; ArcadeDB has one index per property set and a unique index covers both roles, so the upgrade is the equivalent end state and a Neo4j migration script that creates indexes and then constraints keeps working. Cypher's ownCREATE INDEXdoes not opt in — it finds a unique index sufficient and leaves it alone rather than replacing it with a plain one, which closes a silent weakening of a constraint that existed before.Alternative considered
The issue offers "create the unique index" or "fail". Doing the first implicitly is what the code already did, and it is the thing that can lose an index — so this PR does the second by default and makes the first an explicit, restorable opt-in rather than a side effect of a guard clause.
Tests
engine/.../Issue5675IndexIfNotExistsConflictTest— 11 cases: the reported scalar andBY ITEMforms, the reverse direction staying a no-op, index-kind mismatch, manual index names, the builder never dropping a conflicting index, the Cypher constraint upgrade, and a failed constraint upgrade leaving the plain index in place (mutation-checked: reverting the restore fails this test).server/.../Issue5675CreateIndexIfNotExistsHttpTest— the 400 and its detail message over HTTP, plus the idempotent 200 withcreated: falseso the refusal test cannot pass by refusing everything.Full engine suite: 1237 test classes green. (
ACIDTransactionTest#indexCreationWhileAsyncMustFailfailed once while three engine suites ran concurrently on this machine and passes in isolation — a contention flake, unrelated: it kills the database mid-build and asserts on recovery.) Server index + HTTP suites green.🤖 Generated with Claude Code
https://claude.ai/code/session_01W8C35vrQkW1y1gaEvWzRPc