Skip to content

Commit 3f37475

Browse files
committed
pathman: join problem solved
1 parent 21e08dc commit 3f37475

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

contrib/pg_pathman/pg_pathman.c

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static PlannedStmt * pathman_planner_hook(Query *parse, int cursorOptions, Param
7272

7373
/* Utility functions */
7474
static void handle_modification_query(Query *parse);
75-
static void append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
75+
static int append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
7676
RangeTblEntry *rte, int index, Oid childOID, List *wrappers);
7777
static Node *wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue);
7878
static void disable_inheritance(Query *parse);
@@ -391,6 +391,7 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
391391
RangeTblEntry **new_rte_array;
392392
int len;
393393
bool found;
394+
int first_child_relid = 0;
394395

395396
/* This works only for SELECT queries */
396397
if (root->parse->commandType != CMD_SELECT || !inheritance_disabled)
@@ -467,36 +468,26 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
467468

468469
for (i = irange_lower(irange); i <= irange_upper(irange); i++)
469470
{
471+
int idx;
472+
470473
childOid = dsm_arr[i];
471-
append_child_relation(root, rel, rti, rte, i, childOid, wrappers);
474+
idx = append_child_relation(root, rel, rti, rte, i, childOid, wrappers);
475+
476+
if (!first_child_relid)
477+
first_child_relid = idx;
472478
}
473479
}
474480

475481
/* Clear old path list */
476482
list_free(rel->pathlist);
477483

478484
/* Set apropriate varnos */
479-
if (root->simple_rel_array_size > 2)
485+
if (first_child_relid)
480486
{
481-
foreach(lc, root->eq_classes)
482-
{
483-
EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc);
484-
485-
if (list_length(cur_ec->ec_members) > 1)
486-
{
487-
EquivalenceMember *orig_em = (EquivalenceMember *) linitial(cur_ec->ec_members);
488-
489-
change_varnos((Node *) orig_em->em_expr, rti, root->simple_rel_array[2]->relid);
490-
}
491-
}
492-
493-
foreach(lc, root->parse->targetList)
494-
{
495-
TargetEntry *t = (TargetEntry *) lfirst(lc);
496-
497-
change_varnos((Node *) t->expr, rti, root->simple_rel_array[2]->relid);
498-
}
499-
change_varnos((Node *) rel->reltargetlist, rti, root->simple_rel_array[2]->relid);
487+
change_varnos((Node *) root->canon_pathkeys, rti, first_child_relid);
488+
change_varnos((Node *) root->eq_classes, rti, first_child_relid);
489+
change_varnos((Node *) root->parse->targetList, rti, first_child_relid);
490+
change_varnos((Node *) rel->reltargetlist, rti, first_child_relid);
500491
}
501492

502493
rel->pathlist = NIL;
@@ -553,7 +544,11 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
553544
rel->tuples = parent_rows;
554545
}
555546

556-
static void
547+
/*
548+
* Creates child relation and adds it to root.
549+
* Returns child index in simple_rel_array
550+
*/
551+
static int
557552
append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
558553
RangeTblEntry *rte, int index, Oid childOid, List *wrappers)
559554
{
@@ -664,6 +659,8 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
664659
rel->tuples += childrel->tuples;
665660

666661
heap_close(newrelation, NoLock);
662+
663+
return childRTindex;
667664
}
668665

669666
/* Convert wrapper into expression for given index */
@@ -742,7 +739,7 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
742739
}
743740

744741
/*
745-
* Changes varno attribute in RestrictInfo objects
742+
* Changes varno attribute in all variables nested in the node
746743
*/
747744
static void
748745
change_varnos(Node *node, Oid old_varno, Oid new_varno)
@@ -757,31 +754,56 @@ change_varnos(Node *node, Oid old_varno, Oid new_varno)
757754
static bool
758755
change_varno_walker(Node *node, change_varno_context *context)
759756
{
757+
ListCell *lc;
758+
Var *var;
759+
EquivalenceClass *ec;
760+
760761
if (node == NULL)
761762
return false;
762-
if (IsA(node, Var))
763-
{
764-
Var *var = (Var *) node;
765763

766-
if (var->varno == context->old_varno)
767-
{
768-
var->varno = context->new_varno;
769-
var->varnoold = context->new_varno;
770-
}
771-
return false;
772-
}
773-
if (IsA(node, RestrictInfo))
774-
{
775-
change_varnos_in_restrinct_info((RestrictInfo *) node, context);
776-
return false;
777-
}
778-
if (IsA(node, List))
764+
switch(node->type)
779765
{
780-
ListCell *lc;
766+
case T_Var:
767+
var = (Var *) node;
768+
if (var->varno == context->old_varno)
769+
{
770+
var->varno = context->new_varno;
771+
var->varnoold = context->new_varno;
772+
}
773+
return false;
781774

782-
foreach(lc, (List *) node)
783-
change_varno_walker((Node *) lfirst(lc), context);
784-
return false;
775+
case T_RestrictInfo:
776+
change_varnos_in_restrinct_info((RestrictInfo *) node, context);
777+
return false;
778+
779+
case T_PathKey:
780+
change_varno_walker((Node *) ((PathKey *) node)->pk_eclass, context);
781+
return false;
782+
783+
case T_EquivalenceClass:
784+
ec = (EquivalenceClass *) node;
785+
786+
foreach(lc, ec->ec_members)
787+
change_varno_walker((Node *) lfirst(lc), context);
788+
foreach(lc, ec->ec_derives)
789+
change_varno_walker((Node *) lfirst(lc), context);
790+
return false;
791+
792+
case T_EquivalenceMember:
793+
change_varno_walker((Node *) ((EquivalenceMember *) node)->em_expr, context);
794+
return false;
795+
796+
case T_TargetEntry:
797+
change_varno_walker((Node *) ((TargetEntry *) node)->expr, context);
798+
return false;
799+
800+
case T_List:
801+
foreach(lc, (List *) node)
802+
change_varno_walker((Node *) lfirst(lc), context);
803+
return false;
804+
805+
default:
806+
break;
785807
}
786808

787809
/* Should not find an unplanned subquery */
@@ -797,13 +819,11 @@ change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *conte
797819

798820
change_varno_walker((Node *) rinfo->clause, context);
799821
if (rinfo->left_em)
800-
{
801822
change_varno_walker((Node *) rinfo->left_em->em_expr, context);
802-
}
823+
803824
if (rinfo->right_em)
804-
{
805825
change_varno_walker((Node *) rinfo->right_em->em_expr, context);
806-
}
826+
807827
if (rinfo->orclause)
808828
foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
809829
{

0 commit comments

Comments
 (0)