GROUP CUSTOM ... WITHIN ORDER fails in PostgreSQL: the #1700 tiebreak is appended to WITHIN GROUP (ORDER BY ...)

Open
#1,877 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
java, postgresql
Domain
backend, databases

Research direction

Start by reading the OrderGroupProperty change from #1700 and trace how custom aggregates generate ordered calls. Check the PostgreSQL output for the documented percentile_cont example and the BY form. Done when WITHIN GROUP (ORDER BY ...) receives only the specified order expressions and both examples work.

Written by the indexing model from the issue text.

Description

bug

Problem

GROUP CUSTOM ... WITHIN ORDER fails in PostgreSQL on the first read. The example from the GROUP operator documentation:

value = DATA INTEGER (INTEGER);
percentile90() = GROUP CUSTOM NUMERIC[14,2] 'percentile_cont' 0.9 WITHIN ORDER value(INTEGER i);
ERROR: function percentile_cont(numeric, integer, integer) does not exist

The same with BY: GROUP CUSTOM DOUBLE 'percentile_cont' 0.5 WITHIN ORDER sdouble(Store s) BY storeSize(s) gives percentile_cont(numeric, double precision, bigint) does not exist.

Checked on master.

Cause

Since #1700 (c368fb1813) OrderGroupProperty totalizes the order with the inner interfaces when groupType.needsOrderTiebreak(), and for a custom aggregate it returns setOrdered — that is, true exactly for the WITHIN form. The generated call becomes percentile_cont(0.9) WITHIN GROUP (ORDER BY value, i), while ordered-set aggregates (percentile_cont, percentile_disc, ...) take a fixed number of ordered arguments, so the extra column makes the call unresolvable.

The condition looks inverted: for the WITHIN form the tiebreak cannot be added to the call at all, and the result of percentile_cont / percentile_disc does not depend on the order of rows with equal values. In the form without WITHIN (aggrFunc(expr ORDER BY ...), for example string_agg) the order is passed inside the call and does affect the result, and appending the tiebreak there is valid SQL.

Expected

GROUP CUSTOM ... WITHIN ORDER passes only the ORDER expressions to WITHIN GROUP (ORDER BY ...), and the documentation example works.

Dominant language
Java
Stars
214
Forks
46
PR merge metrics
No merged PRs in 30d

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lsfusion/platform

All issues in lsfusion/platform

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.