Skip to content

Commit 77823fc

Browse files
fix(source-postgres): Handle numeric interval values for PostgreSQL 16.x CDC compatibility
Co-Authored-By: sophie.cui@airbyte.io <sophie.cui@airbyte.io>
1 parent 7444533 commit 77823fc

File tree

1 file changed

+27
-1
lines changed
  • airbyte-integrations/connectors/source-postgres/src/main/java/io/airbyte/integrations/source/postgres/cdc

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,14 @@ private void registerDate(final RelationalColumn field, final ConverterRegistrat
292292
case "TIME":
293293
return resolveTime(field, x);
294294
case "INTERVAL":
295-
return convertInterval((PGInterval) x);
295+
if (x instanceof PGInterval) {
296+
return convertInterval((PGInterval) x);
297+
} else if (x instanceof Number) {
298+
return convertIntervalFromMicros(((Number) x).longValue());
299+
} else {
300+
LOGGER.warn("Unexpected INTERVAL value type: {}", x.getClass());
301+
return x.toString();
302+
}
296303
default:
297304
throw new IllegalArgumentException("Unknown field type " + fieldType.toUpperCase(Locale.ROOT));
298305
}
@@ -323,6 +330,25 @@ private String convertInterval(final PGInterval pgInterval) {
323330
return resultInterval.toString();
324331
}
325332

333+
private String convertIntervalFromMicros(final long micros) {
334+
final boolean negative = micros < 0;
335+
final long absMicros = Math.abs(micros);
336+
337+
final long totalSeconds = absMicros / 1_000_000L;
338+
final int days = (int) (totalSeconds / 86_400L);
339+
final int hours = (int) ((totalSeconds % 86_400L) / 3_600L);
340+
final int minutes = (int) ((totalSeconds % 3_600L) / 60L);
341+
final int seconds = (int) (totalSeconds % 60L);
342+
343+
final PGInterval interval = new PGInterval(0, 0,
344+
negative ? -days : days,
345+
negative ? -hours : hours,
346+
negative ? -minutes : minutes,
347+
negative ? -seconds : seconds);
348+
349+
return convertInterval(interval);
350+
}
351+
326352
private void registerMoney(final RelationalColumn field, final ConverterRegistration<SchemaBuilder> registration) {
327353
registration.register(SchemaBuilder.string().optional(), x -> {
328354
if (x == null) {

0 commit comments

Comments
 (0)