datafusion_iceberg_sql/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::{any::Any, sync::Arc};

use arrow_schema::SchemaRef;
use datafusion_expr::TableSource;
use iceberg_rust::catalog::tabular::Tabular;

pub mod context;
pub mod schema;

pub struct IcebergTableSource {
    tabular: Tabular,
    branch: Option<String>,
}

impl IcebergTableSource {
    pub fn new(tabular: Tabular, branch: Option<&str>) -> Self {
        IcebergTableSource {
            tabular,
            branch: branch.map(ToOwned::to_owned),
        }
    }
}

impl TableSource for IcebergTableSource {
    fn as_any(&self) -> &dyn Any {
        &self.tabular
    }
    fn schema(&self) -> SchemaRef {
        match &self.tabular {
            Tabular::Table(table) => {
                let schema = table
                    .current_schema(self.branch.as_deref())
                    .or(table.current_schema(None))
                    .unwrap();
                Arc::new((schema.fields()).try_into().unwrap())
            }
            Tabular::View(view) => {
                let schema = view
                    .current_schema(self.branch.as_deref())
                    .or(view.current_schema(None))
                    .unwrap();
                Arc::new((schema.fields()).try_into().unwrap())
            }
            Tabular::MaterializedView(matview) => {
                let schema = matview
                    .current_schema(self.branch.as_deref())
                    .or(matview.current_schema(None))
                    .unwrap();
                Arc::new((schema.fields()).try_into().unwrap())
            }
        }
    }
}