Skip to content

Commit

Permalink
feat(rust): auto select compat_level in PySeries::into_py
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Jul 7, 2024
1 parent ddd0467 commit 7a7ac9f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pyo3-polars-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn create_field_function(

match result {
Ok(out) => {
let out = polars_core::export::arrow::ffi::export_field_to_c(&out.to_arrow(true));
let out = polars_core::export::arrow::ffi::export_field_to_c(&out.to_arrow(CompatLevel::newest()));
*return_value = out;
},
Err(err) => {
Expand Down Expand Up @@ -278,7 +278,7 @@ fn create_field_function_from_with_dtype(
let mapper = polars_plan::dsl::FieldsMapper::new(&inputs);
let dtype = polars_core::datatypes::DataType::#dtype;
let out = mapper.with_dtype(dtype).unwrap();
let out = polars_core::export::arrow::ffi::export_field_to_c(&out.to_arrow(true));
let out = polars_core::export::arrow::ffi::export_field_to_c(&out.to_arrow(CompatLevel::newest()));
*return_value = out;
}
)
Expand Down
8 changes: 2 additions & 6 deletions pyo3-polars/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ impl std::convert::From<PyPolarsErr> for PyErr {
PolarsError::StringCacheMismatch(err) => {
StringCacheMismatchError::new_err(err.to_string())
}
PolarsError::SQLInterface(err) => {
SQLInterface::new_err(err.to_string())
},
PolarsError::SQLSyntax(err) => {
SQLSyntax::new_err(err.to_string())
}
PolarsError::SQLInterface(err) => SQLInterface::new_err(err.to_string()),
PolarsError::SQLSyntax(err) => SQLSyntax::new_err(err.to_string()),
PolarsError::Context { error, .. } => convert(error),
}
}
Expand Down
12 changes: 10 additions & 2 deletions pyo3-polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,18 @@ impl IntoPy<PyObject> for PySeries {
match s.getattr("_import_from_c") {
// Go via polars
Ok(import_from_c) => {
// Get supported compatibility level
let compat_level = CompatLevel::with_level(
s.getattr("_newest_compat_level")
.map_or(1, |newest_compat_level| {
newest_compat_level.call0().unwrap().extract().unwrap()
}),
)
.unwrap_or(CompatLevel::newest());
// Prepare pointers on the heap.
let mut chunk_ptrs = Vec::with_capacity(self.0.n_chunks());
for i in 0..self.0.n_chunks() {
let array = self.0.to_arrow(i, true);
let array = self.0.to_arrow(i, compat_level);
let schema = Box::leak(Box::new(arrow::ffi::export_field_to_c(
&ArrowField::new("", array.data_type().clone(), true),
)));
Expand Down Expand Up @@ -210,7 +218,7 @@ impl IntoPy<PyObject> for PySeries {
Err(_) => {
let s = self.0.rechunk();
let name = s.name();
let arr = s.to_arrow(0, false);
let arr = s.to_arrow(0, CompatLevel::oldest());
let pyarrow = py.import_bound("pyarrow").expect("pyarrow not installed");

let arg = to_py_array(arr, py, pyarrow).unwrap();
Expand Down

0 comments on commit 7a7ac9f

Please sign in to comment.