You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jackwener (via GitHub)" <gi...@apache.org> on 2023/05/25 12:44:48 UTC

[GitHub] [arrow-datafusion] jackwener opened a new pull request, #6443: feat: eliminate useless join | convert inner to outer when condition is true

jackwener opened a new pull request, #6443:
URL: https://github.com/apache/arrow-datafusion/pull/6443

   # Which issue does this PR close?
   
   Closes #.
   
   # 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.  
   -->
   
   # What changes are included in this PR?
   
   - eliminate useless join 
   - convert inner to outer when condition is true
   - simplify code
   
   # Are these changes tested?
   
   <!--
   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?
   
   <!--
   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@arrow.apache.org

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


[GitHub] [arrow-datafusion] jackwener commented on pull request #6443: feat: eliminate useless join | convert inner to outer when condition is true

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

   >  I wonder if you actually hit any of these queries somehow or if this is a theoretical concern?
   
   we should do this optimization because inner join should exist edge (equal/non-equal condition) in semantic, this rule separate fake inner join to be cross join.
   
   If we don't handle it, some rule may ignore this corner case in some optimization, it may occur BUG. Such as Join Reorder....


-- 
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@arrow.apache.org

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


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #6443: feat: eliminate useless join | convert inner to outer when condition is true

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #6443:
URL: https://github.com/apache/arrow-datafusion/pull/6443#discussion_r1206865268


##########
datafusion/optimizer/src/eliminate_join.rs:
##########
@@ -0,0 +1,122 @@
+// 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.
+
+use crate::optimizer::ApplyOrder;
+use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::{Result, ScalarValue};
+use datafusion_expr::JoinType::Inner;
+use datafusion_expr::{
+    logical_plan::{EmptyRelation, LogicalPlan},
+    CrossJoin, Expr,
+};
+
+/// Eliminates joins when inner join condition is false.
+/// Replaces joins when inner join condition is true with a cross join.
+#[derive(Default)]
+pub struct EliminateJoin;
+
+impl EliminateJoin {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl OptimizerRule for EliminateJoin {
+    fn try_optimize(
+        &self,
+        plan: &LogicalPlan,
+        _config: &dyn OptimizerConfig,
+    ) -> Result<Option<LogicalPlan>> {
+        match plan {
+            LogicalPlan::Join(join) if join.join_type == Inner && join.on.is_empty() => {
+                match join.filter {
+                    Some(Expr::Literal(ScalarValue::Boolean(Some(v)))) => {

Review Comment:
   It's a nice improvement



-- 
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@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6443: feat: eliminate useless join | convert inner to outer when condition is true

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6443:
URL: https://github.com/apache/arrow-datafusion/pull/6443#discussion_r1206795673


##########
datafusion/expr/src/logical_plan/display.rs:
##########
@@ -242,10 +242,8 @@ impl<'a, 'b> TreeNodeVisitor for GraphvizVisitor<'a, 'b> {
         // always be non-empty as pre_visit always pushes
         // So it should always be Ok(true)
         let res = self.parent_ids.pop();
-        match res {
-            Some(_) => Ok(VisitRecursion::Continue),
-            None => Err(DataFusionError::Internal("Fail to format".to_string())),
-        }
+        res.ok_or(DataFusionError::Internal("Fail to format".to_string()))

Review Comment:
   nice cleanup -- thank you



##########
datafusion/core/tests/sqllogictests/test_files/join.slt:
##########
@@ -551,6 +551,27 @@ with t1 as (select 1 as col1, 'asd' as col2),
      t2 as (select 1 as col3, 'sdf' as col4)
 select col2, col4 from t1 full outer join t2 on col1 = col3
 
+# test eliminate join when condition is false

Review Comment:
   👍 



##########
datafusion/optimizer/src/eliminate_join.rs:
##########
@@ -0,0 +1,122 @@
+// 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.
+
+use crate::optimizer::ApplyOrder;
+use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::{Result, ScalarValue};
+use datafusion_expr::JoinType::Inner;
+use datafusion_expr::{
+    logical_plan::{EmptyRelation, LogicalPlan},
+    CrossJoin, Expr,
+};
+
+/// Eliminates joins when inner join condition is false.
+/// Replaces joins when inner join condition is true with a cross join.
+#[derive(Default)]
+pub struct EliminateJoin;
+
+impl EliminateJoin {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl OptimizerRule for EliminateJoin {
+    fn try_optimize(
+        &self,
+        plan: &LogicalPlan,
+        _config: &dyn OptimizerConfig,
+    ) -> Result<Option<LogicalPlan>> {
+        match plan {
+            LogicalPlan::Join(join) if join.join_type == Inner && join.on.is_empty() => {
+                match join.filter {
+                    Some(Expr::Literal(ScalarValue::Boolean(Some(v)))) => {

Review Comment:
   I don't know if it would be better, but you could also write this using two match arms:
   
   ```
   Some(Expr::Literal(ScalarValue::Boolean(Some(true)))) => {
   ...
   }
   Some(Expr::Literal(ScalarValue::Boolean(Some(false)))) => {
   ...
   }
   ```



-- 
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@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb merged pull request #6443: feat: eliminate useless join | convert inner to outer when condition is true

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


-- 
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@arrow.apache.org

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