Apache DataFusion SQL Query Engine
ARROW-9696: [Rust] [DataFusion] fix nested binary expressions
Morgan Cassels23ed6da0699b
diff --git a/rust/datafusion/src/logicalplan.rs b/rust/datafusion/src/logicalplan.rsindex 57571b5a3d..507e8b0af0 100644--- a/rust/datafusion/src/logicalplan.rs+++ b/rust/datafusion/src/logicalplan.rs@@ -321,6 +321,8 @@ pub enum Expr {/// Right-hand side of the expressionright: Box<Expr>,},+ /// Nested expression e.g. `(foo > bar)` or `(1)`+ Nested(Box<Expr>),/// unary NOTNot(Box<Expr>),/// unary IS NOT NULL@@ -399,6 +401,7 @@ impl Expr {Expr::Wildcard => Err(ExecutionError::General("Wildcard expressions are not valid in a logical query plan".to_owned(),)),+ Expr::Nested(e) => e.get_type(schema),}}@@ -646,6 +649,7 @@ impl fmt::Debug for Expr {write!(f, ")")}Expr::Wildcard => write!(f, "*"),+ Expr::Nested(expr) => write!(f, "({:?})", expr),}}}diff --git a/rust/datafusion/src/optimizer/type_coercion.rs b/rust/datafusion/src/optimizer/type_coercion.rsindex a03a92cdfe..78fcf52f1c 100644--- a/rust/datafusion/src/optimizer/type_coercion.rs+++ b/rust/datafusion/src/optimizer/type_coercion.rs@@ -137,6 +137,7 @@ impl<'a> TypeCoercionRule<'a> {Expr::Wildcard { .. } => Err(ExecutionError::General("Wildcard expressions are not valid in a logical query plan".to_owned(),)),+ Expr::Nested(e) => self.rewrite_expr(e, schema),}}}diff --git a/rust/datafusion/src/optimizer/utils.rs b/rust/datafusion/src/optimizer/utils.rsindex 5c59803ec7..6c9d409d34 100644--- a/rust/datafusion/src/optimizer/utils.rs+++ b/rust/datafusion/src/optimizer/utils.rs@@ -64,6 +64,7 @@ pub fn expr_to_column_names(expr: &Expr, accum: &mut HashSet<String>) -> Result<Expr::Wildcard => Err(ExecutionError::General("Wildcard expressions are not valid in a logical query plan".to_owned(),)),+ Expr::Nested(e) => expr_to_column_names(e, accum),}}diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rsindex d80a70ff7f..a78f790852 100644--- a/rust/datafusion/src/sql/planner.rs+++ b/rust/datafusion/src/sql/planner.rs@@ -515,6 +515,8 @@ impl<S: SchemaProvider> SqlToRel<S> {}}+ SQLExpr::Nested(e) => self.sql_to_rex(&e, &schema),+_ => Err(ExecutionError::General(format!("Unsupported ast node {:?} in sqltorel",sql@@ -635,6 +637,22 @@ mod tests {quick_test(sql, expected);}+ #[test]+ fn select_binary_expr() {+ let sql = "SELECT age + salary from person";+ let expected = "Projection: #age Plus #salary\+ \n TableScan: person projection=None";+ quick_test(sql, expected);+ }++ #[test]+ fn select_binary_expr_nested() {+ let sql = "SELECT (age + salary)/2 from person";+ let expected = "Projection: #age Plus #salary Divide Int64(2)\+ \n TableScan: person projection=None";+ quick_test(sql, expected);+ }+#[test]fn select_simple_aggregate() {quick_test(