-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Destination Redshift: Add generation_id and sync_id #40201
Merged
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,12 +90,9 @@ class RedshiftDestinationHandler( | |
for (transaction in transactions) { | ||
val transactionId = UUID.randomUUID() | ||
if (logStatements) { | ||
log.info( | ||
"Executing sql {}-{}: {}", | ||
queryId, | ||
transactionId, | ||
java.lang.String.join("\n", transaction) | ||
) | ||
log.info { | ||
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. these log statements generated a compiler warning, so switch to the non-deprecated version |
||
"Executing sql $queryId-$transactionId: ${transaction.joinToString("\n")}" | ||
} | ||
} | ||
val startTime = System.currentTimeMillis() | ||
|
||
|
@@ -113,7 +110,7 @@ class RedshiftDestinationHandler( | |
logStatements = logStatements | ||
) | ||
} catch (e: SQLException) { | ||
log.error("Sql {}-{} failed", queryId, transactionId, e) | ||
log.error(e) { "Sql $queryId-$transactionId failed" } | ||
// This is a big hammer for something that should be much more targetted, only when | ||
// executing the | ||
// DROP TABLE command. | ||
|
@@ -129,12 +126,9 @@ class RedshiftDestinationHandler( | |
throw e | ||
} | ||
|
||
log.info( | ||
"Sql {}-{} completed in {} ms", | ||
queryId, | ||
transactionId, | ||
System.currentTimeMillis() - startTime | ||
) | ||
log.info { | ||
"Sql $queryId-$transactionId completed in ${System.currentTimeMillis() - startTime} ms" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -156,7 +150,8 @@ class RedshiftDestinationHandler( | |
return RedshiftState( | ||
json.hasNonNull("needsSoftReset") && json["needsSoftReset"].asBoolean(), | ||
json.hasNonNull("isAirbyteMetaPresentInRaw") && | ||
json["isAirbyteMetaPresentInRaw"].asBoolean() | ||
json["isAirbyteMetaPresentInRaw"].asBoolean(), | ||
json.hasNonNull("isGenerationIdPresent") && json["isGenerationIdPresent"].asBoolean(), | ||
) | ||
} | ||
|
||
|
109 changes: 109 additions & 0 deletions
109
...irbyte/integrations/destination/redshift/typing_deduping/RedshiftGenerationIdMigration.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,109 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.integrations.destination.redshift.typing_deduping | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import io.airbyte.cdk.db.jdbc.JdbcDatabase | ||
import io.airbyte.cdk.integrations.base.JavaBaseConstants | ||
import io.airbyte.integrations.base.destination.typing_deduping.DestinationHandler | ||
import io.airbyte.integrations.base.destination.typing_deduping.DestinationInitialStatus | ||
import io.airbyte.integrations.base.destination.typing_deduping.StreamConfig | ||
import io.airbyte.integrations.base.destination.typing_deduping.migrators.Migration | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
class RedshiftGenerationIdMigration( | ||
private val database: JdbcDatabase, | ||
private val databaseName: String, | ||
) : Migration<RedshiftState> { | ||
override fun migrateIfNecessary( | ||
destinationHandler: DestinationHandler<RedshiftState>, | ||
stream: StreamConfig, | ||
state: DestinationInitialStatus<RedshiftState> | ||
): Migration.MigrationResult<RedshiftState> { | ||
if (state.destinationState.isGenerationIdPresent) { | ||
logger.info { | ||
"Skipping generation_id migration for ${stream.id.originalNamespace}.${stream.id.originalName} because our state says it's already done" | ||
} | ||
return Migration.MigrationResult(state.destinationState, invalidateInitialState = false) | ||
} | ||
|
||
if (!state.initialRawTableStatus.rawTableExists) { | ||
// The raw table doesn't exist. No migration necessary. Update the state. | ||
logger.info { | ||
"Skipping generation_id migration for ${stream.id.originalNamespace}.${stream.id.originalName} because the raw table doesn't exist" | ||
} | ||
return Migration.MigrationResult( | ||
state.destinationState.copy(isGenerationIdPresent = true), | ||
invalidateInitialState = false | ||
) | ||
} | ||
|
||
// Add generation_id to the raw table if necessary | ||
val rawTableDefinitionQueryResult: List<JsonNode> = | ||
database.queryJsons( | ||
""" | ||
SHOW COLUMNS | ||
FROM TABLE "$databaseName"."${stream.id.rawNamespace}"."${stream.id.rawName}" | ||
LIKE '${JavaBaseConstants.COLUMN_NAME_AB_GENERATION_ID}' | ||
""".trimIndent() | ||
) | ||
if (rawTableDefinitionQueryResult.isNotEmpty()) { | ||
logger.info { | ||
"${stream.id.originalNamespace}.${stream.id.originalName}: Skipping generation_id migration for raw table because it already has the generation_id column" | ||
} | ||
} else { | ||
logger.info { | ||
"Migrating generation_id for table ${stream.id.rawNamespace}.${stream.id.rawName}" | ||
} | ||
// Quote for raw table columns | ||
val alterRawTableSql = | ||
""" | ||
ALTER TABLE "${stream.id.rawNamespace}"."${stream.id.rawName}" | ||
ADD COLUMN "${JavaBaseConstants.COLUMN_NAME_AB_GENERATION_ID}" BIGINT; | ||
""".trimIndent() | ||
database.execute(alterRawTableSql) | ||
} | ||
|
||
// Add generation_id to the final table if necessary | ||
// As a slight optimization, only do this if we previously detected that the final table | ||
// schema is wrong | ||
if (state.isFinalTablePresent && state.isSchemaMismatch) { | ||
val finalTableColumnQueryResult: List<JsonNode> = | ||
database.queryJsons( | ||
""" | ||
SHOW COLUMNS | ||
FROM TABLE "$databaseName"."${stream.id.finalNamespace}"."${stream.id.finalName}" | ||
LIKE '${JavaBaseConstants.COLUMN_NAME_AB_GENERATION_ID}' | ||
""".trimIndent() | ||
) | ||
if (finalTableColumnQueryResult.isNotEmpty()) { | ||
logger.info { | ||
"${stream.id.originalNamespace}.${stream.id.originalName}: Skipping generation_id migration for final table because it already has the generation_id column" | ||
} | ||
} else { | ||
logger.info { | ||
"Migrating generation_id for table ${stream.id.finalNamespace}.${stream.id.finalName}" | ||
} | ||
database.execute( | ||
""" | ||
ALTER TABLE "${stream.id.finalNamespace}"."${stream.id.finalName}" | ||
ADD COLUMN "${JavaBaseConstants.COLUMN_NAME_AB_GENERATION_ID}" BIGINT NULL; | ||
""".trimIndent() | ||
) | ||
} | ||
} else { | ||
logger.info { | ||
"${stream.id.originalNamespace}.${stream.id.originalName}: Skipping generation_id migration for final table. Final table exists: ${state.isFinalTablePresent}; final table schema is incorrect: ${state.isSchemaMismatch}" | ||
} | ||
} | ||
|
||
return Migration.MigrationResult( | ||
state.destinationState.copy(isGenerationIdPresent = true), | ||
invalidateInitialState = true | ||
) | ||
} | ||
} |
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
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.
this now happens in CatalogParser, so don't do it redundantly