Skip to content
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

Minor: use hashmap for physical_exprs_contains and move PhysicalExprRef to physical-expr-common #14081

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions datafusion/physical-expr-common/src/physical_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use datafusion_expr_common::columnar_value::ColumnarValue;
use datafusion_expr_common::interval_arithmetic::Interval;
use datafusion_expr_common::sort_properties::ExprProperties;

/// Shared [`PhysicalExpr`].
pub type PhysicalExprRef = Arc<dyn PhysicalExpr>;

/// [`PhysicalExpr`]s represent expressions such as `A + 1` or `CAST(c1 AS int)`.
///
/// `PhysicalExpr` knows its type, nullability and can be evaluated directly on
Expand Down
30 changes: 11 additions & 19 deletions datafusion/physical-expr/src/physical_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

use std::sync::Arc;

use datafusion_common::HashMap;
pub(crate) use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
pub use datafusion_physical_expr_common::physical_expr::PhysicalExprRef;
use itertools::izip;

/// Shared [`PhysicalExpr`].
pub type PhysicalExprRef = Arc<dyn PhysicalExpr>;

/// This function is similar to the `contains` method of `Vec`. It finds
/// whether `expr` is among `physical_exprs`.
pub fn physical_exprs_contains(
Expand All @@ -48,31 +47,24 @@ pub fn physical_exprs_bag_equal(
lhs: &[Arc<dyn PhysicalExpr>],
rhs: &[Arc<dyn PhysicalExpr>],
) -> bool {
// TODO: Once we can use `HashMap`s with `Arc<dyn PhysicalExpr>`, this
// function should use a `HashMap` to reduce computational complexity.
if lhs.len() == rhs.len() {
let mut rhs_vec = rhs.to_vec();
for expr in lhs {
if let Some(idx) = rhs_vec.iter().position(|e| expr.eq(e)) {
rhs_vec.swap_remove(idx);
} else {
return false;
}
}
true
} else {
false
let mut multi_set_lhs: HashMap<_, usize> = HashMap::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let mut multi_set_rhs: HashMap<_, usize> = HashMap::new();
for expr in lhs {
*multi_set_lhs.entry(expr).or_insert(0) += 1;
}
for expr in rhs {
*multi_set_rhs.entry(expr).or_insert(0) += 1;
}
multi_set_lhs == multi_set_rhs
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;

use crate::expressions::{Column, Literal};
use crate::physical_expr::{
physical_exprs_bag_equal, physical_exprs_contains, physical_exprs_equal,
PhysicalExpr,
};

use datafusion_common::ScalarValue;
Expand Down
Loading