Analytic functions AVG, REGR_AVGX and REGR_AVGY can return wrong results for decimal parameters with scale greater than 18

Details

Detail name Value
Changelog number 33398
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

AVG([DISTINCT] expr) can return wrong results for a DECIMAL parameter with scale greater than 18 when the partition contains at least two rows, the evaluated window contains at least one non-NULL input value and at least one of the following conditions applies:

  • DISTINCT is specified.
  • The window frame is not the full partition. Note: ORDER BY without an explicit frame sets RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
  • A window exclusion (e.g. EXCLUDE CURRENT ROW) changes which rows contribute.

REGR_AVGX(y,x) and REGR_AVGY(y,x) have similar conditions with small adjustments:

  • Both parameters are of type DECIMAL and at least one of them has scale greater than 18.
  • The partition contains at least two rows and the evaluated window contains at least one row where both parameters are non-NULL.
  • The window frame is not the full partition, or a window exclusion changes which rows contribute (there is no DISTINCT option).

Examples

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

-- Expected: two rows, each | 0.5 |
-- Observed: wrong results
SELECT AVG(DISTINCT x) OVER () AS af
FROM input_values;

-- Expected: | 0.25 | and | 0.5 |
-- Observed: wrong results
SELECT AVG(x) OVER (ORDER BY order_key) AS af
FROM input_values;

-- Expected: | 0.75 | and | 0.25 |
-- Observed: wrong results
SELECT AVG(x) OVER (
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    EXCLUDE CURRENT ROW) AS af
FROM input_values;

-- Expected: | 0.25 | and | 0.5 |
-- Observed: wrong results
SELECT REGR_AVGX(1, x) OVER (ORDER BY order_key) AS af
FROM input_values;

-- Expected: | 0.25 | and | 0.5 |
-- Observed: wrong results
SELECT REGR_AVGY(x, 1) OVER (ORDER BY order_key) 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 AVG(DISTINCT CAST(x AS DOUBLE)) OVER () AS af
FROM input_values;

-- Expected/Observed: | 0.25 | and | 0.5 |
SELECT AVG(CAST(x AS DOUBLE)) OVER (ORDER BY order_key) AS af
FROM input_values;

-- Expected/Observed: | 0.75 | and | 0.25 |
SELECT AVG(CAST(x AS DOUBLE)) OVER (
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    EXCLUDE CURRENT ROW) AS af
FROM input_values;

-- Expected/Observed: | 0.25 | and | 0.5 |
SELECT REGR_AVGX(1, CAST(x AS DOUBLE)) OVER (ORDER BY order_key) AS af
FROM input_values;

-- Expected/Observed: | 0.25 | and | 0.5 |
SELECT REGR_AVGY(CAST(x AS DOUBLE), 1) OVER (ORDER BY order_key) AS af
FROM input_values;

Fix

Queries run as expected.