Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ case class MergeIntoPaimonTable(
constructChangedRows(
sparkSession,
createDataset(sparkSession, filteredTargetPlan),
remainDeletedRow = true))
remainDeletedRow = true,
pruneJoinColumns = true))
}

private def performMergeForNonPkTable(sparkSession: SparkSession): Seq[CommitMessage] = {
Expand Down Expand Up @@ -239,12 +240,26 @@ case class MergeIntoPaimonTable(
remainDeletedRow: Boolean = false,
deletionVectorEnabled: Boolean = false,
extraMetadataCols: Seq[PaimonMetadataColumn] = Seq.empty,
writeRowTracking: Boolean = false): Dataset[Row] = {
writeRowTracking: Boolean = false,
pruneJoinColumns: Boolean = false): Dataset[Row] = {
val targetPlan = targetDataset.queryExecution.analyzed
val (targetJoinOutput, sourceJoinOutput) =
if (pruneJoinColumns) {
pruneJoinColumnsForMerge(
mergeCondition,
matchedActions,
notMatchedActions,
notMatchedBySourceActions,
targetPlan.output,
sourceTable.output,
remainDeletedRow || deletionVectorEnabled)
} else {
(targetPlan.output, sourceTable.output)
}
val targetProject =
Project(targetPlan.output :+ Alias(Literal(true), TARGET_ROW_COL)(), targetPlan)
Project(targetJoinOutput :+ Alias(Literal(true), TARGET_ROW_COL)(), targetPlan)
val sourceProject =
Project(sourceTable.output :+ Alias(Literal(true), SOURCE_ROW_COL)(), sourceTable)
Project(sourceJoinOutput :+ Alias(Literal(true), SOURCE_ROW_COL)(), sourceTable)
val joinNode =
Join(sourceProject, targetProject, FullOuter, Some(mergeCondition), JoinHint.NONE)
val joinedDS = createDataset(sparkSession, joinNode)
Expand All @@ -270,8 +285,15 @@ case class MergeIntoPaimonTable(
(null, null)
}

val targetOutput = if (writeRowTracking) {
val targetOutput: Seq[Expression] = if (writeRowTracking) {
filteredTargetPlan.output ++ Seq(rowIdAttr, sequenceNumberAttr)
} else if (pruneJoinColumns) {
// Templates span the full output schema; NULL-filled slots belong only to rows the processor
// drops, or to DELETE rows, which force unpruned columns.
val keptExprIds = targetJoinOutput.map(_.exprId).toSet
targetPlan.output.map {
attr => if (keptExprIds.contains(attr.exprId)) attr else Literal(null, attr.dataType)
}
} else {
filteredTargetPlan.output
}
Expand Down Expand Up @@ -368,6 +390,44 @@ object MergeIntoPaimonTable {
private val ROW_KIND_COL: String = SparkSystemColumns.ROW_KIND_COL
private val NOOP_ROW_KIND_VALUE: Byte = "-1".toByte

/**
* Computes the target/source columns that must survive the merge join: the references of the
* merge condition plus every action's condition and assignment values, in the original output
* order. A DELETE clause materializes `-D` records carrying the full target row (its value is
* consumed by the merge engines), so with deleted rows the target side is not pruned.
*/
private[commands] def pruneJoinColumnsForMerge(
mergeCondition: Expression,
matchedActions: Seq[MergeAction],
notMatchedActions: Seq[MergeAction],
notMatchedBySourceActions: Seq[MergeAction],
targetOutput: Seq[Attribute],
sourceOutput: Seq[Attribute],
remainDeletedRow: Boolean): (Seq[Attribute], Seq[Attribute]) = {
val actionExpressions =
(matchedActions ++ notMatchedActions ++ notMatchedBySourceActions).flatMap {
case PaimonUpdateAction(condition, assignments) =>
condition.toSeq ++ assignments.map(_.value)
case InsertAction(condition, assignments) =>
condition.toSeq ++ assignments.map(_.value)
case DeleteAction(condition) => condition.toSeq
case _ => Seq.empty[Expression]
}
val referencedExprIds =
(mergeCondition +: actionExpressions).flatMap(_.references).map(_.exprId).toSet
val keepFullTargetRows =
remainDeletedRow &&
(matchedActions ++ notMatchedBySourceActions).exists(_.isInstanceOf[DeleteAction])
val neededTarget =
if (keepFullTargetRows) {
targetOutput
} else {
targetOutput.filter(attr => referencedExprIds.contains(attr.exprId))
}
val neededSource = sourceOutput.filter(attr => referencedExprIds.contains(attr.exprId))
(neededTarget, neededSource)
}

private case class MergeIntoProcessor(
joinedAttributes: Seq[Attribute],
targetRowHasNoMatch: Expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ case class MergeIntoPaimonTable(
constructChangedRows(
sparkSession,
createDataset(sparkSession, filteredTargetPlan),
remainDeletedRow = true))
remainDeletedRow = true,
pruneJoinColumns = true))
}

private def performMergeForNonPkTable(sparkSession: SparkSession): Seq[CommitMessage] = {
Expand Down Expand Up @@ -239,12 +240,26 @@ case class MergeIntoPaimonTable(
remainDeletedRow: Boolean = false,
deletionVectorEnabled: Boolean = false,
extraMetadataCols: Seq[PaimonMetadataColumn] = Seq.empty,
writeRowTracking: Boolean = false): Dataset[Row] = {
writeRowTracking: Boolean = false,
pruneJoinColumns: Boolean = false): Dataset[Row] = {
val targetPlan = targetDataset.queryExecution.analyzed
val (targetJoinOutput, sourceJoinOutput) =
if (pruneJoinColumns) {
pruneJoinColumnsForMerge(
mergeCondition,
matchedActions,
notMatchedActions,
notMatchedBySourceActions,
targetPlan.output,
sourceTable.output,
remainDeletedRow || deletionVectorEnabled)
} else {
(targetPlan.output, sourceTable.output)
}
val targetProject =
Project(targetPlan.output :+ Alias(Literal(true), TARGET_ROW_COL)(), targetPlan)
Project(targetJoinOutput :+ Alias(Literal(true), TARGET_ROW_COL)(), targetPlan)
val sourceProject =
Project(sourceTable.output :+ Alias(Literal(true), SOURCE_ROW_COL)(), sourceTable)
Project(sourceJoinOutput :+ Alias(Literal(true), SOURCE_ROW_COL)(), sourceTable)
val joinNode =
Join(sourceProject, targetProject, FullOuter, Some(mergeCondition), JoinHint.NONE)
val joinedDS = createDataset(sparkSession, joinNode)
Expand All @@ -270,8 +285,15 @@ case class MergeIntoPaimonTable(
(null, null)
}

val targetOutput = if (writeRowTracking) {
val targetOutput: Seq[Expression] = if (writeRowTracking) {
filteredTargetPlan.output ++ Seq(rowIdAttr, sequenceNumberAttr)
} else if (pruneJoinColumns) {
// Templates span the full output schema; NULL-filled slots belong only to rows the processor
// drops, or to DELETE rows, which force unpruned columns.
val keptExprIds = targetJoinOutput.map(_.exprId).toSet
targetPlan.output.map {
attr => if (keptExprIds.contains(attr.exprId)) attr else Literal(null, attr.dataType)
}
} else {
filteredTargetPlan.output
}
Expand Down Expand Up @@ -368,6 +390,44 @@ object MergeIntoPaimonTable {
private val ROW_KIND_COL: String = SparkSystemColumns.ROW_KIND_COL
private val NOOP_ROW_KIND_VALUE: Byte = "-1".toByte

/**
* Computes the target/source columns that must survive the merge join: the references of the
* merge condition plus every action's condition and assignment values, in the original output
* order. A DELETE clause materializes `-D` records carrying the full target row (its value is
* consumed by the merge engines), so with deleted rows the target side is not pruned.
*/
private[commands] def pruneJoinColumnsForMerge(
mergeCondition: Expression,
matchedActions: Seq[MergeAction],
notMatchedActions: Seq[MergeAction],
notMatchedBySourceActions: Seq[MergeAction],
targetOutput: Seq[Attribute],
sourceOutput: Seq[Attribute],
remainDeletedRow: Boolean): (Seq[Attribute], Seq[Attribute]) = {
val actionExpressions =
(matchedActions ++ notMatchedActions ++ notMatchedBySourceActions).flatMap {
case PaimonUpdateAction(condition, assignments) =>
condition.toSeq ++ assignments.map(_.value)
case InsertAction(condition, assignments) =>
condition.toSeq ++ assignments.map(_.value)
case DeleteAction(condition) => condition.toSeq
case _ => Seq.empty[Expression]
}
val referencedExprIds =
(mergeCondition +: actionExpressions).flatMap(_.references).map(_.exprId).toSet
val keepFullTargetRows =
remainDeletedRow &&
(matchedActions ++ notMatchedBySourceActions).exists(_.isInstanceOf[DeleteAction])
val neededTarget =
if (keepFullTargetRows) {
targetOutput
} else {
targetOutput.filter(attr => referencedExprIds.contains(attr.exprId))
}
val neededSource = sourceOutput.filter(attr => referencedExprIds.contains(attr.exprId))
(neededTarget, neededSource)
}

private case class MergeIntoProcessor(
joinedAttributes: Seq[Attribute],
targetRowHasNoMatch: Expression,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.paimon.spark.commands

import org.apache.spark.sql.catalyst.expressions.{Add, AttributeReference, EqualTo, GreaterThan, Literal}
import org.apache.spark.sql.catalyst.plans.logical.{Assignment, DeleteAction, InsertAction, UpdateAction}
import org.apache.spark.sql.types.IntegerType
import org.scalatest.funsuite.AnyFunSuite

/** Unit tests for the pure column-collection logic behind MERGE INTO join pruning. */
class MergeIntoPaimonTablePruneColumnsTest extends AnyFunSuite {

private def attr(name: String): AttributeReference =
AttributeReference(name, IntegerType, nullable = true)()

test("collect referenced columns from conditions and assignment values on both sides") {
val tId = attr("id")
val tV1 = attr("v1")
val tV2 = attr("v2")
val tV3 = attr("v3")
val sId = attr("s_id")
val sV1 = attr("s_v1")

val (neededTarget, neededSource) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
// SET v1 = target.v1 + source.s_v1
Seq(UpdateAction(None, Seq(Assignment(tV1, Add(tV1, sV1))))),
// INSERT (id, v2) VALUES (s_id, literal)
Seq(InsertAction(None, Seq(Assignment(tId, sId), Assignment(tV2, Literal(1))))),
Seq.empty,
Seq(tId, tV1, tV2, tV3),
Seq(sId, sV1),
remainDeletedRow = true
)

// v3 is never referenced; assignment keys are not collected, so `v2` (a literal value) drops.
assert(neededTarget == Seq(tId, tV1))
assert(neededSource == Seq(sId, sV1))
}

test("delete clause keeps the full target side") {
val tId = attr("id")
val tV1 = attr("v1")
val sId = attr("s_id")
val fullTarget = Seq(tId, tV1)

// A matched DELETE materializes a -D record carrying the full target row.
val (matchedDelete, _) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
Seq(DeleteAction(None)),
Seq.empty,
Seq.empty,
fullTarget,
Seq(sId),
remainDeletedRow = true
)
assert(matchedDelete == fullTarget)

// Same for a NOT MATCHED BY SOURCE DELETE; once un-gated, its condition reference (`v1`)
// is collected alongside the merge condition's (`id`).
val nmsDelete = Seq(DeleteAction(Some(GreaterThan(tV1, Literal(5)))))
val (nmsGated, _) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
Seq.empty,
Seq.empty,
nmsDelete,
fullTarget,
Seq(sId),
remainDeletedRow = true
)
assert(nmsGated == fullTarget)
val (nmsUngated, _) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
Seq.empty,
Seq.empty,
nmsDelete,
fullTarget,
Seq(sId),
remainDeletedRow = false
)
assert(nmsUngated == Seq(tId, tV1))

// remainDeletedRow = false does not gate.
val (ungated, _) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
Seq(DeleteAction(None)),
Seq.empty,
Seq.empty,
fullTarget,
Seq(sId),
remainDeletedRow = false
)
assert(ungated == Seq(tId))

// A DELETE under WHEN NOT MATCHED never materializes -D records, so it does not gate.
val (notMatchedDelete, _) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
EqualTo(tId, sId),
Seq.empty,
Seq(DeleteAction(None)),
Seq.empty,
fullTarget,
Seq(sId),
remainDeletedRow = true
)
assert(notMatchedDelete == Seq(tId))
}

test("no referenced columns yields empty sets") {
val tV1 = attr("v1")

val (neededTarget, neededSource) = MergeIntoPaimonTable.pruneJoinColumnsForMerge(
Literal.TrueLiteral,
Seq.empty,
Seq(InsertAction(None, Seq(Assignment(tV1, Literal(1))))),
Seq.empty,
Seq(tV1),
Seq.empty,
remainDeletedRow = true
)

assert(neededTarget.isEmpty)
assert(neededSource.isEmpty)
}
}
Loading
Loading