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

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5322: Support for Sliding Windows Joins with Symmetric Hash Join (SHJ)

alamb commented on code in PR #5322:
URL: https://github.com/apache/arrow-datafusion/pull/5322#discussion_r1114333530


##########
datafusion/physical-expr/src/utils.rs:
##########
@@ -235,6 +234,82 @@ pub fn ordering_satisfy_concrete<F: FnOnce() -> EquivalenceProperties>(
     }
 }
 
+#[derive(Clone, Debug)]
+pub struct ExprTreeNode<T> {

Review Comment:
   Can you please add some documentation to this structure explaining its use?



##########
datafusion/core/src/execution/context.rs:
##########
@@ -1528,6 +1528,9 @@ impl SessionState {
             // repartitioning and local sorting steps to meet distribution and ordering requirements.
             // Therefore, it should run before EnforceDistribution and EnforceSorting.
             Arc::new(JoinSelection::new()),
+            // Enforce sort before PipelineFixer

Review Comment:
   Why doess the enforcement happens before pipeline fixer (and not for example, also before `JoinSelection`)?



##########
datafusion/physical-expr/src/intervals/test_utils.rs:
##########
@@ -0,0 +1,67 @@
+// 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.
+
+//! Test utilities for the interval arithmetic library
+
+use std::sync::Arc;
+
+use crate::expressions::{BinaryExpr, Literal};
+use crate::PhysicalExpr;
+use datafusion_common::ScalarValue;
+use datafusion_expr::Operator;
+
+#[allow(clippy::too_many_arguments)]
+/// This test function generates a conjunctive statement with two numeric
+/// terms with the following form:
+/// left_col (op_1) a  > right_col (op_2) b AND left_col (op_3) c < right_col (op_4) d
+pub fn gen_conjunctive_numeric_expr(
+    left_col: Arc<dyn PhysicalExpr>,
+    right_col: Arc<dyn PhysicalExpr>,
+    op_1: Operator,
+    op_2: Operator,
+    op_3: Operator,
+    op_4: Operator,
+    a: i32,
+    b: i32,
+    c: i32,
+    d: i32,
+) -> Arc<dyn PhysicalExpr> {
+    let left_and_1 = Arc::new(BinaryExpr::new(
+        left_col.clone(),
+        op_1,
+        Arc::new(Literal::new(ScalarValue::Int32(Some(a)))),

Review Comment:
   FWIW I think you can use the `lit` function to make this code easier to write:
   
   https://docs.rs/datafusion-physical-expr/18.0.0/datafusion_physical_expr/expressions/fn.lit.html
   
   ```suggestion
           Arc::new(lit(a)),
   ```



##########
datafusion/physical-expr/Cargo.toml:
##########
@@ -60,6 +60,7 @@ lazy_static = { version = "^1.4.0" }
 md-5 = { version = "^0.10.0", optional = true }
 num-traits = { version = "0.2", default-features = false }
 paste = "^1.0"
+petgraph = "0.6.2"

Review Comment:
   I reviewed this crate and its dependencies: https://crates.io/crates/petgraph/reverse_dependencies and it looks fine to add to me.
   
   Given we already have a dependency on parking_lot I don't think this is a net new dependency, and furthermore it seems widely used across the Rust ecosystem. 



##########
datafusion/core/src/physical_optimizer/pipeline_fixer.rs:
##########
@@ -77,6 +87,104 @@ impl PhysicalOptimizerRule for PipelineFixer {
     }
 }
 
+/// Indicates whether interval arithmetic is supported for the given expression.
+/// Currently, we do not support all [PhysicalExpr]s for interval calculations.
+/// We do not support every type of [Operator]s either. Over time, this check
+/// will relax as more types of [PhysicalExpr]s and [Operator]s are supported.
+/// Currently, [CastExpr], [BinaryExpr], [Column] and [Literal] is supported.
+fn check_support(expr: &Arc<dyn PhysicalExpr>) -> bool {
+    let expr_any = expr.as_any();
+    let expr_supported = if let Some(binary_expr) = expr_any.downcast_ref::<BinaryExpr>()
+    {
+        is_operator_supported(binary_expr.op())
+    } else {
+        expr_any.is::<Column>() || expr_any.is::<Literal>() || expr_any.is::<CastExpr>()
+    };
+    expr_supported && expr.children().iter().all(check_support)
+}
+
+/// This function returns whether a given hash join is replaceable by a
+/// symmetric hash join. Basically, the requirement is that involved
+/// [PhysicalExpr]s, [Operator]s and data types need to be supported,
+/// and order information must cover every column in the filter expression.
+fn is_suitable_for_symmetric_hash_join(hash_join: &HashJoinExec) -> Result<bool> {
+    if let Some(filter) = hash_join.filter() {
+        let left = hash_join.left();
+        if let Some(left_ordering) = left.output_ordering() {
+            let right = hash_join.right();
+            if let Some(right_ordering) = right.output_ordering() {
+                let expr_supported = check_support(filter.expression());
+                let left_convertible = convert_sort_expr_with_filter_schema(
+                    &JoinSide::Left,
+                    filter,
+                    &left.schema(),
+                    &left_ordering[0],
+                )?
+                .is_some();
+                let right_convertible = convert_sort_expr_with_filter_schema(
+                    &JoinSide::Right,
+                    filter,
+                    &right.schema(),
+                    &right_ordering[0],
+                )?
+                .is_some();
+                let fields_supported = filter
+                    .schema()
+                    .fields()
+                    .iter()
+                    .all(|f| is_datatype_supported(f.data_type()));
+                return Ok(expr_supported
+                    && fields_supported
+                    && left_convertible
+                    && right_convertible);
+            }
+        }
+    }
+    Ok(false)
+}
+
+/// This subrule checks if one can replace a hash join with a symmetric hash
+/// join so that the pipeline does not break due to the join operation in
+/// question. If possible, it makes this replacement; otherwise, it has no
+/// effect.
+fn hash_join_convert_symmetric_subrule(
+    input: PipelineStatePropagator,
+) -> Option<Result<PipelineStatePropagator>> {
+    let plan = input.plan;
+    if let Some(hash_join) = plan.as_any().downcast_ref::<HashJoinExec>() {
+        let ub_flags = input.children_unbounded;
+        let (left_unbounded, right_unbounded) = (ub_flags[0], ub_flags[1]);

Review Comment:
   I wonder why this PR limits the use of symmetric hash join to unbounded streams? It seems it could be used for any input where the sort conditions are reasonable, given
   
   1. the very good numbers reported in https://synnada.notion.site/synnada/General-purpose-Stream-Joins-via-Pruning-Symmetric-Hash-Joins-2fe26d3127a241e294a0217b1f18603a 
   2. the fact that this algorithm is general for any sorted stream (not just the unbounded ones)



##########
datafusion/physical-expr/src/intervals/mod.rs:
##########
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one

Review Comment:
   This is a neat approach; However, I am concerned that with it we wll have three partially complete range analysis implementations:
   
   # Range analysis used in cardinality estimation 
   https://docs.rs/datafusion-physical-expr/18.0.0/datafusion_physical_expr/trait.PhysicalExpr.html#method.analyze
   https://docs.rs/datafusion-physical-expr/18.0.0/datafusion_physical_expr/struct.AnalysisContext.html
   (cc @isidentical)
   
   # Pruning Predicate
   https://docs.rs/datafusion/18.0.0/datafusion/physical_optimizer/pruning/struct.PruningPredicate.html
   
   The modules are all structured a differently / have different implementations but what they are computing (interval / range analysis) is the same
   
   Did you consider the other implementations? Would you be willing to help unify the implementations (I don't really have a preference as to which, only that we don't have three)



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