Apache DataFusion SQL Query Engine
ARROW-10808: [Rust][DataFusion] Support nested expressions in aggregations.
Daniel Russobf5c6fa675ca
diff --git a/rust/datafusion/src/logical_plan/expr.rs b/rust/datafusion/src/logical_plan/expr.rsindex ca72948f1a..388e41e1c5 100644--- a/rust/datafusion/src/logical_plan/expr.rs+++ b/rust/datafusion/src/logical_plan/expr.rs@@ -49,7 +49,7 @@ use std::collections::HashSet;/// # Ok(())/// # }/// ```-#[derive(Clone)]+#[derive(Clone, PartialEq)]pub enum Expr {/// An expression with a specific name.Alias(Box<Expr>, String),diff --git a/rust/datafusion/src/physical_plan/functions.rs b/rust/datafusion/src/physical_plan/functions.rsindex fb6c63b709..00e0c30aa0 100644--- a/rust/datafusion/src/physical_plan/functions.rs+++ b/rust/datafusion/src/physical_plan/functions.rs@@ -50,7 +50,7 @@ use fmt::{Debug, Formatter};use std::{fmt, str::FromStr, sync::Arc};/// A function's signature, which defines the function's supported argument types.-#[derive(Debug, Clone)]+#[derive(Debug, Clone, PartialEq)]pub enum Signature {/// arbitrary number of arguments of an common type out of a list of valid types// A function such as `concat` is `Variadic(vec![DataType::Utf8, DataType::LargeUtf8])`diff --git a/rust/datafusion/src/physical_plan/udaf.rs b/rust/datafusion/src/physical_plan/udaf.rsindex a55d2330a3..9abc2ce6b4 100644--- a/rust/datafusion/src/physical_plan/udaf.rs+++ b/rust/datafusion/src/physical_plan/udaf.rs@@ -64,6 +64,12 @@ impl Debug for AggregateUDF {}}+impl PartialEq for AggregateUDF {+ fn eq(&self, other: &Self) -> bool {+ self.name == other.name && self.signature == other.signature+ }+}+impl AggregateUDF {/// Create a new AggregateUDFpub fn new(diff --git a/rust/datafusion/src/physical_plan/udf.rs b/rust/datafusion/src/physical_plan/udf.rsindex 9ac4134e8b..b13fb27de8 100644--- a/rust/datafusion/src/physical_plan/udf.rs+++ b/rust/datafusion/src/physical_plan/udf.rs@@ -56,6 +56,12 @@ impl Debug for ScalarUDF {}}+impl PartialEq for ScalarUDF {+ fn eq(&self, other: &Self) -> bool {+ self.name == other.name && self.signature == other.signature+ }+}+impl ScalarUDF {/// Create a new ScalarUDFpub fn new(diff --git a/rust/datafusion/src/sql/mod.rs b/rust/datafusion/src/sql/mod.rsindex fb4163924c..456ad4c2e3 100644--- a/rust/datafusion/src/sql/mod.rs+++ b/rust/datafusion/src/sql/mod.rs@@ -20,3 +20,4 @@pub mod parser;pub mod planner;+mod utils;diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rsindex 4d469c8305..2d946cae6e 100644--- a/rust/datafusion/src/sql/planner.rs+++ b/rust/datafusion/src/sql/planner.rs@@ -50,6 +50,11 @@ use sqlparser::ast::{ColumnDef as SQLColumnDef, ColumnOption};use sqlparser::ast::{OrderByExpr, Statement};use sqlparser::parser::ParserError::ParserError;+use super::utils::{+ can_columns_satisfy_exprs, expand_wildcard, expr_as_column_expr,+ find_aggregate_exprs, find_column_exprs, rebase_expr,+};+/// The ContextProvider trait allows the query planner to obtain meta-data about tables and/// functions referenced in SQL statementspub trait ContextProvider {@@ -406,80 +411,112 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {};let plan = plan?;- let projection_expr: Vec<Expr> = select- .projection- .iter()- .map(|e| self.sql_select_to_rex(&e, &plan.schema()))- .collect::<Result<Vec<Expr>>>()?;+ // The SELECT expressions, with wildcards expanded.+ let select_exprs = self.prepare_select_exprs(&plan, &select.projection)?;- let aggr_expr: Vec<Expr> = projection_expr- .iter()- .filter(|e| is_aggregate_expr(e))- .cloned()- .collect();+ // All of the aggregate expressions (deduplicated).+ let aggr_exprs = find_aggregate_exprs(&select_exprs);- // apply projection or aggregate- let plan = if !select.group_by.is_empty() | !aggr_expr.is_empty() {- self.aggregate(&plan, projection_expr, &select.group_by, aggr_expr)?- } else {- self.project(&plan, projection_expr)?- };- Ok(plan)+ let (plan, select_exprs_post_aggr) =+ if !select.group_by.is_empty() || !aggr_exprs.is_empty() {+ self.aggregate(&plan, &select_exprs, &select.group_by, &aggr_exprs)?+ } else {+ (plan, select_exprs)+ };++ self.project(&plan, select_exprs_post_aggr, false)+ }++ /// Returns the `Expr`'s corresponding to a SQL query's SELECT expressions.+ ///+ /// Wildcards are expanded into the concrete list of columns.+ fn prepare_select_exprs(+ &self,+ plan: &LogicalPlan,+ projection: &Vec<SelectItem>,+ ) -> Result<Vec<Expr>> {+ let input_schema = plan.schema();++ Ok(projection+ .iter()+ .map(|expr| self.sql_select_to_rex(&expr, &input_schema))+ .collect::<Result<Vec<Expr>>>()?+ .iter()+ .flat_map(|expr| expand_wildcard(&expr, &input_schema))+ .collect::<Vec<Expr>>())}/// Wrap a plan in a projection- fn project(&self, input: &LogicalPlan, expr: Vec<Expr>) -> Result<LogicalPlan> {- LogicalPlanBuilder::from(input).project(expr)?.build()+ ///+ /// If the `force` argument is `false`, the projection is applied only when+ /// necessary, i.e., when the input fields are different than the+ /// projection. Note that if the input fields are the same, but out of+ /// order, the projection will be applied.+ fn project(+ &self,+ input: &LogicalPlan,+ expr: Vec<Expr>,+ force: bool,+ ) -> Result<LogicalPlan> {+ self.validate_schema_satisfies_exprs(&input.schema(), &expr)?;+ let plan = LogicalPlanBuilder::from(input).project(expr)?.build()?;++ let project = force+ || match input {+ LogicalPlan::TableScan { .. } => true,+ _ => plan.schema().fields() != input.schema().fields(),+ };++ if project {+ Ok(plan)+ } else {+ Ok(input.clone())+ }}- /// Wrap a plan in an aggregatefn aggregate(&self,input: &LogicalPlan,- projection_expr: Vec<Expr>,+ select_exprs: &Vec<Expr>,group_by: &Vec<SQLExpr>,- aggr_expr: Vec<Expr>,- ) -> Result<LogicalPlan> {- let group_expr: Vec<Expr> = group_by+ aggr_exprs: &Vec<Expr>,+ ) -> Result<(LogicalPlan, Vec<Expr>)> {+ let group_by_exprs = group_by.iter()- .map(|e| self.sql_to_rex(&e, &input.schema()))+ .map(|e| self.sql_to_rex(e, &input.schema())).collect::<Result<Vec<Expr>>>()?;- let group_by_count = group_expr.len();- let aggr_count = aggr_expr.len();-- if group_by_count + aggr_count != projection_expr.len() {- return Err(DataFusionError::Plan(- "Projection references non-aggregate values".to_owned(),- ));- }+ let aggr_projection_exprs = group_by_exprs+ .iter()+ .chain(aggr_exprs.iter())+ .cloned()+ .collect::<Vec<Expr>>();let plan = LogicalPlanBuilder::from(&input)- .aggregate(group_expr, aggr_expr)?+ .aggregate(group_by_exprs, aggr_exprs.clone())?.build()?;- // optionally wrap in projection to preserve final order of fields- let expected_columns: Vec<String> = projection_expr+ // After aggregation, these are all of the columns that will be+ // available to next phases of planning.+ let column_exprs_post_aggr = aggr_projection_exprs.iter()- .map(|e| e.name(input.schema()))- .collect::<Result<Vec<_>>>()?;- let columns: Vec<String> = plan- .schema()- .fields()+ .map(|expr| expr_as_column_expr(expr, input))+ .collect::<Result<Vec<Expr>>>()?;++ // Rewrite the SELECT expression to use the columns produced by the+ // aggregation.+ let select_exprs_post_aggr = select_exprs.iter()- .map(|f| f.name().clone())- .collect::<Vec<_>>();- if expected_columns != columns {- self.project(- &plan,- expected_columns- .iter()- .map(|c| Expr::Column(c.clone()))- .collect(),- )- } else {- Ok(plan)+ .map(|expr| rebase_expr(expr, &aggr_projection_exprs, input))+ .collect::<Result<Vec<Expr>>>()?;++ if !can_columns_satisfy_exprs(&column_exprs_post_aggr, &select_exprs_post_aggr)? {+ return Err(DataFusionError::Plan(+ "Projection references non-aggregate values".to_owned(),+ ));}++ Ok((plan, select_exprs_post_aggr))}/// Wrap a plan in a limit@@ -526,6 +563,29 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {LogicalPlanBuilder::from(&plan).sort(order_by_rex?)?.build()}+ /// Validate the schema provides all of the columns referenced in the expressions.+ fn validate_schema_satisfies_exprs(+ &self,+ schema: &DFSchema,+ exprs: &Vec<Expr>,+ ) -> Result<()> {+ find_column_exprs(exprs)+ .iter()+ .try_for_each(|col| match col {+ Expr::Column(name) => {+ schema.field_with_unqualified_name(&name).map_err(|_| {+ DataFusionError::Plan(format!(+ "Invalid identifier '{}' for schema {}",+ name,+ schema.to_string()+ ))+ })?;+ Ok(())+ }+ _ => Err(DataFusionError::Internal("Not a column".to_string())),+ })+ }+/// Generate a relational expression from a select SQL expressionfn sql_select_to_rex(&self, sql: &SelectItem, schema: &DFSchema) -> Result<Expr> {match sql {@@ -543,6 +603,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {/// Generate a relational expression from a SQL expressionpub fn sql_to_rex(&self, sql: &SQLExpr, schema: &DFSchema) -> Result<Expr> {+ let expr = self.sql_expr_to_logical_expr(sql)?;+ self.validate_schema_satisfies_exprs(schema, &vec![expr.clone()])?;+ Ok(expr)+ }++ fn sql_expr_to_logical_expr(&self, sql: &SQLExpr) -> Result<Expr> {match sql {SQLExpr::Value(Value::Number(n)) => match n.parse::<i64>() {Ok(n) => Ok(lit(n)),@@ -555,14 +621,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let var_names = vec![id.value.clone()];Ok(Expr::ScalarVariable(var_names))} else {- match schema.field_with_unqualified_name(&id.value) {- Ok(field) => Ok(Expr::Column(field.name().clone())),- Err(_) => Err(DataFusionError::Plan(format!(- "Invalid identifier '{}' for schema {}",- id,- schema.to_string()- ))),- }+ Ok(Expr::Column(id.value.to_string()))}}@@ -575,10 +634,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {if &var_names[0][0..1] == "@" {Ok(Expr::ScalarVariable(var_names))} else {- Err(DataFusionError::Plan(format!(- "Invalid compound identifier '{:?}' for schema {}",+ Err(DataFusionError::NotImplemented(format!(+ "Unsupported compound identifier '{:?}'",var_names,- schema.to_string())))}}@@ -592,20 +650,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {else_result,} => {let expr = if let Some(e) = operand {- Some(Box::new(self.sql_to_rex(e, schema)?))+ Some(Box::new(self.sql_expr_to_logical_expr(e)?))} else {None};let when_expr = conditions.iter()- .map(|e| self.sql_to_rex(e, schema))+ .map(|e| self.sql_expr_to_logical_expr(e)).collect::<Result<Vec<_>>>()?;let then_expr = results.iter()- .map(|e| self.sql_to_rex(e, schema))+ .map(|e| self.sql_expr_to_logical_expr(e)).collect::<Result<Vec<_>>>()?;let else_expr = if let Some(e) = else_result {- Some(Box::new(self.sql_to_rex(e, schema)?))+ Some(Box::new(self.sql_expr_to_logical_expr(e)?))} else {None};@@ -625,7 +683,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {ref expr,ref data_type,} => Ok(Expr::Cast {- expr: Box::new(self.sql_to_rex(&expr, schema)?),+ expr: Box::new(self.sql_expr_to_logical_expr(&expr)?),data_type: convert_data_type(data_type)?,}),@@ -638,18 +696,18 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {}),SQLExpr::IsNull(ref expr) => {- Ok(Expr::IsNull(Box::new(self.sql_to_rex(expr, schema)?)))+ Ok(Expr::IsNull(Box::new(self.sql_expr_to_logical_expr(expr)?)))}- SQLExpr::IsNotNull(ref expr) => {- Ok(Expr::IsNotNull(Box::new(self.sql_to_rex(expr, schema)?)))- }+ SQLExpr::IsNotNull(ref expr) => Ok(Expr::IsNotNull(Box::new(+ self.sql_expr_to_logical_expr(expr)?,+ ))),SQLExpr::UnaryOp { ref op, ref expr } => match op {UnaryOperator::Not => {- Ok(Expr::Not(Box::new(self.sql_to_rex(expr, schema)?)))+ Ok(Expr::Not(Box::new(self.sql_expr_to_logical_expr(expr)?)))}- UnaryOperator::Plus => Ok(self.sql_to_rex(expr, schema)?),+ UnaryOperator::Plus => Ok(self.sql_expr_to_logical_expr(expr)?),UnaryOperator::Minus => {match expr.as_ref() {// optimization: if it's a number literal, we applly the negative operator@@ -665,7 +723,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {})?)),},// not a literal, apply negative operator on expression- _ => Ok(Expr::Negative(Box::new(self.sql_to_rex(expr, schema)?))),+ _ => Ok(Expr::Negative(Box::new(self.sql_expr_to_logical_expr(expr)?))),}}},@@ -676,10 +734,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {ref low,ref high,} => Ok(Expr::Between {- expr: Box::new(self.sql_to_rex(&expr, &schema)?),+ expr: Box::new(self.sql_expr_to_logical_expr(&expr)?),negated: *negated,- low: Box::new(self.sql_to_rex(&low, &schema)?),- high: Box::new(self.sql_to_rex(&high, &schema)?),+ low: Box::new(self.sql_expr_to_logical_expr(&low)?),+ high: Box::new(self.sql_expr_to_logical_expr(&high)?),}),SQLExpr::BinaryOp {@@ -710,9 +768,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {}?;Ok(Expr::BinaryExpr {- left: Box::new(self.sql_to_rex(&left, &schema)?),+ left: Box::new(self.sql_expr_to_logical_expr(&left)?),op: operator,- right: Box::new(self.sql_to_rex(&right, &schema)?),+ right: Box::new(self.sql_expr_to_logical_expr(&right)?),})}@@ -724,7 +782,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let args = function.args.iter()- .map(|a| self.sql_to_rex(a, schema))+ .map(|a| self.sql_expr_to_logical_expr(a)).collect::<Result<Vec<Expr>>>()?;return Ok(Expr::ScalarFunction { fun, args });@@ -739,14 +797,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {.map(|a| match a {SQLExpr::Value(Value::Number(_)) => Ok(lit(1_u8)),SQLExpr::Wildcard => Ok(lit(1_u8)),- _ => self.sql_to_rex(a, schema),+ _ => self.sql_expr_to_logical_expr(a),}).collect::<Result<Vec<Expr>>>()?} else {function.args.iter()- .map(|a| self.sql_to_rex(a, schema))+ .map(|a| self.sql_expr_to_logical_expr(a)).collect::<Result<Vec<Expr>>>()?};@@ -763,7 +821,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let args = function.args.iter()- .map(|a| self.sql_to_rex(a, schema))+ .map(|a| self.sql_expr_to_logical_expr(a)).collect::<Result<Vec<Expr>>>()?;Ok(Expr::ScalarUDF { fun: fm, args })@@ -773,7 +831,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {let args = function.args.iter()- .map(|a| self.sql_to_rex(a, schema))+ .map(|a| self.sql_expr_to_logical_expr(a)).collect::<Result<Vec<Expr>>>()?;Ok(Expr::AggregateUDF { fun: fm, args })@@ -786,7 +844,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {}}- SQLExpr::Nested(e) => self.sql_to_rex(&e, &schema),+ SQLExpr::Nested(e) => self.sql_expr_to_logical_expr(&e),_ => Err(DataFusionError::NotImplemented(format!("Unsupported ast node {:?} in sqltorel",@@ -889,15 +947,6 @@ fn extract_possible_join_keys(}}-/// Determine if an expression is an aggregate expression or not-fn is_aggregate_expr(e: &Expr) -> bool {- match e {- Expr::AggregateFunction { .. } | Expr::AggregateUDF { .. } => true,- Expr::Alias(expr, _) => is_aggregate_expr(expr),- _ => false,- }-}-/// Convert SQL data type to relational representation of data typepub fn convert_data_type(sql: &SQLDataType) -> Result<DataType> {match sql {@@ -924,6 +973,9 @@ mod tests {use crate::{logical_plan::create_udf, sql::parser::DFParser};use functions::ScalarFunctionImplementation;+ const PERSON_COLUMN_NAMES: &str =+ "id, first_name, last_name, age, state, salary, birth_date";+#[test]fn select_no_relation() {quick_test(@@ -933,6 +985,48 @@ mod tests {);}+ #[test]+ fn select_column_does_not_exist() {+ let sql = "SELECT doesnotexist FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'doesnotexist\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_repeated_column() {+ let sql = "SELECT age, age FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#age\\\" at position 0 and \\\"#age\\\" at position 1 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_wildcard_with_repeated_column() {+ let sql = "SELECT *, age FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#age\\\" at position 3 and \\\"#age\\\" at position 7 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_wildcard_with_repeated_column_but_is_aliased() {+ quick_test(+ "SELECT *, first_name AS fn from person",+ "Projection: #id, #first_name, #last_name, #age, #state, #salary, #birth_date, #first_name AS fn\+ \n TableScan: person projection=None",+ );+ }+#[test]fn select_scalar_func_with_literal_no_relation() {quick_test(@@ -952,6 +1046,32 @@ mod tests {quick_test(sql, expected);}+ #[test]+ fn select_filter_column_does_not_exist() {+ let sql = "SELECT first_name FROM person WHERE doesnotexist = 'A'";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'doesnotexist\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_filter_cannot_use_alias() {+ let sql = "SELECT first_name AS x FROM person WHERE x = 'A'";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'x\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }+#[test]fn select_neg_filter() {let sql = "SELECT id, first_name, last_name \@@ -1062,11 +1182,12 @@ mod tests {WHERE age > 20)WHERE fn1 = 'X' AND age < 30";- let expected = "Projection: #fn1, #age\- \n Filter: #fn1 Eq Utf8(\"X\") And #age Lt Int64(30)\- \n Projection: #first_name AS fn1, #age\- \n Filter: #age Gt Int64(20)\- \n TableScan: person projection=None";++ let expected = "Filter: #fn1 Eq Utf8(\"X\") And #age Lt Int64(30)\+ \n Projection: #first_name AS fn1, #age\+ \n Filter: #age Gt Int64(20)\+ \n TableScan: person projection=None";+quick_test(sql, expected);}@@ -1086,6 +1207,21 @@ mod tests {quick_test(sql, expected);}+ #[test]+ fn select_wildcard_with_groupby() {+ quick_test(+ "SELECT * FROM person GROUP BY id, first_name, last_name, age, state, salary, birth_date",+ "Aggregate: groupBy=[[#id, #first_name, #last_name, #age, #state, #salary, #birth_date]], aggr=[[]]\+ \n TableScan: person projection=None",+ );+ quick_test(+ "SELECT * FROM (SELECT first_name, last_name FROM person) GROUP BY first_name, last_name",+ "Aggregate: groupBy=[[#first_name, #last_name]], aggr=[[]]\+ \n Projection: #first_name, #last_name\+ \n TableScan: person projection=None",+ );+ }+#[test]fn select_simple_aggregate() {quick_test(@@ -1104,6 +1240,59 @@ mod tests {);}+ #[test]+ fn select_simple_aggregate_column_does_not_exist() {+ let sql = "SELECT MIN(doesnotexist) FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'doesnotexist\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_repeated_aggregate() {+ let sql = "SELECT MIN(age), MIN(age) FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#MIN(age)\\\" at position 0 and \\\"#MIN(age)\\\" at position 1 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_repeated_aggregate_with_single_alias() {+ quick_test(+ "SELECT MIN(age), MIN(age) AS a FROM person",+ "Projection: #MIN(age), #MIN(age) AS a\+ \n Aggregate: groupBy=[[]], aggr=[[MIN(#age)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_repeated_aggregate_with_unique_aliases() {+ quick_test(+ "SELECT MIN(age) AS a, MIN(age) AS b FROM person",+ "Projection: #MIN(age) AS a, #MIN(age) AS b\+ \n Aggregate: groupBy=[[]], aggr=[[MIN(#age)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_repeated_aggregate_with_repeated_aliases() {+ let sql = "SELECT MIN(age) AS a, MIN(age) AS a FROM person";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#MIN(age) AS a\\\" at position 0 and \\\"#MIN(age) AS a\\\" at position 1 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }+#[test]fn select_simple_aggregate_with_groupby() {quick_test(@@ -1113,6 +1302,203 @@ mod tests {);}+ #[test]+ fn select_simple_aggregate_with_groupby_with_aliases() {+ quick_test(+ "SELECT state AS a, MIN(age) AS b FROM person GROUP BY state",+ "Projection: #state AS a, #MIN(age) AS b\+ \n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_with_aliases_repeated() {+ let sql = "SELECT state AS a, MIN(age) AS a FROM person GROUP BY state";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#state AS a\\\" at position 0 and \\\"#MIN(age) AS a\\\" at position 1 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_column_unselected() {+ quick_test(+ "SELECT MIN(age), MAX(age) FROM person GROUP BY state",+ "Projection: #MIN(age), #MAX(age)\+ \n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age), MAX(#age)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_and_column_in_group_by_does_not_exist() {+ let sql = "SELECT SUM(age) FROM person GROUP BY doesnotexist";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'doesnotexist\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_and_column_in_aggregate_does_not_exist() {+ let sql = "SELECT SUM(doesnotexist) FROM person GROUP BY first_name";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'doesnotexist\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_and_column_is_in_aggregate_and_groupby() {+ quick_test(+ "SELECT MAX(first_name) FROM person GROUP BY first_name",+ "Projection: #MAX(first_name)\+ \n Aggregate: groupBy=[[#first_name]], aggr=[[MAX(#first_name)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_cannot_use_alias() {+ let sql = "SELECT state AS x, MAX(age) FROM person GROUP BY x";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ format!(+ "Plan(\"Invalid identifier \\\'x\\\' for schema {}\")",+ PERSON_COLUMN_NAMES+ ),+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_aggregate_repeated() {+ let sql = "SELECT state, MIN(age), MIN(age) FROM person GROUP BY state";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projections require unique expression names but the expression \\\"#MIN(age)\\\" at position 1 and \\\"#MIN(age)\\\" at position 2 have the same name. Consider aliasing (\\\"AS\\\") one of them.\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_aggregate_repeated_and_one_has_alias() {+ quick_test(+ "SELECT state, MIN(age), MIN(age) AS ma FROM person GROUP BY state",+ "Projection: #state, #MIN(age), #MIN(age) AS ma\+ \n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age)]]\+ \n TableScan: person projection=None",+ )+ }+ #[test]+ fn select_simple_aggregate_with_groupby_non_column_expression_unselected() {+ quick_test(+ "SELECT MIN(first_name) FROM person GROUP BY age + 1",+ "Projection: #MIN(first_name)\+ \n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_non_column_expression_selected_and_resolvable(+ ) {+ quick_test(+ "SELECT age + 1, MIN(first_name) FROM person GROUP BY age + 1",+ "Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\+ \n TableScan: person projection=None",+ );+ quick_test(+ "SELECT MIN(first_name), age + 1 FROM person GROUP BY age + 1",+ "Projection: #MIN(first_name), #age Plus Int64(1)\+ \n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_non_column_expression_nested_and_resolvable()+ {+ quick_test(+ "SELECT ((age + 1) / 2) * (age + 1), MIN(first_name) FROM person GROUP BY age + 1",+ "Projection: #age Plus Int64(1) Divide Int64(2) Multiply #age Plus Int64(1), #MIN(first_name)\+ \n Aggregate: groupBy=[[#age Plus Int64(1)]], aggr=[[MIN(#first_name)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_non_column_expression_nested_and_not_resolvable(+ ) {+ // The query should fail, because age + 9 is not in the group by.+ let sql = "SELECT ((age + 1) / 2) * (age + 9), MIN(first_name) FROM person GROUP BY age + 1";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projection references non-aggregate values\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_with_groupby_non_column_expression_and_its_column_selected(+ ) {+ let sql = "SELECT age, MIN(first_name) FROM person GROUP BY age + 1";+ let err = logical_plan(sql).expect_err("query should have failed");+ assert_eq!(+ "Plan(\"Projection references non-aggregate values\")",+ format!("{:?}", err)+ );+ }++ #[test]+ fn select_simple_aggregate_nested_in_binary_expr_with_groupby() {+ quick_test(+ "SELECT state, MIN(age) < 10 FROM person GROUP BY state",+ "Projection: #state, #MIN(age) Lt Int64(10)\+ \n Aggregate: groupBy=[[#state]], aggr=[[MIN(#age)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_simple_aggregate_and_nested_groupby_column() {+ quick_test(+ "SELECT age + 1, MAX(first_name) FROM person GROUP BY age",+ "Projection: #age Plus Int64(1), #MAX(first_name)\+ \n Aggregate: groupBy=[[#age]], aggr=[[MAX(#first_name)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_aggregate_compounded_with_groupby_column() {+ quick_test(+ "SELECT age + MIN(salary) FROM person GROUP BY age",+ "Projection: #age Plus #MIN(salary)\+ \n Aggregate: groupBy=[[#age]], aggr=[[MIN(#salary)]]\+ \n TableScan: person projection=None",+ );+ }++ #[test]+ fn select_aggregate_with_non_column_inner_expression_with_groupby() {+ quick_test(+ "SELECT state, MIN(age + 1) FROM person GROUP BY state",+ "Aggregate: groupBy=[[#state]], aggr=[[MIN(#age Plus Int64(1))]]\+ \n TableScan: person projection=None",+ );+ }+#[test]fn test_wildcard() {quick_test(@@ -1226,6 +1612,25 @@ mod tests {quick_test(sql, expected);}+ #[test]+ fn select_group_by_columns_not_in_select() {+ let sql = "SELECT MAX(age) FROM person GROUP BY state";+ let expected = "Projection: #MAX(age)\+ \n Aggregate: groupBy=[[#state]], aggr=[[MAX(#age)]]\+ \n TableScan: person projection=None";++ quick_test(sql, expected);+ }++ #[test]+ fn select_group_by_count_star() {+ let sql = "SELECT state, COUNT(*) FROM person GROUP BY state";+ let expected = "Aggregate: groupBy=[[#state]], aggr=[[COUNT(UInt8(1))]]\+ \n TableScan: person projection=None";++ quick_test(sql, expected);+ }+#[test]fn select_group_by_needs_projection() {let sql = "SELECT COUNT(state), state FROM person GROUP BY state";@@ -1240,11 +1645,10 @@ mod tests {#[test]fn select_7480_1() {let sql = "SELECT c1, MIN(c12) FROM aggregate_test_100 GROUP BY c1, c13";- let err = logical_plan(sql).expect_err("query should have failed");- assert_eq!(- "Plan(\"Projection references non-aggregate values\")",- format!("{:?}", err)- );+ let expected = "Projection: #c1, #MIN(c12)\+ \n Aggregate: groupBy=[[#c1, #c13]], aggr=[[MIN(#c12)]]\+ \n TableScan: aggregate_test_100 projection=None";+ quick_test(sql, expected);}#[test]diff --git a/rust/datafusion/src/sql/utils.rs b/rust/datafusion/src/sql/utils.rsnew file mode 100644index 0000000000..0d12dddf41--- /dev/null+++ b/rust/datafusion/src/sql/utils.rs@@ -0,0 +1,364 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++use crate::error::{DataFusionError, Result};+use crate::logical_plan::{DFSchema, Expr, LogicalPlan};++/// Resolves an `Expr::Wildcard` to a collection of `Expr::Column`'s.+pub(crate) fn expand_wildcard(expr: &Expr, schema: &DFSchema) -> Vec<Expr> {+ match expr {+ Expr::Wildcard => schema+ .fields()+ .iter()+ .map(|f| Expr::Column(f.name().to_string()))+ .collect::<Vec<Expr>>(),+ _ => vec![expr.clone()],+ }+}++/// Collect all deeply nested `Expr::AggregateFunction` and+/// `Expr::AggregateUDF`. They are returned in order of occurrence (depth+/// first), with duplicates omitted.+pub(crate) fn find_aggregate_exprs(exprs: &Vec<Expr>) -> Vec<Expr> {+ find_exprs_in_exprs(exprs, &|nested_expr| {+ matches!(+ nested_expr,+ Expr::AggregateFunction { .. } | Expr::AggregateUDF { .. }+ )+ })+}++/// Collect all deeply nested `Expr::Column`'s. They are returned in order of+/// appearance (depth first), with duplicates omitted.+pub(crate) fn find_column_exprs(exprs: &Vec<Expr>) -> Vec<Expr> {+ find_exprs_in_exprs(exprs, &|nested_expr| matches!(nested_expr, Expr::Column(_)))+}++/// Search the provided `Expr`'s, and all of their nested `Expr`, for any that+/// pass the provided test. The returned `Expr`'s are deduplicated and returned+/// in order of appearance (depth first).+fn find_exprs_in_exprs<F>(exprs: &Vec<Expr>, test_fn: &F) -> Vec<Expr>+where+ F: Fn(&Expr) -> bool,+{+ exprs+ .iter()+ .flat_map(|expr| find_exprs_in_expr(expr, test_fn))+ .fold(vec![], |mut acc, expr| {+ if !acc.contains(&expr) {+ acc.push(expr)+ }+ acc+ })+}++/// Search an `Expr`, and all of its nested `Expr`'s, for any that pass the+/// provided test. The returned `Expr`'s are deduplicated and returned in order+/// of appearance (depth first).+fn find_exprs_in_expr<F>(expr: &Expr, test_fn: &F) -> Vec<Expr>+where+ F: Fn(&Expr) -> bool,+{+ let matched_exprs = if test_fn(expr) {+ vec![expr.clone()]+ } else {+ match expr {+ Expr::AggregateFunction { args, .. } => find_exprs_in_exprs(&args, test_fn),+ Expr::AggregateUDF { args, .. } => find_exprs_in_exprs(&args, test_fn),+ Expr::Alias(nested_expr, _) => {+ find_exprs_in_expr(nested_expr.as_ref(), test_fn)+ }+ Expr::Between {+ expr: nested_expr,+ low,+ high,+ ..+ } => {+ let mut matches = vec![];+ matches.extend(find_exprs_in_expr(nested_expr.as_ref(), test_fn));+ matches.extend(find_exprs_in_expr(low.as_ref(), test_fn));+ matches.extend(find_exprs_in_expr(high.as_ref(), test_fn));+ matches+ }+ Expr::BinaryExpr { left, right, .. } => {+ let mut matches = vec![];+ matches.extend(find_exprs_in_expr(left.as_ref(), test_fn));+ matches.extend(find_exprs_in_expr(right.as_ref(), test_fn));+ matches+ }+ Expr::Case {+ expr: case_expr_opt,+ when_then_expr,+ else_expr: else_expr_opt,+ } => {+ let mut matches = vec![];++ if let Some(case_expr) = case_expr_opt {+ matches.extend(find_exprs_in_expr(case_expr.as_ref(), test_fn));+ }++ matches.extend(+ when_then_expr+ .iter()+ .flat_map(|(a, b)| vec![a, b])+ .flat_map(|expr| find_exprs_in_expr(expr.as_ref(), test_fn))+ .collect::<Vec<Expr>>(),+ );++ if let Some(else_expr) = else_expr_opt {+ matches.extend(find_exprs_in_expr(else_expr.as_ref(), test_fn));+ }++ matches+ }+ Expr::Cast {+ expr: nested_expr, ..+ } => find_exprs_in_expr(nested_expr.as_ref(), test_fn),+ Expr::IsNotNull(nested_expr) => {+ find_exprs_in_expr(nested_expr.as_ref(), test_fn)+ }+ Expr::IsNull(nested_expr) => {+ find_exprs_in_expr(nested_expr.as_ref(), test_fn)+ }+ Expr::Negative(nested_expr) => {+ find_exprs_in_expr(nested_expr.as_ref(), test_fn)+ }+ Expr::Not(nested_expr) => find_exprs_in_expr(nested_expr.as_ref(), test_fn),+ Expr::ScalarFunction { args, .. } => find_exprs_in_exprs(&args, test_fn),+ Expr::ScalarUDF { args, .. } => find_exprs_in_exprs(&args, test_fn),+ Expr::Sort {+ expr: nested_expr, ..+ } => find_exprs_in_expr(nested_expr.as_ref(), test_fn),++ // These expressions don't nest other expressions.+ Expr::Column(_)+ | Expr::Literal(_)+ | Expr::ScalarVariable(_)+ | Expr::Wildcard => vec![],+ }+ };++ matched_exprs.into_iter().fold(vec![], |mut acc, expr| {+ if !acc.contains(&expr) {+ acc.push(expr)+ }++ acc+ })+}++/// Convert any `Expr` to an `Expr::Column`.+pub(crate) fn expr_as_column_expr(expr: &Expr, plan: &LogicalPlan) -> Result<Expr> {+ match expr {+ Expr::Column(_) => Ok(expr.clone()),+ _ => Ok(Expr::Column(expr.name(&plan.schema())?)),+ }+}++/// Rebuilds an `Expr` as a projection on top of a collection of `Expr`'s.+///+/// For example, the expression `a + b < 1` would require, as input, the 2+/// individual columns, `a` and `b`. But, if the base expressions already+/// contain the `a + b` result, then that may be used in lieu of the `a` and+/// `b` columns.+///+/// This is useful in the context of a query like:+///+/// SELECT a + b < 1 ... GROUP BY a + b+///+/// where post-aggregation, `a + b` need not be a projection against the+/// individual columns `a` and `b`, but rather it is a projection against the+/// `a + b` found in the GROUP BY.+pub(crate) fn rebase_expr(+ expr: &Expr,+ base_exprs: &Vec<Expr>,+ plan: &LogicalPlan,+) -> Result<Expr> {+ clone_with_replacement(expr, &|nested_expr| {+ if base_exprs.contains(nested_expr) {+ Ok(Some(expr_as_column_expr(nested_expr, plan)?))+ } else {+ Ok(None)+ }+ })+}++/// Determines if the set of `Expr`'s are a valid projection on the input+/// `Expr::Column`'s.+pub(crate) fn can_columns_satisfy_exprs(+ columns: &Vec<Expr>,+ exprs: &Vec<Expr>,+) -> Result<bool> {+ columns.iter().try_for_each(|c| match c {+ Expr::Column(_) => Ok(()),+ _ => Err(DataFusionError::Internal(+ "Expr::Column are required".to_string(),+ )),+ })?;++ Ok(find_column_exprs(exprs).iter().all(|c| columns.contains(c)))+}++/// Returns a cloned `Expr`, but any of the `Expr`'s in the tree may be+/// replaced/customized by the replacement function.+///+/// The replacement function is called repeatedly with `Expr`, starting with+/// the argument `expr`, then descending depth-first through its+/// descendants. The function chooses to replace or keep (clone) each `Expr`.+///+/// The function's return type is `Result<Option<Expr>>>`, where:+///+/// * `Ok(Some(replacement_expr))`: A replacement `Expr` is provided; it is+/// swapped in at the particular node in the tree. Any nested `Expr` are+/// not subject to cloning/replacement.+/// * `Ok(None)`: A replacement `Expr` is not provided. The `Expr` is+/// recreated, with all of its nested `Expr`'s subject to+/// cloning/replacement.+/// * `Err(err)`: Any error returned by the function is returned as-is by+/// `clone_with_replacement()`.+fn clone_with_replacement<F>(expr: &Expr, replacement_fn: &F) -> Result<Expr>+where+ F: Fn(&Expr) -> Result<Option<Expr>>,+{+ let replacement_opt = replacement_fn(expr)?;++ match replacement_opt {+ // If we were provided a replacement, use the replacement. Do not+ // descend further.+ Some(replacement) => Ok(replacement),+ // No replacement was provided, clone the node and recursively call+ // clone_with_replacement() on any nested expressions.+ None => match expr {+ Expr::AggregateFunction {+ fun,+ args,+ distinct,+ } => Ok(Expr::AggregateFunction {+ fun: fun.clone(),+ args: args+ .iter()+ .map(|e| clone_with_replacement(e, replacement_fn))+ .collect::<Result<Vec<Expr>>>()?,+ distinct: *distinct,+ }),+ Expr::AggregateUDF { fun, args } => Ok(Expr::AggregateUDF {+ fun: fun.clone(),+ args: args+ .iter()+ .map(|e| clone_with_replacement(e, replacement_fn))+ .collect::<Result<Vec<Expr>>>()?,+ }),+ Expr::Alias(nested_expr, alias_name) => Ok(Expr::Alias(+ Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),+ alias_name.clone(),+ )),+ Expr::Between {+ expr: nested_expr,+ negated,+ low,+ high,+ } => Ok(Expr::Between {+ expr: Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),+ negated: *negated,+ low: Box::new(clone_with_replacement(&**low, replacement_fn)?),+ high: Box::new(clone_with_replacement(&**high, replacement_fn)?),+ }),+ Expr::BinaryExpr { left, right, op } => Ok(Expr::BinaryExpr {+ left: Box::new(clone_with_replacement(&**left, replacement_fn)?),+ op: op.clone(),+ right: Box::new(clone_with_replacement(&**right, replacement_fn)?),+ }),+ Expr::Case {+ expr: case_expr_opt,+ when_then_expr,+ else_expr: else_expr_opt,+ } => Ok(Expr::Case {+ expr: match case_expr_opt {+ Some(case_expr) => Some(Box::new(clone_with_replacement(+ &**case_expr,+ replacement_fn,+ )?)),+ None => None,+ },+ when_then_expr: when_then_expr+ .iter()+ .map(|(a, b)| {+ Ok((+ Box::new(clone_with_replacement(&**a, replacement_fn)?),+ Box::new(clone_with_replacement(&**b, replacement_fn)?),+ ))+ })+ .collect::<Result<Vec<(_, _)>>>()?,+ else_expr: match else_expr_opt {+ Some(else_expr) => Some(Box::new(clone_with_replacement(+ &**else_expr,+ replacement_fn,+ )?)),+ None => None,+ },+ }),+ Expr::ScalarFunction { fun, args } => Ok(Expr::ScalarFunction {+ fun: fun.clone(),+ args: args+ .iter()+ .map(|e| clone_with_replacement(e, replacement_fn))+ .collect::<Result<Vec<Expr>>>()?,+ }),+ Expr::ScalarUDF { fun, args } => Ok(Expr::ScalarUDF {+ fun: fun.clone(),+ args: args+ .iter()+ .map(|arg| clone_with_replacement(arg, replacement_fn))+ .collect::<Result<Vec<Expr>>>()?,+ }),+ Expr::Negative(nested_expr) => Ok(Expr::Negative(Box::new(+ clone_with_replacement(&**nested_expr, replacement_fn)?,+ ))),+ Expr::Not(nested_expr) => Ok(Expr::Not(Box::new(clone_with_replacement(+ &**nested_expr,+ replacement_fn,+ )?))),+ Expr::IsNotNull(nested_expr) => Ok(Expr::IsNotNull(Box::new(+ clone_with_replacement(&**nested_expr, replacement_fn)?,+ ))),+ Expr::IsNull(nested_expr) => Ok(Expr::IsNull(Box::new(+ clone_with_replacement(&**nested_expr, replacement_fn)?,+ ))),+ Expr::Cast {+ expr: nested_expr,+ data_type,+ } => Ok(Expr::Cast {+ expr: Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),+ data_type: data_type.clone(),+ }),+ Expr::Sort {+ expr: nested_expr,+ asc,+ nulls_first,+ } => Ok(Expr::Sort {+ expr: Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),+ asc: *asc,+ nulls_first: *nulls_first,+ }),++ Expr::Column(_) | Expr::Literal(_) | Expr::ScalarVariable(_) => {+ Ok(expr.clone())+ }+ Expr::Wildcard => Ok(Expr::Wildcard),+ },+ }+}