-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-56856][SDP] Implement SCD1 Batch Processor; Microbatch Deduplication #55969
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
Closed
AnishMahto
wants to merge
9
commits into
apache:master
from
AnishMahto:SPARK-56856-SCD1-microbatch-deduplication
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ada1bdb
Implement deduplicateMicrobatch
AnishMahto a1a0e7b
indenting cleanup
AnishMahto 434f6ad
schema comment
AnishMahto 022a95c
casing
AnishMahto f92f1e3
linting
AnishMahto 04a38f2
PR feedback
AnishMahto 19d9040
use reserved __spark_autocdc* prefix
AnishMahto 1e8b86c
Add deduplicate test when row contains nested columns
AnishMahto 0b498a0
PR feedback
AnishMahto 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 hidden or 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 hidden or 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
67 changes: 67 additions & 0 deletions
67
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/autocdc/Scd1BatchProcessor.scala
This file contains hidden or 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,67 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.pipelines.autocdc | ||
|
|
||
| import org.apache.spark.sql.{functions => F} | ||
| import org.apache.spark.sql.catalyst.util.QuotingUtils | ||
| import org.apache.spark.sql.classic.DataFrame | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
||
| /** | ||
| * Per-microbatch processor for SCD Type 1 AutoCDC flows, complying to the specified [[changeArgs]] | ||
| * configuration. | ||
| */ | ||
| case class Scd1BatchProcessor(changeArgs: ChangeArgs) { | ||
| /** | ||
| * Deduplicate the incoming CDC microbatch by key, keeping the most recent event per key | ||
| * as ordered by [[ChangeArgs.sequencing]]. | ||
| * | ||
| * For SCD1 we only care about the most recent (by sequence value) event per key. When | ||
| * multiple events share the same key and the same sequence value, the row selected is | ||
| * non-deterministic and undefined. | ||
| * | ||
| * @param validatedMicrobatch A microbatch that has already been validated such that the | ||
| * sequencing column should not contain null values, and its data type | ||
| * should support ordering. | ||
| * | ||
| * The schema of the returned dataframe matches the schema of the microbatch exactly. | ||
| */ | ||
| def deduplicateMicrobatch(validatedMicrobatch: DataFrame): DataFrame = { | ||
| // The `max_by` API can only return a single column, so pack/unpack the entire row into a | ||
| // temporary column before and after the `max_by` operation. | ||
| val winningRowCol = Scd1BatchProcessor.winningRowColName | ||
|
|
||
| val allMicrobatchColumns = | ||
| validatedMicrobatch.columns | ||
| .map(colName => F.col(QuotingUtils.quoteIdentifier(colName))) | ||
| .toImmutableArraySeq | ||
|
|
||
| validatedMicrobatch | ||
| .groupBy(changeArgs.keys.map(k => F.col(k.quoted)): _*) | ||
| .agg( | ||
| F.max_by(F.struct(allMicrobatchColumns: _*), changeArgs.sequencing) | ||
| .as(winningRowCol) | ||
| ) | ||
| .select(F.col(s"$winningRowCol.*")) | ||
| } | ||
| } | ||
|
|
||
| object Scd1BatchProcessor { | ||
| // Columns prefixed with `__spark_autocdc_` are reserved for internal SDP AutoCDC processing. | ||
| private[autocdc] val winningRowColName = "__spark_autocdc_winning_row" | ||
| } | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.