datafusion_iceberg_sql/
lib.rs

1use std::{any::Any, sync::Arc};
2
3use arrow_schema::SchemaRef;
4use datafusion_expr::TableSource;
5use iceberg_rust::catalog::tabular::Tabular;
6
7pub mod context;
8pub mod schema;
9
10pub struct IcebergTableSource {
11    tabular: Tabular,
12    branch: Option<String>,
13}
14
15impl IcebergTableSource {
16    pub fn new(tabular: Tabular, branch: Option<&str>) -> Self {
17        IcebergTableSource {
18            tabular,
19            branch: branch.map(ToOwned::to_owned),
20        }
21    }
22}
23
24impl TableSource for IcebergTableSource {
25    fn as_any(&self) -> &dyn Any {
26        &self.tabular
27    }
28    fn schema(&self) -> SchemaRef {
29        match &self.tabular {
30            Tabular::Table(table) => {
31                let schema = table
32                    .current_schema(self.branch.as_deref())
33                    .or(table.current_schema(None))
34                    .unwrap();
35                Arc::new((schema.fields()).try_into().unwrap())
36            }
37            Tabular::View(view) => {
38                let schema = view
39                    .current_schema(self.branch.as_deref())
40                    .or(view.current_schema(None))
41                    .unwrap();
42                Arc::new((schema.fields()).try_into().unwrap())
43            }
44            Tabular::MaterializedView(matview) => {
45                let schema = matview
46                    .current_schema(self.branch.as_deref())
47                    .or(matview.current_schema(None))
48                    .unwrap();
49                Arc::new((schema.fields()).try_into().unwrap())
50            }
51        }
52    }
53}