You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jc...@apache.org on 2018/05/29 23:50:04 UTC

calcite git commit: [CALCITE-2334] Extend simplification of expressions with CEIL function over date types

Repository: calcite
Updated Branches:
  refs/heads/master 76fd6c41d -> e7a4e8431


[CALCITE-2334] Extend simplification of expressions with CEIL function over date types

Close apache/calcite#705


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/e7a4e843
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/e7a4e843
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/e7a4e843

Branch: refs/heads/master
Commit: e7a4e8431f56ee4acd92b0b2ec5731a97f4e2be2
Parents: 76fd6c4
Author: Jesus Camacho Rodriguez <jc...@apache.org>
Authored: Tue May 29 16:37:01 2018 -0700
Committer: Jesus Camacho Rodriguez <jc...@apache.org>
Committed: Tue May 29 16:46:45 2018 -0700

----------------------------------------------------------------------
 .../org/apache/calcite/rex/RexSimplify.java     | 17 ++++-----
 .../calcite/test/RexImplicationCheckerTest.java | 36 +++++++++++++++-----
 2 files changed, 37 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/e7a4e843/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
index 9b0df88..d51ffe7 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -174,8 +174,9 @@ public class RexSimplify {
       return simplifyCoalesce((RexCall) e);
     case CAST:
       return simplifyCast((RexCall) e);
+    case CEIL:
     case FLOOR:
-      return simplifyFloor((RexCall) e);
+      return simplifyCeilFloor((RexCall) e);
     case IS_NULL:
     case IS_NOT_NULL:
     case IS_TRUE:
@@ -1159,31 +1160,31 @@ public class RexSimplify {
     }
   }
 
-  /** Tries to simplify FLOOR function on top of FLOOR.
+  /** Tries to simplify CEIL/FLOOR function on top of CEIL/FLOOR.
    *
    * <p>Examples:
    * <ul>
    *
    * <li>{@code floor(floor($0, flag(hour)), flag(day))} returns {@code floor($0, flag(day))}
    *
-   * <li>{@code floor(floor($0, flag(second)), flag(day))} returns {@code floor($0, flag(day))}
+   * <li>{@code ceil(ceil($0, flag(second)), flag(day))} returns {@code ceil($0, flag(day))}
    *
    * <li>{@code floor(floor($0, flag(day)), flag(second))} does not change
    *
    * </ul>
    */
-  private RexNode simplifyFloor(RexCall e) {
+  private RexNode simplifyCeilFloor(RexCall e) {
     if (e.getOperands().size() != 2) {
       // Bail out since we only simplify floor <date>
       return e;
     }
     final RexNode operand = simplify_(e.getOperands().get(0));
-    switch (operand.getKind()) {
-    case FLOOR:
-      // FLOOR on top of FLOOR
+    if (e.getKind() == operand.getKind()) {
+      assert e.getKind() == SqlKind.CEIL || e.getKind() == SqlKind.FLOOR;
+      // CEIL/FLOOR on top of CEIL/FLOOR
       final RexCall child = (RexCall) operand;
       if (child.getOperands().size() != 2) {
-        // Bail out since we only simplify floor <date>
+        // Bail out since we only simplify ceil/floor <date>
         return e;
       }
       final RexLiteral parentFlag = (RexLiteral) e.operands.get(1);

http://git-wip-us.apache.org/repos/asf/calcite/blob/e7a4e843/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java b/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
index e09487b..52d8d78 100644
--- a/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
@@ -376,8 +376,8 @@ public class RexImplicationCheckerTest {
         is("2014"));
   }
 
-  /** Test case for simplifier of floor. */
-  @Test public void testSimplifyFloor() {
+  /** Test case for simplifier of ceil/floor. */
+  @Test public void testSimplifyCeilFloor() {
     // We can add more time units here once they are supported in
     // RexInterpreter, e.g., TimeUnitRange.HOUR, TimeUnitRange.MINUTE,
     // TimeUnitRange.SECOND.
@@ -393,28 +393,48 @@ public class RexImplicationCheckerTest {
       final RexNode innerFloorCall = f.rexBuilder.makeCall(
           SqlStdOperatorTable.FLOOR, literalTs,
           f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
+      final RexNode innerCeilCall = f.rexBuilder.makeCall(
+          SqlStdOperatorTable.CEIL, literalTs,
+          f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
       for (int j = 0; j <= i; j++) {
         final RexNode outerFloorCall = f.rexBuilder.makeCall(
             SqlStdOperatorTable.FLOOR, innerFloorCall,
             f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
-        final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
-        assertThat(simplifiedExpr.getKind(), is(SqlKind.FLOOR));
-        assertThat(((RexLiteral) simplifiedExpr.getOperands().get(1)).getValue().toString(),
+        final RexNode outerCeilCall = f.rexBuilder.makeCall(
+            SqlStdOperatorTable.CEIL, innerCeilCall,
+            f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
+        final RexCall floorSimplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
+        assertThat(floorSimplifiedExpr.getKind(), is(SqlKind.FLOOR));
+        assertThat(((RexLiteral) floorSimplifiedExpr.getOperands().get(1)).getValue().toString(),
+            is(timeUnitRanges.get(j).toString()));
+        assertThat(floorSimplifiedExpr.getOperands().get(0).toString(), is(literalTs.toString()));
+        final RexCall ceilSimplifiedExpr = (RexCall) defaultSimplifier.apply(outerCeilCall);
+        assertThat(ceilSimplifiedExpr.getKind(), is(SqlKind.CEIL));
+        assertThat(((RexLiteral) ceilSimplifiedExpr.getOperands().get(1)).getValue().toString(),
             is(timeUnitRanges.get(j).toString()));
-        assertThat(simplifiedExpr.getOperands().get(0).toString(), is(literalTs.toString()));
+        assertThat(ceilSimplifiedExpr.getOperands().get(0).toString(), is(literalTs.toString()));
       }
     }
+
     // Negative test
     for (int i = timeUnitRanges.size() - 1; i >= 0; i--) {
       final RexNode innerFloorCall = f.rexBuilder.makeCall(
           SqlStdOperatorTable.FLOOR, literalTs,
           f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
+      final RexNode innerCeilCall = f.rexBuilder.makeCall(
+          SqlStdOperatorTable.CEIL, literalTs,
+          f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
       for (int j = timeUnitRanges.size() - 1; j > i; j--) {
         final RexNode outerFloorCall = f.rexBuilder.makeCall(
             SqlStdOperatorTable.FLOOR, innerFloorCall,
             f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
-        final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
-        assertThat(simplifiedExpr.toString(), is(outerFloorCall.toString()));
+        final RexNode outerCeilCall = f.rexBuilder.makeCall(
+            SqlStdOperatorTable.CEIL, innerCeilCall,
+            f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
+        final RexCall floorSimplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
+        assertThat(floorSimplifiedExpr.toString(), is(outerFloorCall.toString()));
+        final RexCall ceilSimplifiedExpr = (RexCall) defaultSimplifier.apply(outerCeilCall);
+        assertThat(ceilSimplifiedExpr.toString(), is(outerCeilCall.toString()));
       }
     }
   }