Skip to content

CollectionUtils.compare(Map,Map) returns 1 in both directions for disjoint keys and 0 for unequal maps sharing a null key, and BinaryComparator orders index entries with it #7111

Closed

Description

@ruispereira

Summary

CollectionUtils.compare(Map, Map) violates the comparator contract in two ways: for maps with disjoint keys it returns 1 in both directions, and if the first shared key is null on both sides it returns 0 immediately, declaring unequal maps equal. It is reached from BinaryComparator, the comparator that orders index entries.

Where (HEAD 38034d9c8)

engine/src/main/java/com/arcadedb/utility/CollectionUtils.java:45-67:

public static int compare(final Map<?, Comparable> m1, final Map<?, Comparable> m2) {
  final Set<? extends Map.Entry<?, Comparable>> entries1 = m1.entrySet();
  for (Map.Entry<?, Comparable> entry : entries1) {
    final Comparable value1 = entry.getValue();
    final Comparable value2 = m2.get(entry.getKey());
    if (value1 == null) {
      if (value2 == null)
        return 0;                    // <- (b) declares the maps equal on a shared null
      return -1;
    } else if (value2 == null)
      return 1;                      // <- (a) fires for every disjoint key, in both directions
    ...

Callers: BinaryComparator.java:250 and :468 (plus :242 for the List overload), and TransactionIndexContext.java:185.

Repro

Verified at 38034d9c8:

chaves disjuntas {a:1} vs {b:1}
  compare(m1,m2) = 1
  compare(m2,m1) = 1
  antisymmetric? false

first shared key null on both: {k:null,z:1} vs {k:null,z:2}
  compare = 0            <- declares them equal; z is never examined

via BinaryComparator (the index-ordering entry point)
  compareTo(m1,m2) = 1
  compareTo(m2,m1) = 1

For (a): m2.get(key) is null for every key of m1 when the key sets are disjoint, so the value2 == null -> return 1 arm fires whichever way round the arguments go.

For (b): a null on both sides is treated as "equal, and therefore the whole maps are equal", returning before any later key is compared.

Note a List.sort with this comparator over three disjoint-key maps did not throw Comparison method violates its general contract! in my run - TimSort only detects some violations - so the failure mode is a wrong order rather than a loud error.

Impact

Antisymmetry is what index ordering depends on. A MAP-valued key whose comparator answers "greater" in both directions gives a non-deterministic order: which entry sorts first depends on the comparison order the sort happens to use, so an index over such a property can place equal-ranked entries inconsistently between a build and a rebuild, and a range scan's stop condition can be decided by whichever way round the comparison was made. That is the same class of problem as #6997 (two orderings disagreeing) in the same class.

(b) is the simpler bug: two maps that differ only after a shared null key compare equal, so a unique index over such a key would reject the second as a duplicate.

Suggested fix

  • Iterate a deterministic union of both key sets rather than only m1's, and break the disjoint case by comparing key sets (or fall back to size, then to a stable key ordering) so the answer is antisymmetric.
  • On a shared null, continue to the next key instead of returning 0; return 0 only after every key has compared equal.

Worth a contract test over a handful of map pairs asserting signum(compare(a,b)) == -signum(compare(b,a)) and transitivity - that invariant is what neither case satisfies today.

Scope / confidence

High on the mechanism and the propagation to BinaryComparator, both reproduced above. Medium on how often a MAP-typed index key occurs in practice, which is what bounds the real-world impact.

Activity

  1. added
    engine
    on Sep 3, 2026
  2. added this to the 26.9.1 milestone on Sep 3, 2026
  3. added
    in progress
    on Sep 3, 2026
  4. self-assigned this
    on Sep 3, 2026
  5. added 3 commits that reference this issue on Sep 3, 2026
  6. removed
    in progress
    on Sep 3, 2026
  7. lvca commented on Sep 3, 2026

    @lvca
    Member

    Fixed in #7117 (milestone 26.9.1).

    Maps are now compared as their sorted sequence of (key, value) pairs: the order is antisymmetric and transitive, independent of insertion order, and a null value on a shared key no longer ends the comparison early.

    Note if you have an index on a MAP property: its on-disk order changes with this release, so rebuild that index after upgrading.


    ❤️ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengine

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions