-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfederation.rs
95 lines (84 loc) · 3.01 KB
/
federation.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::sql::db_connection_pool::dbconnection::{get_schema, Error as DbError};
use crate::sql::sql_provider_datafusion::{get_stream, to_execution_error};
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::sql::unparser::dialect::Dialect;
use datafusion_federation::sql::{SQLExecutor, SQLFederationProvider, SQLTableSource};
use datafusion_federation::{FederatedTableProviderAdaptor, FederatedTableSource};
use futures::TryStreamExt;
use snafu::ResultExt;
use async_trait::async_trait;
use std::sync::Arc;
use super::sql_table::{get_cte, DuckDBTable};
use datafusion::{
datasource::TableProvider,
error::{DataFusionError, Result as DataFusionResult},
execution::SendableRecordBatchStream,
physical_plan::stream::RecordBatchStreamAdapter,
sql::TableReference,
};
impl<T, P> DuckDBTable<T, P> {
fn create_federated_table_source(
self: Arc<Self>,
) -> DataFusionResult<Arc<dyn FederatedTableSource>> {
let table_name = self.base_table.table_reference.to_quoted_string();
let schema = Arc::clone(&Arc::clone(&self).base_table.schema());
let fed_provider = Arc::new(SQLFederationProvider::new(self));
Ok(Arc::new(SQLTableSource::new_with_schema(
fed_provider,
table_name,
schema,
)?))
}
pub fn create_federated_table_provider(
self: Arc<Self>,
) -> DataFusionResult<FederatedTableProviderAdaptor> {
let table_source = Self::create_federated_table_source(Arc::clone(&self))?;
Ok(FederatedTableProviderAdaptor::new_with_provider(
table_source,
self,
))
}
}
#[async_trait]
impl<T, P> SQLExecutor for DuckDBTable<T, P> {
fn name(&self) -> &str {
self.base_table.name()
}
fn compute_context(&self) -> Option<String> {
self.base_table.compute_context()
}
fn dialect(&self) -> Arc<dyn Dialect> {
self.base_table.dialect()
}
fn execute(
&self,
query: &str,
schema: SchemaRef,
) -> DataFusionResult<SendableRecordBatchStream> {
let fut = get_stream(
self.base_table.clone_pool(),
format!("{cte} {query}", cte = get_cte(&self.table_functions)),
Arc::clone(&schema),
);
let stream = futures::stream::once(fut).try_flatten();
Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream)))
}
async fn table_names(&self) -> DataFusionResult<Vec<String>> {
Err(DataFusionError::NotImplemented(
"table inference not implemented".to_string(),
))
}
async fn get_table_schema(&self, table_name: &str) -> DataFusionResult<SchemaRef> {
let conn = self
.base_table
.clone_pool()
.connect()
.await
.map_err(to_execution_error)?;
get_schema(conn, &TableReference::from(table_name))
.await
.boxed()
.map_err(|e| DbError::UnableToGetSchema { source: e })
.map_err(to_execution_error)
}
}