datafusion_remote_table/
schema.rs

1use datafusion::arrow::datatypes::{DataType, Field, Schema};
2use std::sync::Arc;
3
4#[derive(Debug, Clone)]
5pub enum RemoteDataType {
6    Boolean,
7    Int8,
8    Int16,
9    Int32,
10    Int64,
11    UInt8,
12    UInt16,
13    UInt32,
14    UInt64,
15    Float16,
16    Float32,
17    Float64,
18    List(Box<RemoteField>),
19}
20
21impl RemoteDataType {
22    pub fn to_arrow_type(&self) -> DataType {
23        match self {
24            RemoteDataType::Boolean => DataType::Boolean,
25            RemoteDataType::Int8 => DataType::Int8,
26            RemoteDataType::Int16 => DataType::Int16,
27            RemoteDataType::Int32 => DataType::Int32,
28            RemoteDataType::Int64 => DataType::Int64,
29            RemoteDataType::UInt8 => DataType::UInt8,
30            RemoteDataType::UInt16 => DataType::UInt16,
31            RemoteDataType::UInt32 => DataType::UInt32,
32            RemoteDataType::UInt64 => DataType::UInt64,
33            RemoteDataType::Float16 => DataType::Float16,
34            RemoteDataType::Float32 => DataType::Float32,
35            RemoteDataType::Float64 => DataType::Float64,
36            RemoteDataType::List(field) => DataType::List(Arc::new(field.to_arrow_field())),
37        }
38    }
39}
40
41#[derive(Debug, Clone)]
42pub struct RemoteField {
43    pub name: String,
44    pub data_type: RemoteDataType,
45    pub nullable: bool,
46}
47
48impl RemoteField {
49    pub fn new(name: impl Into<String>, data_type: RemoteDataType, nullable: bool) -> Self {
50        RemoteField {
51            name: name.into(),
52            data_type,
53            nullable,
54        }
55    }
56
57    pub fn to_arrow_field(&self) -> Field {
58        Field::new(
59            self.name.clone(),
60            self.data_type.to_arrow_type(),
61            self.nullable,
62        )
63    }
64}
65
66#[derive(Debug, Clone)]
67pub struct RemoteSchema {
68    pub fields: Vec<RemoteField>,
69}
70
71impl RemoteSchema {
72    pub fn empty() -> Self {
73        RemoteSchema { fields: vec![] }
74    }
75    pub fn new(fields: Vec<RemoteField>) -> Self {
76        RemoteSchema { fields }
77    }
78
79    pub fn to_arrow_schema(&self) -> Schema {
80        let mut fields = vec![];
81        for remote_field in self.fields.iter() {
82            fields.push(remote_field.to_arrow_field());
83        }
84        Schema::new(fields)
85    }
86}