Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
treat Percentile as an error if we can't use PQL Percentile
Browse files Browse the repository at this point in the history
If we can't successfully generate a PQL Percentile call, error
out rather than implementing an actual Percentile function in SQL.
This can be revisited if anyone needs it.
  • Loading branch information
seebs committed Apr 7, 2023
1 parent c658e77 commit 6383a96
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sql3/planner/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,7 @@ func (p *ExecutionPlanner) compileCallExpr(expr *parser.Call) (_ types.PlanExpre
return agg, nil

case "PERCENTILE":
agg := newPercentilePlanExpression(args[0], args[1], expr.ResultDataType)
agg := newPercentilePlanExpression(expr.Name.NamePos, args[0], args[1], expr.ResultDataType)
return agg, nil

case "CORR":
Expand Down
8 changes: 5 additions & 3 deletions sql3/planner/expressionagg.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,15 +881,17 @@ func (n *maxPlanExpression) WithChildren(children ...types.PlanExpression) (type

// percentilePlanExpression handles PERCENTILE()
type percentilePlanExpression struct {
pos parser.Pos
arg types.PlanExpression
nthArg types.PlanExpression
returnDataType parser.ExprDataType
}

var _ types.Aggregable = (*percentilePlanExpression)(nil)

func newPercentilePlanExpression(arg types.PlanExpression, nthArg types.PlanExpression, returnDataType parser.ExprDataType) *percentilePlanExpression {
func newPercentilePlanExpression(pos parser.Pos, arg types.PlanExpression, nthArg types.PlanExpression, returnDataType parser.ExprDataType) *percentilePlanExpression {
return &percentilePlanExpression{
pos: pos,
arg: arg,
nthArg: nthArg,
returnDataType: returnDataType,
Expand All @@ -905,7 +907,7 @@ func (n *percentilePlanExpression) Evaluate(currentRow []interface{}) (interface
}

func (n *percentilePlanExpression) NewBuffer() (types.AggregationBuffer, error) {
return NewAggCountBuffer(n), nil
return nil, sql3.NewErrUnsupported(n.pos.Line, n.pos.Column, true, "Percentile call that can't be pushed down to PQL")
}

func (n *percentilePlanExpression) FirstChildExpr() types.PlanExpression {
Expand Down Expand Up @@ -941,7 +943,7 @@ func (n *percentilePlanExpression) WithChildren(children ...types.PlanExpression
if len(children) != 2 {
return nil, sql3.NewErrInternalf("unexpected number of children '%d'", len(children))
}
return newPercentilePlanExpression(children[0], children[1], n.returnDataType), nil
return newPercentilePlanExpression(n.pos, children[0], children[1], n.returnDataType), nil
}

// aggregator for CORR()
Expand Down
6 changes: 6 additions & 0 deletions sql3/test/defs/defs_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ var percentileTests = TableTest{
),
ExpErr: "integer, decimal or timestamp expression expected",
},
{
SQLs: sqls(
"SELECT percentile(i1, 50) AS avg_rows FROM percentile_test WHERE s1 != 'a'",
),
ExpErr: "Percentile call that can't be pushed down to PQL is not supported",
},
{
SQLs: sqls(
"SELECT percentile(i1, 50) AS p_rows FROM percentile_test",
Expand Down

0 comments on commit 6383a96

Please sign in to comment.