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 2014/08/11 21:46:02 UTC

[5/8] git commit: Remove the 2-minute wait at the top of the hour for tests of CURRENT_TIME, etc.

Remove the 2-minute wait at the top of the hour for tests of CURRENT_TIME, etc.

For a little variety, we still do it the old way if -Doptiq.test.slow=true.


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

Branch: refs/heads/master
Commit: d8bf47383dac4537ef380ef0e381d91de797e169
Parents: 7c205f0
Author: Julian Hyde <jh...@apache.org>
Authored: Fri Aug 8 14:06:53 2014 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Fri Aug 8 14:06:53 2014 -0700

----------------------------------------------------------------------
 .../java/net/hydromatic/optiq/DataContext.java  |  2 +
 .../optiq/jdbc/OptiqConnectionImpl.java         | 15 ++--
 .../java/net/hydromatic/optiq/runtime/Hook.java |  4 +
 .../eigenbase/sql/test/SqlOperatorBaseTest.java | 81 +++++++++++++-------
 4 files changed, 70 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/d8bf4738/core/src/main/java/net/hydromatic/optiq/DataContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/net/hydromatic/optiq/DataContext.java b/core/src/main/java/net/hydromatic/optiq/DataContext.java
index 5b7e420..6a9ff10 100644
--- a/core/src/main/java/net/hydromatic/optiq/DataContext.java
+++ b/core/src/main/java/net/hydromatic/optiq/DataContext.java
@@ -62,6 +62,8 @@ public interface DataContext {
 
   /** Variable that may be asked for in a call to {@link DataContext#get}. */
   enum Variable {
+    UTC_TIMESTAMP("utcTimestamp", Long.class),
+
     /** The time at which the current statement started executing. In
      * milliseconds after 1970-01-01 00:00:00, UTC. Required. */
     CURRENT_TIMESTAMP("currentTimestamp", Long.class),

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/d8bf4738/core/src/main/java/net/hydromatic/optiq/jdbc/OptiqConnectionImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/net/hydromatic/optiq/jdbc/OptiqConnectionImpl.java b/core/src/main/java/net/hydromatic/optiq/jdbc/OptiqConnectionImpl.java
index f4bac67..e030140 100644
--- a/core/src/main/java/net/hydromatic/optiq/jdbc/OptiqConnectionImpl.java
+++ b/core/src/main/java/net/hydromatic/optiq/jdbc/OptiqConnectionImpl.java
@@ -30,6 +30,7 @@ import net.hydromatic.optiq.config.OptiqConnectionProperty;
 import net.hydromatic.optiq.impl.AbstractSchema;
 import net.hydromatic.optiq.impl.java.JavaTypeFactory;
 import net.hydromatic.optiq.prepare.OptiqCatalogReader;
+import net.hydromatic.optiq.runtime.Hook;
 import net.hydromatic.optiq.server.OptiqServer;
 import net.hydromatic.optiq.server.OptiqServerStatement;
 
@@ -272,16 +273,20 @@ abstract class OptiqConnectionImpl
       // Store the time at which the query started executing. The SQL
       // standard says that functions such as CURRENT_TIMESTAMP return the
       // same value throughout the query.
-      final long time = System.currentTimeMillis();
+      final long[] times = {System.currentTimeMillis()};
+
+      // Give a hook chance to alter the clock.
+      Hook.CURRENT_TIME.run(times);
+      final long time = times[0];
       final TimeZone timeZone = connection.getTimeZone();
       final long localOffset = timeZone.getOffset(time);
       final long currentOffset = localOffset;
 
       ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
-      builder.put("utcTimestamp", time)
-          .put("currentTimestamp", time + currentOffset)
-          .put("localTimestamp", time + localOffset)
-          .put("timeZone", timeZone);
+      builder.put(Variable.UTC_TIMESTAMP.camelName, time)
+          .put(Variable.CURRENT_TIMESTAMP.camelName, time + currentOffset)
+          .put(Variable.LOCAL_TIMESTAMP.camelName, time + localOffset)
+          .put(Variable.TIME_ZONE.camelName, timeZone);
       for (Ord<Object> value : Ord.zip(parameterValues)) {
         Object e = value.e;
         if (e == null) {

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/d8bf4738/core/src/main/java/net/hydromatic/optiq/runtime/Hook.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/net/hydromatic/optiq/runtime/Hook.java b/core/src/main/java/net/hydromatic/optiq/runtime/Hook.java
index 5bb42e7..cc803d4 100644
--- a/core/src/main/java/net/hydromatic/optiq/runtime/Hook.java
+++ b/core/src/main/java/net/hydromatic/optiq/runtime/Hook.java
@@ -29,6 +29,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
  * <p>For testing and debugging rather than for end-users.</p>
  */
 public enum Hook {
+  /** Called to get the current time. Use this to return a predictable time
+   * in tests. */
+  CURRENT_TIME,
+
   /** Called with the SQL string and parse tree, in an array. */
   PARSE_TREE,
 

http://git-wip-us.apache.org/repos/asf/incubator-optiq/blob/d8bf4738/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java b/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
index a5c4a83..232a43a 100644
--- a/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/eigenbase/sql/test/SqlOperatorBaseTest.java
@@ -32,6 +32,11 @@ import org.eigenbase.sql.util.SqlString;
 import org.eigenbase.test.*;
 import org.eigenbase.util.*;
 
+import net.hydromatic.optiq.runtime.Hook;
+import net.hydromatic.optiq.test.OptiqAssert;
+
+import com.google.common.base.Function;
+
 import org.junit.*;
 
 import static org.junit.Assert.*;
@@ -3739,18 +3744,18 @@ public abstract class SqlOperatorBaseTest {
         "LOCALTIME(1)", TIME_PATTERN,
         "TIME(1) NOT NULL");
 
+    final Pair<String, Hook.Closeable> pair = currentTimeString(LOCAL_TZ);
     tester.checkScalar(
         "CAST(LOCALTIME AS VARCHAR(30))",
         Pattern.compile(
-            currentTimeString(LOCAL_TZ).substring(11)
-                + "[0-9][0-9]:[0-9][0-9]"),
+            pair.left.substring(11) + "[0-9][0-9]:[0-9][0-9]"),
         "VARCHAR(30) NOT NULL");
     tester.checkScalar(
         "LOCALTIME",
         Pattern.compile(
-            currentTimeString(LOCAL_TZ).substring(11)
-                + "[0-9][0-9]:[0-9][0-9]"),
+            pair.left.substring(11) + "[0-9][0-9]:[0-9][0-9]"),
         "TIME(0) NOT NULL");
+    pair.right.close();
   }
 
   @Test public void testLocalTimestampFunc() {
@@ -3770,18 +3775,17 @@ public abstract class SqlOperatorBaseTest {
 
     // Check that timestamp is being generated in the right timezone by
     // generating a specific timestamp.
+    final Pair<String, Hook.Closeable> pair = currentTimeString(
+        LOCAL_TZ);
     tester.checkScalar(
         "CAST(LOCALTIMESTAMP AS VARCHAR(30))",
-        Pattern.compile(
-            currentTimeString(LOCAL_TZ)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left + "[0-9][0-9]:[0-9][0-9]"),
         "VARCHAR(30) NOT NULL");
     tester.checkScalar(
         "LOCALTIMESTAMP",
-        Pattern.compile(
-            currentTimeString(LOCAL_TZ)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left + "[0-9][0-9]:[0-9][0-9]"),
         "TIMESTAMP(0) NOT NULL");
+    pair.right.close();
   }
 
   @Test public void testCurrentTimeFunc() {
@@ -3796,18 +3800,16 @@ public abstract class SqlOperatorBaseTest {
     tester.checkScalar(
         "CURRENT_TIME(1)", TIME_PATTERN, "TIME(1) NOT NULL");
 
+    final Pair<String, Hook.Closeable> pair = currentTimeString(CURRENT_TZ);
     tester.checkScalar(
         "CAST(CURRENT_TIME AS VARCHAR(30))",
-        Pattern.compile(
-            currentTimeString(CURRENT_TZ).substring(11)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left.substring(11) + "[0-9][0-9]:[0-9][0-9]"),
         "VARCHAR(30) NOT NULL");
     tester.checkScalar(
         "CURRENT_TIME",
-        Pattern.compile(
-            currentTimeString(CURRENT_TZ).substring(11)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left.substring(11) + "[0-9][0-9]:[0-9][0-9]"),
         "TIME(0) NOT NULL");
+    pair.right.close();
   }
 
   @Test public void testCurrentTimestampFunc() {
@@ -3825,18 +3827,17 @@ public abstract class SqlOperatorBaseTest {
         "CURRENT_TIMESTAMP(1)", TIMESTAMP_PATTERN,
         "TIMESTAMP(1) NOT NULL");
 
+    final Pair<String, Hook.Closeable> pair = currentTimeString(
+        CURRENT_TZ);
     tester.checkScalar(
         "CAST(CURRENT_TIMESTAMP AS VARCHAR(30))",
-        Pattern.compile(
-            currentTimeString(CURRENT_TZ)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left + "[0-9][0-9]:[0-9][0-9]"),
         "VARCHAR(30) NOT NULL");
     tester.checkScalar(
         "CURRENT_TIMESTAMP",
-        Pattern.compile(
-            currentTimeString(CURRENT_TZ)
-                + "[0-9][0-9]:[0-9][0-9]"),
+        Pattern.compile(pair.left + "[0-9][0-9]:[0-9][0-9]"),
         "TIMESTAMP(0) NOT NULL");
+    pair.right.close();
   }
 
   /**
@@ -3849,12 +3850,36 @@ public abstract class SqlOperatorBaseTest {
    * @param tz Time zone
    * @return Time string
    */
-  protected static String currentTimeString(TimeZone tz) {
-    final Calendar calendar = getCalendarNotTooNear(Calendar.HOUR_OF_DAY);
+  protected static Pair<String, Hook.Closeable> currentTimeString(TimeZone tz) {
+    final Calendar calendar;
+    final Hook.Closeable closeable;
+    if (OptiqAssert.ENABLE_SLOW) {
+      calendar = getCalendarNotTooNear(Calendar.HOUR_OF_DAY);
+      closeable = new Hook.Closeable() {
+        public void close() {}
+      };
+    } else {
+      calendar = Calendar.getInstance();
+      calendar.set(Calendar.YEAR, 2014);
+      calendar.set(Calendar.MONTH, 8);
+      calendar.set(Calendar.DATE, 7);
+      calendar.set(Calendar.HOUR_OF_DAY, 17);
+      calendar.set(Calendar.MINUTE, 8);
+      calendar.set(Calendar.SECOND, 48);
+      calendar.set(Calendar.MILLISECOND, 15);
+      final long timeInMillis = calendar.getTimeInMillis();
+      closeable = Hook.CURRENT_TIME.addThread(
+          new Function<long[], Void>() {
+            public Void apply(long[] o) {
+              o[0] = timeInMillis;
+              return null;
+            }
+          });
+    }
 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:");
     sdf.setTimeZone(tz);
-    return sdf.format(calendar.getTime());
+    return Pair.of(sdf.format(calendar.getTime()), closeable);
   }
 
   @Test public void testCurrentDateFunc() {
@@ -3873,14 +3898,16 @@ public abstract class SqlOperatorBaseTest {
         false);
 
     // Check the actual value.
+    final Pair<String, Hook.Closeable> pair = currentTimeString(LOCAL_TZ);
     tester.checkScalar(
         "CAST(CURRENT_DATE AS VARCHAR(30))",
-        currentTimeString(LOCAL_TZ).substring(0, 10),
+        pair.left.substring(0, 10),
         "VARCHAR(30) NOT NULL");
     tester.checkScalar(
         "CURRENT_DATE",
-        currentTimeString(LOCAL_TZ).substring(0, 10),
+        pair.left.substring(0, 10),
         "DATE NOT NULL");
+    pair.right.close();
   }
 
   @Test public void testSubstringFunction() {