Skip to content

Manually implement PartialEq and Hash for BinaryExpr #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,43 @@ use kernels::{
};

/// Binary expression
#[derive(Debug, Hash, Clone, Eq, PartialEq)]
pub struct BinaryExpr<DynPhysicalExpr: ?Sized = dyn PhysicalExpr> {
left: Arc<DynPhysicalExpr>,
#[derive(Debug, Clone, Eq)]
pub struct BinaryExpr {
left: Arc<dyn PhysicalExpr>,
op: Operator,
right: Arc<DynPhysicalExpr>,
right: Arc<dyn PhysicalExpr>,
/// Specifies whether an error is returned on overflow or not
fail_on_overflow: bool,
}

// Manaully derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808
//
// Without this workaround the derive macro generates code with the following issue:
// error[E0507]: cannot move out of `other.left` which is behind a shared reference
// --> datafusion/physical-expr/src/expressions/binary.rs:53:5
// |
// 51 | #[derive(Debug, Hash, Clone, Eq, PartialEq)]
// | --------- in this derive macro expansion
// 52 | pub struct BinaryExpr {
// 53 | left: Arc<dyn PhysicalExpr>,
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `other.left` has type `Arc<dyn PhysicalExpr>`, which does not implement the `Copy` trait
impl PartialEq for BinaryExpr {
fn eq(&self, other: &Self) -> bool {
self.left.eq(&other.left)
&& self.op.eq(&other.op)
&& self.right.eq(&other.right)
&& self.fail_on_overflow.eq(&other.fail_on_overflow)
}
}
impl Hash for BinaryExpr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.left.hash(state);
self.op.hash(state);
self.right.hash(state);
self.fail_on_overflow.hash(state);
}
}

impl BinaryExpr {
/// Create new binary expression
pub fn new(
Expand Down