-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
MDEV-37939 Add function context to division by zero warnings #4569
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
base: main
Are you sure you want to change the base?
Conversation
gkodinov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a preliminary review. The approach is sound IMHO, but there's some things missing (see the comments below).
Also, please make sure you re-record the rest of the failing test cases too.
| } | ||
| String *val_str_from_val_str_ascii(String *str, String *str2); | ||
|
|
||
| void signal_divide_by_null(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto here: remove the old one.
Also, please consider moving this under "protected": let's make sure it's not used anywhere externally.
I'd even go as far as making it an inline static function in item_func.cc: it doesn't have to be a class method. I'd at least make it static to highlight that it's just a utility function.
sql/share/errmsg-utf8.txt
Outdated
| ER_WARN_NO_IMPLICIT_QB_NAMES_IN_VIEW | ||
| eng "Implicit query block names are ignored for hints specified within VIEWs" | ||
| ER_DIVISION_BY_ZERO_IN_FUNC 22012 | ||
| eng "Division by 0 in function %s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are at it, I'd even add a specific cause for the error. Some functions could throw this error on several occasions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to let you know, I haven’t touched this in the current commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get the final review approval, I think you also should address Sergei's question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current changes should address Sergei's question now.
Add contextual warnings for LOG/LN/LOG2/LOG10 while preserving error code 1365 for compatibility. Make divide by zero reporting explicit at call sites and expand tests to cover div/mod with real, decimal, and string arguments.
|
Thank you for the suggestion @gkodinov . I removed the no arg signal_divide_by_null() and made the remaining method protected |
Replace division by zero warnings for LOG/LN/LOG2/LOG10 with ER_INVALID_ARGUMENT_FOR_LOGARITHM while keeping division warnings unchanged. Reorder and expand the test to verify empty warnings, log warnings, and division warnings.
|
Updated it so LOG/LN/LOG2/LOG10 now emit the correct warning (Invalid argument for logarithm) instead of Division by 0. Division/DIV/MOD remain unchanged also reordered the expanded test of func_math_div_zero to verify empty warnings and all cases. Build and test pass. @gkodinov @vuvova Requesting to review. Thank you |
gkodinov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something interesting from https://dev.mysql.com/doc/refman/8.4/en/mathematical-functions.html#function_log2:
If X is less than or equal to 0.0E0, the function returns NULL and a warning “Invalid argument for logarithm” is reported. Returns NULL if X is NULL.
I think this is a good idea. But I will leave this to the final reviewer.
|
|
||
| namespace { | ||
|
|
||
| void push_divide_by_zero_warning(THD *thd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a but of an overkill these helper functions. push_warning itself is not considered too much IMHO. It's OK to leave it like this. Just saying I wouldn't have went to the trouble. I was hoping that you can either abandon Item_func::signal_... altogether or use local static helper functions. Now you're doing both. This is definitely redundant.
| { | ||
| push_warning(thd, Sql_condition::WARN_LEVEL_WARN, | ||
| ER_INVALID_ARGUMENT_FOR_LOGARITHM, | ||
| ER_THD(thd, ER_INVALID_ARGUMENT_FOR_LOGARITHM)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, you've found yourself a dead error code! This code is not used anywhere currently.
I'd personally prefer to leave it like that and add a new one that actually helps, e.g. "passing a negative argument to ln produces undefined results".
Otherwise we're back to the situation before your change: you get a cryptic error message that you can't really pinpoint to any specific part of a complex expression.
I will try to offer what I think is a meaningful error message in each case below.
| if (value <= 0.0) | ||
| { | ||
| signal_divide_by_null(); | ||
| signal_invalid_log_argument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message: Passing negative argument to ln() makes the function's result undefined.
| if (value <= 0.0) | ||
| { | ||
| signal_divide_by_null(); | ||
| signal_invalid_log_argument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message: Passing negative argument to log() makes the function's result undefined.
| if (value2 <= 0.0 || value == 1.0) | ||
| { | ||
| signal_divide_by_null(); | ||
| signal_invalid_log_argument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message: Passing negative 2nd argument to log() makes the function's result undefined.
| if (value <= 0.0) | ||
| { | ||
| signal_divide_by_null(); | ||
| signal_invalid_log_argument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message: Passing negative argument to log2() makes the function's result undefined.
| if (value <= 0.0) | ||
| { | ||
| signal_divide_by_null(); | ||
| signal_invalid_log_argument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message: Passing negative argument to log10() makes the function's result undefined.
JIRA: https://jira.mariadb.org/browse/MDEV-37939
Summary
Testing