zabetak commented on code in PR #4505:
URL: https://github.com/apache/calcite/pull/4505#discussion_r2485912671
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -1189,6 +1102,23 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs
unknownAs) {
}
}
return RexUtil.composeDisjunction(rexBuilder, operands, false);
+ case CUSTOM:
+ if (a.getKind() == SqlKind.DIVIDE) {
+ RexNode op0 = ((RexCall) a).getOperands().get(0);
+ RexNode op1 = ((RexCall) a).getOperands().get(1);
+ if (RexUtil.isNull(op0) || RexUtil.isNull(op1)) {
+ return rexBuilder.makeLiteral(true);
+ }
+ if (!op1.isA(SqlKind.LITERAL)) {
+ return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL,
simplifyGenericNode((RexCall) a));
+ }
+ if (checkLiteralValue(op1, BigDecimal.ZERO)) {
+ return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL,
simplifyGenericNode((RexCall) a));
+ }
+ // op1 is a non-null and non-zero literal, so simplify op0
+ return simplifyIsNull(op0);
+ }
+ return null;
Review Comment:
The code handling the division here is very similar to the code inside
`simplifyIsNotNull` so I am wondering if it's worth refactoring it into a
separate private method.
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -1140,9 +1037,23 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs
unknownAs) {
switch (a.getKind()) {
case LITERAL:
return rexBuilder.makeLiteral(!((RexLiteral) a).isNull());
+ case DIVIDE: {
+ RexNode op0 = ((RexCall) a).getOperands().get(0);
+ RexNode op1 = ((RexCall) a).getOperands().get(1);
+ if (RexUtil.isNull(op0) || RexUtil.isNull(op1)) {
+ return rexBuilder.makeLiteral(false);
+ }
Review Comment:
If we handle simplifications of arithmetic operations with nulls this code
seems redundant and it might be unreachable. For instance, if `simplifyDivide`
is able to transform `x/null` and `null/y` to `null` then I guess we cannot hit
this path.
##########
core/src/main/java/org/apache/calcite/plan/Strong.java:
##########
@@ -356,7 +358,8 @@ private static Map<SqlKind, Policy> createPolicyMap() {
map.put(SqlKind.CHECKED_TIMES, Policy.ANY);
map.put(SqlKind.CHECKED_DIVIDE, Policy.ANY);
Review Comment:
Wondering if we should also handle `CHECKED_DIVIDE` with the same custom
policy.
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -488,9 +493,13 @@ private RexNode simplifyMultiply(RexCall e) {
}
private RexNode simplifyDivide(RexCall e) {
- final int oneIndex = findLiteralIndex(e.operands, BigDecimal.ONE);
- if (oneIndex == 1) {
- RexNode leftOperand = e.getOperands().get(0);
+ RexNode leftOperand = e.getOperands().get(0);
+ RexNode rightOperand = e.getOperands().get(1);
+ if ((isSafeExpression(leftOperand) && STRONG.isNull(leftOperand))
Review Comment:
This appears to be somewhat "new" simplification logic that was not there
before. I get the feeling that simplifications of arithmetic operations with
nulls is a problem that we should tackle in a more general fashion and not only
for the division operator. From my point of view this deserves a separate JIRA
ticket and PR dedicated to simplification of arithmetic operations with nulls.
I see there is already some code doing this for comparison operators
(`simplifyComparisonWithNull`).
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -1028,9 +1037,25 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs
unknownAs) {
switch (a.getKind()) {
case LITERAL:
return rexBuilder.makeLiteral(!((RexLiteral) a).isNull());
+ case DIVIDE: {
+ RexNode op0 = ((RexCall) a).getOperands().get(0);
+ RexNode op1 = ((RexCall) a).getOperands().get(1);
+ if (RexUtil.isNull(op0) || RexUtil.isNull(op1)) {
+ return rexBuilder.makeLiteral(false);
+ }
+ if (!op1.isA(SqlKind.LITERAL)) {
+ return rexBuilder.makeCall(
+ SqlStdOperatorTable.IS_NOT_NULL, simplifyGenericNode((RexCall)
a));
+ }
+ if (checkLiteralValue(op1, BigDecimal.ZERO)) {
+ return rexBuilder.makeCall(
+ SqlStdOperatorTable.IS_NOT_NULL, simplifyGenericNode((RexCall)
a));
+ }
+ // op1 is a non-null and non-zero literal, so simplify op0
+ return simplifyIsNotNull(op0);
+ }
default:
- throw new AssertionError("every CUSTOM policy needs a handler, "
- + a.getKind());
+ return null;
Review Comment:
Actually, I suggested to not add a **new** Assertion in `simplifyIsNull`
method to avoid breaking changes. Removing the assertion from here for
consistency purposes makes sense but I wouldn't push hard on it especially if
others disagree.
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -415,17 +415,22 @@ private RexNode simplifyGenericNode(RexCall e) {
*/
private static int findLiteralIndex(List<RexNode> operands, BigDecimal
value) {
for (int i = 0; i < operands.size(); i++) {
- if (operands.get(i).isA(SqlKind.LITERAL)) {
- Comparable comparable = ((RexLiteral) operands.get(i)).getValue();
- if (comparable instanceof BigDecimal
- && value.compareTo((BigDecimal) comparable) == 0) {
- return i;
- }
+ if (checkLiteralValue(operands.get(i), value)) {
+ return i;
}
}
return -1;
}
+ /** Check whether the operand is a literal of the specified value. */
Review Comment:
It seems that this method will always return false for approximate numeric
(DOUBLE, FLOAT, REAL) literals since `comparable instanceof BigDecimal` is
`false`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]