Skip to content

Commit 30d68a1

Browse files
committed
Simplified initGroupBy
1 parent c9ae582 commit 30d68a1

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

sql/planbuilder/aggregates.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro
9595
// 3) an index into selects
9696
// 4) a simple non-aggregate expression
9797
groupings := make([]sql.Expression, 0)
98-
if fromScope.groupBy == nil {
99-
fromScope.initGroupBy()
100-
}
98+
fromScope.initGroupBy()
99+
101100
g := fromScope.groupBy
102101
for _, e := range groupby {
103102
var col scopeColumn
@@ -194,9 +193,7 @@ func (b *Builder) buildAggregation(fromScope, projScope *scope, groupingCols []s
194193
// - grouping cols projection
195194
// - aggregate expressions
196195
// - output projection
197-
if fromScope.groupBy == nil {
198-
fromScope.initGroupBy()
199-
}
196+
fromScope.initGroupBy()
200197

201198
group := fromScope.groupBy
202199
outScope := group.outScope
@@ -281,9 +278,7 @@ func (b *Builder) buildAggregateFunc(inScope *scope, name string, e *ast.FuncExp
281278
b.handleErr(err)
282279
}
283280

284-
if inScope.groupBy == nil {
285-
inScope.initGroupBy()
286-
}
281+
inScope.initGroupBy()
287282
gb := inScope.groupBy
288283

289284
if strings.EqualFold(name, "count") {
@@ -466,9 +461,7 @@ func (b *Builder) buildCountStarAggregate(e *ast.FuncExpr, gb *groupBy) sql.Expr
466461

467462
// buildGroupConcat builds a GROUP_CONCAT aggregate function
468463
func (b *Builder) buildGroupConcat(inScope *scope, e *ast.GroupConcatExpr) sql.Expression {
469-
if inScope.groupBy == nil {
470-
inScope.initGroupBy()
471-
}
464+
inScope.initGroupBy()
472465
gb := inScope.groupBy
473466

474467
args := make([]sql.Expression, len(e.Exprs))
@@ -898,9 +891,7 @@ func (b *Builder) buildHaving(fromScope, projScope, outScope *scope, having *ast
898891
if having == nil {
899892
return
900893
}
901-
if fromScope.groupBy == nil {
902-
fromScope.initGroupBy()
903-
}
894+
fromScope.initGroupBy()
904895

905896
havingScope := b.newScope()
906897
if fromScope.parent != nil {

sql/planbuilder/scope.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ func (s *scope) initProc() {
224224
// initGroupBy creates a container scope for aggregation
225225
// functions and function inputs.
226226
func (s *scope) initGroupBy() {
227-
s.groupBy = &groupBy{outScope: s.replace()}
227+
if s.groupBy != nil {
228+
s.groupBy = &groupBy{outScope: s.replace()}
229+
}
228230
}
229231

230232
// pushSubquery creates a new scope with the subquery already initialized.

0 commit comments

Comments
 (0)