From a70369ca9a5f07168f3fa3ec93bef0b1b0141179 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Thu, 9 Nov 2023 00:45:53 -0500 Subject: [PATCH] Minor: use `Expr::alias` in a few places to make the code more concise (#8097) --- datafusion/optimizer/src/decorrelate.rs | 7 +++---- .../src/single_distinct_to_groupby.rs | 20 +++++++++---------- datafusion/sql/src/select.rs | 2 +- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/datafusion/optimizer/src/decorrelate.rs b/datafusion/optimizer/src/decorrelate.rs index b5cf73733896..c8162683f39e 100644 --- a/datafusion/optimizer/src/decorrelate.rs +++ b/datafusion/optimizer/src/decorrelate.rs @@ -227,10 +227,9 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr { )?; if !expr_result_map_for_count_bug.is_empty() { // has count bug - let un_matched_row = Expr::Alias(Alias::new( - Expr::Literal(ScalarValue::Boolean(Some(true))), - UN_MATCHED_ROW_INDICATOR.to_string(), - )); + let un_matched_row = + Expr::Literal(ScalarValue::Boolean(Some(true))) + .alias(UN_MATCHED_ROW_INDICATOR); // add the unmatched rows indicator to the Aggregation's group expressions missing_exprs.push(un_matched_row); } diff --git a/datafusion/optimizer/src/single_distinct_to_groupby.rs b/datafusion/optimizer/src/single_distinct_to_groupby.rs index 548f00b4138a..414217612d1e 100644 --- a/datafusion/optimizer/src/single_distinct_to_groupby.rs +++ b/datafusion/optimizer/src/single_distinct_to_groupby.rs @@ -25,7 +25,7 @@ use crate::{OptimizerConfig, OptimizerRule}; use datafusion_common::Result; use datafusion_expr::{ col, - expr::{AggregateFunction, Alias}, + expr::AggregateFunction, logical_plan::{Aggregate, LogicalPlan}, Expr, }; @@ -168,16 +168,14 @@ impl OptimizerRule for SingleDistinctToGroupBy { args[0].clone().alias(SINGLE_DISTINCT_ALIAS), ); } - Ok(Expr::Alias(Alias::new( - Expr::AggregateFunction(AggregateFunction::new( - fun.clone(), - vec![col(SINGLE_DISTINCT_ALIAS)], - false, // intentional to remove distinct here - filter.clone(), - order_by.clone(), - )), - aggr_expr.display_name()?, - ))) + Ok(Expr::AggregateFunction(AggregateFunction::new( + fun.clone(), + vec![col(SINGLE_DISTINCT_ALIAS)], + false, // intentional to remove distinct here + filter.clone(), + order_by.clone(), + )) + .alias(aggr_expr.display_name()?)) } _ => Ok(aggr_expr.clone()), }) diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs index 2062afabfc1a..e9a7941ab064 100644 --- a/datafusion/sql/src/select.rs +++ b/datafusion/sql/src/select.rs @@ -373,7 +373,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { &[&[plan.schema()]], &plan.using_columns()?, )?; - let expr = Expr::Alias(Alias::new(col, self.normalizer.normalize(alias))); + let expr = col.alias(self.normalizer.normalize(alias)); Ok(vec![expr]) } SelectItem::Wildcard(options) => {