From cf31c39a0197103cbf20dcec1c9ad1d3ec3d8279 Mon Sep 17 00:00:00 2001 From: duripeng <453243496@qq.com> Date: Sun, 17 Apr 2022 01:24:06 +0800 Subject: [PATCH] negative operator support null constants --- datafusion/core/src/physical_plan/planner.rs | 8 ++-- datafusion/core/tests/sql/expr.rs | 39 +++++++++++++++++++ .../physical-expr/src/expressions/negative.rs | 35 +++++++---------- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rs index 98076d1365bcd..ef6ff3cb8a4a1 100644 --- a/datafusion/core/src/physical_plan/planner.rs +++ b/datafusion/core/src/physical_plan/planner.rs @@ -1031,10 +1031,12 @@ pub fn create_physical_expr( input_schema, execution_props, )?), - Expr::Negative(expr) => expressions::negative( - create_physical_expr(expr, input_dfschema, input_schema, execution_props)?, + Expr::Negative(expr) => expressions::negative(create_physical_expr( + expr, + input_dfschema, input_schema, - ), + execution_props, + )?), Expr::IsNull(expr) => expressions::is_null(create_physical_expr( expr, input_dfschema, diff --git a/datafusion/core/tests/sql/expr.rs b/datafusion/core/tests/sql/expr.rs index 4bb2cafad56cf..8693e71f62d2d 100644 --- a/datafusion/core/tests/sql/expr.rs +++ b/datafusion/core/tests/sql/expr.rs @@ -406,6 +406,45 @@ async fn test_not_expressions() -> Result<()> { Ok(()) } +#[tokio::test] +async fn test_negative_expressions() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = "SELECT -(2+1), -(1.0*5.0), -1"; + let actual = execute_to_batches(&ctx, sql).await; + let expected = vec![ + "+-------------------------+-----------------------------+-----------+", + "| (- Int64(2) + Int64(1)) | (- Float64(1) * Float64(5)) | Int64(-1) |", + "+-------------------------+-----------------------------+-----------+", + "| -3 | -5 | -1 |", + "+-------------------------+-----------------------------+-----------+", + ]; + assert_batches_eq!(expected, &actual); + + let sql = "SELECT null, -null"; + let actual = execute_to_batches(&ctx, sql).await; + let expected = vec![ + "+------------+----------------+", + "| Utf8(NULL) | (- Utf8(NULL)) |", + "+------------+----------------+", + "| | |", + "+------------+----------------+", + ]; + + let sql = "SELECT -'hi'"; + let result = plan_and_collect(&ctx, sql).await; + match result { + Ok(_) => panic!("expected error"), + Err(e) => { + assert_contains!(e.to_string(), + "(- 'Literal { value: Utf8(\"hi\") }') can't be evaluated because the expression's type is Utf8, not signed numeric or NULL" + ); + } + } + assert_batches_eq!(expected, &actual); + Ok(()) +} + #[tokio::test] async fn test_boolean_expressions() -> Result<()> { test_expression!("true", "true"); diff --git a/datafusion/physical-expr/src/expressions/negative.rs b/datafusion/physical-expr/src/expressions/negative.rs index 4974bdb329206..336681eff6d90 100644 --- a/datafusion/physical-expr/src/expressions/negative.rs +++ b/datafusion/physical-expr/src/expressions/negative.rs @@ -30,7 +30,7 @@ use arrow::{ use crate::coercion_rule::binary_rule::is_signed_numeric; use crate::PhysicalExpr; -use datafusion_common::{DataFusionError, Result}; +use datafusion_common::{DataFusionError, Result, ScalarValue}; use datafusion_expr::ColumnarValue; /// Invoke a compute kernel on array(s) @@ -104,6 +104,18 @@ impl PhysicalExpr for NegativeExpr { result.map(|a| ColumnarValue::Array(a)) } ColumnarValue::Scalar(scalar) => { + if scalar.is_null() { + return Ok(ColumnarValue::Scalar(ScalarValue::Int32(None))); + } + let value_type = scalar.get_datatype(); + if !is_signed_numeric(&value_type) { + return Err(DataFusionError::Internal( + format!( + "(- '{:?}') can't be evaluated because the expression's type is {:?}, not signed numeric or NULL", + self.arg, value_type, + ), + )); + } Ok(ColumnarValue::Scalar(scalar.arithmetic_negate())) } } @@ -111,23 +123,6 @@ impl PhysicalExpr for NegativeExpr { } /// Creates a unary expression NEGATIVE -/// -/// # Errors -/// -/// This function errors when the argument's type is not signed numeric -pub fn negative( - arg: Arc, - input_schema: &Schema, -) -> Result> { - let data_type = arg.data_type(input_schema)?; - if !is_signed_numeric(&data_type) { - Err(DataFusionError::Internal( - format!( - "(- '{:?}') can't be evaluated because the expression's type is {:?}, not signed numeric", - arg, data_type, - ), - )) - } else { - Ok(Arc::new(NegativeExpr::new(arg))) - } +pub fn negative(arg: Arc) -> Result> { + Ok(Arc::new(NegativeExpr::new(arg))) }