andygrove commented on code in PR #4587:
URL: https://github.com/apache/datafusion-comet/pull/4587#discussion_r4107051685
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -3003,6 +3024,8 @@ object CometSortMergeJoinExec extends
CometOperatorSerde[SortMergeJoinExec] {
case FullOuter => JoinType.FullOuter
case LeftSemi => JoinType.LeftSemi
case LeftAnti => JoinType.LeftAnti
+ // Existence SMJ falls back to Spark: DF 55.1.0's BitwiseSortMergeJoin
buffers output
Review Comment:
This says existence SMJs fall back, but with
`spark.comet.exec.forceShuffledHashJoin=true` they don't. `RewriteJoin` turns
them into a `ShuffledHashJoinExec` with `BuildRight`, which now runs natively.
I confirmed that with AQE on and off, and the answers matched on a small table.
`RewriteJoin` skips that rewrite for `LeftSemi` with `BuildRight` because of
the q69 wrong results in #2667, and #2697 is still open. Should `ExistenceJoin`
get the same exclusion for now? If you think it's safe, could you add a test
with that config on?
##########
spark/src/test/resources/sql-tests/join/existence_join.sql:
##########
@@ -0,0 +1,234 @@
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing,
+-- software distributed under the License is distributed on an
+-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+-- KIND, either express or implied. See the License for the
+-- specific language governing permissions and limitations
+-- under the License.
+
+-- Tests for ExistenceJoin: produced when EXISTS / NOT EXISTS is combined
+-- with another predicate via OR, preventing rewrite to LeftSemi / LeftAnti.
+--
+-- Strategy hints are placed INSIDE the EXISTS subquery (on the subquery's own
+-- relation alias), because an outer hint referencing the subquery table cannot
+-- resolve it and silently falls back to a broadcast hash join. Subquery-local
+-- hints (verified on Spark 4.1.3 with AQE off) select ShuffledHashJoin /
+-- SortMergeJoin.
+--
+-- Native existence support is currently hash-only: BROADCAST and SHUFFLE_HASH
+-- cases exercise CometBroadcastHashJoinExec / CometHashJoinExec, while MERGE
+-- cases fall back to Spark's SortMergeJoin (existence SMJ is not yet native)
and
+-- verify result parity under the Comet-enabled config.
+
+-- Native ExistenceJoin support is experimental and disabled by default.
+-- Config: spark.comet.exec.existenceJoin.enabled=true
+
+-- ============================================================
+-- Setup: NULLs (both sides), duplicates, empty build, all-NULL build
+-- ============================================================
+
+statement
+CREATE TABLE ex_left(id int, k int, region string) USING parquet
+
+statement
+INSERT INTO ex_left VALUES
+ (1, 1, 'US'),
+ (2, 2, 'EU'),
+ (3, NULL, 'US'),
+ (4, 4, 'EU'),
+ (5, 5, 'EU'),
+ (6, NULL, 'EU')
+
+statement
+CREATE TABLE ex_right(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right VALUES (10, 1), (11, 2), (12, 2), (13, NULL)
+
+statement
+CREATE TABLE ex_right_no_nulls(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_no_nulls VALUES (10, 1), (11, 5)
+
+statement
+CREATE TABLE ex_right_empty(id int, k int) USING parquet
+
+statement
+CREATE TABLE ex_right_dups(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_dups VALUES (10, 1), (11, 1), (12, 1), (13, 2)
+
+statement
+CREATE TABLE ex_right_all_null(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_all_null VALUES (10, NULL), (11, NULL)
+
+statement
+CREATE TABLE ex_left_empty(id int, k int, region string) USING parquet
+
+-- ============================================================
+-- EXISTS with OR across all three strategies (hint in subquery)
+-- ============================================================
+
+query
+SELECT * FROM ex_left l
+WHERE l.region = 'US'
+ OR EXISTS (SELECT /*+ BROADCAST(r) */ 1 FROM ex_right r WHERE r.k = l.k)
Review Comment:
Every query here reads the marker through `OR`, so rows where the other side
is true pass whatever the marker says. Could we add cases that select it
directly, such as `SELECT l.id, EXISTS (SELECT 1 FROM ex_right r WHERE r.k =
l.k) FROM ex_left l` and the `l.k IN (SELECT r.k ...)` form, plus a string key?
`IN` has no coverage yet even though the config doc lists it. I ran those
shapes on BHJ and SHJ with nulls, duplicates and an empty build side, and they
matched Spark, so this just locks that in.
##########
spark/src/main/scala/org/apache/comet/CometConf.scala:
##########
@@ -235,6 +235,11 @@ object CometConf extends ShimCometConf {
createExecEnabledConfig("broadcastNestedLoopJoin", defaultValue = true)
val COMET_EXEC_SORT_MERGE_JOIN_ENABLED: ConfigEntry[Boolean] =
createExecEnabledConfig("sortMergeJoin", defaultValue = true)
+ val COMET_EXEC_EXISTENCE_JOIN_ENABLED: ConfigEntry[Boolean] =
+ createExecEnabledConfig(
+ "existenceJoin",
+ defaultValue = true,
+ notes = Some("Enables native ExistenceJoin (EXISTS/NOT EXISTS/IN
combined with OR)."))
Review Comment:
This renders as "Whether to enable existenceJoin by default. Enables native
ExistenceJoin (EXISTS/NOT EXISTS/IN combined with OR).." with a double period,
because `createExecEnabledConfig` adds its own. Now that the flag is on by
default, could it be a plain `conf(...)` whose doc says what still falls back
(sort-merge joins, extra join conditions, computed keys, `NOT IN`)? Could the
joins table in `docs/source/user-guide/latest/operators.md` carry the same note?
##########
spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala:
##########
@@ -1572,4 +1572,132 @@ class CometJoinSuite extends CometTestBase {
}
}
}
+
+ test("ExistenceJoin via BroadcastHashJoin (EXISTS combined with OR)") {
+ withSQLConf(
+ CometConf.COMET_EXEC_EXISTENCE_JOIN_ENABLED.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "10MB",
+ SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "10MB") {
+ withParquetTable((0 until 100).map(i => (i, if (i % 3 == 0) "US" else
"EU")), "tbl_a") {
+ withParquetTable((0 until 30).map(i => (i, i + 1)), "tbl_b") {
+ val df = sql("SELECT * FROM tbl_a a " +
+ "WHERE a._2 = 'US' OR EXISTS (SELECT /*+ BROADCAST(b) */ 1 FROM
tbl_b b WHERE b._1 = a._1)")
+ checkSparkAnswerAndOperator(
+ df,
+ Seq(classOf[CometBroadcastExchangeExec],
classOf[CometBroadcastHashJoinExec]))
+ }
+ }
+ }
+ }
+
+ test("ExistenceJoin via ShuffledHashJoin (EXISTS combined with OR)") {
+ withSQLConf(
+ CometConf.COMET_EXEC_EXISTENCE_JOIN_ENABLED.key -> "true",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false",
+ "spark.sql.join.forceApplyShuffledHashJoin" -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ withParquetTable((0 until 100).map(i => (i, if (i % 3 == 0) "US" else
"EU")), "tbl_a") {
+ withParquetTable((0 until 30).map(i => (i, i + 1)), "tbl_b") {
+ val df = sql(
+ "SELECT * FROM tbl_a a " +
+ "WHERE a._2 = 'US' OR EXISTS (SELECT 1 FROM tbl_b b WHERE b._1 =
a._1)")
+ checkSparkAnswerAndOperator(df, Seq(classOf[CometHashJoinExec]))
+ }
+ }
+ }
+ }
+
+ test("ExistenceJoin via SortMergeJoin falls back to Spark") {
Review Comment:
This test and the residual-condition and computed-key tests below only call
`checkSparkAnswer`, so they pass whether or not Comet falls back. I deleted the
`condition.isEmpty` and key checks and added an `ExistenceJoin` case to the SMJ
serde. All three queries then ran natively and every ExistenceJoin test still
passed. Could they use `checkSparkAnswerAndFallbackReason` instead? It would
also help if each guard had its own reason. Today the flag being off, a
residual condition and a computed key all report `Unsupported join type
ExistenceJoin(exists#N)`, so the tests can't tell which guard fired, and the
message reads like a permanent limitation to someone looking at their plan.
##########
spark/src/test/resources/sql-tests/join/existence_join.sql:
##########
@@ -0,0 +1,234 @@
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing,
+-- software distributed under the License is distributed on an
+-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+-- KIND, either express or implied. See the License for the
+-- specific language governing permissions and limitations
+-- under the License.
+
+-- Tests for ExistenceJoin: produced when EXISTS / NOT EXISTS is combined
+-- with another predicate via OR, preventing rewrite to LeftSemi / LeftAnti.
+--
+-- Strategy hints are placed INSIDE the EXISTS subquery (on the subquery's own
+-- relation alias), because an outer hint referencing the subquery table cannot
+-- resolve it and silently falls back to a broadcast hash join. Subquery-local
+-- hints (verified on Spark 4.1.3 with AQE off) select ShuffledHashJoin /
+-- SortMergeJoin.
+--
+-- Native existence support is currently hash-only: BROADCAST and SHUFFLE_HASH
+-- cases exercise CometBroadcastHashJoinExec / CometHashJoinExec, while MERGE
+-- cases fall back to Spark's SortMergeJoin (existence SMJ is not yet native)
and
+-- verify result parity under the Comet-enabled config.
+
+-- Native ExistenceJoin support is experimental and disabled by default.
+-- Config: spark.comet.exec.existenceJoin.enabled=true
+
+-- ============================================================
+-- Setup: NULLs (both sides), duplicates, empty build, all-NULL build
+-- ============================================================
+
+statement
+CREATE TABLE ex_left(id int, k int, region string) USING parquet
+
+statement
+INSERT INTO ex_left VALUES
+ (1, 1, 'US'),
+ (2, 2, 'EU'),
+ (3, NULL, 'US'),
+ (4, 4, 'EU'),
+ (5, 5, 'EU'),
+ (6, NULL, 'EU')
+
+statement
+CREATE TABLE ex_right(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right VALUES (10, 1), (11, 2), (12, 2), (13, NULL)
+
+statement
+CREATE TABLE ex_right_no_nulls(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_no_nulls VALUES (10, 1), (11, 5)
+
+statement
+CREATE TABLE ex_right_empty(id int, k int) USING parquet
+
+statement
+CREATE TABLE ex_right_dups(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_dups VALUES (10, 1), (11, 1), (12, 1), (13, 2)
+
+statement
+CREATE TABLE ex_right_all_null(id int, k int) USING parquet
+
+statement
+INSERT INTO ex_right_all_null VALUES (10, NULL), (11, NULL)
+
+statement
+CREATE TABLE ex_left_empty(id int, k int, region string) USING parquet
+
+-- ============================================================
+-- EXISTS with OR across all three strategies (hint in subquery)
+-- ============================================================
+
+query
+SELECT * FROM ex_left l
+WHERE l.region = 'US'
+ OR EXISTS (SELECT /*+ BROADCAST(r) */ 1 FROM ex_right r WHERE r.k = l.k)
+ORDER BY l.id
+
+query
+SELECT * FROM ex_left l
+WHERE l.region = 'US'
+ OR EXISTS (SELECT /*+ SHUFFLE_HASH(r) */ 1 FROM ex_right r WHERE r.k = l.k)
+ORDER BY l.id
+
+query
Review Comment:
Confirmed locally. With the four `MERGE` cases switched to `query
expect_fallback(Unsupported join type)`, the rest of the file passes.
--
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]