From dfe3631627f28e148786f9eaa107fdc30e2889dc Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Wed, 8 Nov 2023 12:17:00 -0500 Subject: [PATCH 1/2] Minor: use Expr::alias --- datafusion/optimizer/src/decorrelate.rs | 6 ++---- datafusion/optimizer/src/single_distinct_to_groupby.rs | 10 ++++------ datafusion/sql/src/select.rs | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/datafusion/optimizer/src/decorrelate.rs b/datafusion/optimizer/src/decorrelate.rs index b5cf73733896..f5a2582134ea 100644 --- a/datafusion/optimizer/src/decorrelate.rs +++ b/datafusion/optimizer/src/decorrelate.rs @@ -227,10 +227,8 @@ 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..66ed76d0de8e 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( + Ok(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()?, - ))) + )).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) => { From 890806ea9df791203b6bb9d268cac755b0ccb7e6 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Wed, 8 Nov 2023 12:51:56 -0500 Subject: [PATCH 2/2] fmt --- datafusion/optimizer/src/decorrelate.rs | 5 +++-- .../optimizer/src/single_distinct_to_groupby.rs | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/datafusion/optimizer/src/decorrelate.rs b/datafusion/optimizer/src/decorrelate.rs index f5a2582134ea..c8162683f39e 100644 --- a/datafusion/optimizer/src/decorrelate.rs +++ b/datafusion/optimizer/src/decorrelate.rs @@ -227,8 +227,9 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr { )?; if !expr_result_map_for_count_bug.is_empty() { // has count bug - let un_matched_row = Expr::Literal(ScalarValue::Boolean(Some(true))) - .alias(UN_MATCHED_ROW_INDICATOR); + 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 66ed76d0de8e..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}, + expr::AggregateFunction, logical_plan::{Aggregate, LogicalPlan}, Expr, }; @@ -169,13 +169,13 @@ impl OptimizerRule for SingleDistinctToGroupBy { ); } 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()?), - ) + 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()), })