diff --git a/sql/planbuilder/aggregates.go b/sql/planbuilder/aggregates.go index 8fabe2ff9d..2fd7280f36 100644 --- a/sql/planbuilder/aggregates.go +++ b/sql/planbuilder/aggregates.go @@ -131,8 +131,12 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro b.handleErr(fmt.Errorf("expected integer order by literal")) } if intIdx < 1 { + // TODO: this actually works in MySQL b.handleErr(fmt.Errorf("expected positive integer order by literal")) } + if int(intIdx) > len(selects) { + b.handleErr(fmt.Errorf("column ordinal out of range: %d", intIdx)) + } col = projScope.cols[intIdx-1] default: expr := b.buildScalar(fromScope, e) diff --git a/sql/planbuilder/parse_test.go b/sql/planbuilder/parse_test.go index a9ca2fd73f..82e1225d5b 100644 --- a/sql/planbuilder/parse_test.go +++ b/sql/planbuilder/parse_test.go @@ -2950,6 +2950,25 @@ func TestPlanBuilderErr(t *testing.T) { Query: "select x + 1 as xx from xy join uv on (x = u) having x = 123;", Err: "column \"x\" could not be found in any table in scope", }, + + // Test GroupBy Ordinals + { + Query: "select 1 from xy group by 'abc';", + Err: "expected integer order by literal", + }, + { + // TODO: this actually works in MySQL + Query: "select 1 from xy group by -123;", + Err: "expected positive integer order by literal", + }, + { + Query: "select 1 from xy group by 0;", + Err: "expected positive integer order by literal", + }, + { + Query: "select 1 from xy group by 100;", + Err: "column ordinal out of range: 100", + }, } db := memory.NewDatabase("mydb")