datafusion_table_providers/util/
schema.rs1use 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 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}