This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 4fca0d5122 fix: Verify supported type for Unary::Plus in sql planner
(#13019)
4fca0d5122 is described below
commit 4fca0d5122918c70e0783e44016609b27ba7c253
Author: Emil Ejbyfeldt <[email protected]>
AuthorDate: Mon Oct 21 22:37:50 2024 +0200
fix: Verify supported type for Unary::Plus in sql planner (#13019)
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.
---
datafusion/sql/src/expr/unary_op.rs | 19 ++++++++++++++++---
datafusion/sqllogictest/test_files/scalar.slt | 3 +++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/datafusion/sql/src/expr/unary_op.rs
b/datafusion/sql/src/expr/unary_op.rs
index 3c54705038..06988eb038 100644
--- a/datafusion/sql/src/expr/unary_op.rs
+++ b/datafusion/sql/src/expr/unary_op.rs
@@ -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> {
@@ -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 {
diff --git a/datafusion/sqllogictest/test_files/scalar.slt
b/datafusion/sqllogictest/test_files/scalar.slt
index 0c2fa41e5b..d510206b19 100644
--- a/datafusion/sqllogictest/test_files/scalar.slt
+++ b/datafusion/sqllogictest/test_files/scalar.slt
@@ -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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]