Skip to content

chore: Attach Diagnostic to "incompatible type in unary expression" error #15209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025

Conversation

onlyjackfrost
Copy link
Contributor

@onlyjackfrost onlyjackfrost commented Mar 13, 2025

Which issue does this PR close?

What changes are included in this PR?

attach a Diagnostic when dealing with "incompatible type in unary expression" error

Are these changes tested

Yes, I've tested the PLUS operator with column and non-column operands.

@github-actions github-actions bot added the sql SQL Planner label Mar 13, 2025
@onlyjackfrost
Copy link
Contributor Author

@eliaperantoni could you help review? :)

Copy link
Contributor

@eliaperantoni eliaperantoni left a comment

Choose a reason for hiding this comment

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

Hey @onlyjackfrost, thank you for your contribution!

The PR looks good, I left some comments. Though, I don't think we can close issue #14433 just with this PR because only a very specific operator is being handled. Is there any way you can extend it to all unary expressions?

Comment on lines 49 to 50
let spans = operand.spans();
let span = if let Some(s) = spans.as_ref() { s.first() } else { None };
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let spans = operand.spans();
let span = if let Some(s) = spans.as_ref() { s.first() } else { None };
let spans = operand.spans().map(|s| s.first());

Comment on lines 51 to 52
let diagnostic =
Diagnostic::new_error("Unary operator '+' only supports numeric, interval and timestamp types".to_string(), span);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a big deal but I think this message is a bit long for a Diagnostic and it addresses the problem in a very broad way, saying "+ only works with X, Y, and Z" instead of simply "+ cannot be used with W".

Is it possible to make the Diagnostic look like this:

  • message = "+ cannot be used with {type-name}"
  • note (without span) = "+ can only be used with numbers, intervals, and timestamps"
  • help (without span) = "perhaps you need to cast {expression}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it will be better this way.

@onlyjackfrost
Copy link
Contributor Author

@eliaperantoni,

Is there any way you can extend it to all unary expressions

Sure. I was thinking about doing the same data type check in other unary operator.
Does it make sense to you?

@eliaperantoni
Copy link
Contributor

@onlyjackfrost

the same data type check in other unary operator.

I'm not 100% what you mean by that. For + you used the existing error that was returned and added a Diagnostic to it. As long as other unary operators already have good error coverage, I think adding Diagnostics to them is perfectly good :)

I just hope that all necessary checks already happen in that file, and not "further up" cause that would require more work probably.

@onlyjackfrost
Copy link
Contributor Author

@eliaperantoni could I raise another PR for the others unary expressions and keep this PR for the PLUS unary expression?

@eliaperantoni
Copy link
Contributor

@eliaperantoni could I raise another PR for the others unary expressions and keep this PR for the PLUS unary expression?

Yes absolutely 😊

Copy link
Contributor

@eliaperantoni eliaperantoni left a comment

Choose a reason for hiding this comment

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

Awesome! :)

Comment on lines 54 to 57
if span.is_none() {
diagnostic.add_note("+ can only be used with numbers, intervals, and timestamps", None);
diagnostic.add_help(format!("perhaps you need to cast {operand}"), None);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do this only when the span is None? I think it should always be there

Copy link
Contributor Author

@onlyjackfrost onlyjackfrost Mar 18, 2025

Choose a reason for hiding this comment

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

note (without span) = "+ can only be used with numbers, intervals, and timestamps"
help (without span) = "perhaps you need to cast {expression}"

oops... I might have misunderstood your reply.
I'll remove the condition from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

delivered in ef3fca8

Copy link
Contributor

@eliaperantoni eliaperantoni Mar 18, 2025

Choose a reason for hiding this comment

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

Ahh sorry! Hahah. Yeah I meant to say that the note and the help should have no Span attached to them, in my opinion. But they (the note and the help) should always be there.

#[test]
fn test_unary_op_plus_with_non_column() -> Result<()> {
// create a table with a column of type varchar
let query = "SELECT /*whole*/+'a'/*whole*/ ";
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove the span tags /*whole*/ since they aren't used by the test anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

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

delivered in ef3fca8

Copy link
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.

Other than the comments from @eliaperantoni I think this PR looks good to me

Thank you @onlyjackfrost (and @eliaperantoni 🙏 )

@alamb alamb merged commit 13af5f3 into apache:main Mar 18, 2025
27 checks passed
@alamb
Copy link
Contributor

alamb commented Mar 18, 2025

Thanks again everyone!

@onlyjackfrost onlyjackfrost deleted the chore/unary-function branch March 25, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants