Skip to content
Open
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
17 changes: 17 additions & 0 deletions datafusion/core/tests/sql/sql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use datafusion::prelude::*;
use datafusion_common::assert_contains;

use tempfile::TempDir;

Expand Down Expand Up @@ -206,3 +207,19 @@ async fn ddl_can_not_be_planned_by_session_state() {
"This feature is not implemented: Unsupported logical plan: DropTable"
);
}

#[tokio::test]
async fn invalid_wrapped_negation_fails_during_optimization() {
let ctx = SessionContext::new();
let err = ctx
.sql("SELECT * FROM (SELECT 1) WHERE ((-'a') IS NULL)")
.await
.unwrap()
.into_optimized_plan()
.unwrap_err();

assert_contains!(
err.strip_backtrace(),
"Negation only supports numeric, interval and timestamp types"
);
}
30 changes: 28 additions & 2 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ use datafusion_expr::expr_schema::cast_subquery;
use datafusion_expr::logical_plan::Subquery;
use datafusion_expr::type_coercion::binary::{comparison_coercion, like_coercion};
use datafusion_expr::type_coercion::functions::{UDFCoercionExt, fields_with_udf};
use datafusion_expr::type_coercion::is_datetime;
use datafusion_expr::type_coercion::other::{
get_coerce_type_for_case_expression, get_coerce_type_for_list,
};
use datafusion_expr::type_coercion::{
is_datetime, is_interval, is_signed_numeric, is_timestamp,
};
use datafusion_expr::utils::merge_schema;
use datafusion_expr::{
Cast, Expr, ExprSchemable, Join, Limit, LogicalPlan, Operator, Projection, Union,
Expand Down Expand Up @@ -559,6 +561,20 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> {
Expr::IsNotUnknown(expr) => Ok(Transformed::yes(is_not_unknown(
get_casted_expr_for_bool_op(*expr, self.schema)?,
))),
Expr::Negative(expr) => {
let data_type = expr.get_type(self.schema)?;
if data_type.is_null()
|| is_signed_numeric(&data_type)
|| is_interval(&data_type)
|| is_timestamp(&data_type)
{
Ok(Transformed::no(Expr::Negative(expr)))
} else {
plan_err!(
"Negation only supports numeric, interval and timestamp types"
)
}
}
Expr::Like(Like {
negated,
expr,
Expand Down Expand Up @@ -753,7 +769,6 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> {
| Expr::SimilarTo(_)
| Expr::IsNotNull(_)
| Expr::IsNull(_)
| Expr::Negative(_)
| Expr::Cast(_)
| Expr::TryCast(_)
| Expr::Wildcard { .. }
Expand Down Expand Up @@ -1369,6 +1384,17 @@ mod test {
)
}

#[test]
fn negative_expr_wrapped_by_is_null_errors() -> Result<()> {
let predicate = Expr::IsNull(Box::new(Expr::Negative(Box::new(lit("a")))));
let plan = LogicalPlan::Filter(Filter::try_new(predicate, empty())?);

assert_type_coercion_error(
plan,
"Negation only supports numeric, interval and timestamp types",
)
}

#[test]
fn test_coerce_union() -> Result<()> {
let left_plan = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
Expand Down