Skip to content

Commit

Permalink
TEIID-5204 moving command deterministic time functions to commandcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jan 8, 2018
1 parent 0f80bbe commit c3f6f7a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
Expand Up @@ -506,20 +506,20 @@ public static Object sqrt(Number x) {

// ================== Function = currentDate =====================

public static Object currentDate() {
return TimestampWithTimezone.createDate(new Date());
public static Object currentDate(CommandContext context) {
return context.currentDate();
}

// ================== Function = currentTime =====================

public static Object currentTime() {
return TimestampWithTimezone.createTime(new Date());
public static Object currentTime(CommandContext context) {
return context.currentTime();
}

// ================== Function = currentTimestamp =====================

public static Object currentTimestamp() {
return new Timestamp(System.currentTimeMillis());
public static Object currentTimestamp(CommandContext context) {
return context.currentTimestamp();
}

// ================== Helper for a bunch of date functions =====================
Expand Down
17 changes: 17 additions & 0 deletions engine/src/main/java/org/teiid/query/util/CommandContext.java
Expand Up @@ -24,6 +24,8 @@
import java.lang.ref.WeakReference;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
Expand Down Expand Up @@ -53,6 +55,7 @@
import org.teiid.core.util.ArgCheck;
import org.teiid.core.util.ExecutorUtils;
import org.teiid.core.util.LRUCache;
import org.teiid.core.util.TimestampWithTimezone;
import org.teiid.dqp.internal.process.AuthorizationValidator;
import org.teiid.dqp.internal.process.DQPWorkContext;
import org.teiid.dqp.internal.process.PreparedPlan;
Expand Down Expand Up @@ -201,6 +204,8 @@ private static class GlobalState implements Cloneable {
private Throwable batchUpdateException;

public boolean parallel;

private long timestamp = System.currentTimeMillis();
}

private GlobalState globalState = new GlobalState();
Expand Down Expand Up @@ -1205,5 +1210,17 @@ public boolean setParallel(boolean value) {
this.globalState.parallel = value;
return result;
}

public Date currentDate() {
return TimestampWithTimezone.createDate(new Date(this.globalState.timestamp));
}

public Time currentTime() {
return TimestampWithTimezone.createTime(new Date(this.globalState.timestamp));
}

public Timestamp currentTimestamp() {
return new Timestamp(this.globalState.timestamp);
}

}
Expand Up @@ -1109,7 +1109,7 @@ public void helpTestModifyTimeZone(String tsStr, String tzStart, String tzEnd, S
// case 2458
@Test public void testCurrentDate() throws Exception {

Date curDate = (Date)FunctionMethods.currentDate();
Date curDate = (Date)FunctionMethods.currentDate(new CommandContext());

Calendar cal1 = Calendar.getInstance();
cal1.setTime(curDate);
Expand All @@ -1124,7 +1124,7 @@ public void helpTestModifyTimeZone(String tsStr, String tzStart, String tzEnd, S
// case 2458
@Test public void testCurrentTime() throws Exception {

Time curDate = (Time)FunctionMethods.currentTime();
Time curDate = (Time)FunctionMethods.currentTime(new CommandContext());

Calendar cal1 = Calendar.getInstance();
cal1.setTime(curDate);
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -359,4 +360,17 @@ public boolean supportsCompareCriteriaEquals() {
this.execute("create local temporary table #x (e1 string)");
this.execute("insert into #x (e1) values (?)", "a");
}

@Test public void testCommandDeterministic() throws Exception {
String sql = "select now(), a.* from sys.columns a"; //$NON-NLS-1$
this.execute(sql);
Timestamp ts = null;
while (this.internalResultSet.next()) {
if (ts == null) {
ts = this.internalResultSet.getTimestamp(1);
} else {
assertEquals(ts, this.internalResultSet.getTimestamp(1));
}
}
}
}

0 comments on commit c3f6f7a

Please sign in to comment.