Skip to content

Commit

Permalink
[CALCITE-5837] Make RexUtil#pullFactors method RexNode condition orde…
Browse files Browse the repository at this point in the history
…red when input RexNode's kind is OR
  • Loading branch information
LakeShen committed Jul 14, 2023
1 parent 55f714c commit 08b208f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/main/java/org/apache/calcite/rex/RexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -2670,7 +2671,8 @@ private List<RexNode> pullList(List<RexNode> nodes) {
}

private static Map<RexNode, RexNode> commonFactors(List<RexNode> nodes) {
final Map<RexNode, RexNode> map = new HashMap<>();
// make sure the result is in deterministic order.
final Map<RexNode, RexNode> map = new LinkedHashMap<>();
int i = 0;
for (RexNode node : nodes) {
if (i++ == 0) {
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ private void checkExponentialCnf(int n) {
final RelDataType booleanType =
typeFactory.createSqlType(SqlTypeName.BOOLEAN);
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
final RelDataType rowType = typeFactory.builder()
.add("a", booleanType)
.add("b", booleanType)
Expand All @@ -748,6 +749,7 @@ private void checkExponentialCnf(int n) {
.add("f", booleanType)
.add("g", booleanType)
.add("h", intType)
.add("i", varcharType)
.build();

final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
Expand All @@ -759,6 +761,7 @@ private void checkExponentialCnf(int n) {
final RexNode fRef = rexBuilder.makeFieldAccess(range, 5);
final RexNode gRef = rexBuilder.makeFieldAccess(range, 6);
final RexNode hRef = rexBuilder.makeFieldAccess(range, 7);
final RexNode iRef = rexBuilder.makeFieldAccess(range, 8);

final RexNode hEqSeven = eq(hRef, literal(7));

Expand Down Expand Up @@ -801,6 +804,17 @@ private void checkExponentialCnf(int n) {
and(eRef,
or(fRef,
and(gRef, or(trueLiteral, falseLiteral)))))))));

checkPullFactors(
or(
and(aRef, eq(hRef, literal(12)), bRef,
rexBuilder.makeIn(iRef, ImmutableList.of(literal("a"), literal("b")))),
and(aRef, eq(hRef, literal(13)), bRef,
rexBuilder.makeIn(iRef, ImmutableList.of(literal("c"), literal("d")))),
and(aRef, eq(hRef, literal(14)), bRef,
rexBuilder.makeIn(iRef, ImmutableList.of(literal("e"), literal("f"))))),
"AND(?0.a, ?0.b, OR(AND(=(?0.h, 12), SEARCH(?0.i, Sarg['a', 'b']:CHAR(1))), AND(=(?0.h, 13), "
+ "SEARCH(?0.i, Sarg['c', 'd']:CHAR(1))), AND(=(?0.h, 14), SEARCH(?0.i, Sarg['e', 'f']:CHAR(1)))))");
}

@Test void testSimplify() {
Expand Down

0 comments on commit 08b208f

Please sign in to comment.