Skip to content

[doc] Document Spark TopN with NaN file statistics - #11346

Merged
JingsongLi merged 2 commits into
apache:masterfrom
skyyyy955:docs/spark-topn-legacy-nan
Nov 23, 2026
Merged

JingsongLi merged 2 commits into
apache:masterfrom
skyyyy955:docs/spark-topn-legacy-nan

Conversation

@skyyyy955

@skyyyy955 skyyyy955 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Document why descending TopN can miss NaN when a file has a finite recorded maximum. This can affect historical PyPaimon files and new files created by the native batch writer with pypaimon-rust 0.5.0. The guide now adds isnan(score) to the sort order so Spark can choose candidates per partition. Reproduction and discussion

Tests

  • The original Spark 3.5.9 replay returned 10.0 with TopN pushed down and NaN when Spark sorted both files.
  • JingsongLi reproduced the native writer case and verified that the revised ordering returns NaN with TakeOrderedAndProject. The plan had no Paimon TopN pushdown or single-partition exchange gathering all input. Writer check · Plan check
  • Checked the documentation diff and patch application.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guide addresses a real wrong-result case. I built the documentation and reproduced 10.0 with pushed TopN and NaN with the documented hint on an old snapshot. Two separate checks found a current writer configuration that is still affected and a workaround without a single-partition exchange.

Comment thread docs/docs/spark/sql-query.md Outdated
### TopN on Older PyPaimon Snapshots

Spark sorts `NaN` above all numeric values. In Spark 3.5.9, a descending
`ORDER BY ... LIMIT` on a floating-point column can miss `NaN` if an older

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Describe the affected file bounds independently of snapshot age. On current master ae135d59c9, the default NativeBatchTableWrite path with supported pypaimon-rust==0.5.0 still stored a finite maximum of 1.0 for a new Parquet file containing [NaN, 1.0]. With that file and a [10.0] file in separate splits, Spark 3.5.9 returned 10.0 with TopN pushed down. The same current checkout with Python writing and committing forced returned NaN. Please mention the affected native writer configuration as well as historical files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking the current native writer. The updated section describes the finite file maximum as the trigger and calls out the native batch writer case with pypaimon-rust 0.5.0. Documentation CI passed. Update · CI run

Comment thread docs/docs/spark/sql-query.md Outdated

```sql
CREATE OR REPLACE TEMP VIEW scores_for_sort AS
SELECT /*+ REPARTITION(1) */ score

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep the workaround distributed on large snapshots. The documented hint returned NaN in my replay and the physical plan showed Exchange SinglePartition, which sends every candidate row to one task before sorting. I also verified ORDER BY isnan(score) DESC, score DESC LIMIT 1 on the same Spark 3.5.9 table. It returned NaN with TakeOrderedAndProject, no pushed TopN and no single-partition exchange. This ordering preserves Spark's NaN-first descending behavior and gives operators a way to avoid gathering the entire input.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking the physical plan. I replaced the repartition hint with ORDER BY isnan(score) DESC, score DESC LIMIT 1 and added an EXPLAIN EXTENDED check for TakeOrderedAndProject and the absence of TopN: on the Paimon scan. Documentation CI passed.

@skyyyy955 skyyyy955 changed the title [doc] Document Spark TopN on older PyPaimon snapshots [doc] Document Spark TopN with NaN file statistics Nov 23, 2026
@JingsongLi
JingsongLi merged commit 6573422 into apache:master Nov 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants