1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

pgindent run over code.

This commit is contained in:
Bruce Momjian
1999-05-25 16:15:34 +00:00
parent 4b04b01aaa
commit 07842084fe
413 changed files with 11723 additions and 10769 deletions

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.55 1999/05/18 21:34:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.56 1999/05/25 16:09:34 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -42,18 +42,18 @@
#define NONAME_SORT 1
#define NONAME_MATERIAL 2
#define NONAME_MATERIAL 2
static List *switch_outer(List *clauses);
static Oid *generate_merge_input_sortorder(List *pathkeys,
MergeOrder *mergeorder);
MergeOrder *mergeorder);
static Scan *create_scan_node(Path *best_path, List *tlist);
static Join *create_join_node(JoinPath *best_path, List *tlist);
static SeqScan *create_seqscan_node(Path *best_path, List *tlist,
List *scan_clauses);
static IndexScan *create_indexscan_node(IndexPath *best_path, List *tlist,
List *scan_clauses);
static NestLoop *create_nestloop_node(NestPath *best_path, List *tlist,
static NestLoop *create_nestloop_node(NestPath * best_path, List *tlist,
List *clauses, Plan *outer_node, List *outer_tlist,
Plan *inner_node, List *inner_tlist);
static MergeJoin *create_mergejoin_node(MergePath *best_path, List *tlist,
@@ -64,7 +64,7 @@ static HashJoin *create_hashjoin_node(HashPath *best_path, List *tlist,
Plan *inner_node, List *inner_tlist);
static Node *fix_indxqual_references(Node *clause, Path *index_path);
static Noname *make_noname(List *tlist, List *pathkeys, Oid *operators,
Plan *plan_node, int nonametype);
Plan *plan_node, int nonametype);
static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
List *indxid, List *indxqual, List *indxqualorig);
static NestLoop *make_nestloop(List *qptlist, List *qpqual, Plan *lefttree,
@@ -133,7 +133,7 @@ create_plan(Path *best_path)
pages = 1;
plan_node->plan_tupperpage = tuples / pages;
#ifdef NOT_USED /* fix xfunc */
#ifdef NOT_USED /* fix xfunc */
/* sort clauses by cost/(1-selectivity) -- JMH 2/26/92 */
if (XfuncMode != XFUNC_OFF)
{
@@ -197,7 +197,7 @@ create_scan_node(Path *best_path, List *tlist)
}
/*
* create_join_node
* create_join_node
* Create a join path for 'best_path' and(recursively) paths for its
* inner and outer paths.
*
@@ -263,7 +263,7 @@ create_join_node(JoinPath *best_path, List *tlist)
/*
* * Expensive function pullups may have pulled local predicates *
* into this path node. Put them in the qpqual of the plan node. *
* into this path node. Put them in the qpqual of the plan node. *
* JMH, 6/15/92
*/
if (get_loc_restrictinfo(best_path) != NIL)
@@ -400,11 +400,11 @@ create_indexscan_node(IndexPath *best_path,
fixed_indxqual = (List *) fix_indxqual_references((Node *) indxqual, (Path *) best_path);
scan_node = make_indexscan(tlist,
qpqual,
lfirsti(best_path->path.parent->relids),
best_path->indexid,
fixed_indxqual,
indxqual);
qpqual,
lfirsti(best_path->path.parent->relids),
best_path->indexid,
fixed_indxqual,
indxqual);
scan_node->scan.plan.cost = best_path->path.path_cost;
@@ -418,7 +418,7 @@ create_indexscan_node(IndexPath *best_path,
*****************************************************************************/
static NestLoop *
create_nestloop_node(NestPath *best_path,
create_nestloop_node(NestPath * best_path,
List *tlist,
List *clauses,
Plan *outer_node,
@@ -471,7 +471,7 @@ create_nestloop_node(NestPath *best_path,
clauses = set_difference(clauses, inner_indxqual); /* XXX */
new_inner_qual = index_outerjoin_references(inner_indxqual,
outer_node->targetlist,
outer_node->targetlist,
((Scan *) inner_node)->scanrelid);
((IndexScan *) inner_node)->indxqual = lcons(new_inner_qual, NIL);
}
@@ -479,10 +479,10 @@ create_nestloop_node(NestPath *best_path,
else if (IsA_Join(inner_node))
{
inner_node = (Plan *) make_noname(inner_tlist,
NIL,
NULL,
inner_node,
NONAME_MATERIAL);
NIL,
NULL,
inner_node,
NONAME_MATERIAL);
}
join_node = make_nestloop(tlist,
@@ -535,8 +535,9 @@ create_mergejoin_node(MergePath *best_path,
if (best_path->outersortkeys)
{
Oid *outer_order = generate_merge_input_sortorder(
best_path->outersortkeys,
best_path->jpath.path.pathorder->ord.merge);
best_path->outersortkeys,
best_path->jpath.path.pathorder->ord.merge);
outer_node = (Plan *) make_noname(outer_tlist,
best_path->outersortkeys,
outer_order,
@@ -547,8 +548,9 @@ create_mergejoin_node(MergePath *best_path,
if (best_path->innersortkeys)
{
Oid *inner_order = generate_merge_input_sortorder(
best_path->innersortkeys,
best_path->jpath.path.pathorder->ord.merge);
best_path->innersortkeys,
best_path->jpath.path.pathorder->ord.merge);
inner_node = (Plan *) make_noname(inner_tlist,
best_path->innersortkeys,
inner_order,
@@ -595,17 +597,17 @@ create_hashjoin_node(HashPath *best_path,
* and set those clauses to contain references to lower attributes.
*/
qpqual = join_references(set_difference(clauses,
best_path->path_hashclauses),
outer_tlist,
inner_tlist);
best_path->path_hashclauses),
outer_tlist,
inner_tlist);
/*
* Now set the references in the hashclauses and rearrange them so
* that the outer variable is always on the left.
*/
hashclauses = switch_outer(join_references(best_path->path_hashclauses,
outer_tlist,
inner_tlist));
outer_tlist,
inner_tlist));
innerhashkey = get_rightop(lfirst(hashclauses));
@@ -671,20 +673,22 @@ fix_indxqual_references(Node *clause, Path *index_path)
is_funcclause((Node *) get_leftop((Expr *) clause)) &&
((Func *) ((Expr *) get_leftop((Expr *) clause))->oper)->funcisindex)
{
/* This looks pretty seriously wrong to me, but I'm not sure what it's
* supposed to be doing ... tgl 5/99
/*
* This looks pretty seriously wrong to me, but I'm not sure what
* it's supposed to be doing ... tgl 5/99
*/
Var *newvar = makeVar((Index) lfirsti(index_path->parent->relids),
1, /* func indices have one key */
((Func *) ((Expr *) clause)->oper)->functype,
-1,
0,
(Index) lfirsti(index_path->parent->relids),
0);
1, /* func indices have one key */
((Func *) ((Expr *) clause)->oper)->functype,
-1,
0,
(Index) lfirsti(index_path->parent->relids),
0);
return ((Node *) make_opclause((Oper *) ((Expr *) clause)->oper,
newvar,
get_rightop((Expr *) clause)));
newvar,
get_rightop((Expr *) clause)));
}
else if (IsA(clause, Expr))
@@ -696,6 +700,7 @@ fix_indxqual_references(Node *clause, Path *index_path)
foreach(i, expr->args)
{
Node *subclause = lfirst(i);
new_subclauses = lappend(new_subclauses,
fix_indxqual_references(subclause,
index_path));
@@ -711,6 +716,7 @@ fix_indxqual_references(Node *clause, Path *index_path)
foreach(i, (List *) clause)
{
Node *subclause = lfirst(i);
new_subclauses = lappend(new_subclauses,
fix_indxqual_references(subclause,
index_path));
@@ -768,15 +774,17 @@ switch_outer(List *clauses)
foreach(i, clauses)
{
clause = lfirst(i);
Assert(is_opclause((Node*) clause));
Assert(is_opclause((Node *) clause));
op = (Node *) get_rightop(clause);
Assert(op != (Node*) NULL);
Assert(op != (Node *) NULL);
if (IsA(op, ArrayRef))
op = ((ArrayRef *) op)->refexpr;
Assert(IsA(op, Var));
if (var_is_outer((Var *) op))
{
/* Duplicate just enough of the structure to allow commuting
/*
* Duplicate just enough of the structure to allow commuting
* the clause without changing the original list. Could use
* copyObject, but a complete deep copy is overkill.
*/
@@ -812,13 +820,14 @@ static Oid *
generate_merge_input_sortorder(List *pathkeys, MergeOrder *mergeorder)
{
int listlength = length(pathkeys);
Oid *result = (Oid*) palloc(sizeof(Oid) * (listlength+1));
Oid *result = (Oid *) palloc(sizeof(Oid) * (listlength + 1));
Oid *nextsortop = result;
List *p;
foreach(p, pathkeys)
{
Var *pkey = (Var*) lfirst((List*) lfirst(p));
Var *pkey = (Var *) lfirst((List *) lfirst(p));
Assert(IsA(pkey, Var));
if (pkey->vartype == mergeorder->left_type)
*nextsortop++ = mergeorder->left_operator;
@@ -826,7 +835,7 @@ generate_merge_input_sortorder(List *pathkeys, MergeOrder *mergeorder)
*nextsortop++ = mergeorder->right_operator;
else
elog(ERROR,
"generate_merge_input_sortorder: can't handle data type %d",
"generate_merge_input_sortorder: can't handle data type %d",
pkey->vartype);
}
*nextsortop++ = InvalidOid;
@@ -859,12 +868,13 @@ set_noname_tlist_operators(List *tlist, List *pathkeys, Oid *operators)
resdom = tlist_member((Var *) pathkey, tlist);
if (resdom)
{
/*
* Order the resdom pathkey and replace the operator OID for each
* key with the regproc OID.
* Order the resdom pathkey and replace the operator OID for
* each key with the regproc OID.
*/
resdom->reskey = keyno;
resdom->reskeyop = get_opcode(operators[keyno-1]);
resdom->reskeyop = get_opcode(operators[keyno - 1]);
}
keyno += 1;
}
@@ -878,7 +888,7 @@ set_noname_tlist_operators(List *tlist, List *pathkeys, Oid *operators)
*/
static void
copy_costsize (Plan *dest, Plan *src)
copy_costsize(Plan *dest, Plan *src)
{
if (src)
{
@@ -915,10 +925,10 @@ copy_costsize (Plan *dest, Plan *src)
*/
static Noname *
make_noname(List *tlist,
List *pathkeys,
Oid *operators,
Plan *plan_node,
int nonametype)
List *pathkeys,
Oid *operators,
Plan *plan_node,
int nonametype)
{
List *noname_tlist;
Noname *retval = NULL;
@@ -931,22 +941,22 @@ make_noname(List *tlist,
{
case NONAME_SORT:
retval = (Noname *) make_seqscan(tlist,
NIL,
_NONAME_RELATION_ID_,
(Plan *) make_sort(noname_tlist,
_NONAME_RELATION_ID_,
plan_node,
length(pathkeys)));
NIL,
_NONAME_RELATION_ID_,
(Plan *) make_sort(noname_tlist,
_NONAME_RELATION_ID_,
plan_node,
length(pathkeys)));
break;
case NONAME_MATERIAL:
retval = (Noname *) make_seqscan(tlist,
NIL,
_NONAME_RELATION_ID_,
(Plan *) make_material(noname_tlist,
_NONAME_RELATION_ID_,
plan_node,
length(pathkeys)));
NIL,
_NONAME_RELATION_ID_,
(Plan *) make_material(noname_tlist,
_NONAME_RELATION_ID_,
plan_node,
length(pathkeys)));
break;
default:
@@ -1014,8 +1024,9 @@ make_nestloop(List *qptlist,
NestLoop *node = makeNode(NestLoop);
Plan *plan = &node->join;
/* this cost estimate is entirely bogus...
* hopefully it will be overwritten by caller.
/*
* this cost estimate is entirely bogus... hopefully it will be
* overwritten by caller.
*/
plan->cost = (lefttree ? lefttree->cost : 0) +
(righttree ? righttree->cost : 0);
@@ -1039,8 +1050,9 @@ make_hashjoin(List *tlist,
HashJoin *node = makeNode(HashJoin);
Plan *plan = &node->join;
/* this cost estimate is entirely bogus...
* hopefully it will be overwritten by caller.
/*
* this cost estimate is entirely bogus... hopefully it will be
* overwritten by caller.
*/
plan->cost = (lefttree ? lefttree->cost : 0) +
(righttree ? righttree->cost : 0);
@@ -1082,8 +1094,9 @@ make_mergejoin(List *tlist,
MergeJoin *node = makeNode(MergeJoin);
Plan *plan = &node->join;
/* this cost estimate is entirely bogus...
* hopefully it will be overwritten by caller.
/*
* this cost estimate is entirely bogus... hopefully it will be
* overwritten by caller.
*/
plan->cost = (lefttree ? lefttree->cost : 0) +
(righttree ? righttree->cost : 0);
@@ -1142,7 +1155,7 @@ make_agg(List *tlist, Plan *lefttree)
{
Agg *node = makeNode(Agg);
copy_costsize(& node->plan, lefttree);
copy_costsize(&node->plan, lefttree);
node->plan.state = (EState *) NULL;
node->plan.qual = NULL;
node->plan.targetlist = tlist;
@@ -1162,7 +1175,7 @@ make_group(List *tlist,
{
Group *node = makeNode(Group);
copy_costsize(& node->plan, (Plan *) lefttree);
copy_costsize(&node->plan, (Plan *) lefttree);
node->plan.state = (EState *) NULL;
node->plan.qual = NULL;
node->plan.targetlist = tlist;

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.29 1999/02/22 05:26:21 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.30 1999/05/25 16:09:36 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -40,7 +40,7 @@
extern int Quiet;
static void add_restrict_and_join_to_rel(Query *root, List *clause);
static void add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
static void add_join_info_to_rels(Query *root, RestrictInfo * restrictinfo,
Relids join_relids);
static void add_vars_to_targetlist(Query *root, List *vars, Relids join_relids);
@@ -183,6 +183,7 @@ add_restrict_and_join_to_rel(Query *root, List *clause)
if (length(relids) == 1)
{
/*
* There is only one relation participating in 'clause', so
* 'clause' must be a restriction clause.
@@ -207,11 +208,13 @@ add_restrict_and_join_to_rel(Query *root, List *clause)
}
else
{
/*
* 'clause' is a join clause, since there is more than one atom in
* the relid list.
*/
if (is_funcclause((Node *) clause))
/*
* XXX If we have a func clause set selectivity to 1/3, really
* need a true selectivity function.
@@ -237,8 +240,8 @@ add_restrict_and_join_to_rel(Query *root, List *clause)
*
*/
static void
add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
Relids join_relids)
add_join_info_to_rels(Query *root, RestrictInfo * restrictinfo,
Relids join_relids)
{
List *join_relid;
@@ -247,7 +250,7 @@ add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
{
JoinInfo *joininfo;
Relids unjoined_relids = NIL;
List *rel;
List *rel;
/* Get the relids not equal to the current relid */
foreach(rel, join_relids)
@@ -259,7 +262,7 @@ add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
joininfo = find_joininfo_node(get_base_rel(root, lfirsti(join_relid)),
unjoined_relids);
joininfo->jinfo_restrictinfo = lcons(copyObject((void *) restrictinfo),
joininfo->jinfo_restrictinfo);
joininfo->jinfo_restrictinfo);
}
}
@@ -375,7 +378,7 @@ mergejoinop(Expr *clause)
rightOp;
bool sortable;
if (!is_opclause((Node*) clause))
if (!is_opclause((Node *) clause))
return NULL;
left = get_leftop(clause);
@@ -384,7 +387,7 @@ mergejoinop(Expr *clause)
/* caution: is_opclause accepts more than I do, so check it */
if (!right)
return NULL; /* unary opclauses need not apply */
if (!IsA(left, Var) || !IsA(right, Var))
if (!IsA(left, Var) ||!IsA(right, Var))
return NULL;
opno = ((Oper *) clause->oper)->opno;
@@ -422,7 +425,7 @@ hashjoinop(Expr *clause)
Var *left,
*right;
if (!is_opclause((Node*) clause))
if (!is_opclause((Node *) clause))
return InvalidOid;
left = get_leftop(clause);
@@ -431,7 +434,7 @@ hashjoinop(Expr *clause)
/* caution: is_opclause accepts more than I do, so check it */
if (!right)
return InvalidOid; /* unary opclauses need not apply */
if (!IsA(left, Var) || !IsA(right, Var))
if (!IsA(left, Var) ||!IsA(right, Var))
return InvalidOid;
return op_hashjoinable(((Oper *) clause->oper)->opno,

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.35 1999/05/03 00:38:43 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.36 1999/05/25 16:09:36 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -154,7 +154,7 @@ query_planner(Query *root,
else
return (Plan *) scan;
}
break;
break;
default:
return (Plan *) NULL;
}
@@ -195,9 +195,9 @@ query_planner(Query *root,
* tree. -- Wei
*
* Note: formerly there was a test here to skip the flatten call if we
* expected union_planner to insert a Group or Agg node above our result.
* However, now union_planner tells us exactly what it wants returned,
* and we just do it. Much cleaner.
* expected union_planner to insert a Group or Agg node above our
* result. However, now union_planner tells us exactly what it wants
* returned, and we just do it. Much cleaner.
*/
else
{
@@ -207,6 +207,7 @@ query_planner(Query *root,
}
#ifdef NOT_USED
/*
* Destructively modify the query plan's targetlist to add fjoin lists
* to flatten functions that return sets of base types
@@ -251,7 +252,7 @@ subplanner(Query *root,
final_rel = make_one_rel(root, root->base_rel_list);
#ifdef NOT_USED /* fix xfunc */
#ifdef NOT_USED /* fix xfunc */
/*
* Perform Predicate Migration on each path, to optimize and correctly

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.53 1999/05/17 17:03:15 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.54 1999/05/25 16:09:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -56,10 +56,10 @@
#include "parser/parse_oper.h"
static List *make_subplanTargetList(Query *parse, List *tlist,
AttrNumber **groupColIdx);
AttrNumber **groupColIdx);
static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
List *groupClause, AttrNumber *grpColIdx,
Plan *subplan);
List *groupClause, AttrNumber *grpColIdx,
Plan *subplan);
static bool need_sortplan(List *sortcls, Plan *plan);
static Plan *make_sortplan(List *tlist, List *sortcls, Plan *plannode);
@@ -113,12 +113,12 @@ union_planner(Query *parse)
if (parse->unionClause)
{
result_plan = (Plan *) plan_union_queries(parse);
/* XXX do we need to do this? bjm 12/19/97 */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
result_plan = (Plan *) plan_union_queries(parse);
/* XXX do we need to do this? bjm 12/19/97 */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
}
else if ((rt_index = first_inherit_rt_entry(rangetable)) != -1)
{
@@ -127,78 +127,80 @@ union_planner(Query *parse)
result_plan = (Plan *) plan_inherit_queries(parse, rt_index);
/* XXX do we need to do this? bjm 12/19/97 */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
parse->commandType,
parse->resultRelation,
parse->rtable);
}
else
{
List **vpm = NULL;
List *sub_tlist;
List **vpm = NULL;
List *sub_tlist;
/* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
/* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
/* Add row-mark targets for UPDATE
* (should this be done in preprocess_targetlist?)
*/
if (parse->rowMark != NULL)
{
List *l;
foreach (l, parse->rowMark)
/*
* Add row-mark targets for UPDATE (should this be done in
* preprocess_targetlist?)
*/
if (parse->rowMark != NULL)
{
RowMark *rowmark = (RowMark*) lfirst(l);
TargetEntry *ctid;
Resdom *resdom;
Var *var;
char *resname;
List *l;
if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
continue;
foreach(l, parse->rowMark)
{
RowMark *rowmark = (RowMark *) lfirst(l);
TargetEntry *ctid;
Resdom *resdom;
Var *var;
char *resname;
resname = (char*) palloc(32);
sprintf(resname, "ctid%u", rowmark->rti);
resdom = makeResdom(length(tlist) + 1,
TIDOID,
-1,
resname,
0,
0,
true);
if (!(rowmark->info & ROW_MARK_FOR_UPDATE))
continue;
var = makeVar(rowmark->rti, -1, TIDOID,
-1, 0, rowmark->rti, -1);
resname = (char *) palloc(32);
sprintf(resname, "ctid%u", rowmark->rti);
resdom = makeResdom(length(tlist) + 1,
TIDOID,
-1,
resname,
0,
0,
true);
ctid = makeTargetEntry(resdom, (Node *) var);
tlist = lappend(tlist, ctid);
var = makeVar(rowmark->rti, -1, TIDOID,
-1, 0, rowmark->rti, -1);
ctid = makeTargetEntry(resdom, (Node *) var);
tlist = lappend(tlist, ctid);
}
}
}
/* Generate appropriate target list for subplan; may be different
* from tlist if grouping or aggregation is needed.
*/
sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
/*
* Generate appropriate target list for subplan; may be different
* from tlist if grouping or aggregation is needed.
*/
sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
/* Generate the (sub) plan */
if (parse->rtable != NULL)
{
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
}
PlannerVarParam = lcons(vpm, PlannerVarParam);
result_plan = query_planner(parse,
parse->commandType,
sub_tlist,
(List *) parse->qual);
PlannerVarParam = lnext(PlannerVarParam);
if (vpm != NULL)
pfree(vpm);
/* Generate the (sub) plan */
if (parse->rtable != NULL)
{
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
}
PlannerVarParam = lcons(vpm, PlannerVarParam);
result_plan = query_planner(parse,
parse->commandType,
sub_tlist,
(List *) parse->qual);
PlannerVarParam = lnext(PlannerVarParam);
if (vpm != NULL)
pfree(vpm);
}
/*
* If we have a GROUP BY clause, insert a group node (with the
* appropriate sort node.)
@@ -216,7 +218,8 @@ union_planner(Query *parse)
*/
tuplePerGroup = parse->hasAggs;
/* If there are aggregates then the Group node should just return
/*
* If there are aggregates then the Group node should just return
* the same (simplified) tlist as the subplan, which we indicate
* to make_groupplan by passing NIL. If there are no aggregates
* then the Group node had better compute the final tlist.
@@ -235,7 +238,7 @@ union_planner(Query *parse)
*/
if (parse->havingQual)
{
List **vpm = NULL;
List **vpm = NULL;
if (parse->rtable != NULL)
{
@@ -249,15 +252,20 @@ union_planner(Query *parse)
if (parse->hasSubLinks)
{
/* There is a subselect in the havingQual, so we have to process it
* using the same function as for a subselect in 'where'
/*
* There is a subselect in the havingQual, so we have to
* process it using the same function as for a subselect in
* 'where'
*/
parse->havingQual =
(Node *) SS_process_sublinks(parse->havingQual);
/* Check for ungrouped variables passed to subplans.
* (Probably this should be done by the parser, but right now
* the parser is not smart enough to tell which level the vars
* belong to?)
/*
* Check for ungrouped variables passed to subplans. (Probably
* this should be done by the parser, but right now the parser
* is not smart enough to tell which level the vars belong
* to?)
*/
check_having_for_ungrouped_vars(parse->havingQual,
parse->groupClause,
@@ -269,7 +277,7 @@ union_planner(Query *parse)
PlannerVarParam = lnext(PlannerVarParam);
if (vpm != NULL)
pfree(vpm);
pfree(vpm);
}
/*
@@ -283,22 +291,22 @@ union_planner(Query *parse)
result_plan->qual = (List *) parse->havingQual;
/*
* Update vars to refer to subplan result tuples,
* find Aggrefs, make sure there is an Aggref in every HAVING clause.
* Update vars to refer to subplan result tuples, find Aggrefs,
* make sure there is an Aggref in every HAVING clause.
*/
if (! set_agg_tlist_references((Agg *) result_plan))
if (!set_agg_tlist_references((Agg *) result_plan))
elog(ERROR, "SELECT/HAVING requires aggregates to be valid");
/*
* Check that we actually found some aggregates, else executor
* will die unpleasantly. (The rewrite module currently has bugs
* that allow hasAggs to be incorrectly set 'true' sometimes.
* It's not easy to recover here, since we've already made decisions
* that allow hasAggs to be incorrectly set 'true' sometimes. It's
* not easy to recover here, since we've already made decisions
* assuming there will be an Agg node.)
*/
if (((Agg *) result_plan)->aggs == NIL)
elog(ERROR, "union_planner: query is marked hasAggs, but I don't see any");
}
}
/*
* For now, before we hand back the plan, check to see if there is a
@@ -363,7 +371,7 @@ union_planner(Query *parse)
* SELECT a+1, ... GROUP BY a+1
* Note, however, that other varnodes in the parent's targetlist (and
* havingQual, if any) will still need to be updated to refer to outputs
* of the subplan. This routine is quite large enough already, so we do
* of the subplan. This routine is quite large enough already, so we do
* that later.
*---------------
*/
@@ -384,13 +392,15 @@ make_subplanTargetList(Query *parse,
*groupColIdx = NULL;
/* If we're not grouping or aggregating, nothing to do here;
/*
* If we're not grouping or aggregating, nothing to do here;
* query_planner should receive the unmodified target list.
*/
if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
return tlist;
/* If grouping, make a working copy of groupClause list (which we use
/*
* If grouping, make a working copy of groupClause list (which we use
* just to verify that we found all the groupClause items in tlist).
* Also allocate space to remember where the group columns are in the
* subplan tlist.
@@ -403,14 +413,14 @@ make_subplanTargetList(Query *parse,
*groupColIdx = grpColIdx;
}
sub_tlist = new_unsorted_tlist(tlist); /* make a modifiable copy */
sub_tlist = new_unsorted_tlist(tlist); /* make a modifiable copy */
/*
* Step 1: build grpColIdx by finding targetlist items that match
* GroupBy entries. If there are aggregates, remove non-GroupBy items
* from sub_tlist, and reset its resnos accordingly. When we leave an
* expression in the subplan tlist, modify the parent tlist to copy the
* value from the subplan output rather than re-evaluating it.
* expression in the subplan tlist, modify the parent tlist to copy
* the value from the subplan output rather than re-evaluating it.
*/
prnt_tlist = tlist; /* scans parent tlist in sync with sl */
foreach(sl, sub_tlist)
@@ -434,23 +444,28 @@ make_subplanTargetList(Query *parse,
resdom->reskey = keyno;
resdom->reskeyop = get_opcode(grpcl->grpOpoid);
grpColIdx[keyno - 1] = next_resno;
/* Remove groupclause from our list of unmatched groupclauses.
* NB: this depends on having used a shallow listCopy() above.
/*
* Remove groupclause from our list of unmatched
* groupclauses. NB: this depends on having used a shallow
* listCopy() above.
*/
glc = lremove((void*) grpcl, glc);
glc = lremove((void *) grpcl, glc);
break;
}
}
if (! foundGroupClause)
if (!foundGroupClause)
{
/*
* Non-GroupBy entry: remove it from subplan if there are
* aggregates in query - it will be evaluated by Aggregate plan.
* But do not remove simple-Var entries; we'd just have to add
* them back anyway, and we risk confusing INSERT/UPDATE.
* aggregates in query - it will be evaluated by Aggregate
* plan. But do not remove simple-Var entries; we'd just have
* to add them back anyway, and we risk confusing
* INSERT/UPDATE.
*/
if (parse->hasAggs && ! IsA(te->expr, Var))
if (parse->hasAggs && !IsA(te->expr, Var))
keepInSubPlan = false;
}
@@ -458,15 +473,16 @@ make_subplanTargetList(Query *parse,
{
/* Assign new sequential resnos to subplan tlist items */
resdom->resno = next_resno++;
if (! IsA(parentte->expr, Var))
if (!IsA(parentte->expr, Var))
{
/* Since the item is being computed in the subplan,
* we can just make a Var node to reference it in the
* outer plan, rather than recomputing it there.
* Note we use varnoold = -1 as a flag to let
* replace_vars_with_subplan_refs know it needn't change
* this Var node.
* If it's only a Var anyway, we leave it alone for now;
/*
* Since the item is being computed in the subplan, we can
* just make a Var node to reference it in the outer plan,
* rather than recomputing it there. Note we use varnoold
* = -1 as a flag to let replace_vars_with_subplan_refs
* know it needn't change this Var node. If it's only a
* Var anyway, we leave it alone for now;
* replace_vars_with_subplan_refs will fix it later.
*/
parentte->expr = (Node *) makeVar(1, resdom->resno,
@@ -477,9 +493,11 @@ make_subplanTargetList(Query *parse,
}
else
{
/* Remove this tlist item from the subplan, but remember the
* vars it needs. The outer tlist item probably needs changes,
* but that will happen later.
/*
* Remove this tlist item from the subplan, but remember the
* vars it needs. The outer tlist item probably needs
* changes, but that will happen later.
*/
sub_tlist = lremove(te, sub_tlist);
extravars = nconc(extravars, pull_var_clause(te->expr));
@@ -493,8 +511,8 @@ make_subplanTargetList(Query *parse,
elog(ERROR, "make_subplanTargetList: GROUP BY attribute not found in target list");
/*
* Add subplan targets for any variables needed by removed tlist entries
* that aren't otherwise mentioned in the subplan target list.
* Add subplan targets for any variables needed by removed tlist
* entries that aren't otherwise mentioned in the subplan target list.
* We'll also need targets for any variables seen only in HAVING.
*/
extravars = nconc(extravars, pull_var_clause(parse->havingQual));
@@ -505,9 +523,11 @@ make_subplanTargetList(Query *parse,
if (tlist_member(v, sub_tlist) == NULL)
{
/* Make sure sub_tlist element is a fresh object not shared with
* any other structure; not sure if anything will break if it is
* shared, but better to be safe...
/*
* Make sure sub_tlist element is a fresh object not shared
* with any other structure; not sure if anything will break
* if it is shared, but better to be safe...
*/
sub_tlist = lappend(sub_tlist,
create_tl_element((Var *) copyObject(v),
@@ -535,9 +555,9 @@ make_groupplan(List *group_tlist,
/*
* Make the targetlist for the Sort node; it always just references
* each of the corresponding target items of the subplan. We need to
* ensure that simple Vars in the subplan's target list are recognizable
* by replace_vars_with_subplan_refs when it's applied to the Sort/Group
* target list, so copy up their varnoold/varoattno.
* ensure that simple Vars in the subplan's target list are
* recognizable by replace_vars_with_subplan_refs when it's applied to
* the Sort/Group target list, so copy up their varnoold/varoattno.
*/
sort_tlist = NIL;
foreach(sl, subplan->targetlist)
@@ -548,7 +568,8 @@ make_groupplan(List *group_tlist,
if (IsA(te->expr, Var))
{
Var *subvar = (Var *) te->expr;
Var *subvar = (Var *) te->expr;
newvar = makeVar(1, resdom->resno,
resdom->restype, resdom->restypmod,
0, subvar->varnoold, subvar->varoattno);
@@ -561,8 +582,8 @@ make_groupplan(List *group_tlist,
}
sort_tlist = lappend(sort_tlist,
makeTargetEntry((Resdom *) copyObject(resdom),
(Node *) newvar));
makeTargetEntry((Resdom *) copyObject(resdom),
(Node *) newvar));
}
/*
@@ -575,20 +596,19 @@ make_groupplan(List *group_tlist,
sortplan->plan.cost = subplan->cost; /* XXX assume no cost */
/*
* If the caller gave us a target list, use it after fixing the variables.
* If not, we need the same sort of "repeater" tlist as for the Sort node.
* If the caller gave us a target list, use it after fixing the
* variables. If not, we need the same sort of "repeater" tlist as for
* the Sort node.
*/
if (group_tlist)
{
group_tlist = copyObject(group_tlist); /* necessary?? */
group_tlist = copyObject(group_tlist); /* necessary?? */
replace_tlist_with_subplan_refs(group_tlist,
(Index) 0,
subplan->targetlist);
}
else
{
group_tlist = copyObject(sort_tlist);
}
/*
* Make the Group node
@@ -686,7 +706,7 @@ pg_checkretval(Oid rettype, List *queryTreeList)
int i;
/* find the final query */
parse = (Query *) nth(length(queryTreeList)-1, queryTreeList);
parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
/*
* test 1: if the last query is a utility invocation, then there had
@@ -787,7 +807,7 @@ pg_checkretval(Oid rettype, List *queryTreeList)
tlist = lnext(tlist);
tletype = exprType(thenode);
#ifdef NOT_USED /* fix me */
#ifdef NOT_USED /* fix me */
/* this is tedious */
if (IsA(thenode, Var))
tletype = (Oid) ((Var *) thenode)->vartype;
@@ -830,11 +850,12 @@ pg_checkretval(Oid rettype, List *queryTreeList)
static TargetEntry *
get_matching_tle(Plan *plan, Resdom *resdom)
{
List *i;
TargetEntry *tle;
List *i;
TargetEntry *tle;
foreach (i, plan->targetlist) {
tle = (TargetEntry *)lfirst(i);
foreach(i, plan->targetlist)
{
tle = (TargetEntry *) lfirst(i);
if (tle->resdom->resno == resdom->resno)
return tle;
}
@@ -853,50 +874,45 @@ static bool
need_sortplan(List *sortcls, Plan *plan)
{
Relation indexRel;
IndexScan *indexScan;
Oid indexId;
List *i;
IndexScan *indexScan;
Oid indexId;
List *i;
HeapTuple htup;
Form_pg_index index_tup;
int key_no = 0;
Form_pg_index index_tup;
int key_no = 0;
/* ----------
* Must be an IndexScan
* ----------
*/
if (nodeTag(plan) != T_IndexScan) {
if (nodeTag(plan) != T_IndexScan)
return TRUE;
}
indexScan = (IndexScan *)plan;
indexScan = (IndexScan *) plan;
/* ----------
* Should not have left- or righttree
* ----------
*/
if (plan->lefttree != NULL) {
if (plan->lefttree != NULL)
return TRUE;
}
if (plan->righttree != NULL) {
if (plan->righttree != NULL)
return TRUE;
}
/* ----------
* Must be a single index scan
* ----------
*/
if (length(indexScan->indxid) != 1) {
if (length(indexScan->indxid) != 1)
return TRUE;
}
/* ----------
* Indices can only have up to 8 attributes. So an ORDER BY using
* more that 8 attributes could never be satisfied by an index.
* ----------
*/
if (length(sortcls) > 8) {
if (length(sortcls) > 8)
return TRUE;
}
/* ----------
* The choosen Index must be a btree
@@ -905,7 +921,8 @@ need_sortplan(List *sortcls, Plan *plan)
indexId = lfirsti(indexScan->indxid);
indexRel = index_open(indexId);
if (strcmp(nameout(&(indexRel->rd_am->amname)), "btree") != 0) {
if (strcmp(nameout(&(indexRel->rd_am->amname)), "btree") != 0)
{
heap_close(indexRel);
return TRUE;
}
@@ -916,34 +933,36 @@ need_sortplan(List *sortcls, Plan *plan)
* ----------
*/
htup = SearchSysCacheTuple(INDEXRELID,
ObjectIdGetDatum(indexId), 0, 0, 0);
if (!HeapTupleIsValid(htup)) {
ObjectIdGetDatum(indexId), 0, 0, 0);
if (!HeapTupleIsValid(htup))
elog(ERROR, "cache lookup for index %u failed", indexId);
}
index_tup = (Form_pg_index) GETSTRUCT(htup);
/* ----------
* Check if all the sort clauses match the attributes in the index
* ----------
*/
foreach (i, sortcls) {
SortClause *sortcl;
Resdom *resdom;
TargetEntry *tle;
Var *var;
foreach(i, sortcls)
{
SortClause *sortcl;
Resdom *resdom;
TargetEntry *tle;
Var *var;
sortcl = (SortClause *) lfirst(i);
resdom = sortcl->resdom;
tle = get_matching_tle(plan, resdom);
if (tle == NULL) {
if (tle == NULL)
{
/* ----------
* Could this happen?
* ----------
*/
return TRUE;
}
if (nodeTag(tle->expr) != T_Var) {
if (nodeTag(tle->expr) != T_Var)
{
/* ----------
* The target list expression isn't a var, so it
* cannot be the indexed attribute
@@ -951,9 +970,10 @@ need_sortplan(List *sortcls, Plan *plan)
*/
return TRUE;
}
var = (Var *)(tle->expr);
var = (Var *) (tle->expr);
if (var->varno != indexScan->scan.scanrelid) {
if (var->varno != indexScan->scan.scanrelid)
{
/* ----------
* This Var isn't from the scan relation. So it isn't
* that of the index
@@ -962,7 +982,8 @@ need_sortplan(List *sortcls, Plan *plan)
return TRUE;
}
if (var->varattno != index_tup->indkey[key_no]) {
if (var->varattno != index_tup->indkey[key_no])
{
/* ----------
* It isn't the indexed attribute.
* ----------
@@ -970,7 +991,8 @@ need_sortplan(List *sortcls, Plan *plan)
return TRUE;
}
if (oprid(oper("<", resdom->restype, resdom->restype, FALSE)) != sortcl->opoid) {
if (oprid(oper("<", resdom->restype, resdom->restype, FALSE)) != sortcl->opoid)
{
/* ----------
* Sort order isn't in ascending order.
* ----------
@@ -987,4 +1009,3 @@ need_sortplan(List *sortcls, Plan *plan)
*/
return FALSE;
}

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.46 1999/05/12 15:01:39 wieck Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.47 1999/05/25 16:09:39 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -35,13 +35,13 @@
static void set_join_tlist_references(Join *join);
static void set_nonamescan_tlist_references(SeqScan *nonamescan);
static void set_noname_tlist_references(Noname *noname);
static void set_noname_tlist_references(Noname * noname);
static Node *replace_clause_joinvar_refs(Node *clause,
List *outer_tlist,
List *inner_tlist);
List *outer_tlist,
List *inner_tlist);
static Var *replace_joinvar_refs(Var *var,
List *outer_tlist,
List *inner_tlist);
List *outer_tlist,
List *inner_tlist);
static List *tlist_noname_references(Oid nonameid, List *tlist);
static bool OperandIsInner(Node *opnd, int inner_relid);
static List *pull_agg_clause(Node *clause);
@@ -114,11 +114,12 @@ set_join_tlist_references(Join *join)
foreach(entry, qptlist)
{
TargetEntry *xtl = (TargetEntry *) lfirst(entry);
Node *joinvar = replace_clause_joinvar_refs(xtl->expr,
outer_tlist,
inner_tlist);
Node *joinvar = replace_clause_joinvar_refs(xtl->expr,
outer_tlist,
inner_tlist);
new_join_targetlist = lappend(new_join_targetlist,
makeTargetEntry(xtl->resdom, joinvar));
makeTargetEntry(xtl->resdom, joinvar));
}
((Plan *) join)->targetlist = new_join_targetlist;
@@ -144,7 +145,7 @@ set_nonamescan_tlist_references(SeqScan *nonamescan)
Noname *noname = (Noname *) ((Plan *) nonamescan)->lefttree;
((Plan *) nonamescan)->targetlist = tlist_noname_references(noname->nonameid,
((Plan *) nonamescan)->targetlist);
((Plan *) nonamescan)->targetlist);
set_noname_tlist_references(noname);
}
@@ -160,7 +161,7 @@ set_nonamescan_tlist_references(SeqScan *nonamescan)
*
*/
static void
set_noname_tlist_references(Noname *noname)
set_noname_tlist_references(Noname * noname)
{
Plan *source = ((Plan *) noname)->lefttree;
@@ -168,7 +169,7 @@ set_noname_tlist_references(Noname *noname)
{
set_tlist_references(source);
((Plan *) noname)->targetlist = copy_vars(((Plan *) noname)->targetlist,
(source)->targetlist);
(source)->targetlist);
}
else
elog(ERROR, "calling set_noname_tlist_references with empty lefttree");
@@ -235,9 +236,9 @@ index_outerjoin_references(List *inner_indxqual,
if (OperandIsInner((Node *) get_rightop(clause), inner_relid))
{
Var *joinvar = (Var *)
replace_clause_joinvar_refs((Node *) get_leftop(clause),
outer_tlist,
NIL);
replace_clause_joinvar_refs((Node *) get_leftop(clause),
outer_tlist,
NIL);
temp = make_opclause(replace_opid((Oper *) ((Expr *) clause)->oper),
joinvar,
@@ -248,9 +249,9 @@ index_outerjoin_references(List *inner_indxqual,
{
/* inner scan on left */
Var *joinvar = (Var *)
replace_clause_joinvar_refs((Node *) get_rightop(clause),
outer_tlist,
NIL);
replace_clause_joinvar_refs((Node *) get_rightop(clause),
outer_tlist,
NIL);
temp = make_opclause(replace_opid((Oper *) ((Expr *) clause)->oper),
get_leftop(clause),
@@ -290,8 +291,9 @@ replace_clause_joinvar_refs(Node *clause,
return NULL;
if (IsA(clause, Var))
{
Var *temp = replace_joinvar_refs((Var *) clause,
outer_tlist, inner_tlist);
Var *temp = replace_joinvar_refs((Var *) clause,
outer_tlist, inner_tlist);
if (temp != NULL)
return (Node *) temp;
else
@@ -344,32 +346,32 @@ replace_clause_joinvar_refs(Node *clause,
else if (is_funcclause(clause))
{
return (Node *) make_funcclause(
(Func *) ((Expr *) clause)->oper,
(List *) replace_clause_joinvar_refs(
(Node *) ((Expr *) clause)->args,
outer_tlist,
inner_tlist));
(Func *) ((Expr *) clause)->oper,
(List *) replace_clause_joinvar_refs(
(Node *) ((Expr *) clause)->args,
outer_tlist,
inner_tlist));
}
else if (not_clause(clause))
{
return (Node *) make_notclause((Expr *)
replace_clause_joinvar_refs(
(Node *) get_notclausearg((Expr *) clause),
outer_tlist,
inner_tlist));
replace_clause_joinvar_refs(
(Node *) get_notclausearg((Expr *) clause),
outer_tlist,
inner_tlist));
}
else if (is_opclause(clause))
{
return (Node *) make_opclause(
replace_opid((Oper *) ((Expr *) clause)->oper),
(Var *) replace_clause_joinvar_refs(
(Node *) get_leftop((Expr *) clause),
outer_tlist,
inner_tlist),
(Var *) replace_clause_joinvar_refs(
(Node *) get_rightop((Expr *) clause),
outer_tlist,
inner_tlist));
replace_opid((Oper *) ((Expr *) clause)->oper),
(Var *) replace_clause_joinvar_refs(
(Node *) get_leftop((Expr *) clause),
outer_tlist,
inner_tlist),
(Var *) replace_clause_joinvar_refs(
(Node *) get_rightop((Expr *) clause),
outer_tlist,
inner_tlist));
}
else if (IsA(clause, List))
{
@@ -379,25 +381,26 @@ replace_clause_joinvar_refs(Node *clause,
foreach(subclause, (List *) clause)
{
t_list = lappend(t_list,
replace_clause_joinvar_refs(lfirst(subclause),
outer_tlist,
inner_tlist));
replace_clause_joinvar_refs(lfirst(subclause),
outer_tlist,
inner_tlist));
}
return (Node *) t_list;
}
else if (is_subplan(clause))
{
/* This is a tad wasteful of space, but it works... */
Expr *newclause = (Expr *) copyObject(clause);
Expr *newclause = (Expr *) copyObject(clause);
newclause->args = (List *)
replace_clause_joinvar_refs((Node *) newclause->args,
outer_tlist,
inner_tlist);
((SubPlan *) newclause->oper)->sublink->oper = (List *)
replace_clause_joinvar_refs(
(Node *) ((SubPlan *) newclause->oper)->sublink->oper,
outer_tlist,
inner_tlist);
(Node *) ((SubPlan *) newclause->oper)->sublink->oper,
outer_tlist,
inner_tlist);
return (Node *) newclause;
}
else if (IsA(clause, CaseExpr))
@@ -406,7 +409,8 @@ replace_clause_joinvar_refs(Node *clause,
CaseExpr *newnode = makeNode(CaseExpr);
newnode->casetype = oldnode->casetype;
newnode->arg = oldnode->arg; /* XXX should always be null anyway ... */
newnode->arg = oldnode->arg; /* XXX should always be null
* anyway ... */
newnode->args = (List *)
replace_clause_joinvar_refs((Node *) oldnode->args,
outer_tlist,
@@ -494,7 +498,7 @@ replace_joinvar_refs(Var *var, List *outer_tlist, List *inner_tlist)
*/
static List *
tlist_noname_references(Oid nonameid,
List *tlist)
List *tlist)
{
List *t_list = NIL;
TargetEntry *noname = (TargetEntry *) NULL;
@@ -512,13 +516,13 @@ tlist_noname_references(Oid nonameid,
oattno = 0;
noname = makeTargetEntry(xtl->resdom,
(Node *) makeVar(nonameid,
xtl->resdom->resno,
xtl->resdom->restype,
xtl->resdom->restypmod,
0,
nonameid,
oattno));
(Node *) makeVar(nonameid,
xtl->resdom->resno,
xtl->resdom->restype,
xtl->resdom->restypmod,
0,
nonameid,
oattno));
t_list = lappend(t_list, noname);
}
@@ -573,11 +577,12 @@ replace_tlist_with_subplan_refs(List *tlist,
Index subvarno,
List *subplanTargetList)
{
List *t;
List *t;
foreach(t, tlist)
{
TargetEntry *entry = (TargetEntry *) lfirst(t);
replace_vars_with_subplan_refs((Node *) get_expr(entry),
subvarno, subplanTargetList);
}
@@ -609,19 +614,20 @@ replace_vars_with_subplan_refs(Node *clause,
return;
if (IsA(clause, Var))
{
/*
* Ha! A Var node!
*
* It could be that this varnode has been created by make_groupplan
* and is already set up to reference the subplan target list.
* We recognize that case by varno = 1, varnoold = -1,
* varattno = varoattno, and varlevelsup = 0. (Probably ought to
* have an explicit flag, but this should do for now.)
* and is already set up to reference the subplan target list. We
* recognize that case by varno = 1, varnoold = -1, varattno =
* varoattno, and varlevelsup = 0. (Probably ought to have an
* explicit flag, but this should do for now.)
*/
Var *var = (Var *) clause;
Var *var = (Var *) clause;
TargetEntry *subplanVar;
if (var->varno == (Index) 1 &&
if (var->varno == (Index) 1 &&
var->varnoold == ((Index) -1) &&
var->varattno == var->varoattno &&
var->varlevelsup == 0)
@@ -629,7 +635,7 @@ replace_vars_with_subplan_refs(Node *clause,
/* Otherwise it had better be in the subplan list. */
subplanVar = match_varid(var, subplanTargetList);
if (! subplanVar)
if (!subplanVar)
elog(ERROR, "replace_vars_with_subplan_refs: variable not in target list");
/*
@@ -656,10 +662,11 @@ replace_vars_with_subplan_refs(Node *clause,
}
else if (IsA(clause, Expr))
{
/*
* Recursively scan the arguments of an expression.
* NOTE: this must come after is_subplan() case since
* subplan is a kind of Expr node.
* Recursively scan the arguments of an expression. NOTE: this
* must come after is_subplan() case since subplan is a kind of
* Expr node.
*/
foreach(t, ((Expr *) clause)->args)
replace_vars_with_subplan_refs(lfirst(t),
@@ -671,6 +678,7 @@ replace_vars_with_subplan_refs(Node *clause,
else if (IsA(clause, ArrayRef))
{
ArrayRef *aref = (ArrayRef *) clause;
foreach(t, aref->refupperindexpr)
replace_vars_with_subplan_refs(lfirst(t),
subvarno, subplanTargetList);
@@ -687,6 +695,7 @@ replace_vars_with_subplan_refs(Node *clause,
foreach(t, ((CaseExpr *) clause)->args)
{
CaseWhen *when = (CaseWhen *) lfirst(t);
replace_vars_with_subplan_refs(when->expr,
subvarno, subplanTargetList);
replace_vars_with_subplan_refs(when->result,
@@ -767,15 +776,16 @@ set_agg_tlist_references(Agg *aggNode)
all_quals_ok = true;
foreach(ql, aggNode->plan.qual)
{
Node *qual = lfirst(ql);
List *qualaggs;
Node *qual = lfirst(ql);
List *qualaggs;
replace_vars_with_subplan_refs(qual,
(Index) 0,
subplanTargetList);
qualaggs = pull_agg_clause(qual);
if (qualaggs == NIL)
all_quals_ok = false; /* this qual clause has no agg functions! */
all_quals_ok = false; /* this qual clause has no agg
* functions! */
else
aggNode->aggs = nconc(qualaggs, aggNode->aggs);
}
@@ -800,7 +810,7 @@ pull_agg_clause(Node *clause)
return pull_agg_clause(((Iter *) clause)->iterexpr);
else if (is_subplan(clause))
{
SubLink *sublink = ((SubPlan *) ((Expr *) clause)->oper)->sublink;
SubLink *sublink = ((SubPlan *) ((Expr *) clause)->oper)->sublink;
/*
* Only the lefthand side of the sublink should be checked for
@@ -814,10 +824,11 @@ pull_agg_clause(Node *clause)
}
else if (IsA(clause, Expr))
{
/*
* Recursively scan the arguments of an expression.
* NOTE: this must come after is_subplan() case since
* subplan is a kind of Expr node.
* Recursively scan the arguments of an expression. NOTE: this
* must come after is_subplan() case since subplan is a kind of
* Expr node.
*/
foreach(t, ((Expr *) clause)->args)
agg_list = nconc(pull_agg_clause(lfirst(t)), agg_list);
@@ -830,6 +841,7 @@ pull_agg_clause(Node *clause)
else if (IsA(clause, ArrayRef))
{
ArrayRef *aref = (ArrayRef *) clause;
foreach(t, aref->refupperindexpr)
agg_list = nconc(pull_agg_clause(lfirst(t)), agg_list);
foreach(t, aref->reflowerindexpr)
@@ -842,6 +854,7 @@ pull_agg_clause(Node *clause)
foreach(t, ((CaseExpr *) clause)->args)
{
CaseWhen *when = (CaseWhen *) lfirst(t);
agg_list = nconc(agg_list, pull_agg_clause(when->expr));
agg_list = nconc(agg_list, pull_agg_clause(when->result));
}
@@ -951,7 +964,7 @@ del_agg_clause(Node *clause)
* GROUP BY clauses and checks for subplans in the havingQual that are being
* passed ungrouped variables as parameters. In other contexts, ungrouped
* vars in the havingQual will be detected by the parser (see parse_agg.c,
* exprIsAggOrGroupCol()). But that routine currently does not check subplans,
* exprIsAggOrGroupCol()). But that routine currently does not check subplans,
* because the necessary info is not computed until the planner runs.
* This ought to be cleaned up someday.
*
@@ -962,7 +975,7 @@ del_agg_clause(Node *clause)
void
check_having_for_ungrouped_vars(Node *clause, List *groupClause,
List *targetList)
List *targetList)
{
List *t;
@@ -971,8 +984,10 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause,
if (IsA(clause, Var))
{
/* Ignore vars elsewhere in the having clause, since the
* parser already checked 'em.
/*
* Ignore vars elsewhere in the having clause, since the parser
* already checked 'em.
*/
}
else if (single_node(clause))
@@ -986,20 +1001,21 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause,
}
else if (is_subplan(clause))
{
/*
* The args list of the subplan node represents attributes from outside
* passed into the sublink.
* The args list of the subplan node represents attributes from
* outside passed into the sublink.
*/
foreach(t, ((Expr *) clause)->args)
{
bool contained_in_group_clause = false;
bool contained_in_group_clause = false;
List *gl;
foreach(gl, groupClause)
{
if (var_equal(lfirst(t),
get_groupclause_expr((GroupClause *)
lfirst(gl), targetList)))
get_groupclause_expr((GroupClause *)
lfirst(gl), targetList)))
{
contained_in_group_clause = true;
break;
@@ -1012,23 +1028,25 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause,
}
else if (IsA(clause, Expr))
{
/*
* Recursively scan the arguments of an expression.
* NOTE: this must come after is_subplan() case since
* subplan is a kind of Expr node.
* Recursively scan the arguments of an expression. NOTE: this
* must come after is_subplan() case since subplan is a kind of
* Expr node.
*/
foreach(t, ((Expr *) clause)->args)
check_having_for_ungrouped_vars(lfirst(t), groupClause,
targetList);
targetList);
}
else if (IsA(clause, List))
{
/*
* Recursively scan AND subclauses (see NOTE above).
*/
foreach(t, ((List *) clause))
check_having_for_ungrouped_vars(lfirst(t), groupClause,
targetList);
targetList);
}
else if (IsA(clause, Aggref))
{
@@ -1045,24 +1063,25 @@ check_having_for_ungrouped_vars(Node *clause, List *groupClause,
*/
foreach(t, aref->refupperindexpr)
check_having_for_ungrouped_vars(lfirst(t), groupClause,
targetList);
targetList);
foreach(t, aref->reflowerindexpr)
check_having_for_ungrouped_vars(lfirst(t), groupClause,
targetList);
targetList);
check_having_for_ungrouped_vars(aref->refexpr, groupClause,
targetList);
targetList);
check_having_for_ungrouped_vars(aref->refassgnexpr, groupClause,
targetList);
targetList);
}
else if (case_clause(clause))
{
foreach(t, ((CaseExpr *) clause)->args)
{
CaseWhen *when = (CaseWhen *) lfirst(t);
check_having_for_ungrouped_vars(when->expr, groupClause,
targetList);
targetList);
check_having_for_ungrouped_vars(when->result, groupClause,
targetList);
targetList);
}
check_having_for_ungrouped_vars(((CaseExpr *) clause)->defresult,
groupClause, targetList);

View File

@@ -338,9 +338,7 @@ SS_replace_correlation_vars(Node *expr)
}
}
else if (IsA(expr, Iter))
{
((Iter *) expr)->iterexpr = SS_replace_correlation_vars(((Iter *) expr)->iterexpr);
}
else if (single_node(expr))
return expr;
else if (or_clause(expr) || and_clause(expr) || is_opclause(expr) ||
@@ -398,9 +396,7 @@ SS_process_sublinks(Node *expr)
((Expr *) expr)->args = (List *)
SS_process_sublinks((Node *) ((Expr *) expr)->args);
else if (IsA(expr, SubLink))/* got it! */
{
expr = _make_subplan((SubLink *) expr);
}
return expr;
}
@@ -529,13 +525,13 @@ SS_pull_subplan(Node *expr)
return SS_pull_subplan(((Aggref *) expr)->target);
else if (IsA(expr, ArrayRef))
{
result = SS_pull_subplan((Node *)((ArrayRef *) expr)->refupperindexpr);
result = SS_pull_subplan((Node *) ((ArrayRef *) expr)->refupperindexpr);
result = nconc(result,
SS_pull_subplan((Node*) ((ArrayRef *) expr)->reflowerindexpr));
SS_pull_subplan((Node *) ((ArrayRef *) expr)->reflowerindexpr));
result = nconc(result,
SS_pull_subplan(((ArrayRef *) expr)->refexpr));
result = nconc(result,
SS_pull_subplan(((ArrayRef *) expr)->refassgnexpr));
SS_pull_subplan(((ArrayRef *) expr)->refassgnexpr));
}
else if (IsA(expr, TargetEntry))
return SS_pull_subplan(((TargetEntry *) expr)->expr);