From 959da2ca654c5a5670fc4922186852159b3a8a99 Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Thu, 8 Oct 2026 15:36:54 -0700 Subject: [PATCH] group by ordinal: Fix bug on aliased columns This commit fixes an issue when using group by ordinal on aliased columns that would produce an incorrect expression that could be matched with the original from clause expression. Fix by resolving the expression using the column alias rather the original expression. Fixes #7357 --- compiler/semantic/sql.go | 7 +++++-- compiler/ztests/sql/group-by-ordinal.yaml | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) 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: