// Group-join stage runner: the decorrelated OPTIONAL-MATCH aggregate // join. The standalone inner plan runs once (lazily, on the first outer // row) into a key -> aggregate-values table; each outer row binds its // synthetic aggregate slots by correlation-key lookup, with the // aggregates' empty-group identities as the fill. The segment projection // re-aggregates the synthetic columns, so the emitted rows reproduce the // nested left-join answer exactly.
package exec import ( ) type groupJoinSink struct { ctx *eval.Ctx gj *plan.GroupJoinStage buf []value.Value // table maps an encoded correlation key to its group's aggregate // values through the interned-key probe table -- the map[string] form // paid one heap string per group on build. table flatset.ByteMap groups [][]value.Value built bool keyBuf []byte next rowSink count *uint64 } func ( *groupJoinSink) ( []value.Value) bool { if !.built { := len(.gj.KeySlots) for , := range runSubplan(.ctx, .gj.Sub, nil) { := .keyBuf[:0] for := range { = value.AppendKey(, []) } .keyBuf = := [:] := .table.GetOrCreate(, func() int { .groups = append(.groups, ) return len(.groups) - 1 }) .groups[] = } .built = true } := .keyBuf[:0] for , := range .gj.KeySlots { = value.AppendKey(, []) } .keyBuf = copy(.buf, ) if , := .table.Get(); { := .groups[] for , := range .gj.OutSlots { .buf[] = [] } } else { for , := range .gj.OutSlots { if .gj.Fills[] == plan.FillZero { .buf[] = value.Int(0) } else { .buf[] = value.Null() } } } if .count != nil { *.count++ } return .next.push(.buf) } func ( *groupJoinSink) () { .next.close() }