diff --git a/src/backend/cdb/cdbpullup.c b/src/backend/cdb/cdbpullup.c index 03730827bc9106b3b525b610cc04824706150482..2f452db87e31228783dbd6a919af9e5f36bf4b75 100644 --- a/src/backend/cdb/cdbpullup.c +++ b/src/backend/cdb/cdbpullup.c @@ -379,52 +379,53 @@ cdbpullup_findPathKeyExprInTargetList(PathKey *item, List *targetlist) ListCell *lc; EquivalenceClass *eclass = item->pk_eclass; - /* Bail if targetlist is empty. */ - if (!targetlist) - return NULL; - foreach(lc, eclass->ec_members) { EquivalenceMember *em = (EquivalenceMember *) lfirst(lc); Expr *key = (Expr *) em->em_expr; - TargetEntry *tle; + /* A constant is OK regardless of the target list */ if (em->em_is_const) return key; - /* Ignore possible RelabelType node atop the PathKey expr. */ - if (IsA(key, RelabelType)) - key = ((RelabelType *)key)->arg; - - /* Check if targetlist is a List of TargetEntry */ - if (IsA(linitial(targetlist), TargetEntry)) - { - tle = tlist_member_ignoring_RelabelType(key, targetlist); - if (tle) - return key; - } - /* Planner's RelOptInfo targetlists don't have TargetEntry nodes */ - else + if (targetlist) { - ListCell *tcell; + /* Ignore possible RelabelType node atop the PathKey expr. */ + if (IsA(key, RelabelType)) + key = ((RelabelType *)key)->arg; - foreach(tcell, targetlist) + /* Check if targetlist is a List of TargetEntry */ + if (IsA(linitial(targetlist), TargetEntry)) { - Expr *expr = (Expr *) lfirst(tcell); + TargetEntry *tle; - if (IsA(expr, RelabelType)) - expr = ((RelabelType *)expr)->arg; - - if (equal(expr, key)) + tle = tlist_member_ignoring_RelabelType(key, targetlist); + if (tle) return key; } - } + /* Planner's RelOptInfo targetlists don't have TargetEntry nodes */ + else + { + ListCell *tcell; - /* Return this item if all referenced Vars are in targetlist. */ - if (!IsA(key, Var) && - !cdbpullup_missingVarWalker((Node *) key, targetlist)) - { - return key; + foreach(tcell, targetlist) + { + Expr *expr = (Expr *) lfirst(tcell); + + if (IsA(expr, RelabelType)) + expr = ((RelabelType *)expr)->arg; + + if (equal(expr, key)) + return key; + } + } + + /* Return this item if all referenced Vars are in targetlist. */ + if (!IsA(key, Var) && + !cdbpullup_missingVarWalker((Node *) key, targetlist)) + { + return key; + } } } diff --git a/src/test/regress/expected/subselect_gp.out b/src/test/regress/expected/subselect_gp.out index 40b6837dde6bc43b91a833ad3aa722833a0a88df..57b9847b09548b58c27d1d741cb75677b2c57138 100644 --- a/src/test/regress/expected/subselect_gp.out +++ b/src/test/regress/expected/subselect_gp.out @@ -1247,3 +1247,16 @@ where s_suppkey in ( foo10 (1 row) +-- +-- Another case that failed at one point. (A planner bug in pulling up a +-- subquery with constant distribution key, 1, in the outer queries.) +-- +create table nested_in_tbl(tc1 int, tc2 int) distributed by (tc1); +select * from nested_in_tbl t1 where tc1 in + (select 1 from nested_in_tbl t2 where tc1 in + (select 1 from nested_in_tbl t3 where t3.tc2 = t2.tc2)); + tc1 | tc2 +-----+----- +(0 rows) + +drop table nested_in_tbl; diff --git a/src/test/regress/expected/subselect_gp_optimizer.out b/src/test/regress/expected/subselect_gp_optimizer.out index 6179fac8d932b48d238b037469d8b16a8371eb02..331ed46e0ca2124b156013078850098cd9fbaa73 100644 --- a/src/test/regress/expected/subselect_gp_optimizer.out +++ b/src/test/regress/expected/subselect_gp_optimizer.out @@ -1260,3 +1260,16 @@ where s_suppkey in ( foo10 (1 row) +-- +-- Another case that failed at one point. (A planner bug in pulling up a +-- subquery with constant distribution key, 1, in the outer queries.) +-- +create table nested_in_tbl(tc1 int, tc2 int) distributed by (tc1); +select * from nested_in_tbl t1 where tc1 in + (select 1 from nested_in_tbl t2 where tc1 in + (select 1 from nested_in_tbl t3 where t3.tc2 = t2.tc2)); + tc1 | tc2 +-----+----- +(0 rows) + +drop table nested_in_tbl; diff --git a/src/test/regress/sql/subselect_gp.sql b/src/test/regress/sql/subselect_gp.sql index 7e7bb12f9089f95c1cd61e30b1e9282d0bc204bc..99747c1948a7bdc3d4a0fc7d301b54f168414e9d 100644 --- a/src/test/regress/sql/subselect_gp.sql +++ b/src/test/regress/sql/subselect_gp.sql @@ -568,3 +568,13 @@ select s_name from xsupplier where s_suppkey in ( select g.l_suppkey from xlineitem g ) ; + +-- +-- Another case that failed at one point. (A planner bug in pulling up a +-- subquery with constant distribution key, 1, in the outer queries.) +-- +create table nested_in_tbl(tc1 int, tc2 int) distributed by (tc1); +select * from nested_in_tbl t1 where tc1 in + (select 1 from nested_in_tbl t2 where tc1 in + (select 1 from nested_in_tbl t3 where t3.tc2 = t2.tc2)); +drop table nested_in_tbl;