Skip to content

Commit 13d0ce7

Browse files
authored
check group by ordinal range (#2911)
1 parent 45c4c4c commit 13d0ce7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

sql/planbuilder/aggregates.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro
131131
b.handleErr(fmt.Errorf("expected integer order by literal"))
132132
}
133133
if intIdx < 1 {
134+
// TODO: this actually works in MySQL
134135
b.handleErr(fmt.Errorf("expected positive integer order by literal"))
135136
}
137+
if int(intIdx) > len(selects) {
138+
b.handleErr(fmt.Errorf("column ordinal out of range: %d", intIdx))
139+
}
136140
col = projScope.cols[intIdx-1]
137141
default:
138142
expr := b.buildScalar(fromScope, e)

sql/planbuilder/parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,6 +2950,25 @@ func TestPlanBuilderErr(t *testing.T) {
29502950
Query: "select x + 1 as xx from xy join uv on (x = u) having x = 123;",
29512951
Err: "column \"x\" could not be found in any table in scope",
29522952
},
2953+
2954+
// Test GroupBy Ordinals
2955+
{
2956+
Query: "select 1 from xy group by 'abc';",
2957+
Err: "expected integer order by literal",
2958+
},
2959+
{
2960+
// TODO: this actually works in MySQL
2961+
Query: "select 1 from xy group by -123;",
2962+
Err: "expected positive integer order by literal",
2963+
},
2964+
{
2965+
Query: "select 1 from xy group by 0;",
2966+
Err: "expected positive integer order by literal",
2967+
},
2968+
{
2969+
Query: "select 1 from xy group by 100;",
2970+
Err: "column ordinal out of range: 100",
2971+
},
29532972
}
29542973

29552974
db := memory.NewDatabase("mydb")

0 commit comments

Comments
 (0)