-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: iceberg timestamp value handling (#49807)
- Loading branch information
1 parent
3af71ab
commit 369cde7
Showing
7 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...src/main/kotlin/io/airbyte/cdk/load/data/iceberg/parquet/IcebergParquetPipelineFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.data.iceberg.parquet | ||
|
||
import io.airbyte.cdk.load.command.DestinationStream | ||
import io.airbyte.cdk.load.data.AirbyteSchemaNoopMapper | ||
import io.airbyte.cdk.load.data.AirbyteValueNoopMapper | ||
import io.airbyte.cdk.load.data.MapperPipeline | ||
import io.airbyte.cdk.load.data.MapperPipelineFactory | ||
import io.airbyte.cdk.load.data.MergeUnions | ||
import io.airbyte.cdk.load.data.NullOutOfRangeIntegers | ||
import io.airbyte.cdk.load.data.SchemalessValuesToJsonString | ||
import io.airbyte.cdk.load.data.UnionTypeToDisjointRecord | ||
import io.airbyte.cdk.load.data.UnionValueToDisjointRecord | ||
|
||
class IcebergParquetPipelineFactory : MapperPipelineFactory { | ||
override fun create(stream: DestinationStream): MapperPipeline = | ||
MapperPipeline( | ||
stream.schema, | ||
listOf( | ||
AirbyteSchemaNoopMapper() to SchemalessValuesToJsonString(), | ||
AirbyteSchemaNoopMapper() to NullOutOfRangeIntegers(), | ||
MergeUnions() to AirbyteValueNoopMapper(), | ||
UnionTypeToDisjointRecord() to UnionValueToDisjointRecord(), | ||
), | ||
) | ||
} |
55 changes: 55 additions & 0 deletions
55
...erg-parquet/src/main/kotlin/io/airbyte/cdk/load/data/iceberg/parquet/TimeStringUtility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.data.iceberg.parquet | ||
|
||
import io.airbyte.cdk.load.data.TimeStringToInteger | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
import java.time.OffsetDateTime | ||
import java.time.OffsetTime | ||
import java.time.ZoneOffset | ||
import java.time.ZonedDateTime | ||
|
||
object TimeStringUtility { | ||
|
||
fun toLocalDate(dateString: String): LocalDate { | ||
return LocalDate.parse(dateString, TimeStringToInteger.DATE_TIME_FORMATTER) | ||
} | ||
|
||
fun toOffset(timeString: String): LocalTime { | ||
return try { | ||
toMicrosOfDayWithTimezone(timeString) | ||
} catch (e: Exception) { | ||
toMicrosOfDayWithoutTimezone(timeString) | ||
} | ||
} | ||
|
||
private fun toMicrosOfDayWithTimezone(timeString: String): LocalTime { | ||
return OffsetTime.parse(timeString, TimeStringToInteger.TIME_FORMATTER).toLocalTime() | ||
} | ||
|
||
private fun toMicrosOfDayWithoutTimezone(timeString: String): LocalTime { | ||
return LocalTime.parse(timeString, TimeStringToInteger.TIME_FORMATTER) | ||
} | ||
|
||
fun toOffsetDateTime(timestampString: String): OffsetDateTime { | ||
return try { | ||
toOffsetDateTimeWithTimezone(timestampString) | ||
} catch (e: Exception) { | ||
toOffsetDateTimeWithoutTimezone(timestampString) | ||
} | ||
} | ||
|
||
private fun toOffsetDateTimeWithTimezone(timestampString: String): OffsetDateTime { | ||
return ZonedDateTime.parse(timestampString, TimeStringToInteger.DATE_TIME_FORMATTER) | ||
.toOffsetDateTime() | ||
} | ||
|
||
private fun toOffsetDateTimeWithoutTimezone(timestampString: String): OffsetDateTime { | ||
return LocalDateTime.parse(timestampString, TimeStringToInteger.DATE_TIME_FORMATTER) | ||
.atOffset(ZoneOffset.UTC) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...rc/testFixtures/kotlin/io/airbyte/cdk/load/data/icerberg/parquet/TimeStringUtilityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.data.icerberg.parquet | ||
|
||
import io.airbyte.cdk.load.data.iceberg.parquet.TimeStringUtility | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
import java.time.OffsetDateTime | ||
import java.time.ZoneOffset | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Test | ||
|
||
class TimeStringUtilityTest { | ||
|
||
@Test | ||
fun `toLocalDate should parse a valid date string`() { | ||
val dateStr = "2024-12-16T00:00:00" | ||
val date = TimeStringUtility.toLocalDate(dateStr) | ||
assertEquals(LocalDate.of(2024, 12, 16), date) | ||
} | ||
|
||
@Test | ||
fun `toLocalDate should throw exception for invalid date string`() { | ||
val invalidDateStr = "invalid-date" | ||
assertThrows(java.time.format.DateTimeParseException::class.java) { | ||
TimeStringUtility.toLocalDate(invalidDateStr) | ||
} | ||
} | ||
|
||
@Test | ||
fun `toOffset should parse time with timezone`() { | ||
val timeStrWithOffset = "12:34:56+02:00" | ||
val localTime = TimeStringUtility.toOffset(timeStrWithOffset) | ||
assertEquals(LocalTime.of(12, 34, 56), localTime) | ||
} | ||
|
||
@Test | ||
fun `toOffset should parse time without timezone`() { | ||
val timeStrWithoutOffset = "12:34:56" | ||
val localTime = TimeStringUtility.toOffset(timeStrWithoutOffset) | ||
assertEquals(LocalTime.of(12, 34, 56), localTime) | ||
} | ||
|
||
@Test | ||
fun `toOffsetDateTime should parse datetime with timezone`() { | ||
val dateTimeWithTz = "2024-12-16T12:34:56-05:00" | ||
val odt = TimeStringUtility.toOffsetDateTime(dateTimeWithTz) | ||
assertEquals(OffsetDateTime.of(2024, 12, 16, 12, 34, 56, 0, ZoneOffset.of("-05:00")), odt) | ||
} | ||
|
||
@Test | ||
fun `toOffsetDateTime should parse datetime without timezone as UTC`() { | ||
val dateTimeWithoutTz = "2024-12-16T12:34:56" | ||
val odt = TimeStringUtility.toOffsetDateTime(dateTimeWithoutTz) | ||
assertEquals( | ||
OffsetDateTime.of(LocalDateTime.of(2024, 12, 16, 12, 34, 56), ZoneOffset.UTC), | ||
odt | ||
) | ||
} | ||
|
||
@Test | ||
fun `toOffsetDateTime should throw exception for invalid format`() { | ||
val invalidDateTime = "invalid-datetime" | ||
assertThrows(java.time.format.DateTimeParseException::class.java) { | ||
TimeStringUtility.toOffsetDateTime(invalidDateTime) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters