mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-30 19:00:29 +08:00
Repair some bugs in new union/intersect/except code.
Thanks to Kevin O'Gorman for finding these...
This commit is contained in:
parent
26adbc7b48
commit
a1d133990f
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.94 2000/11/05 00:15:53 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.95 2000/11/09 02:46:16 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -48,6 +48,8 @@ static List *make_subplanTargetList(Query *parse, List *tlist,
|
|||||||
static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
|
static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
|
||||||
List *groupClause, AttrNumber *grpColIdx,
|
List *groupClause, AttrNumber *grpColIdx,
|
||||||
bool is_presorted, Plan *subplan);
|
bool is_presorted, Plan *subplan);
|
||||||
|
static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
*
|
*
|
||||||
@ -635,17 +637,22 @@ union_planner(Query *parse,
|
|||||||
if (parse->setOperations)
|
if (parse->setOperations)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Construct the plan for set operations. The result will
|
* Construct the plan for set operations. The result will not
|
||||||
* not need any work except perhaps a top-level sort.
|
* need any work except perhaps a top-level sort and/or LIMIT.
|
||||||
*/
|
*/
|
||||||
result_plan = plan_set_operations(parse);
|
result_plan = plan_set_operations(parse);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We should not need to call preprocess_targetlist, since we must
|
* We should not need to call preprocess_targetlist, since we must
|
||||||
* be in a SELECT query node.
|
* be in a SELECT query node. Instead, use the targetlist
|
||||||
|
* returned by plan_set_operations (since this tells whether it
|
||||||
|
* returned any resjunk columns!), and transfer any sort key
|
||||||
|
* information from the original tlist.
|
||||||
*/
|
*/
|
||||||
Assert(parse->commandType == CMD_SELECT);
|
Assert(parse->commandType == CMD_SELECT);
|
||||||
|
|
||||||
|
tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We leave current_pathkeys NIL indicating we do not know sort
|
* We leave current_pathkeys NIL indicating we do not know sort
|
||||||
* order. This is correct when the top set operation is UNION ALL,
|
* order. This is correct when the top set operation is UNION ALL,
|
||||||
@ -1255,3 +1262,41 @@ make_sortplan(List *tlist, Plan *plannode, List *sortcls)
|
|||||||
|
|
||||||
return (Plan *) make_sort(sort_tlist, plannode, keyno);
|
return (Plan *) make_sort(sort_tlist, plannode, keyno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* postprocess_setop_tlist
|
||||||
|
* Fix up targetlist returned by plan_set_operations().
|
||||||
|
*
|
||||||
|
* We need to transpose sort key info from the orig_tlist into new_tlist.
|
||||||
|
* NOTE: this would not be good enough if we supported resjunk sort keys
|
||||||
|
* for results of set operations --- then, we'd need to project a whole
|
||||||
|
* new tlist to evaluate the resjunk columns. For now, just elog if we
|
||||||
|
* find any resjunk columns in orig_tlist.
|
||||||
|
*/
|
||||||
|
static List *
|
||||||
|
postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
|
||||||
|
{
|
||||||
|
List *l;
|
||||||
|
|
||||||
|
foreach(l, new_tlist)
|
||||||
|
{
|
||||||
|
TargetEntry *new_tle = (TargetEntry *) lfirst(l);
|
||||||
|
TargetEntry *orig_tle;
|
||||||
|
|
||||||
|
/* ignore resjunk columns in setop result */
|
||||||
|
if (new_tle->resdom->resjunk)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Assert(orig_tlist != NIL);
|
||||||
|
orig_tle = (TargetEntry *) lfirst(orig_tlist);
|
||||||
|
orig_tlist = lnext(orig_tlist);
|
||||||
|
if (orig_tle->resdom->resjunk)
|
||||||
|
elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
|
||||||
|
Assert(new_tle->resdom->resno == orig_tle->resdom->resno);
|
||||||
|
Assert(new_tle->resdom->restype == orig_tle->resdom->restype);
|
||||||
|
new_tle->resdom->ressortgroupref = orig_tle->resdom->ressortgroupref;
|
||||||
|
}
|
||||||
|
if (orig_tlist != NIL)
|
||||||
|
elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
|
||||||
|
return new_tlist;
|
||||||
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.54 2000/10/05 19:11:30 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.55 2000/11/09 02:46:17 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -39,8 +39,8 @@ typedef struct
|
|||||||
} fix_parsetree_attnums_context;
|
} fix_parsetree_attnums_context;
|
||||||
|
|
||||||
static Plan *recurse_set_operations(Node *setOp, Query *parse,
|
static Plan *recurse_set_operations(Node *setOp, Query *parse,
|
||||||
List *colTypes, int flag,
|
List *colTypes, bool junkOK,
|
||||||
List *refnames_tlist);
|
int flag, List *refnames_tlist);
|
||||||
static Plan *generate_union_plan(SetOperationStmt *op, Query *parse,
|
static Plan *generate_union_plan(SetOperationStmt *op, Query *parse,
|
||||||
List *refnames_tlist);
|
List *refnames_tlist);
|
||||||
static Plan *generate_nonunion_plan(SetOperationStmt *op, Query *parse,
|
static Plan *generate_nonunion_plan(SetOperationStmt *op, Query *parse,
|
||||||
@ -49,9 +49,10 @@ static List *recurse_union_children(Node *setOp, Query *parse,
|
|||||||
SetOperationStmt *top_union,
|
SetOperationStmt *top_union,
|
||||||
List *refnames_tlist);
|
List *refnames_tlist);
|
||||||
static List *generate_setop_tlist(List *colTypes, int flag,
|
static List *generate_setop_tlist(List *colTypes, int flag,
|
||||||
|
bool hack_constants,
|
||||||
List *input_tlist,
|
List *input_tlist,
|
||||||
List *refnames_tlist);
|
List *refnames_tlist);
|
||||||
static bool tlist_same_datatypes(List *tlist, List *colTypes);
|
static bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK);
|
||||||
static void fix_parsetree_attnums(Index rt_index, Oid old_relid,
|
static void fix_parsetree_attnums(Index rt_index, Oid old_relid,
|
||||||
Oid new_relid, Query *parsetree);
|
Oid new_relid, Query *parsetree);
|
||||||
static bool fix_parsetree_attnums_walker(Node *node,
|
static bool fix_parsetree_attnums_walker(Node *node,
|
||||||
@ -95,10 +96,11 @@ plan_set_operations(Query *parse)
|
|||||||
/*
|
/*
|
||||||
* Recurse on setOperations tree to generate plans for set ops.
|
* Recurse on setOperations tree to generate plans for set ops.
|
||||||
* The final output plan should have just the column types shown
|
* The final output plan should have just the column types shown
|
||||||
* as the output from the top-level node.
|
* as the output from the top-level node, plus possibly a resjunk
|
||||||
|
* working column (we can rely on upper-level nodes to deal with that).
|
||||||
*/
|
*/
|
||||||
return recurse_set_operations((Node *) topop, parse,
|
return recurse_set_operations((Node *) topop, parse,
|
||||||
topop->colTypes, -1,
|
topop->colTypes, true, -1,
|
||||||
leftmostQuery->targetList);
|
leftmostQuery->targetList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,13 +109,14 @@ plan_set_operations(Query *parse)
|
|||||||
* Recursively handle one step in a tree of set operations
|
* Recursively handle one step in a tree of set operations
|
||||||
*
|
*
|
||||||
* colTypes: integer list of type OIDs of expected output columns
|
* colTypes: integer list of type OIDs of expected output columns
|
||||||
|
* junkOK: if true, child resjunk columns may be left in the result
|
||||||
* flag: if >= 0, add a resjunk output column indicating value of flag
|
* flag: if >= 0, add a resjunk output column indicating value of flag
|
||||||
* refnames_tlist: targetlist to take column names from
|
* refnames_tlist: targetlist to take column names from
|
||||||
*/
|
*/
|
||||||
static Plan *
|
static Plan *
|
||||||
recurse_set_operations(Node *setOp, Query *parse,
|
recurse_set_operations(Node *setOp, Query *parse,
|
||||||
List *colTypes, int flag,
|
List *colTypes, bool junkOK,
|
||||||
List *refnames_tlist)
|
int flag, List *refnames_tlist)
|
||||||
{
|
{
|
||||||
if (IsA(setOp, RangeTblRef))
|
if (IsA(setOp, RangeTblRef))
|
||||||
{
|
{
|
||||||
@ -133,7 +136,7 @@ recurse_set_operations(Node *setOp, Query *parse,
|
|||||||
* Add a SubqueryScan with the caller-requested targetlist
|
* Add a SubqueryScan with the caller-requested targetlist
|
||||||
*/
|
*/
|
||||||
plan = (Plan *)
|
plan = (Plan *)
|
||||||
make_subqueryscan(generate_setop_tlist(colTypes, flag,
|
make_subqueryscan(generate_setop_tlist(colTypes, flag, true,
|
||||||
subplan->targetlist,
|
subplan->targetlist,
|
||||||
refnames_tlist),
|
refnames_tlist),
|
||||||
NIL,
|
NIL,
|
||||||
@ -165,10 +168,11 @@ recurse_set_operations(Node *setOp, Query *parse,
|
|||||||
* the referencing Vars will equal the tlist entries they reference.
|
* the referencing Vars will equal the tlist entries they reference.
|
||||||
* Ugly but I don't feel like making that code more general right now.
|
* Ugly but I don't feel like making that code more general right now.
|
||||||
*/
|
*/
|
||||||
if (flag >= 0 || ! tlist_same_datatypes(plan->targetlist, colTypes))
|
if (flag >= 0 ||
|
||||||
|
! tlist_same_datatypes(plan->targetlist, colTypes, junkOK))
|
||||||
{
|
{
|
||||||
plan = (Plan *)
|
plan = (Plan *)
|
||||||
make_result(generate_setop_tlist(colTypes, flag,
|
make_result(generate_setop_tlist(colTypes, flag, false,
|
||||||
plan->targetlist,
|
plan->targetlist,
|
||||||
refnames_tlist),
|
refnames_tlist),
|
||||||
NULL,
|
NULL,
|
||||||
@ -215,7 +219,7 @@ generate_union_plan(SetOperationStmt *op, Query *parse,
|
|||||||
make_append(planlist,
|
make_append(planlist,
|
||||||
0,
|
0,
|
||||||
NIL,
|
NIL,
|
||||||
generate_setop_tlist(op->colTypes, -1,
|
generate_setop_tlist(op->colTypes, -1, false,
|
||||||
((Plan *) lfirst(planlist))->targetlist,
|
((Plan *) lfirst(planlist))->targetlist,
|
||||||
refnames_tlist));
|
refnames_tlist));
|
||||||
/*
|
/*
|
||||||
@ -251,10 +255,10 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse,
|
|||||||
|
|
||||||
/* Recurse on children, ensuring their outputs are marked */
|
/* Recurse on children, ensuring their outputs are marked */
|
||||||
lplan = recurse_set_operations(op->larg, parse,
|
lplan = recurse_set_operations(op->larg, parse,
|
||||||
op->colTypes, 0,
|
op->colTypes, false, 0,
|
||||||
refnames_tlist);
|
refnames_tlist);
|
||||||
rplan = recurse_set_operations(op->rarg, parse,
|
rplan = recurse_set_operations(op->rarg, parse,
|
||||||
op->colTypes, 1,
|
op->colTypes, false, 1,
|
||||||
refnames_tlist);
|
refnames_tlist);
|
||||||
/*
|
/*
|
||||||
* Append the child results together.
|
* Append the child results together.
|
||||||
@ -267,7 +271,7 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse,
|
|||||||
make_append(makeList2(lplan, rplan),
|
make_append(makeList2(lplan, rplan),
|
||||||
0,
|
0,
|
||||||
NIL,
|
NIL,
|
||||||
generate_setop_tlist(op->colTypes, 0,
|
generate_setop_tlist(op->colTypes, 0, false,
|
||||||
lplan->targetlist,
|
lplan->targetlist,
|
||||||
refnames_tlist));
|
refnames_tlist));
|
||||||
/*
|
/*
|
||||||
@ -321,10 +325,19 @@ recurse_union_children(Node *setOp, Query *parse,
|
|||||||
top_union, refnames_tlist));
|
top_union, refnames_tlist));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Not same, so plan this child separately */
|
/*
|
||||||
|
* Not same, so plan this child separately.
|
||||||
|
*
|
||||||
|
* Note we disallow any resjunk columns in child results. This
|
||||||
|
* is necessary since the Append node that implements the union
|
||||||
|
* won't do any projection, and upper levels will get confused if
|
||||||
|
* some of our output tuples have junk and some don't. This case
|
||||||
|
* only arises when we have an EXCEPT or INTERSECT as child, else
|
||||||
|
* there won't be resjunk anyway.
|
||||||
|
*/
|
||||||
return makeList1(recurse_set_operations(setOp, parse,
|
return makeList1(recurse_set_operations(setOp, parse,
|
||||||
top_union->colTypes, -1,
|
top_union->colTypes, false,
|
||||||
refnames_tlist));
|
-1, refnames_tlist));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -332,6 +345,7 @@ recurse_union_children(Node *setOp, Query *parse,
|
|||||||
*/
|
*/
|
||||||
static List *
|
static List *
|
||||||
generate_setop_tlist(List *colTypes, int flag,
|
generate_setop_tlist(List *colTypes, int flag,
|
||||||
|
bool hack_constants,
|
||||||
List *input_tlist,
|
List *input_tlist,
|
||||||
List *refnames_tlist)
|
List *refnames_tlist)
|
||||||
{
|
{
|
||||||
@ -359,14 +373,17 @@ generate_setop_tlist(List *colTypes, int flag,
|
|||||||
* HACK: constants in the input's targetlist are copied up as-is
|
* HACK: constants in the input's targetlist are copied up as-is
|
||||||
* rather than being referenced as subquery outputs. This is mainly
|
* rather than being referenced as subquery outputs. This is mainly
|
||||||
* to ensure that when we try to coerce them to the output column's
|
* to ensure that when we try to coerce them to the output column's
|
||||||
* datatype, the right things happen for UNKNOWN constants.
|
* datatype, the right things happen for UNKNOWN constants. But do
|
||||||
|
* this only at the first level of subquery-scan plans; we don't
|
||||||
|
* want phony constants appearing in the output tlists of upper-level
|
||||||
|
* nodes!
|
||||||
*/
|
*/
|
||||||
resdom = makeResdom((AttrNumber) resno++,
|
resdom = makeResdom((AttrNumber) resno++,
|
||||||
colType,
|
colType,
|
||||||
-1,
|
-1,
|
||||||
pstrdup(reftle->resdom->resname),
|
pstrdup(reftle->resdom->resname),
|
||||||
false);
|
false);
|
||||||
if (inputtle->expr && IsA(inputtle->expr, Const))
|
if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const))
|
||||||
expr = inputtle->expr;
|
expr = inputtle->expr;
|
||||||
else
|
else
|
||||||
expr = (Node *) makeVar(0,
|
expr = (Node *) makeVar(0,
|
||||||
@ -407,10 +424,11 @@ generate_setop_tlist(List *colTypes, int flag,
|
|||||||
/*
|
/*
|
||||||
* Does tlist have same datatypes as requested colTypes?
|
* Does tlist have same datatypes as requested colTypes?
|
||||||
*
|
*
|
||||||
* Resjunk columns are ignored.
|
* Resjunk columns are ignored if junkOK is true; otherwise presence of
|
||||||
|
* a resjunk column will always cause a 'false' result.
|
||||||
*/
|
*/
|
||||||
static bool
|
static bool
|
||||||
tlist_same_datatypes(List *tlist, List *colTypes)
|
tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
|
||||||
{
|
{
|
||||||
List *i;
|
List *i;
|
||||||
|
|
||||||
@ -418,7 +436,12 @@ tlist_same_datatypes(List *tlist, List *colTypes)
|
|||||||
{
|
{
|
||||||
TargetEntry *tle = (TargetEntry *) lfirst(i);
|
TargetEntry *tle = (TargetEntry *) lfirst(i);
|
||||||
|
|
||||||
if (!tle->resdom->resjunk)
|
if (tle->resdom->resjunk)
|
||||||
|
{
|
||||||
|
if (! junkOK)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (colTypes == NIL)
|
if (colTypes == NIL)
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user