mbutrovich commented on code in PR #3704:
URL: https://github.com/apache/iceberg-rust/pull/3704#discussion_r4786894838
##########
crates/iceberg/src/scan/task.rs:
##########
@@ -257,6 +263,17 @@ impl FileScanTask {
self.sort_order_id
}
+ /// The data file's split offsets, see
[`DataFile::split_offsets()`](crate::spec::DataFile::split_offsets).
+ ///
+ /// A task planned by [`TableScan`](crate::scan::TableScan) carries them
only when they
+ /// are well defined, as Java's `BaseFile.splitOffsets()` decides it:
`Some` is then
+ /// non-empty and its last offset is below [`Self::file_size_in_bytes`]. A
list a writer
+ /// recorded that fails that check is dropped, so an engine cutting a file
at these
+ /// offsets never cuts past its end.
+ pub fn split_offsets(&self) -> Option<&[i64]> {
+ self.split_offsets.as_deref()
+ }
Review Comment:
The doc promises the well-defined property only for tasks planned by
`TableScan`. A task from the builder or from deserialization can carry any
list. The serde round-trip test in this PR builds one: a 123-byte file ([line
3042](https://github.com/apache/iceberg-rust/blob/61f398c5caeb39235c757ef915ace4f899ed0fb8/crates/iceberg/src/scan/mod.rs#L3042))
with offsets `[4, 1_048_580]` ([line
3072](https://github.com/apache/iceberg-rust/blob/61f398c5caeb39235c757ef915ace4f899ed0fb8/crates/iceberg/src/scan/mod.rs#L3072)).
An engine that receives serialized tasks from another planner then has to
re-check the list itself.
Should `FileScanTask::validate()` reject a `Some` list that fails
`split_offsets_are_well_defined`? `build()` and deserialization already run it,
so every task would hold the property and this doc could state it without the
`TableScan` qualifier. Planning still filters before `build()`, so a bad
manifest drops the offsets instead of failing. No existing caller sets this
field, so the new error can't break anyone. The serde test would need offsets
below its file size.
##########
crates/iceberg/src/scan/task.rs:
##########
@@ -140,6 +140,12 @@ pub struct FileScanTask {
#[builder(default, setter(transform = |stats:
Option<FileScanTaskColumnStats>| stats.map(Box::new)))]
column_stats: Option<Box<FileScanTaskColumnStats>>,
+ /// Split offsets recorded on the data file (spec field 132): the byte
offsets at which
+ /// the file can be split for parallel reading, in Parquet the start of
each row group.
+ /// An engine that splits large files into several tasks uses these so a
split boundary
+ /// never falls inside a row group. `None` when the writer did not record
them.
+ #[builder(default)]
+ split_offsets: Option<Vec<i64>>,
Review Comment:
Could this be `Option<Vec<u64>>`? `start`, `length`, and
`file_size_in_bytes` on this struct are all `u64`, and an engine computing
splits compares offsets against them, so `i64` means a cast at every
comparison. `DataFile::split_offsets` is `i64`, but planning is the one place
the value enters `FileScanTask`, so converting there keeps the rest of the code
free of sign checks. This is new public API (the field, `with_split_offsets`,
and `split_offsets()`), so the type is fixed once it ships.
##########
crates/iceberg/src/scan/mod.rs:
##########
@@ -1561,6 +1561,76 @@ mod tests {
}
}
+ #[tokio::test]
+ async fn
test_plan_files_carries_well_defined_split_offsets_from_the_manifest_entry() {
+ let mut fixture = TableTestFixture::new();
+
+ // Java's `BaseFile.splitOffsets()` hands offsets on only when the
list is non-empty
+ // and its last offset is below the file size; the task carries the
same set. The
+ // cases: the usual recording (one offset per row group), the last
offset just
+ // inside the file, an empty list, a file whose writer did not record
them, the last
+ // offset at the file size, and one past it.
+ fixture
+ .setup_manifest_files_with_split_offsets(|file_size| {
+ let file_size = i64::try_from(file_size).unwrap();
+ vec![
+ Some(vec![4, 100, 200]),
+ Some(vec![4, file_size - 1]),
+ Some(vec![]),
+ None,
+ Some(vec![4, file_size]),
+ Some(vec![4, file_size + 1]),
+ ]
+ })
+ .await;
+
+ let mut tasks: Vec<_> = fixture
+ .table
+ .scan()
+ .build()
+ .unwrap()
+ .plan_files()
+ .await
+ .unwrap()
+ .try_collect()
+ .await
+ .unwrap();
+ tasks.sort_by(|a, b| a.data_file_path().cmp(b.data_file_path()));
+ assert_eq!(tasks.len(), 6);
+ let file_size = i64::try_from(tasks[0].file_size_in_bytes()).unwrap();
+
+ assert_eq!(
+ tasks[0].split_offsets(),
+ Some(&[4, 100, 200][..]),
+ "offsets recorded on the data file reach the task unchanged"
+ );
+ assert_eq!(
+ tasks[1].split_offsets(),
+ Some(&[4, file_size - 1][..]),
+ "a last offset just below the file size is well defined"
+ );
+ assert_eq!(
+ tasks[2].split_offsets(),
+ None,
+ "an empty list carries nothing to split on, so it is None, as in
Java"
+ );
+ assert_eq!(
+ tasks[3].split_offsets(),
+ None,
+ "a file without recorded offsets carries None"
+ );
+ assert_eq!(
+ tasks[4].split_offsets(),
+ None,
+ "a last offset at the file size is not below it and is dropped"
+ );
+ assert_eq!(
+ tasks[5].split_offsets(),
+ None,
+ "a last offset past the file size is dropped, so no split cuts
past the end"
+ );
+ }
Review Comment:
Could we build the expected list and compare it in one `assert_eq!`, without
the comment block and the per-assert messages? That's the convention in
`AGENTS.md`, and it makes room for the cases from my `context.rs` comment: an
unsorted list, a repeated offset, and a negative offset.
```rust
#[tokio::test]
async fn
test_plan_files_carries_well_defined_split_offsets_from_the_manifest_entry() {
let mut fixture = TableTestFixture::new();
fixture
.setup_manifest_files_with_split_offsets(|file_size| {
let file_size = i64::try_from(file_size).unwrap();
vec![
Some(vec![4, 100, 200]),
Some(vec![4, file_size - 1]),
Some(vec![]),
None,
Some(vec![4, file_size]),
Some(vec![4, file_size + 1]),
Some(vec![4, 200, 100]),
Some(vec![4, 4]),
Some(vec![-1, 4]),
]
})
.await;
let mut tasks: Vec<_> = fixture
.table
.scan()
.build()
.unwrap()
.plan_files()
.await
.unwrap()
.try_collect()
.await
.unwrap();
tasks.sort_by(|a, b| a.data_file_path().cmp(b.data_file_path()));
let file_size = tasks[0].file_size_in_bytes();
let split_offsets: Vec<_> = tasks
.iter()
.map(|task| task.split_offsets().map(<[u64]>::to_vec))
.collect();
assert_eq!(split_offsets, vec![
Some(vec![4, 100, 200]),
Some(vec![4, file_size - 1]),
None,
None,
None,
None,
None,
None,
None,
]);
}
```
##########
crates/iceberg/src/scan/context.rs:
##########
@@ -249,6 +250,19 @@ impl ManifestEntryContext {
}
}
+/// The data file's split offsets when they are well defined, as Java's
+/// `BaseFile.splitOffsets()` decides it: non-empty, with the last offset
below the
+/// file size. Some writers recorded offsets that do not belong to the file,
and an
+/// engine splitting on those would cut past its end, so such a list is dropped
+/// rather than handed to the task.
+fn well_defined_split_offsets(data_file: &DataFile) -> Option<Vec<i64>> {
+ let offsets = data_file.split_offsets()?;
+ let last = *offsets.last()?;
+ u64::try_from(last)
+ .is_ok_and(|last| last < data_file.file_size_in_bytes())
+ .then(|| offsets.to_vec())
+}
Review Comment:
Java checks one more thing before it splits on these offsets.
[`BaseContentScanTask.split`](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/core/src/main/java/org/apache/iceberg/BaseContentScanTask.java#L100-L117)
uses them only when
[`ArrayUtil.isStrictlyAscending`](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/core/src/main/java/org/apache/iceberg/util/ArrayUtil.java#L323-L331)
holds, and otherwise falls back to fixed-size splits. The spec also says the
list ["must be sorted
ascending"](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/format/spec.md?plain=1#L917).
Here only the last offset is checked, so `[4, 200, 100]` or `[-1, 4]` in a
300-byte file reaches the task, and an engine splitting on it gets ranges Java
would never produce.
Should we also require strictly ascending, non-negative offsets? Converting
to `u64` (see my comment on the field) handles the negative case on the way in:
```rust
fn well_defined_split_offsets(data_file: &DataFile) -> Option<Vec<u64>> {
let offsets: Vec<u64> = data_file
.split_offsets()?
.iter()
.map(|&offset| u64::try_from(offset).ok())
.collect::<Option<_>>()?;
split_offsets_are_well_defined(&offsets,
data_file.file_size_in_bytes()).then_some(offsets)
}
```
with the predicate next to `FileScanTask` so `validate()` can share it:
```rust
/// Whether `offsets` can split a file of `file_size_in_bytes` bytes:
non-empty, strictly
/// ascending, and below the file size.
pub(crate) fn split_offsets_are_well_defined(offsets: &[u64],
file_size_in_bytes: u64) -> bool {
offsets.last().is_some_and(|&last| last < file_size_in_bytes)
&& offsets.windows(2).all(|pair| pair[0] < pair[1])
}
```
Dropping the list keeps a bad manifest from failing the scan, which matches
how Java treats offsets it can't use.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]