kgyrtkirk commented on a change in pull request #1854: [CALCITE-3852]
RexSimplify doesn't simplify NOT EQUAL predicates
URL: https://github.com/apache/calcite/pull/1854#discussion_r393480796
##########
File path: core/src/main/java/org/apache/calcite/rex/RexSimplify.java
##########
@@ -1560,18 +1560,62 @@ RexNode simplifyAnd2ForUnknownAsFalse(List<RexNode>
terms,
return RexUtil.composeConjunction(rexBuilder, terms);
}
+ private RexNode simplifyNotEqual(RexNode e) {
+ final Comparison comparison = Comparison.of(e);
+ // Check for comparison with null values
+ if (comparison == null
+ || comparison.literal.getValue() == null) {
+ return e;
+ }
+
+ for (RexNode node: predicates.pulledUpPredicates) {
+ final Comparison predicate = Comparison.of(node);
+ if (predicate == null
+ || predicate.kind != SqlKind.EQUALS
+ || predicate.literal.getValue() == null
+ || !predicate.ref.equals(comparison.ref)) {
+ continue;
+ }
+
+ // Given x=5 and x is not nullable, x!=5 can be simplified to false and
x!=3 can be
+ // simplified to true.
+ // Given x=5 and x is nullable, x!=5 can be simplified to 'null and x is
null' and x!=3 can
+ // be simplified to 'null or x is not null'.
+ final RelDataType type = comparison.ref.getType();
+ final boolean nullable = type.isNullable();
+ if (predicate.literal.equals(comparison.literal)) {
+ return nullable ? rexBuilder.makeCall(SqlStdOperatorTable.AND,
+ rexBuilder.makeNullLiteral(type),
+ rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, comparison.ref))
+ : rexBuilder.makeLiteral(false);
+ } else {
+ return nullable ? rexBuilder.makeCall(SqlStdOperatorTable.OR,
+ rexBuilder.makeNullLiteral(type),
+ rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
comparison.ref))
+ : rexBuilder.makeLiteral(true);
+ }
+ }
+ return e;
+ }
+
private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
Class<C> clazz) {
if (predicates.pulledUpPredicates.isEmpty()) {
return e;
}
+
+ if (e.getKind() == SqlKind.NOT_EQUALS) {
Review comment:
I believe the current simplification logic could be extended to also handle
the case when we have an `a!=0` predicate; and we have an `a!=0` node to
simplify.
this wasn't working before this patch; so it's entirely optional...
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services