-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose Parquet Schema Adapter (#10515)
* feat: Expose Parquet Schema Adapter
- Loading branch information
1 parent
ea023e2
commit 4e55768
Showing
6 changed files
with
312 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
datafusion/core/src/datasource/physical_plan/parquet/schema_adapter.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 arrow_array::RecordBatch; | ||
use arrow_schema::{Schema, SchemaRef}; | ||
use std::fmt::Debug; | ||
use std::sync::Arc; | ||
|
||
/// Factory of schema adapters. | ||
/// | ||
/// Provides means to implement custom schema adaptation. | ||
pub trait SchemaAdapterFactory: Debug + Send + Sync + 'static { | ||
/// Provides `SchemaAdapter` for the ParquetExec. | ||
fn create(&self, schema: SchemaRef) -> Box<dyn SchemaAdapter>; | ||
} | ||
|
||
/// A utility which can adapt file-level record batches to a table schema which may have a schema | ||
/// obtained from merging multiple file-level schemas. | ||
/// | ||
/// This is useful for enabling schema evolution in partitioned datasets. | ||
/// | ||
/// This has to be done in two stages. | ||
/// | ||
/// 1. Before reading the file, we have to map projected column indexes from the table schema to | ||
/// the file schema. | ||
/// | ||
/// 2. After reading a record batch we need to map the read columns back to the expected columns | ||
/// indexes and insert null-valued columns wherever the file schema was missing a colum present | ||
/// in the table schema. | ||
pub trait SchemaAdapter: Send + Sync { | ||
/// Map a column index in the table schema to a column index in a particular | ||
/// file schema | ||
/// | ||
/// Panics if index is not in range for the table schema | ||
fn map_column_index(&self, index: usize, file_schema: &Schema) -> Option<usize>; | ||
|
||
/// Creates a `SchemaMapping` that can be used to cast or map the columns from the file schema to the table schema. | ||
/// | ||
/// If the provided `file_schema` contains columns of a different type to the expected | ||
/// `table_schema`, the method will attempt to cast the array data from the file schema | ||
/// to the table schema where possible. | ||
/// | ||
/// Returns a [`SchemaMapper`] that can be applied to the output batch | ||
/// along with an ordered list of columns to project from the file | ||
fn map_schema( | ||
&self, | ||
file_schema: &Schema, | ||
) -> datafusion_common::Result<(Arc<dyn SchemaMapper>, Vec<usize>)>; | ||
} | ||
|
||
/// Transforms a RecordBatch from Parquet to a RecordBatch that meets the table schema. | ||
pub trait SchemaMapper: Send + Sync { | ||
/// Adapts a `RecordBatch` to match the `table_schema` using the stored mapping and conversions. | ||
fn map_batch(&self, batch: RecordBatch) -> datafusion_common::Result<RecordBatch>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.