AQL `COLLECT` served from a persistent index merges the array values `[]` and `[null]` into one group
Description
When a COLLECT groups by an attribute that has a persistent index and the optimizer feeds the collect from the
index (IndexNode -> CollectNode), the array values [] and [null] end up in one group: the group [] gets the
count of both and the group [null] is missing. With indexes disabled, or without the index, the two values are two
groups, as they are two distinct values in AQL ([] == [null] is false).
Reproduction
db._create("c");
db.c.save([{ v1: null }, { v1: [] }, { v1: [null] }]);
db.c.ensureIndex({ type: "persistent", fields: ["v1"] });
db._query("FOR d IN c COLLECT x = d.v1 WITH COUNT INTO n RETURN [x, n]").toArray();
// [ [null, 1], [[], 2] ] <- expected [ [null, 1], [[], 1], [[null], 1] ]
db._query("FOR d IN c OPTIONS { disableIndex: true } COLLECT x = d.v1 WITH COUNT INTO n RETURN [x, n]").toArray();
// [ [null, 1], [[], 1], [[null], 1] ]COLLECT x = d.v1, y = d.v2 over an index on v1, v2 shows the same merge. Reading the values through the index is
fine: FILTER d.v1 != 5 RETURN d.v1, SORT d.v1 RETURN d.v1 and RETURN DISTINCT d.v1 through the same index all
return [] and [null] as distinct values. The plan of the failing query is IndexNode -> CollectNode with the
rules use-indexes, use-index-for-sort, reduce-extraction-to-projection, optimize-projections.
Expected behavior
The same groups as without the index: [] and [null] are different values and different groups.
Source: arangodb/arangodb