Weiwei Su created FLINK-41807:
---------------------------------
Summary: MIN(DOUBLE) and MAX(DOUBLE) return order-dependent wrong
results when input contains NaN
Key: FLINK-41807
URL: https://issues.apache.org/jira/browse/FLINK-41807
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner, Table SQL / Runtime
Affects Versions: 2.3.0
Environment: Apache Flink: 2.3.0 (SQL Gateway, Batch execution mode)
Reporter: Weiwei Su
h3. Summary
In Flink SQL (Batch mode, tested on Flink 2.3.0), {{MIN(DOUBLE)}} and
{{MAX(DOUBLE)}} produce non-deterministic, input-order-dependent results when
the input contains {{Double.NaN}}:
* If {{NaN}} is the *first* non-null row in the input, *both* {{MIN(x)}} and
{{MAX(x)}} evaluate to {{NaN}} (discarding all subsequent finite values such as
{{-10.0}} and {{5.0}}).
* If any finite value arrives *before* {{NaN}}, {{NaN}} is completely ignored
and {{MIN(x)}} / {{MAX(x)}} return {{-10.0}} / {{5.0}}.
This reproduces across all aggregation phase strategies ({{AUTO}},
{{ONE_PHASE}}, and {{TWO_PHASE}}). In real table scans (e.g., Apache Iceberg
tables), simply toggling {{table.optimizer.source.predicate-pushdown-enabled}}
changes split/row arrival order and silently flips {{MIN(x)}} / {{MAX(x)}}
between finite bounds and {{(NaN, NaN)}} on the exact same dataset.
h3. Minimal Reproduction (Pure SQL, No External Tables)
*Case 1: {{NaN}} arrives first*
{code:sql}
SELECT
MIN(x) AS mn,
MAX(x) AS mx,
COUNT(x) AS cnt
FROM (
VALUES
(CAST('NaN' AS DOUBLE)),
(CAST(-10.0 AS DOUBLE)),
(CAST(5.0 AS DOUBLE))
) AS t(x);
{code}
Actual result in Flink 2.3.0 ({{AUTO}} / {{ONE_PHASE}} / {{TWO_PHASE}}):
{noformat}
mn | mx | cnt
----+-----+----
NaN | NaN | 3
{noformat}
*Case 2: Same multiset, {{NaN}} arrives second or last*
{code:sql}
SELECT
MIN(x) AS mn,
MAX(x) AS mx,
COUNT(x) AS cnt
FROM (
VALUES
(CAST(-10.0 AS DOUBLE)),
(CAST(5.0 AS DOUBLE)),
(CAST('NaN' AS DOUBLE))
) AS t(x);
{code}
Actual result in Flink 2.3.0 ({{AUTO}} / {{ONE_PHASE}} / {{TWO_PHASE}}):
{noformat}
mn | mx | cnt
------+-----+----
-10.0 | 5.0 | 3
{noformat}
h3. Cross-Engine Comparison (Same Two Queries)
All other tested engines are strictly order-independent:
* *Spark 4.1.2*: {{[-10.0, NaN, 3]}} for both Case 1 and Case 2
* *DuckDB 2.0.1*: {{[-10.0, NaN, 3]}} for both Case 1 and Case 2
* *Trino 484*: {{[-10.0, 5.0, 3]}} for both Case 1 and Case 2
h3. Expected Behavior
{{MIN(DOUBLE)}} and {{MAX(DOUBLE)}} must be associative, commutative, and
independent of input row order. Returning {{MIN = NaN}} while {{-10.0}} and
{{5.0}} are present (when {{NaN}} happens to be the first row) violates
aggregate determinism and SQL semantics.
h3. Suspected Root Cause
In IEEE-754 floating-point arithmetic, both {{val < NaN}} and {{val > NaN}}
evaluate to {{false}}. If the generated {{MIN}}/{{MAX}} aggregate accumulator
uses raw Java {{<}} / {{>}} comparisons without {{Double.compare}} or explicit
{{Double.isNaN}} handling:
# When the first row is {{NaN}}, the accumulator is initialized to {{NaN}}, and
every subsequent comparison {{val < NaN}} / {{val > NaN}} is {{false}},
permanently locking both {{MIN}} and {{MAX}} to {{NaN}}.
# When a finite value arrives first, {{NaN < acc}} / {{NaN > acc}} is
{{false}}, so {{NaN}} is silently ignored.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)