yihua commented on code in PR #20895:
URL: https://github.com/apache/hudi/pull/20895#discussion_r4722792267
##########
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) {
Review Comment:
With #20840 merged and this rebased onto it, the clause is refused at
analysis, before the statement reaches this path; the always-false test now
checks that refusal too.
##########
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:
Fixed: this path now resolves the partition fields from the source as the
merge on the record key does, so a source without `dt` fails with the same
"Failed to resolve partition fields" error instead of writing to the default
partition. Covered with a partitioned table in the always-false tests.
##########
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))
+ val conditions =
insertingActions.map(_.condition.getOrElse(Literal.TrueLiteral))
+ val values = insertingActions.map(action =>
+ alignAssignments(action.assignments,
Some(PartialAssignmentMode.NULL_VALUE), keepUpdatedFieldsOnly =
false).map(_.value))
+ val output = mergeInto.targetTable.output.filterNot(attr =>
isMetaField(attr.name)).zip(values.transpose).map {
+ case (attr, clauseValues) =>
+ Alias(CaseWhen(conditions.zip(clauseValues.map(castIfNeeded(_,
attr.dataType)))), attr.name)()
+ }
+ val query = InsertIntoHoodieTableCommand.alignQueryOutput(
+ Project(output, Filter(conditions.reduce(Or), mergeInto.sourceTable)),
Review Comment:
Fixed: the clause conditions are now evaluated once, in one projection that
picks the first clause accepting each source record, and the filter and the
inserted values both read that pick. A test inserts with `rand(7) < 0.5` and
checks that every inserted row is whole.
--
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]