Skip to content

Commit

Permalink
ADBDEV-3099: Fix TIMESTAMP_WITH_TIME_ZONE for readable tables
Browse files Browse the repository at this point in the history
We don't need to convert OffsetDateTime to LocalDateTime on the PXF site as it has its own time zone that might be not the same as in GP.
  • Loading branch information
RomaZe committed Apr 4, 2023
1 parent d6a0c17 commit 499c574
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
Expand All @@ -50,6 +49,7 @@
import java.util.Set;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_TIME;
import static java.time.format.DateTimeFormatter.ISO_OFFSET_TIME;

/**
* JDBC tables resolver
Expand All @@ -73,6 +73,14 @@ public class JdbcResolver extends JdbcBasePlugin implements Resolver {
.appendPattern(" G")
.toFormatter();

private static final DateTimeFormatter OFFSET_DATE_TIME_GET_FORMATTER = (new DateTimeFormatterBuilder())
.appendValue(ChronoField.YEAR_OF_ERA, 4, 9, SignStyle.NORMAL).appendLiteral("-")
.appendValue(ChronoField.MONTH_OF_YEAR, 2).appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2).appendLiteral(" ")
.append(ISO_OFFSET_TIME)
.appendPattern(" G")
.toFormatter();

private static final DateTimeFormatter LOCAL_DATE_SET_FORMATTER = (new DateTimeFormatterBuilder())
.appendValue(ChronoField.YEAR_OF_ERA, 1, 9, SignStyle.NORMAL).appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2).appendLiteral('-')
Expand Down Expand Up @@ -177,14 +185,12 @@ public List<OneField> getFields(OneRow row) throws SQLException {
case TIMESTAMP_WITH_TIME_ZONE:
if (isDateWideRange) {
OffsetDateTime offsetDateTime = result.getObject(colName, OffsetDateTime.class);
if (offsetDateTime != null) {
LocalDateTime localDateTime = offsetDateTime.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
value = localDateTime.format(LOCAL_DATE_TIME_GET_FORMATTER);
} else {
value = null;
}
value = offsetDateTime != null ? offsetDateTime.format(OFFSET_DATE_TIME_GET_FORMATTER) : null;
} else {
value = result.getTimestamp(colName);
throw new UnsupportedOperationException(
String.format("Field type '%s' (column '%s') is not supported",
DataType.get(oneField.type),
column));
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void getFieldOffsetDateTimeWithWideRangeTest() throws SQLException {
resolver.columns = context.getTupleDescription();
resolver.isDateWideRange = isDateWideRange;
List<OneField> oneFields = resolver.getFields(row);
assertEquals("1977-12-11 05:15:30.1234 AD", oneFields.get(0).val);
assertEquals("1977-12-11 10:15:30.1234+05:00 AD", oneFields.get(0).val);
}
}

Expand Down Expand Up @@ -300,7 +300,7 @@ void getFieldOffsetDateTimeWithWideRangeWithLeadingZeroTest() throws SQLExceptio
resolver.columns = context.getTupleDescription();
resolver.isDateWideRange = isDateWideRange;
List<OneField> oneFields = resolver.getFields(row);
assertEquals("0003-01-02 01:05:06.0000015 AD", oneFields.get(0).val);
assertEquals("0003-01-02 04:05:06.0000015+03:00 AD", oneFields.get(0).val);
}
}

Expand All @@ -320,7 +320,7 @@ void getFieldOffsetDateTimeWithMoreThan4digitsInYearTest() throws SQLException {
resolver.columns = context.getTupleDescription();
resolver.isDateWideRange = isDateWideRange;
List<OneField> oneFields = resolver.getFields(row);
assertEquals("9876543-12-11 14:15:30.1234 AD", oneFields.get(0).val);
assertEquals("9876543-12-11 11:15:30.1234-03:00 AD", oneFields.get(0).val);
}
}

Expand All @@ -341,7 +341,7 @@ void getFieldOffsetDateTimeWithEraTest() throws SQLException {
resolver.isDateWideRange = isDateWideRange;
List<OneField> oneFields = resolver.getFields(row);
// The year -3456 is transferred to 3457 BC: https://en.wikipedia.org/wiki/Astronomical_year_numbering
assertEquals("3457-12-11 09:15:30 BC", oneFields.get(0).val);
assertEquals("3457-12-11 11:15:30+02:00 BC", oneFields.get(0).val);
}
}

Expand Down

0 comments on commit 499c574

Please sign in to comment.