mbutrovich commented on code in PR #3467:
URL: https://github.com/apache/iceberg-rust/pull/3467#discussion_r4604510468
##########
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:
Main now has
[`coerce_fixed_size_lists`](https://github.com/apache/iceberg-rust/blob/7cf825fcfb64a8943ddb627ef7f0b9f09f0c8046/crates/iceberg/src/arrow/reader/projection.rs#L897-L957)
from #3629, which has the same shape as `coerce_list_views` and runs at the
same spot in the pipeline. Could we add the list view arms to that function,
and give it a name that covers both, instead of adding a second rewrite pass?
These arms are all it needs:
```rust
DataType::ListView(element) =>
DataType::List(coerce_fixed_size_lists_or_share(element)),
DataType::LargeListView(element) => {
DataType::LargeList(coerce_fixed_size_lists_or_share(element))
}
```
The reason in the doc comment also changed. arrow-schema 60.1.0 fixed
`Fields::filter_leaves` in apache/arrow-rs#11657. The rewrite is still needed
because `arrow_schema_to_schema` and `RecordBatchTransformer` can't convert a
list view. Could the doc comment and the PR description say that instead?
--
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]