mihaibudiu commented on code in PR #5110:
URL: https://github.com/apache/calcite/pull/5110#discussion_r3616846000
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2367,9 +2364,46 @@ private RexNode simplifyOrs(List<RexNode> terms,
RexUnknownAs unknownAs) {
break;
}
}
+
+ // Absorption law: a OR (a AND b) => a
+ absorb(terms, SqlKind.AND);
+
return RexUtil.composeDisjunction(rexBuilder, terms);
}
+ /**
+ * Applies the absorption law to a list of terms, removing any composite term
+ * that is absorbed by a sibling term.
+ *
+ * <p>When {@code compositeKind} is {@link SqlKind#OR}, removes any
+ * {@code (a OR b)} term whose disjunctions contain a sibling {@code a}, so
+ * {@code a AND (a OR b) => a}. When it is {@link SqlKind#AND}, removes any
+ * {@code (a AND b)} term whose conjunctions contain a sibling {@code a}, so
+ * {@code a OR (a AND b) => a}.
+ *
+ * <p>The absorbing sibling {@code a} must be deterministic; otherwise its
two
+ * occurrences might evaluate differently and the rewrite would not be
+ * equivalence-preserving.
+ */
+ private static void absorb(List<RexNode> terms, SqlKind compositeKind) {
+ for (int i = 0; i < terms.size(); i++) {
+ final RexNode term = terms.get(i);
+ if (term.getKind() == compositeKind) {
+ final List<RexNode> components = compositeKind == SqlKind.OR
+ ? RelOptUtil.disjunctions(term)
Review Comment:
This looks useful, but these two function calls are not cheap.
I hope that this does not matter in practice, but this could be expensive
for some very complex boolean expressions.
--
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]