Skip to content

fix: keep rows from Iceberg data files that store a narrower type than the column - #7712

Open
mixermt wants to merge 6 commits into
apache:mainfrom
mixermt:fix/7138-narrow-file-types
Open

mixermt wants to merge 6 commits into
apache:mainfrom
mixermt:fix/7138-narrow-file-types

Conversation

@mixermt

@mixermt mixermt commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #7138.

Rationale for this change

A data file can store a narrower type than its column: INT in a BIGINT column or FLOAT in a DOUBLE column written before a type promotion, or TINYINT, SMALLINT, INT, or FLOAT in a file added by add_files, migrate, snapshot, or the Java API. At Comet's iceberg-rust pin, a range, IN, or != residual on such a column drops matching rows:

Both fixes landed after iceberg-rust moved to Arrow 60, so the pin cannot take them until #7249 and #7798 merge.

Only the file's footer shows its types. Spark plans the scan without column stats, and neither the schema history nor the name mapping records every such file. #7156 and #7233 guard the cases the metadata records, for a whole table. This PR checks each file instead. It also covers promotions made before Iceberg 0.12 and files appended without a name mapping, and files that store the column's own type keep their pruning.

What changes are included in this PR?

  • iceberg_narrow_types.rs: a task's residual may compare an int, long, or double column with an operator other than IS [NOT] NULL or =. In that case the scan reads the file's Parquet footer once and matches columns as iceberg-rust does: by field id, else through the name mapping. If the file stores such a column with a narrower type, the scan does not push that task's residual, and the filter above the scan still applies it.
  • iceberg-rust's reader reuses the footer bytes the check read, through a FileIO that serves them from memory, so the reader doesn't fetch the footer a second time. The remaining cost is one extra footer decode per checked file.
  • Setting spark.comet.scan.icebergNative.narrowTypeCheck.enabled to false turns the check off. It defaults to true.
  • A new scan metric, narrow_type_splits, documented in the user guide.

The check can be removed once the pin includes both upstream fixes.

Not covered: equality deletes on such a file, because iceberg-rust builds their predicate inside the reader. #7233 falls back to Spark for them.

How are these changes tested?

  • Rust unit tests cover:
    • operator classification, including NOT;
    • column matching by field id, by name mapping, and with neither;
    • footer checks on real Parquet files, including no footer read for a safe residual and one read per file;
    • the reader getting the shared footer bytes, and dropping them once it reads past them.
  • CometIcebergNativeSuite compares range, IN, !=, and NOT (=) predicates with Spark, and asserts narrow_type_splits, in four cases:
    • a promoted table, also with the check turned off;
    • a promoted file whose column spans 6 pages;
    • a file appended with narrower types under field ids and no name mapping;
    • a name-mapped file without field ids.
  • CometIcebergEncryptionSuite runs the same check on an encrypted table.
  • With the check off, each of these Scala tests returns wrong rows.
  • Spark 4.1 on JDK 17: CometIcebergNativeSuite, CometIcebergNativeScanSuite, CometFuzzIcebergSuite, and CometIcebergEncryptionSuite ran with 191 passed and 2 canceled by version gates.
  • Spark 3.5 and 4.0: the four #7138 tests in CometIcebergNativeSuite passed. The Spark 3.5 strict-warnings compile passes.

🤖 Generated with Claude Code

mixermt and others added 2 commits November 20, 2026 21:46
…n the column

A data file can store INT values in a BIGINT column (written before a type
promotion), or TINYINT, SMALLINT or FLOAT values in a file that add_files,
migrate, snapshot or the Java API added. At Comet's iceberg-rust pin, a
range, IN or != residual on such a column drops matching rows: the page
index skips pages whose INT32/FLOAT bounds it cannot compare, and the row
filter casts the literal down to the file's type, so an out-of-range literal
becomes NULL and a double rounds to float.

The JVM cannot see which files are narrower, so the native scan now reads a
data file's Parquet footer when its residual compares an int, long or double
column with such an operator, and replaces those comparisons on a column the
file stores with another type by TRUE. The residual has no NOT, so this only
weakens it, and the filter above the scan still applies the full predicate.
Files that store the table's type keep their pruning. A new metric,
narrow_type_splits, counts the weakened splits.

Closes apache#7138.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Do not push the residual of a file that stores a filtered column with a
  narrower type, instead of rewriting and rebinding it per leaf.
- Reuse METADATA_SIZE_HINT, parquet_support::field_id and the suite's
  test helpers; skip statistics when reading the footer.
- Check footers with buffer_unordered and share one FileIO.
- Document the narrow_type_splits metric and shorten comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tracking this down, and for checking each file instead of guarding the whole table. Matching columns by field id and then through the name mapping, the way iceberg-rust does, looks right to me. The Eq exemption also holds, since rounding a double literal down to float only adds rows that the filter above the scan removes.

My main concern is cost. Any range, IN, != or NOT predicate on an int, long or double column now adds a footer read per data file. That is a very common filter, and it applies to tables that were never promoted too. The reader then fetches the same footer again. The release does not do this, so users upgrading pay for it without a way to opt out. Could you add a supported config to turn the check off until the pin includes iceberg-rust#3507 and #3540? Would it also be possible to hand the footer you already read to the reader, so each file's footer is fetched once?

Two test questions:

  1. The task() helper in iceberg_narrow_types.rs calls rewrite_not() before binding, so the Not arm of visit with negated = true never runs. Can Comet send an un-rewritten Not to the scan? If it can, could you add a test that binds a Not over Eq, In and IsNull?
  2. The Scala tests use files of a few rows, so each file has a single page. Is the page-index skipping from iceberg-rust#3507 covered anywhere? A case with enough rows for several pages that fails with the check off would show that both causes are handled.

I also wondered how this relates to #7156 and #7233, which guard overlapping cases. Is the plan for those to land first, or does this one replace part of them?

mixermt and others added 3 commits November 21, 2026 15:30
…fig to turn the check off

The narrow-type check reads a data file's footer before the reader reads it again. The reader now
gets the bytes the check read, through a FileIO that serves them from memory until each split of
the file has opened it. `spark.comet.scan.icebergNative.narrowTypeCheck.enabled` turns the check
off. A `NOT` now negates the comparisons below it instead of marking them all at risk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mixermt

mixermt commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. I've pushed changes for each point.

Cost

  • Setting spark.comet.scan.icebergNative.narrowTypeCheck.enabled to false turns the check off. It defaults to true.
  • The reader now reuses the footer the check read. The scan gives iceberg-rust a FileIO that serves those bytes from memory until each split of the file has opened it, so the reader doesn't fetch the footer a second time. Both sides use the same 512 KiB metadata size hint, so the reader asks for the same footer bytes the check already has.

What's left is one extra footer decode per checked file. On MinIO with 20 ms added to each request (median of 5 runs, check off → on):

Table Query GET requests Time
400 files of ~60 KB a < 0.001 729 → 365 2475 → 1300 ms
16 files of ~2.4 MB a < 0.001 33 → 33 264 → 251 ms

Small files come out faster than today, because the check's read covers the whole file and the reader serves its data reads from the same bytes. Before footer sharing, the same small-file query was 27% slower with the check on.

1. Not

Comet can't send one: planner.rs calls rewrite_not() before binding. Binding itself keeps Not, though, and the classification test binds !(id = 1) without rewriting, so that arm did run, but only for Eq. It now negates each comparison the way rewrite_not does, so NOT (d IS NULL) counts as safe instead of at risk. I added In, IsNull, and double NOT cases.

2. Page index

It was covered, though not obviously: the page index skips a file's only page too. With the check disabled, the first query to fail in each existing test is id > 0 or id > 1. Those literals fit in INT, so the literal cast is fine and only page skipping can drop the rows. With row selection also disabled, those queries return the right rows. I added a test whose file stores id in 6 pages, with in-range literals only. With the check off, it returns 0 of 499 rows.

#7156 and #7233

This replaces #7233 and the promoted-column rule in #7156. Because it checks each file rather than the whole table, files that store the table's type keep their pruning. It also catches files the metadata doesn't record, such as promotions made before Iceberg 0.12 and files appended without a name mapping. The rest of #7156, the float/NaN and NOT fixes for #7136 and #7137, is independent and still needed. Its timestamp rule is preventive, since Iceberg 1.11 doesn't allow promoting date yet. If #7156 lands first, I can remove its promotion rule in this PR, or #7156 can drop it. Whichever you prefer.

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Everything I asked for in my earlier review is addressed. The config to turn the check off is in as spark.comet.scan.icebergNative.narrowTypeCheck.enabled, the footer is shared with the reader so each file is read once, and the multi-page case is covered. The Not arm is covered through rewrite_not in the planner, which is the only way Comet sends it. I ran the 9 Rust unit tests and the 4 new Scala tests, and they pass. CometIcebergNativeSuite and CometIcebergEncryptionSuite on Spark 4.1.3 had 149 pass, 2 canceled and 0 failed. Returning Ok(false) from the check makes all 4 new Scala tests fail on row mismatch. Making GreaterThan safe fails 4 of the 9 Rust tests, and emptying the name-mapping fallback fails 1. I did not verify the object-store numbers in the description, because I have no object store here.

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @mixermt

@andygrove
andygrove enabled auto-merge November 30, 2026 21:46
@github-actions github-actions Bot added bug Something isn't working area:scan Parquet scan / data reading area:Iceberg labels Nov 30, 2026
@andygrove
andygrove added this pull request to the merge queue Nov 30, 2026
@andygrove

Copy link
Copy Markdown
Member

One note on the overlap. #7233 now also falls back to Spark for an equality delete keyed on a column that may store a narrower type, because iceberg-rust builds that predicate inside the reader. This PR does not cover it. So #7233 does not go away when this lands. It narrows to that fallback. The residual guard there becomes redundant once this merges.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Iceberg area:scan Parquet scan / data reading bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native Iceberg scan drops rows from data files that store a narrower type than the column

2 participants