Skip to content

Ensure that iceberg partition values agree between iceberg metadata and sink partitioning - #1237

Open
mwylde wants to merge 1 commit into
masterfrom
fix_iceberg_partitioning
Open

mwylde wants to merge 1 commit into
masterfrom
fix_iceberg_partitioning

Conversation

@mwylde

@mwylde mwylde commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

We compute iceberg partitions in two places today. The first, to decide which events go to which files, using our own arrow kernels. The second for computing partition metadata that's included in the iceberg files transforms parquet metadata values (from stats) into iceberg partitions.

Unfortunately, there are some cases today where these two methods disagree and produce different partition values, which can cause data to be missed when queried.

This PR includes a comprehensive test for equivalence and fixes various cases where they did not match or align with the iceberg spec or outright failed in computation.


Devin Review

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 4 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +35 to +38
let array = cast(&array, &iceberg_arrow_type(array.data_type()))?;
create_transform_function(&transform.into())
.and_then(|f| f.transform(array))
.map_err(|e| ArrowError::ComputeError(e.to_string()))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Wrong day before pre-epoch midnight

transform_arrow puts most of the final second before a pre-epoch midnight into the following day. The manifest uses the same wrong day, so readers can skip those rows.

Learn more

The sink groups rows by the day transform in partition_expr. At commit it applies the literal day transform to the minimum timestamp in each file in build_datafile_from_meta. In iceberg-rust 0.10.1, both paths call day_timestamp_micro, which calculates whole seconds from the original timestamp, but derives nanoseconds from the timestamp plus one microsecond for negative values. Combining those results can move a timestamp across midnight before computing the day.

Example: 1969-12-30 23:59:59.500000 yields day -1 instead of -2. With 23:59:58 in the same batch, the old grouping put both in one file whose minimum yielded -2. The new grouping separates the rows and labels the later one -1, so an Iceberg reader that filters for December 30 can prune its file.

Recommended fix: Use floor division by microseconds per day in both paths, or upgrade to a verified dependency fix. Assert the expected day for subsecond samples on both sides of a pre-epoch midnight rather than comparing only the two implementations.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +35 to +38
let array = cast(&array, &iceberg_arrow_type(array.data_type()))?;
create_transform_function(&transform.into())
.and_then(|f| f.transform(array))
.map_err(|e| ArrowError::ComputeError(e.to_string()))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Wrong buckets for pre-epoch nanoseconds

transform_arrow rounds pre-epoch nanosecond timestamps toward zero before hashing, rather than down to the previous microsecond. The manifest repeats that calculation, so readers using floor division can skip files assigned to the wrong bucket.

Learn more

Plain SQL TIMESTAMP maps to a nanosecond Arrow timestamp in convert_simple_data_type. partition_expr passes it to the bucket UDF. The new array path and the literal path in build_datafile_from_meta both use iceberg-rust 0.10.1, which divides nanoseconds by 1,000 toward zero. Apache Iceberg's Java bucket transform uses Math.floorDiv for that conversion, so the bucket stored by Arroyo can disagree with the bucket a reader computes for a query predicate.

Example: In a v3 Iceberg table with bucket(ts, 16), a timestamp one nanosecond before the epoch hashes as microsecond 0 and lands in bucket 12. Apache Iceberg hashes microsecond -1 and looks in bucket 8.

Recommended fix: Normalize nanosecond timestamps with floor division before passing them to the microsecond bucket transform on both paths, or adopt a verified dependency fix. Test against a known bucket from an independent Iceberg implementation.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +160 to +162
if transform == iceberg::spec::Transform::Void {
return Ok(None);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Void partitions on structs crash commits

build_datafile_from_meta now passes a null partition for void on a struct column. Iceberg's manifest writer still treats the partition type as primitive and panics before publishing the file.

Learn more

The SQL partition parser allows void, and as_partition_spec delegates validation to iceberg-rust. Iceberg-rust 0.10.1 exempts Void from the primitive source restriction and uses the source type as the partition type. partition_expr produces a null key, and commit now gets a DataFile instead of stopping on missing bounds for the struct. Iceberg-rust's construct_partition_summaries unconditionally unwraps as_primitive_type() for each field before looking at any record.

Example: Define address STRUCT<city TEXT> and partition by void(address). The first checkpoint with data reaches manifest construction and panics even though the partition value is null.

Recommended fix: Reject Void on nonprimitive source columns during sink validation until manifest writing supports them. Keep the null shortcut for primitive source columns and add a regression that exercises manifest construction or rejects the spec before startup.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

match iceberg_arrow_type(lhs) {
t @ (DataType::Int32
| DataType::Int64
| DataType::Decimal128(_, _)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Decimal truncation can crash commits

ice_truncate can round a valid negative decimal beyond the byte width of its partition type. The manifest encoder unwraps the failed conversion and panics.

Learn more

The Iceberg sink accepts positive truncate widths in partition_expr. Iceberg-rust rounds the unscaled decimal down to a multiple of the width, and build_datafile_from_meta stores that result as a partition literal. The manifest schema uses fixed-width Avro decimals sized to the source precision. Apache Avro 0.21.0 unwraps the fixed-width sign-extension operation, which fails when the integer requires more bytes.

Example: A DECIMAL(2,2) column partitioned by truncate(amount, 200) accepts -0.01. The truncated value is -2.00, whose unscaled integer -200 needs two signed bytes, but the partition field has room for one.

Recommended fix: Validate decimal truncate widths against the source precision during planning, or check the transformed value before passing it to manifest serialization and return a descriptive error. Test through manifest encoding, not only DataFile construction.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch has not been deployed

No deployments
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.

1 participant