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
19 changes: 16 additions & 3 deletions datafusion/sql/src/expr/unary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// under the License.

use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{not_impl_err, DFSchema, Result};
use datafusion_expr::Expr;
use datafusion_common::{not_impl_err, plan_err, DFSchema, Result};
use datafusion_expr::{
type_coercion::{is_interval, is_timestamp},
Expr, ExprSchemable,
};
use sqlparser::ast::{Expr as SQLExpr, UnaryOperator, Value};

impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Expand All @@ -33,7 +36,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
self.sql_expr_to_logical_expr(expr, schema, planner_context)?,
))),
UnaryOperator::Plus => {
Ok(self.sql_expr_to_logical_expr(expr, schema, planner_context)?)
let operand =
self.sql_expr_to_logical_expr(expr, schema, planner_context)?;
let (data_type, _) = operand.data_type_and_nullable(schema)?;
if data_type.is_numeric()
|| is_interval(&data_type)
|| is_timestamp(&data_type)
{
Ok(operand)
} else {
plan_err!("Unary operator '+' only supports numeric, interval and timestamp types")
}
}
UnaryOperator::Minus => {
match expr {
Expand Down
3 changes: 3 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,9 @@ NULL NULL
query error DataFusion error: Error during planning: Negation only supports numeric, interval and timestamp types
SELECT -'100'

query error DataFusion error: Error during planning: Unary operator '\+' only supports numeric, interval and timestamp types
SELECT +true

statement ok
drop table test_boolean

Expand Down