danny0405 commented on a change in pull request #2065:
URL: https://github.com/apache/calcite/pull/2065#discussion_r454931771



##########
File path: core/src/test/java/org/apache/calcite/rex/RexNormalizeTest.java
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rex;
+
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/** Test cases for {@link RexNormalize}. */
+class RexNormalizeTest extends RexProgramTestBase {
+
+  @Test void digestIsNormalized() {
+    final RexNode node0 = and(or(vBool(1), vBool(0)), vBool(0));
+    final RexNode node1 = and(vBool(0), or(vBool(0), vBool(1)));
+    assertNodeEquals(node0, node1, true);
+
+    final RexNode node2 = eq(vVarchar(0), literal("0123456789012345"));
+    final RexNode node3 = eq(literal("0123456789012345"), vVarchar(0));
+    assertNodeEquals(node2, node3, true);
+
+    final RexNode node4 = eq(vVarchar(0), literal("01"));
+    final RexNode node5 = eq(literal("01"), vVarchar(0));
+    assertNodeEquals(node4, node5, true);
+  }
+
+  @Test void reversibleNormalizedToLess() {
+    // Same type operands.
+    final RexNode node0 = lt(vBool(0), vBool(0));
+    final RexNode node1 = gt(vBool(0), vBool(0));
+    assertNodeEquals(node0, node1, true);
+
+    final RexNode node2 = le(vBool(0), vBool(0));
+    final RexNode node3 = ge(vBool(0), vBool(0));
+    assertNodeEquals(node2, node3, true);
+
+    // Different type operands.
+    final RexNode node4 = lt(vSmallInt(0), vInt(1));
+    final RexNode node5 = gt(vInt(1), vSmallInt(0));
+    assertNodeEquals(node4, node5, true);
+
+    final RexNode node6 = le(vSmallInt(0), vInt(1));
+    final RexNode node7 = ge(vInt(1), vSmallInt(0));
+    assertNodeEquals(node6, node7, true);
+  }
+
+  @Test void reversibleDifferentArgTypesShouldNotBeShuffled() {
+    final RexNode node0 = plus(vSmallInt(1), vInt(0));
+    final RexNode node1 = plus(vInt(0), vSmallInt(1));
+    assertNodeEquals(node0, node1, false);
+
+    final RexNode node2 = mul(vSmallInt(0), vInt(1));
+    final RexNode node3 = mul(vInt(1), vSmallInt(0));
+    assertNodeEquals(node2, node3, false);
+  }
+
+  @Test void reversibleDifferentNullabilityArgsAreNormalized() {
+    final RexNode node0 = plus(vIntNotNull(0), vInt(1));
+    final RexNode node1 = plus(vInt(1), vIntNotNull(0));
+    assertNodeEquals(node0, node1, true);
+
+    final RexNode node2 = mul(vIntNotNull(1), vInt(0));
+    final RexNode node3 = mul(vInt(0), vIntNotNull(1));
+    assertNodeEquals(node2, node3, true);
+  }
+
+  @Test void symmetricalDifferentArgOps() {
+    final RexNode node0 = eq(vBool(0), vBool(1));
+    final RexNode node1 = eq(vBool(1), vBool(0));
+    assertNodeEquals(node0, node1, true);
+
+    final RexNode node2 = ne(vBool(0), vBool(1));
+    final RexNode node3 = ne(vBool(1), vBool(0));
+    assertNodeEquals(node2, node3, true);
+  }
+
+  @Test void reversibleDifferentArgOps() {
+    final RexNode node0 = lt(vBool(0), vBool(1));
+    final RexNode node1 = lt(vBool(1), vBool(0));
+    assertNodeEquals(node0, node1, false);
+
+    final RexNode node2 = le(vBool(0), vBool(1));
+    final RexNode node3 = le(vBool(1), vBool(0));
+    assertNodeEquals(node2, node3, false);
+
+    final RexNode node4 = gt(vBool(0), vBool(1));
+    final RexNode node5 = gt(vBool(1), vBool(0));
+    assertNodeEquals(node4, node5, false);
+
+    final RexNode node6 = ge(vBool(0), vBool(1));
+    final RexNode node7 = ge(vBool(1), vBool(0));
+    assertNodeEquals(node6, node7, false);
+  }
+
+  /** Assert whether two rex nodes are equal. */
+  private static void assertNodeEquals(RexNode node1, RexNode node2, boolean 
equal) {
+    StringBuilder reason = new StringBuilder("The two rex nodes expect to be 
");
+    if (!equal) {
+      reason.append("not ");
+    }
+    reason.append("equal");

Review comment:
       Well, let me fix it.




----------------------------------------------------------------
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]


Reply via email to