comphead commented on code in PR #3467:
URL: https://github.com/apache/iceberg-rust/pull/3467#discussion_r4654350369


##########
crates/iceberg/src/arrow/reader/projection.rs:
##########
@@ -533,6 +532,64 @@ fn try_map_fields(
     Ok(Fields::from(mapped))
 }
 
+/// Rewrites every `ListView` and `LargeListView` in `arrow_schema` to `List` 
and `LargeList`,
+/// because `Fields::filter_leaves`, which the projection relies on, treats 
list views as leaves
+/// (#3393). Returns `None` if there are none.
+pub(super) fn coerce_list_views(arrow_schema: &ArrowSchemaRef) -> 
Option<ArrowSchemaRef> {
+    let fields = coerce_list_views_in_fields(arrow_schema.fields())?;
+    Some(Arc::new(ArrowSchema::new_with_metadata(
+        fields,
+        arrow_schema.metadata().clone(),
+    )))
+}
+
+/// Returns `fields` with their list views rewritten, or `None` if they have 
none. Unchanged
+/// fields are shared, and nothing is allocated before the first change.
+fn coerce_list_views_in_fields(fields: &Fields) -> Option<Fields> {
+    let (index, first) = fields
+        .iter()
+        .enumerate()
+        .find_map(|(index, field)| Some((index, 
coerce_list_views_in_field(field)?)))?;
+    let rest = fields
+        .iter()
+        .skip(index + 1)
+        .map(coerce_list_views_or_share);
+    Some(
+        fields
+            .iter()
+            .take(index)
+            .cloned()
+            .chain([first])
+            .chain(rest)
+            .collect(),
+    )
+}
+
+/// Returns `field` with its list views rewritten, or `None` if it has none.
+fn coerce_list_views_in_field(field: &FieldRef) -> Option<FieldRef> {
+    let data_type = match field.data_type() {
+        DataType::ListView(element) => 
DataType::List(coerce_list_views_or_share(element)),
+        DataType::LargeListView(element) => {
+            DataType::LargeList(coerce_list_views_or_share(element))
+        }
+        DataType::List(element) => 
DataType::List(coerce_list_views_in_field(element)?),
+        DataType::LargeList(element) => 
DataType::LargeList(coerce_list_views_in_field(element)?),
+        DataType::FixedSizeList(element, size) => {
+            DataType::FixedSizeList(coerce_list_views_in_field(element)?, 
*size)
+        }
+        DataType::Map(entries, sorted) => {
+            DataType::Map(coerce_list_views_in_field(entries)?, *sorted)
+        }
+        DataType::Struct(fields) => 
DataType::Struct(coerce_list_views_in_fields(fields)?),
+        _ => return None,
+    };
+    Some(Arc::new(field.as_ref().clone().with_data_type(data_type)))
+}
+
+fn coerce_list_views_or_share(field: &FieldRef) -> FieldRef {
+    coerce_list_views_in_field(field).unwrap_or_else(|| Arc::clone(field))
+}

Review Comment:
   Done in fe3120a0d. The arms are in the `FixedSizeList` rewrite, renamed 
`coerce_list_layouts` after Arrow's list layouts, and its doc comment gives the 
reason after 60.1.0. `PresenceWalk::visit` folds its list view arm into the 
`List` arm without the `pick` flag, and the comments that pointed at this PR 
are updated.



##########
crates/iceberg/src/arrow/reader/pipeline.rs:
##########
@@ -655,25 +655,24 @@ impl FileScanTaskReader {
         // otherwise, use applyNameMapping() followed by pruneColumns() when 
configured, or
         // addFallbackIds() followed by pruneColumnsFallback() for 
position-based fallback.
         // The fast path returns early without materializing an owned schema.
+        let without_list_views = coerce_list_views(arrow_metadata.schema());
+        let file_schema = without_list_views
+            .as_ref()
+            .unwrap_or(arrow_metadata.schema());
         let arrow_schema = if missing_field_ids {
             let schema = if let Some(name_mapping) = task.name_mapping() {
-                apply_name_mapping_to_arrow_schema(
-                    Arc::clone(arrow_metadata.schema()),
-                    name_mapping,
-                )?
+                apply_name_mapping_to_arrow_schema(Arc::clone(file_schema), 
name_mapping)?

Review Comment:
   Done in 33c4193c5. `test_read_list_view_columns` also writes the file 
without field IDs and reads it through a name mapping. Passing the original 
hint to `apply_name_mapping_to_arrow_schema` now trips the new `debug_assert!`, 
and with the assert removed, `lv` reads as all NULL.



##########
crates/iceberg/src/arrow/reader/pipeline.rs:
##########
@@ -655,25 +655,24 @@ impl FileScanTaskReader {
         // otherwise, use applyNameMapping() followed by pruneColumns() when 
configured, or
         // addFallbackIds() followed by pruneColumnsFallback() for 
position-based fallback.
         // The fast path returns early without materializing an owned schema.
+        let without_list_views = coerce_list_views(arrow_metadata.schema());

Review Comment:
   Done in fe3120a0d, without the `filter_leaves` reason, since 60.1.0 fixed 
that part. The PR description now says a list view column comes back as `List`.



##########
crates/iceberg/src/arrow/reader/pipeline.rs:
##########
@@ -655,25 +655,24 @@ impl FileScanTaskReader {
         // otherwise, use applyNameMapping() followed by pruneColumns() when 
configured, or
         // addFallbackIds() followed by pruneColumnsFallback() for 
position-based fallback.
         // The fast path returns early without materializing an owned schema.
+        let without_list_views = coerce_list_views(arrow_metadata.schema());
+        let file_schema = without_list_views
+            .as_ref()
+            .unwrap_or(arrow_metadata.schema());
         let arrow_schema = if missing_field_ids {
             let schema = if let Some(name_mapping) = task.name_mapping() {
-                apply_name_mapping_to_arrow_schema(
-                    Arc::clone(arrow_metadata.schema()),
-                    name_mapping,
-                )?
+                apply_name_mapping_to_arrow_schema(Arc::clone(file_schema), 
name_mapping)?
             } else {
-                add_fallback_field_ids_to_arrow_schema(arrow_metadata.schema())
+                add_fallback_field_ids_to_arrow_schema(file_schema)

Review Comment:
   Done in 33c4193c5. The file without IDs is a `LargeListView`, read through a 
name mapping and through fallback IDs. Fallback IDs are top-level only, so that 
run reads the struct's fields as null 
(https://github.com/apache/iceberg-rust/pull/3437) and checks the list offsets 
and `id`. Each run fails if its branch gets the original hint.



##########
crates/iceberg/src/arrow/reader/projection.rs:
##########
@@ -499,8 +499,7 @@ fn apply_name_mapping_to_field(
                 .with_data_type(DataType::Struct(entry_fields));
             DataType::Map(Arc::new(entries), *sorted)
         }
-        // `Fields::filter_leaves` treats `ListView` and `LargeListView` as 
leaves, so projection
-        // cannot select their elements even with IDs.
+        // List views were already rewritten to lists by `coerce_list_views`.

Review Comment:
   Done in 33c4193c5. The `arrow_schema` argument doc states the precondition, 
and the `other` arm has the `debug_assert!`.



##########
crates/iceberg/src/arrow/reader/projection.rs:
##########
@@ -1393,6 +1450,64 @@ message schema {
         );
     }
 
+    /// List views become lists at any depth and keep their metadata, and a 
schema without one
+    /// is left as it is.
+    #[test]
+    fn test_coerce_list_views() {
+        use arrow_schema::{FieldRef, Fields};
+
+        use super::coerce_list_views;
+
+        // The same schema, built with list views or with lists.
+        let schema = |list: fn(FieldRef) -> DataType, large_list: fn(FieldRef) 
-> DataType| {
+            let id = HashMap::from([(PARQUET_FIELD_ID_META_KEY.to_string(), 
"9".to_string())]);
+            let int = |name: &str| Field::new(name, DataType::Int32, true);
+            let element = |data_type| Arc::new(Field::new("element", 
data_type, true));
+            let ints = element(DataType::Int32);

Review Comment:
   Done in fe3120a0d. `test_coerce_list_layouts` replaces this test and main's 
`FixedSizeList` one, and every field carries an ID.



-- 
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]

Reply via email to