-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
SQL: rewrite ROUND and TRUNCATE functions with a different optional parameter handling method #40242
Conversation
approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done.
Pinging @elastic/es-search |
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.
Nice refactoring. Left some comments.
|
||
} | ||
|
||
return right == null ? TypeResolution.TYPE_RESOLVED : isNumeric(right, sourceText(), ParamOrdinal.SECOND); |
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 right
should be of integer type (not rational), so we should catch it here before the execution time.
public DataType dataType() { | ||
return left().dataType(); | ||
public Expression replaceChildren(List<Expression> newChildren) { | ||
if (right() != null && newChildren.size() != 2) { |
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.
I'd extract this to the parent method and leave here only the instance creation, as it's duplicated also in Round
.
} | ||
|
||
private ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) { | ||
if (right == 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.
Wouldn't be possible to avoid the "double" methods in painless and just have one with both args where null is passed for the right arg?
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.
Gave it a try, but failed. If you have any ideas, I'd love to hear them. Otherwise, I'll try later today or tomorrow.
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.
I took some time here and it works like that: matriv@b35090e
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.
Indeed. Nice one. I've incorporated it in the PR. Thanks.
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Processor for binary mathematical operations that can have the second parameter optional. |
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.
Maybe rephrase: ... that have a second optional parameter.
@@ -200,6 +200,23 @@ ROUND(SQRT(CAST(EXP(languages) AS SMALLINT)),2):d| COUNT(*):l | |||
null |10 | |||
; | |||
|
|||
groupByRoundWithTwoParams |
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.
Can't this be tested in QueryTranslatorTests?
@elasticmachine run elasticsearch-ci/1 |
@elasticmachine run elasticsearch-ci/default-distro |
3 similar comments
@elasticmachine run elasticsearch-ci/default-distro |
@elasticmachine run elasticsearch-ci/default-distro |
@elasticmachine run elasticsearch-ci/default-distro |
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.
LGTM
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.
Considering the timing and everything, I'm fine with the approach.
Going forward I think we need a more generic way of dealing with optional arguments - whether that requires updates in the Analyzer or/and the functions remains to be determined.
…arameter handling method (#40242) * Rewrite Round and Truncate functions to have a slightly different approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done. (cherry picked from commit 3e314f8)
…arameter handling method (#40242) * Rewrite Round and Truncate functions to have a slightly different approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done. (cherry picked from commit 3e314f8)
…arameter handling method (#40242) * Rewrite Round and Truncate functions to have a slightly different approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done. (cherry picked from commit 3e314f8)
…arameter handling method (#40242) * Rewrite Round and Truncate functions to have a slightly different approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done. (cherry picked from commit 3e314f8)
The optional parameter for these two functions was handled in the constructor by passing the value of
0
whenever the parameter was missing. This had an apparently minor implication of defining a second child for the function (the second parameter). If the value was kept asnull
(missing) the function would have had a single child (the first parameter).This affected the comparison operation from the Analyzer and the same function that was already resolved wouldn't match the new function found because the new one had no second parameter while the resolved one had the parameter
0
. Because of this comparison failing, aGROUP BY
the same function would fail.This PR also fixes a bug with ATAN2 and POWER functions which didn't have any Painless methods.
Fixes #40001.