-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
feat: implement schema comparison logic #50400
Open
subodh1810
wants to merge
7
commits into
master
Choose a base branch
from
iceberg-schema-comparator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2e9356
feat: implement schema comparison logic
subodh1810 fe20d62
chore: auto-fix lint and format issues
octavia-squidington-iii 6432c80
change logic around struct types
subodh1810 e943ba3
Merge branch 'master' into iceberg-schema-comparator
subodh1810 812a48d
increment version
subodh1810 b4aefa4
Merge branch 'master' into iceberg-schema-comparator
subodh1810 bff174d
increment version
subodh1810 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
231 changes: 231 additions & 0 deletions
231
.../src/main/kotlin/io/airbyte/integrations/destination/iceberg/v2/IcebergTypesComparator.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,231 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.integrations.destination.iceberg.v2 | ||
|
||
import jakarta.inject.Singleton | ||
import org.apache.iceberg.Schema | ||
import org.apache.iceberg.types.Type | ||
import org.apache.iceberg.types.Types | ||
|
||
/** | ||
* Compares two Iceberg [Schema] definitions (including nested structs) to identify: | ||
* - New columns that do not exist in the "existing" schema. | ||
* - Columns whose data types have changed. | ||
* - Columns that no longer exist in the incoming schema (removed). | ||
* - Columns that changed from required to optional. | ||
*/ | ||
@Singleton | ||
class IcebergTypesComparator { | ||
|
||
companion object { | ||
/** Separator used to represent nested field paths: parent~child. */ | ||
const val PARENT_CHILD_SEPARATOR: Char = '~' | ||
|
||
/** | ||
* Returns a fully-qualified field name by appending `child` to `parent` using | ||
* [PARENT_CHILD_SEPARATOR]. If `parent` is blank, returns `child` alone. | ||
*/ | ||
private fun fullyQualifiedName(parent: String?, child: String): String = | ||
if (parent.isNullOrBlank()) child else "$parent$PARENT_CHILD_SEPARATOR$child" | ||
|
||
/** | ||
* Splits a fully-qualified name (e.g. `"outer~inner~field"`) into: | ||
* ``` | ||
* parent = "outer~inner" | ||
* leaf = "field" | ||
* ``` | ||
* If there's no [PARENT_CHILD_SEPARATOR], then it's top-level: | ||
* ``` | ||
* parent = "" | ||
* leaf = "outer" | ||
* ``` | ||
*/ | ||
fun splitIntoParentAndLeaf(fqName: String): Pair<String, String> { | ||
val idx = fqName.lastIndexOf(PARENT_CHILD_SEPARATOR) | ||
return if (idx < 0) { | ||
"" to fqName | ||
} else { | ||
fqName.substring(0, idx) to fqName.substring(idx + 1) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A data class representing differences between two Iceberg schemas. | ||
* | ||
* @property newColumns list of fully-qualified column names that are new in the incoming | ||
* schema. | ||
* @property updatedDataTypes list of fully-qualified column names whose types differ. | ||
* @property removedColumns list of fully-qualified column names that are no longer in the | ||
* incoming schema. | ||
* @property newlyOptionalColumns list of fully-qualified column names that changed from | ||
* required -> optional. | ||
*/ | ||
data class ColumnDiff( | ||
frifriSF59 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val newColumns: MutableList<String> = mutableListOf(), | ||
val updatedDataTypes: MutableList<String> = mutableListOf(), | ||
val removedColumns: MutableList<String> = mutableListOf(), | ||
val newlyOptionalColumns: MutableList<String> = mutableListOf() | ||
) { | ||
fun hasChanges(): Boolean { | ||
return newColumns.isNotEmpty() || | ||
updatedDataTypes.isNotEmpty() || | ||
removedColumns.isNotEmpty() || | ||
newlyOptionalColumns.isNotEmpty() | ||
} | ||
} | ||
|
||
/** | ||
* Compares [incomingSchema] with [existingSchema], returning a [ColumnDiff]. | ||
* | ||
* @param incomingSchema the schema of incoming data. | ||
* @param existingSchema the schema currently known/used by Iceberg. | ||
*/ | ||
fun compareSchemas(incomingSchema: Schema, existingSchema: Schema): ColumnDiff { | ||
val diff = ColumnDiff() | ||
compareStructFields( | ||
parentPath = null, | ||
incomingType = incomingSchema.asStruct(), | ||
existingType = existingSchema.asStruct(), | ||
diff = diff | ||
) | ||
return diff | ||
} | ||
|
||
/** | ||
* Recursively compares fields of two struct types, identifying new, updated, or removed | ||
* columns, and appending the results to [diff]. | ||
* | ||
* @param parentPath fully-qualified parent path, or `null` if at top-level. | ||
* @param incomingType struct type of the incoming schema. | ||
* @param existingType struct type of the existing schema. | ||
* @param diff the [ColumnDiff] object to be updated. | ||
*/ | ||
private fun compareStructFields( | ||
parentPath: String?, | ||
incomingType: Types.StructType, | ||
existingType: Types.StructType, | ||
diff: ColumnDiff | ||
) { | ||
val incomingFieldsByName = incomingType.fields().associateBy { it.name() } | ||
val existingFieldsByName = existingType.fields().associateBy { it.name() } | ||
|
||
// 1) Identify new and changed fields | ||
for ((fieldName, incomingField) in incomingFieldsByName) { | ||
val fqName = fullyQualifiedName(parentPath, fieldName) | ||
val existingField = existingFieldsByName[fieldName] | ||
|
||
if (existingField == null) { | ||
// This column does not exist in the existing schema => new column | ||
diff.newColumns.add(fqName) | ||
} else { | ||
// The column exists in both => check for type differences at top-level | ||
if ( | ||
parentPath.isNullOrBlank() && | ||
!typesAreEqual(incomingField.type(), existingField.type()) | ||
) { | ||
diff.updatedDataTypes.add(fqName) | ||
} | ||
|
||
// Check if it changed from required to optional at top-level | ||
val wasRequired = !existingField.isOptional | ||
val isNowOptional = incomingField.isOptional | ||
if (parentPath.isNullOrBlank() && wasRequired && isNowOptional) { | ||
diff.newlyOptionalColumns.add(fqName) | ||
} | ||
|
||
// If both are struct types, recursively compare subfields | ||
if (incomingField.type().isStructType && existingField.type().isStructType) { | ||
compareStructFields( | ||
parentPath = fqName, | ||
incomingType = incomingField.type().asStructType(), | ||
existingType = existingField.type().asStructType(), | ||
diff = diff | ||
) | ||
} | ||
} | ||
} | ||
|
||
// 2) Identify removed fields (only at top-level) | ||
if (parentPath.isNullOrBlank()) { | ||
for ((existingName) in existingFieldsByName) { | ||
if (!incomingFieldsByName.containsKey(existingName)) { | ||
val fqName = fullyQualifiedName(parentPath, existingName) | ||
diff.removedColumns.add(fqName) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if two Iceberg [Type]s are semantically equal by comparing type IDs and any relevant | ||
* sub-properties (e.g., for timestamps, lists, structs). | ||
* | ||
* @param incomingType the type from the incoming schema. | ||
* @param existingType the type from the existing schema. | ||
* @return `true` if they are effectively the same type, `false` otherwise. | ||
* @throws IllegalArgumentException if an unsupported or unmapped Iceberg type is encountered. | ||
*/ | ||
fun typesAreEqual(incomingType: Type, existingType: Type): Boolean { | ||
if (existingType.typeId() != incomingType.typeId()) return false | ||
|
||
return when (val typeId = existingType.typeId()) { | ||
Type.TypeID.BOOLEAN, | ||
Type.TypeID.INTEGER, | ||
Type.TypeID.LONG, | ||
Type.TypeID.FLOAT, | ||
Type.TypeID.DOUBLE, | ||
Type.TypeID.DATE, | ||
Type.TypeID.TIME, | ||
Type.TypeID.STRING -> { | ||
// Matching primitive types | ||
true | ||
} | ||
Type.TypeID.TIMESTAMP -> { | ||
require( | ||
existingType is Types.TimestampType && incomingType is Types.TimestampType | ||
) { "Expected TIMESTAMP types, got $existingType and $incomingType." } | ||
// Must match UTC adjustment or not | ||
existingType.shouldAdjustToUTC() == incomingType.shouldAdjustToUTC() | ||
} | ||
Type.TypeID.LIST -> { | ||
require(existingType is Types.ListType && incomingType is Types.ListType) { | ||
"Expected LIST types, but received $existingType and $incomingType." | ||
} | ||
val sameElementType = | ||
typesAreEqual(incomingType.elementType(), existingType.elementType()) | ||
sameElementType && | ||
(existingType.isElementOptional == incomingType.isElementOptional) | ||
} | ||
Type.TypeID.STRUCT -> { | ||
val incomingStructFields = | ||
incomingType.asStructType().fields().associateBy { it.name() } | ||
val existingStructFields = | ||
existingType.asStructType().fields().associateBy { it.name() } | ||
|
||
// For all fields in existing, ensure there's a matching field in incoming | ||
for ((name, existingField) in existingStructFields) { | ||
val incomingField = incomingStructFields[name] ?: return false | ||
if (existingField.isOptional != incomingField.isOptional) return false | ||
if (!typesAreEqual(incomingField.type(), existingField.type())) return false | ||
} | ||
// If there are extra fields in `incoming`, that doesn't mean they're "unequal" per | ||
// se — | ||
// but for this function's purpose, we only check the existing fields. | ||
true | ||
} | ||
Type.TypeID.BINARY, | ||
Type.TypeID.DECIMAL, | ||
Type.TypeID.FIXED, | ||
Type.TypeID.UUID, | ||
Type.TypeID.MAP, | ||
Type.TypeID.TIMESTAMP_NANO -> { | ||
throw IllegalArgumentException( | ||
"Unsupported or unmapped Iceberg type: $typeId. Implement handling if needed." | ||
) | ||
} | ||
} | ||
} | ||
} |
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.
wondering: which of these is better
I think diffing iceberg schemas (i.e. keep your current approach) is better? b/c (a) iceberg types don't perfectly align with airbyte types, and (b) iceberg is weird b/c it has recursive schemas?
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.
Yup! I did try to make it generic but realised it's not worth trying to solve a future problem when we dont know enough about it. I think for now this being iceberg specific is fair enough and we can re-visit it once we have more understandings of how this can be generalised and made better