Skip to content

fix: align Document values() with keys() — use toMap(false) to exclude metadata fields - #6500

Merged
lvca merged 3 commits into
ArcadeData:mainfrom
waterWang:fix/6472-keys-values-asymmetry
Aug 21, 2026
Merged

lvca merged 3 commits into
ArcadeData:mainfrom
waterWang:fix/6472-keys-values-asymmetry

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Problem

SQLMethodValues handles the Document branch asymmetrically compared to SQLMethodKeys:

  • keys() → document.getPropertyNames() — declared properties only
  • values() → document.toMap().values() — declared properties plus internal metadata @rid, @type, @cat (because toMap() defaults to toMap(true))

For the same document, doc.keys().size() != doc.values().size() and the two lists are not index-aligned.

Fix

values() now uses document.toMap(false) which excludes the metadata fields, matching the property set returned by getPropertyNames(). This is the direction suggested in the issue as "more consistent with user expectations of keys()/values() as a matched pair".

// Before: metadata fields included
return new ArrayList<>(document.toMap().values());

// After: declared properties only
return new ArrayList<>(document.toMap(false).values());

The Result branch already operates on a map without metadata fields, so it was left unchanged.

Tests

Updated SQLMethodValuesTest:

  • withDocument(): now asserts only the 2 declared property values (Alice, 30) instead of 5 (declared + metadata)
  • withCollectionOfDocuments(): now asserts 4 flat values (2 per document) instead of 10

Verification

After the fix, for any Document:

  • keys().size() == values().size()
  • Both operate on the same property set

Fixes #6472

…e metadata fields

SQLMethodValues used document.toMap().values() which returns @Rid, @type, and @cat
metadata fields alongside declared properties, while keys() only returns declared
property names (getPropertyNames()). This caused keys()/values() to produce
different-sized lists that were not index-aligned.

Fixed by using document.toMap(false) which excludes metadata fields, matching
the property set returned by getPropertyNames().

Fixes ArcadeData#6472
…fields

Update withDocument() and withCollectionOfDocuments() tests to assert only
declared property values (no @rid/@type/@cat), aligning with the new
values() behavior that uses toMap(false).
@waterWang waterWang changed the title fix: align Document values() with keys() — use toMap(false) to exclude metadata fields [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] fix: align Document values() with keys() — use toMap(false) to exclude metadata fields Aug 20, 2026
@mergify

mergify Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

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.

@lvca
lvca self-requested a review August 21, 2026 01:09
…mes()

PR ArcadeData#6500's own fix (toMap(false) instead of toMap()) only equalizes the
SIZE of keys()/values() for a Document, not their ordering: toMap(false)
copied the properties into a plain HashMap, whose iteration order (hash
bucket order) can differ from getPropertyNames()'s insertion order (the
properties LinkedHashMap's keySet()). For a document with 3+ properties
this leaves doc.keys()[i] and doc.values()[i] still misaligned - the
exact defect issue ArcadeData#6472 set out to fix - just harder to notice because
it depends on the specific property names/hash codes involved.

Switch MutableDocument.toMap(boolean) to build a LinkedHashMap instead,
so its iteration order always matches the properties map it copies from
(and getPropertyNames(), which is that same map's keySet()). This also
brings MutableDocument in line with ImmutableDocument.toMap(), which
already returns a LinkedHashMap sourced from the same ordered binary
property scan as its own getPropertyNames(). MutableVertex/MutableEdge
inherit the fix since both extend MutableDocument.

Adds regression coverage: a direct toMap()/getPropertyNames() ordering
check in DocumentTest, and index-alignment checks for keys()/values() in
SQLMethodValuesTest covering both an in-memory document and one reloaded
from storage via SQL (the exact scenario from issue ArcadeData#6472).
@lvca lvca added this to the 26.9.1 milestone Aug 21, 2026
@lvca

lvca commented Aug 21, 2026

Copy link
Copy Markdown
Member

@claude review this

@claude

claude Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Claude finished @lvca's task in 2m 39s —— View job


Review

  • Gather context (diff, changed files)
  • Understand the request
  • Review the code changes
  • Post review feedback

The core fix is sound: SQLMethodValues now calls document.toMap(false) to match SQLMethodKeys's getPropertyNames(), and MutableDocument.toMap() was correctly switched from HashMap to LinkedHashMap so its iteration order matches map.keySet() (which is what getPropertyNames() returns). Without that second commit, keys()/values() would have the same size but could still be misaligned by index since a plain HashMap copy reorders by hash bucket. Good catch bundling that in. Tests are well targeted, including the SQL-level regression test that goes through a reload (ImmutableDocument), not just the in-memory MutableDocument path.

One real gap: DetachedDocument still has the pre-fix bug.

engine/src/main/java/com/arcadedb/database/DetachedDocument.java:92-101 still does:

public synchronized Map<String, Object> toMap(final boolean includeMetadata) {
  final Map<String, Object> result = new HashMap<>(map);
  ...

while its getPropertyNames() (line 166) returns map.keySet() directly from the same LinkedHashMap-backed map field. This is the exact same order-mismatch pattern that was just fixed in MutableDocument.toMap() — document.detach().keys() and document.detach().values() (or SQLMethodValues/SQLMethodKeys called on a detached document) can still disagree on order, since new HashMap<>(map) doesn't preserve map's insertion order. Since Document.detach() is a common pattern for handing documents outside a transaction, this leaves the exact bug class from #6472 unfixed for a common code path. Worth changing to new LinkedHashMap<>(map) for consistency, along with a regression test analogous to keysAndValuesAreIndexAlignedViaSQLOnStoredDocument but calling .detach() first.

Fix this →

Minor observations (non-blocking):

  • LinkedHashMap has slightly more per-entry overhead than HashMap (extra before/after pointers for the linked list). Given CLAUDE.md's performance emphasis, this is worth being aware of, but it's a justified, necessary tradeoff here since correctness (index-aligned keys/values) requires deterministic order — there isn't a lighter-weight way to get that guarantee.
  • This is a behavior change for any existing caller of values() that relied on the old (arguably buggy) inclusion of @rid/@type/@cat. I didn't find other in-repo callers or docs relying on the old behavior (SQLMethodValues has no other internal callers besides SQL registration), so this looks safe, but it is a user-visible SQL semantics change worth calling out in release notes if there's a changelog process outside this repo.

Verification note: I wasn't able to run mvn in this environment (build/test commands require approval that isn't available here), so I reviewed the diff and cross-checked call sites manually rather than executing the test suite. The added tests (DocumentTest.toMapPreservesPropertyInsertionOrder, SQLMethodValuesTest.keysAndValuesAreIndexAlignedForDocumentWithSeveralProperties, keysAndValuesAreIndexAlignedViaSQLOnStoredDocument) look correctly constructed and should catch regressions in this exact area; if you have CI results for this branch, use those to confirm they pass.
· branch fix/6472-keys-values-asymmetry

@lvca
lvca merged commit c751bca into ArcadeData:main Aug 21, 2026
19 of 21 checks passed
tae898 pushed a commit to humemai/arcadedb-embedded-python that referenced this pull request Aug 21, 2026
…rder

Follow-up to PR ArcadeData#6500 (issue ArcadeData#6472): that PR fixed the keys()/values()
index-misalignment for MutableDocument by switching its toMap(boolean) from
a plain HashMap to a LinkedHashMap, but DetachedDocument.toMap(boolean) had
the exact same bug and was left unfixed - it also copied into a plain
HashMap while its getPropertyNames() returns the same LinkedHashMap-backed
map's keySet(). Document.detach() is a common pattern for handing documents
outside a transaction, so keys()/values() on a detached document could
still disagree by index.

Switch DetachedDocument.toMap(boolean) to LinkedHashMap as well, matching
MutableDocument and ImmutableDocument. Confirmed the added regression tests
fail against the pre-fix code (order came back scrambled, e.g.
[name, country, city, age, email] instead of insertion order) and pass
with the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keys()/values() on a Document are not parallel arrays: values() includes internal fields (@rid/@type/@cat) that keys() omits

2 participants