Skip to content

The negation operator escapes type checking at logical planning phase #20355

Open

Description

@Acfboy

Describe the bug

    #[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:

Expr::Negative(expr) => expr.to_field(schema).map(|(_, f)| f),

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

Expr::Negative(expr) => expr.get_type(schema),

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

pub fn negative(
arg: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
) -> Result<Arc<dyn PhysicalExpr>> {
let data_type = arg.data_type(input_schema)?;
if data_type.is_null() {
Ok(arg)
} else if !is_signed_numeric(&data_type)
&& !is_interval(&data_type)
&& !is_timestamp(&data_type)
{
plan_err!("Negation only supports numeric, interval and timestamp types")

Fix

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

Activity

  1. added theissue type on Feb 14, 2026
  2. Acfboy commented on Feb 14, 2026

    @Acfboy
    ContributorAuthor

    take

  3. u70b3 commented on Jul 20, 2026

    @u70b3
    Contributor

    Verified on current main (1e58928): this no longer reproduces. SELECT -'a'; is now rejected during planning with Error during planning: Unary operator '-' only supports signed numeric, interval and timestamp types — exactly the behavior requested in the issue. Suggest closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions