voonhous commented on code in PR #20886:
URL: https://github.com/apache/hudi/pull/20886#discussion_r4695720360
##########
hudi-spark-datasource/hudi-spark4-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -463,6 +464,153 @@ case class MergeIntoHoodieTableCommand(mergeInto:
MergeIntoTable) extends Hoodie
sparkAdapter.getUnsafeUtils.createDataFrameFrom(sparkSession, amendedPlan)
}
+ /**
+ * The source records of a table without a record key, each with the meta
columns of the target record it
+ * matches. A matched record that takes an update clause giving a partition
column another value cannot be
+ * written back to the file group it was read from, so its source record
holds the updated record in
+ * [[MergeIntoKeyGenerator.MOVED_RECORD_FIELD]] and is split in two: one at
the location of the target record,
+ * which [[movedRecordClauses]] make delete it, and one without a location,
which they insert into the partition
+ * of its values. The update clauses are tried in the order
[[ExpressionPayload]] tries them, against the joined
+ * target row.
+ *
+ * A record is moved only when the write applies its update, which it
decides by the ordering value of the
+ * source record. The statement fails when that cannot be told from the
merge mode, see
+ * [[partitionChangeRefusal]], and when a moved record matches more than one
source record, as each of them
+ * would insert it again.
+ */
+ private def splitMovedRecords(joinData: LogicalPlan,
+ tableMetaCols: Seq[Attribute],
+ incomingDataCols: Seq[Attribute]): LogicalPlan
= {
+ val resolver = sparkSession.sessionState.conf.resolver
+ val targetDataAttrs = mergeInto.targetTable.output.filterNot(attr =>
isMetaField(attr.name))
+ val partitionAttrs = hoodieCatalogTable.partitionFields.toSeq.flatMap {
field =>
+ targetDataAttrs.find(attr => resolver(attr.name, field))
+ }
+
+ def updated(action: UpdateAction, attr: Attribute): Expression =
action.assignments.collectFirst {
+ case Assignment(assigned: Attribute, value) if attributeEquals(assigned,
attr) => castIfNeeded(value, attr.dataType)
+ }.getOrElse(attr)
+
+ val conditions =
updatingActions.map(_.condition.getOrElse(Literal.TrueLiteral))
+ val changesPartition = updatingActions.map { action =>
+ partitionAttrs.map(attr => updated(action, attr) -> attr)
+ .collect { case (value, attr) if !value.semanticEquals(attr) =>
Not(EqualNullSafe(value, attr)): Expression }
+ .reduceOption(Or).getOrElse(Literal.FalseLiteral)
+ }
+ if (changesPartition.forall(_ == Literal.FalseLiteral)) {
+ Project(tableMetaCols ++ incomingDataCols, joinData)
+ } else {
+ val recordKeyCol = tableMetaCols.find(_.name ==
HoodieRecord.RECORD_KEY_METADATA_FIELD).get
+ val partitionPathCol = tableMetaCols.find(_.name ==
HoodieRecord.PARTITION_PATH_METADATA_FIELD).get
+ def updatedByClause(attr: Attribute): Expression =
CaseWhen(conditions.zip(updatingActions.map(updated(_, attr))), attr)
+
+ // A merge that evaluates the clauses on the target record compares the
ordering value of the updated record,
+ // and one that does not compares that of the source record. Null when
the two disagree or the merge mode does
+ // not tell.
+ val (targetOrdering, sourceOrdering) =
orderingFieldsAssociatedExpressions.map {
+ case (attr, expr) => (attr, castIfNeeded(expr, attr.dataType))
+ }.unzip
+ val mergeMode = hoodieCatalogTable.tableConfig.getRecordMergeMode
+ val bySource = appliesUpdate(mergeMode, targetOrdering, sourceOrdering)
+ val byUpdate = appliesUpdate(mergeMode, targetOrdering,
targetOrdering.map(updatedByClause))
+ val updateApplies = If(EqualTo(bySource, byUpdate), bySource,
Literal(null, BooleanType))
+ val changes = And(IsNotNull(recordKeyCol),
CaseWhen(conditions.zip(changesPartition), Literal.FalseLiteral))
+ val moves = Alias(And(changes, EqualNullSafe(updateApplies,
Literal.TrueLiteral)), "_moves_record")()
+ val withMoves = Project(joinData.output :+ moves, joinData)
+
+ // Source records of a matched record, spreading the ones that match no
record by their values
+ val hashableCols =
incomingDataCols.filterNot(_.dataType.existsRecursively(_.isInstanceOf[MapType]))
Review Comment:
**blocker:** `hashableCols` drops only `MapType`, but Spark 4's
`HashExpression` also rejects VARIANT (`DATATYPE_MISMATCH.HASH_VARIANT_TYPE`),
so a keyless partitioned MERGE INTO with `UPDATE SET *` and a VARIANT source
column would start failing analysis on Spark 4. I have not run this. Could the
filter keep a column only when `new
Murmur3Hash(Seq(col)).checkInputDataTypes().isSuccess`, which holds across
Spark versions?
##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/HoodieCreateRecordUtils.scala:
##########
@@ -162,7 +173,16 @@ object HoodieCreateRecordUtils {
HoodieRecordUtils.createHoodieRecord(processedRecord, hoodieKey,
config.getPayloadClass, recordLocation, isDelete)
}
- hoodieRecord
+ if (movedTo.isDefined) {
+ val deleteRecord =
HoodieRecordUtils.generateEmptyAvroRecord(readKey, OrderingValues.getDefault,
config.getPayloadClass, null)
Review Comment:
**blocker:** On a MOR table with a nested ordering field, the delete this
adds looks like it cannot be written: `HoodieSchemaUtils.createDeleteLogSchema`
resolves `attributes.ts` with a top-level `getField` and throws, the failure is
swallowed by `hoodie.write.ignore.failed=true`, and the UPDATE returns without
committing or raising. Until that is fixed, could `movesOnlyAppliedUpdates`
refuse the move for MOR with a nested ordering field, the same way it refuses a
custom merger?
##########
hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -463,6 +464,153 @@ case class MergeIntoHoodieTableCommand(mergeInto:
MergeIntoTable) extends Hoodie
Dataset.ofRows(sparkSession, amendedPlan)
}
+ /**
+ * The source records of a table without a record key, each with the meta
columns of the target record it
+ * matches. A matched record that takes an update clause giving a partition
column another value cannot be
+ * written back to the file group it was read from, so its source record
holds the updated record in
+ * [[MergeIntoKeyGenerator.MOVED_RECORD_FIELD]] and is split in two: one at
the location of the target record,
+ * which [[movedRecordClauses]] make delete it, and one without a location,
which they insert into the partition
+ * of its values. The update clauses are tried in the order
[[ExpressionPayload]] tries them, against the joined
+ * target row.
+ *
+ * A record is moved only when the write applies its update, which it
decides by the ordering value of the
+ * source record. The statement fails when that cannot be told from the
merge mode, see
+ * [[partitionChangeRefusal]], and when a moved record matches more than one
source record, as each of them
+ * would insert it again.
+ */
+ private def splitMovedRecords(joinData: LogicalPlan,
+ tableMetaCols: Seq[Attribute],
+ incomingDataCols: Seq[Attribute]): LogicalPlan
= {
+ val resolver = sparkSession.sessionState.conf.resolver
+ val targetDataAttrs = mergeInto.targetTable.output.filterNot(attr =>
isMetaField(attr.name))
+ val partitionAttrs = hoodieCatalogTable.partitionFields.toSeq.flatMap {
field =>
+ targetDataAttrs.find(attr => resolver(attr.name, field))
+ }
+
+ def updated(action: UpdateAction, attr: Attribute): Expression =
action.assignments.collectFirst {
+ case Assignment(assigned: Attribute, value) if attributeEquals(assigned,
attr) => castIfNeeded(value, attr.dataType)
+ }.getOrElse(attr)
+
+ val conditions =
updatingActions.map(_.condition.getOrElse(Literal.TrueLiteral))
+ val changesPartition = updatingActions.map { action =>
+ partitionAttrs.map(attr => updated(action, attr) -> attr)
+ .collect { case (value, attr) if !value.semanticEquals(attr) =>
Not(EqualNullSafe(value, attr)): Expression }
+ .reduceOption(Or).getOrElse(Literal.FalseLiteral)
+ }
+ if (changesPartition.forall(_ == Literal.FalseLiteral)) {
Review Comment:
**blocker:** For a keyless partitioned table, `UPDATE SET *` expands to `pt
= s.pt`, which is never `semanticEquals` to `t.pt`, so this early return is not
taken and every such MERGE pays for the Window (an extra shuffle and sort of
the join output) plus the 2x Expand, even with `ON ... AND t.pt = s.pt` where
nothing can move. Could a partition attr count as unchanged when the ON
condition equates it to the assigned value?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]