You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by ta...@apache.org on 2021/02/12 17:32:27 UTC

[impala] branch master updated: IMPALA-4805: Avoid hash exchange before analytic function if appropriate

This is an automated email from the ASF dual-hosted git repository.

tarmstrong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git


The following commit(s) were added to refs/heads/master by this push:
     new 4721978  IMPALA-4805: Avoid hash exchange before analytic function if appropriate
4721978 is described below

commit 4721978e8fb6d80a9f023e568b983b12b14f8acc
Author: Aman Sinha <am...@cloudera.com>
AuthorDate: Thu Feb 11 14:44:08 2021 -0800

    IMPALA-4805: Avoid hash exchange before analytic function if appropriate
    
    This patch avoids adding a hash exchange below an analytic function
    that has partition by b as long as the child can satisfy that
    requirement through an equivalence relationship .. i.e an exact match is
    not required.
    
    For example:
    select count(*) over (partition by b) from t1, t2 where a = b
    
    In this case, the analytic sort has a required partitioning on b but the
    child is an inner join whose output partition key could be either 'a' or
    'b' (it happens to be 'a' given how the data partition was populated),
    then we should still be able to use the child's partitioning without
    adding a hash exchange. Note that for outer joins the logic is slightly
    different.
    
    Testing:
     - Added a new planner test with analytic function + inner join
       (outer join test case already existed before).
    
    Change-Id: Icb6289d1e70cfb6bbd5b38eedb00856dbc85ac77
    Reviewed-on: http://gerrit.cloudera.org:8080/16888
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 .../apache/impala/planner/DistributedPlanner.java  | 21 +++++++++++-
 .../queries/PlannerTest/analytic-fns.test          | 38 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/fe/src/main/java/org/apache/impala/planner/DistributedPlanner.java b/fe/src/main/java/org/apache/impala/planner/DistributedPlanner.java
index 8a75f9d..46242e9 100644
--- a/fe/src/main/java/org/apache/impala/planner/DistributedPlanner.java
+++ b/fe/src/main/java/org/apache/impala/planner/DistributedPlanner.java
@@ -1085,7 +1085,26 @@ public class DistributedPlanner {
           childFragment.getPlanRoot().getOutputSmap(), ctx_.getRootAnalyzer());
       // Make sure the childFragment's output is partitioned as required by the sortNode.
       DataPartition sortPartition = sortNode.getInputPartition();
-      if (!childFragment.getDataPartition().equals(sortPartition)) {
+      boolean hasNullableTupleIds = childFragment.getPlanRoot().
+          getNullableTupleIds().size() > 0;
+      boolean hasCompatiblePartition = false;
+      if (hasNullableTupleIds) {
+        // If the input stream has nullable tuple ids (produced from the nullable
+        // side of an outer join), do an exact equality comparison of the child's
+        // data partition with the required partition) since a hash exchange is
+        // required to co-locate all the null values produced from the outer join
+        // (these tuples may not be originally null but became null after OJ).
+        hasCompatiblePartition = childFragment.getDataPartition().equals(sortPartition);
+      } else {
+        // Otherwise, a mutual value transfer is sufficient for compatible partitions.
+        // E.g if analytic fragment's required partition key is t2.a2 and the child is
+        // an inner join with t1.a1 = t2.a2, either a1 or a2 are sufficient to satisfy
+        // the required partitioning.
+        hasCompatiblePartition = ctx_.getRootAnalyzer().setsHaveValueTransfer(
+            childFragment.getDataPartition().getPartitionExprs(),
+            sortPartition.getPartitionExprs(), true);
+      }
+      if (!hasCompatiblePartition) {
         if (sortNode.isTypeTopN() || sortNode.isPartitionedTopN()) {
           lowerTopN = sortNode;
           childFragment.addPlanRoot(lowerTopN);
diff --git a/testdata/workloads/functional-planner/queries/PlannerTest/analytic-fns.test b/testdata/workloads/functional-planner/queries/PlannerTest/analytic-fns.test
index 5253b09..d74fd9e 100644
--- a/testdata/workloads/functional-planner/queries/PlannerTest/analytic-fns.test
+++ b/testdata/workloads/functional-planner/queries/PlannerTest/analytic-fns.test
@@ -3343,3 +3343,41 @@ PLAN-ROOT SINK
    HDFS partitions=24/24 files=24 size=478.45KB
    row-size=0B cardinality=7.30K
 ====
+# IMPALA-4805: Avoid hash exchange below analytic operator since join
+# output is already hash partitioned
+select [straight_join] count(*) over (partition by t2.id)
+from functional.alltypes t1
+inner join [shuffle] functional.alltypes t2
+  on t1.id = t2.id
+---- DISTRIBUTEDPLAN
+PLAN-ROOT SINK
+|
+07:EXCHANGE [UNPARTITIONED]
+|
+04:ANALYTIC
+|  functions: count(*)
+|  partition by: t2.id
+|  row-size=16B cardinality=7.30K
+|
+03:SORT
+|  order by: id ASC NULLS LAST
+|  row-size=8B cardinality=7.30K
+|
+02:HASH JOIN [INNER JOIN, PARTITIONED]
+|  hash predicates: t1.id = t2.id
+|  runtime filters: RF000 <- t2.id
+|  row-size=8B cardinality=7.30K
+|
+|--06:EXCHANGE [HASH(t2.id)]
+|  |
+|  01:SCAN HDFS [functional.alltypes t2]
+|     HDFS partitions=24/24 files=24 size=478.45KB
+|     row-size=4B cardinality=7.30K
+|
+05:EXCHANGE [HASH(t1.id)]
+|
+00:SCAN HDFS [functional.alltypes t1]
+   HDFS partitions=24/24 files=24 size=478.45KB
+   runtime filters: RF000 -> t1.id
+   row-size=4B cardinality=7.30K
+====