Skip to content

Commit 4a3986a

Browse files
authored
NOT operator not return internal error when args are not boolean value (#8982)
* optimize NOT Expr logic * fix Null type * fix test case * fmt
1 parent 4ac7de1 commit 4a3986a

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use datafusion_expr::type_coercion::other::{
4444
use datafusion_expr::type_coercion::{is_datetime, is_utf8_or_large_utf8};
4545
use datafusion_expr::utils::merge_schema;
4646
use datafusion_expr::{
47-
is_false, is_not_false, is_not_true, is_not_unknown, is_true, is_unknown,
47+
is_false, is_not_false, is_not_true, is_not_unknown, is_true, is_unknown, not,
4848
type_coercion, AggregateFunction, BuiltinScalarFunction, Expr, ExprSchemable,
4949
LogicalPlan, Operator, Projection, ScalarFunctionDefinition, Signature, WindowFrame,
5050
WindowFrameBound, WindowFrameUnits,
@@ -176,6 +176,10 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
176176
negated,
177177
)))
178178
}
179+
Expr::Not(expr) => {
180+
let expr = not(get_casted_expr_for_bool_op(&expr, &self.schema)?);
181+
Ok(expr)
182+
}
179183
Expr::IsTrue(expr) => {
180184
let expr = is_true(get_casted_expr_for_bool_op(&expr, &self.schema)?);
181185
Ok(expr)

datafusion/physical-expr/src/expressions/not.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ use crate::physical_expr::down_cast_any_ref;
2626
use crate::PhysicalExpr;
2727
use arrow::datatypes::{DataType, Schema};
2828
use arrow::record_batch::RecordBatch;
29-
use datafusion_common::{
30-
cast::as_boolean_array, internal_err, DataFusionError, Result, ScalarValue,
31-
};
29+
use datafusion_common::{cast::as_boolean_array, Result, ScalarValue};
3230
use datafusion_expr::ColumnarValue;
3331

3432
/// Not expression
@@ -83,13 +81,6 @@ impl PhysicalExpr for NotExpr {
8381
if scalar.is_null() {
8482
return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None)));
8583
}
86-
let value_type = scalar.data_type();
87-
if value_type != DataType::Boolean {
88-
return internal_err!(
89-
"NOT '{:?}' can't be evaluated because the expression's type is {:?}, not boolean or NULL",
90-
self.arg, value_type
91-
);
92-
}
9384
let bool_value: bool = scalar.try_into()?;
9485
Ok(ColumnarValue::Scalar(ScalarValue::Boolean(Some(
9586
!bool_value,

datafusion/sqllogictest/test_files/scalar.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,15 +1527,15 @@ SELECT not(true), not(false)
15271527
----
15281528
false true
15291529

1530-
query error
1530+
query error type_coercion\ncaused by\nError during planning: Cannot infer common argument type for comparison operation Int64 IS DISTINCT FROM Boolean
15311531
SELECT not(1), not(0)
15321532

15331533
query ?B
15341534
SELECT null, not(null)
15351535
----
15361536
NULL NULL
15371537

1538-
query error
1538+
query error type_coercion\ncaused by\nError during planning: Cannot infer common argument type for comparison operation Utf8 IS DISTINCT FROM Boolean
15391539
SELECT NOT('hi')
15401540

15411541
# test_negative_expressions()

0 commit comments

Comments
 (0)