-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathtable_provider_factory.rs
More file actions
87 lines (76 loc) · 2.65 KB
/
table_provider_factory.rs
File metadata and controls
87 lines (76 loc) · 2.65 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::sync::Arc;
use async_trait::async_trait;
use datafusion_catalog::{Session, TableProvider, TableProviderFactory};
use datafusion_common::error::Result as DataFusionResult;
use datafusion_expr::CreateExternalTable;
use datafusion_ffi::table_provider_factory::FFI_TableProviderFactory;
use pyo3::types::PyCapsule;
use pyo3::{Bound, PyAny, PyResult, Python, pyclass, pymethods};
use crate::catalog_provider;
use crate::utils::ffi_logical_codec_from_pycapsule;
#[derive(Debug)]
pub(crate) struct ExampleTableProviderFactory {}
impl ExampleTableProviderFactory {
fn new() -> Self {
Self {}
}
}
#[async_trait]
impl TableProviderFactory for ExampleTableProviderFactory {
async fn create(
&self,
_state: &dyn Session,
_cmd: &CreateExternalTable,
) -> DataFusionResult<Arc<dyn TableProvider>> {
Ok(catalog_provider::my_table())
}
}
#[pyclass(
name = "MyTableProviderFactory",
module = "datafusion_ffi_example",
subclass
)]
#[derive(Debug)]
pub struct MyTableProviderFactory {
inner: Arc<ExampleTableProviderFactory>,
}
impl Default for MyTableProviderFactory {
fn default() -> Self {
let inner = Arc::new(ExampleTableProviderFactory::new());
Self { inner }
}
}
#[pymethods]
impl MyTableProviderFactory {
#[new]
pub fn new() -> Self {
Self::default()
}
pub fn __datafusion_table_provider_factory__<'py>(
&self,
py: Python<'py>,
codec: Bound<PyAny>,
) -> PyResult<Bound<'py, PyCapsule>> {
let name = cr"datafusion_table_provider_factory".into();
let codec = ffi_logical_codec_from_pycapsule(codec)?;
let factory = Arc::clone(&self.inner) as Arc<dyn TableProviderFactory + Send>;
let factory = FFI_TableProviderFactory::new_with_ffi_codec(factory, None, codec);
PyCapsule::new(py, factory, Some(name))
}
}