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
[CALCITE-4420] Some simple arithmetic operations can be simplified #2282
Conversation
1f832ee
to
1633330
Compare
if (RexUtil.isNullLiteral(e.operands.get(0), true) | ||
|| RexUtil.isNullLiteral(e.operands.get(1), true)) { | ||
return rexBuilder.makeNullLiteral(e.type); | ||
} |
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 might not be true. Sometimes users may want it to throw an exception.
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.
Thanks for your feedback. It makes sense. This logic is removed.
if (zeroIndex == 1) { | ||
RexNode leftOperand = e.getOperands().get(0); | ||
return leftOperand.getType().equals(e.getType()) | ||
? leftOperand : rexBuilder.makeCast(e.getType(), leftOperand); |
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 a - a
be simplified to 0 if a is not nullable?
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.
Thanks for the suggestion.
There are some special cases for which this is not true. For example,
NaN - NaN = NaN
Inf - Inf = NaN
1633330
to
68f497f
Compare
|
||
private RexNode simplifyMultiply(RexCall e) { | ||
int oneIndex = findLiteralIndex(e.operands, 1L); | ||
if (oneIndex >= 0) { |
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.
Make it final since it's our convention.
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.
Revised. Thanks for the good suggestion.
|| e.getOperands().stream() | ||
.anyMatch(o -> e.getType().getSqlTypeName().getFamily() != SqlTypeFamily.NUMERIC)) { | ||
// we only support simplifying numeric types | ||
return simplifyGenericNode(e); |
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.
Add . to the end of the sentence.
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.
Done. Thanks.
Comparable comparable = ((RexLiteral) operands.get(i)).getValue(); | ||
if (comparable instanceof BigDecimal && ((BigDecimal) comparable).longValue() == value) { | ||
return i; | ||
} |
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.
Could you mark it that it only supports numeric type?
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.
Sure. I've made it explicit in the JavaDoc.
00b866e
to
1edcd5a
Compare
LGTM |
Dear reviewers, thanks a lot for your good comments. |
Dear reviewers, thanks a lot for your comments. |
LGTM |
Some simple arithmetic operations can be simplified, for example:
In our product, we found such scenarios are fairly frequently encountered. For example, some SQL programmers use a * 1.0 as a convenient way to cast
a
to floating point type.