You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by av...@apache.org on 2024/12/27 11:17:42 UTC

(cloudberry) 03/13: Fix incorrect hash-key of Redistribute-Motion when creating path for multi-DQA expr. (#14135)

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

avamingli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 4106ae50e9bcc6b6da958d241332c0f39e623c41
Author: Xing Guo <hi...@gmail.com>
AuthorDate: Thu Sep 22 08:48:26 2022 +0800

    Fix incorrect hash-key of Redistribute-Motion when creating path for multi-DQA expr. (#14135)
    
    This patch is to fix #14096.
    
    How does multi-DQA work:
    
    When creating path for multi-DQA (multiple distinct qualified
    aggregate), the planner generates the plan as follows:
    
    ```
    Finalize Aggregate
      -> Gather Motion
           -> Partial Aggregate
                -> HashAggregate, to remove duplicates
                     -> Redistribute Motion
                          -> TupleSplit (according to DISTINCT expr)
                               -> input
    ```
    
    The plan is generated via the following steps:
    
    1. The planner compares the expr in DQA with the input tuples, if the
       DQA expr is not in PathTarget, the TupleSplit node will append the
       corresponding column the the PathTarget together with a special
       column 'AggExprId' to the output tuple. E.g., if the input of
       TupleSplit is '(a: 1, b: 2, c: 3)' and the DQA expressions are
       'DQA(a)', 'DQA(b)', the output of TupleSplit are
       '(a: 1, b: null, c: 3, AggExprId: 1)',
       '(a: null, b: 2, c: 3, AggExprId: 2)'. Only one of the DQA expr's
       column is set to non-null per tuple and the AggExprId is used to
       indicate which DQA expr's column is non-null in that tuple.
    
    2. The output tuples of TupleSplit are hashed and sent to different
       segments via Redistribute-Motion. Because in the previous stage, only
       one of the DQA expr's column is set to non-null per tuple, DQA expr
       with same value is gathered on the same segment and we can apply
       HashAggregate on these tuples to remove duplicate values.
    
    3. We apply Partial Aggregate function on tuples across segments.
    
    4. We gather tuples to QD and apply the Finalize Aggregate function on
       tuples.
    
    The RCA is given below:
    
    1. When there're type conversions in DQA exprs, the planner will wrap
       the expr with type conversion node. E.g., for binary-compatible
       datatypes, like 'VARCHAR' and 'TEXT' the expr will be wrapped with
       'ReplabelType' node.
    
    2. When there are queries like 'SELECT DQA(a::TEXT) FROM foo(a
       VARCHAR)', the planner compares the DQA expr with exprs in PathTarget
       directly without removing the 'RelabelType' node. So, the TupleSplit
       looks like:
    
       ```
       TupleSplit:
         Input: a
         Output: a, a::TEXT
       ```
    
    3. When the planner generating the Redistribute-Motion operator, it
       tries to find the expr of equivalent class with the expr in targetlist
       as the hashkey (src/backend/cdb/cdbpullup.c:cdbpullup_findEclassInTargetList).
       However, in this stage, the planner will ignore the 'RelabelType' when
       comparing two exprs. Finally, the planner may choose incorrect column
       as the hash key.
    
    This patch helps resolve the issue by teaching 'TupleSplit' node to
    ignore 'RelabelType' node when comparing DQA expr and the one in
    PathTarget. So the TupleSplit node looks like:
    
    ```
    TupleSplit:
      Input: a
      Output: a
    ```
    
    when planning queries like: 'SELECT DQA(a::TEXT) FROM foo(a VARCHAR)'.
---
 src/test/regress/expected/gp_dqa.out           | 6 +++---
 src/test/regress/expected/gp_dqa_optimizer.out | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/test/regress/expected/gp_dqa.out b/src/test/regress/expected/gp_dqa.out
index 9090a1547a..91b38ae965 100644
--- a/src/test/regress/expected/gp_dqa.out
+++ b/src/test/regress/expected/gp_dqa.out
@@ -2247,11 +2247,11 @@ explain (verbose, costs off) select count(distinct (b)::text) as b, count(distin
                                                 QUERY PLAN
 -----------------------------------------------------------------------------------------------------------
  Finalize Aggregate
-   Output: count(DISTINCT ((b)::text)), count(DISTINCT a)
+   Output: count(DISTINCT ((b)::text)), count(DISTINCT (a)::character varying)
    ->  Gather Motion 3:1  (slice1; segments: 3)
-         Output: (PARTIAL count(DISTINCT ((b)::text))), (PARTIAL count(DISTINCT a))
+         Output: (PARTIAL count(DISTINCT ((b)::text))), (PARTIAL count(DISTINCT (a)::character varying))
          ->  Partial Aggregate
-               Output: PARTIAL count(DISTINCT ((b)::text)), PARTIAL count(DISTINCT a)
+               Output: PARTIAL count(DISTINCT ((b)::text)), PARTIAL count(DISTINCT (a)::character varying)
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, a, ((b)::text), (AggExprId)
                      Hash Key: ((b)::text), a, (AggExprId)
diff --git a/src/test/regress/expected/gp_dqa_optimizer.out b/src/test/regress/expected/gp_dqa_optimizer.out
index 43e023d100..73f7dfb1b8 100644
--- a/src/test/regress/expected/gp_dqa_optimizer.out
+++ b/src/test/regress/expected/gp_dqa_optimizer.out
@@ -2386,11 +2386,11 @@ DETAIL:  Feature not supported: Multiple Distinct Qualified Aggregates are disab
                                                  QUERY PLAN
 -------------------------------------------------------------------------------------------------------------
  Finalize Aggregate
-   Output: count(DISTINCT ((b)::text)), count(DISTINCT a)
+   Output: count(DISTINCT ((b)::text)), count(DISTINCT (a)::character varying)
    ->  Gather Motion 3:1  (slice1; segments: 3)
-         Output: (PARTIAL count(DISTINCT ((b)::text))), (PARTIAL count(DISTINCT a))
+         Output: (PARTIAL count(DISTINCT ((b)::text))), (PARTIAL count(DISTINCT (a)::character varying))
          ->  Partial Aggregate
-               Output: PARTIAL count(DISTINCT ((b)::text)), PARTIAL count(DISTINCT a)
+               Output: PARTIAL count(DISTINCT ((b)::text)), PARTIAL count(DISTINCT (a)::character varying)
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, a, ((b)::text), (AggExprId)
                      Hash Key: ((b)::text), a, (AggExprId)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org