Skip to content

Allow select aliases to be in group by/having #3073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions sql/planbuilder/aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,16 @@ func (b *Builder) buildAggFunctionArgs(inScope *scope, e *ast.FuncExpr, gb *grou
e := b.selectExprToExpression(inScope, arg)
switch e := e.(type) {
case *expression.GetField:
if e.TableId() == 0 {
// TODO: not sure where this came from but it's not true
// aliases are not valid aggregate arguments, the alias must be masking a column
gf := b.selectExprToExpression(inScope.parent, arg)
var ok bool
e, ok = gf.(*expression.GetField)
if !ok || e.TableId() == 0 {
b.handleErr(fmt.Errorf("failed to resolve aggregate column argument: %s", gf))
}
}
//if e.TableId() == 0 {
// // TODO: not sure where this came from but it's not true
// // aliases are not valid aggregate arguments, the alias must be masking a column
// gf := b.selectExprToExpression(inScope.parent, arg)
// // var ok bool
// e, ok := gf.(*expression.GetField)
// if !ok || e.TableId() == 0 {
// b.handleErr(fmt.Errorf("failed to resolve aggregate column argument: %s", gf))
// }
//}
args = append(args, e)
col := scopeColumn{tableId: e.TableID(), db: e.Database(), table: e.Table(), col: e.Name(), scalar: e, typ: e.Type(), nullable: e.IsNullable()}
gb.addInCol(col)
Expand Down
6 changes: 5 additions & 1 deletion sql/planbuilder/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
err := sql.ErrColumnNotFound.New(gf.String())
b.handleErr(err)
}
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), scalar: e, typ: gf.Type(), nullable: gf.IsNullable()}
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), typ: gf.Type(), scalar: e, nullable: gf.IsNullable()}
} else if sq, ok := e.Child.(*plan.Subquery); ok {
col = scopeColumn{col: e.Name(), scalar: e, typ: sq.Type(), nullable: sq.IsNullable()}
} else {
Expand All @@ -151,6 +151,10 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
col.scalar = e
tempScope.addColumn(col)
}
if inScope.selectColumnAliases == nil {
inScope.selectColumnAliases = make(map[string]scopeColumn)
}
inScope.selectColumnAliases[e.Name()] = col
exprs = append(exprs, e)
default:
exprs = append(exprs, pe)
Expand Down
8 changes: 5 additions & 3 deletions sql/planbuilder/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,16 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
colName := strings.ToLower(v.Name.String())
c, ok := inScope.resolveColumn(dbName, tblName, colName, true, false)
if !ok {
alias, ok := inScope.selectColumnAliases[colName]
if ok {
return alias.scalar
}
sysVar, scope, ok := b.buildSysVar(v, ast.SetScope_None)
if ok {
return sysVar
}
var err error
if scope == ast.SetScope_User {
err = sql.ErrUnknownUserVariable.New(colName)
} else if scope == ast.SetScope_Persist || scope == ast.SetScope_PersistOnly {
if scope == ast.SetScope_User || scope == ast.SetScope_Persist || scope == ast.SetScope_PersistOnly {
err = sql.ErrUnknownUserVariable.New(colName)
} else if scope == ast.SetScope_Global || scope == ast.SetScope_Session {
err = sql.ErrUnknownSystemVariable.New(colName)
Expand Down
15 changes: 13 additions & 2 deletions sql/planbuilder/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type scope struct {

insertTableAlias string
insertColumnAliases map[string]string

selectColumnAliases map[string]scopeColumn
}

// resolveColumn matches a variable use to a column definition with a unique
Expand Down Expand Up @@ -441,6 +443,12 @@ func (s *scope) copy() *scope {
if !s.colset.Empty() {
ret.colset = s.colset.Copy()
}
if s.selectColumnAliases != nil {
ret.selectColumnAliases = make(map[string]scopeColumn, len(s.selectColumnAliases))
for k, v := range s.selectColumnAliases {
ret.selectColumnAliases[k] = v
}
}

return &ret
}
Expand Down Expand Up @@ -644,8 +652,11 @@ func (c scopeColumn) withOriginal(origTbl, col string) scopeColumn {
// scalarGf returns a getField reference to this column's expression.
func (c scopeColumn) scalarGf() sql.Expression {
if c.scalar != nil {
if p, ok := c.scalar.(*expression.ProcedureParam); ok {
return p
switch e := c.scalar.(type) {
case *expression.ProcedureParam:
return e
case *expression.Alias:
return e
}
}
if c.originalCol != "" {
Expand Down
Loading