Skip to content
Merged
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
28 changes: 24 additions & 4 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::{
datatypes::{DataType, Schema},
record_batch::RecordBatch,
};
use datafusion_common::{internal_err, DataFusionError, Result};
use datafusion_common::{plan_err, DataFusionError, Result};
use datafusion_expr::interval_arithmetic::Interval;
use datafusion_expr::{
type_coercion::{is_interval, is_null, is_signed_numeric, is_timestamp},
Expand Down Expand Up @@ -164,9 +164,7 @@ pub fn negative(
&& !is_interval(&data_type)
&& !is_timestamp(&data_type)
{
internal_err!(
"Can't create negative physical expr for (- '{arg:?}'), the type of child expr is {data_type}, not signed numeric"
)
plan_err!("Negation only supports numeric, interval and timestamp types")
} else {
Ok(Arc::new(NegativeExpr::new(arg)))
}
Expand Down Expand Up @@ -252,4 +250,26 @@ mod tests {
);
Ok(())
}

#[test]
fn test_negation_valid_types() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 very nice -- thank you

let negatable_types = [
DataType::Int8,
DataType::Timestamp(TimeUnit::Second, None),
DataType::Interval(IntervalUnit::YearMonth),
];
for negatable_type in negatable_types {
let schema = Schema::new(vec![Field::new("a", negatable_type, true)]);
let _expr = negative(col("a", &schema)?, &schema)?;
}
Ok(())
}

#[test]
fn test_negation_invalid_types() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
let expr = negative(col("a", &schema)?, &schema).unwrap_err();
matches!(expr, DataFusionError::Plan(_));
Ok(())
}
}
3 changes: 3 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,9 @@ SELECT null, -null
----
NULL NULL

query error DataFusion error: Error during planning: Negation only supports numeric, interval and timestamp types
SELECT -'100'

statement ok
drop table test_boolean

Expand Down