Skip to content
Merged
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
4 changes: 4 additions & 0 deletions sql/planbuilder/aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions sql/planbuilder/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading