Skip to content

Commit 5271ba2

Browse files
committed
Fixed PyO3 type mismatch by cloning Array/ChunkedArray types before unbinding and binding fresh copies when checking array-likeness, eliminating the Bound reference error
1 parent 7f363a7 commit 5271ba2

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/udaf.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,25 @@ impl RustAccumulator {
5252
}
5353
}
5454

55-
fn ensure_pyarrow_types(
56-
&mut self,
57-
py: Python<'_>,
58-
) -> PyResult<(Bound<'_, PyType>, Bound<'_, PyType>)> {
55+
fn ensure_pyarrow_types(&mut self, py: Python<'_>) -> PyResult<(Py<PyType>, Py<PyType>)> {
5956
if self.pyarrow_array_type.is_none() || self.pyarrow_chunked_array_type.is_none() {
6057
let pyarrow = PyModule::import(py, "pyarrow")?;
6158
let array_attr = pyarrow.getattr("Array")?;
6259
let array_type = array_attr.downcast::<PyType>()?;
6360
let chunked_array_attr = pyarrow.getattr("ChunkedArray")?;
6461
let chunked_array_type = chunked_array_attr.downcast::<PyType>()?;
65-
self.pyarrow_array_type = Some(array_type.unbind());
66-
self.pyarrow_chunked_array_type = Some(chunked_array_type.unbind());
62+
self.pyarrow_array_type = Some(array_type.clone().unbind());
63+
self.pyarrow_chunked_array_type = Some(chunked_array_type.clone().unbind());
6764
}
6865
Ok((
6966
self.pyarrow_array_type
7067
.as_ref()
7168
.expect("array type set")
72-
.bind(py),
69+
.clone_ref(py),
7370
self.pyarrow_chunked_array_type
7471
.as_ref()
7572
.expect("chunked array type set")
76-
.bind(py),
73+
.clone_ref(py),
7774
))
7875
}
7976

@@ -83,6 +80,8 @@ impl RustAccumulator {
8380
value: &Bound<'_, PyAny>,
8481
) -> PyResult<bool> {
8582
let (array_type, chunked_array_type) = self.ensure_pyarrow_types(py)?;
83+
let array_type = array_type.bind(py);
84+
let chunked_array_type = chunked_array_type.bind(py);
8685
Ok(value.is_instance(&array_type)? || value.is_instance(&chunked_array_type)?)
8786
}
8887
}

0 commit comments

Comments
 (0)