-
Type:
Task
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Execution
-
None
-
None
-
None
-
None
-
None
-
None
-
None
When an accumulator has a variable initializer, we add a project to rewrite the group key into an object form, and use that as the group key slot as long as the group key is also an object. Currently we don't use streaming group in this case.
To support, need to correctly build a cluster key representation of the rewritten grouping key.
Example rewrites:
Grouping key is an object. We build an object form of the key (s6) and use that as the grouping key and the slot used by the initializer:
db.coll.aggregate([{$group: {_id: {a: "$a", c:"$c"}, t: {$topN: {n: "$a", sortBy: {b:1}, output: "foo"}}}}])
[3] project [s11 = newBsonObj("_id", s6, "t", s10)] [3] project [s10 = aggTopNFinalize(s9, s7)] [3] group [s6] [s9 = aggTopN(s7, null, getSortKeyAsc(s4), "foo") init{ let [ l2.0 = convert ( getField(s6, "a"), int64) ] in if (exists(l2.0) && (l2.0 > 0ll)) then newArray([], 0ll, move(l2.0), 0, 104857600) else fail(7548607, "parameter 'n' must be coercible to a positive 64-bit integer") }] spillSlots[s8] mergingExprs[aggTopNMerge(s8, s7)] [3] project [s6 = newObj("a", s3, "c", s5)] [1] scan generic [s1 = record, s2 = recordId] [s3 = a, s4 = b, s5 = c] @"3ee3e1d5-fa2d-47d8-9ab2-72182452bf9d"
Grouping key is a scalar field ref. the accumulator uses a different slot than the grouping stage. The group uses s9 (the group ref with missing converted to null) and the accumulator uses s5 (object form of s9).
db.coll.aggregate([{$group: {_id: "$b", t: {$topN: {n: "$c", sortBy: {a:1}, output: "foo"}}}}])[3] project [s11 = newBsonObj("_id", s9, "t", s10)]
[3] project [s10 = aggTopNFinalize(s8, s6)] [3] group [s9] [s8 = aggTopN(s6, null, getSortKeyAsc(s3), "foo") init{ let [ l2.0 = convert ( getField(s5, "c"), int64) ] in if (exists(l2.0) && (l2.0 > 0ll)) then newArray([], 0ll, move(l2.0), 0, 104857600) else fail(7548607, "parameter 'n' must be coercible to a positive 64-bit integer") }] spillSlots[s7] mergingExprs[aggTopNMerge(s7, s6)] [3] project [s5 = if isObject(s9) then s9 else {} ] [3] project [s9 = (s4 ?: null)] [1] scan generic [s1 = record, s2 = recordId] [s3 = a, s4 = b] @"3dadecbd-2508-4794-9190-2256bd02a621"