cloud-fan commented on code in PR #58077:
URL: https://github.com/apache/spark/pull/58077#discussion_r3849443918


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/subquery.scala:
##########
@@ -165,14 +186,173 @@ case class InSubqueryExec(
     }
   }
 
+  // Invariant schema/ordering data for the multi-column evaluator, computed 
once after the result
+  // is available. @transient so that serialization (result=null) does not 
trigger evaluation.
+  @transient private lazy val multiColFieldTypes: Array[DataType] =
+    plan.output.map(_.dataType).toArray
+  @transient private lazy val multiColFieldOrderings: Array[Ordering[Any]] =
+    multiColFieldTypes.map(TypeUtils.getInterpretedOrdering)
+  // Struct-level ordering used to index fully non-null result rows in a 
TreeSet.
+  @transient private lazy val multiColRowOrdering: Ordering[InternalRow] =
+    
TypeUtils.getInterpretedOrdering(child.dataType).asInstanceOf[Ordering[InternalRow]]
+
+  // Split collected rows into a sorted set of fully non-null rows (O(log n) 
membership test)
+  // and an array of rows that contain at least one null field (must be 
scanned linearly).
+  // Built once; the TreeSet uses the struct-level Catalyst ordering. See 
SPARK-58481.
+  @transient private lazy val (multiColNonNullSet, multiColNullRows) = {
+    val withNull = Array.newBuilder[InternalRow]
+    val nonNull = TreeSet.newBuilder[InternalRow](multiColRowOrdering)
+    result.foreach { r =>
+      val row = r.asInstanceOf[InternalRow]
+      if (row.anyNull) withNull += row else nonNull += row
+    }
+    (nonNull.result(), withNull.result())
+  }
+
+  // Three-valued IN semantics for multi-column subqueries.
+  // Result rows are InternalRow objects; InSet's TreeSet uses Catalyst 
ordering, but membership
+  // cannot distinguish a definitively-false candidate from an indeterminate 
one.
+  //
+  // When the LHS struct has no null fields:
+  //   Fast path: O(log n) TreeSet lookup against fully non-null result rows 
for TRUE.
+  //   Slow path: linear scan over null-containing result rows only for 
potential UNKNOWN.
+  //
+  // When the LHS struct has at least one null field, the fast path cannot be 
used (a null LHS
+  // field produces UNKNOWN against any non-null RHS row whose non-null fields 
all match). In
+  // that case we scan all result rows linearly.

Review Comment:
   **Nit:**
   
   Please describe this as a linear scan that stops once UNKNOWN is 
established. Both loops short-circuit on `!hasUnknown`, so 'all result rows' is 
also inaccurate in the repeated comment at `subquery.scala:265`.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/subquery.scala:
##########
@@ -125,7 +129,24 @@ case class InSubqueryExec(
 
   @transient private lazy val inSet = InSet(child, result.toSet)
 
-  override def nullable: Boolean = child.nullable
+  // Mirror the logical InSubquery.nullable: nullable when any output column 
is nullable
+  // (null in any column position produces UNKNOWN on a miss) or when any LHS 
field is nullable.

Review Comment:
   **Nit:**
   
   Describe this as a possible outcome rather than an unconditional one. 
`(1,1)` versus `(99,NULL)` is definitively FALSE because the first field 
differs, even though another comparison is UNKNOWN.
   ```suggestion
     // (a nullable RHS field can produce UNKNOWN on a miss) or when any LHS 
field is nullable.
   ```



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/subquery.scala:
##########
@@ -165,14 +186,173 @@ case class InSubqueryExec(
     }
   }
 
+  // Invariant schema/ordering data for the multi-column evaluator, computed 
once after the result
+  // is available. @transient so that serialization (result=null) does not 
trigger evaluation.
+  @transient private lazy val multiColFieldTypes: Array[DataType] =
+    plan.output.map(_.dataType).toArray
+  @transient private lazy val multiColFieldOrderings: Array[Ordering[Any]] =
+    multiColFieldTypes.map(TypeUtils.getInterpretedOrdering)
+  // Struct-level ordering used to index fully non-null result rows in a 
TreeSet.
+  @transient private lazy val multiColRowOrdering: Ordering[InternalRow] =
+    
TypeUtils.getInterpretedOrdering(child.dataType).asInstanceOf[Ordering[InternalRow]]
+
+  // Split collected rows into a sorted set of fully non-null rows (O(log n) 
membership test)
+  // and an array of rows that contain at least one null field (must be 
scanned linearly).
+  // Built once; the TreeSet uses the struct-level Catalyst ordering. See 
SPARK-58481.
+  @transient private lazy val (multiColNonNullSet, multiColNullRows) = {
+    val withNull = Array.newBuilder[InternalRow]
+    val nonNull = TreeSet.newBuilder[InternalRow](multiColRowOrdering)
+    result.foreach { r =>
+      val row = r.asInstanceOf[InternalRow]
+      if (row.anyNull) withNull += row else nonNull += row
+    }
+    (nonNull.result(), withNull.result())
+  }
+
+  // Three-valued IN semantics for multi-column subqueries.
+  // Result rows are InternalRow objects; InSet's TreeSet uses Catalyst 
ordering, but membership
+  // cannot distinguish a definitively-false candidate from an indeterminate 
one.
+  //
+  // When the LHS struct has no null fields:
+  //   Fast path: O(log n) TreeSet lookup against fully non-null result rows 
for TRUE.
+  //   Slow path: linear scan over null-containing result rows only for 
potential UNKNOWN.
+  //
+  // When the LHS struct has at least one null field, the fast path cannot be 
used (a null LHS
+  // field produces UNKNOWN against any non-null RHS row whose non-null fields 
all match). In
+  // that case we scan all result rows linearly.
+  //
+  // Per-candidate three-valued logic: TRUE if every field matches; UNKNOWN if 
no field is
+  // definitively unequal but at least one comparison involves null; FALSE 
otherwise.
+  private def evalMultiColumn(inputRow: InternalRow): Any = {
+    val value = child.eval(inputRow)
+    if (value == null) return null
+    val inputStruct = value.asInstanceOf[InternalRow]
+    val fieldTypes = multiColFieldTypes
+    val orderings = multiColFieldOrderings
+    val numFields = fieldTypes.length
+
+    if (!inputStruct.anyNull) {
+      // Fast path: indexed lookup among fully non-null candidates.
+      if (multiColNonNullSet.contains(inputStruct)) return true
+      // Materialize LHS fields once before the candidate scans. For types 
like BinaryType,
+      // InternalRow.get copies the payload on every call; caching avoids one 
copy per

Review Comment:
   **Nit:**
   
   Please remove the BinaryType copy-avoidance rationale here and at 
`subquery.scala:267`. The LHS is produced by `CreateNamedStruct` as a 
`GenericInternalRow`, and `BaseGenericInternalRow.get` returns the stored field 
reference rather than copying a binary payload.



##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -2678,4 +2678,164 @@ class SubquerySuite extends SharedSparkSession
 
     assert(exposedAttribute.exprId == outerReferenceAttribute.exprId)
   }
+
+  test("SPARK-58481: InSubqueryExec nullable correctly accounts for subquery 
output nullability") {
+    // 5 NOT IN (99, NULL) is UNKNOWN, not TRUE or FALSE.  A join condition 
that is not TRUE
+    // matches no rows, so a FULL OUTER JOIN must emit null-padded rows for 
every row in each
+    // side -- 3 + 3 = 6 null-padded rows -- not the full cross product (9 
rows).
+    withTable("t0", "t1", "t3") {
+      sql("CREATE TABLE t0(c0 INT) USING PARQUET")
+      sql("INSERT INTO t0 VALUES (1), (2), (3)")
+      sql("CREATE TABLE t1(c0 INT) USING PARQUET")
+      sql("INSERT INTO t1 VALUES (10), (20), (30)")
+      sql("CREATE TABLE t3(c0 INT) USING PARQUET")
+      sql("INSERT INTO t3 VALUES (99), (CAST(NULL AS INT))")
+
+      // t1 rows are null-padded (no match on left), t0 rows are null-padded 
(no match on right).
+      val expected = Seq(
+        Row(null, 10), Row(null, 20), Row(null, 30),  // t0 side: null-padded
+        Row(1, null), Row(2, null), Row(3, null))      // t1 side: null-padded
+      checkAnswer(
+        sql("SELECT t0.c0, t1.c0 FROM t1 FULL OUTER JOIN t0 ON (5 NOT IN 
(SELECT t3.c0 FROM t3))"),
+        expected)
+    }
+  }
+
+  test("SPARK-58481: multi-column IN subquery with nullable non-head output is 
nullable") {
+    // Disable the optimizer's join-condition IN rewrite so the query 
exercises InSubqueryExec.
+    // Covers both per-candidate cases:
+    //   (1,1) vs (99,NULL): first field differs => definitely FALSE (not 
UNKNOWN).
+    //   (1,1) vs (1,NULL):  first fields equal, second null => UNKNOWN.
+    //   (2,2) vs either row: both are FALSE => NOT IN = TRUE.
+    // Expected: (1,1) gets UNKNOWN => null-padded; (2,2) gets TRUE => joined.
+    withSQLConf(
+      
"spark.sql.optimizer.optimizeUncorrelatedInSubqueriesInJoinCondition.enabled" 
-> "false"
+    ) {
+      withTable("lhs", "rhs") {
+        sql("CREATE TABLE lhs(a INT NOT NULL, b INT NOT NULL) USING PARQUET")

Review Comment:
   **Non-blocking:**
   
   Use non-nullable `VALUES`-derived inputs for the LHS and the RHS first 
column, leaving only a later RHS column nullable. File-source analysis applies 
`dataSchema.asNullable`, so both this LHS and the RHS head analyze as nullable 
despite the DDL; the test therefore still passes with the old 
`plan.output.head.nullable` check and does not isolate the behavior named by 
the test.



##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -2678,4 +2678,164 @@ class SubquerySuite extends SharedSparkSession
 
     assert(exposedAttribute.exprId == outerReferenceAttribute.exprId)
   }
+
+  test("SPARK-58481: InSubqueryExec nullable correctly accounts for subquery 
output nullability") {
+    // 5 NOT IN (99, NULL) is UNKNOWN, not TRUE or FALSE.  A join condition 
that is not TRUE
+    // matches no rows, so a FULL OUTER JOIN must emit null-padded rows for 
every row in each
+    // side -- 3 + 3 = 6 null-padded rows -- not the full cross product (9 
rows).
+    withTable("t0", "t1", "t3") {
+      sql("CREATE TABLE t0(c0 INT) USING PARQUET")
+      sql("INSERT INTO t0 VALUES (1), (2), (3)")
+      sql("CREATE TABLE t1(c0 INT) USING PARQUET")
+      sql("INSERT INTO t1 VALUES (10), (20), (30)")
+      sql("CREATE TABLE t3(c0 INT) USING PARQUET")
+      sql("INSERT INTO t3 VALUES (99), (CAST(NULL AS INT))")
+
+      // t1 rows are null-padded (no match on left), t0 rows are null-padded 
(no match on right).

Review Comment:
   **Nit:**
   
   The relation labels are reversed: `Row(null, 10)` retains the unmatched t1 
row and pads t0, while `Row(1, null)` retains t0 and pads t1.
   ```suggestion
         // Unmatched t1 rows null-pad t0; unmatched t0 rows null-pad t1.
   ```



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