Skip to content

Commit

Permalink
pyo3-polars 0.17 (Polars 0.43.0) (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Sep 12, 2024
1 parent c84f966 commit 60c487f
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 38 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ members = [
]

[workspace.dependencies]
polars = { version = "0.42.0", default-features = false }
polars-core = { version = "0.42.0", default-features = false }
polars-ffi = { version = "0.42.0", default-features = false }
polars-plan = { version = "0.42.0", default-feautres = false }
polars-lazy = { version = "0.42.0", default-features = false }
polars = { version = "0.43.0", default-features = false }
polars-core = { version = "0.43.0", default-features = false }
polars-ffi = { version = "0.43.0", default-features = false }
polars-plan = { version = "0.43.0", default-feautres = false }
polars-lazy = { version = "0.43.0", default-features = false }

[workspace.dependencies.arrow]
package = "polars-arrow"
version = "0.42.0"
version = "0.43.0"
path = "../polars/crates/polars-arrow"
default-features = false

Expand Down
10 changes: 5 additions & 5 deletions example/derive_expression/expression_lib/src/distances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ where
T::Native: Float,
{
let out: ChunkedArray<T> = start_lat
.into_iter()
.zip(start_long.into_iter())
.zip(end_lat.into_iter())
.zip(end_long.into_iter())
.iter()
.zip(start_long.iter())
.zip(end_lat.iter())
.zip(end_long.iter())
.map(|(((start_lat, start_long), end_lat), end_long)| {
let start_lat = start_lat?;
let start_long = start_long?;
Expand All @@ -90,5 +90,5 @@ where
})
.collect();

Ok(out.with_name(start_lat.name()))
Ok(out.with_name(start_lat.name().clone()))
}
8 changes: 4 additions & 4 deletions example/derive_expression/expression_lib/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn pig_latinnify_with_paralellism(
.collect();

Ok(
StringChunked::from_chunk_iter(ca.name(), chunks.into_iter().flatten())
StringChunked::from_chunk_iter(ca.name().clone(), chunks.into_iter().flatten())
.into_series(),
)
})
Expand Down Expand Up @@ -176,7 +176,7 @@ fn is_leap_year(input: &[Series]) -> PolarsResult<Series> {
let out: BooleanChunked = ca
.as_date_iter()
.map(|opt_dt| opt_dt.map(|dt| dt.leap_year()))
.collect_ca(ca.name());
.collect_ca(ca.name().clone());

Ok(out.into_series())
}
Expand All @@ -193,7 +193,7 @@ struct TimeZone {

fn convert_timezone(input_fields: &[Field], kwargs: TimeZone) -> PolarsResult<Field> {
FieldsMapper::new(input_fields).try_map_dtype(|dtype| match dtype {
DataType::Datetime(tu, _) => Ok(DataType::Datetime(*tu, Some(kwargs.tz.clone()))),
DataType::Datetime(tu, _) => Ok(DataType::Datetime(*tu, Some(kwargs.tz.into()))),
_ => polars_bail!(ComputeError: "expected datetime"),
})
}
Expand All @@ -206,6 +206,6 @@ fn change_time_zone(input: &[Series], kwargs: TimeZone) -> PolarsResult<Series>
let ca = input.datetime()?;

let mut out = ca.clone();
out.set_time_zone(kwargs.tz)?;
out.set_time_zone(kwargs.tz.into())?;
Ok(out.into_series())
}
2 changes: 1 addition & 1 deletion example/io_plugin/io_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl RandomSource {
.iter()
.map(|s| {
let s = s.0.lock().unwrap();
Field::new(s.name(), s.dtype())
Field::new(s.name().into(), s.dtype())
})
.collect::<Schema>();
PySchema(Arc::new(schema))
Expand Down
4 changes: 2 additions & 2 deletions example/io_plugin/io_plugin/src/samplers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
let v = self.d.sample(&mut self.rng);
out.push(v);
}
Series::from_vec(self.name(), out)
Series::from_vec(self.name().into(), out)
}
}

Expand Down Expand Up @@ -107,7 +107,7 @@ impl Sampler for BernoulliSample {
}

Series::from_arrow(
self.name(),
self.name().into(),
BooleanArray::from_data_default(bits.freeze(), None).boxed(),
)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion pyo3-polars-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyo3-polars-derive"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
1 change: 0 additions & 1 deletion pyo3-polars-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ fn create_expression_function(ast: syn::ItemFn) -> proc_macro2::TokenStream {
#quote_call

#quote_process_result
()
});

if panic_result.is_err() {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-polars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyo3-polars"
version = "0.16.1"
version = "0.17.0"
edition = "2021"
license = "MIT"
readme = "../README.md"
Expand All @@ -19,7 +19,7 @@ polars-ffi = { workspace = true, optional = true }
polars-lazy = { workspace = true, optional = true }
polars-plan = { workspace = true, optional = true }
pyo3 = "0.22.2"
pyo3-polars-derive = { version = "0.10.0", path = "../pyo3-polars-derive", optional = true }
pyo3-polars-derive = { version = "0.11.0", path = "../pyo3-polars-derive", optional = true }
serde = { version = "1", optional = true }
serde-pickle = { version = "1", optional = true }
thiserror = "1"
Expand Down
6 changes: 6 additions & 0 deletions pyo3-polars/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ impl PolarsAllocator {
}
}

impl Default for PolarsAllocator {
fn default() -> Self {
Self::new()
}
}

unsafe impl GlobalAlloc for PolarsAllocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
Expand Down
8 changes: 6 additions & 2 deletions pyo3-polars/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ thread_local! {
static LAST_ERROR: RefCell<CString> = RefCell::new(CString::default());
}

pub unsafe fn _parse_kwargs<'a, T>(kwargs: &'a [u8]) -> PolarsResult<T>
pub fn _parse_kwargs<'a, T>(kwargs: &'a [u8]) -> PolarsResult<T>
where
T: Deserialize<'a>,
{
Expand All @@ -30,12 +30,14 @@ pub fn _update_last_error(err: PolarsError) {
}

pub fn _set_panic() {
let msg = format!("PANIC");
let msg = "PANIC";
let msg = CString::new(msg).unwrap();
LAST_ERROR.with(|prev| *prev.borrow_mut() = msg)
}

#[no_mangle]
/// # Safety
/// FFI function, so unsafe
pub unsafe extern "C" fn _polars_plugin_get_last_error_message() -> *const std::os::raw::c_char {
LAST_ERROR.with(|prev| prev.borrow_mut().as_ptr())
}
Expand All @@ -53,6 +55,8 @@ fn start_up_init() {
}

#[no_mangle]
/// # Safety
/// FFI function, so unsafe
pub unsafe extern "C" fn _polars_plugin_get_version() -> u32 {
if !INIT.swap(true, Ordering::Relaxed) {
// Plugin version is is always called at least once.
Expand Down
4 changes: 2 additions & 2 deletions pyo3-polars/src/ffi/to_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub(crate) fn to_py_array(
pyarrow: Bound<'_, PyModule>,
) -> PyResult<PyObject> {
let schema = Box::new(ffi::export_field_to_c(&ArrowField::new(
"",
array.data_type().clone(),
"".into(),
array.dtype().clone(),
true,
)));
let array = Box::new(ffi::export_array_to_c(array));
Expand Down
2 changes: 1 addition & 1 deletion pyo3-polars/src/ffi/to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn array_to_rust(obj: &Bound<PyAny>) -> PyResult<ArrayRef> {

unsafe {
let field = ffi::import_field_from_c(schema.as_ref()).map_err(PyPolarsErr::from)?;
let array = ffi::import_array_from_c(*array, field.data_type).map_err(PyPolarsErr::from)?;
let array = ffi::import_array_from_c(*array, field.dtype).map_err(PyPolarsErr::from)?;
Ok(array)
}
}
24 changes: 13 additions & 11 deletions pyo3-polars/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl<'py> FromPyObject<'py> for PyField {
.str()?
.extract::<PyBackedStr>()?;
let dtype = ob.getattr(intern!(py, "dtype"))?.extract::<PyDataType>()?;
Ok(PyField(Field::new(&name, dtype.0)))
let name: &str = name.as_ref();
Ok(PyField(Field::new(name.into(), dtype.0)))
}
}

Expand Down Expand Up @@ -177,8 +178,9 @@ impl<'a> FromPyObject<'a> for PySeries {
}
let arr = ob.call_method("to_arrow", (), Some(&kwargs))?;
let arr = ffi::to_rust::array_to_rust(&arr)?;
let name = name.as_ref();
Ok(PySeries(
Series::try_from((&*name, arr)).map_err(PyPolarsErr::from)?,
Series::try_from((PlSmallStr::from(name), arr)).map_err(PyPolarsErr::from)?,
))
}
}
Expand Down Expand Up @@ -246,8 +248,8 @@ impl IntoPy<PyObject> for PySeries {
for i in 0..self.0.n_chunks() {
let array = self.0.to_arrow(i, compat_level);
let schema = Box::new(arrow::ffi::export_field_to_c(&ArrowField::new(
"",
array.data_type().clone(),
"".into(),
array.dtype().clone(),
true,
)));
let array = Box::new(arrow::ffi::export_array_to_c(array.clone()));
Expand All @@ -260,7 +262,7 @@ impl IntoPy<PyObject> for PySeries {

// Somehow we need to clone the Vec, because pyo3 doesn't accept a slice here.
let pyseries = import_arrow_from_c
.call1((self.0.name(), chunk_ptrs.clone()))
.call1((self.0.name().as_str(), chunk_ptrs.clone()))
.unwrap();
// Deallocate boxes
for (schema_ptr, array_ptr) in chunk_ptrs {
Expand All @@ -285,7 +287,7 @@ impl IntoPy<PyObject> for PySeries {
// Go via pyarrow
Err(_) => {
let s = self.0.rechunk();
let name = s.name();
let name = s.name().as_str();
let arr = s.to_arrow(0, CompatLevel::oldest());
let pyarrow = py.import_bound("pyarrow").expect("pyarrow not installed");

Expand Down Expand Up @@ -432,7 +434,7 @@ impl ToPyObject for PyDataType {
DataType::Datetime(tu, tz) => {
let datetime_class = pl.getattr(intern!(py, "Datetime")).unwrap();
datetime_class
.call1((tu.to_ascii(), tz.clone()))
.call1((tu.to_ascii(), tz.as_ref().map(|s| s.as_str())))
.unwrap()
.into()
}
Expand All @@ -459,7 +461,7 @@ impl ToPyObject for PyDataType {
// we should always have an initialized rev_map coming from rust
let categories = rev_map.as_ref().unwrap().get_categories();
let class = pl.getattr(intern!(py, "Enum")).unwrap();
let s = Series::from_arrow("category", categories.clone().boxed()).unwrap();
let s = Series::from_arrow("category".into(), categories.clone().boxed()).unwrap();
let series = to_series(py, PySeries(s));
return class.call1((series,)).unwrap().into();
}
Expand All @@ -469,7 +471,7 @@ impl ToPyObject for PyDataType {
let field_class = pl.getattr(intern!(py, "Field")).unwrap();
let iter = fields.iter().map(|fld| {
let name = fld.name().as_str();
let dtype = PyDataType(fld.data_type().clone()).to_object(py);
let dtype = PyDataType(fld.dtype().clone()).to_object(py);
field_class.call1((name, dtype)).unwrap()
});
let fields = PyList::new_bound(py, iter);
Expand Down Expand Up @@ -598,8 +600,8 @@ impl<'py> FromPyObject<'py> for PyDataType {
let time_unit = ob.getattr(intern!(py, "time_unit")).unwrap();
let time_unit = time_unit.extract::<PyTimeUnit>()?.0;
let time_zone = ob.getattr(intern!(py, "time_zone")).unwrap();
let time_zone = time_zone.extract()?;
DataType::Datetime(time_unit, time_zone)
let time_zone: Option<String> = time_zone.extract()?;
DataType::Datetime(time_unit, time_zone.map(PlSmallStr::from))
},
"Duration" => {
let time_unit = ob.getattr(intern!(py, "time_unit")).unwrap();
Expand Down

0 comments on commit 60c487f

Please sign in to comment.