dongjoon-hyun opened a new pull request, #60699:
URL: https://github.com/apache/spark/pull/60699

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue of the cost-based join reorder 
(`spark.sql.cbo.enabled` and `spark.sql.cbo.joinReorder.enabled`). 
`JoinReorderDP` dropped a join condition that references only one side of the 
joins (or no side), so the query returned extra rows. This PR also fixes query 
failures when the reorder evaluates a throwable condition, or one that can fail 
after it, ahead of the conditions written before it.
   
   `CostBasedJoinReorder` collects every conjunct of the conditions of the 
inner joins it reorders, but `JoinReorderDP.buildJoin` only puts into a new 
join the conjuncts that reference both of its sides, and `JoinReorderDP.search` 
returned the final plan without the conjuncts that no join took. Such a 
conjunct reaches the rule since SPARK-46707, because `PushPredicateThroughJoin` 
keeps a throwable conjunct (e.g. one with `raise_error`, which is throwable 
since SPARK-58627, or `sequence` with a step) in the join condition instead of 
pushing it to one side. Since SPARK-61924, it also keeps there a conjunct that 
can fail, e.g. a division in ANSI mode, written after a throwable one.
   
   This PR changes `JoinReorderDP` as follows.
   1. `search` puts the conjuncts that no join took into the condition of the 
top join, after its own conditions, in their written order, instead of dropping 
them. Before, the query also failed an assertion when such a conjunct 
referenced a column that the query does not output.
   2. `buildJoin` joins two plans only by the conjuncts that 
`PredicateHelper.splitMovable` allows to move, as `ReorderJoin` does since 
SPARK-61930. From the first throwable conjunct on, the ones that can fail are 
not used, so they also go into the top join by 1, after the conjuncts written 
before them. Before, the reorder could put a throwable conjunct into a lower 
join than the one it was written in, where it was evaluated on rows that an 
upper join would remove, and it could put a conjunct that can fail ahead of a 
throwable one written before it. In both cases, the query failed.
   3. `search` returns `None`, and `CostBasedJoinReorder` keeps the original 
order, when the join conditions that `buildJoin` uses do not connect all the 
items, e.g. when the only condition of a join is throwable or references one 
side. Before, this failed an assertion.
   
   ### Why are the changes needed?
   
   To fix a correctness issue. The cost-based join reorder returns wrong 
results.
   
   ```sql
   CREATE TABLE s(id INT, value INT) USING parquet;
   CREATE TABLE v(value INT) USING parquet;
   CREATE TABLE w(value INT) USING parquet;
   INSERT INTO s VALUES (-1, 10), (0, 20), (1, 20);
   INSERT INTO v VALUES 10, 20;
   INSERT INTO w VALUES 20;
   ANALYZE TABLE s COMPUTE STATISTICS FOR ALL COLUMNS;
   ANALYZE TABLE v COMPUTE STATISTICS FOR ALL COLUMNS;
   ANALYZE TABLE w COMPUTE STATISTICS FOR ALL COLUMNS;
   SET spark.sql.cbo.enabled=true;
   SET spark.sql.cbo.joinReorder.enabled=true;
   
   -- Issue 1: It should return [1], but returns [0] and [1].
   SELECT sv.id FROM (SELECT s.id, s.value FROM s JOIN v ON s.value = v.value) 
sv
   JOIN w ON sv.value = w.value AND
     CASE WHEN sv.id > 0 THEN true WHEN sv.id = 0 THEN false ELSE 
raise_error('boom') END;
   
   -- Issue 2: It should fail with USER_RAISED_EXCEPTION, but returns [0] and 
[1].
   SELECT sv.id FROM (SELECT s.id, s.value FROM s JOIN v ON s.value = v.value) 
sv
   JOIN w ON sv.value = w.value AND raise_error('boom') IS NULL;
   
   -- Issue 3: It should return [20], but fails with INTERNAL_ERROR (assertion 
failed).
   SELECT w.value FROM (SELECT s.id, s.value FROM s JOIN v ON s.value = 
v.value) sv
   JOIN w ON sv.value = w.value AND
     CASE WHEN sv.id > 0 THEN true WHEN sv.id = 0 THEN false ELSE 
raise_error('boom') END;
   
   -- Issue 4: It should return [20] and [20], but fails with INTERNAL_ERROR 
(assertion failed).
   SELECT w.value FROM (SELECT s.value FROM s JOIN v ON
     CASE WHEN s.id > 0 THEN true WHEN s.id >= -1 THEN false ELSE 
raise_error('boom') END) sv
   JOIN w ON sv.value = w.value;
   
   -- Issue 5: It should return [1], but fails with DIVIDE_BY_ZERO.
   SELECT sv.id FROM (SELECT s.id, v.value FROM s JOIN v ON s.value = v.value) 
sv
   JOIN w ON sv.value = w.value AND
     CASE WHEN sv.id > 0 THEN true WHEN sv.id = 0 THEN false ELSE 
raise_error('boom') END AND
     10 / (sv.id + sv.value - 20) > 0;
   ```
   
   The pruning project between the joins keeps `ReorderJoin` from flattening 
them, so only the cost-based join reorder changes their order. Without the 
cost-based join reorder, these queries return the expected results. In Issue 5, 
the reorder puts the division into the top join ahead of the `CASE WHEN`, which 
is false on the row whose `id` is 0, so the division is evaluated on that row.
   
   The reorder could also move a throwable condition over both sides into a 
lower join. For example, with a table `w100` that has 100 rows of 20, the 
following query should return 100 rows of 1, but fails with 
USER_RAISED_EXCEPTION, because the reorder moves the `CASE WHEN` into the join 
of `s` and `v`, where it is evaluated on the row whose `id` is -1.
   
   ```sql
   SELECT sv.id FROM (SELECT s.id, v.value FROM s JOIN v ON s.value = v.value) 
sv
   JOIN w100 ON sv.value = w100.value AND
     CASE WHEN sv.id + sv.value > 20 THEN true WHEN sv.id + sv.value = 20 THEN 
false
     ELSE raise_error('boom') END;
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this fixes wrong results and query failures of the cost-based join 
reorder. Spark 4.0.0 and later are affected (SPARK-46707).
   - In Apache Spark 4.2.1 and 4.3.0, Issue 2 returns the wrong result as 
above. Issue 1 and Issue 3 fail there with USER_RAISED_EXCEPTION with or 
without the cost-based join reorder, because `InferFiltersFromConstraints` 
copies the condition below the join (SPARK-60985 fixed it in `master`). With 
`spark.sql.constraintPropagation.enabled=false`, Issue 1 returns [0] and [1], 
and Issue 3 fails with the assertion, as above.
   - In Apache Spark 4.0.5 and 4.1.4, `raise_error` is not throwable. When 
`size(sequence(0, abs(sv.id), 1)) > 1` is used instead of the `CASE WHEN`, 
Issue 1 returns [0] and [1], and Issue 3 fails with the assertion, with 
`spark.sql.constraintPropagation.enabled=false`. With the default 
configurations, the filter that `InferFiltersFromConstraints` adds between the 
project and the join keeps the joins from being reordered.
   - Issue 5 fails with or without the cost-based join reorder in the released 
versions. In Apache Spark 4.2.1 and 4.3.0, it fails with DIVIDE_BY_ZERO, 
because `PushPredicateThroughJoin` pushes the division below the join 
(SPARK-61924 fixed it in `master`). In Apache Spark 4.0.5 and 4.1.4, it fails 
with USER_RAISED_EXCEPTION, because `raise_error` is not throwable there and 
the `CASE WHEN` is pushed below the join.
   - Apache Spark 3.5.9 is not affected.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases.
   - `JoinReorderSuite`: a throwable condition over one item, a throwable 
condition over two items and a division after it go into the top join, and the 
original order is kept when the join conditions do not connect all items.
   - `DataFrameJoinSuite`: Issue 1 above.
   
   Both fail without this fix.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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