@@ -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
0 commit comments