|
| 1 | +use arrow_array::{ArrayRef, Int32Array, RecordBatch}; |
| 2 | +use arrow_schema::{DataType, Field, Schema, SchemaRef}; |
| 3 | +use datafusion_catalog::{MemTable, Session, TableProvider, TableType}; |
| 4 | +use datafusion_common::Result as DataFusionResult; |
| 5 | +use datafusion_expr::Expr; |
| 6 | +use datafusion_ffi::table_provider::FFI_TableProvider; |
| 7 | +use datafusion_physical_plan::ExecutionPlan; |
| 8 | +use pyo3::types::PyCapsule; |
| 9 | +use pyo3::{pyclass, pymethods, Bound, PyResult, Python}; |
| 10 | +use std::any::Any; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +#[pyclass( |
| 14 | + name = "BlockingTableProvider", |
| 15 | + module = "datafusion_ffi_example", |
| 16 | + subclass |
| 17 | +)] |
| 18 | +#[derive(Clone)] |
| 19 | +pub(crate) struct BlockingTableProvider; |
| 20 | + |
| 21 | +#[pymethods] |
| 22 | +impl BlockingTableProvider { |
| 23 | + #[new] |
| 24 | + fn new() -> Self { |
| 25 | + Self |
| 26 | + } |
| 27 | + |
| 28 | + fn __datafusion_table_provider__<'py>( |
| 29 | + &self, |
| 30 | + py: Python<'py>, |
| 31 | + ) -> PyResult<Bound<'py, PyCapsule>> { |
| 32 | + let name = cr"datafusion_table_provider".into(); |
| 33 | + let provider = FFI_TableProvider::new(Arc::new(self.clone()), false, None); |
| 34 | + PyCapsule::new(py, provider, Some(name)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[async_trait::async_trait] |
| 39 | +impl TableProvider for BlockingTableProvider { |
| 40 | + fn as_any(&self) -> &dyn Any { |
| 41 | + self |
| 42 | + } |
| 43 | + |
| 44 | + fn schema(&self) -> SchemaRef { |
| 45 | + Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])) |
| 46 | + } |
| 47 | + |
| 48 | + fn table_type(&self) -> TableType { |
| 49 | + TableType::Base |
| 50 | + } |
| 51 | + |
| 52 | + async fn scan( |
| 53 | + &self, |
| 54 | + state: &dyn Session, |
| 55 | + projection: Option<&Vec<usize>>, |
| 56 | + _filters: &[Expr], |
| 57 | + _limit: Option<usize>, |
| 58 | + ) -> DataFusionResult<Arc<dyn ExecutionPlan>> { |
| 59 | + tokio::spawn_blocking(|| ()).await.unwrap(); |
| 60 | + let batch = RecordBatch::try_new( |
| 61 | + self.schema(), |
| 62 | + vec![Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef], |
| 63 | + )?; |
| 64 | + let table = MemTable::try_new(batch.schema(), vec![vec![batch]])?; |
| 65 | + table.scan(state, projection, &[], None).await |
| 66 | + } |
| 67 | +} |
0 commit comments