You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2017/06/06 03:00:40 UTC

[5/5] calcite git commit: [CALCITE-1829] Add TIME/TIMESTAMP/DATE datatype handling to RexImplicationChecker

[CALCITE-1829] Add TIME/TIMESTAMP/DATE datatype handling to RexImplicationChecker

Close apache/calcite#467


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

Branch: refs/heads/master
Commit: 419b810fe67539cf7a3d653e918352fbaf10e8b0
Parents: 91d9576
Author: Minji Kim <mi...@dremio.com>
Authored: Fri Jun 2 13:52:45 2017 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Mon Jun 5 18:41:27 2017 -0700

----------------------------------------------------------------------
 .../apache/calcite/plan/VisitorDataContext.java | 63 ++++++--------------
 .../java/org/apache/calcite/rex/RexLiteral.java |  7 +++
 .../calcite/test/RexImplicationCheckerTest.java | 35 +++++++----
 3 files changed, 47 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/419b810f/core/src/main/java/org/apache/calcite/plan/VisitorDataContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/plan/VisitorDataContext.java b/core/src/main/java/org/apache/calcite/plan/VisitorDataContext.java
index e2ab6b5..38b2ce3 100644
--- a/core/src/main/java/org/apache/calcite/plan/VisitorDataContext.java
+++ b/core/src/main/java/org/apache/calcite/plan/VisitorDataContext.java
@@ -36,8 +36,6 @@ import org.apache.calcite.util.trace.CalciteLogger;
 import org.slf4j.LoggerFactory;
 
 import java.math.BigDecimal;
-import java.sql.Date;
-import java.util.Calendar;
 import java.util.List;
 
 /**
@@ -116,7 +114,7 @@ public class VisitorDataContext implements DataContext {
     if (inputRef instanceof RexInputRef
         && literal instanceof RexLiteral)  {
       final int index = ((RexInputRef) inputRef).getIndex();
-      Object value = ((RexLiteral) literal).getValue();
+      final RexLiteral rexLiteral = (RexLiteral) literal;
       final RelDataType type = inputRef.getType();
 
       if (type.getSqlTypeName() == null) {
@@ -126,61 +124,36 @@ public class VisitorDataContext implements DataContext {
 
       switch (type.getSqlTypeName()) {
       case INTEGER:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, ((BigDecimal) value).intValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Integer.class));
       case DOUBLE:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, ((BigDecimal) value).doubleValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Double.class));
       case REAL:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, ((BigDecimal) value).floatValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Float.class));
       case BIGINT:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, ((BigDecimal) value).longValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Long.class));
       case SMALLINT:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, ((BigDecimal) value).shortValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Short.class));
       case TINYINT:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, (short) ((BigDecimal) value).byteValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Byte.class));
       case DECIMAL:
-        if (value instanceof BigDecimal) {
-          return Pair.of(index, value);
-        }
+        return Pair.of(index, rexLiteral.getValueAs(BigDecimal.class));
       case DATE:
-        if (value instanceof NlsString) {
-          value = ((RexLiteral) literal).getValue2();
-          final Date dateValue = Date.valueOf((String) value);
-          return Pair.of(index, dateValue);
-        } else if (value instanceof Calendar) {
-          final long timeInMillis = ((Calendar) value).getTimeInMillis();
-          return Pair.of(index, new Date(timeInMillis));
-        }
+      case TIME:
+        return Pair.of(index, rexLiteral.getValueAs(Integer.class));
+      case TIMESTAMP:
+        return Pair.of(index, rexLiteral.getValueAs(Long.class));
       case CHAR:
-        if (value instanceof NlsString) {
-          // TODO: Support collation. Not supported in NlsString compare too.
-          final NlsString nl = (NlsString) value;
-          return Pair.of(index, nl.getValue().charAt(0));
-        }
+        return Pair.of(index, rexLiteral.getValueAs(Character.class));
       case VARCHAR:
-        if (value instanceof NlsString) {
-          // TODO: Support coallation. Not supported in {@link #NlsString} compare too.
-          return Pair.of(index, ((NlsString) value).getValue());
-        }
+        return Pair.of(index, rexLiteral.getValueAs(String.class));
       default:
         //TODO: Support few more supported cases
         LOGGER.warn("{} for value of class {} is being handled in default way",
-            type.getSqlTypeName(), value.getClass());
-        if (value instanceof NlsString) {
-          return Pair.of(index, ((NlsString) value).getValue());
+            type.getSqlTypeName(), rexLiteral.getValue().getClass());
+        if (rexLiteral.getValue() instanceof NlsString) {
+          return Pair.of(index, ((NlsString) rexLiteral.getValue()).getValue());
         } else {
-          return Pair.of(index, value);
+          return Pair.of(index, rexLiteral.getValue());
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/calcite/blob/419b810f/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexLiteral.java b/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
index 97e3055..674cdd8 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
@@ -718,6 +718,13 @@ public class RexLiteral extends RexNode {
     case CHAR:
       if (clazz == String.class) {
         return clazz.cast(((NlsString) value).getValue());
+      } else if (clazz == Character.class) {
+        return clazz.cast(((NlsString) value).getValue().charAt(0));
+      }
+      break;
+    case VARCHAR:
+      if (clazz == String.class) {
+        return clazz.cast(((NlsString) value).getValue());
       }
       break;
     case DECIMAL:

http://git-wip-us.apache.org/repos/asf/calcite/blob/419b810f/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 f089170..331fb06 100644
--- a/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java
@@ -43,17 +43,16 @@ import org.apache.calcite.util.TimeString;
 import org.apache.calcite.util.TimestampString;
 import org.apache.calcite.util.Util;
 
-import org.junit.Ignore;
 import org.junit.Test;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
 import java.math.BigDecimal;
 import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 /**
  * Unit tests for {@link RexImplicationChecker}.
  */
@@ -178,36 +177,46 @@ public class RexImplicationCheckerTest {
     f.checkImplies(node1, node1);
   }
 
-  @Ignore("work in progress")
   @Test public void testSimpleDate() {
     final Fixture f = new Fixture();
     final DateString d = DateString.fromCalendarFields(Util.calendar());
     final RexNode node1 = f.ge(f.dt, f.rexBuilder.makeDateLiteral(d));
     final RexNode node2 = f.eq(f.dt, f.rexBuilder.makeDateLiteral(d));
-
     f.checkImplies(node2, node1);
     f.checkNotImplies(node1, node2);
+
+    final DateString dBeforeEpoch1 = DateString.fromDaysSinceEpoch(-12345);
+    final DateString dBeforeEpcoh2 = DateString.fromDaysSinceEpoch(-123);
+    final RexNode nodeBe1 = f.lt(f.dt, f.rexBuilder.makeDateLiteral(dBeforeEpoch1));
+    final RexNode nodeBe2 = f.lt(f.dt, f.rexBuilder.makeDateLiteral(dBeforeEpcoh2));
+    f.checkImplies(nodeBe1, nodeBe2);
+    f.checkNotImplies(nodeBe2, nodeBe1);
   }
 
-  @Ignore("work in progress")
   @Test public void testSimpleTimeStamp() {
     final Fixture f = new Fixture();
     final TimestampString ts =
         TimestampString.fromCalendarFields(Util.calendar());
-    final RexNode node1 = f.le(f.ts, f.timestampLiteral(ts));
+    final RexNode node1 = f.lt(f.ts, f.timestampLiteral(ts));
     final RexNode node2 = f.le(f.ts, f.timestampLiteral(ts));
-
     f.checkImplies(node1, node2);
     f.checkNotImplies(node2, node1);
+
+    final TimestampString tsBeforeEpoch1 =
+        TimestampString.fromMillisSinceEpoch(-1234567890L);
+    final TimestampString tsBeforeEpoch2 =
+        TimestampString.fromMillisSinceEpoch(-1234567L);
+    final RexNode nodeBe1 = f.lt(f.ts, f.timestampLiteral(tsBeforeEpoch1));
+    final RexNode nodeBe2 = f.lt(f.ts, f.timestampLiteral(tsBeforeEpoch2));
+    f.checkImplies(nodeBe1, nodeBe2);
+    f.checkNotImplies(nodeBe2, nodeBe1);
   }
 
-  @Ignore("work in progress")
   @Test public void testSimpleTime() {
     final Fixture f = new Fixture();
     final TimeString t = TimeString.fromCalendarFields(Util.calendar());
-    final RexNode node1 = f.le(f.ts, f.timeLiteral(t));
-    final RexNode node2 = f.le(f.ts, f.timeLiteral(t));
-
+    final RexNode node1 = f.lt(f.t, f.timeLiteral(t));
+    final RexNode node2 = f.le(f.t, f.timeLiteral(t));
     f.checkImplies(node1, node2);
     f.checkNotImplies(node2, node1);
   }