Skip to content
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

Merged
merged 1 commit into from Jul 12, 2021

Conversation

liyafan82
Copy link
Contributor

Some simple arithmetic operations can be simplified, for example:

a + 0 = a
a - 0 = a
a * 1 = a
a / 1 = a

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.

@liyafan82 liyafan82 force-pushed the fly_1126_art branch 3 times, most recently from 1f832ee to 1633330 Compare November 27, 2020 06:23
if (RexUtil.isNullLiteral(e.operands.get(0), true)
|| RexUtil.isNullLiteral(e.operands.get(1), true)) {
return rexBuilder.makeNullLiteral(e.type);
}
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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


private RexNode simplifyMultiply(RexCall e) {
int oneIndex = findLiteralIndex(e.operands, 1L);
if (oneIndex >= 0) {
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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;
}
Copy link
Contributor

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?

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. I've made it explicit in the JavaDoc.

@liyafan82 liyafan82 force-pushed the fly_1126_art branch 2 times, most recently from 00b866e to 1edcd5a Compare December 1, 2020 06:25
@Aaaaaaron
Copy link
Member

LGTM

@liyafan82
Copy link
Contributor Author

Dear reviewers, thanks a lot for your good comments.
If there are no other comments, I am going to merge this in a few days.

@liyafan82 liyafan82 added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jun 25, 2021
@liyafan82 liyafan82 removed the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jun 28, 2021
@liyafan82 liyafan82 added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jul 6, 2021
@liyafan82
Copy link
Contributor Author

Dear reviewers, thanks a lot for your comments.
If there are no more comments, I want to merge it in a few days.

@NobiGo
Copy link
Contributor

NobiGo commented Jul 6, 2021

LGTM

@liyafan82 liyafan82 merged commit c700e37 into apache:master Jul 12, 2021
@liyafan82 liyafan82 deleted the fly_1126_art branch July 12, 2021 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LGTM-will-merge-soon Overall PR looks OK. Only minor things left.
Projects
None yet
5 participants