Skip to content

Commit dbe6a42

Browse files
committed
pass all tests
1 parent 55ca62e commit dbe6a42

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

sql/planbuilder/aggregates.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,18 +372,14 @@ func (b *Builder) buildAggFunctionArgs(inScope *scope, e *ast.FuncExpr, gb *grou
372372
var args []sql.Expression
373373
for _, arg := range e.Exprs {
374374
e := b.selectExprToExpression(inScope, arg)
375+
if gf, ok := e.(*expression.GetField); ok && gf.TableId() == 0 {
376+
e = b.selectExprToExpression(inScope.parent, arg)
377+
}
375378
switch e := e.(type) {
376379
case *expression.GetField:
377-
//if e.TableId() == 0 {
378-
// // TODO: not sure where this came from but it's not true
379-
// // aliases are not valid aggregate arguments, the alias must be masking a column
380-
// gf := b.selectExprToExpression(inScope.parent, arg)
381-
// // var ok bool
382-
// e, ok := gf.(*expression.GetField)
383-
// if !ok || e.TableId() == 0 {
384-
// b.handleErr(fmt.Errorf("failed to resolve aggregate column argument: %s", gf))
385-
// }
386-
//}
380+
if e.TableId() == 0 {
381+
b.handleErr(fmt.Errorf("failed to resolve aggregate column argument: %s", e))
382+
}
387383
args = append(args, e)
388384
col := scopeColumn{tableId: e.TableID(), db: e.Database(), table: e.Table(), col: e.Name(), scalar: e, typ: e.Type(), nullable: e.IsNullable()}
389385
gb.addInCol(col)
@@ -953,6 +949,7 @@ func (b *Builder) buildHaving(fromScope, projScope, outScope *scope, having *ast
953949
havingScope := b.newScope()
954950
if fromScope.parent != nil {
955951
havingScope.parent = fromScope.parent
952+
havingScope.parent.selectColumnAliases = fromScope.selectColumnAliases
956953
}
957954

958955
// add columns from fromScope referenced in the groupBy

sql/planbuilder/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
144144
err := sql.ErrColumnNotFound.New(gf.String())
145145
b.handleErr(err)
146146
}
147-
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), typ: gf.Type(), scalar: e, nullable: gf.IsNullable()}
147+
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), scalar: e, typ: gf.Type(), nullable: gf.IsNullable()}
148148
} else if sq, ok := e.Child.(*plan.Subquery); ok {
149149
col = scopeColumn{col: e.Name(), scalar: e, typ: sq.Type(), nullable: sq.IsNullable()}
150150
} else {

sql/planbuilder/scalar.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
123123
colName := strings.ToLower(v.Name.String())
124124
c, ok := inScope.resolveColumn(dbName, tblName, colName, true, false)
125125
if !ok {
126-
alias, ok := inScope.selectColumnAliases[colName]
127-
if ok {
126+
if alias, ok := inScope.selectColumnAliases[colName]; ok {
128127
return alias.scalar
129128
}
130129
sysVar, scope, ok := b.buildSysVar(v, ast.SetScope_None)

sql/planbuilder/scope.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,6 @@ func (c scopeColumn) scalarGf() sql.Expression {
655655
switch e := c.scalar.(type) {
656656
case *expression.ProcedureParam:
657657
return e
658-
case *expression.Alias:
659-
return e
660658
}
661659
}
662660
if c.originalCol != "" {

0 commit comments

Comments
 (0)