Skip to content

Commit 67766e2

Browse files
committed
fix: standardize TABLE_PROVIDER_CAPSULE_NAME usage across modules for consistency
1 parent c164ecb commit 67766e2

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

examples/datafusion-ffi-example/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ edition = "2021"
2323
[dependencies]
2424
datafusion = { version = "49.0.2" }
2525
datafusion-ffi = { version = "49.0.2" }
26+
datafusion-python = { path = "../../" }
2627
pyo3 = { version = "0.23", features = ["extension-module", "abi3", "abi3-py39"] }
2728
arrow = { version = "55.0.0" }
2829
arrow-array = { version = "55.0.0" }

examples/datafusion-ffi-example/src/table_provider.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use arrow_array::{ArrayRef, RecordBatch};
1919
use arrow_schema::{DataType, Field, Schema};
2020
use datafusion::catalog::MemTable;
2121
use datafusion::error::{DataFusionError, Result as DataFusionResult};
22+
use datafusion_python::utils::TABLE_PROVIDER_CAPSULE_NAME;
2223
use datafusion_ffi::table_provider::FFI_TableProvider;
2324
use pyo3::exceptions::PyRuntimeError;
2425
use pyo3::types::PyCapsule;
@@ -91,13 +92,11 @@ impl MyTableProvider {
9192
&self,
9293
py: Python<'py>,
9394
) -> PyResult<Bound<'py, PyCapsule>> {
94-
let name = cr"datafusion_table_provider".into();
95-
9695
let provider = self
9796
.create_table()
9897
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
9998
let provider = FFI_TableProvider::new(Arc::new(provider), false, None);
10099

101-
PyCapsule::new(py, provider, Some(name))
100+
PyCapsule::new(py, provider, Some(TABLE_PROVIDER_CAPSULE_NAME.to_owned()))
102101
}
103102
}

src/catalog.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::dataset::Dataset;
1919
use crate::errors::{py_datafusion_err, to_datafusion_err, PyDataFusionError, PyDataFusionResult};
2020
use crate::utils::{
2121
get_tokio_runtime, try_table_provider_from_object, validate_pycapsule, wait_for_future,
22+
TABLE_PROVIDER_CAPSULE_NAME,
2223
};
2324
use async_trait::async_trait;
2425
use datafusion::catalog::{MemoryCatalogProvider, MemorySchemaProvider};
@@ -36,7 +37,6 @@ use pyo3::types::PyCapsule;
3637
use pyo3::IntoPyObjectExt;
3738
use std::any::Any;
3839
use std::collections::HashSet;
39-
use std::ffi::CString;
4040
use std::sync::Arc;
4141

4242
#[pyclass(name = "RawCatalog", module = "datafusion.catalog", subclass)]
@@ -257,14 +257,13 @@ impl PyTable {
257257
&self,
258258
py: Python<'py>,
259259
) -> PyResult<Bound<'py, PyCapsule>> {
260-
let name = CString::new("datafusion_table_provider").unwrap();
261260
let runtime = get_tokio_runtime().0.handle().clone();
262261

263262
let provider = Arc::clone(&self.table);
264263
let provider: Arc<dyn TableProvider + Send> = provider;
265264
let provider = FFI_TableProvider::new(provider, false, Some(runtime));
266265

267-
PyCapsule::new(py, provider, Some(name.clone()))
266+
PyCapsule::new(py, provider, Some(TABLE_PROVIDER_CAPSULE_NAME.to_owned()))
268267
}
269268

270269
fn __repr__(&self) -> PyResult<String> {

src/dataframe.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use crate::record_batch::PyRecordBatchStream;
5252
use crate::sql::logical::PyLogicalPlan;
5353
use crate::utils::{
5454
get_tokio_runtime, is_ipython_env, py_obj_to_scalar_value, validate_pycapsule, wait_for_future,
55+
TABLE_PROVIDER_CAPSULE_NAME,
5556
};
5657
use crate::{
5758
errors::PyDataFusionResult,
@@ -83,12 +84,10 @@ impl PyTableProvider {
8384
&self,
8485
py: Python<'py>,
8586
) -> PyResult<Bound<'py, PyCapsule>> {
86-
let name = CString::new("datafusion_table_provider").unwrap();
87-
8887
let runtime = get_tokio_runtime().0.handle().clone();
8988
let provider = FFI_TableProvider::new(Arc::clone(&self.provider), false, Some(runtime));
9089

91-
PyCapsule::new(py, provider, Some(name.clone()))
90+
PyCapsule::new(py, provider, Some(TABLE_PROVIDER_CAPSULE_NAME.to_owned()))
9291
}
9392
}
9493

src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ use datafusion_ffi::table_provider::{FFI_TableProvider, ForeignTableProvider};
2828
use pyo3::prelude::*;
2929
use pyo3::{exceptions::PyValueError, types::PyCapsule};
3030
use std::{
31+
ffi::CStr,
3132
future::Future,
3233
sync::{Arc, OnceLock},
3334
time::Duration,
3435
};
3536
use tokio::{runtime::Runtime, time::sleep};
37+
38+
pub const TABLE_PROVIDER_CAPSULE_NAME: &CStr =
39+
unsafe { CStr::from_bytes_with_nul_unchecked(b"datafusion_table_provider\0") };
40+
41+
pub const TABLE_PROVIDER_CAPSULE_NAME_STR: &str = "datafusion_table_provider";
3642
/// Utility to get the Tokio Runtime from Python
3743
#[inline]
3844
pub(crate) fn get_tokio_runtime() -> &'static TokioRuntime {
@@ -125,7 +131,7 @@ pub(crate) fn validate_pycapsule(capsule: &Bound<PyCapsule>, name: &str) -> PyRe
125131
pub(crate) fn foreign_table_provider_from_capsule(
126132
capsule: &Bound<PyCapsule>,
127133
) -> PyResult<ForeignTableProvider> {
128-
validate_pycapsule(capsule, "datafusion_table_provider")?;
134+
validate_pycapsule(capsule, TABLE_PROVIDER_CAPSULE_NAME_STR)?;
129135
Ok(unsafe { capsule.reference::<FFI_TableProvider>() }.into())
130136
}
131137

0 commit comments

Comments
 (0)