You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "goldmedal (via GitHub)" <gi...@apache.org> on 2025/03/20 15:31:24 UTC

[PR] Enforce JOIN plan to require condition [datafusion]

goldmedal opened a new pull request, #15334:
URL: https://github.com/apache/datafusion/pull/15334

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #13486 
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   When working on unparsing the plan optimized by `ScalarSubqueryToJoin`, I noticed it could generate a LEFT JOIN without the join condition.
   ```
   Projection: customer.c_custkey [c_custkey:Int64]
     Filter: customer.c_custkey = __scalar_sq_1.max(orders.o_custkey) [c_custkey:Int64, c_name:Utf8, max(orders.o_custkey):Int64;N]
       Left Join:  [c_custkey:Int64, c_name:Utf8, max(orders.o_custkey):Int64;N]
         TableScan: customer [c_custkey:Int64, c_name:Utf8]
         SubqueryAlias: __scalar_sq_1 [max(orders.o_custkey):Int64;N]
           Projection: max(orders.o_custkey) [max(orders.o_custkey):Int64;N]
             Aggregate: groupBy=[[]], aggr=[[max(orders.o_custkey)]] [max(orders.o_custkey):Int64;N]
               TableScan: orders [o_orderkey:Int64, o_custkey:Int64, o_orderstatus:Utf8, o_totalprice:Float64;N]
   ```
   The unparsing result will look like
   ```sql
   SELECT 
     customer.c_custkey 
   FROM 
     customer 
     LEFT JOIN (
       SELECT 
         max(orders.o_custkey) 
       FROM 
         orders
     ) AS __scalar_sq_1
   WHERE 
     (
       customer.c_custkey = __scalar_sq_1."max(orders.o_custkey)"
     )
   ```
   
   Most databases don't allow the join without the condition besides the cross join. 
   ## DuckDB
   ```
   D select * from c1 join c2;
   Parser Error:
   syntax error at or near ";"
   ```
   
   ## Postgres
   ```
   test=# select * from t1 join t2;
   ERROR:  syntax error at or near ";"
   LINE 1: select * from t1 join t2;
   ```
   
   ## DataFusion
   ```
   > select * from t1 join t2;
   +----+----+
   | c1 | c1 |
   +----+----+
   | 1  | 1  |
   +----+----+
   1 row(s) fetched. 
   Elapsed 0.005 seconds.
   ```
   
   I prefer to align this behavior with others. It makes the logical plan created by DataFusion reliable.
   #13486 also mentioned that `join` without condition isn't a valid SQL.
   
   ## What changes are included in this PR?
   - Enforce the join to re
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   - Enforce JOIN plan to require conditions when built
   - ScalarSubqueryToJoin will generate the `LEFT JOIN` plan with the condition `True`.
   
   ## Are these changes tested?
   yes
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   The join syntax without condition is not allowed anymore.
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#issuecomment-2818265303

   > @goldmedal I don't understand this change. Previously, we have a sql:
   > 
   > ```
   > select * from t
   > where (select sid from t) = (select a from t limit 1)
   > order by ts
   > ```
   
   The `ScalarSubquery` will be transformed by `ScalarSubqueryToJoin` to a left join, and then this PR forces the left join to have a condition. So, in this PR, I added the `True` expression for the left join in `ScalarSubqueryToJoin`.
   
   I think the result is expected.
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "niebayes (via GitHub)" <gi...@apache.org>.
niebayes commented on PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#issuecomment-2817844756

   I don't understand this change. 
   Previously, we have a sql:
   ```
   select * from t
   where (select sid from t) = (select a from t limit 1)
   order by ts
   ```
   which definitely should not have any filters. But now, after upgrade datafusion to v47, the generated plan has a `Filter: Boolean(true)`.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on code in PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#discussion_r2009971193


##########
datafusion/sqllogictest/test_files/limit.slt:
##########
@@ -723,14 +723,14 @@ statement ok
 create table testSubQueryLimit (a int, b int) as values (1,2), (2,3), (3,4);
 
 query IIII
-select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 1) limit 10;
+select * from testSubQueryLimit as t1, (select * from testSubQueryLimit limit 1) limit 10;

Review Comment:
   I thought that It was an invalid SQL mentioned by #13486. But indeed, we can modify it after #13486 is fixed. I revert this change here.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on code in PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#discussion_r2008714660


##########
datafusion/sqllogictest/test_files/tpch/plans/q22.slt.part:
##########
@@ -65,7 +65,7 @@ logical_plan
 07)------------Projection: customer.c_phone, customer.c_acctbal
 08)--------------LeftAnti Join: customer.c_custkey = __correlated_sq_1.o_custkey
 09)----------------Filter: substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8View("13"), Utf8View("31"), Utf8View("23"), Utf8View("29"), Utf8View("30"), Utf8View("18"), Utf8View("17")])
-10)------------------TableScan: customer projection=[c_custkey, c_phone, c_acctbal], partial_filters=[substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8View("13"), Utf8View("31"), Utf8View("23"), Utf8View("29"), Utf8View("30"), Utf8View("18"), Utf8View("17")])]
+10)------------------TableScan: customer projection=[c_custkey, c_phone, c_acctbal], partial_filters=[substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8View("13"), Utf8View("31"), Utf8View("23"), Utf8View("29"), Utf8View("30"), Utf8View("18"), Utf8View("17")]), Boolean(true)]

Review Comment:
   I benchmarked to ensure the plan change won't impact the performance.
   ```
   --------------------
   Benchmark tpch_sf1.json
   --------------------
   ┏━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
   ┃ Query        ┃    main ┃ fix_join-check ┃       Change ┃
   ┡━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
   │ QQuery 1     │ 68.97ms │        70.14ms │    no change │
   │ QQuery 2     │ 18.72ms │        18.55ms │    no change │
   │ QQuery 3     │ 30.47ms │        29.46ms │    no change │
   │ QQuery 4     │ 21.45ms │        21.77ms │    no change │
   │ QQuery 5     │ 43.53ms │        41.56ms │    no change │
   │ QQuery 6     │ 14.39ms │        14.88ms │    no change │
   │ QQuery 7     │ 55.03ms │        55.30ms │    no change │
   │ QQuery 8     │ 41.24ms │        39.45ms │    no change │
   │ QQuery 9     │ 50.84ms │        53.19ms │    no change │
   │ QQuery 10    │ 44.85ms │        44.83ms │    no change │
   │ QQuery 11    │ 13.42ms │        13.22ms │    no change │
   │ QQuery 12    │ 28.49ms │        28.20ms │    no change │
   │ QQuery 13    │ 29.56ms │        29.38ms │    no change │
   │ QQuery 14    │ 23.03ms │        24.40ms │ 1.06x slower │
   │ QQuery 15    │ 33.59ms │        34.98ms │    no change │
   │ QQuery 16    │ 13.07ms │        13.08ms │    no change │
   │ QQuery 17    │ 58.00ms │        58.67ms │    no change │
   │ QQuery 18    │ 76.00ms │        75.06ms │    no change │
   │ QQuery 19    │ 39.99ms │        38.40ms │    no change │
   │ QQuery 20    │ 29.04ms │        29.79ms │    no change │
   │ QQuery 21    │ 62.14ms │        64.14ms │    no change │
   │ QQuery 22    │ 13.82ms │        14.01ms │    no change │
   └──────────────┴─────────┴────────────────┴──────────────┘
   ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
   ┃ Benchmark Summary             ┃          ┃
   ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
   │ Total Time (main)             │ 809.65ms │
   │ Total Time (fix_join-check)   │ 812.45ms │
   │ Average Time (main)           │  36.80ms │
   │ Average Time (fix_join-check) │  36.93ms │
   │ Queries Faster                │        0 │
   │ Queries Slower                │        1 │
   │ Queries with No Change        │       21 │
   └───────────────────────────────┴──────────┘
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on code in PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#discussion_r2005913705


##########
datafusion/optimizer/src/push_down_limit.rs:
##########
@@ -861,167 +849,6 @@ mod test {
         assert_optimized_plan_equal(outer_query, expected)
     }
 
-    #[test]
-    fn limit_should_push_down_join_without_condition() -> Result<()> {

Review Comment:
   This case has never occurred now. 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#issuecomment-2752894014

   Thanks @comphead and @alamb 👍 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #15334:
URL: https://github.com/apache/datafusion/pull/15334


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#discussion_r2009201610


##########
datafusion/sqllogictest/test_files/join.slt.part:
##########
@@ -625,6 +625,24 @@ FROM t1
 ----
 11 11 11
 
+# join condition is required
+# TODO: query error join condition should not be empty
+# related to: https://github.com/apache/datafusion/issues/13486
+statement ok
+SELECT * FROM t1 JOIN t2
+
+# join condition is required
+query error join condition should not be empty
+SELECT * FROM t1 LEFT JOIN t2
+
+# join condition is required
+query error join condition should not be empty
+SELECT * FROM t1 RIGHT JOIN t2
+
+# join condition is required
+query error join condition should not be empty
+SELECT * FROM t1 FULL JOIN t2

Review Comment:
   Please include also a cross join without filters like
   ```
   SELECT * FROM t1 CROSS JOIN t2
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enforce JOIN plan to require condition [datafusion]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #15334:
URL: https://github.com/apache/datafusion/pull/15334#discussion_r2009201037


##########
datafusion/sqllogictest/test_files/limit.slt:
##########
@@ -723,14 +723,14 @@ statement ok
 create table testSubQueryLimit (a int, b int) as values (1,2), (2,3), (3,4);
 
 query IIII
-select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 1) limit 10;
+select * from testSubQueryLimit as t1, (select * from testSubQueryLimit limit 1) limit 10;

Review Comment:
   why this change is needed? 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org