yihua commented on code in PR #20895:
URL: https://github.com/apache/hudi/pull/20895#discussion_r4725590192
##########
hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -318,20 +319,86 @@ case class MergeIntoHoodieTableCommand(mergeInto:
MergeIntoTable) extends Hoodie
override def run(sparkSession: SparkSession): Seq[Row] = {
this.sparkSession = sparkSession
- // TODO move to analysis phase
- // Create the write parameters
- val props = buildMergeIntoConfig(hoodieCatalogTable)
- validate(props)
-
- val processedInputDf: DataFrame = getProcessedInputDf(props)
- // Do the upsert
- executeUpsert(processedInputDf, props)
- // Refresh the table in the catalog
- sparkSession.catalog.refreshTable(hoodieCatalogTable.table.qualifiedName)
+ if (hasPrimaryKey() && matchesNoRecord && indexFindsEveryKey) {
+ insertSourceRecords()
+ } else {
+ // TODO move to analysis phase
+ // Create the write parameters
+ val props = buildMergeIntoConfig(hoodieCatalogTable)
+ validate(props)
+
+ val processedInputDf: DataFrame = getProcessedInputDf(props)
+ // Do the upsert
+ executeUpsert(processedInputDf, props)
+ // Refresh the table in the catalog
+ sparkSession.catalog.refreshTable(hoodieCatalogTable.table.qualifiedName)
+ }
Seq.empty[Row]
}
+ /**
+ * Whether the merge condition is always false, so that no source record
matches a record of the table.
+ */
+ private lazy val matchesNoRecord: Boolean =
mergeInto.mergeCondition.foldable &&
+
Try(mergeInto.mergeCondition.eval()).toOption.contains(java.lang.Boolean.FALSE)
+
+ /**
+ * Whether the index finds every record key the table holds, so that the
insert of a statement whose merge
+ * condition matches no record can fail on a key the table already has: a
built record-level index that the write
+ * does not drop with the metadata table, or an index of a copy-on-write
table that reads the keys of the base
+ * files, as a record-level index also does until it is built. A
user-defined index class, which takes precedence
+ * over the index type, is not known to.
+ */
+ private lazy val indexFindsEveryKey: Boolean = {
+ val opts = combineOptions(hoodieCatalogTable,
hoodieCatalogTable.tableConfig, sparkSession.sessionState.conf,
+ defaultOpts = Map.empty)
+ val indexType = opts.getOrElse(HoodieIndexConfig.INDEX_TYPE.key,
HoodieIndex.IndexType.SIMPLE.name)
+ def isOneOf(types: HoodieIndex.IndexType*): Boolean =
types.exists(_.name.equalsIgnoreCase(indexType))
+ val isRecordIndex = isOneOf(HoodieIndex.IndexType.RECORD_INDEX,
HoodieIndex.IndexType.GLOBAL_RECORD_LEVEL_INDEX,
+ HoodieIndex.IndexType.RECORD_LEVEL_INDEX)
+ val keepsMetadataTable = opts.getOrElse(HoodieMetadataConfig.ENABLE.key,
HoodieMetadataConfig.ENABLE.defaultValue.toString).toBoolean
+ val findsEveryKey = if (isRecordIndex &&
hoodieCatalogTable.tableConfig.isMetadataPartitionAvailable(MetadataPartitionType.RECORD_INDEX))
{
+ keepsMetadataTable
+ } else {
+ targetTableType == COW_TABLE_TYPE_OPT_VAL && (isRecordIndex ||
isOneOf(HoodieIndex.IndexType.SIMPLE,
+ HoodieIndex.IndexType.BLOOM, HoodieIndex.IndexType.GLOBAL_SIMPLE,
HoodieIndex.IndexType.GLOBAL_BLOOM))
+ }
+ findsEveryKey &&
opts.get(HoodieIndexConfig.INDEX_CLASS_NAME.key).forall(_.isEmpty)
+ }
+
+ /**
+ * Runs a statement whose merge condition matches no record as the INSERT
INTO it comes to: the first
+ * not-matched clause that accepts a source record inserts it, and the
matched clauses apply to no record.
+ * This is how a table with a record key takes such a statement, as its
merge condition is otherwise an
+ * equality on the record key. The insert fails on a key the table already
holds, or that more than one
+ * inserted source record carries, as the upsert of a not-matched clause
never writes a second record for
+ * one key.
+ */
+ private def insertSourceRecords(): Unit = {
+ if (insertingActions.nonEmpty) {
+ checkInsertingActions(insertingActions,
buildMergeIntoConfig(hoodieCatalogTable))
Review Comment:
Follow-up fix: an insert clause that leaves out the partition field now
takes it from the source, as the merge on the record key does, so `insert (id,
name)` with a source that has `dt` writes to that partition instead of the
default one, and a clause that assigns the field keeps its value. The
partitioned always-false test covers both.
--
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]