timsaucer commented on code in PR #16371: URL: https://github.com/apache/datafusion/pull/16371#discussion_r2156903806
########## datafusion/common/src/nested_struct.rs: ########## @@ -0,0 +1,150 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::error::Result; +use arrow::{ + array::{new_null_array, Array, ArrayRef, StructArray}, + compute::cast, + datatypes::{DataType::Struct, Field}, +}; +use std::sync::Arc; +/// Adapt a struct column to match the target field type, handling nested structs recursively +fn adapt_struct_column( + source_col: &ArrayRef, + target_fields: &[Arc<Field>], +) -> Result<ArrayRef> { + if let Some(struct_array) = source_col.as_any().downcast_ref::<StructArray>() { + let mut children: Vec<(Arc<Field>, Arc<dyn Array>)> = Vec::new(); + let num_rows = source_col.len(); + + for target_child_field in target_fields { + let field_arc = Arc::clone(target_child_field); + match struct_array.column_by_name(target_child_field.name()) { + Some(source_child_col) => { + let adapted_child = + adapt_column(source_child_col, target_child_field)?; + children.push((field_arc, adapted_child)); + } + None => { + children.push(( + field_arc, + new_null_array(target_child_field.data_type(), num_rows), + )); + } + } + } + + let struct_array = StructArray::from(children); + Ok(Arc::new(struct_array)) + } else { + // If source is not a struct, return null array with target struct type + Ok(new_null_array( + &Struct(target_fields.to_vec().into()), + source_col.len(), + )) + } Review Comment: From the function signature it seems like this is an improper usage - would it make sense that we would return an error in the case of calling this function for a non-struct? ########## datafusion/common/src/nested_struct.rs: ########## @@ -0,0 +1,150 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::error::Result; +use arrow::{ + array::{new_null_array, Array, ArrayRef, StructArray}, + compute::cast, + datatypes::{DataType::Struct, Field}, +}; +use std::sync::Arc; +/// Adapt a struct column to match the target field type, handling nested structs recursively +fn adapt_struct_column( Review Comment: nit: would `cast_struct_column` be a more intuitive name? Same for `cast_column` below ########## datafusion/datasource/src/nested_schema_adapter/adapter.rs: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::schema_adapter::{ + create_field_mapping, SchemaAdapter, SchemaMapper, SchemaMapping, +}; +use arrow::{ + array::ArrayRef, + datatypes::{DataType::Struct, Field, Schema, SchemaRef}, +}; +use datafusion_common::nested_struct::adapt_column; +use datafusion_common::Result; +use std::sync::Arc; + +/// A SchemaAdapter that handles schema evolution for nested struct types +#[derive(Debug, Clone)] +pub struct NestedStructSchemaAdapter { + /// The schema for the table, projected to include only the fields being output (projected) by the + /// associated ParquetSource + projected_table_schema: SchemaRef, + /// The entire table schema for the table we're using this to adapt. + /// + /// This is used to evaluate any filters pushed down into the scan + /// which may refer to columns that are not referred to anywhere + /// else in the plan. + table_schema: SchemaRef, +} + +impl NestedStructSchemaAdapter { + /// Create a new NestedStructSchemaAdapter with the target schema + pub fn new(projected_table_schema: SchemaRef, table_schema: SchemaRef) -> Self { + Self { + projected_table_schema, + table_schema, + } + } + + pub fn projected_table_schema(&self) -> &Schema { + self.projected_table_schema.as_ref() + } + + pub fn table_schema(&self) -> &Schema { + self.table_schema.as_ref() + } +} + +impl SchemaAdapter for NestedStructSchemaAdapter { + fn map_column_index(&self, index: usize, file_schema: &Schema) -> Option<usize> { + let field_name = self.table_schema.field(index).name(); + file_schema.index_of(field_name).ok() + } + + fn map_schema( + &self, + file_schema: &Schema, + ) -> Result<(Arc<dyn SchemaMapper>, Vec<usize>)> { + let (field_mappings, projection) = create_field_mapping( + file_schema, + &self.projected_table_schema, + |file_field, table_field| { + // Special handling for struct fields - always include them even if the + // internal structure differs, as we'll adapt them later + match (file_field.data_type(), table_field.data_type()) { + (Struct(_), Struct(_)) => Ok(true), Review Comment: Is it right that this presumes that we *can* always cast from struct to struct? It feels like that isn't necessarily true. For example, if I have an input struct that contains two fields, both of which are strings and I attempt to cast it to an output struct with one field of type u32 then I *think* the current approach would give me a pass but an empty array. Is that correct? -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org