Skip to content

Commit

Permalink
Merge #49287
Browse files Browse the repository at this point in the history
49287: opt: throw error if ordered-set aggregate is malformed r=RaduBerinde a=Anthuang

Currently, if an ordered-set aggregate contains no WITHIN GROUP clause
or no single ORDER BY column, no error is thrown and thus we end up with
a panic when we try to access the missing ORDER BY column.
Instead, throw a syntax error to the user.

Resolves: #49240

Release note: None

Co-authored-by: Anthony Huang <anthuang@umich.edu>
  • Loading branch information
craig[bot] and Anthuang committed May 21, 2020
2 parents 1bd13e7 + 1837115 commit 30d24f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/aggregate
Original file line number Diff line number Diff line change
Expand Up @@ -2547,3 +2547,10 @@ query TT
SHOW CREATE osagg_view
----
osagg_view CREATE VIEW osagg_view (disc, cont) AS SELECT percentile_disc(0.50) WITHIN GROUP (ORDER BY f), percentile_cont(0.50) WITHIN GROUP (ORDER BY f DESC) FROM test.public.osagg

# Test malformed ordered-set aggregation.
statement error ordered-set aggregations must have a WITHIN GROUP clause containing one ORDER BY column
SELECT percentile_disc(0.50) FROM osagg

statement error ordered-set aggregations must have a WITHIN GROUP clause containing one ORDER BY column
SELECT percentile_cont(0.50) FROM osagg
7 changes: 7 additions & 0 deletions pkg/sql/opt/optbuilder/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,13 @@ func (s *scope) replaceAggregate(f *tree.FuncExpr, def *tree.FunctionDefinition)
fCopy := *f
// Override ordered-set aggregates to use their impl counterparts.
if orderedSetDef, found := isOrderedSetAggregate(def); found {
// Ensure that the aggregation is well formed.
if f.AggType != tree.OrderedSetAgg || len(f.OrderBy) != 1 {
panic(pgerror.Newf(
pgcode.InvalidFunctionDefinition,
"ordered-set aggregations must have a WITHIN GROUP clause containing one ORDER BY column"))
}

// Override function definition.
def = orderedSetDef
fCopy.Func.FunctionReference = orderedSetDef
Expand Down

0 comments on commit 30d24f6

Please sign in to comment.