Skip to content

Commit 2cd6200

Browse files
Fallback to parsing datetime and time strings w/ and w/o timezones in case DateTimeParseException is thrown (#13745)
* Fall back to parsing w/ or w/o TZ if parsing a date or a time string fails * auto-bump connector version Co-authored-by: Octavia Squidington III <[email protected]>
1 parent fe6eda5 commit 2cd6200

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

airbyte-config/init/src/main/resources/seed/source_definitions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@
715715
- name: Postgres
716716
sourceDefinitionId: decd338e-5647-4c0b-adf4-da0e75f5a750
717717
dockerRepository: airbyte/source-postgres
718-
dockerImageTag: 0.4.22
718+
dockerImageTag: 0.4.23
719719
documentationUrl: https://docs.airbyte.io/integrations/sources/postgres
720720
icon: postgresql.svg
721721
sourceType: database

airbyte-config/init/src/main/resources/seed/source_specs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6719,7 +6719,7 @@
67196719
supportsNormalization: false
67206720
supportsDBT: false
67216721
supported_destination_sync_modes: []
6722-
- dockerImage: "airbyte/source-postgres:0.4.22"
6722+
- dockerImage: "airbyte/source-postgres:0.4.23"
67236723
spec:
67246724
documentationUrl: "https://docs.airbyte.com/integrations/sources/postgres"
67256725
connectionSpecification:

airbyte-integrations/connectors/source-postgres/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ ENV APPLICATION source-postgres
1616

1717
COPY --from=build /airbyte /airbyte
1818

19-
LABEL io.airbyte.version=0.4.22
19+
LABEL io.airbyte.version=0.4.23
2020
LABEL io.airbyte.name=airbyte/source-postgres

airbyte-integrations/connectors/source-postgres/src/main/java/io/airbyte/integrations/source/postgres/PostgresSourceOperations.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.time.LocalTime;
3232
import java.time.OffsetDateTime;
3333
import java.time.OffsetTime;
34+
import java.time.format.DateTimeParseException;
3435
import java.util.Collections;
3536
import org.postgresql.jdbc.PgResultSetMetaData;
3637
import org.slf4j.Logger;
@@ -108,22 +109,42 @@ public void setStatementField(final PreparedStatement preparedStatement,
108109
}
109110
}
110111

111-
private void setTimeWithTimezone(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException {
112-
preparedStatement.setObject(parameterIndex, OffsetTime.parse(value));
112+
private void setTimeWithTimezone(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException {
113+
try {
114+
preparedStatement.setObject(parameterIndex, OffsetTime.parse(value));
115+
} catch (final DateTimeParseException e) {
116+
//attempt to parse the time w/o timezone. This can be caused by schema created with a different version of the connector
117+
preparedStatement.setObject(parameterIndex, LocalTime.parse(value));
118+
}
113119
}
114120

115-
private void setTimestampWithTimezone(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException {
116-
preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value));
121+
private void setTimestampWithTimezone(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException {
122+
try {
123+
preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value));
124+
} catch (final DateTimeParseException e) {
125+
//attempt to parse the datetime w/o timezone. This can be caused by schema created with a different version of the connector
126+
preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value));
127+
}
117128
}
118129

119130
@Override
120-
protected void setTimestamp(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException {
121-
preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value));
131+
protected void setTimestamp(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException {
132+
try {
133+
preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value));
134+
} catch (final DateTimeParseException e) {
135+
//attempt to parse the datetime with timezone. This can be caused by schema created with an older version of the connector
136+
preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value));
137+
}
122138
}
123139

124140
@Override
125-
protected void setTime(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException {
126-
preparedStatement.setObject(parameterIndex, LocalTime.parse(value));
141+
protected void setTime(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException {
142+
try {
143+
preparedStatement.setObject(parameterIndex, LocalTime.parse(value));
144+
} catch (final DateTimeParseException e) {
145+
//attempt to parse the datetime with timezone. This can be caused by schema created with an older version of the connector
146+
preparedStatement.setObject(parameterIndex, OffsetTime.parse(value));
147+
}
127148
}
128149

129150
@Override
@@ -170,21 +191,21 @@ public void setJsonField(final ResultSet resultSet, final int colIndex, final Ob
170191
}
171192

172193
@Override
173-
protected void putDate(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException {
174-
LocalDate date = getDateTimeObject(resultSet, index, LocalDate.class);
194+
protected void putDate(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException {
195+
final LocalDate date = getDateTimeObject(resultSet, index, LocalDate.class);
175196
node.put(columnName, resolveEra(date, date.toString()));
176197
}
177198

178199
@Override
179-
protected void putTime(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException {
180-
LocalTime time = getDateTimeObject(resultSet, index, LocalTime.class);
200+
protected void putTime(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException {
201+
final LocalTime time = getDateTimeObject(resultSet, index, LocalTime.class);
181202
node.put(columnName, time.toString());
182203
}
183204

184205
@Override
185-
protected void putTimestamp(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException {
186-
LocalDateTime timestamp = getDateTimeObject(resultSet, index, LocalDateTime.class);
187-
LocalDate date = timestamp.toLocalDate();
206+
protected void putTimestamp(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException {
207+
final LocalDateTime timestamp = getDateTimeObject(resultSet, index, LocalDateTime.class);
208+
final LocalDate date = timestamp.toLocalDate();
188209
node.put(columnName, resolveEra(date, timestamp.toString()));
189210
}
190211

@@ -214,7 +235,7 @@ public JDBCType getFieldType(final JsonNode field) {
214235
}
215236

216237
@Override
217-
public JsonSchemaType getJsonType(JDBCType jdbcType) {
238+
public JsonSchemaType getJsonType(final JDBCType jdbcType) {
218239
return switch (jdbcType) {
219240
case BOOLEAN -> JsonSchemaType.BOOLEAN;
220241
case TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, NUMERIC, DECIMAL -> JsonSchemaType.NUMBER;
@@ -264,7 +285,7 @@ private void putHstoreAsJson(final ObjectNode node, final String columnName, fin
264285
final var data = resultSet.getObject(index);
265286
try {
266287
node.put(columnName, OBJECT_MAPPER.writeValueAsString(data));
267-
} catch (JsonProcessingException e) {
288+
} catch (final JsonProcessingException e) {
268289
throw new RuntimeException("Could not parse 'hstore' value:" + e);
269290
}
270291
}

docs/integrations/sources/postgres.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ According to Postgres [documentation](https://www.postgresql.org/docs/14/datatyp
275275

276276
| Version | Date | Pull Request | Subject |
277277
|:--------|:-----------|:-------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------|
278+
| 0.4.23 | 2022-06-13 | [13655](https://github.com/airbytehq/airbyte/pull/13745) | Fixed handling datetime cursors when upgrading from older versions of the connector |
278279
| 0.4.22 | 2022-06-09 | [13655](https://github.com/airbytehq/airbyte/pull/13655) | Fixed bug with unsupported date-time datatypes during incremental sync |
279280
| 0.4.21 | 2022-06-06 | [13435](https://github.com/airbytehq/airbyte/pull/13435) | Adjust JDBC fetch size based on max memory and max row size |
280281
| 0.4.20 | 2022-06-02 | [13367](https://github.com/airbytehq/airbyte/pull/13367) | Added convertion hstore to json format |

0 commit comments

Comments
 (0)