kosiew commented on code in PR #16148:
URL: https://github.com/apache/datafusion/pull/16148#discussion_r2106461041


##########
datafusion/datasource-parquet/tests/apply_schema_adapter_tests.rs:
##########
@@ -0,0 +1,224 @@
+// 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.
+
+mod parquet_adapter_tests {
+    use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+    use arrow::record_batch::RecordBatch;
+    use datafusion_common::{ColumnStatistics, DataFusionError, Result};
+    use datafusion_datasource::file::FileSource;
+    use datafusion_datasource::file_scan_config::{
+        FileScanConfig, FileScanConfigBuilder,
+    };
+    use datafusion_datasource::schema_adapter::{
+        SchemaAdapter, SchemaAdapterFactory, SchemaMapper,
+    };
+    use datafusion_datasource_parquet::source::ParquetSource;
+    use datafusion_execution::object_store::ObjectStoreUrl;
+    use std::fmt::Debug;
+    use std::sync::Arc;
+
+    /// A test schema adapter factory that adds prefix to column names
+    #[derive(Debug)]
+    struct PrefixAdapterFactory {
+        prefix: String,
+    }
+
+    impl SchemaAdapterFactory for PrefixAdapterFactory {
+        fn create(
+            &self,
+            projected_table_schema: SchemaRef,
+            _table_schema: SchemaRef,
+        ) -> Box<dyn SchemaAdapter> {
+            Box::new(PrefixAdapter {
+                input_schema: projected_table_schema,
+                prefix: self.prefix.clone(),
+            })
+        }
+    }
+
+    /// A test schema adapter that adds prefix to column names
+    #[derive(Debug)]
+    struct PrefixAdapter {
+        input_schema: SchemaRef,
+        prefix: String,
+    }
+
+    impl SchemaAdapter for PrefixAdapter {
+        fn map_column_index(&self, index: usize, file_schema: &Schema) -> 
Option<usize> {
+            let field = self.input_schema.field(index);
+            file_schema.fields.find(field.name()).map(|(i, _)| i)
+        }
+
+        fn map_schema(
+            &self,
+            file_schema: &Schema,
+        ) -> Result<(Arc<dyn SchemaMapper>, Vec<usize>)> {
+            let mut projection = 
Vec::with_capacity(file_schema.fields().len());
+            for (file_idx, file_field) in 
file_schema.fields().iter().enumerate() {
+                if 
self.input_schema.fields().find(file_field.name()).is_some() {
+                    projection.push(file_idx);
+                }
+            }
+
+            // Create a schema mapper that adds a prefix to column names
+            #[derive(Debug)]
+            struct PrefixSchemaMapping {
+                // Keep only the prefix field which is actually used in the 
implementation
+                prefix: String,
+            }
+
+            impl SchemaMapper for PrefixSchemaMapping {
+                fn map_batch(&self, batch: RecordBatch) -> Result<RecordBatch> 
{
+                    // Create a new schema with prefixed field names
+                    let prefixed_fields: Vec<Field> = batch
+                        .schema()
+                        .fields()
+                        .iter()
+                        .map(|field| {
+                            Field::new(
+                                format!("{}{}", self.prefix, field.name()),
+                                field.data_type().clone(),
+                                field.is_nullable(),
+                            )
+                        })
+                        .collect();
+                    let prefixed_schema = 
Arc::new(Schema::new(prefixed_fields));
+
+                    // Create a new batch with the prefixed schema but the 
same data
+                    let options = 
arrow::record_batch::RecordBatchOptions::default();
+                    RecordBatch::try_new_with_options(
+                        prefixed_schema,
+                        batch.columns().to_vec(),
+                        &options,
+                    )
+                    .map_err(|e| DataFusionError::ArrowError(e, None))
+                }
+
+                fn map_column_statistics(
+                    &self,
+                    stats: &[ColumnStatistics],
+                ) -> Result<Vec<ColumnStatistics>> {
+                    // For testing, just return the input statistics
+                    Ok(stats.to_vec())
+                }
+            }
+
+            Ok((
+                Arc::new(PrefixSchemaMapping {
+                    prefix: self.prefix.clone(),
+                }),
+                projection,
+            ))
+        }
+    }
+
+    // Implementation of apply_schema_adapter for testing purposes
+    // This mimics the private function in the datafusion-parquet crate
+    fn apply_schema_adapter(

Review Comment:
   I like your suggestion!



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

Reply via email to