Skip to content

Commit

Permalink
ORCA produces bogus plan for queries with CTE during handling distrib…
Browse files Browse the repository at this point in the history
…ution for Sequence children

The condition for checking the requested distribution does not work
in case of inlining the outer CTE by a filter, because the filter replaces
the requested distribution.

Therefore, for cases of CTE inlining by a filter, I added a flag that remembers
this, and then if this flag is set, then the filter passes the requested
distribution, and does not replace it.
  • Loading branch information
RekGRpth committed Jun 30, 2023
1 parent 4fa8a09 commit 26e3b52
Show file tree
Hide file tree
Showing 30 changed files with 1,142 additions and 329 deletions.
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/CTE-4.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="21">
<dxl:Plan Id="0" SpaceSize="16">
<dxl:GatherMotion InputSegments="0,1" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1293.003882" Rows="10.000000" Width="16"/>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/CTE-Preds1.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="240">
<dxl:Plan Id="0" SpaceSize="720">
<dxl:GatherMotion InputSegments="0,1" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2155.000518" Rows="2.000000" Width="16"/>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/CTE-Preds2.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="1818">
<dxl:Plan Id="0" SpaceSize="2188">
<dxl:GatherMotion InputSegments="0,1" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2586.000757" Rows="3.000000" Width="16"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ SELECT * FROM T2;
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="1">
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Result>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="0.000032" Rows="2.000000" Width="8"/>
Expand Down
259 changes: 140 additions & 119 deletions src/backend/gporca/data/dxl/minidump/CTE15HAReplicated.mdp

Large diffs are not rendered by default.

259 changes: 140 additions & 119 deletions src/backend/gporca/data/dxl/minidump/CTE15Replicated.mdp

Large diffs are not rendered by default.

728 changes: 728 additions & 0 deletions src/backend/gporca/data/dxl/minidump/CTE25Replicated.mdp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
<dxl:LogicalCTEConsumer CTEId="1" Columns="19,20,21,22"/>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="10">
<dxl:Plan Id="0" SpaceSize="12">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="862.000587" Rows="1.000000" Width="16"/>
Expand Down
26 changes: 11 additions & 15 deletions src/backend/gporca/data/dxl/minidump/CTEWithMergedGroup.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,25 @@
</dxl:LogicalProject>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="41793672096">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Plan Id="0" SpaceSize="83636818464">
<dxl:Result>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1356250698.553420" Rows="1.000000" Width="4"/>
<dxl:Cost StartupCost="0" TotalCost="1356250698.553411" Rows="1.000000" Width="4"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="41" Alias="?column?">
<dxl:Ident ColId="41" ColName="?column?" TypeMdid="0.23.1.0"/>
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:Filter/>
<dxl:SortingColumnList/>
<dxl:Result>
<dxl:OneTimeFilter/>
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1356250698.553405" Rows="1.000000" Width="4"/>
<dxl:Cost StartupCost="0" TotalCost="1356250698.553407" Rows="1.000000" Width="1"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="41" Alias="?column?">
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:ProjList/>
<dxl:Filter/>
<dxl:OneTimeFilter/>
<dxl:SortingColumnList/>
<dxl:HashJoin JoinType="LeftAntiSemiJoin">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1356250698.553403" Rows="1.000000" Width="1"/>
Expand Down Expand Up @@ -925,8 +921,8 @@
</dxl:Result>
</dxl:BroadcastMotion>
</dxl:HashJoin>
</dxl:Result>
</dxl:GatherMotion>
</dxl:GatherMotion>
</dxl:Result>
</dxl:Plan>
</dxl:Thread>
</dxl:DXLMessage>
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/CTEinlining.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ explain with v as (select x,y from bar) select v1.x from v v1, v v2 where v1.x =
</dxl:LogicalJoin>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="44">
<dxl:Plan Id="0" SpaceSize="52">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="862.000443" Rows="1.000000" Width="4"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Plan Id="0" SpaceSize="6">
<dxl:Sequence>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2648064.219082" Rows="5.000000" Width="8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Plan Id="0" SpaceSize="6">
<dxl:Sequence>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2648064.154894" Rows="4.000000" Width="8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Plan Id="0" SpaceSize="6">
<dxl:Sequence>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2648064.111144" Rows="3.000000" Width="8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ SELECT a FROM y WHERE (a IN (SELECT b FROM bar WHERE b = 2));
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="209">
<dxl:Plan Id="0" SpaceSize="342">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="868.000741" Rows="1.000000" Width="4"/>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/NLJ-Rewindability.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="432">
<dxl:Plan Id="0" SpaceSize="648">
<dxl:Sequence>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1324033.254833" Rows="1.000000" Width="8"/>
Expand Down
23 changes: 10 additions & 13 deletions src/backend/gporca/data/dxl/minidump/NoRedistributeOnAppend.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -425,35 +425,32 @@ GROUP BY b;
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="28999731200">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Plan Id="0" SpaceSize="57999462400">
<dxl:Result>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2155.000796" Rows="1.000000" Width="8"/>
<dxl:Cost StartupCost="0" TotalCost="2155.000786" Rows="1.000000" Width="8"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="48" Alias="b">
<dxl:Ident ColId="48" ColName="b" TypeMdid="0.23.1.0"/>
</dxl:ProjElem>
<dxl:ProjElem ColId="96" Alias="c">
<dxl:Ident ColId="96" ColName="c" TypeMdid="0.23.1.0"/>
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:Filter/>
<dxl:SortingColumnList/>
<dxl:Result>
<dxl:OneTimeFilter/>
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2155.000766" Rows="1.000000" Width="8"/>
<dxl:Cost StartupCost="0" TotalCost="2155.000778" Rows="1.000000" Width="4"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="48" Alias="b">
<dxl:Ident ColId="48" ColName="b" TypeMdid="0.23.1.0"/>
</dxl:ProjElem>
<dxl:ProjElem ColId="96" Alias="c">
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:Filter/>
<dxl:OneTimeFilter/>
<dxl:SortingColumnList/>
<dxl:Aggregate AggregationStrategy="Sorted" StreamSafe="false">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="2155.000764" Rows="1.000000" Width="4"/>
Expand Down Expand Up @@ -942,8 +939,8 @@ GROUP BY b;
</dxl:Append>
</dxl:Sort>
</dxl:Aggregate>
</dxl:Result>
</dxl:GatherMotion>
</dxl:GatherMotion>
</dxl:Result>
</dxl:Plan>
</dxl:Thread>
</dxl:DXLMessage>
Original file line number Diff line number Diff line change
Expand Up @@ -1120,29 +1120,25 @@
</dxl:LogicalProject>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="1">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Result>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="431.024947" Rows="161.400000" Width="4"/>
<dxl:Cost StartupCost="0" TotalCost="431.023573" Rows="161.400000" Width="4"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="20" Alias="?column?">
<dxl:Ident ColId="20" ColName="?column?" TypeMdid="0.23.1.0"/>
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:Filter/>
<dxl:SortingColumnList/>
<dxl:Result>
<dxl:OneTimeFilter/>
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="431.022541" Rows="161.400000" Width="4"/>
<dxl:Cost StartupCost="0" TotalCost="431.022927" Rows="161.400000" Width="1"/>
</dxl:Properties>
<dxl:ProjList>
<dxl:ProjElem ColId="20" Alias="?column?">
<dxl:ConstValue TypeMdid="0.23.1.0" Value="1"/>
</dxl:ProjElem>
</dxl:ProjList>
<dxl:ProjList/>
<dxl:Filter/>
<dxl:OneTimeFilter/>
<dxl:SortingColumnList/>
<dxl:Result>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="431.022326" Rows="161.400000" Width="1"/>
Expand Down Expand Up @@ -1224,8 +1220,8 @@
</dxl:TableScan>
</dxl:Result>
</dxl:Result>
</dxl:Result>
</dxl:GatherMotion>
</dxl:GatherMotion>
</dxl:Result>
</dxl:Plan>
</dxl:Thread>
</dxl:DXLMessage>
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@
</dxl:UnionAll>
</dxl:LogicalSelect>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Plan Id="0" SpaceSize="1">
<dxl:GatherMotion InputSegments="0,1" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1074.205091" Rows="998707.000000" Width="8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@
</dxl:LogicalJoin>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="8">
<dxl:Plan Id="0" SpaceSize="10">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="862.030854" Rows="100.000001" Width="16"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
</dxl:LogicalCTEAnchor>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="10">
<dxl:Plan Id="0" SpaceSize="6">
<dxl:Sequence>
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="1293.000999" Rows="1.000000" Width="24"/>
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gporca/data/dxl/minidump/UnionOfDQAQueries.mdp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
</dxl:LogicalGroupBy>
</dxl:Union>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="62776">
<dxl:Plan Id="0" SpaceSize="49476">
<dxl:GatherMotion InputSegments="0,1" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="862.002353" Rows="13.000000" Width="8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ with x as (select a a1, a a2, b from foo) select * from x where a1 = 1;
</dxl:LogicalSelect>
</dxl:LogicalCTEAnchor>
</dxl:Query>
<dxl:Plan Id="0" SpaceSize="2">
<dxl:Plan Id="0" SpaceSize="3">
<dxl:GatherMotion InputSegments="0,1,2" OutputSegments="-1">
<dxl:Properties>
<dxl:Cost StartupCost="0" TotalCost="431.000117" Rows="1.000000" Width="12"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ class CLogicalSelect : public CLogicalUnary
// table descriptor
CTableDescriptor *m_ptabdesc;

BOOL m_fake;

public:
// ctor
explicit CLogicalSelect(CMemoryPool *mp);

// ctor
CLogicalSelect(CMemoryPool *mp, CTableDescriptor *ptabdesc);

// ctor
CLogicalSelect(CMemoryPool *mp, BOOL fake);

// dtor
virtual ~CLogicalSelect();

Expand All @@ -72,6 +77,12 @@ class CLogicalSelect : public CLogicalUnary
return m_ptabdesc;
}

BOOL
Fake() const
{
return m_fake;
}

//-------------------------------------------------------------------------------------
// Derived Relational Properties
//-------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ class CPhysicalFilter : public CPhysical
// private copy ctor
CPhysicalFilter(const CPhysicalFilter &);

BOOL m_fake;

public:
// ctor
explicit CPhysicalFilter(CMemoryPool *mp);

explicit CPhysicalFilter(CMemoryPool *mp, BOOL fake);

// dtor
virtual ~CPhysicalFilter();

Expand All @@ -51,6 +55,12 @@ class CPhysicalFilter : public CPhysical
return "CPhysicalFilter";
}

BOOL
Fake() const
{
return m_fake;
}

// match function
virtual BOOL Matches(COperator *pop) const;

Expand Down
10 changes: 8 additions & 2 deletions src/backend/gporca/libgpopt/src/operators/CLogicalSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ using namespace gpopt;
//
//---------------------------------------------------------------------------
CLogicalSelect::CLogicalSelect(CMemoryPool *mp)
: CLogicalUnary(mp), m_ptabdesc(NULL)
: CLogicalUnary(mp), m_ptabdesc(NULL), m_fake(false)
{
m_phmPexprPartPred = GPOS_NEW(mp) ExprPredToExprPredPartMap(mp);
}

CLogicalSelect::CLogicalSelect(CMemoryPool *mp, CTableDescriptor *ptabdesc)
: CLogicalUnary(mp), m_ptabdesc(ptabdesc)
: CLogicalUnary(mp), m_ptabdesc(ptabdesc), m_fake(false)
{
m_phmPexprPartPred = GPOS_NEW(mp) ExprPredToExprPredPartMap(mp);
}

CLogicalSelect::CLogicalSelect(CMemoryPool *mp, BOOL fake)
: CLogicalUnary(mp), m_ptabdesc(NULL), m_fake(fake)
{
m_phmPexprPartPred = GPOS_NEW(mp) ExprPredToExprPredPartMap(mp);
}
Expand Down
Loading

0 comments on commit 26e3b52

Please sign in to comment.