Skip to content

Commit

Permalink
Stop copying LogicalPlan and Exprs in OptimizeProjections
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed May 7, 2024
1 parent 826d51f commit ac5c77e
Show file tree
Hide file tree
Showing 5 changed files with 490 additions and 386 deletions.
47 changes: 47 additions & 0 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,13 +1087,60 @@ impl Expr {
}

/// Remove an alias from an expression if one exists.
///
/// If the expression is not an alias, the expression is returned unchanged.
/// This method does not remove aliases from nested expressions.
///
/// # Example
/// ```
/// # use datafusion_expr::col;
/// // `foo as "bar"` is unaliased to `foo`
/// let expr = col("foo").alias("bar");
/// assert_eq!(expr.unalias(), col("foo"));
///
/// // `foo as "bar" + baz` is not unaliased
/// let expr = col("foo").alias("bar") + col("baz");
/// assert_eq!(expr.clone().unalias(), expr);
///
/// // `foo as "bar" as "baz" is unalaised to foo as "bar"
/// let expr = col("foo").alias("bar").alias("baz");
/// assert_eq!(expr.unalias(), col("foo").alias("bar"));
/// ```
pub fn unalias(self) -> Expr {
match self {
Expr::Alias(alias) => *alias.expr,
_ => self,
}
}

/// Recursively potentially multiple aliases from an expression.
///
/// If the expression is not an alias, the expression is returned unchanged.
/// This method removes directly nested aliases, but not other nested
/// aliases.
///
/// # Example
/// ```
/// # use datafusion_expr::col;
/// // `foo as "bar"` is unaliased to `foo`
/// let expr = col("foo").alias("bar");
/// assert_eq!(expr.unalias_nested(), col("foo"));
///
/// // `foo as "bar" + baz` is not unaliased
/// let expr = col("foo").alias("bar") + col("baz");
/// assert_eq!(expr.clone().unalias_nested(), expr);
///
/// // `foo as "bar" as "baz" is unalaised to foo
/// let expr = col("foo").alias("bar").alias("baz");
/// assert_eq!(expr.unalias_nested(), col("foo"));
/// ```
pub fn unalias_nested(self) -> Expr {
match self {
Expr::Alias(alias) => alias.expr.unalias_nested(),
_ => self,
}
}

/// Return `self IN <list>` if `negated` is false, otherwise
/// return `self NOT IN <list>`.a
pub fn in_list(self, list: Vec<Expr>, negated: bool) -> Expr {
Expand Down
Loading

0 comments on commit ac5c77e

Please sign in to comment.