Skip to content

chore: attach Diagnostic to unary operator type errors#21288

Open
hcrosse wants to merge 3 commits intoapache:mainfrom
hcrosse:diagnostic-unary-op
Open

chore: attach Diagnostic to unary operator type errors#21288
hcrosse wants to merge 3 commits intoapache:mainfrom
hcrosse:diagnostic-unary-op

Conversation

@hcrosse
Copy link
Copy Markdown
Contributor

@hcrosse hcrosse commented Apr 1, 2026

Which issue does this PR close?

What changes are included in this PR?

Attaches Diagnostic (message, note, help) to the NOT and - error paths in unary_op.rs. Also extends Expr::spans() to recurse through Not/Negative so column source locations propagate to the error.

+ already had this, this PR covers the remaining two operators.

Are these changes tested?

Yes, added tests in diagnostic.rs for column and non-column operands for both operators.

Are there any user-facing changes?

Error messages for NOT <non-boolean> and - <non-numeric> now include source location and a fix suggestion.

Closes apache#14433

- Attach rich `Diagnostic` (primary message, note, help) to the
  'NOT requires boolean' and 'minus requires signed numeric' errors
  raised during SQL planning in `unary_op.rs`
- Use `BinaryTypeCoercer` for the NOT boolean-coercibility check so
  `Dictionary(_, Boolean)` and other coercible types are accepted,
  matching the analyzer's acceptance rule
- Extend `Expr::spans()` to recurse through `Not` and `Negative`
  wrappers so column source spans propagate to the error
- Update `select_neg_filter` test to expect the new planning-time error
- Add four diagnostic tests covering column and non-column operands for
  both NOT and unary minus
Copy link
Copy Markdown
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @hcrosse - I left some comments


#[test]
fn select_neg_filter() {
// NOT requires a boolean expression; applying it to a Utf8 column is an error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also ensure we have test coverage for planning an actual valid not expression?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

))),
UnaryOperator::Not => {
let operand =
self.sql_expr_to_logical_expr(expr, schema, planner_context)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checking is normally done as part of analysis (not the sql planner) (aka in an Analyzer rule) -- if we put it in sql planning then it won't apply to queries that don't come from SQL.

Copy link
Copy Markdown
Contributor Author

@hcrosse hcrosse Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! The + operator diagnostic was done the same way in #15209, do you think we should move all three to the analyzer, or do you want me to add a similar check there in addition to what I'm adding here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to move this into the analyzer too (as a follow on PR once we have the pattern sorted out for this one)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to do that!

hcrosse added 2 commits April 2, 2026 18:02
- Add select_not_bool_filter test for the happy path (NOT on a boolean
  column) to complement the error-case test in select_neg_filter
- Update invalid_wrapped_negation test to expect error at planning time
- Update scalar.slt expected error messages for NOT and minus
@github-actions github-actions bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attach Diagnostic to "incompatible type in unary expression" error

2 participants