Skip to content

[core][datafusion] Prune and coalesce sparse ROW reads - #1555

Merged
JingsongLi merged 8 commits into
apache:mainfrom
JingsongLi:codex/optimize-row-id-reads
Nov 28, 2026
Merged

JingsongLi merged 8 commits into
apache:mainfrom
JingsongLi:codex/optimize-row-id-reads

Conversation

@JingsongLi

@JingsongLi JingsongLi commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Closes #1554.

Sparse Data Evolution SQL lookups can read every ROW file because the DataFusion predicate translator only sees the physical fields. A lookup that crosses a ROW block boundary also issues separate data reads for adjacent blocks.

Brief change log

  • Use the SQL read fields for filters on _ROW_ID alone so the planner can prune by row ID. Extract inclusive BETWEEN ranges and compact consecutive IN values into ranges.
  • Coalesce short runs of adjacent selected ROW blocks, with each merged request capped at 1 MiB of compressed data. Decompress one block at a time and preserve its row selection and order.
  • Count actual blocks toward the eight-block read-ahead window. Read scans without a row selection and long selected runs one block at a time so that early termination retains the original read-ahead depth.
  • Scale the cold ROW metadata tail read from 4 KiB to 64 KiB with file size. This loads the footer and index together when they fit. Fetch larger indexes from their indexed byte range.

Tests

  • SQL regression for BETWEEN and IN checks that the plan prunes an unselected file and returns the expected values. It reproduced the extra file on the base revision.
  • Core tests cover range boundaries, adjacent block reads, skipped blocks, request limits, and the large-index fallback.
  • The ROW reader regression stops after one row and checks the requests for full scans, a one-row deletion, a wide row range, and multiple sparse pairs in the same file.
  • Core and DataFusion unit suites, ROW sidecar, parallel reads, preplanned row IDs, ROW variant, scan-pruning statistics, SQL reads, and MERGE INTO integration suites passed locally. DataFusion used the CI test stack size. Formatting and focused Clippy checks passed.

On a local OpenDAL counting fixture with 32 files, 200 Float32 fields, and sixteen 80-row lookups, both SQL forms returned the same 1,280 rows. The original SQL scan issued 800 data range reads. The updated reader issued 16, totaling 0.93 MB for block-aligned lookups. Moving each range one row forward crossed two blocks per file and took 16 data reads, totaling 1.86 MB. Placing eight of the disjoint ranges in each of two files also took 16 data reads. For small indexes, cold metadata took one range read per file.

On the same fixture, LIMIT 1 now requests 464,568 data bytes in eight single-block reads with a 58,030-byte first request. Before the read-ahead fixes, an unfiltered scan and a scan with one row deleted per file each requested 1,451,815 bytes. A scan with "_ROW_ID" BETWEEN 1 AND 1900 requested 1,393,731 bytes. All three cases previously started with a 1,045,238-byte request.

A synthetic ROW file of about 256 MiB with the same schema had 18.4 KiB of footer and index bytes. The 64 KiB tail covers both in one read. The roughly 1.4 MiB files from the range-lookup fixture still use 4 KiB.

API and Format

The changes use the existing SQL and table read APIs and accept existing Java-compatible ROW files.

Documentation

The existing Data Evolution SQL predicate syntax applies.

@JingsongLi
JingsongLi marked this pull request as draft November 27, 2026 14:03
@JingsongLi
JingsongLi marked this pull request as ready for review November 28, 2026 02:46

@leaves12138 leaves12138 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed base main at head f700edc. Full file list: crates/integrations/datafusion/src/table/mod.rs, crates/integrations/datafusion/tests/scan_pruning_stats.rs, crates/paimon/src/arrow/format/row.rs, crates/paimon/src/table/row_id_predicate.rs.

Core change notice: this touches generic core read paths, not just the SQL layer: the ROW format reader (block coalescing, read-ahead accounting, metadata tail prefetch) and the shared _ROW_ID predicate-to-range extraction (BETWEEN, IN compaction). All core/C/Python/DataFusion readers of ROW files and row-ID range planning are affected.

Analysis:

  • Predicate extraction: Between maps to one inclusive range with the low bound clamped at 0 and inverted bounds yielding an empty range (no rows), which is correct since row IDs are non-negative; In now goes through merge_row_ranges, which sorts and merges overlapping and adjacent singletons, preserving the exact value set. NotBetween/NotIn are not convertible in leaf_to_ranges, so negated forms keep the residual filter and never over-prune. The AND-intersect / OR-union semantics of the extractor are unchanged.
  • DataFusion gating: the read-fields schema (with the synthesized _ROW_ID) is only used for filter analysis when data evolution is enabled and every column reference in every filter is _ROW_ID; mixed predicates keep the physical-field path, and supports_filters_pushdown uses the same read fields so classification and scan agree.
  • Block coalescing: only adjacent runs in blocks_to_read (already ascending and gap-free by construction) are merged, capped at 1 MiB compressed per request and at runs no longer than the read-ahead window; a block larger than the cap still reads alone. read_row_range validates the returned length, and the per-block split uses index compressed sizes, so decompression boundaries cannot drift. Per-block selection and order are computed exactly as before.
  • Read-ahead accounting: FuturesOrdered preserves block order; a merged request consumes one slot per block and slots are only refilled after its blocks are yielded, so an early-stopping consumer stays within the eight-block window. The no-selection and long-run cases read one block at a time, matching the old prefetch depth. The new regression asserts requested ranges are a prefix of the expected reads for four stop-early scenarios.
  • Metadata tail: the tail size scales with file size between 4 KiB and 64 KiB, the footer is sliced from the tail end, and the index is served from the tail when contained or fetched by its exact validated range otherwise. Index/footer cross-validation (validate_for_file) is unchanged, and the cache-hit read-count tests were updated consistently (3 to 2 cold reads).

No findings. Behavior outside the new fast paths is preserved, and the test coverage is strong: SQL regression reproducing the extra-file scan on the base revision, range boundary cases including i64::MAX, request-limit splitting, adjacent/skipped block reads, early-stop request prefixes, and the large-index fallback.

CI note: most jobs are still pending on this head (1 pass, 14 pending); the author reports the core/DataFusion suites, SQL reads, and MERGE INTO integration passing locally with fmt and focused Clippy clean. Approving without waiting for CI to finish.

LGTM.

@JingsongLi
JingsongLi merged commit a79c9c9 into apache:main Nov 28, 2026
15 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.

[Bug] DataFusion row ID filters scan unselected ROW blocks

2 participants