Repository navigation
test: add correlated subquery cases for general decorrelation - #26904
Conversation
jayzhan211
left a comment
There was a problem hiding this comment.
@mohammadnaqvi04 thanks for putting this catalog together! Please add the reference answer as a comment above each query whose recorded result isn't the right one: 11 queries record wrong rows and 34 record errors, and nothing in the file tells them apart from the 8 that are right. Without it, a later PR that changes a recorded result has nothing in the file to check its new rows against, which is what step 1 of the plan on #5492 asks for ("each with its reference answer"). The DuckDB answers you already have are enough, for example:
# correlated_lossy_cast_below_count
+# Wrong today. Expected (DuckDB):
+# 2024-01-01 2
+# 2024-01-02 1
query DIThe 11 recorded wrong results, with DuckDB 1.1.3's answer
| Line | Issue | Recorded | DuckDB |
|---|---|---|---|
| 162 | #16134 (config off) | 4 rows | error: more than one row |
| 322 | #26234 | (empty) | 1 |
| 337 | #26014 | 01-01 1, 01-01 1, 01-02 1 |
01-01 2, 01-02 1 |
| 358 | #26032 | 1 |
1, 2 |
| 377 | #25808 | 0 1, 0 1, 0 2 |
0 1, 0 2 |
| 415 | #26503 | 1 1 1, 2 1 0, 1 9 0 |
1 1 1, 2 1 NULL, 1 9 NULL |
| 424 | #26505 | 1 10, 2 NULL |
1 NULL, 2 NULL |
| 443 | #26695 | -1, 1, 2 |
1, 2 |
| 464 | #26629 | 1 |
(empty) |
| 552 | #21198 | 1 2, 2 NULL |
1 2 |
| 722 | #26692 | 1 |
1, 1 |
This file also fails 5 queries on current main (I ran it on 3f4a37f, one commit behind main), so it needs a rebase and a re-record before merge. The #26503 (line 416) and #26629 (line 465) queries now error, after #26723 and #26640. The #26285 (line 530), #26622 (line 607) and #26572 (line 867) queries now return rows, and those rows match DuckDB.
What main returns now for the three that return rows
- line 530:
1,3,100000 - line 607:
false,true - line 867:
1 2,2 2
|
|
||
| # https://github.com/apache/datafusion/issues/26285 | ||
| statement ok | ||
| set datafusion.optimizer.prefer_hash_join = false; |
There was a problem hiding this comment.
Optional cleanup: on current main this query also returns the right rows (1, 3, 100000) with the default hash join, so after the rebase this override and the RESET at line 543 can go, and the test runs the default join path.
-statement ok
-set datafusion.optimizer.prefer_hash_join = false;
-
statement ok
CREATE TABLE a(k INT) AS VALUES (1), (100000);
@@
-statement ok
-RESET datafusion.optimizer.prefer_hash_join;
-There was a problem hiding this comment.
Done. The override and the RESET are gone.
e96cbea to
3650291
Compare
|
Thanks @jayzhan211! I added DuckDB's answer above the 42 queries that aren't correct yet and rebased and re-recorded on current main. From what I can tell, six results changed, one more than you saw. #26503, #26629 and #21198 now error, #21198 after #26874, which merged after your review. #26285, #26622 and #26572 now return the right rows, though #26285 and #26622 are still open. I've also updated the counts in the description. |
|
Re-ran all 57 queries in DuckDB 1.5.6. Every expected answer in the comments matches, the 11 recorded as right match too, and the 4 error cases also fail in DuckDB. One small thing: (the HAVING one gives Otherwise looks good to me. |
jayzhan211
left a comment
There was a problem hiding this comment.
Thanks for the update, @mohammadnaqvi04. The file passes on the current main for me too. +1 to @raghavyadav01's suggestion: the two invalid_aggregate_over_outer_column_* queries fail in the physical planner rather than while planning, so a comment saying they should fail while planning would stop a later PR from recording rows for them as a fix.
|
Thanks for the reviews @jayzhan211 @raghavyadav01! I've updated the relevant test comments to add the expected failure. |
Mark the apache#26904 corpus case for apache#26692 as an error. Drop the two "without a shared alias" cases, which main now rejects through apache#27244. Signed-off-by: Raghav Yadav <raghavmnnit@gmail.com>
…queryAlias (apache#27019) ## Which issue does this PR close? - Part of apache#26692 (the wrong-results shapes). ## Rationale for this change `EXISTS (SELECT 1 FROM (SELECT * FROM t2 AS s WHERE s.a = t.a) x CROSS JOIN t2 AS s)` returns 3 rows instead of 9 (apache#26692 query 1). `PullUpCorrelatedExpr` keys `correlated_subquery_cols_map` and `collected_count_expr_map` by plan value, so the two equal `t2 AS s` share one entry and one picks up or replaces the other's correlated columns. Following apache#5492, these shapes now error rather than being made to work. ## What changes are included in this PR? The rewriter remembers the order in which each `SubqueryAlias` plan is first returned by `f_up`. If a node returns one that was first returned outside its own subtree, and either has correlated facts, `can_pull_up` is set to false and the subquery stays correlated. ## Are these changes tested? Yes. sqllogictests for q1 in EXISTS / NOT EXISTS / IN / scalar forms, the same with the plain alias first, the `LIMIT` case, the case where a second optimizer pass collides on the scans below the aliases, and shapes that must keep working (including two equal aliases without correlated columns of their own). The apache#26904 corpus case for apache#26692 now expects an error. I also compared 33 shapes against DuckDB 1.5.6. Every one that changed went from wrong rows to an error, except the shapes listed below, which go from correct rows to an error. Not covered: a `LATERAL` count over a correlated derived table and the same table still returns wrong rows, filed as apache#27566. The scalar form of that query now errors through apache#27244. ## Are there any user-facing changes? Queries that returned wrong rows now return an error. These shapes also error now, although they return correct rows in 55.3.0: - two identical correlated copies of an aliased derived table in `EXISTS` / `NOT EXISTS`, e.g. `EXISTS (SELECT 1 FROM (SELECT * FROM t2 AS s WHERE s.a = t.a) x CROSS JOIN (SELECT * FROM t2 AS s WHERE s.a = t.a) y)` - the plain `t2 AS s` before the correlated copy in `EXISTS` They are correct only because the two copies hold the same rows. Once the query reads the second copy's columns the same shape is wrong in 55.3.0: this returns `1`, and the expected result is empty. ```sql SELECT t.a FROM t WHERE EXISTS ( SELECT 1 FROM (SELECT * FROM t2 AS s WHERE s.a = t.a) x JOIN (SELECT * FROM t2 AS s WHERE s.a = t.a) y ON y.b = x.b + 29 ) ORDER BY 1; ``` AI-assisted. I reviewed every line and ran the tests above. --------- Signed-off-by: Raghav Yadav <raghavmnnit@gmail.com>
Which issue does this PR close?
Part of #5492.
Rationale for this change
Many correlated subqueries fail to plan in DataFusion today, and some return wrong rows. #5492 tracks a general decorrelation rule to replace the current rules, and the plan proposed there starts with a tests-only PR, so every later PR has a fixed set of queries to check its fixes against.
What changes are included in this PR?
subquery_decorrelation.sltcontaining 57 queries.Includes the following:
#26569 from that list isn't included, because it only affects joins built through the Rust API. DataFusion's own decorrelation sets the value key count (
null_aware(value_keys=2)for a two-columnNOT IN), so no SQL query shows it.The cases @jayzhan211's fuzzer found will follow in a second PR.
Each query records what
mainreturns today. The 42 that aren't right yet have DuckDB's answer in a comment above them, so a PR that fixes one replaces the recorded error or rows with that answer. Cases taken from an issue link to it. Today the 57 queries break down as follows.What is the testing strategy for this PR?
The file passes on
main, with output recorded by--complete, errors trimmed to stable patterns, androwsortfor queries without anORDER BY. Answers were checked against DuckDB 2.0.0, and SQLite 3.51 agrees on 51 of the 57. @jayzhan211's harness agrees with the file on the 40 of 44 repro queries it can check.Are there any user-facing changes?
No, this only adds tests.
AI Usage
AI was used during development of this PR, primarily to help review the test suite and verify test cases against the DB binaries.