Skip to content

Commit

Permalink
Fix incorrect result when reading dates during calendar switch in SQL…
Browse files Browse the repository at this point in the history
… Server

Co-Authored-By: Jonas Haag <jonas@lophus.org>
  • Loading branch information
ebyhr and jonashaag committed Dec 4, 2023
1 parent 717c223 commit 19f9677
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import static io.trino.plugin.jdbc.StandardColumnMappings.booleanWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.charReadFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.charWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.dateReadFunctionUsingLocalDate;
import static io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.doubleWriteFunction;
Expand Down Expand Up @@ -549,7 +550,7 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect
case Types.DATE:
return Optional.of(ColumnMapping.longMapping(
DATE,
sqlServerDateReadFunction(),
dateReadFunctionUsingLocalDate(),
sqlServerDateWriteFunction()));

case Types.TIME:
Expand Down Expand Up @@ -886,11 +887,6 @@ private static LongWriteFunction sqlServerDateWriteFunction()
return (statement, index, day) -> statement.setString(index, DATE_FORMATTER.format(LocalDate.ofEpochDay(day)));
}

private static LongReadFunction sqlServerDateReadFunction()
{
return (resultSet, index) -> LocalDate.parse(resultSet.getString(index), DATE_FORMATTER).toEpochDay();
}

private static ColumnMapping timestampWithTimeZoneColumnMapping(int precision)
{
checkArgument(precision <= MAX_SUPPORTED_TEMPORAL_PRECISION, "Unsupported datetimeoffset precision %s", precision);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ protected TestTable createTableWithUnsupportedColumn()
protected Optional<DataMappingTestSetup> filterDataMappingSmokeTestData(DataMappingTestSetup dataMappingTestSetup)
{
String typeName = dataMappingTestSetup.getTrinoTypeName();
if (typeName.equals("date")) {
// SQL Server plus 10 days when the date is the range of 1582 Oct 5 and 14
if (dataMappingTestSetup.getSampleValueLiteral().equals("DATE '1582-10-05'") || dataMappingTestSetup.getSampleValueLiteral().equals("DATE '1582-10-14'")) {
return Optional.empty();
}
}
if (typeName.equals("timestamp(3) with time zone") ||
typeName.equals("timestamp(6) with time zone")) {
return Optional.of(dataMappingTestSetup.asUnsupported());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.plugin.sqlserver;

import com.google.common.collect.ImmutableList;
import io.trino.Session;
import io.trino.spi.type.TimeZoneKey;
import io.trino.testing.AbstractTestQueryFramework;
Expand Down Expand Up @@ -481,6 +480,9 @@ private SqlDataTypeTest dateTest(Function<String, String> inputLiteralFactory)
.addRoundTrip("date", inputLiteralFactory.apply("'0012-12-12'"), DATE, "DATE '0012-12-12'")
// before julian->gregorian switch
.addRoundTrip("date", inputLiteralFactory.apply("'1500-01-01'"), DATE, "DATE '1500-01-01'")
// during julian->gregorian switch
.addRoundTrip("date", inputLiteralFactory.apply("'1582-10-05'"), DATE, "DATE '1582-10-05'")
.addRoundTrip("date", inputLiteralFactory.apply("'1582-10-14'"), DATE, "DATE '1582-10-14'")
// before epoch
.addRoundTrip("date", inputLiteralFactory.apply("'1952-04-03'"), DATE, "DATE '1952-04-03'")
.addRoundTrip("date", inputLiteralFactory.apply("'1970-01-01'"), DATE, "DATE '1970-01-01'")
Expand All @@ -493,17 +495,6 @@ private SqlDataTypeTest dateTest(Function<String, String> inputLiteralFactory)
.addRoundTrip("date", inputLiteralFactory.apply("'1983-10-01'"), DATE, "DATE '1983-10-01'");
}

@Test
public void testDateJulianGregorianCalendarSwitch()
{
try (TestTable table = new TestTable(getQueryRunner()::execute, "test_old_date", "(dt DATE)", ImmutableList.of("DATE '1582-10-05'", "DATE '1582-10-14'"))) {
// SQL Server returns +10 days when the date is in the range of 1582-10-05 and 1582-10-14, but we need to pass the original value in predicates
assertQuery("SELECT * FROM " + table.getName(), "VALUES DATE '1582-10-15', DATE '1582-10-24'");
assertQuery("SELECT * FROM " + table.getName() + " WHERE dt = DATE '1582-10-05'", "VALUES DATE '1582-10-15'");
assertQueryReturnsEmptyResult("SELECT * FROM " + table.getName() + " WHERE dt = DATE '1582-10-15'");
}
}

@Test
public void testSqlServerDateUnsupported()
{
Expand Down

0 comments on commit 19f9677

Please sign in to comment.