datafusion_datasource/
schema_adapter.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Deprecated: [`SchemaAdapter`] and [`SchemaAdapterFactory`] have been removed.
19//!
20//! Use [`PhysicalExprAdapterFactory`] instead. See `upgrading.md` for more details.
21//!
22//! [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
23
24#![allow(deprecated)]
25
26use arrow::array::{ArrayRef, RecordBatch};
27use arrow::datatypes::{Field, Schema, SchemaRef};
28use datafusion_common::{ColumnStatistics, Result, not_impl_err};
29use log::warn;
30use std::fmt::Debug;
31use std::sync::Arc;
32
33/// Deprecated: Function type for casting columns.
34///
35/// This type has been removed. Use [`PhysicalExprAdapterFactory`] instead.
36/// See `upgrading.md` for more details.
37///
38/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
39#[deprecated(
40    since = "52.0.0",
41    note = "SchemaAdapter has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
42)]
43pub type CastColumnFn = dyn Fn(&ArrayRef, &Field, &arrow::compute::CastOptions) -> Result<ArrayRef>
44    + Send
45    + Sync;
46
47/// Deprecated: Factory for creating [`SchemaAdapter`].
48///
49/// This trait has been removed. Use [`PhysicalExprAdapterFactory`] instead.
50/// See `upgrading.md` for more details.
51///
52/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
53#[deprecated(
54    since = "52.0.0",
55    note = "SchemaAdapter has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
56)]
57pub trait SchemaAdapterFactory: Debug + Send + Sync + 'static {
58    /// Create a [`SchemaAdapter`]
59    fn create(
60        &self,
61        projected_table_schema: SchemaRef,
62        table_schema: SchemaRef,
63    ) -> Box<dyn SchemaAdapter>;
64
65    /// Create a [`SchemaAdapter`] using only the projected table schema.
66    fn create_with_projected_schema(
67        &self,
68        projected_table_schema: SchemaRef,
69    ) -> Box<dyn SchemaAdapter> {
70        self.create(Arc::clone(&projected_table_schema), projected_table_schema)
71    }
72}
73
74/// Deprecated: Creates [`SchemaMapper`]s to map file-level [`RecordBatch`]es to a table schema.
75///
76/// This trait has been removed. Use [`PhysicalExprAdapterFactory`] instead.
77/// See `upgrading.md` for more details.
78///
79/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
80#[deprecated(
81    since = "52.0.0",
82    note = "SchemaAdapter has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
83)]
84pub trait SchemaAdapter: Send + Sync {
85    /// Map a column index in the table schema to a column index in a particular file schema.
86    fn map_column_index(&self, index: usize, file_schema: &Schema) -> Option<usize>;
87
88    /// Creates a mapping for casting columns from the file schema to the table schema.
89    fn map_schema(
90        &self,
91        file_schema: &Schema,
92    ) -> Result<(Arc<dyn SchemaMapper>, Vec<usize>)>;
93}
94
95/// Deprecated: Maps columns from a specific file schema to the table schema.
96///
97/// This trait has been removed. Use [`PhysicalExprAdapterFactory`] instead.
98/// See `upgrading.md` for more details.
99///
100/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
101#[deprecated(
102    since = "52.0.0",
103    note = "SchemaMapper has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
104)]
105pub trait SchemaMapper: Debug + Send + Sync {
106    /// Adapts a `RecordBatch` to match the `table_schema`.
107    fn map_batch(&self, batch: RecordBatch) -> Result<RecordBatch>;
108
109    /// Adapts file-level column `Statistics` to match the `table_schema`.
110    fn map_column_statistics(
111        &self,
112        file_col_statistics: &[ColumnStatistics],
113    ) -> Result<Vec<ColumnStatistics>>;
114}
115
116/// Deprecated: Default [`SchemaAdapterFactory`] for mapping schemas.
117///
118/// This struct has been removed.
119///
120/// Use [`PhysicalExprAdapterFactory`] instead to customize scans via
121/// [`FileScanConfigBuilder`], i.e. if you had implemented a custom [`SchemaAdapter`]
122/// and passed that into [`FileScanConfigBuilder`] / [`ParquetSource`].
123/// Use [`BatchAdapter`] if you want to map a stream of [`RecordBatch`]es
124/// between one schema and another, i.e. if you were calling [`SchemaMapper::map_batch`] manually.
125///
126/// See `upgrading.md` for more details.
127///
128/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
129/// [`FileScanConfigBuilder`]: crate::file_scan_config::FileScanConfigBuilder
130/// [`ParquetSource`]: https://docs.rs/datafusion-datasource-parquet/latest/datafusion_datasource_parquet/source/struct.ParquetSource.html
131/// [`BatchAdapter`]: datafusion_physical_expr_adapter::BatchAdapter
132#[deprecated(
133    since = "52.0.0",
134    note = "DefaultSchemaAdapterFactory has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
135)]
136#[derive(Clone, Debug, Default)]
137pub struct DefaultSchemaAdapterFactory;
138
139impl SchemaAdapterFactory for DefaultSchemaAdapterFactory {
140    fn create(
141        &self,
142        projected_table_schema: SchemaRef,
143        _table_schema: SchemaRef,
144    ) -> Box<dyn SchemaAdapter> {
145        Box::new(DeprecatedSchemaAdapter {
146            _projected_table_schema: projected_table_schema,
147        })
148    }
149}
150
151impl DefaultSchemaAdapterFactory {
152    /// Deprecated: Create a new factory for mapping batches from a file schema to a table schema.
153    #[deprecated(
154        since = "52.0.0",
155        note = "DefaultSchemaAdapterFactory has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
156    )]
157    pub fn from_schema(table_schema: SchemaRef) -> Box<dyn SchemaAdapter> {
158        // Note: this method did not return an error thus the errors are raised from the returned adapter
159        warn!(
160            "DefaultSchemaAdapterFactory::from_schema is deprecated. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
161        );
162        Box::new(DeprecatedSchemaAdapter {
163            _projected_table_schema: table_schema,
164        })
165    }
166}
167
168/// Internal deprecated adapter that returns errors when methods are called.
169struct DeprecatedSchemaAdapter {
170    _projected_table_schema: SchemaRef,
171}
172
173impl SchemaAdapter for DeprecatedSchemaAdapter {
174    fn map_column_index(&self, _index: usize, _file_schema: &Schema) -> Option<usize> {
175        None // Safe no-op
176    }
177
178    fn map_schema(
179        &self,
180        _file_schema: &Schema,
181    ) -> Result<(Arc<dyn SchemaMapper>, Vec<usize>)> {
182        not_impl_err!(
183            "SchemaAdapter has been removed. Use PhysicalExprAdapterFactory instead. \
184            See upgrading.md for more details."
185        )
186    }
187}
188
189/// Deprecated: The SchemaMapping struct held a mapping from the file schema to the table schema.
190///
191/// This struct has been removed.
192///
193/// Use [`PhysicalExprAdapterFactory`] instead to customize scans via
194/// [`FileScanConfigBuilder`], i.e. if you had implemented a custom [`SchemaAdapter`]
195/// and passed that into [`FileScanConfigBuilder`] / [`ParquetSource`].
196/// Use [`BatchAdapter`] if you want to map a stream of [`RecordBatch`]es
197/// between one schema and another, i.e. if you were calling [`SchemaMapper::map_batch`] manually.
198///
199/// See `upgrading.md` for more details.
200///
201/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
202/// [`FileScanConfigBuilder`]: crate::file_scan_config::FileScanConfigBuilder
203/// [`ParquetSource`]: https://docs.rs/datafusion-datasource-parquet/latest/datafusion_datasource_parquet/source/struct.ParquetSource.html
204/// [`BatchAdapter`]: datafusion_physical_expr_adapter::BatchAdapter
205#[deprecated(
206    since = "52.0.0",
207    note = "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
208)]
209#[derive(Debug)]
210pub struct SchemaMapping {
211    // Private fields removed - this is a skeleton for deprecation purposes only
212    _private: (),
213}
214
215impl SchemaMapper for SchemaMapping {
216    fn map_batch(&self, _batch: RecordBatch) -> Result<RecordBatch> {
217        not_impl_err!(
218            "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. \
219            See upgrading.md for more details."
220        )
221    }
222
223    fn map_column_statistics(
224        &self,
225        _file_col_statistics: &[ColumnStatistics],
226    ) -> Result<Vec<ColumnStatistics>> {
227        not_impl_err!(
228            "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. \
229            See upgrading.md for more details."
230        )
231    }
232}