Skip to content

Commit e893004

Browse files
author
Sagar Agarwal
committed
normalize years and months part in coverting Avro interval to ISO8601 string
1 parent 254a83e commit e893004

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/avro/GenericRecordTypeConvertor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,9 @@ static String handleRecordFieldType(String fieldName, GenericRecord element, Sch
708708
Period.ZERO
709709
.plusYears(((Number) getOrDefault(element, "years", 0L)).longValue())
710710
.plusMonths(((Number) getOrDefault(element, "months", 0L)).longValue())
711-
.plusDays(((Number) getOrDefault(element, "days", 0L)).longValue());
711+
.plusDays(((Number) getOrDefault(element, "days", 0L)).longValue())
712+
.normalized(); // Normalize years and months
713+
712714
/*
713715
* Convert the period to a ISO-8601 period formatted String, such as P6Y3M1D.
714716
* A zero period will be represented as zero days, 'P0D'.

v2/spanner-common/src/test/java/com/google/cloud/teleport/v2/spanner/migrations/avro/GenericRecordTypeConvertorTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,9 @@ public void testIntervalNanos() {
712712
result =
713713
GenericRecordTypeConvertor.handleRecordFieldType(
714714
"interval_nanos_column",
715-
AvroTestingHelper.createIntervalNanosRecord(1000L, 1000L, 3890L, 25L, 331L, 12L, 9L),
715+
AvroTestingHelper.createIntervalNanosRecord(1000L, 1000L, 3890L, 25L, 331L, 3672L, 9L),
716716
AvroTestingHelper.INTERVAL_NANOS_SCHEMA);
717-
assertEquals(
718-
"Test #1 interval nano conversion:", "P1000Y1000M3890DT30H31M12.000000009S", result);
717+
assertEquals("Test #1 interval nano conversion:", "P1083Y4M3890DT31H32M12.000000009S", result);
719718

720719
/* Test with any field set as null gets treated as 0. */
721720
result =
@@ -736,7 +735,7 @@ public void testIntervalNanos() {
736735
AvroTestingHelper.INTERVAL_NANOS_SCHEMA);
737736
assertEquals(
738737
"Test #3 interval nano conversion with negative months:",
739-
"P1000Y-1000M3890DT25H31M12.000000009S",
738+
"P916Y8M3890DT25H31M12.000000009S",
740739
result);
741740

742741
/* Test that negative nanos subtract from the fractional seconds, for example 12 Seconds -1 Nanos becomes 11.999999991s. */

0 commit comments

Comments
 (0)