suhuruli commented on code in PR #60463:
URL: https://github.com/apache/spark/pull/60463#discussion_r4589882210


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -682,7 +682,9 @@ object DatasetManager extends Logging {
       caseSensitive: Boolean): Unit = {
     val currentSchema = v2ColumnsToStructType(existingTable.columns())
     val targetSchema = if (mergeWithExistingSchema) {
-      SchemaMergingUtils.mergeSchemas(currentSchema, desiredSchema, 
caseSensitive)
+      // Column metadata (e.g. comments) of columns present in both schemas 
comes from the desired
+      // schema, so that metadata changes are diffed and applied below rather 
than dropped.
+      SchemaMergingUtils.mergeSchemasWithDataMetadata(currentSchema, 
desiredSchema, caseSensitive)

Review Comment:
   Agreed, and thanks for tracing the AutoCDC path. The auxiliary table's 
schema is derived from the target's inferred schema, so it picked up source 
comment drift that the declared target never sees, and under 
`array<array<struct>>` that drift turned into a rejected `UpdateColumnComment`. 
I made the merge a caller choice: `evolveTable` now takes 
`mergeWithDesiredMetadata`, `materializeTable` passes `true` for incrementally 
updated streaming tables, and `materializeAuxiliaryTable` passes `false` so the 
auxiliary table keeps its persisted metadata on shared columns through the 
plain `mergeSchemas`, still folding names under the target's effective case 
sensitivity. The new `AutoCdcAuxiliaryTableMaterializationSuite` covers both 
cases you listed: an SCD type 2 target that declares its full schema and whose 
change feed has `arr ARRAY<ARRAY<STRUCT<a: INT COMMENT 'c1'>>>` is materialized 
again after the source comment becomes `'c2'`, the run succeeds and no 
`alterTable` is issued for the a
 uxiliary table, and a second test checks that a top-level comment change in 
the derived auxiliary schema issues no `alterTable` either. Both tests fail on 
the previous revision, the first with 
`PIPELINE_NESTED_COMPLEX_TYPE_SCHEMA_EVOLUTION_UNSUPPORTED` and the second with 
a metadata-only alter of the auxiliary table.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -682,7 +682,9 @@ object DatasetManager extends Logging {
       caseSensitive: Boolean): Unit = {
     val currentSchema = v2ColumnsToStructType(existingTable.columns())
     val targetSchema = if (mergeWithExistingSchema) {
-      SchemaMergingUtils.mergeSchemas(currentSchema, desiredSchema, 
caseSensitive)
+      // Column metadata (e.g. comments) of columns present in both schemas 
comes from the desired
+      // schema, so that metadata changes are diffed and applied below rather 
than dropped.

Review Comment:
   You are right that the comment overclaimed: `diffSchemas` only emits type, 
nullability and comment changes, and `warnOnFieldMetadataDrift` just logs 
everything else. I took your wording. The comment now says that shared columns 
take their metadata from the desired schema so comment changes are diffed and 
applied, and that other metadata changes such as defaults (SPARK-59269) are 
only logged.



##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/util/SchemaMergingUtilsSuite.scala:
##########
@@ -54,4 +53,59 @@ class SchemaMergingUtilsSuite extends SparkFunSuite {
     assert(
       mergedDefaults("score").getCurrentDefaultValue() === Some("0"))
   }
+
+  test("SPARK-59268: mergeSchemasWithDataMetadata takes data-side metadata on 
shared fields") {
+    val currentSchema = new StructType()
+      .add("id", IntegerType, nullable = false, "id comment")
+      .add("changed", StringType, nullable = true, "old comment")
+      .add("removed", StringType, nullable = true, "removed comment")
+      .add("added", StringType)
+      .add(StructField("score", IntegerType).withCurrentDefaultValue("0"))
+      .add("onlyInCurrent", StringType, nullable = true, "kept comment")
+    val desiredSchema = new StructType()
+      .add("id", IntegerType, nullable = false, "id comment")
+      .add("changed", StringType, nullable = true, "new comment")
+      .add("removed", StringType)
+      .add("added", StringType, nullable = true, "added comment")
+      .add(StructField("score", IntegerType).withCurrentDefaultValue("42"))
+      .add("onlyInDesired", StringType, nullable = true, "new column comment")
+
+    val merged = SchemaMergingUtils.mergeSchemasWithDataMetadata(
+      currentSchema, desiredSchema, caseSensitive = true)
+    assert(merged.fieldNames.toSeq ===
+      Seq("id", "changed", "removed", "added", "score", "onlyInCurrent", 
"onlyInDesired"))
+    assert(merged("id").getComment() === Some("id comment"))
+    assert(merged("changed").getComment() === Some("new comment"))
+    assert(merged("removed").getComment() === None)
+    assert(merged("added").getComment() === Some("added comment"))
+    assert(merged("score").getCurrentDefaultValue() === Some("42"))
+    assert(merged("onlyInCurrent").getComment() === Some("kept comment"))
+    assert(merged("onlyInDesired").getComment() === Some("new column comment"))
+  }
+
+  test("SPARK-59268: mergeSchemasWithDataMetadata takes data-side metadata on 
nested fields") {
+    def schema(comment: String): StructType = {
+      val inner = new StructType().add("a", IntegerType, nullable = true, 
comment)
+      new StructType()
+        .add("s", inner)
+        .add("arr", ArrayType(inner))
+        .add("m", MapType(StringType, inner))

Review Comment:
   Good catch, both regressions you describe would have passed the old 
assertions. The nested test in `SchemaMergingUtilsSuite` now uses a struct map 
key, `MapType(inner, inner)`, whose commented field changes comment on both the 
key and the value side, so the key recursion in `overlayFieldMetadata` is 
observed. The case-insensitive test now also has a nested field whose declared 
name differs only in case from the table's (`s.a` on the table, `s.A` declared) 
with a changed comment, and asserts that the merged schema keeps the table's 
`s.a` with the declared comment.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to