hudi-agent commented on code in PR #18540:
URL: https://github.com/apache/hudi/pull/18540#discussion_r3143065830
##########
hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/hudi/command/MergeIntoHoodieTableCommand.scala:
##########
@@ -407,6 +407,16 @@ case class MergeIntoHoodieTableCommand(mergeInto:
MergeIntoTable) extends Hoodie
* expressions to the ExpressionPayload#getInsertValue.
*/
private def executeUpsert(sourceDF: DataFrame, parameters: Map[String,
String]): Unit = {
+ // Source data doesn't carry custom Hudi logical-type metadata (hudi_type,
e.g. VECTOR/BLOB);
+ // restore it from the catalog schema so downstream Avro conversion emits
the right branch.
+ val enrichedSourceSchema =
HoodieSchemaConversionUtils.alignSchemaWithCatalog(
+ sourceDF.schema,
+ hoodieCatalogTable.tableSchemaWithoutMetaFields,
+ sparkSession.sessionState.conf.caseSensitiveAnalysis,
+ alignNullability = false)
Review Comment:
🤖 `enrichedSourceDF` is built here at the top of `executeUpsert`, but the
diff only updates two later references (`enrichedSourceDF.schema` at ~497 and
`enrichedSourceDF` at ~508). Could you double-check that no other `sourceDF`
reference in the unchanged portion of this method (e.g. building joined output,
projections, schema bookkeeping) is still seeing the un-enriched schema? A
mixed view would leave `hudi_type` metadata only partially threaded through.
Same applies to the spark4-common copy.
<sub><i>- AI-generated; verify before applying. React 👍/👎 to flag
quality.</i></sub>
##########
hudi-client/hudi-spark-client/src/main/scala/org/apache/hudi/HoodieSchemaConversionUtils.scala:
##########
@@ -145,6 +146,82 @@ object HoodieSchemaConversionUtils {
}
}
+ /**
+ * Re-attach catalog metadata/nullability that Spark's write-path rewrites
strip:
+ *
+ * 1. VECTOR/BLOB logical-type metadata - dropped by TableOutputResolver's
Cast and
+ * UPDATE's castIfNeeded; without it, conversion falls back to the
physical type
+ * (ARRAY/STRUCT) and schema compatibility fails.
+ * 2. Nullability (only when `alignNullability = true`) - VALUES literals
and Cast
+ * outputs are nullable by default, which conflicts with non-null
catalog columns
+ * (e.g. primaryKey) once this schema becomes the writer's canonical
schema.
+ *
+ * `alignNullability = true` is only safe when upstream null-assertion
expressions
+ * guarantee no nulls flow into non-null columns (INSERT/UPDATE via
TableOutputResolver/
+ * castIfNeeded). MERGE must pass `false`: assignments run later inside
ExpressionPayload,
+ * so raw source rows may legitimately carry nulls.
+ *
+ * BLOB is projected nullable-everywhere by
[[HoodieSparkSchemaConverters.toSqlType]], so
+ * the nullability branch is a no-op inside BLOB subtrees; RFC-100 non-null
invariants are
+ * enforced at the physical-schema boundary in HoodieSchema.Blob#createBlob.
+ *
+ * Recurses into nested structs, struct-element arrays, and struct-value
maps. Source-only
+ * fields (e.g. MERGE join keys) are returned unchanged.
+ */
+ def alignSchemaWithCatalog(sourceSchema: StructType,
+ targetSchema: StructType,
+ caseSensitive: Boolean,
+ alignNullability: Boolean): StructType = {
+ val lookupKey: String => String =
+ if (caseSensitive) identity else (_: String).toLowerCase(Locale.ROOT)
+ val targetByName: Map[String, StructField] =
+ targetSchema.fields.map(f => lookupKey(f.name) -> f).toMap
+
+ StructType(sourceSchema.fields.map { field =>
+ targetByName.get(lookupKey(field.name)) match {
+ case Some(target) => alignField(field, target, caseSensitive,
alignNullability)
+ case None => field
+ }
+ })
+ }
+
+ private def alignField(source: StructField,
+ target: StructField,
+ caseSensitive: Boolean,
+ alignNullability: Boolean): StructField = {
+ val alignedNullable = if (alignNullability) target.nullable else
source.nullable
Review Comment:
🤖 For VECTOR (`ArrayType[FloatType]`), neither element is a `StructType`, so
this branch is skipped and only top-level nullability is adjusted; source's
`containsNull` is preserved. In the MERGE path (`alignNullability=false`) with
literals like `array(cast(0.9 as float), ...)` whose `containsNull` may be
`true`, the metadata is re-attached on top of an array-of-nullable-floats. Have
you verified that `convertStructTypeToHoodieSchema` emits the correct non-null
VECTOR element schema in that case (rather than e.g. failing schema-compat with
a nullable element union)?
<sub><i>- AI-generated; verify before applying. React 👍/👎 to flag
quality.</i></sub>
--
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]