diff --git a/pkg/sql/opt/exec/explain/result_columns.go b/pkg/sql/opt/exec/explain/result_columns.go index 9ef00112bd62..20bdb0424ccc 100644 --- a/pkg/sql/opt/exec/explain/result_columns.go +++ b/pkg/sql/opt/exec/explain/result_columns.go @@ -258,7 +258,9 @@ func groupByColumns( columns := make(colinfo.ResultColumns, 0, len(groupCols)+len(aggregations)) if inputCols != nil { for _, col := range groupCols { - columns = append(columns, inputCols[col]) + if len(inputCols) > int(col) { + columns = append(columns, inputCols[col]) + } } } for _, agg := range aggregations { diff --git a/pkg/sql/opt/exec/explain/testdata/gists b/pkg/sql/opt/exec/explain/testdata/gists index 441bda114816..c30c8552c1b5 100644 --- a/pkg/sql/opt/exec/explain/testdata/gists +++ b/pkg/sql/opt/exec/explain/testdata/gists @@ -1122,3 +1122,33 @@ explain(shape): syntax: "select 123" explain(gist): • show completions + +# Regression for #111346 +explain-plan-gist +AgFWBgCPAQIAABNWCgsGBwwHHA0UAXoCBgEFUCJ6AQ== +---- +• upsert +│ into: ?() +│ auto commit +│ +└── • lookup join (left outer) + │ table: ?@? + │ equality: (_, _, _) = (?,?,?) + │ equality cols are key + │ + └── • distinct + │ distinct on + │ + └── • render + │ + └── • render + │ + └── • group (hash) + │ group by: _, _, _ + │ + └── • index join + │ table: ?@? + │ + └── • scan + table: ?@? + spans: 1 span