You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "Zouxxyy (via GitHub)" <gi...@apache.org> on 2026/05/08 14:52:56 UTC
[PR] [spark] Support merge schema in MERGE INTO [paimon]
Zouxxyy opened a new pull request, #7789:
URL: https://github.com/apache/paimon/pull/7789
### Purpose
- Support write merge schema in MERGE INTO: when `write-merge-schema` is enabled, commit schema changes from source to the Paimon table before action alignment, allowing `alignMergeIntoTable` to see the full merged schema including new columns
- Naturally supports nested struct evolution through the pre-merge schema approach
- Move `AssignmentAlignmentHelper` from spark3 to spark-common for reuse across Spark versions
- Refactor `SchemaHelper.scala` to better handle schema resolution during MERGE INTO
- Add `MergeIntoNotMatchedBySourceTest` and expand `MergeIntoTableTestBase` with comprehensive MERGE INTO test cases
### Tests
- `MergeIntoNotMatchedBySourceTest` for NOT MATCHED BY SOURCE clauses
- `MergeIntoTableTestBase` with 408+ lines of comprehensive MERGE INTO test coverage
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Support merge schema in MERGE INTO [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #7789:
URL: https://github.com/apache/paimon/pull/7789#discussion_r3212618948
##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/catalyst/analysis/AssignmentAlignmentHelper.scala:
##########
@@ -177,4 +175,72 @@ trait AssignmentAlignmentHelper extends SQLConfHelper with ExpressionHelper {
}
}
+ /**
+ * Resolve an assignment value expression by-name against the target type, then cast if needed.
+ * Recursively reorders nested type fields (Struct, Array, Map and any combination) by name to
+ * match target field order before casting. This is consistent with Spark's native MERGE INTO
+ * behavior (see TableOutputResolver.resolveUpdate).
+ */
+ private def resolveByNameAndCast(expression: Expression, targetType: DataType): Expression = {
+ if (PaimonUtils.sameType(expression.dataType, targetType)) {
+ // Types already structurally identical — no reordering needed.
+ // This guarantees idempotence when the rule is applied multiple times.
+ castIfNeeded(expression, targetType)
+ } else {
+ val reordered = reorderFieldsByName(expression, expression.dataType, targetType)
+ castIfNeeded(reordered, targetType)
+ }
+ }
+
+ /**
+ * Recursively reorder nested type fields by name to match target type's field order. Supports
+ * StructType, ArrayType and MapType in any nesting combination. Returns the original expression
+ * if no reordering is needed.
+ */
+ private def reorderFieldsByName(
+ expression: Expression,
+ sourceType: DataType,
+ targetType: DataType): Expression = {
+ (sourceType, targetType) match {
+ case (s: StructType, t: StructType) if s != t =>
+ reorderStructByName(expression, s, t)
+ case (ArrayType(sElem, sNull), ArrayType(tElem, _)) if sElem != tElem =>
+ val elementVar = NamedLambdaVariable("element", sElem, sNull)
+ val reordered = reorderFieldsByName(elementVar, sElem, tElem)
+ ArrayTransform(expression, LambdaFunction(reordered, Seq(elementVar)))
+ case (MapType(sKey, sVal, sValNull), MapType(tKey, tVal, _))
+ if sKey != tKey || sVal != tVal =>
+ val keyVar = NamedLambdaVariable("key", sKey, nullable = false)
+ val valVar = NamedLambdaVariable("value", sVal, sValNull)
+ val reorderedKey = reorderFieldsByName(keyVar, sKey, tKey)
+ val reorderedVal = reorderFieldsByName(valVar, sVal, tVal)
+ val newKeys = ArrayTransform(MapKeys(expression), LambdaFunction(reorderedKey, Seq(keyVar)))
+ val newVals =
+ ArrayTransform(MapValues(expression), LambdaFunction(reorderedVal, Seq(valVar)))
+ MapFromArrays(newKeys, newVals)
+ case _ =>
+ expression
+ }
+ }
+
+ /** Reorder source struct fields to match target field order by name, recursing into nested types. */
+ private def reorderStructByName(
Review Comment:
reorderStructByName crashes when target struct has fields absent from source
Should we support this?
The same issue applies to MapType value reordering in reorderFieldsByName.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Support merge schema in MERGE INTO [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi merged PR #7789:
URL: https://github.com/apache/paimon/pull/7789
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [spark] Support merge schema in MERGE INTO [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #7789:
URL: https://github.com/apache/paimon/pull/7789#discussion_r3212622974
##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/SchemaHelper.scala:
##########
@@ -41,21 +41,66 @@ private[spark] trait SchemaHelper extends WithFileStoreTable {
def mergeSchema(sparkSession: SparkSession, input: DataFrame, options: Options): DataFrame = {
val dataSchema = SparkSystemColumns.filterSparkSystemColumns(input.schema)
- val newTableSchema = mergeSchema(input.schema, options)
- if (!PaimonUtils.sameType(newTableSchema, dataSchema)) {
+ val writeSchema = mergeSchema(dataSchema, options)
+ if (!PaimonUtils.sameType(writeSchema, dataSchema)) {
val resolve = sparkSession.sessionState.conf.resolver
- val cols = alignColumns(newTableSchema, dataSchema, resolve)
+ val cols = SchemaHelper.alignColumns(writeSchema, dataSchema, resolve)
input.select(cols: _*)
} else {
input
}
}
+ def mergeSchema(dataSchema: StructType, options: Options): StructType = {
+ val mergeSchemaEnabled =
+ options.get(SparkConnectorOptions.MERGE_SCHEMA) || OptionUtils.writeMergeSchemaEnabled()
+ if (!mergeSchemaEnabled) {
+ return dataSchema
+ }
+
+ val filteredDataSchema = SparkSystemColumns.filterSparkSystemColumns(dataSchema)
+ val allowExplicitCast = options.get(SparkConnectorOptions.EXPLICIT_CAST) || OptionUtils
+ .writeMergeSchemaExplicitCastEnabled()
+ SchemaHelper.mergeAndCommitSchema(table, filteredDataSchema, allowExplicitCast).foreach {
+ updatedTable => newTable = Some(updatedTable)
+ }
+
+ val writeSchema = SparkTypeUtils.fromPaimonRowType(table.schema().logicalRowType())
+ if (!PaimonUtils.sameType(writeSchema, filteredDataSchema)) {
+ writeSchema
+ } else {
+ filteredDataSchema
+ }
+ }
+
+ def updateTableWithOptions(options: Map[String, String]): Unit = {
+ newTable = Some(table.copy(options.asJava))
+ }
+}
+
+private[spark] object SchemaHelper {
+
+ /**
+ * Merge the given dataSchema into the table's schema. If the schema changed, commit the change
+ * and return the updated table; otherwise return None.
+ */
+ def mergeAndCommitSchema(
+ table: FileStoreTable,
+ dataSchema: StructType,
+ allowExplicitCast: Boolean): Option[FileStoreTable] = {
+ val dataRowType = SparkTypeUtils.toPaimonType(dataSchema).asInstanceOf[RowType]
+ if (table.store().mergeSchema(dataRowType, allowExplicitCast)) {
+ Some(table.copyWithLatestSchema())
+ } else {
+ None
+ }
+ }
+
/**
* Recursively align columns from dataSchema to targetSchema by name. For nested struct fields,
* reorder and fill nulls for missing sub-fields.
*/
- private def alignColumns(
+ def alignColumns(
Review Comment:
SchemaHelper.scala now handles ArrayType<StructType> alignment via transform, but MapType<StructType> is not handled. Meanwhile, AssignmentAlignmentHelper.reorderFieldsByName does handle MapType. This inconsistency means the DataFrame write path won't align map values while the MERGE path will.
Is this a problem?
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org