Skip to content

Commit

Permalink
fix: Verify supported type for Unary::Plus in sql planner
Browse files Browse the repository at this point in the history
This adds a type check when planning unary plus operator. Since we
currently do not represent the operator in our logical plan we can not
check it later. Instead of introducing a new `Expr` this patch just
verifies the type during the translation instead.
  • Loading branch information
eejbyfeldt committed Oct 20, 2024
1 parent 7a34147 commit 86c848c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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

0 comments on commit 86c848c

Please sign in to comment.