This is an automated email from the ASF dual-hosted git repository.

LuciferYang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new ef679ab348 [GLUTEN-13828][VL] Fix wrong results of a semi or anti join 
that reuses a broadcast with expression build keys (#13839)
ef679ab348 is described below

commit ef679ab3488dc20af3d3c30148e5801d65d31b45
Author: YangJie <[email protected]>
AuthorDate: Tue Dec 1 06:50:55 2026 -0500

    [GLUTEN-13828][VL] Fix wrong results of a semi or anti join that reuses a 
broadcast with expression build keys (#13839)
    
    ## What changes are proposed in this pull request?
    
    When the build keys of a broadcast hash join are expressions, such as 
`cast(build.k as bigint)` for an int and a bigint key, the broadcast exchange 
appends them to the build side, and the native hash table is built with the 
broadcast relation's column names. A join that reuses the exchange has other 
expression IDs, so the build columns its filter reads matched none of them, 
`nativeHashTableBuild` skipped them, and the placeholder filter read no build 
column. Since the Velox bump in #1 [...]
    
    The filter's build columns are now matched by position and passed with the 
relation's names, as `hashTableBuildKey` already matches them. A filter column 
missing from the join's build side now throws instead of being skipped.
    
    Fixes #13828.
    
    ## How was this patch tested?
    
    The test from #13710 in `VeloxHashJoinSuite` now also runs with a bigint 
probe key, so the reused broadcast has an expression build key. That run fails 
without the fix.
    
    ## Was this patch authored or co-authored using generative AI tooling?
    
    Yes. Generated-by: Claude Opus.
---
 .../gluten/execution/HashJoinExecTransformer.scala |  24 ++++-
 .../sql/execution/ColumnarBuildSideRelation.scala  |   4 +-
 .../unsafe/UnsafeColumnarBuildSideRelation.scala   |   4 +-
 .../gluten/execution/VeloxHashJoinSuite.scala      | 112 +++++++++++----------
 4 files changed, 87 insertions(+), 57 deletions(-)

diff --git 
a/backends-velox/src/main/scala/org/apache/gluten/execution/HashJoinExecTransformer.scala
 
b/backends-velox/src/main/scala/org/apache/gluten/execution/HashJoinExecTransformer.scala
index acb6a9c34b..6ab00bc6c0 100644
--- 
a/backends-velox/src/main/scala/org/apache/gluten/execution/HashJoinExecTransformer.scala
+++ 
b/backends-velox/src/main/scala/org/apache/gluten/execution/HashJoinExecTransformer.scala
@@ -314,13 +314,33 @@ case class BroadcastHashJoinContext(
         substraitJoinType == JoinRel.JoinType.JOIN_TYPE_LEFT_ANTI &&
         hasMixedFiltCondition && filterPropagatesNulls
       ) {
-        val buildColumns = 
buildSideStructure.map(ConverterUtils.genColumnNameWithExprId)
-        filterBuildColumns.map(buildColumns.indexOf(_)).sorted.distinct.toList
+        filterBuildColumnPositions.sorted.distinct.toList
       } else {
         Nil
       }
     HashTableBuildKey(droppedDuplicates, nullFilteredColumns)
   }
+
+  /**
+   * The build columns the filter reads, named as in `buildOutput`, the output 
of the broadcast
+   * relation that the native hash table is built from. A join that reuses the 
broadcast exchange
+   * names them with other expression IDs, so they are matched by position.
+   */
+  def filterBuildColumnsIn(buildOutput: Seq[Attribute]): Array[String] =
+    filterBuildColumnPositions.map(i => 
ConverterUtils.genColumnNameWithExprId(buildOutput(i)))
+
+  private def filterBuildColumnPositions: Array[Int] = {
+    val buildColumns = 
buildSideStructure.map(ConverterUtils.genColumnNameWithExprId)
+    filterBuildColumns.map {
+      column =>
+        val position = buildColumns.indexOf(column)
+        if (position < 0) {
+          throw new IllegalStateException(
+            s"Join filter column $column is not in the build side: 
${buildColumns.mkString(", ")}")
+        }
+        position
+    }
+  }
 }
 
 /** See [[BroadcastHashJoinContext.hashTableBuildKey]]. */
diff --git 
a/backends-velox/src/main/scala/org/apache/spark/sql/execution/ColumnarBuildSideRelation.scala
 
b/backends-velox/src/main/scala/org/apache/spark/sql/execution/ColumnarBuildSideRelation.scala
index 28bf523e4c..6a158839d0 100644
--- 
a/backends-velox/src/main/scala/org/apache/spark/sql/execution/ColumnarBuildSideRelation.scala
+++ 
b/backends-velox/src/main/scala/org/apache/spark/sql/execution/ColumnarBuildSideRelation.scala
@@ -232,7 +232,7 @@ case class ColumnarBuildSideRelation(
               broadcastContext.buildHashTableId,
               batchArray.toArray,
               joinKeys,
-              broadcastContext.filterBuildColumns,
+              broadcastContext.filterBuildColumnsIn(newOutput.asScala.toSeq),
               broadcastContext.filterPropagatesNulls,
               broadcastContext.substraitJoinType.ordinal(),
               broadcastContext.hasMixedFiltCondition,
@@ -327,7 +327,7 @@ case class ColumnarBuildSideRelation(
               broadcastContext.buildHashTableId,
               batchArray.toArray,
               joinKeys,
-              broadcastContext.filterBuildColumns,
+              broadcastContext.filterBuildColumnsIn(newOutput.asScala.toSeq),
               broadcastContext.filterPropagatesNulls,
               broadcastContext.substraitJoinType.ordinal(),
               broadcastContext.hasMixedFiltCondition,
diff --git 
a/backends-velox/src/main/scala/org/apache/spark/sql/execution/unsafe/UnsafeColumnarBuildSideRelation.scala
 
b/backends-velox/src/main/scala/org/apache/spark/sql/execution/unsafe/UnsafeColumnarBuildSideRelation.scala
index 7d4fa3c4b7..60a4bab13c 100644
--- 
a/backends-velox/src/main/scala/org/apache/spark/sql/execution/unsafe/UnsafeColumnarBuildSideRelation.scala
+++ 
b/backends-velox/src/main/scala/org/apache/spark/sql/execution/unsafe/UnsafeColumnarBuildSideRelation.scala
@@ -193,7 +193,7 @@ class UnsafeColumnarBuildSideRelation(
               broadcastContext.buildHashTableId,
               batchArray.toArray,
               joinKeys,
-              broadcastContext.filterBuildColumns,
+              broadcastContext.filterBuildColumnsIn(newOutput.asScala.toSeq),
               broadcastContext.filterPropagatesNulls,
               broadcastContext.substraitJoinType.ordinal(),
               broadcastContext.hasMixedFiltCondition,
@@ -288,7 +288,7 @@ class UnsafeColumnarBuildSideRelation(
               broadcastContext.buildHashTableId,
               batchArray.toArray,
               joinKeys,
-              broadcastContext.filterBuildColumns,
+              broadcastContext.filterBuildColumnsIn(newOutput.asScala.toSeq),
               broadcastContext.filterPropagatesNulls,
               broadcastContext.substraitJoinType.ordinal(),
               broadcastContext.hasMixedFiltCondition,
diff --git 
a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxHashJoinSuite.scala
 
b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxHashJoinSuite.scala
index b32dae4b56..99e2d3ad21 100644
--- 
a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxHashJoinSuite.scala
+++ 
b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxHashJoinSuite.scala
@@ -827,58 +827,68 @@ class VeloxHashJoinSuite extends 
VeloxWholeStageTransformerSuite {
   }
 
   test("joins that reuse a broadcast but filter nulls differently get their 
own hash table") {
-    withTempView("probe", "build") {
-      withTempPath {
-        dir =>
-          Seq((1, 5), (2, 1), (3, 7), (4, 2), (5, 9), (6, 0), (7, 4))
-            .toDF("k", "x")
-            .write
-            .parquet(s"$dir/probe")
-          Seq[(Int, Option[Int])](
-            (1, Some(3)),
-            (1, None),
-            (2, None),
-            (3, Some(8)),
-            (4, Some(1)),
-            (5, None),
-            (8, Some(2)))
-            .toDF("k", "y")
-            .write
-            .parquet(s"$dir/build")
-          spark.read.parquet(s"$dir/probe").createOrReplaceTempView("probe")
-          spark.read.parquet(s"$dir/build").createOrReplaceTempView("build")
-          // This anti join's filter is null-intolerant, so its hash table 
leaves out the build
-          // rows whose y is null. The other join's filter is true for them, 
so it needs them.
-          val nullDropping =
-            "SELECT 'a' t, k, x FROM probe WHERE NOT EXISTS " +
-              "(SELECT 1 FROM build WHERE probe.k = build.k AND probe.x > 
build.y)"
-          val nullDroppingRows = Seq((2, 1), (3, 7), (5, 9), (6, 0), (7, 4))
-          Seq(
-            "NOT EXISTS" -> Seq((3, 7), (6, 0), (7, 4)),
-            "EXISTS" -> Seq((1, 5), (2, 1), (4, 2), (5, 9))
-          ).foreach {
-            case (predicate, rows) =>
-              val nullKeeping =
-                s"SELECT 'b' t, k, x FROM probe WHERE $predicate (SELECT 1 
FROM build " +
-                  "WHERE probe.k = build.k AND (probe.x > build.y OR build.y 
IS NULL))"
-              val expected = nullDroppingRows.map { case (k, x) => Row("a", k, 
x) } ++
-                rows.map { case (k, x) => Row("b", k, x) }
-              // Which join builds the shared hash table first depends on the 
order they run.
-              Seq(s"$nullDropping UNION ALL $nullKeeping", s"$nullKeeping 
UNION ALL $nullDropping")
-                .foreach {
-                  query =>
-                    val df = spark.sql(query)
-                    checkAnswer(df, expected)
-                    val plan = df.queryExecution.executedPlan
-                    assert(
-                      collect(plan) { case j: BroadcastHashJoinExecTransformer 
=> j }.size == 2,
-                      plan)
-                    assert(
-                      collectWithSubqueries(plan) { case r: ReusedExchangeExec 
=> r }.nonEmpty,
-                      s"Expected the joins to share one broadcast: $query")
-                }
+    // With a bigint probe key, the build key is cast(build.k as bigint), 
which the broadcast
+    // exchange adds to the build side as a column. The hash table is then 
built with the columns
+    // named as in the broadcast relation, not as in the join that reuses the 
exchange.
+    Seq("int", "bigint").foreach {
+      probeKeyType =>
+        val probeKey: Int => Any = k => if (probeKeyType == "int") k else 
k.toLong
+        withTempView("probe", "build") {
+          withTempPath {
+            dir =>
+              Seq((1, 5), (2, 1), (3, 7), (4, 2), (5, 9), (6, 0), (7, 4))
+                .toDF("k", "x")
+                .selectExpr(s"cast(k as $probeKeyType) k", "x")
+                .write
+                .parquet(s"$dir/probe")
+              Seq[(Int, Option[Int])](
+                (1, Some(3)),
+                (1, None),
+                (2, None),
+                (3, Some(8)),
+                (4, Some(1)),
+                (5, None),
+                (8, Some(2)))
+                .toDF("k", "y")
+                .write
+                .parquet(s"$dir/build")
+              
spark.read.parquet(s"$dir/probe").createOrReplaceTempView("probe")
+              
spark.read.parquet(s"$dir/build").createOrReplaceTempView("build")
+              // This anti join's filter is null-intolerant, so its hash table 
leaves out the build
+              // rows whose y is null. The other join's filter is true for 
them, so it needs them.
+              val nullDropping =
+                "SELECT 'a' t, k, x FROM probe WHERE NOT EXISTS " +
+                  "(SELECT 1 FROM build WHERE probe.k = build.k AND probe.x > 
build.y)"
+              val nullDroppingRows = Seq((2, 1), (3, 7), (5, 9), (6, 0), (7, 
4))
+              Seq(
+                "NOT EXISTS" -> Seq((3, 7), (6, 0), (7, 4)),
+                "EXISTS" -> Seq((1, 5), (2, 1), (4, 2), (5, 9))
+              ).foreach {
+                case (predicate, rows) =>
+                  val nullKeeping =
+                    s"SELECT 'b' t, k, x FROM probe WHERE $predicate (SELECT 1 
FROM build " +
+                      "WHERE probe.k = build.k AND (probe.x > build.y OR 
build.y IS NULL))"
+                  val expected = nullDroppingRows.map { case (k, x) => 
Row("a", probeKey(k), x) } ++
+                    rows.map { case (k, x) => Row("b", probeKey(k), x) }
+                  // Which join builds the shared hash table first depends on 
the order they run.
+                  Seq(
+                    s"$nullDropping UNION ALL $nullKeeping",
+                    s"$nullKeeping UNION ALL $nullDropping")
+                    .foreach {
+                      query =>
+                        val df = spark.sql(query)
+                        checkAnswer(df, expected)
+                        val plan = df.queryExecution.executedPlan
+                        assert(
+                          collect(plan) { case j: 
BroadcastHashJoinExecTransformer => j }.size == 2,
+                          plan)
+                        assert(
+                          collectWithSubqueries(plan) { case r: 
ReusedExchangeExec => r }.nonEmpty,
+                          s"Expected the joins to share one broadcast: $query")
+                    }
+              }
           }
-      }
+        }
     }
   }
 


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

Reply via email to