Skip to content

Commit c321326

Browse files
committed
fix: standardize usage of TABLE_PROVIDER_CAPSULE_NAME to table_provider_capsule_name function
1 parent c164ecb commit c321326

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

examples/datafusion-ffi-example/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ edition = "2021"
2323
[dependencies]
2424
datafusion = { version = "49.0.2" }
2525
datafusion-ffi = { version = "49.0.2" }
26-
pyo3 = { version = "0.23", features = ["extension-module", "abi3", "abi3-py39"] }
26+
datafusion-python = { path = "../../" }
27+
pyo3 = { version = "0.25", features = [
28+
"extension-module",
29+
"abi3",
30+
"abi3-py39",
31+
] }
2732
arrow = { version = "55.0.0" }
2833
arrow-array = { version = "55.0.0" }
2934
arrow-schema = { 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
@@ -20,6 +20,7 @@ use arrow_schema::{DataType, Field, Schema};
2020
use datafusion::catalog::MemTable;
2121
use datafusion::error::{DataFusionError, Result as DataFusionResult};
2222
use datafusion_ffi::table_provider::FFI_TableProvider;
23+
use datafusion_python::utils::table_provider_capsule_name;
2324
use pyo3::exceptions::PyRuntimeError;
2425
use pyo3::types::PyCapsule;
2526
use pyo3::{pyclass, pymethods, Bound, PyResult, Python};
@@ -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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
use crate::dataset::Dataset;
1919
use crate::errors::{py_datafusion_err, to_datafusion_err, PyDataFusionError, PyDataFusionResult};
2020
use crate::utils::{
21-
get_tokio_runtime, try_table_provider_from_object, validate_pycapsule, wait_for_future,
21+
get_tokio_runtime, table_provider_capsule_name, try_table_provider_from_object,
22+
validate_pycapsule, wait_for_future,
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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ use crate::physical_plan::PyExecutionPlan;
5151
use crate::record_batch::PyRecordBatchStream;
5252
use crate::sql::logical::PyLogicalPlan;
5353
use crate::utils::{
54-
get_tokio_runtime, is_ipython_env, py_obj_to_scalar_value, validate_pycapsule, wait_for_future,
54+
get_tokio_runtime, is_ipython_env, py_obj_to_scalar_value, table_provider_capsule_name,
55+
validate_pycapsule, wait_for_future,
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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ use datafusion::{
2727
use datafusion_ffi::table_provider::{FFI_TableProvider, ForeignTableProvider};
2828
use pyo3::prelude::*;
2929
use pyo3::{exceptions::PyValueError, types::PyCapsule};
30+
use std::ffi::CString;
3031
use std::{
32+
ffi::CStr,
3133
future::Future,
3234
sync::{Arc, OnceLock},
3335
time::Duration,
3436
};
3537
use tokio::{runtime::Runtime, time::sleep};
38+
39+
/// Return a static CStr for the PyCapsule name.
40+
///
41+
/// We create this lazily from a `CString` to avoid unsafe const
42+
/// initialization from a byte literal and to satisfy compiler lints.
43+
pub fn table_provider_capsule_name() -> &'static CStr {
44+
static NAME: OnceLock<CString> = OnceLock::new();
45+
NAME.get_or_init(|| CString::new("datafusion_table_provider").unwrap())
46+
.as_c_str()
47+
}
48+
49+
pub const TABLE_PROVIDER_CAPSULE_NAME_STR: &str = "datafusion_table_provider";
3650
/// Utility to get the Tokio Runtime from Python
3751
#[inline]
3852
pub(crate) fn get_tokio_runtime() -> &'static TokioRuntime {
@@ -125,7 +139,7 @@ pub(crate) fn validate_pycapsule(capsule: &Bound<PyCapsule>, name: &str) -> PyRe
125139
pub(crate) fn foreign_table_provider_from_capsule(
126140
capsule: &Bound<PyCapsule>,
127141
) -> PyResult<ForeignTableProvider> {
128-
validate_pycapsule(capsule, "datafusion_table_provider")?;
142+
validate_pycapsule(capsule, TABLE_PROVIDER_CAPSULE_NAME_STR)?;
129143
Ok(unsafe { capsule.reference::<FFI_TableProvider>() }.into())
130144
}
131145

0 commit comments

Comments
 (0)