voonhous commented on code in PR #20895:
URL: https://github.com/apache/hudi/pull/20895#discussion_r4717208886
##########
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:
**blocker:** With `ON false`, `when not matched by source then delete`
should remove every target row, but this path ignores the clause: on head `...
on false when not matched then insert * when not matched by source then delete`
keeps rows 1 and 2 and inserts 3, while master refuses the statement. #20840 is
still open. Could this land only after #20840, with an `ON false` plus BY
SOURCE refusal case added to its test or to this one?
##########
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:
**major:** The clause condition is evaluated once in the `Filter` and again
in every column's `CaseWhen`, so with `when not matched and rand() < 0.5` a row
can pass the filter, match no branch and fail the statement with `recordKey
value: "null" for field: "id"`. Could non-deterministic clause conditions be
refused here, or the clause index computed once in a single `Project` and
filtered on?
##########
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:
**blocker:** On a partitioned table with a record key, `on false when not
matched then insert (id, name) values (...)` writes the row to
`__HIVE_DEFAULT_PARTITION__` with `dt` NULL. With a source that has no `dt`,
the equality MERGE fails with "Failed to resolve partition fields" (#19610),
but `ON false` still writes the row. Could this path apply the same
partition-field resolution, with a partitioned table added to the
partial-insert case?
--
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]