Skip to content

Commit 21c03c6

Browse files
committed
ORCA: Support pushdown partial aggregate below join
1 parent e601fb6 commit 21c03c6

34 files changed

+1909
-292
lines changed

src/backend/gporca/libgpopt/include/gpopt/operators/CLogicalGbAgg.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ class CLogicalGbAgg : public CLogicalUnary
131131
return m_fGeneratesDuplicates;
132132
}
133133

134+
// dose this aggregate generated by pushdown
135+
BOOL
136+
FAggPushdown() const
137+
{
138+
return m_aggpushdown;
139+
}
140+
141+
// this aggregate generated by pushdown
142+
void
143+
MarkAggPushdown()
144+
{
145+
m_aggpushdown = true;
146+
}
147+
134148
// match function
135149
BOOL Matches(COperator *pop) const override;
136150

@@ -274,6 +288,8 @@ class CLogicalGbAgg : public CLogicalUnary
274288
// which type of multi-stage agg it is
275289
EAggStage m_aggStage;
276290

291+
// is current agg pushdown?
292+
BOOL m_aggpushdown;
277293
}; // class CLogicalGbAgg
278294

279295
} // namespace gpopt

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalAgg.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class CPhysicalAgg : public CPhysical
4444

4545
CLogicalGbAgg::EAggStage m_aggStage;
4646

47+
// is aggregate gernerated by pushdown
48+
BOOL m_aggpushdown;
49+
4750
// compute required distribution of the n-th child of an intermediate aggregate
4851
CDistributionSpec *PdsRequiredIntermediateAgg(CMemoryPool *mp,
4952
ULONG ulOptReq) const;
@@ -111,7 +114,7 @@ class CPhysicalAgg : public CPhysical
111114
COperator::EGbAggType egbaggtype, BOOL fGeneratesDuplicates,
112115
CColRefArray *pdrgpcrArgDQA, BOOL fMultiStage,
113116
BOOL isAggFromSplitDQA, CLogicalGbAgg::EAggStage aggStage,
114-
BOOL should_enforce_distribution);
117+
BOOL isAggPushdown, BOOL should_enforce_distribution);
115118

116119
// is this agg generated by CXformSplitDQA
117120
BOOL IsAggFromSplitDQA() const;

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalHashAgg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CPhysicalHashAgg : public CPhysicalAgg
4141
BOOL fGeneratesDuplicates, CColRefArray *pdrgpcrArgDQA,
4242
BOOL fMultiStage, BOOL isAggFromSplitDQA,
4343
CLogicalGbAgg::EAggStage aggStage,
44-
BOOL should_enforce_distribution = true
44+
BOOL isAggPushdown, BOOL should_enforce_distribution
4545
// should_enforce_distribution should be set to false if
4646
// 'local' and 'global' splits don't need to have different
4747
// distributions. This flag is set to false if the local

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalHashAggDeduplicate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CPhysicalHashAggDeduplicate : public CPhysicalHashAgg
4242
BOOL fGeneratesDuplicates, BOOL fMultiStage,
4343
BOOL isAggFromSplitDQA,
4444
CLogicalGbAgg::EAggStage aggStage,
45+
BOOL isAggPushdown,
4546
BOOL should_enforce_distribution);
4647

4748
// dtor

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalScalarAgg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class CPhysicalScalarAgg : public CPhysicalAgg
4040
CColRefArray *pdrgpcrMinimal, // minimal grouping columns based on FD's
4141
COperator::EGbAggType egbaggtype, BOOL fGeneratesDuplicates,
4242
CColRefArray *pdrgpcrArgDQA, BOOL fMultiStage, BOOL isAggFromSplitDQA,
43-
CLogicalGbAgg::EAggStage aggStage, BOOL should_enforce_distribution);
43+
CLogicalGbAgg::EAggStage aggStage, BOOL isAggPushdown,
44+
BOOL should_enforce_distribution);
4445

4546
// dtor
4647
~CPhysicalScalarAgg() override;

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalStreamAgg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class CPhysicalStreamAgg : public CPhysicalAgg
6060
CColRefArray *pdrgpcrMinimal, // minimal grouping columns based on FD's
6161
COperator::EGbAggType egbaggtype, BOOL fGeneratesDuplicates,
6262
CColRefArray *pdrgpcrArgDQA, BOOL fMultiStage, BOOL isAggFromSplitDQA,
63-
CLogicalGbAgg::EAggStage aggStage,
64-
BOOL should_enforce_distribution = true
63+
CLogicalGbAgg::EAggStage aggStage, BOOL isAggPushdown,
64+
BOOL should_enforce_distribution
6565
// should_enforce_distribution should be set to false if
6666
// 'local' and 'global' splits don't need to have different
6767
// distributions. This flag is set to false if the local

src/backend/gporca/libgpopt/include/gpopt/operators/CPhysicalStreamAggDeduplicate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CPhysicalStreamAggDeduplicate : public CPhysicalStreamAgg
4343
BOOL fGeneratesDuplicates, BOOL fMultiStage,
4444
BOOL isAggFromSplitDQA,
4545
CLogicalGbAgg::EAggStage aggStage,
46+
BOOL isAggPushdown,
4647
BOOL should_enforce_distribution);
4748

4849
// dtor

src/backend/gporca/libgpopt/include/gpopt/xforms/CXform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class CXform : public CRefCount, public DbgPrintMixin<CXform>
165165
ExfPushGbWithHavingBelowJoin,
166166
ExfPushGbBelowUnion,
167167
ExfPushGbBelowUnionAll,
168+
ExfPushPartialAggBelowJoin,
168169
ExfSplitGbAgg,
169170
ExfSplitGbAggDedup,
170171
ExfSplitDQA,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//---------------------------------------------------------------------------
2+
// Greenplum Database
3+
// Copyright (C) 2012 EMC Corp.
4+
//
5+
// @filename:
6+
// CXformPushPartialAggBelowJoin.h
7+
//
8+
// @doc:
9+
// Push group by below join transform
10+
//---------------------------------------------------------------------------
11+
#ifndef GPOPT_CXformPushPartialAggBelowJoin_H
12+
#define GPOPT_CXformPushPartialAggBelowJoin_H
13+
14+
#include <tuple>
15+
16+
#include "gpos/base.h"
17+
18+
#include "gpopt/xforms/CXformExploration.h"
19+
namespace gpopt
20+
{
21+
using namespace gpos;
22+
23+
//---------------------------------------------------------------------------
24+
// @class:
25+
// CXformPushPartialAggBelowJoin
26+
//
27+
// @doc:
28+
// Push group by below join transform
29+
//
30+
//---------------------------------------------------------------------------
31+
class CXformPushPartialAggBelowJoin : public CXformExploration
32+
{
33+
private:
34+
static BOOL FLocalGbAggAlreadyPushed(CExpression *pexprGlobalGb);
35+
36+
static std::pair<BOOL, BOOL> FCanPushLocalGbAggBelowJoin(
37+
CMemoryPool *mp, CExpression *pexpr);
38+
39+
static CExpression *ExchangeGbkeyFromJoinKey(CMemoryPool *mp,
40+
CExpression *pexpr);
41+
static CColRefSet *PcrsJoinKey(CMemoryPool *mp, CExpression *pexprOuter,
42+
CExpression *pexprInner,
43+
CExpression *pexprScalar);
44+
45+
static CExpression *PushLocalGbAggBelowJoin(CMemoryPool *mp,
46+
CExpression *pexprGlobalGb);
47+
48+
static CColRefSet *PexprGetGbAggkey(CMemoryPool *mp,
49+
CExpression *pexprGbAgg);
50+
51+
public:
52+
CXformPushPartialAggBelowJoin(const CXformPushPartialAggBelowJoin &) =
53+
delete;
54+
55+
// ctor
56+
explicit CXformPushPartialAggBelowJoin(CMemoryPool *mp);
57+
58+
// ctor
59+
explicit CXformPushPartialAggBelowJoin(CExpression *pexprPattern);
60+
61+
// dtor
62+
~CXformPushPartialAggBelowJoin() override = default;
63+
64+
// ident accessors
65+
EXformId
66+
Exfid() const override
67+
{
68+
return ExfPushPartialAggBelowJoin;
69+
}
70+
71+
const CHAR *
72+
SzId() const override
73+
{
74+
return "CXformPushPartialAggBelowJoin";
75+
}
76+
77+
// Compatibility function
78+
BOOL
79+
FCompatible(CXform::EXformId exfid) override
80+
{
81+
return (CXform::ExfPushPartialAggBelowJoin != exfid);
82+
}
83+
84+
// compute xform promise for a given expression handle
85+
EXformPromise Exfp(CExpressionHandle &exprhdl) const override;
86+
87+
// actual transform
88+
void Transform(CXformContext *pxfctxt, CXformResult *pxfres,
89+
CExpression *pexpr) const override;
90+
91+
}; // class CXformPushPartialAggBelowJoin
92+
93+
} // namespace gpopt
94+
95+
#endif // !GPOPT_CXformPushPartialAggBelowJoin_H
96+
97+
// EOF

src/backend/gporca/libgpopt/include/gpopt/xforms/CXformSplitDQA.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class CXformSplitDQA : public CXformExploration
117117
FCompatible(CXform::EXformId exfid) override
118118
{
119119
return (CXform::ExfSplitDQA != exfid) &&
120-
(CXform::ExfSplitGbAgg != exfid);
120+
(CXform::ExfSplitGbAgg != exfid) &&
121+
(CXform::ExfPushPartialAggBelowJoin != exfid);
121122
}
122123

123124
// compute xform promise for a given expression handle

0 commit comments

Comments
 (0)