// Columnar-aggregate candidate marking: a segment whose whole job is a // bare single-label scan feeding an aggregation is a candidate for the // fused one-pass column operator (exec's tryColumnarAgg). The plan side // checks only STRUCTURE -- stage and aggregate shapes -- and never looks // at expressions or the graph; the executor classifies every filter, // group key, and aggregate argument against typed columns at run time // and falls back to the general chain when anything declines, so the // marking is purely an opportunity, never a semantic commitment. The // trigger is shape-generic by construction: any unseen query with the // same structure is marked identically.
package plan // lowerColumnarAggs marks each candidate chain head of a built branch. func lowerColumnarAggs( []*Segment) { for := range { [].ColAgg = ColAggChainLen(, ) > 0 } } // ColAggChainLen is the number of segments a columnar-aggregate fusion // starting at i would consume: the scan segment itself when its own // boundary aggregates, or the scan plus a run of stage-less pure per-row // boundaries (the LET pipeline) ending in a stage-less aggregated // segment. 0 means segment i heads no fusable chain. Shared with the // executor so the plan marking and the runtime walk agree. func ( []*Segment, int) int { := [] if !colAggScanStage() { return 0 } if .Proj.Aggregated { if colAggProj(&.Proj) { return 1 } return 0 } if !colAggPassthrough() { return 0 } for := + 1; < len(); ++ { := [] if len(.Stages) != 0 { return 0 } if .Proj.Aggregated { if colAggProj(&.Proj) { return - + 1 } return 0 } if !colAggPassthrough() { return 0 } } return 0 } // colAggScanStage reports the head shape: one bare single-label scan // stage, no expands, no path bind, no inline props. OPTIONAL is a flag, // not a reason to decline: the fused pass is the same scan with a // null-fill row emitted through the aggregator when nothing survives // (the executor handles it), so both spellings take the same kernel. func colAggScanStage( *Segment) bool { if len(.Stages) != 1 { return false } , := .Stages[0].(*MatchStage) if ! || .PathBind != nil || len(.Ops) != 1 { return false } := &.Ops[0] if .Kind != OpScan || .Source.Kind != ScanLabel { return false } return len(.Props) == 0 && len(.Labels) <= 1 } // colAggPassthrough reports a pure per-row boundary with no filter: rows // map 1:1 through it, so the fused pass can absorb its derived columns. func colAggPassthrough( *Segment) bool { := &.Proj return !.Aggregated && !.Distinct && len(.OrderBy) == 0 && .Skip == nil && .Limit == nil && len(.Post) == 0 && .NHidden == 0 && .PostWhere == nil } // colAggProj reports a fusable aggregated boundary: non-DISTINCT, only // count/sum aggregates without DISTINCT. func colAggProj( *ProjPlan) bool { if .Distinct { return false } for , := range .Aggs { if .Distinct || (.Kind != AggCount && .Kind != AggSum) { return false } } return true }