apache / apache/datafusion

The negation operator escapes type checking at logical planning phase

Open
#20,355 2 comments 0 reactions 1 assignee Claimed by @Acfboy View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

```rust
#[tokio::test]
async fn test_unary_negation_on_string_error() -> Result<()> {
let ctx = SessionContext::new();
let res = ctx.sql("SELECT -'a';").await;
assert!(res.is_err());
Ok(())
}
```
Negation operator can't work on Utf8 should be reported at planning phase, but this test failed.

### Root Cause

It invokes `Expr::to_field` to check types and get filed of each expressions in query while generating logical plan . However, in the `Expr::Negative` bench it only returns the field of its inner expression:

https://github.com/apache/datafusion/blob/7d217b1ec3bd4d447961e84bc3fe7f7708546b2d/datafusion/expr/src/expr_schema.rs#L452

Similarly, it also returns the types of its inner expression in `Expr::get_type`, which cause it also escape checks in `type_coercion`.

https://github.com/apache/datafusion/blob/7d217b1ec3bd4d447961e84bc3fe7f7708546b2d/datafusion/expr/src/expr_schema.rs#L119

Finally, it will be caught while generating physical plan (in some code like `df.show()`);

https://github.com/apache/datafusion/blob/7d217b1ec3bd4d447961e84bc3fe7f7708546b2d/datafusion/physical-expr/src/expressions/negative.rs#L188-L199

### Fix

It's quite strightforward, I'll open a pr soon.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.