Skip to content

Commit

Permalink
Allow running tests with a connector that supports TIMESTAMP but not …
Browse files Browse the repository at this point in the history
…DATE

For this, `H2QueryRunner` needs to be able to convert a DATE value to
TIMESTAMP. It used to be able when `ResultSet.getTimestamp` was used to
retrieve the TIMESTAMP value, but this broke when Java Time classes were
used to retrieve values from the `ResultSet`.
  • Loading branch information
findepi committed Mar 9, 2018
1 parent 00c3bc5 commit 50224d4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static com.facebook.presto.spi.type.BigintType.BIGINT;
Expand Down Expand Up @@ -287,7 +288,20 @@ else if (TIME_WITH_TIME_ZONE.equals(type)) {
}
else if (TIMESTAMP.equals(type)) {
// resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
LocalDateTime timestampValue = resultSet.getObject(i, LocalDateTime.class);
LocalDateTime timestampValue;
try {
timestampValue = resultSet.getObject(i, LocalDateTime.class);
}
catch (SQLException first) {
// H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
try {
timestampValue = Optional.ofNullable(resultSet.getObject(i, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
}
catch (RuntimeException e) {
first.addSuppressed(e);
throw first;
}
}
if (resultSet.wasNull()) {
row.add(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.tests;

import com.facebook.presto.testing.MaterializedResult;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
import static com.facebook.presto.spi.type.TimestampType.TIMESTAMP;
import static com.google.common.base.Preconditions.checkState;
import static org.testng.Assert.assertEquals;

public class TestH2QueryRunner
{
private H2QueryRunner h2QueryRunner;

@BeforeClass
public void init()
{
h2QueryRunner = new H2QueryRunner();
}

@AfterClass(alwaysRun = true)
public void close()
{
h2QueryRunner.close();
h2QueryRunner = null;
}

@Test
public void testDateToTimestampCoercion()
{
// allow running tests with a connector that supports TIMESTAMP but not DATE

// ordinary date
MaterializedResult rows = h2QueryRunner.execute(TEST_SESSION, "SELECT DATE '2018-01-13'", ImmutableList.of(TIMESTAMP));
assertEquals(rows.getOnlyValue(), LocalDate.of(2018, 1, 13).atStartOfDay());

// date, which midnight was skipped in JVM zone
LocalDate forwardOffsetChangeAtMidnightInJvmZone = LocalDate.of(1986, 1, 1);
checkState(ZoneId.systemDefault().getRules().getValidOffsets(forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()).size() == 0, "This test assumes certain JVM time zone");
rows = h2QueryRunner.execute(TEST_SESSION, DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(forwardOffsetChangeAtMidnightInJvmZone), ImmutableList.of(TIMESTAMP));
assertEquals(rows.getOnlyValue(), forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay());
}
}

0 comments on commit 50224d4

Please sign in to comment.