Skip to content

Commit 3e44cab

Browse files
authored
refactor: Implement Hash and use SpecialEq for RenameAliasFn (#22989)
1 parent baf06d8 commit 3e44cab

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

crates/polars-plan/src/dsl/expr/expr_dyn_fn.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ impl<T: ?Sized> PartialEq for SpecialEq<Arc<T>> {
8888
}
8989
}
9090

91-
impl<T> Eq for SpecialEq<Arc<T>> {}
91+
impl<T: ?Sized> Eq for SpecialEq<Arc<T>> {}
92+
93+
impl<T: ?Sized> Hash for SpecialEq<Arc<T>> {
94+
fn hash<H: Hasher>(&self, state: &mut H) {
95+
Arc::as_ptr(self).hash(state);
96+
}
97+
}
9298

9399
impl PartialEq for SpecialEq<Series> {
94100
fn eq(&self, other: &Self) -> bool {

crates/polars-plan/src/dsl/expr/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,10 @@ impl Hash for Expr {
361361
input.hash(state);
362362
excl.hash(state);
363363
},
364-
Expr::RenameAlias { function: _, expr } => expr.hash(state),
364+
Expr::RenameAlias { function, expr } => {
365+
function.hash(state);
366+
expr.hash(state);
367+
},
365368
Expr::AnonymousFunction {
366369
input,
367370
function: _,
@@ -596,7 +599,7 @@ impl Operator {
596599
}
597600
}
598601

599-
#[derive(Clone, PartialEq)]
602+
#[derive(Clone, PartialEq, Eq, Hash)]
600603
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
601604
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
602605
pub enum RenameAliasFn {
@@ -605,7 +608,7 @@ pub enum RenameAliasFn {
605608
ToLowercase,
606609
ToUppercase,
607610
#[cfg(feature = "python")]
608-
Python(Arc<polars_utils::python_function::PythonObject>),
611+
Python(SpecialEq<Arc<polars_utils::python_function::PythonObject>>),
609612
#[cfg_attr(any(feature = "serde", feature = "dsl-schema"), serde(skip))]
610613
Rust(SpecialEq<Arc<RenameAliasRustFn>>),
611614
}
@@ -620,20 +623,14 @@ impl RenameAliasFn {
620623
#[cfg(feature = "python")]
621624
Self::Python(lambda) => {
622625
let name = name.as_str();
623-
let out = pyo3::marker::Python::with_gil(|py| {
626+
pyo3::marker::Python::with_gil(|py| {
624627
let out: PlSmallStr = lambda
625628
.call1(py, (name,))?
626629
.extract::<std::borrow::Cow<str>>(py)?
627630
.as_ref()
628631
.into();
629632
pyo3::PyResult::<_>::Ok(out)
630-
});
631-
match out {
632-
Ok(out) => format_pl_smallstr!("{}", out),
633-
Err(e) => {
634-
polars_bail!(ComputeError: "Python function in 'name.map' produced an error: {e}.")
635-
},
636-
}
633+
}).map_err(|e| polars_err!(ComputeError: "Python function in 'name.map' produced an error: {e}."))?
637634
},
638635
Self::Rust(f) => f(name)?,
639636
};

crates/polars-plan/src/dsl/name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl ExprNameNameSpace {
4141
pub fn map_udf(self, function: polars_utils::python_function::PythonObject) -> Expr {
4242
Expr::RenameAlias {
4343
expr: Arc::new(self.0),
44-
function: RenameAliasFn::Python(Arc::new(function)),
44+
function: RenameAliasFn::Python(SpecialEq::new(Arc::new(function))),
4545
}
4646
}
4747

0 commit comments

Comments
 (0)