forked from datafusion-contrib/datafusion-table-providers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfederation.rs
More file actions
108 lines (96 loc) · 3.53 KB
/
federation.rs
File metadata and controls
108 lines (96 loc) · 3.53 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::sql::db_connection_pool::{dbconnection::get_schema, JoinPushDown};
use async_trait::async_trait;
use datafusion_federation::sql::{
RemoteTableRef, SQLExecutor, SQLFederationProvider, SQLTableSource,
};
use datafusion_federation::{FederatedTableProviderAdaptor, FederatedTableSource};
use futures::TryStreamExt;
use snafu::prelude::*;
use std::sync::Arc;
use crate::sql::sql_provider_datafusion::{
get_stream, to_execution_error, SqlTable, UnableToGetSchemaSnafu,
};
use datafusion::{
arrow::datatypes::SchemaRef,
error::{DataFusionError, Result as DataFusionResult},
physical_plan::{stream::RecordBatchStreamAdapter, SendableRecordBatchStream},
sql::{
unparser::dialect::{DefaultDialect, Dialect},
TableReference,
},
};
impl<T, P> SqlTable<T, P> {
// Return the current memory location of the object as a unique identifier
fn unique_id(&self) -> usize {
std::ptr::from_ref(self) as usize
}
fn arc_dialect(&self) -> Arc<dyn Dialect + Send + Sync> {
match &self.dialect {
Some(dialect) => Arc::clone(dialect),
None => Arc::new(DefaultDialect {}),
}
}
fn create_federated_table_source(
self: Arc<Self>,
) -> DataFusionResult<Arc<dyn FederatedTableSource>> {
let table_reference = self.table_reference.clone();
let schema = Arc::clone(&self.schema);
let fed_provider = Arc::new(SQLFederationProvider::new(self));
Ok(Arc::new(SQLTableSource::new_with_schema(
fed_provider,
RemoteTableRef::from(table_reference),
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 SqlTable<T, P> {
fn name(&self) -> &str {
&self.name
}
fn compute_context(&self) -> Option<String> {
match self.pool.join_push_down() {
JoinPushDown::AllowedFor(context) => Some(context),
// Don't return None here - it will cause incorrect federation with other providers of the same name that also have a compute_context of None.
// Instead return a random string that will never match any other provider's context.
JoinPushDown::Disallow => Some(format!("{}", self.unique_id())),
}
}
fn dialect(&self) -> Arc<dyn Dialect> {
self.arc_dialect()
}
fn execute(
&self,
query: &str,
schema: SchemaRef,
) -> DataFusionResult<SendableRecordBatchStream> {
let fut = get_stream(
Arc::clone(&self.pool),
query.to_string(),
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.pool.connect().await.map_err(to_execution_error)?;
get_schema(conn, &TableReference::from(table_name))
.await
.context(UnableToGetSchemaSnafu)
.map_err(to_execution_error)
}
}