Skip to content

Commit

Permalink
[ORCA]: fallback to postgres optimizer when NL inner plan is index scan
Browse files Browse the repository at this point in the history
ORCA may produce wrong plan for missing motion, see issue 567.
  • Loading branch information
gfphoenix78 authored and my-ship-it committed Aug 20, 2024
1 parent 4b7f9d2 commit fa858b0
Show file tree
Hide file tree
Showing 18 changed files with 1,652 additions and 1,615 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "gpopt/base/CDistributionSpecHashed.h"
#include "gpopt/base/CDistributionSpecNonSingleton.h"
#include "gpopt/base/CDistributionSpecReplicated.h"
#include "gpopt/exception.h"
#include "gpopt/operators/CExpressionHandle.h"
#include "gpopt/operators/CPredicateUtils.h"

Expand Down Expand Up @@ -118,6 +119,13 @@ CPhysicalInnerIndexNLJoin::Ped(CMemoryPool *mp, CExpressionHandle &exprhdl,
CEnfdDistribution::EDistributionMatching dmatch =
Edm(prppInput, child_index, pdrgpdpCtxt, ulDistrReq);

// FIXME: nestloop with inner index scan may produce wrong plan, see
// issue https://github.com/cloudberrydb/cloudberrydb/issues/567
// Fallback to postgres optimizer to avoid wrong plan. We should
// fix this issue and remove the following exception.
GPOS_RAISE(gpopt::ExmaGPOPT, gpopt::ExmiUnsupportedOp,
GPOS_WSZ_LIT("Fallback: InnerIndexNestLoopJoin may have wrong plan"));

if (1 == child_index)
{
// inner (index-scan side) is requested for Any distribution,
Expand Down
44 changes: 26 additions & 18 deletions src/test/regress/expected/aggregates_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -1346,31 +1346,39 @@ explain (costs off) select a,c from t1 group by a,c,d;
explain (costs off) select *
from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z;
QUERY PLAN
-------------------------------------------------------
INFO: GPORCA failed to produce a plan, falling back to planner
DETAIL: Operator Fallback: InnerIndexNestLoopJoin may have wrong plan not supported
QUERY PLAN
------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
-> Nested Loop
Join Filter: true
-> Seq Scan on t1
-> Index Scan using t2_pkey on t2
Index Cond: ((x = t1.a) AND (y = t1.b))
Optimizer: Pivotal Optimizer (GPORCA)
(7 rows)
-> HashAggregate
Group Key: t1.a, t1.b, t2.x, t2.y
-> Hash Join
Hash Cond: ((t2.x = t1.a) AND (t2.y = t1.b))
-> Seq Scan on t2
-> Hash
-> Seq Scan on t1
Optimizer: Postgres query optimizer
(9 rows)

-- Test case where t1 can be optimized but not t2
explain (costs off) select t1.*,t2.x,t2.z
from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z;
QUERY PLAN
-------------------------------------------------------
INFO: GPORCA failed to produce a plan, falling back to planner
DETAIL: Operator Fallback: InnerIndexNestLoopJoin may have wrong plan not supported
QUERY PLAN
------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
-> Nested Loop
Join Filter: true
-> Seq Scan on t1
-> Index Scan using t2_pkey on t2
Index Cond: ((x = t1.a) AND (y = t1.b))
Optimizer: Pivotal Optimizer (GPORCA)
(7 rows)
-> HashAggregate
Group Key: t1.a, t1.b, t2.x, t2.z
-> Hash Join
Hash Cond: ((t2.x = t1.a) AND (t2.y = t1.b))
-> Seq Scan on t2
-> Hash
-> Seq Scan on t1
Optimizer: Postgres query optimizer
(9 rows)

-- Cannot optimize when PK is deferrable
explain (costs off) select * from t3 group by a,b,c;
Expand Down
322 changes: 98 additions & 224 deletions src/test/regress/expected/bfv_index_optimizer.out

Large diffs are not rendered by default.

56 changes: 26 additions & 30 deletions src/test/regress/expected/co_nestloop_idxscan_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ create index foo_id_idx on co_nestloop_idxscan.foo(id);
explain select f.id from co_nestloop_idxscan.foo f, co_nestloop_idxscan.bar b where f.id = b.id;
QUERY PLAN
-------------------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3) (cost=0.00..499.13 rows=1 width=8)
-> Nested Loop (cost=0.00..499.13 rows=1 width=8)
Join Filter: true
-> Seq Scan on bar (cost=0.00..431.00 rows=1 width=8)
-> Bitmap Heap Scan on foo (cost=0.00..68.13 rows=1 width=8)
Recheck Cond: (id = bar.id)
-> Bitmap Index Scan on foo_id_idx (cost=0.00..0.00 rows=0 width=0)
Index Cond: (id = bar.id)
Optimizer: Pivotal Optimizer (GPORCA) version 3.72.0
(9 rows)
Gather Motion 3:1 (slice1; segments: 3) (cost=1.02..510.33 rows=6 width=8)
-> Hash Join (cost=1.02..510.25 rows=2 width=8)
Hash Cond: (f.id = b.id)
-> Seq Scan on foo f (cost=0.00..509.17 rows=17 width=8)
-> Hash (cost=1.01..1.01 rows=1 width=8)
-> Seq Scan on bar b (cost=0.00..1.01 rows=1 width=8)
Optimizer: Postgres query optimizer
(7 rows)

select f.id from co_nestloop_idxscan.foo f, co_nestloop_idxscan.bar b where f.id = b.id;
id
Expand All @@ -51,16 +49,15 @@ set enable_nestloop=on;
explain select f.id from co_nestloop_idxscan.foo f, co_nestloop_idxscan.bar b where f.id = b.id;
QUERY PLAN
-------------------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3) (cost=0.00..499.13 rows=1 width=8)
-> Nested Loop (cost=0.00..499.13 rows=1 width=8)
Join Filter: true
-> Seq Scan on bar (cost=0.00..431.00 rows=1 width=8)
-> Bitmap Heap Scan on foo (cost=0.00..68.13 rows=1 width=8)
Recheck Cond: (id = bar.id)
-> Bitmap Index Scan on foo_id_idx (cost=0.00..0.00 rows=0 width=0)
Index Cond: (id = bar.id)
Optimizer: Pivotal Optimizer (GPORCA) version 3.72.0
(9 rows)
Gather Motion 3:1 (slice1; segments: 3) (cost=8.15..13.26 rows=6 width=8)
-> Nested Loop (cost=8.15..13.18 rows=2 width=8)
-> Seq Scan on bar b (cost=0.00..1.01 rows=1 width=8)
-> Bitmap Heap Scan on foo f (cost=8.15..12.16 rows=1 width=8)
Recheck Cond: (id = b.id)
-> Bitmap Index Scan on foo_id_idx (cost=0.00..8.15 rows=1 width=0)
Index Cond: (id = b.id)
Optimizer: Postgres query optimizer
(8 rows)

select f.id from co_nestloop_idxscan.foo f, co_nestloop_idxscan.bar b where f.id = b.id;
id
Expand All @@ -76,16 +73,15 @@ set enable_seqscan = off;
explain select f.id from co_nestloop_idxscan.bar b, co_nestloop_idxscan.foo f where f.id = b.id;
QUERY PLAN
-------------------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3) (cost=0.00..499.13 rows=1 width=8)
-> Nested Loop (cost=0.00..499.13 rows=1 width=8)
Join Filter: true
-> Seq Scan on bar (cost=0.00..431.00 rows=1 width=8)
-> Bitmap Heap Scan on foo (cost=0.00..68.13 rows=1 width=8)
Recheck Cond: (id = bar.id)
-> Bitmap Index Scan on foo_id_idx (cost=0.00..0.00 rows=0 width=0)
Index Cond: (id = bar.id)
Optimizer: Pivotal Optimizer (GPORCA) version 3.72.0
(9 rows)
Gather Motion 3:1 (slice1; segments: 3) (cost=10000000008.15..10000000013.26 rows=6 width=8)
-> Nested Loop (cost=10000000008.15..10000000013.18 rows=2 width=8)
-> Seq Scan on bar b (cost=10000000000.00..10000000001.01 rows=1 width=8)
-> Bitmap Heap Scan on foo f (cost=8.15..12.16 rows=1 width=8)
Recheck Cond: (id = b.id)
-> Bitmap Index Scan on foo_id_idx (cost=0.00..8.15 rows=1 width=0)
Index Cond: (id = b.id)
Optimizer: Postgres query optimizer
(8 rows)

select f.id from co_nestloop_idxscan.foo f, co_nestloop_idxscan.bar b where f.id = b.id;
id
Expand Down
Loading

0 comments on commit fa858b0

Please sign in to comment.