hcrosse commented on code in PR #3623:
URL: https://github.com/apache/iceberg-rust/pull/3623#discussion_r4589999446
##########
crates/iceberg/src/arrow/reader/projection.rs:
##########
@@ -214,6 +214,22 @@ impl ArrowReader {
}),
arrow_schema.metadata().clone(),
);
+ // A millisecond column under a nanosecond field would otherwise read
as nulls.
+ for field in projected_arrow_schema.flattened_fields() {
+ if let DataType::Timestamp(TimeUnit::Millisecond, _) =
field.data_type()
+ && let Some(field_id) = parquet_field_id(field)
+ && let Some(Type::Primitive(
+ PrimitiveType::TimestampNs | PrimitiveType::TimestamptzNs,
+ )) = iceberg_schema_of_task
+ .field_by_id(field_id)
+ .map(|field| field.field_type.as_ref())
+ {
+ return Err(invalid_data!(
+ "Unsupported Arrow data type: {}",
+ field.data_type()
+ ));
+ }
+ }
Review Comment:
Thanks for the review. I now reject any projected millisecond leaf whose
Iceberg type does not match the table field. That covers the nanosecond types,
UTC flag mismatches and legacy `TIMESTAMP_MILLIS`. The `DataInvalid` error
names the column, field id and both types. A table driven test covers your case
and the matching types. `MICROS` is unchanged.
##########
crates/iceberg/src/arrow/reader/row_filter.rs:
##########
@@ -2037,4 +2038,127 @@ mod tests {
.await;
assert_eq!(rows(&on), 1);
}
+
+ async fn read_id_and_ts(
+ reader: ArrowReader,
+ path: &str,
+ schema: SchemaRef,
+ predicate: &Predicate,
+ project_ts: bool,
+ ) -> Vec<(i32, Option<i64>)> {
+ let task = FileScanTask::builder()
+ .with_file_size_in_bytes(std::fs::metadata(path).unwrap().len())
+ .with_start(0)
+ .with_length(0)
+ .with_data_file_path(path.to_string())
+ .with_data_file_format(DataFileFormat::Parquet)
+ .with_schema(schema.clone())
+ .with_project_field_ids(if project_ts { vec![1, 2] } else {
vec![1] })
+ .with_predicate(Some(predicate.clone().bind(schema,
true).unwrap()))
+ .with_case_sensitive(true)
+ .build()
+ .unwrap();
+ let batches: Vec<RecordBatch> = reader
+ .read(Box::pin(futures::stream::iter(vec![Ok(task)])))
+ .unwrap()
+ .stream()
+ .try_collect()
+ .await
+ .unwrap();
+
+ let mut rows = vec![];
+ for batch in &batches {
+ let ids = batch
+ .column(0)
+ .as_primitive::<arrow_array::types::Int32Type>();
+ let ts = project_ts.then(|| {
+ batch
+ .column(1)
+
.as_primitive::<arrow_array::types::TimestampMicrosecondType>()
+ });
+ for row in 0..batch.num_rows() {
+ rows.push((ids.value(row), ts.map(|ts| ts.value(row))));
+ }
+ }
+ rows
+ }
+
+ #[tokio::test]
+ async fn test_millisecond_timestamp_column_in_every_pruning_mode() {
+ let tmp = TempDir::new().unwrap();
+ let path = format!("{}/ts_millis.parquet",
tmp.path().to_str().unwrap());
+
+ for (primitive_type, tz) in [
+ (PrimitiveType::Timestamp, None),
+ (PrimitiveType::Timestamptz, Some("UTC")),
+ ] {
+ let ts =
+ TimestampMillisecondArray::from(vec![-1_500, 1_000,
2_000]).with_timezone_opt(tz);
+ let arrow_schema = Arc::new(ArrowSchema::new(vec![
+ field_with_id("id", DataType::Int32, 1),
+ field_with_id("ts", ts.data_type().clone(), 2),
+ ]));
+ let batch = RecordBatch::try_new(arrow_schema.clone(), vec![
+ Arc::new(Int32Array::from(vec![0, 1, 2])),
+ Arc::new(ts),
+ ])
+ .unwrap();
+ write_row_groups(&path, arrow_schema, vec![batch], true);
+
+ let schema = Arc::new(
+ Schema::builder()
+ .with_fields(vec![
+ NestedField::required(1, "id",
Type::Primitive(PrimitiveType::Int)).into(),
+ NestedField::required(2, "ts",
Type::Primitive(primitive_type.clone()))
+ .into(),
+ ])
+ .build()
+ .unwrap(),
+ );
+ let micros = |v| Datum::new(primitive_type.clone(),
PrimitiveLiteral::Long(v));
+ let cases = [
+ (Reference::new("ts").equal_to(micros(2_000_000)), vec![(
+ 2, 2_000_000,
+ )]),
+ (Reference::new("ts").less_than(micros(1_000_500)), vec![
+ (0, -1_500_000),
+ (1, 1_000_000),
+ ]),
+ (Reference::new("ts").equal_to(micros(1_000_500)), vec![]),
+ ];
+
+ for (mode, row_group_filtering, row_selection, bloom_filter) in [
+ ("default", true, false, false),
+ ("page index only", false, true, false),
+ ("bloom filter only", false, false, true),
+ ("all off", false, false, false),
+ ] {
+ let reader = ArrowReaderBuilder::new(FileIO::new_with_fs(),
Runtime::current())
+ .with_row_group_filtering_enabled(row_group_filtering)
+ .with_row_selection_enabled(row_selection)
+ .with_bloom_filter_enabled(bloom_filter)
+ .build();
Review Comment:
I changed the test to use `with_options` and `ReaderOptions::builder()`
instead of the removed builder methods.
##########
crates/iceberg/src/arrow/record_batch_transformer.rs:
##########
@@ -454,7 +454,14 @@ impl PromotePlan {
fn apply(&self, array: &ArrayRef) -> Result<ArrayRef> {
match self {
PromotePlan::PassThrough => Ok(Arc::clone(array)),
- PromotePlan::Cast(target_type) => Ok(cast(array.as_ref(),
target_type)?),
+ PromotePlan::Cast(target_type) => Ok(cast_with_options(
+ array.as_ref(),
+ target_type,
+ &CastOptions {
+ safe: false,
+ ..Default::default()
+ },
+ )?),
Review Comment:
I now return `DataInvalid` with the source error attached and a message
naming the source and target types. I added "Part of #3404" and the date
overflow case to the description, and removed the claim that promotions cannot
fail this way.
##########
crates/iceberg/src/arrow/record_batch_transformer.rs:
##########
@@ -1590,6 +1598,14 @@ mod test {
Ok(transformer.process_record_batch(batch)?.column(1).clone())
}
+ #[test]
+ fn promote_millisecond_timestamp_overflow_errors() {
+ let column = NestedField::optional(2, "ts",
Type::Primitive(PrimitiveType::Timestamp));
+ let file_column =
Arc::new(TimestampMillisecondArray::from(vec![i64::MAX])) as ArrayRef;
+
+ assert!(transform_top_level(column, file_column).is_err());
+ }
Review Comment:
The millisecond overflow test now asserts `ErrorKind::DataInvalid`. I added
`promote_date_beyond_timestamp_ns_range_errors`, which asserts the same for a
`date` beyond the `timestamp_ns` range.
--
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]