]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Repair some bugs in new union/intersect/except code.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 9 Nov 2000 02:46:17 +0000 (02:46 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 9 Nov 2000 02:46:17 +0000 (02:46 +0000)
Thanks to Kevin O'Gorman for finding these...

src/backend/optimizer/plan/planner.c
src/backend/optimizer/prep/prepunion.c

index 5951e8f0c9b03a41206f3913e1d6aee6e45ac8f9..67089e68d29af7d0dca50eb8f95dde9ae5846714 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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,
                           List *groupClause, AttrNumber *grpColIdx,
                           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)
        {
                /*
-                * Construct the plan for set operations.  The result will
-                * not need any work except perhaps a top-level sort.
+                * Construct the plan for set operations.  The result will not
+                * need any work except perhaps a top-level sort and/or LIMIT.
                 */
                result_plan = plan_set_operations(parse);
 
                /*
                 * 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);
 
+               tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);
+
                /*
                 * We leave current_pathkeys NIL indicating we do not know sort
                 * 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);
 }
+
+/*
+ * 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;
+}
index 0c91631563d89d0c1074d7ad0ce422c9278548e9..ea9b902bc5b7361af81c92009c9740be2b4f032a 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * 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;
 
 static Plan *recurse_set_operations(Node *setOp, Query *parse,
-                                                                       List *colTypes, int flag,
-                                                                       List *refnames_tlist);
+                                                                       List *colTypes, bool junkOK,
+                                                                       int flag, List *refnames_tlist);
 static Plan *generate_union_plan(SetOperationStmt *op, Query *parse,
                                                                 List *refnames_tlist);
 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,
                                                                        List *refnames_tlist);
 static List *generate_setop_tlist(List *colTypes, int flag,
+                                                                 bool hack_constants,
                                                                  List *input_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,
                                          Oid new_relid, Query *parsetree);
 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.
         * 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,
-                                                                 topop->colTypes, -1,
+                                                                 topop->colTypes, true, -1,
                                                                  leftmostQuery->targetList);
 }
 
@@ -107,13 +109,14 @@ plan_set_operations(Query *parse)
  *       Recursively handle one step in a tree of set operations
  *
  * 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
  * refnames_tlist: targetlist to take column names from
  */
 static Plan *
 recurse_set_operations(Node *setOp, Query *parse,
-                                          List *colTypes, int flag,
-                                          List *refnames_tlist)
+                                          List *colTypes, bool junkOK,
+                                          int flag, List *refnames_tlist)
 {
        if (IsA(setOp, RangeTblRef))
        {
@@ -133,7 +136,7 @@ recurse_set_operations(Node *setOp, Query *parse,
                 * Add a SubqueryScan with the caller-requested targetlist
                 */
                plan = (Plan *)
-                       make_subqueryscan(generate_setop_tlist(colTypes, flag,
+                       make_subqueryscan(generate_setop_tlist(colTypes, flag, true,
                                                                                                   subplan->targetlist,
                                                                                                   refnames_tlist),
                                                          NIL,
@@ -165,10 +168,11 @@ recurse_set_operations(Node *setOp, Query *parse,
                 * the referencing Vars will equal the tlist entries they reference.
                 * 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 *)
-                               make_result(generate_setop_tlist(colTypes, flag,
+                               make_result(generate_setop_tlist(colTypes, flag, false,
                                                                                                 plan->targetlist,
                                                                                                 refnames_tlist),
                                                        NULL,
@@ -215,7 +219,7 @@ generate_union_plan(SetOperationStmt *op, Query *parse,
                make_append(planlist,
                                        0,
                                        NIL,
-                                       generate_setop_tlist(op->colTypes, -1,
+                                       generate_setop_tlist(op->colTypes, -1, false,
                                                                        ((Plan *) lfirst(planlist))->targetlist,
                                                                        refnames_tlist));
        /*
@@ -251,10 +255,10 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse,
 
        /* Recurse on children, ensuring their outputs are marked */
        lplan = recurse_set_operations(op->larg, parse,
-                                                                  op->colTypes, 0,
+                                                                  op->colTypes, false, 0,
                                                                   refnames_tlist);
        rplan = recurse_set_operations(op->rarg, parse,
-                                                                  op->colTypes, 1,
+                                                                  op->colTypes, false, 1,
                                                                   refnames_tlist);
        /*
         * Append the child results together.
@@ -267,7 +271,7 @@ generate_nonunion_plan(SetOperationStmt *op, Query *parse,
                make_append(makeList2(lplan, rplan),
                                        0,
                                        NIL,
-                                       generate_setop_tlist(op->colTypes, 0,
+                                       generate_setop_tlist(op->colTypes, 0, false,
                                                                                 lplan->targetlist,
                                                                                 refnames_tlist));
        /*
@@ -321,10 +325,19 @@ recurse_union_children(Node *setOp, Query *parse,
                                                                                                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,
-                                                                                       top_union->colTypes, -1,
-                                                                                       refnames_tlist));
+                                                                                       top_union->colTypes, false,
+                                                                                       -1, refnames_tlist));
 }
 
 /*
@@ -332,6 +345,7 @@ recurse_union_children(Node *setOp, Query *parse,
  */
 static List *
 generate_setop_tlist(List *colTypes, int flag,
+                                        bool hack_constants,
                                         List *input_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
                 * rather than being referenced as subquery outputs.  This is mainly
                 * 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++,
                                                        colType,
                                                        -1,
                                                        pstrdup(reftle->resdom->resname),
                                                        false);
-               if (inputtle->expr && IsA(inputtle->expr, Const))
+               if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const))
                        expr = inputtle->expr;
                else
                        expr = (Node *) makeVar(0,
@@ -407,10 +424,11 @@ generate_setop_tlist(List *colTypes, int flag,
 /*
  * 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
-tlist_same_datatypes(List *tlist, List *colTypes)
+tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
 {
        List       *i;
 
@@ -418,7 +436,12 @@ tlist_same_datatypes(List *tlist, List *colTypes)
        {
                TargetEntry *tle = (TargetEntry *) lfirst(i);
 
-               if (!tle->resdom->resjunk)
+               if (tle->resdom->resjunk)
+               {
+                       if (! junkOK)
+                               return false;
+               }
+               else
                {
                        if (colTypes == NIL)
                                return false;