datafusion_table_providers/util/
schema.rs

1use std::sync::Arc;
2
3use super::handle_unsupported_type_error;
4use crate::UnsupportedTypeAction;
5use arrow_schema::{DataType, Field, SchemaBuilder};
6use datafusion::arrow::datatypes::SchemaRef;
7
8type GenericError = Box<dyn std::error::Error + Send + Sync>;
9type Result<T, E = GenericError> = std::result::Result<T, E>;
10
11pub trait SchemaValidator {
12    type Error: std::error::Error + Send + Sync;
13
14    #[must_use]
15    fn is_field_supported(field: &Arc<Field>) -> bool {
16        Self::is_data_type_supported(field.data_type())
17    }
18
19    #[must_use]
20    fn is_schema_supported(schema: &SchemaRef) -> bool {
21        schema.fields.iter().all(Self::is_field_supported)
22    }
23
24    fn is_data_type_supported(data_type: &DataType) -> bool;
25
26    fn unsupported_type_error(data_type: &DataType, field_name: &str) -> Self::Error;
27
28    /// For a given input schema, rebuild it according to the `UnsupportedTypeAction`.
29    /// Implemented for a specific connection type, that connection determines which types it supports.
30    /// If the `UnsupportedTypeAction` is `Error`/`String`, the function will return an error if the schema contains an unsupported data type.
31    /// If the `UnsupportedTypeAction` is `Warn`, the function will log a warning if the schema contains an unsupported data type and remove the column.
32    /// If the `UnsupportedTypeAction` is `Ignore`, the function will remove the column silently.
33    ///
34    /// Components that want to handle `UnsupportedTypeAction::String` via the component's own logic should re-implement this trait function.
35    ///
36    /// # Errors
37    ///
38    /// If the `UnsupportedTypeAction` is `Error` or `String` and the schema contains an unsupported data type, the function will return an error.
39    fn handle_unsupported_schema(
40        schema: &SchemaRef,
41        unsupported_type_action: UnsupportedTypeAction,
42    ) -> Result<SchemaRef, Self::Error> {
43        let mut schema_builder = SchemaBuilder::new();
44        for field in &schema.fields {
45            if Self::is_field_supported(field) {
46                schema_builder.push(Arc::clone(field));
47            } else {
48                handle_unsupported_type_error(
49                    unsupported_type_action,
50                    Self::unsupported_type_error(field.data_type(), field.name()),
51                )?;
52            }
53        }
54
55        Ok(Arc::new(schema_builder.finish()))
56    }
57}