|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use pyo3::{pyclass, pymethods, Bound, PyResult, Python}; |
| 19 | +use std::{any::Any, fmt::Debug, sync::Arc}; |
| 20 | + |
| 21 | +use arrow::datatypes::Schema; |
| 22 | +use async_trait::async_trait; |
| 23 | +use datafusion::{ |
| 24 | + catalog::{ |
| 25 | + CatalogProvider, MemoryCatalogProvider, MemorySchemaProvider, SchemaProvider, TableProvider, |
| 26 | + }, |
| 27 | + common::exec_err, |
| 28 | + datasource::MemTable, |
| 29 | + error::{DataFusionError, Result}, |
| 30 | +}; |
| 31 | +use datafusion_ffi::catalog_provider::FFI_CatalogProvider; |
| 32 | +use pyo3::types::PyCapsule; |
| 33 | + |
| 34 | +pub fn my_table() -> Arc<dyn TableProvider + 'static> { |
| 35 | + use arrow::datatypes::{DataType, Field}; |
| 36 | + use datafusion::common::record_batch; |
| 37 | + |
| 38 | + let schema = Arc::new(Schema::new(vec![ |
| 39 | + Field::new("units", DataType::Int32, true), |
| 40 | + Field::new("price", DataType::Float64, true), |
| 41 | + ])); |
| 42 | + |
| 43 | + let partitions = vec![ |
| 44 | + record_batch!( |
| 45 | + ("units", Int32, vec![10, 20, 30]), |
| 46 | + ("price", Float64, vec![1.0, 2.0, 5.0]) |
| 47 | + ) |
| 48 | + .unwrap(), |
| 49 | + record_batch!( |
| 50 | + ("units", Int32, vec![5, 7]), |
| 51 | + ("price", Float64, vec![1.5, 2.5]) |
| 52 | + ) |
| 53 | + .unwrap(), |
| 54 | + ]; |
| 55 | + |
| 56 | + Arc::new(MemTable::try_new(schema, vec![partitions]).unwrap()) |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Debug)] |
| 60 | +pub struct FixedSchemaProvider { |
| 61 | + inner: MemorySchemaProvider, |
| 62 | +} |
| 63 | + |
| 64 | +impl Default for FixedSchemaProvider { |
| 65 | + fn default() -> Self { |
| 66 | + let inner = MemorySchemaProvider::new(); |
| 67 | + |
| 68 | + let table = my_table(); |
| 69 | + |
| 70 | + let _ = inner.register_table("my_table".to_string(), table).unwrap(); |
| 71 | + |
| 72 | + Self { inner } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[async_trait] |
| 77 | +impl SchemaProvider for FixedSchemaProvider { |
| 78 | + fn as_any(&self) -> &dyn Any { |
| 79 | + self |
| 80 | + } |
| 81 | + |
| 82 | + fn table_names(&self) -> Vec<String> { |
| 83 | + self.inner.table_names() |
| 84 | + } |
| 85 | + |
| 86 | + async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>, DataFusionError> { |
| 87 | + self.inner.table(name).await |
| 88 | + } |
| 89 | + |
| 90 | + fn register_table( |
| 91 | + &self, |
| 92 | + name: String, |
| 93 | + table: Arc<dyn TableProvider>, |
| 94 | + ) -> Result<Option<Arc<dyn TableProvider>>> { |
| 95 | + self.inner.register_table(name, table) |
| 96 | + } |
| 97 | + |
| 98 | + fn deregister_table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> { |
| 99 | + self.inner.deregister_table(name) |
| 100 | + } |
| 101 | + |
| 102 | + fn table_exist(&self, name: &str) -> bool { |
| 103 | + self.inner.table_exist(name) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// This catalog provider is intended only for unit tests. It prepopulates with one |
| 108 | +/// schema and only allows for schemas named after four types of fruit. |
| 109 | +#[pyclass( |
| 110 | + name = "MyCatalogProvider", |
| 111 | + module = "datafusion_ffi_example", |
| 112 | + subclass |
| 113 | +)] |
| 114 | +#[derive(Debug)] |
| 115 | +pub(crate) struct MyCatalogProvider { |
| 116 | + inner: MemoryCatalogProvider, |
| 117 | +} |
| 118 | + |
| 119 | +impl Default for MyCatalogProvider { |
| 120 | + fn default() -> Self { |
| 121 | + let inner = MemoryCatalogProvider::new(); |
| 122 | + |
| 123 | + let schema_name: &str = "my_schema"; |
| 124 | + let _ = inner.register_schema(schema_name, Arc::new(FixedSchemaProvider::default())); |
| 125 | + |
| 126 | + Self { inner } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl CatalogProvider for MyCatalogProvider { |
| 131 | + fn as_any(&self) -> &dyn Any { |
| 132 | + self |
| 133 | + } |
| 134 | + |
| 135 | + fn schema_names(&self) -> Vec<String> { |
| 136 | + self.inner.schema_names() |
| 137 | + } |
| 138 | + |
| 139 | + fn schema(&self, name: &str) -> Option<Arc<dyn SchemaProvider>> { |
| 140 | + self.inner.schema(name) |
| 141 | + } |
| 142 | + |
| 143 | + fn register_schema( |
| 144 | + &self, |
| 145 | + name: &str, |
| 146 | + schema: Arc<dyn SchemaProvider>, |
| 147 | + ) -> Result<Option<Arc<dyn SchemaProvider>>> { |
| 148 | + self.inner.register_schema(name, schema) |
| 149 | + } |
| 150 | + |
| 151 | + fn deregister_schema( |
| 152 | + &self, |
| 153 | + name: &str, |
| 154 | + cascade: bool, |
| 155 | + ) -> Result<Option<Arc<dyn SchemaProvider>>> { |
| 156 | + self.inner.deregister_schema(name, cascade) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +#[pymethods] |
| 161 | +impl MyCatalogProvider { |
| 162 | + #[new] |
| 163 | + pub fn new() -> Self { |
| 164 | + Self { |
| 165 | + inner: Default::default(), |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + pub fn __datafusion_catalog_provider__<'py>( |
| 170 | + &self, |
| 171 | + py: Python<'py>, |
| 172 | + ) -> PyResult<Bound<'py, PyCapsule>> { |
| 173 | + let name = cr"datafusion_catalog_provider".into(); |
| 174 | + let catalog_provider = |
| 175 | + FFI_CatalogProvider::new(Arc::new(MyCatalogProvider::default()), None); |
| 176 | + |
| 177 | + PyCapsule::new(py, catalog_provider, Some(name)) |
| 178 | + } |
| 179 | +} |
0 commit comments