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