Last active
October 5, 2016 19:56
-
-
Save sirianni/d56ad3909241cd576a566308f238e161 to your computer and use it in GitHub Desktop.
Vertica Timestamp JDBC Bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package adhoc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
import java.sql.Timestamp; | |
import java.time.Instant; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import com.vertica.dsi.dataengine.utilities.TimestampTz; | |
/** | |
* Seems related to https://community.dev.hpe.com/t5/Vertica-Forum/JDBC-driver-adjusts-TIMESTAMPS-without-zone-to-the-local-time/m-p/215785/highlight/true#M5549 | |
* In this case, even a TIMESTAMPTZ is being incorrectly converted to the local time. | |
* The wrong epoch value is being used in the super() call to the java.sql.Timestamp constructor | |
*/ | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
Instant now = Instant.now(); | |
System.out.println("CURRENT TIME (UTC): " + now); | |
System.out.println("CURRENT TIME (Local): " + now.atZone(ZoneId.systemDefault())); | |
//Connection connection = DriverManager.getConnection("jdbc:postgresql://ln-esirianni-sjc/portaldb", "nimble", null); | |
try (Connection connection = DriverManager.getConnection("jdbc:vertica://vertica-vip.test2.nimblestorage.com:5433/dsci01", "adhoc_user1", "isau1")) { | |
System.out.println("=== VERTICA ==="); | |
runTest(connection, false); | |
System.out.println("======"); | |
System.out.println("=== VERTICA (HACKED) ==="); | |
runTest(connection, true); | |
System.out.println("======"); | |
} | |
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://portaldb-1.test1.nimblestorage.com/portaldb", "nimble", "")) { | |
System.out.println("=== POSTGRES ==="); | |
runTest(connection, false); | |
System.out.println("======"); | |
} | |
} | |
public static void runTest(Connection connection, boolean hack) throws Exception { | |
Statement statement = connection.createStatement(); | |
//statement.execute("select * from test"); | |
statement.execute("select current_timestamp"); | |
ResultSet resultSet = statement.getResultSet(); | |
resultSet.next(); | |
Timestamp timestamp = resultSet.getTimestamp(1); | |
Instant instant = timestamp.toInstant(); | |
if (hack) { | |
instant = hackVerticaTimestamp((TimestampTz) timestamp); | |
} | |
System.out.println("java.sql.Timestamp: " + timestamp); | |
System.out.println("java.time.Instant: " + instant); | |
System.out.println("java.time.ZonedDateTime (Pacific): " + instant.atZone(ZoneId.of("America/Los_Angeles"))); | |
System.out.println("java.time.ZonedDateTime (Local): " + instant.atZone(ZoneId.systemDefault())); | |
} | |
public static Instant hackVerticaTimestamp(TimestampTz timestampTz) { | |
ZonedDateTime zonedDateTime = ZonedDateTime.of(timestampTz.toLocalDateTime(), timestampTz.getTimezoneCalendar().getTimeZone().toZoneId()); | |
return zonedDateTime.toInstant(); | |
} | |
// CURRENT TIME (UTC): 2016-10-05T19:49:09.658Z | |
// CURRENT TIME (Local): 2016-10-05T15:49:09.658-04:00[America/New_York] | |
// === VERTICA === | |
// java.sql.Timestamp: 2016-10-05 12:49:10.561617 | |
// java.time.Instant: 2016-10-05T16:49:10.561617Z | |
// java.time.ZonedDateTime (Pacific): 2016-10-05T09:49:10.561617-07:00[America/Los_Angeles] | |
// java.time.ZonedDateTime (Local): 2016-10-05T12:49:10.561617-04:00[America/New_York] | |
// ====== | |
// === VERTICA (HACKED) === | |
// java.sql.Timestamp: 2016-10-05 12:49:10.817294 | |
// java.time.Instant: 2016-10-05T19:49:10.817294Z | |
// java.time.ZonedDateTime (Pacific): 2016-10-05T12:49:10.817294-07:00[America/Los_Angeles] | |
// java.time.ZonedDateTime (Local): 2016-10-05T15:49:10.817294-04:00[America/New_York] | |
// ====== | |
// === POSTGRES === | |
// java.sql.Timestamp: 2016-10-05 15:49:11.423541 | |
// java.time.Instant: 2016-10-05T19:49:11.423541Z | |
// java.time.ZonedDateTime (Pacific): 2016-10-05T12:49:11.423541-07:00[America/Los_Angeles] | |
// java.time.ZonedDateTime (Local): 2016-10-05T15:49:11.423541-04:00[America/New_York] | |
// ====== | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment