anoopj commented on code in PR #3058:
URL: https://github.com/apache/iceberg-rust/pull/3058#discussion_r3845680733
##########
crates/iceberg/src/arrow/reader/pipeline.rs:
##########
@@ -1453,6 +1510,523 @@ mod tests {
);
}
+ /// A scan task projecting `id` + `_row_id`, with the given `first_row_id`.
+ fn row_id_task(file_path: String, first_row_id: Option<i64>) ->
FileScanTask {
+ let schema = Arc::new(
+ Schema::builder()
+ .with_schema_id(1)
+ .with_fields(vec![
+ NestedField::required(1, "id",
Type::Primitive(PrimitiveType::Int)).into(),
+ ])
+ .build()
+ .unwrap(),
+ );
+
+ FileScanTask::builder()
+
.with_file_size_in_bytes(std::fs::metadata(&file_path).unwrap().len())
+ .with_start(0)
+ .with_length(0)
+ .with_data_file_path(file_path)
+ .with_data_file_format(DataFileFormat::Parquet)
+ .with_schema(schema)
+ .with_project_field_ids(vec![1, RESERVED_FIELD_ID_ROW_ID])
+ .with_first_row_id(first_row_id)
+ .with_case_sensitive(false)
+ .build()
+ }
+
+ /// Asserts the logical per-row values of the `_row_id` column across all
batches,
+ /// independent of the physical (run-end) encoding.
+ fn assert_row_id_column(batches: &[RecordBatch], expected: &[Option<i64>])
{
+ use arrow_array::cast::AsArray;
+ use arrow_cast::cast;
+ use arrow_schema::DataType;
+
+ let mut actual = Vec::new();
+ for batch in batches {
+ let col = batch
+ .column_by_name(RESERVED_COL_NAME_ROW_ID)
+ .expect("_row_id column should be present");
+ let logical = cast(col, &DataType::Int64).unwrap();
+ let values =
logical.as_primitive::<arrow_array::types::Int64Type>();
+ for i in 0..values.len() {
+ actual.push((!values.is_null(i)).then(|| values.value(i)));
+ }
+ }
+ assert_eq!(actual, expected);
+ }
+
+ /// A parquet field carrying the embedded `_row_id` field id.
+ fn physical_row_id_field() -> Field {
+ Field::new(RESERVED_COL_NAME_ROW_ID, DataType::Int64,
true).with_metadata(HashMap::from([
+ (
+ PARQUET_FIELD_ID_META_KEY.to_string(),
+ RESERVED_FIELD_ID_ROW_ID.to_string(),
+ ),
+ ]))
+ }
+
+ #[tokio::test]
+ async fn test_row_id_synthesized_from_first_row_id_and_pos() {
+ let tmp_dir = TempDir::new().unwrap();
+ let dir = tmp_dir.path().to_str().unwrap();
+ let file_path = write_plain_parquet(dir, "row_id_synth.parquet",
vec![], vec![]);
+
+ // No physical column: every row is first_row_id + pos.
+ let task = row_id_task(file_path, Some(100));
+
+ let reader = ArrowReaderBuilder::new(FileIO::new_with_fs(),
Runtime::current()).build();
+ let tasks = Box::pin(futures::stream::iter(vec![Ok(task)])) as
FileScanTaskStream;
+ let batches: Vec<RecordBatch> = reader
+ .read(tasks)
+ .unwrap()
+ .stream()
+ .try_collect()
+ .await
+ .unwrap();
+
+ assert_row_id_column(&batches, &[Some(100), Some(101), Some(102)]);
Review Comment:
Good idea. This also addresses @blackmwk feedback. Added
`test_row_id_global_across_row_groups`. PTAL
--
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]