n-young commented on code in PR #60074:
URL: https://github.com/apache/spark/pull/60074#discussion_r4484870032


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -213,13 +214,15 @@ object LogicalRDD extends Logging {
     )(originDataset.sparkSession, stats, constraints, fromCheckpoint)
   }
 
+  // Associate outputs with matching names and compatible nested nullability 
for metadata rewriting.
   private[sql] def buildOutputAssocForRewrite(
       source: Seq[Attribute],
       destination: Seq[Attribute]): Option[Map[Attribute, Attribute]] = {
-    // We check the name and type, allowing nullability, exprId, metadata, 
qualifier be different
-    // E.g. This could happen during optimization phase.
+    // Optimization can change exprId, metadata, and qualifier. Recursive 
references can also
+    // widen nested nullability; narrowing it would not preserve the source's 
type guarantees.
     val rewrite = source.zip(destination).flatMap { case (attr1, attr2) =>
-      if (attr1.name == attr2.name && attr1.dataType == attr2.dataType) {
+      if (attr1.name == attr2.name &&
+          DataType.equalsIgnoreCompatibleNullability(attr1.dataType, 
attr2.dataType)) {

Review Comment:
   Compatible matching now requires an explicit opt-in, enabled only by 
UnionLoopExec when the legacy setting is off. Other LogicalRDD consumers retain 
exact nested type matching. Tests cover ordinary checkpoints and legacy 
recursive iterations with both narrower and wider nested types.



##########
sql/core/src/test/scala/org/apache/spark/sql/CTEInlineSuite.scala:
##########
@@ -32,6 +33,38 @@ abstract class CTEInlineSuiteBase
 
   import testImplicits._
 
+  // More than 100 rows per iteration exercises recursive reference 
substitution through LogicalRDD.
+  test("SPARK-60860: nested nulls in recursive CTEs with an RDD-backed 
anchor") {
+    for {
+      localRelationLimit <- Seq(0, 100, 200)
+      legacy <- Seq(false, true)
+    } {
+      withSQLConf(
+        SQLConf.LEGACY_RECURSIVE_CTE_USE_ANCHOR_NESTED_NULLABILITY.key -> 
legacy.toString,
+        
SQLConf.CTE_RECURSION_ANCHOR_ROWS_LIMIT_TO_CONVERT_TO_LOCAL_RELATION.key ->
+          localRelationLimit.toString) {
+        val appender = new LogAppender("recursive nested nullability")
+        withLogAppender(appender, loggerNames = 
Seq(classOf[LogicalRDD].getName)) {
+          val result = sql(
+            """WITH RECURSIVE r(id, n, a, saw_null) AS (
+              |  SELECT id, 0, array(1), false FROM range(101)
+              |  UNION ALL
+              |  SELECT id, n + 1, array(CAST(NULL AS INT)), exists(a, x -> x 
IS NULL)
+              |  FROM r WHERE n < 2
+              |)
+              |SELECT n, saw_null, count(*) FROM r GROUP BY n, saw_null
+              |""".stripMargin)
+          checkAnswer(result, Seq(Row(0, false, 101L), Row(1, false, 101L), 
Row(2, !legacy, 101L)))
+        }
+        // The legacy schema can still require narrowing nested nullability in 
later iterations.
+        if (!legacy) {

Review Comment:
   The test now distinguishes local and RDD-backed iterations and asserts that 
legacy runs log the mismatch at thresholds 0/100, while threshold 200 logs 
none. Disabling compatible matching makes the default-setting RDD regression 
fail, providing the requested negative control.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -213,13 +214,15 @@ object LogicalRDD extends Logging {
     )(originDataset.sparkSession, stats, constraints, fromCheckpoint)
   }
 
+  // Associate outputs with matching names and compatible nested nullability 
for metadata rewriting.

Review Comment:
   The association-helper header now says "rewriting statistics and 
constraints"; metadata in the following comment refers to attribute metadata.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -7251,6 +7251,18 @@ object SQLConf {
       .intConf
       .createWithDefault(100)
 
+  val LEGACY_RECURSIVE_CTE_USE_ANCHOR_NESTED_NULLABILITY =
+    buildConf("spark.sql.legacy.recursiveCTE.useAnchorNestedNullability")
+      .internal()
+      .doc("When true, recursive CTE references inherit the anchor's nested 
nullability " +
+        "and recursive type coercion uses the legacy rules. This can return 
incorrect results " +
+        "or fail when later iterations introduce null array elements, map 
values, or struct " +
+        "fields. When false, recursive references allow nested nulls.")
+      .version("4.4.0")
+      .withBindingPolicy(ConfigBindingPolicy.SESSION)

Review Comment:
   Added a persisted-view regression that stores the old schema and removes the 
saved legacy key. Default reads fail and setting the reading session's legacy 
key restores the stored schema, in both COMPENSATION and BINDING modes. 
Changing the config binding to PERSISTED makes this test fail.



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