A stored -0.0 is one value to AQL comparison and two values to AQL hashing, so creating an index changes the result of a COLLECT
Author: adoublebCreated Sep 24, 2026Updated Sep 30, 2026
Version
- ArangoDB 3.12.11, official
arangodb:3.12.11image, single server. Verified via both the HTTP API andarangosh. - Also reproduced unchanged on 3.12.4-3 (
arangodb/arangodb:latest).
Summary of the bug
A document attribute holding -0.0 is treated as the same value as 0 by every comparison-based path in the engine:
| path | verdict |
|---|---|
== |
equal |
| unique persistent index | duplicate — insert rejected with errorNum 1210 |
COLLECT ... OPTIONS {method: 'sorted'} |
one group |
SORTED_UNIQUE |
one element |
and as two different values by every hash-based path:
| path | verdict |
|---|---|
COLLECT with the default (hash) method |
two groups, both keyed by zero |
UNIQUE |
two elements |
COUNT_DISTINCT |
2 |
UNION_DISTINCT |
two elements |
Also, COUNT_DISTINCT(x) is 2 while LENGTH(SORTED_UNIQUE(x)) is 1.
Repro
db._create("z");
db.z.insert([{_key: "a", f: 0.0}, {_key: "b", f: -0.0}, {_key: "c", f: 0}]);
db._query("FOR n IN z SORT n._key RETURN TO_STRING(n.f)").toArray();
-> ["0", "-0", "0"]
// the engine's comparison says there is one value here
db._query("RETURN [DOCUMENT('z/b').f == DOCUMENT('z/a').f, DOCUMENT('z/b').f == 0]").toArray();
-> [[true, true]]
db.z.ensureIndex({type: "persistent", fields: ["f"], unique: true});
-> ERROR 1210: unique constraint violated - in index ... over 'f'; conflicting key: a
db._query("FOR n IN z COLLECT k = n.f WITH COUNT INTO c OPTIONS {method: 'sorted'} RETURN [k, c]").toArray();
-> [[0, 3]]
// ... and the engine's hashing says there are two
db._query("FOR n IN z COLLECT k = n.f WITH COUNT INTO c RETURN [k, c]").toArray();
-> [[0, 1], [0, 2]] // two groups, both keyed zero
db._query("RETURN [COUNT_DISTINCT(FOR n IN z RETURN n.f), LENGTH(SORTED_UNIQUE(FOR n IN z RETURN n.f))]").toArray();
-> [[2, 1]] // COUNT_DISTINCT and LENGTH(SORTED_UNIQUE returns different answers
db._query("RETURN UNIQUE(FOR n IN z RETURN n.f)").toArray(); -> [[0, 0]]
db._query("FOR n IN z COLLECT k = n.f INTO g RETURN [k, g[*].n._key]").toArray();
-> [[0, ["b"]], [0, ["a", "c"]]] // the -0.0 row is the odd one outIndex changes query results
Q = FOR n IN z COLLECT k = n.f WITH COUNT INTO c RETURN [k, c]
A) no index on f
plan: SingletonNode EnumerateCollectionNode CollectNode(hash) SortNode CalculationNode ReturnNode
result: [[-0, 1], [0, 2]]
B) after db.z.ensureIndex({type: "persistent", fields: ["f"]}) // plain, non-unique
plan: SingletonNode IndexNode CollectNode(sorted) CalculationNode ReturnNode
result: [[0, 3]]
C) after dropping that index
result: [[-0, 1], [0, 2]]Why it is a bug
Values that compare equal should hash equal.
An index changes a query result.
Source: arangodb/arangodb