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 fa57e8defa [GLUTEN-13711][VL] Keep build rows an anti join's filter
can match through a null-tolerant expression (#13712)
fa57e8defa is described below
commit fa57e8defa853d431e69bc97d84243107144be2c
Author: YangJie <[email protected]>
AuthorDate: Tue Nov 24 21:37:33 2026 -0500
[GLUTEN-13711][VL] Keep build rows an anti join's filter can match through
a null-tolerant expression (#13712)
## What changes are proposed in this pull request?
For a broadcast anti join with a filter, the native hash table build drops
the build rows that are null in any build column the filter references, if it
is told the filter is null-intolerant. That was decided by
`SparkShims.isNullIntolerant(condition)`, which only looks at the root, so
`probe.x > coalesce(build.y, 0)` dropped the rows with a null `y` although they
match, and the anti join returned probe rows it should have removed.
The flag is now true only when a null in each of those build columns makes
the whole condition null, following null-intolerant expressions down to the
column, the way Spark infers `IsNotNull` constraints. Both places that compute
it, the executor-side and the driver-side hash table build, use the new check.
Fixes #13711.
## How was this patch tested?
Added to `VeloxHashJoinSuite`: anti joins with `coalesce` and `nvl` in the
filter, with the driver-side build on and off, check the results. They fail
without the fix. A unit test checks the new rule on conditions with
null-intolerant and null-tolerant paths to the build columns.
## Was this patch authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude Opus.
---
.../backendsapi/velox/VeloxSparkPlanExecApi.scala | 3 +-
.../gluten/execution/HashJoinExecTransformer.scala | 24 ++++++++-
.../gluten/execution/VeloxHashJoinSuite.scala | 62 +++++++++++++++++++++-
3 files changed, 85 insertions(+), 4 deletions(-)
diff --git
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
index f545413017..adcfc1b5b3 100644
---
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
+++
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
@@ -1049,7 +1049,8 @@ class VeloxSparkPlanExecApi extends SparkPlanExecApi with
Logging {
case a: Attribute if buildAttrs.contains(a) =>
ConverterUtils.genColumnNameWithExprId(a)
}.toArray
- val propagatesNulls =
SparkShimLoader.getSparkShims.isNullIntolerant(cond)
+ val propagatesNulls =
+ BroadcastHashJoinFilter.isNullOnNullBuildColumn(cond,
buildAttrs)
(cols, propagatesNulls, true)
case None =>
(Array.empty[String], false, false)
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 705f938a20..581fe0d5b8 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
@@ -159,8 +159,7 @@ case class BroadcastHashJoinExecTransformer(
case a: Attribute if buildOutputSet.contains(a) =>
ConverterUtils.genColumnNameWithExprId(a)
}.toArray
- val propagates = SparkShimLoader.getSparkShims.isNullIntolerant(expr)
- (cols, propagates)
+ (cols, BroadcastHashJoinFilter.isNullOnNullBuildColumn(expr,
buildOutputSet))
case None =>
(Array.empty[String], false)
}
@@ -251,6 +250,27 @@ case class BroadcastHashJoinExecTransformer(
}
}
+object BroadcastHashJoinFilter {
+
+ /**
+ * Whether `condition` is null when any build-side column it references is
null. The native hash
+ * table build of an anti join then leaves out the build rows that are null
in these columns,
+ * since they can't match.
+ */
+ def isNullOnNullBuildColumn(condition: Expression, buildOutput:
AttributeSet): Boolean = {
+ val buildColumns = condition.references.filter(buildOutput.contains)
+ buildColumns.nonEmpty &&
buildColumns.subsetOf(nullPropagatingColumns(condition))
+ }
+
+ // The columns whose null makes `expr` null.
+ private def nullPropagatingColumns(expr: Expression): AttributeSet = expr
match {
+ case a: Attribute => AttributeSet(a)
+ case e if SparkShimLoader.getSparkShims.isNullIntolerant(e) =>
+ AttributeSet(e.children.flatMap(nullPropagatingColumns(_)))
+ case _ => AttributeSet.empty
+ }
+}
+
case class BroadcastHashJoinContext(
buildSideJoinKeys: Seq[Expression],
substraitJoinType: JoinRel.JoinType,
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 8f837cc506..fe3d50b980 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
@@ -21,13 +21,14 @@ import org.apache.gluten.sql.shims.SparkShimLoader
import org.apache.spark.{SparkConf, SparkException}
import org.apache.spark.sql.Row
-import org.apache.spark.sql.catalyst.expressions.AttributeReference
+import org.apache.spark.sql.catalyst.expressions.{Add, AttributeReference,
AttributeSet, Coalesce, Expression, GreaterThan, IsNull, Literal, Or}
import org.apache.spark.sql.execution.{ColumnarBroadcastExchangeExec,
ColumnarBuildSideRelation, ColumnarSubqueryBroadcastExec,
InputIteratorTransformer, SerializedHashTableBroadcastRelation}
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
import org.apache.spark.sql.execution.joins.BuildSideRelation
import org.apache.spark.sql.execution.joins.HashedRelationBroadcastMode
import org.apache.spark.sql.execution.unsafe.UnsafeColumnarBuildSideRelation
import org.apache.spark.sql.functions.broadcast
+import org.apache.spark.sql.types.IntegerType
class VeloxHashJoinSuite extends VeloxWholeStageTransformerSuite {
override protected val resourcePath: String = "/tpch-data-parquet"
@@ -302,6 +303,65 @@ class VeloxHashJoinSuite extends
VeloxWholeStageTransformerSuite {
}
}
+ test("anti join keeps build rows its filter can match through a
null-tolerant expression") {
+ withTempView("probe", "build") {
+ withTempPath {
+ dir =>
+ Seq((1, 5), (2, 1)).toDF("k", "x").write.parquet(s"$dir/probe")
+ Seq[(Int, Option[Int])]((1, None), (2, Some(3)))
+ .toDF("k", "y")
+ .write
+ .parquet(s"$dir/build")
+ spark.read.parquet(s"$dir/probe").createOrReplaceTempView("probe")
+ spark.read.parquet(s"$dir/build").createOrReplaceTempView("build")
+ // (1, 5) matches (1, null): coalesce(null, 0) = 0 < 5.
+ for {
+ driverSideBuild <- Seq(false, true)
+ query <- Seq(
+ "SELECT * FROM probe WHERE NOT EXISTS (SELECT 1 FROM build " +
+ "WHERE probe.k = build.k AND probe.x > coalesce(build.y, 0))",
+ "SELECT * FROM probe LEFT ANTI JOIN build " +
+ "ON probe.k = build.k AND probe.x > nvl(build.y, 0)"
+ )
+ } {
+ // The driver-side build computes the filter flags on its own, so
check both builds.
+ withSQLConf(
+ "spark.sql.adaptive.enabled" -> (!driverSideBuild).toString,
+ VeloxConfig.VELOX_DRIVER_SIDE_BROADCAST_HASH_TABLE_BUILD.key ->
+ driverSideBuild.toString) {
+ val df = spark.sql(query)
+ checkAnswer(df, Seq(Row(2, 1)))
+ assert(
+ collect(df.queryExecution.executedPlan) {
+ case j: BroadcastHashJoinExecTransformer => j
+ }.nonEmpty,
+ query)
+ assert(
+ collectBroadcastRelations(df).exists(
+ _.isInstanceOf[SerializedHashTableBroadcastRelation]) ==
driverSideBuild,
+ query)
+ }
+ }
+ }
+ }
+ }
+
+ test("anti join filter null propagation is decided per build column") {
+ val x = AttributeReference("x", IntegerType)()
+ val y = AttributeReference("y", IntegerType)()
+ val z = AttributeReference("z", IntegerType)()
+ val build = AttributeSet(Seq(y, z))
+ def check(condition: Expression): Boolean =
+ BroadcastHashJoinFilter.isNullOnNullBuildColumn(condition, build)
+ assert(check(GreaterThan(x, y)))
+ assert(check(GreaterThan(Add(x, y), z)))
+ assert(check(GreaterThan(y, Literal(0))))
+ assert(!check(GreaterThan(x, Coalesce(Seq(y, Literal(0))))))
+ assert(!check(GreaterThan(Add(x, y), Coalesce(Seq(z, Literal(0))))))
+ assert(!check(Or(GreaterThan(x, y), IsNull(y))))
+ assert(!check(GreaterThan(x, Literal(0))))
+ }
+
test("Broadcast join preserves original cast expression in join keys") {
withSQLConf(
("spark.sql.autoBroadcastJoinThreshold", "10MB"),
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]