You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/18 07:02:25 UTC

[GitHub] [arrow-datafusion] jackwener opened a new pull request, #4272: reimplement `eliminate_outer_join`

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

   # 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 #4270 .
   
   # 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?
   
   <!--
   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.
   -->
   
   # 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] HaoYang670 commented on a diff in pull request #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#discussion_r1026146513


##########
datafusion/optimizer/src/eliminate_outer_join.rs:
##########
@@ -39,191 +38,107 @@ impl ReduceOuterJoin {
     }
 }
 
+/// Attempt to eliminate outer joins to inner joins.
+/// for query: select ... from a left join b on ... where b.xx = 100;
+/// if b.xx is null, and b.xx = 100 returns false, filtered those null rows.
+/// Therefore, there is no need to produce null rows for output, we can use
+/// inner join instead of left join.
+///
+/// Generally, an outer join can be eliminated to inner join if equals from where
+/// return false while any inputs are null and columns of those equals are come from
+/// nullable side of outer join.

Review Comment:
   We can simplify the docs to just illustrate that " We will replace the outer join by the inner join if the `null` rows in the nullable side will be filtered out.



##########
datafusion/optimizer/src/eliminate_outer_join.rs:
##########
@@ -39,191 +38,107 @@ impl ReduceOuterJoin {
     }
 }
 
+/// Attempt to eliminate outer joins to inner joins.

Review Comment:
   Question from a beginner: 
   
     Can a JOIN be always optimized to the INNER JOIN?
     For example:
     for a query
     ```
     select ... from a full join b where a.xx = 100
     ```
     we could only optimize it to a RIGHT JOIN.



-- 
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 #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#discussion_r1030716347


##########
datafusion/optimizer/src/eliminate_outer_join.rs:
##########
@@ -15,235 +15,141 @@
 // specific language governing permissions and limitations
 // under the License.
 
-//! Optimizer rule to reduce left/right/full join to inner join if possible.
-use crate::{OptimizerConfig, OptimizerRule};
+//! Optimizer rule to eliminate left/right/full join to inner join if possible.
+use crate::{utils, OptimizerConfig, OptimizerRule};
 use datafusion_common::{Column, DFSchema, Result};
 use datafusion_expr::{
     expr::BinaryExpr,
-    logical_plan::{Filter, Join, JoinType, LogicalPlan, Projection},
+    logical_plan::{Join, JoinType, LogicalPlan},
     utils::from_plan,
 };
 use datafusion_expr::{Expr, Operator};
 
 use datafusion_expr::expr::Cast;
-use std::collections::HashMap;
 use std::sync::Arc;
 
 #[derive(Default)]
-pub struct ReduceOuterJoin;
+pub struct EliminateOuterJoin;
 
-impl ReduceOuterJoin {
+impl EliminateOuterJoin {
     #[allow(missing_docs)]
     pub fn new() -> Self {
         Self {}
     }
 }
 
-impl OptimizerRule for ReduceOuterJoin {
+/// Attempt to eliminate outer joins.
+impl OptimizerRule for EliminateOuterJoin {
     fn optimize(
         &self,
         plan: &LogicalPlan,
         optimizer_config: &mut OptimizerConfig,
     ) -> Result<LogicalPlan> {
-        let mut nonnullable_cols: Vec<Column> = vec![];
+        match plan {
+            LogicalPlan::Filter(filter) => match filter.input().as_ref() {
+                LogicalPlan::Join(join) => {
+                    let mut non_nullable_cols: Vec<Column> = vec![];
+
+                    extract_non_nullable_columns(
+                        filter.predicate(),
+                        &mut non_nullable_cols,
+                        join.left.schema(),
+                        join.right.schema(),
+                        true,
+                    )?;
 
-        reduce_outer_join(self, plan, &mut nonnullable_cols, optimizer_config)
+                    let new_join_type = if join.join_type.is_outer() {
+                        let mut left_non_nullable = false;
+                        let mut right_non_nullable = false;
+                        for col in non_nullable_cols.iter() {
+                            if join.left.schema().field_from_column(col).is_ok() {
+                                left_non_nullable = true;
+                            }
+                            if join.right.schema().field_from_column(col).is_ok() {
+                                right_non_nullable = true;
+                            }
+                        }
+                        eliminate_outer(
+                            join.join_type,
+                            left_non_nullable,
+                            right_non_nullable,
+                        )
+                    } else {
+                        join.join_type
+                    };
+                    let new_join = LogicalPlan::Join(Join {
+                        left: Arc::new((*join.left).clone()),
+                        right: Arc::new((*join.right).clone()),
+                        join_type: new_join_type,
+                        join_constraint: join.join_constraint,
+                        on: join.on.clone(),
+                        filter: join.filter.clone(),
+                        schema: join.schema.clone(),
+                        null_equals_null: join.null_equals_null,
+                    });
+                    let new_plan = from_plan(plan, &plan.expressions(), &[new_join])?;
+                    utils::optimize_children(self, &new_plan, optimizer_config)
+                }
+                _ => utils::optimize_children(self, plan, optimizer_config),
+            },
+            _ => utils::optimize_children(self, plan, optimizer_config),
+        }
     }
 
     fn name(&self) -> &str {
-        "reduce_outer_join"
+        "eliminate_outer_join"
     }
 }
 
-/// Attempt to reduce outer joins to inner joins.
-/// for query: select ... from a left join b on ... where b.xx = 100;
-/// if b.xx is null, and b.xx = 100 returns false, filterd those null rows.
-/// Therefore, there is no need to produce null rows for output, we can use
-/// inner join instead of left join.
-///
-/// Generally, an outer join can be reduced to inner join if quals from where
-/// return false while any inputs are null and columns of those quals are come from
-/// nullable side of outer join.
-fn reduce_outer_join(
-    _optimizer: &ReduceOuterJoin,
-    plan: &LogicalPlan,
-    nonnullable_cols: &mut Vec<Column>,
-    _optimizer_config: &OptimizerConfig,
-) -> Result<LogicalPlan> {
-    match plan {
-        LogicalPlan::Filter(filter) => match filter.input().as_ref() {
-            LogicalPlan::Join(join) => {
-                extract_nonnullable_columns(
-                    filter.predicate(),
-                    nonnullable_cols,
-                    join.left.schema(),
-                    join.right.schema(),
-                    true,
-                )?;
-                Ok(LogicalPlan::Filter(Filter::try_new(
-                    filter.predicate().clone(),
-                    Arc::new(reduce_outer_join(
-                        _optimizer,
-                        filter.input(),
-                        nonnullable_cols,
-                        _optimizer_config,
-                    )?),
-                )?))
+pub fn eliminate_outer(

Review Comment:
   Thank you for eliminating the duplication



-- 
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 #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#discussion_r1026185811


##########
datafusion/optimizer/src/eliminate_outer_join.rs:
##########
@@ -39,191 +38,107 @@ impl ReduceOuterJoin {
     }
 }
 
+/// Attempt to eliminate outer joins to inner joins.

Review Comment:
   You can see `fn eliminate_outer()`
   Your example, this rule can do.
   We can eliminate Full -> ROJ/LOJ/Inner



-- 
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 #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
alamb merged PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272


-- 
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 pull request #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#issuecomment-1325439118

   Sorry for the delay in reviewing @jackwener  -- the PRs are flowing fast an furious this week


-- 
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 pull request #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#issuecomment-1325438880

   Filed a follow on PR to add some docstrings: https://github.com/apache/arrow-datafusion/pull/4343


-- 
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] ursabot commented on pull request #4272: reimplement `eliminate_outer_join`

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4272:
URL: https://github.com/apache/arrow-datafusion/pull/4272#issuecomment-1325431266

   Benchmark runs are scheduled for baseline = e817d75bbf400bfcf4f27e4fb8c4ba14efd8102c and contender = 502b7e3cae169b25caf85062be6dfca32f9f173b. 502b7e3cae169b25caf85062be6dfca32f9f173b is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/b100b93754ab40579f589f8a54517373...0060c9e5d5634e0e90a8f7455c62a9ce/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/df35dcc095a64edba9310fef31702721...8e1fbd4228034ded9cbca9210f5472d6/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/6782022b4f0c4c85a4e6b9a4a9684bd8...ba8711e485514f098f17e10d42b50374/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/2d218001632f47a19306c67cf9def534...eb3bc4dec2274a5993bdf7190fa7e15b/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


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