Weiwei Su created FLINK-41799:
---------------------------------
Summary: Batch SortLimit (ORDER BY DOUBLE ASC LIMIT) with NaN
corrupts heap ordering and silently drops smaller finite rows
Key: FLINK-41799
URL: https://issues.apache.org/jira/browse/FLINK-41799
Project: Flink
Issue Type: Bug
Components: Table SQL / Runtime
Affects Versions: 2.3.0
Environment: Apache Flink 2.3.0 (official Docker image
{{flink:2.3.0-scala_2.12-java17}}), Flink SQL Gateway, batch mode ({{SET
'execution.runtime-mode' = 'batch'}}).
Reporter: Weiwei Su
h3. Problem Description
In Apache Flink 2.3.0 (batch mode), {{ORDER BY <double_col> ASC NULLS LAST
LIMIT k}} compiles to a {{SortLimit}} physical operator. When the input
contains {{CAST('NaN' AS DOUBLE)}}, {{SortLimit}} produces corrupted ordering —
placing {{NaN}} ahead of finite negative values (e.g. {{-1E40}}) — and silently
evicts smaller finite rows from the Top-N result.
Removing the {{LIMIT}} clause (using full {{Sort}}) on the exact same input
returns the expected order with {{NaN}} sorted last, creating a direct
self-contradiction within Flink SQL.
h3. Minimal Reproduction (Pure VALUES, No External Connector)
{code:sql}
SET 'execution.runtime-mode' = 'batch';
-- 1. With LIMIT 4 (executes via SortLimit) -> WRONG RESULT
SELECT id, x
FROM (
VALUES
(1, CAST(-1e40 AS DOUBLE)),
(2, CAST('NaN' AS DOUBLE)),
(3, CAST(-1e40 AS DOUBLE)),
(4, CAST(0.0 AS DOUBLE)),
(5, CAST(0.0 AS DOUBLE)),
(6, CAST(1.0 AS DOUBLE))
) AS t(id, x)
ORDER BY x ASC NULLS LAST, id ASC NULLS LAST
LIMIT 4;
{code}
*Actual result in Flink 2.3.0 ({{SortLimit}}):*
{noformat}
(1, -1.0E40)
(2, NaN)
(3, -1.0E40)
(4, 0.0)
{noformat}
Row {{(2, NaN)}} is placed in position 2 ahead of {{-1.0E40}} and {{0.0}}, and
row {{(5, 0.0)}} is silently dropped from the Top-4.
*Control: Same query WITHOUT {{LIMIT 4}} in Flink 2.3.0 (full {{Sort}}):*
{code:sql}
SELECT id, x
FROM (
VALUES
(1, CAST(-1e40 AS DOUBLE)),
(2, CAST('NaN' AS DOUBLE)),
(3, CAST(-1e40 AS DOUBLE)),
(4, CAST(0.0 AS DOUBLE)),
(5, CAST(0.0 AS DOUBLE)),
(6, CAST(1.0 AS DOUBLE))
) AS t(id, x)
ORDER BY x ASC NULLS LAST, id ASC NULLS LAST;
{code}
*Actual result in Flink 2.3.0 (without {{LIMIT}}):*
{noformat}
(1, -1.0E40)
(3, -1.0E40)
(4, 0.0)
(5, 0.0)
(6, 1.0)
(2, NaN)
{noformat}
The first 4 rows are {{(1, -1.0E40), (3, -1.0E40), (4, 0.0), (5, 0.0)}},
matching Apache Spark 4.1.2 and DuckDB 2.0.1.
h3. Execution Plan ({{EXPLAIN PLAN FOR}})
{noformat}
SortLimit(orderBy=[EXPR$1 ASC, EXPR$0 ASC], offset=[0], fetch=[4],
global=[true])
+- Exchange(distribution=[single])
+- SortLimit(orderBy=[EXPR$1 ASC, EXPR$0 ASC], offset=[0], fetch=[4],
global=[false])
+- Union(all=[true], union=[EXPR$0, EXPR$1])
:- Calc(select=[1 AS EXPR$0, -1E40 AS EXPR$1])
: +- Values(tuples=[[{ 0 }]], values=[ZERO])(reuse_id=[1])
:- Calc(select=[2 AS EXPR$0, CAST('NaN' AS DOUBLE) AS EXPR$1])
: +- Reused(reference_id=[1])
:- Calc(select=[3 AS EXPR$0, -1E40 AS EXPR$1])
: +- Reused(reference_id=[1])
:- Calc(select=[4 AS EXPR$0, 0.0 AS EXPR$1])
: +- Reused(reference_id=[1])
:- Calc(select=[5 AS EXPR$0, 0.0 AS EXPR$1])
: +- Reused(reference_id=[1])
+- Calc(select=[6 AS EXPR$0, 1.0 AS EXPR$1])
+- Reused(reference_id=[1])
{noformat}
h3. Expected Behavior
{{SortLimit}} should enforce a consistent total order for floating-point
{{NaN}} (matching full {{Sort}}, where {{NaN}} sorts after all finite values)
so that Top-N results match the prefix of the full sorted result and never
evict smaller finite rows or depend on input arrival order.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)