yihua commented on code in PR #20886:
URL: https://github.com/apache/hudi/pull/20886#discussion_r4702874570


##########
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:
   Fixed, UPDATE now refuses to change a partition field of a merge-on-read 
table whose ordering field is nested, unless the table uses commit time 
ordering. Without that, I saw the statement commit and leave the record in both 
partitions.



##########
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:
   Done, the filter now keeps only the columns that `Murmur3Hash` accepts, with 
a Spark 4 VARIANT test.



##########
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:
   Done, a partition column that the merge condition equates to the assigned 
value now counts as unchanged and skips the window.



-- 
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]

Reply via email to