Analytic functions PERCENTILE_CONT, PERCENTILE_DISC and MEDIAN can return wrong results for decimal parameters with scale greater than 18

Details

Detail name Value
Changelog number 33129
Type Bug
Status Resolved
Affected versions Exasol 7.1.0, Exasol 8.0.0, Exasol 8.29.0, Exasol 2025.1.0, Exasol 2025.2.0, Exasol 2026.1.0
Fix versions Exasol 2025.1.17
Resolution Date 2026-11-12

Description

PERCENTILE_CONT, PERCENTILE_DISC and MEDIAN can return wrong results or throw unexpected exceptions when the partition contains at least two rows and at least one non-NULL ordered value, and at least one of the following conditions applies:

  • PERCENTILE_CONT: the ordered expression is DECIMAL with scale > 18.
  • PERCENTILE_CONT: the percentile parameter is DECIMAL with scale > 18, and the partition contains at least two non-NULL ordered values.
  • PERCENTILE_DISC: the percentile parameter is DECIMAL with scale > 18.
  • MEDIAN: the input expression is DECIMAL with scale > 18, and PARTITION BY is specified.

For PERCENTILE_CONT/PERCENTILE_DISC, the percentile parameter can be a literal or a column included in PARTITION BY.

Examples

CREATE TABLE input_values(x DECIMAL(20,20), order_key INT);
INSERT INTO input_values VALUES (0.25, 0), (0.75, 2);

-- Expected: two rows, each | 0.5 |
-- Observed: wrong results
SELECT PERCENTILE_CONT(0.5)
       WITHIN GROUP (ORDER BY x) OVER () AS af
FROM input_values;

-- Expected: two rows, each | 1 |
-- Observed: "data exception - PERCENTILE_CONT parameter cannot be NULL and must be between 0 and 1"
SELECT PERCENTILE_CONT(CAST(0.5 AS DECIMAL(20,20)))
       WITHIN GROUP (ORDER BY order_key) OVER () AS af
FROM input_values;

-- Expected: two rows, each | 0 |
-- Observed: "data exception - PERCENTILE_DISC parameter cannot be NULL and must be between 0 and 1"
SELECT PERCENTILE_DISC(CAST(0.5 AS DECIMAL(20,20)))
       WITHIN GROUP (ORDER BY order_key) OVER () AS af
FROM input_values;

-- Expected: two rows, each | 0.5 |
-- Observed: wrong results
SELECT MEDIAN(x) OVER (PARTITION BY 1) AS af
FROM input_values;

Workaround

Cast DECIMAL parameters with a scale greater than 18 to DOUBLE or to DECIMAL with a scale of 18 or less.

-- Expected/Observed: two rows, each | 0.5 |
SELECT PERCENTILE_CONT(0.5)
       WITHIN GROUP (ORDER BY CAST(x AS DOUBLE)) OVER () AS af
FROM input_values;

-- Expected/Observed: two rows, each | 1 |
SELECT PERCENTILE_CONT(CAST(0.5 AS DOUBLE))
       WITHIN GROUP (ORDER BY order_key) OVER () AS af
FROM input_values;

-- Expected/Observed: two rows, each | 0 |
SELECT PERCENTILE_DISC(CAST(0.5 AS DOUBLE))
       WITHIN GROUP (ORDER BY order_key) OVER () AS af
FROM input_values;

-- Expected/Observed: two rows, each | 0.5 |
SELECT MEDIAN(CAST(x AS DOUBLE)) OVER (PARTITION BY 1) AS af
FROM input_values;

Fix

Queries run as expected.