Skip to content

Commit fead863

Browse files
authored
prevent panics in replacePkSort (#2916)
1 parent b37ee95 commit fead863

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sql/analyzer/replace_sort.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func replaceAgg(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.Scope,
231231
return n, transform.SameTree, nil
232232
}
233233

234+
if pkIdx == nil {
235+
return n, transform.SameTree, nil
236+
}
237+
234238
// generate sort fields from aggregations
235239
var sf sql.SortField
236240
switch agg := gb.SelectedExprs[0].(type) {
@@ -257,7 +261,9 @@ func replaceAgg(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.Scope,
257261
}
258262

259263
// since we're only supporting one aggregation, it must be on the first column of the primary key
260-
if !strings.EqualFold(pkIdx.Expressions()[0], sf.Column.String()) {
264+
if pkCols := pkIdx.Expressions(); len(pkCols) < 1 {
265+
return n, transform.SameTree, nil
266+
} else if !strings.EqualFold(pkCols[0], sf.Column.String()) {
261267
return n, transform.SameTree, nil
262268
}
263269

0 commit comments

Comments
 (0)