diff --git a/compiler/semantic/sql.go b/compiler/semantic/sql.go index 4f96d86c0..3f52a7709 100644 --- a/compiler/semantic/sql.go +++ b/compiler/semantic/sql.go @@ -674,11 +674,14 @@ func (t *translator) groupBy(sch *selectScope, in []ast.Expr, inType super.Type) } else { save := sch.groupByLoc sch.groupByLoc = expr - if col := sch.columns[colno-1]; col.semExpr != nil { + switch col := sch.columns[colno-1]; { + case col.semExpr != nil: // If semExpr is not nil, column refers to an expanded star // expression - use this value. e, typ = col.semExpr, col.typ - } else { + case col.lateral: + e, typ = resolveLateralColumn(t, sch, col.name, inType) + default: e, typ = t.expr(col.astExpr, inType) } sch.groupByLoc = save diff --git a/compiler/ztests/sql/group-by-ordinal.yaml b/compiler/ztests/sql/group-by-ordinal.yaml index 153cd42d4..359739f46 100644 --- a/compiler/ztests/sql/group-by-ordinal.yaml +++ b/compiler/ztests/sql/group-by-ordinal.yaml @@ -4,6 +4,8 @@ script: | super -s -c 'select 1, b, count() as c from test.sup group by 1, b order by c desc' echo // === super -s -c 'select * from test.sup group by 1,2,3,4 order by 2 desc' + echo // === + super -s -c 'select a+1 a, count(*) as c from test.sup group by 1' ! super -s -c 'select a, b, c, d from test.sup group by 5, 0' echo // === >&2 ! super -s -c 'select count() as c, a from test.sup group by 1,2' @@ -25,6 +27,8 @@ outputs: // === {a:1,b:"foo",c:1,d:1} {a:1,b:"bar",c:1,d:1} + // === + {a:2,c:3} - name: stderr data: | position 5 is not in select list at line 1, column 42: