-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bulk Load CDK: Root-Level Flattening & S3V2 Usage #48377
Merged
johnny-schmidt
merged 1 commit into
master
from
jschmidt/s3v2/issue-10580/root-level-flattening
Nov 7, 2024
+444
−65
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
78 changes: 78 additions & 0 deletions
78
...ore/load/src/test/kotlin/io/airbyte/cdk/load/data/AirbyteTypeToAirbyteTypeWithMetaTest.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,78 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.data | ||
|
||
import io.airbyte.cdk.load.message.DestinationRecord | ||
import java.util.LinkedHashMap | ||
import org.junit.jupiter.api.Assertions | ||
|
||
class AirbyteTypeToAirbyteTypeWithMetaTest { | ||
private val expectedMeta = | ||
linkedMapOf( | ||
DestinationRecord.Meta.COLUMN_NAME_AB_RAW_ID to FieldType(StringType, nullable = false), | ||
DestinationRecord.Meta.COLUMN_NAME_AB_EXTRACTED_AT to | ||
FieldType(IntegerType, nullable = false), | ||
DestinationRecord.Meta.COLUMN_NAME_AB_META to | ||
FieldType( | ||
ObjectType( | ||
linkedMapOf( | ||
"sync_id" to FieldType(IntegerType, nullable = false), | ||
"changes" to | ||
FieldType( | ||
ArrayType( | ||
FieldType( | ||
ObjectType( | ||
linkedMapOf( | ||
"field" to | ||
FieldType(StringType, nullable = false), | ||
"change" to | ||
FieldType(StringType, nullable = false), | ||
"reason" to | ||
FieldType(StringType, nullable = false) | ||
) | ||
), | ||
nullable = false | ||
) | ||
), | ||
nullable = false | ||
) | ||
) | ||
), | ||
nullable = false | ||
), | ||
DestinationRecord.Meta.COLUMN_NAME_AB_GENERATION_ID to | ||
FieldType(IntegerType, nullable = false) | ||
) | ||
|
||
fun testWithoutFlattening() { | ||
val schema = | ||
ObjectType( | ||
linkedMapOf( | ||
"name" to FieldType(StringType, nullable = false), | ||
"age" to FieldType(IntegerType, nullable = false), | ||
"is_cool" to FieldType(BooleanType, nullable = false) | ||
) | ||
) | ||
val withMeta = schema.withAirbyteMeta(flatten = false) | ||
val expected = LinkedHashMap(expectedMeta) | ||
expected[DestinationRecord.Meta.COLUMN_NAME_DATA] = FieldType(schema, nullable = false) | ||
Assertions.assertEquals(expected, withMeta) | ||
} | ||
|
||
fun testWithFlattening() { | ||
val schema = | ||
ObjectType( | ||
linkedMapOf( | ||
"name" to FieldType(StringType, nullable = false), | ||
"age" to FieldType(IntegerType, nullable = false), | ||
"is_cool" to FieldType(BooleanType, nullable = false) | ||
) | ||
) | ||
val withMeta = schema.withAirbyteMeta(flatten = true) | ||
val expected = LinkedHashMap(expectedMeta) | ||
schema.properties.forEach { (name, field) -> expected[name] = field } | ||
Assertions.assertEquals(expected, withMeta) | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...d/src/test/kotlin/io/airbyte/cdk/load/data/DestinationRecordToAirbyteValueWithMetaTest.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,86 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.data | ||
|
||
import io.airbyte.cdk.load.command.MockDestinationCatalogFactory | ||
import io.airbyte.cdk.load.message.DestinationRecord | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class DestinationRecordToAirbyteValueWithMetaTest { | ||
val stream = MockDestinationCatalogFactory.stream1 | ||
val emittedAtMs = 123456L | ||
val syncId = stream.syncId | ||
val generationId = stream.generationId | ||
val expectedMeta = | ||
linkedMapOf( | ||
// Don't do raw_id, we'll evict it and validate that it's a uuid | ||
DestinationRecord.Meta.COLUMN_NAME_AB_EXTRACTED_AT to IntegerValue(emittedAtMs), | ||
DestinationRecord.Meta.COLUMN_NAME_AB_META to | ||
ObjectValue( | ||
linkedMapOf( | ||
"sync_id" to IntegerValue(syncId), | ||
"changes" to ArrayValue(emptyList()) | ||
) | ||
), | ||
DestinationRecord.Meta.COLUMN_NAME_AB_GENERATION_ID to IntegerValue(generationId) | ||
) | ||
|
||
@Test | ||
fun testWithoutFlattening() { | ||
val data = | ||
ObjectValue( | ||
linkedMapOf( | ||
"name" to StringValue("John"), | ||
"age" to IntegerValue(30), | ||
"is_cool" to BooleanValue(true) | ||
) | ||
) | ||
val expected = LinkedHashMap(expectedMeta) | ||
expected[DestinationRecord.Meta.COLUMN_NAME_DATA] = data | ||
val mockRecord = | ||
DestinationRecord( | ||
stream.descriptor, | ||
data, | ||
emittedAtMs, | ||
DestinationRecord.Meta(), | ||
"dummy" | ||
) | ||
val withMeta = mockRecord.dataWithAirbyteMeta(stream, flatten = false) | ||
val uuid = | ||
withMeta.values.remove(DestinationRecord.Meta.COLUMN_NAME_AB_RAW_ID) as StringValue | ||
Assertions.assertTrue( | ||
uuid.value.matches( | ||
Regex("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}") | ||
) | ||
) | ||
Assertions.assertEquals(expected, withMeta.values) | ||
} | ||
|
||
@Test | ||
fun testWithFlattening() { | ||
val data = | ||
ObjectValue( | ||
linkedMapOf( | ||
"name" to StringValue("John"), | ||
"age" to IntegerValue(30), | ||
"is_cool" to BooleanValue(true) | ||
) | ||
) | ||
val expected = LinkedHashMap(expectedMeta) | ||
data.values.forEach { (name, value) -> expected[name] = value } | ||
val mockRecord = | ||
DestinationRecord( | ||
stream.descriptor, | ||
data, | ||
emittedAtMs, | ||
DestinationRecord.Meta(), | ||
"dummy" | ||
) | ||
val withMeta = mockRecord.dataWithAirbyteMeta(stream, flatten = true) | ||
withMeta.values.remove(DestinationRecord.Meta.COLUMN_NAME_AB_RAW_ID) | ||
Assertions.assertEquals(expected, withMeta.values) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import io.airbyte.cdk.load.message.DestinationRecord | |
import io.airbyte.protocol.models.v0.AirbyteRecordMessageMetaChange | ||
import java.time.Instant | ||
import java.util.* | ||
import kotlin.collections.LinkedHashMap | ||
|
||
class AirbyteValueWithMetaToOutputRecord { | ||
fun convert(value: ObjectValue): OutputRecord { | ||
|
@@ -60,6 +61,19 @@ class AirbyteValueWithMetaToOutputRecord { | |
} | ||
} | ||
|
||
fun AirbyteValue.maybeUnflatten(wasFlattened: Boolean): ObjectValue { | ||
this as ObjectValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove this? (unless it's for the sake of doing a smart cast, in which case add a comment? or maybe define this as |
||
if (!wasFlattened) { | ||
return this | ||
} | ||
val (meta, data) = | ||
this.values.toList().partition { DestinationRecord.Meta.COLUMN_NAMES.contains(it.first) } | ||
val properties = LinkedHashMap(meta.toMap()) | ||
val dataObject = ObjectValue(LinkedHashMap(data.toMap())) | ||
properties[DestinationRecord.Meta.COLUMN_NAME_DATA] = dataObject | ||
return ObjectValue(properties) | ||
} | ||
|
||
fun AirbyteValue.toOutputRecord(): OutputRecord { | ||
return AirbyteValueWithMetaToOutputRecord().convert(this as ObjectValue) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should throw if a property clashes with an airbyte field? (honestly not sure, I've never figured out what the expected behavior is for people chaining source -> airbyte -> destination -> airbyte -> destination)