Skip to content

keys() and values() fall out of alignment on a sub-query row: values() returns @cat, @type and @rid` as data, and the record's value where the row overrode it #9843

Closed

Description

@ruispereira

Summary

keys() and values() are an index-aligned pair: the n-th key names the n-th value. That rule was
stated and fixed once, for a Document receiver. A Result receiver - which is what a row of a
sub-query, a LET variable or any nested result set is - breaks it in two separate ways, both
because values() goes through ResultInternal.toMap():

  1. Metadata leaks into the values. ResultInternal.toMap() answers element.toMap() for a row
    that wraps a record, and Document.toMap() is toMap(true). So the record's @cat, @type and
    @rid - and, on an edge, @in and @out - come back as unlabelled data values. A two-property
    document answers 2 keys and 5 values; an edge with one property answers 1 key and 6 values.
  2. The values are the RECORD's, not the ROW's. ResultInternal.toMap() is, by its own comment,
    "deliberately element-first, which is the opposite precedence to
    getProperty()/getPropertyNames()". keys() uses getPropertyNames(). So on a row that
    overrides a property of its element, keys() lists the row's names and values() returns the
    element's values, and the pair reports a value the row does not have.

Where (HEAD ac84740)

  • engine/src/main/java/com/arcadedb/query/sql/method/collection/SQLMethodValues.java 52-57:
    line 55 return new ArrayList<>(document.toMap(false).values()); with the keys()/values() on a Document are not parallel arrays: values() includes internal fields (@rid/@type/@cat) that keys() omits #6472 comment at 53-54,
    and line 57 return result.toMap().values();
  • engine/src/main/java/com/arcadedb/query/sql/method/collection/SQLMethodKeys.java 50-55:
    document.getPropertyNames() and result.getPropertyNames().
  • engine/src/main/java/com/arcadedb/query/sql/executor/ResultInternal.java 582-601 (toMap(),
    element-first by design) and 386 (getPropertyNames(), content-first).
  • The contract that makes toMap() the metadata overload:
    engine/src/main/java/com/arcadedb/database/Document.java 149-154.

Repro

Embedded, built from this revision. D1 holds {n:1, s:'x'} at #1:0; V1/R1 hold one vertex
{n:1} and one edge {w:7}.

Control, a Document receiver - correct:

select @this.keys() as k, @this.values() as v from D1
  -> {k: [n, s], v: [1, x]}

A Result receiver, same record:

select $a[0].keys() as k, $a[0].values() as v from D1 let $a = (select from D1)
  -> {k: [n, s],  v: [1, x, d, D1, #1:0]}
select $a[0].keys().size() as nk, $a[0].values().size() as nv from D1 let $a = (select from D1)
  -> {nk: 2, nv: 5}
select $a[0].keys() as k, $a[0].values() as v from D1 let $a = (select from V1 where n=1)
  -> {k: [n],     v: [1, v, V1, #2:0]}
select $a[0].keys() as k, $a[0].values() as v from D1 let $a = (select from R1)
  -> {k: [w],     v: [7, e, R1, #5:0, #2:1, #2:0]}

The second half, on a row that overrides one of its element's properties:

select $a[0].s as s, $a[0].keys() as k, $a[0].values() as v
  from D1 let $a = (select *, 'OVERRIDE' as s from D1)
  -> {s: OVERRIDE, k: [n, s, @rid, @type], v: [1, x, d, D1, #1:0]}

$a[0].s is OVERRIDE; the s slot of values() is x.

A row with no element behind it is correct (let $a = (select n from D1) gives k: [n], v: [1]),
which is what confines this to element-backed rows.

Suggested fix

values() must read from the same place keys() reads from. The smallest correct change is to make
the Result branch walk result.getPropertyNames() and collect result.getProperty(name), which
fixes both halves at once: no metadata, and the row's own values with the row's own precedence.

Better, because it stops the pair drifting a third time: give the two methods one shared helper that
turns any receiver into the ordered name/value view both of them project, and have keys() return
its keys and values() its values. The Document branch was already aligned by hand once (#6472)
and the Result branch three lines below it was not; two methods in two files each dispatching over
four receiver types is how that happened.

Leave ResultInternal.toMap() alone. Its element-first precedence is deliberate and documented
(it is what JsonSerializer flattens a whole-entity projection through), so it is the wrong map for
this caller rather than a wrong map.

Scope / confidence

Reproduced through the SQL engine against a database built from this revision, for a document, a
vertex, an edge, an element-free projection row (the negative control) and an overriding row.
Refutations tried: (a) that values() on a Result is documented to include metadata while
keys() is not - the method's own comment states the opposite rule for the branch three lines up,
and Map and Document receivers both honour it; (b) that a Result receiver is an internal shape
users cannot reach - let $a = (select ...) and any sub-query row reach it, shown above;
(c) that the length mismatch alone explains it, so the override case is the same defect counted
twice - it is not, the override case keeps the same length and changes a value, and it comes from
the precedence half of ResultInternal.toMap() rather than from the metadata half, so a fix that
only switched to a toMap(false)-equivalent would leave it.
Not established, and recorded rather than claimed: select *, 'OVERRIDE' as s from D1 itself
renders "s":"x" in the row it returns while getProperty("s") answers OVERRIDE. That is the
same element-first precedence, reached through JsonSerializer rather than through values(), and
ResultInternal.toMap()'s comment says the precedence is deliberate for a different reason
(issue #5613), so settling whether the rendered row is right needs the projection semantics
decided first.


Activity

  1. added
    engine
    severity:minor
    on Oct 12, 2026
  2. added this to the 26.11.1 milestone on Oct 12, 2026
  3. added
    in progress
    on Oct 12, 2026
  4. self-assigned this
    on Oct 12, 2026
  5. added a commit that references this issue on Oct 12, 2026
    eeaee11
  6. lvca commented on Oct 12, 2026

    @lvca
    Member

    Fixed in #9866 (26.11.1).

    Confirmed: on a sub-query row (or a LET variable), values() returned the record's @cat, @type and @rid as extra values, and for a row that overrode a property it returned the record's value instead of the row's.

    It now returns exactly one value per keys() entry, in the same order, using the row's own value. keys() and values() share one code path, so they can't drift apart again.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineseverity:minorsql

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions