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

Improve CommonSubexprEliminate rule with surely and conditionally evaluated stats #11357

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 37 additions & 2 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Logical Expressions: [`Expr`]
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::mem;
Expand Down Expand Up @@ -1380,7 +1380,7 @@ impl Expr {
/// // refs contains "a" and "b"
/// assert_eq!(refs.len(), 2);
/// assert!(refs.contains(&Column::new_unqualified("a")));
/// assert!(refs.contains(&Column::new_unqualified("b")));
/// assert!(refs.contains(&Column::new_unqualified("b")));
/// ```
pub fn column_refs(&self) -> HashSet<&Column> {
let mut using_columns = HashSet::new();
Expand All @@ -1401,6 +1401,41 @@ impl Expr {
.expect("traversal is infallable");
}

/// Return all references to columns and their occurrence counts in the expression.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this new API makes sense to me as a parallel set of APIs for column_refs / add_column_refs

///
/// # Example
/// ```
/// # use std::collections::HashMap;
/// # use datafusion_common::Column;
/// # use datafusion_expr::col;
/// // For an expression `a + (b * a)`
/// let expr = col("a") + (col("b") * col("a"));
/// let mut refs = expr.column_refs_counts();
/// // refs contains "a" and "b"
/// assert_eq!(refs.len(), 2);
/// assert_eq!(*refs.get(&Column::new_unqualified("a")).unwrap(), 2);
/// assert_eq!(*refs.get(&Column::new_unqualified("b")).unwrap(), 1);
/// ```
pub fn column_refs_counts(&self) -> HashMap<&Column, usize> {
let mut map = HashMap::new();
self.add_column_ref_counts(&mut map);
map
}

/// Adds references to all columns and their occurrence counts in the expression to
/// the map.
///
/// See [`Self::column_refs`] for details
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// See [`Self::column_refs`] for details
/// See [`Self::column_refs_counts`] for details

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in 17f33f7.

pub fn add_column_ref_counts<'a>(&'a self, map: &mut HashMap<&'a Column, usize>) {
self.apply(|expr| {
if let Expr::Column(col) = expr {
*map.entry(col).or_default() += 1;
}
Ok(TreeNodeRecursion::Continue)
})
.expect("traversal is infallable");
}

/// Returns true if there are any column references in this Expr
pub fn any_column_refs(&self) -> bool {
self.exists(|expr| Ok(matches!(expr, Expr::Column(_))))
Expand Down
Loading
Loading