Skip to content

Commit 2ae3d47

Browse files
committed
allow select aliases to be in group by/having
1 parent de1be10 commit 2ae3d47

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

sql/planbuilder/project.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
135135
err := sql.ErrColumnNotFound.New(gf.String())
136136
b.handleErr(err)
137137
}
138-
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), scalar: e, typ: gf.Type(), nullable: gf.IsNullable()}
138+
col = scopeColumn{id: id, tableId: gf.TableId(), col: e.Name(), db: gf.Database(), table: gf.Table(), typ: gf.Type(), nullable: gf.IsNullable(), originalCol: gf.Name()}
139139
} else if sq, ok := e.Child.(*plan.Subquery); ok {
140140
col = scopeColumn{col: e.Name(), scalar: e, typ: sq.Type(), nullable: sq.IsNullable()}
141141
} else {
@@ -151,6 +151,10 @@ func (b *Builder) analyzeSelectList(inScope, outScope *scope, selectExprs ast.Se
151151
col.scalar = e
152152
tempScope.addColumn(col)
153153
}
154+
if inScope.selectColumnAliases == nil {
155+
inScope.selectColumnAliases = make(map[string]scopeColumn)
156+
}
157+
inScope.selectColumnAliases[e.Name()] = col
154158
exprs = append(exprs, e)
155159
default:
156160
exprs = append(exprs, pe)

sql/planbuilder/scalar.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ 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 {
128+
return alias.scalarGf()
129+
}
126130
sysVar, scope, ok := b.buildSysVar(v, ast.SetScope_None)
127131
if ok {
128132
return sysVar
129133
}
130134
var err error
131-
if scope == ast.SetScope_User {
132-
err = sql.ErrUnknownUserVariable.New(colName)
133-
} else if scope == ast.SetScope_Persist || scope == ast.SetScope_PersistOnly {
135+
if scope == ast.SetScope_User || scope == ast.SetScope_Persist || scope == ast.SetScope_PersistOnly {
134136
err = sql.ErrUnknownUserVariable.New(colName)
135137
} else if scope == ast.SetScope_Global || scope == ast.SetScope_Session {
136138
err = sql.ErrUnknownSystemVariable.New(colName)

sql/planbuilder/scope.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type scope struct {
6161

6262
insertTableAlias string
6363
insertColumnAliases map[string]string
64+
65+
selectColumnAliases map[string]scopeColumn
6466
}
6567

6668
// resolveColumn matches a variable use to a column definition with a unique
@@ -644,8 +646,11 @@ func (c scopeColumn) withOriginal(origTbl, col string) scopeColumn {
644646
// scalarGf returns a getField reference to this column's expression.
645647
func (c scopeColumn) scalarGf() sql.Expression {
646648
if c.scalar != nil {
647-
if p, ok := c.scalar.(*expression.ProcedureParam); ok {
648-
return p
649+
switch e := c.scalar.(type) {
650+
case *expression.ProcedureParam:
651+
return e
652+
case *expression.Alias:
653+
return e.Child
649654
}
650655
}
651656
if c.originalCol != "" {

0 commit comments

Comments
 (0)