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. Use [`PhysicalExprAdapterFactory`] instead.
119/// See `upgrading.md` for more details.
120///
121/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
122#[deprecated(
123    since = "52.0.0",
124    note = "DefaultSchemaAdapterFactory has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
125)]
126#[derive(Clone, Debug, Default)]
127pub struct DefaultSchemaAdapterFactory;
128
129impl SchemaAdapterFactory for DefaultSchemaAdapterFactory {
130    fn create(
131        &self,
132        projected_table_schema: SchemaRef,
133        _table_schema: SchemaRef,
134    ) -> Box<dyn SchemaAdapter> {
135        Box::new(DeprecatedSchemaAdapter {
136            _projected_table_schema: projected_table_schema,
137        })
138    }
139}
140
141impl DefaultSchemaAdapterFactory {
142    /// Deprecated: Create a new factory for mapping batches from a file schema to a table schema.
143    #[deprecated(
144        since = "52.0.0",
145        note = "DefaultSchemaAdapterFactory has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
146    )]
147    pub fn from_schema(table_schema: SchemaRef) -> Box<dyn SchemaAdapter> {
148        // Note: this method did not return an error thus the errors are raised from the returned adapter
149        warn!(
150            "DefaultSchemaAdapterFactory::from_schema is deprecated. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
151        );
152        Box::new(DeprecatedSchemaAdapter {
153            _projected_table_schema: table_schema,
154        })
155    }
156}
157
158/// Internal deprecated adapter that returns errors when methods are called.
159struct DeprecatedSchemaAdapter {
160    _projected_table_schema: SchemaRef,
161}
162
163impl SchemaAdapter for DeprecatedSchemaAdapter {
164    fn map_column_index(&self, _index: usize, _file_schema: &Schema) -> Option<usize> {
165        None // Safe no-op
166    }
167
168    fn map_schema(
169        &self,
170        _file_schema: &Schema,
171    ) -> Result<(Arc<dyn SchemaMapper>, Vec<usize>)> {
172        not_impl_err!(
173            "SchemaAdapter has been removed. Use PhysicalExprAdapterFactory instead. \
174            See upgrading.md for more details."
175        )
176    }
177}
178
179/// Deprecated: The SchemaMapping struct held a mapping from the file schema to the table schema.
180///
181/// This struct has been removed. Use [`PhysicalExprAdapterFactory`] instead.
182/// See `upgrading.md` for more details.
183///
184/// [`PhysicalExprAdapterFactory`]: datafusion_physical_expr_adapter::PhysicalExprAdapterFactory
185#[deprecated(
186    since = "52.0.0",
187    note = "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. See upgrading.md for more details."
188)]
189#[derive(Debug)]
190pub struct SchemaMapping {
191    // Private fields removed - this is a skeleton for deprecation purposes only
192    _private: (),
193}
194
195impl SchemaMapper for SchemaMapping {
196    fn map_batch(&self, _batch: RecordBatch) -> Result<RecordBatch> {
197        not_impl_err!(
198            "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. \
199            See upgrading.md for more details."
200        )
201    }
202
203    fn map_column_statistics(
204        &self,
205        _file_col_statistics: &[ColumnStatistics],
206    ) -> Result<Vec<ColumnStatistics>> {
207        not_impl_err!(
208            "SchemaMapping has been removed. Use PhysicalExprAdapterFactory instead. \
209            See upgrading.md for more details."
210        )
211    }
212}