hudi-agent commented on code in PR #18540:
URL: https://github.com/apache/hudi/pull/18540#discussion_r3143829436
##########
hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/avro/HoodieSparkSchemaConverters.scala:
##########
@@ -373,14 +420,25 @@ object HoodieSparkSchemaConverters extends
SparkAdapterSupport {
* Validates that a StructType matches the expected unshredded variant schema
* (two non-null {@code BinaryType} fields: {@code metadata} and {@code
value}).
*
+ * Note on nullability: unlike BLOB, VARIANT is not projected
nullable-everywhere at the
+ * Spark type layer because the user-facing path is gated. Spark 3.x rejects
VARIANT at
+ * schema resolution, and Spark 4.0+ exposes it as the native {@code
VariantType} populated
+ * via {@code parse_json(...)}, never a user-supplied {@code named_struct}.
The internal
+ * physical layout ({@code struct<metadata, value>} with non-null fields)
only appears
+ * through {@link HoodieSparkSchemaConverters#toSqlType}, which produces the
canonical
+ * non-null shape this validator expects.
+ *
* @param structType the StructType to validate
* @throws IllegalArgumentException if the structure does not match the
expected variant schema
*/
private def validateVariantStructure(structType: StructType): Unit = {
- val fieldsByName = structType.fields.map(f => f.name -> f).toMap
+ val caseSensitive = SQLConf.get.caseSensitiveAnalysis
+ val key: String => String =
+ if (caseSensitive) identity else (_: String).toLowerCase(Locale.ROOT)
+ val fieldsByName = structType.fields.map(f => key(f.name) -> f).toMap
val ok = structType.length == 2 &&
- fieldsByName.get(HoodieSchema.Variant.VARIANT_METADATA_FIELD).exists(f
=> f.dataType == BinaryType && !f.nullable) &&
- fieldsByName.get(HoodieSchema.Variant.VARIANT_VALUE_FIELD).exists(f =>
f.dataType == BinaryType && !f.nullable)
+
fieldsByName.get(key(HoodieSchema.Variant.VARIANT_METADATA_FIELD)).exists(f =>
f.dataType == BinaryType && !f.nullable) &&
+ fieldsByName.get(key(HoodieSchema.Variant.VARIANT_VALUE_FIELD)).exists(f
=> f.dataType == BinaryType && !f.nullable)
Review Comment:
_⚠️ Potential issue_ | _🟠 Major_
**Reject reordered VARIANT physical structs.**
This validator only checks for two named binary fields via a map, so
`struct<value: binary, metadata: binary>` currently passes. The documented
canonical layout is `struct<metadata: binary, value: binary>`, and Spark rows
are positional, so accepting reordered fields can corrupt the metadata/value
swap on write.
<details>
<summary>🔧 Suggested fix</summary>
```diff
private def validateVariantStructure(structType: StructType): Unit = {
val caseSensitive = SQLConf.get.caseSensitiveAnalysis
- val key: String => String =
- if (caseSensitive) identity else (_: String).toLowerCase(Locale.ROOT)
- val fieldsByName = structType.fields.map(f => key(f.name) -> f).toMap
- val ok = structType.length == 2 &&
-
fieldsByName.get(key(HoodieSchema.Variant.VARIANT_METADATA_FIELD)).exists(f =>
f.dataType == BinaryType && !f.nullable) &&
-
fieldsByName.get(key(HoodieSchema.Variant.VARIANT_VALUE_FIELD)).exists(f =>
f.dataType == BinaryType && !f.nullable)
+ val expectedFields = expectedVariantStructType.fields
+ val ok = structType.length == expectedFields.length &&
+ structType.fields.zip(expectedFields).forall { case (actual,
expected) =>
+ nameEquals(actual.name, expected.name, caseSensitive) &&
+ actual.dataType == expected.dataType &&
+ actual.nullable == expected.nullable
+ }
if (!ok) {
throw new IllegalArgumentException(
s"""Invalid variant schema structure. Expected schema:
```
</details>
<details>
<summary>🤖 Prompt for AI Agents</summary>
```
Verify each finding against the current code and only fix it if needed.
In
`@hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/avro/HoodieSparkSchemaConverters.scala`
around lines 434 - 441, The validator validateVariantStructure currently
accepts
any two-field binary struct by name lookup, which allows reordered physical
structs like struct<value: binary, metadata: binary>; update
validateVariantStructure to enforce the canonical positional layout by
checking
structType.fields length == 2 and then validating that the first field (apply
case-sensitive keying using SQLConf.get.caseSensitiveAnalysis and the key
function) has name equal to HoodieSchema.Variant.VARIANT_METADATA_FIELD and
is
BinaryType non-nullable, and the second field has name equal to
HoodieSchema.Variant.VARIANT_VALUE_FIELD and is BinaryType non-nullable (do
not
rely on a fieldsByName map for this check).
```
</details>
<!--
fingerprinting:phantom:medusa:grasshopper:35812c3a-9e4a-4e1e-bd5e-a4a34016e80a
-->
<!-- 4e71b3a2 -->
<!-- This is an auto-generated comment by CodeRabbit -->
— *CodeRabbit*
([original](https://github.com/hudi-agent/hudi/pull/15#discussion_r3143071733))
(source:comment#3143071733)
--
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]