Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions datafusion/core/src/physical_plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions datafusion/core/tests/sql/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
35 changes: 15 additions & 20 deletions datafusion/physical-expr/src/expressions/negative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -104,30 +104,25 @@ 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()))
}
}
}
}

/// Creates a unary expression NEGATIVE
///
/// # Errors
///
/// This function errors when the argument's type is not signed numeric
pub fn negative(
arg: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
) -> Result<Arc<dyn PhysicalExpr>> {
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<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> {
Ok(Arc::new(NegativeExpr::new(arg)))
}