Repository navigation
Description
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,
continueto the next key instead of returning0; return0only 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
fix(#7109,#7111): route the FULL_TEXT BY ITEM update through the shar…
fix(#7111): strict map key order for comparator-equal distinct keys; …
Merge pull request #7117 from ArcadeData/fix/issues-7109-7110-7111-7112
lvca commentedon Sep 3, 2026
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
nullvalue 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.
fix(ArcadeData#7109,ArcadeData#7110,ArcadeData#7111,ArcadeData#7112):…