sunchao commented on code in PR #4587:
URL: https://github.com/apache/datafusion-comet/pull/4587#discussion_r4104655922
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -2488,6 +2488,17 @@ trait CometHashJoin {
case FullOuter => JoinType.FullOuter
case LeftSemi => JoinType.LeftSemi
case LeftAnti => JoinType.LeftAnti
+ // Native only for equi-join keys that are bare column refs /
literals: Spark short-
+ // circuits on first match and can skip key evaluation, but DF
evaluates all keys eagerly
+ // over the batch, so a computed key (e.g. a throwing cast) could
error on skipped rows.
+ case ExistenceJoin(_)
+ if CometConf.COMET_EXEC_EXISTENCE_JOIN_ENABLED.get(join.conf) &&
+ join.condition.isEmpty &&
+ (join.leftKeys ++ join.rightKeys).forall {
+ case _: Attribute | _: Literal => true
Review Comment:
[P1] Import `Literal` before using it in the new guard. The explicit
Catalyst expression imports omit
`org.apache.spark.sql.catalyst.expressions.Literal`, so this pattern fails
compilation with `not found: type Literal`. This blocks the JVM build and tests
even when existence joins are disabled. Add the missing import or fully qualify
the type.
Evidence: Exact-head CI reproduced this with `./mvnw -B install -DskipTests
-Dmaven.test.skip=true -Pspark-4.1`: operators.scala:2498 reports `not found:
type Literal`. The Spark 3.4 compilation and Spark 4.1 execution jobs report
the same error. Build log:
https://github.com/apache/datafusion-comet/actions/runs/36071677324/job/107874995590
##########
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:
[P2] Use an expected-fallback directive for the `MERGE` cases. Plain `query`
calls `checkSparkAnswerAndOperator`, which requires native execution, but these
hints select an existence `SortMergeJoin` that this PR deliberately leaves in
Spark. Once compilation is fixed, this case fails the native-operator assertion
and stops the fixture despite correct results. Change the sort-merge cases to
`query expect_fallback(Unsupported join type)` so they verify the intended
behavior.
Evidence: Fresh Spark 4.1.3 probes of this exact query returned ids [1, 2,
3] and selected `SortMergeJoin ... ExistenceJoin` with AQE both enabled and
disabled. At this head, CometSortMergeJoinExec.convert rejects that join type
at operators.scala:3029. CometSqlFileTestSuite.scala:155–156 routes plain
queries to the native assertion, and CometTestBase.scala:619–626 rejects
remaining Spark operators. This is a source-confirmed assertion conflict; the
full Comet fixture could not run because of the compilation failure.
--
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]