Skip to content

Commit

Permalink
Reverting datetime behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Admin_mschuemi authored and Admin_mschuemi committed Jun 17, 2024
1 parent 7405b49 commit daf452e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 36 deletions.
3 changes: 0 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
DatabaseConnector 6.3.3
=======================

Bugfixes:

1. Fixed discrepancies when server and client are not in same time zone.

DatabaseConnector 6.3.2
=======================
Expand Down
16 changes: 8 additions & 8 deletions R/Sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ parseJdbcColumnData <- function(batchedQuery,
column <- format(column, "%Y-%m-%d")
}
} else if (columnTypes[i] == 4) {
column <- rJava::.jcall(batchedQuery, "[Ljava/lang/String;", "getString", as.integer(i))
if (length(column) == 0) {
timeZone <- ""
} else {
timeZone <- substr(column, 21, 999)[1]
}
column <- as.POSIXct(substr(column, 1, 19), format = "%Y-%m-%d %H:%M:%OS", tz = timeZone)
column <- rJava::.jcall(batchedQuery, "[D", "getNumeric", as.integer(i))
column <- as.POSIXct(column, origin = "1970-01-01")
} else {
column <- rJava::.jcall(batchedQuery, "[Ljava/lang/String;", "getString", as.integer(i))
column <- rJava::.jcall(batchedQuery, "[Ljava/lang/String;", "getString", i)
if (!datesAsString) {
if (columnTypes[i] == 4) {
column <- as.POSIXct(column)
}
}
}
columns[[i]] <- column
}
Expand Down
2 changes: 1 addition & 1 deletion inst/csv/jarChecksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ec60fa18e389072c979178a05dbccd921c739adf5e2a94bef5a843017f7d8631
41e7d71b717d9b51cbeca5dd10f85cd9a882163f595ce52cb56048f08ff0aef1
Binary file modified inst/java/DatabaseConnector.jar
Binary file not shown.
10 changes: 5 additions & 5 deletions java/org/ohdsi/databaseConnector/BatchedInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BatchedInsert {

public static final int BIG_DATA_BATCH_INSERT_LIMIT = 1000;

public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

private Object[] columns;
private int[] columnTypes;
Expand All @@ -42,7 +42,6 @@ public BatchedInsert(Connection connection, String dbms, String sql, int columnC
this.columnCount = columnCount;
columns = new Object[columnCount];
columnTypes = new int[columnCount];
// TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

private void trySettingAutoCommit(boolean value) throws SQLException {
Expand Down Expand Up @@ -104,7 +103,7 @@ private void setValue(PreparedStatement statement, int statementIndex, int rowIn
if (dbms.equals(SNOWFLAKE))
setTimestampForSnowflake(statement, statementIndex, value);
else
statement.setTimestamp(statementIndex, java.sql.Timestamp.valueOf(value));
statement.setTimestamp(statementIndex, java.sql.Timestamp.valueOf(value));
}
} else if (columnTypes[columnIndex] == BIGINT) {
long value = ((long[]) columns[columnIndex])[rowIndex];
Expand Down Expand Up @@ -268,8 +267,9 @@ public void setBigint(int columnIndex, double column) {
}

private static void setTimestampForSnowflake(PreparedStatement statement, int statementIndex, String value) throws ParseException, SQLException {
dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = dateTimeFormat.parse(value);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(value);
statement.setTimestamp(statementIndex, new Timestamp(date.getTime()));
}
}
12 changes: 4 additions & 8 deletions java/org/ohdsi/databaseConnector/BatchedQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
// import java.util.TimeZone;
import java.sql.Date;

public class BatchedQuery {
Expand All @@ -27,7 +25,6 @@ public class BatchedQuery {
public static double NA_DOUBLE = Double.longBitsToDouble(0x7ff00000000007a2L);
public static int NA_INTEGER = Integer.MIN_VALUE;
public static long NA_LONG = Long.MIN_VALUE;
public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");

private Object[] columns;
private int[] columnTypes;
Expand Down Expand Up @@ -84,7 +81,7 @@ else if (columnTypes[columnIndex] == INTEGER64)
else if (columnTypes[columnIndex] == DATE)
bytesPerRow += 4;
else if (columnTypes[columnIndex] == DATETIME)
bytesPerRow += 20;
bytesPerRow += 8;
else // String
bytesPerRow += 512;
batchSize = (int) Math.min(MAX_BATCH_SIZE, Math.round((availableMemoryAtStart / 10d) / (double) bytesPerRow));
Expand All @@ -102,7 +99,7 @@ else if (columnTypes[columnIndex] == STRING)
else if (columnTypes[columnIndex] == DATE)
columns[columnIndex] = new int[batchSize];
else if (columnTypes[columnIndex] == DATETIME)
columns[columnIndex] = new String[batchSize];
columns[columnIndex] = new double[batchSize];
else
columns[columnIndex] = new String[batchSize];
byteBuffer = ByteBuffer.allocate(8 * batchSize);
Expand Down Expand Up @@ -131,7 +128,6 @@ private void trySettingAutoCommit(boolean value) throws SQLException {
public BatchedQuery(Connection connection, String query, String dbms) throws SQLException {
this.connection = connection;
this.dbms = dbms;
// TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
trySettingAutoCommit(false);
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(FETCH_SIZE);
Expand Down Expand Up @@ -198,9 +194,9 @@ else if (columnTypes[columnIndex] == DATE) {
} else {
Timestamp timestamp = resultSet.getTimestamp(columnIndex + 1);
if (timestamp == null)
((String[]) columns[columnIndex])[rowCount] = "";
((double[]) columns[columnIndex])[rowCount] = NA_DOUBLE;
else
((String[]) columns[columnIndex])[rowCount] = dateTimeFormat.format(timestamp);
((double[]) columns[columnIndex])[rowCount] = timestamp.getTime() / 1000;

}
rowCount++;
Expand Down
12 changes: 1 addition & 11 deletions tests/testthat/test-insertTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ for (testServer in testServers) {
)

# Check data on server is same as local
dataCopy2 <- renderTranslateQuerySql(connection, "SELECT some_datetime FROM #temp;", integer64AsNumeric = FALSE)
dataCopy2 <- renderTranslateQuerySql(connection, "SELECT * FROM #temp;", integer64AsNumeric = FALSE)
names(dataCopy2) <- tolower(names(dataCopy2))
dataCopy1 <- data[order(dataCopy1$person_id), ]
dataCopy2 <- dataCopy2[order(dataCopy2$person_id), ]
Expand All @@ -72,16 +72,6 @@ for (testServer in testServers) {
attr(dataCopy1$some_datetime, "tzone") <- NULL
attr(dataCopy2$some_datetime, "tzone") <- NULL
expect_equal(dataCopy1, dataCopy2, check.attributes = FALSE, tolerance = 1e-7)
#
# dataCopy1$some_datetime[1]
# dataCopy2$some_datetime[1]
#
# format(dataCopy1$some_datetime[1], format="%Y-%m-%d %H:%M:%S")
# querySql(connection, "SELECT current_timezone();")
# SqlRender::translate("SELECT TOP 1 * FROM #temp;", "spark")
# querySql(connection, "SELECT date_format(some_datetime, 'yyyy-MM-dd hh:mm:ss VV') FROM scratch.ir8uu9zutemp WHERE person_id = 2005;")
# lowLevelQuerySql(connection, "SELECT some_datetime FROM scratch.ir8uu9zutemp WHERE person_id = 2005;")
# executeSql(connection, "SET timezone = UTC;")

# Check data types
res <- dbSendQuery(connection, "SELECT * FROM #temp;")
Expand Down

0 comments on commit daf452e

Please sign in to comment.