Ignore resjunk targetlist entries when matching arguments to

a SubLink with the subplan's targetlist.  This fixes a problem seen with,
for example, a subselect that uses GROUP BY.
This commit is contained in:
Tom Lane 1999-07-11 02:04:19 +00:00
parent 8c32f99f67
commit eeb3abe017
1 changed files with 33 additions and 22 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.49 1999/05/26 12:55:37 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.50 1999/07/11 02:04:19 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -278,7 +278,6 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
SubLink *sublink = (SubLink *) expr; SubLink *sublink = (SubLink *) expr;
List *qtrees; List *qtrees;
Query *qtree; Query *qtree;
List *llist;
pstate->p_hasSubLinks = true; pstate->p_hasSubLinks = true;
qtrees = parse_analyze(lcons(sublink->subselect, NIL), pstate); qtrees = parse_analyze(lcons(sublink->subselect, NIL), pstate);
@ -293,36 +292,48 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
if (sublink->subLinkType != EXISTS_SUBLINK) if (sublink->subLinkType != EXISTS_SUBLINK)
{ {
char *op = lfirst(sublink->oper); char *op = lfirst(sublink->oper);
List *left_expr = sublink->lefthand; List *left_list = sublink->lefthand;
List *right_expr = ((Query *) sublink->subselect)->targetList; List *right_list = qtree->targetList;
List *elist; List *elist;
foreach(llist, left_expr) foreach(elist, left_list)
lfirst(llist) = transformExpr(pstate, lfirst(llist), precedence); lfirst(elist) = transformExpr(pstate, lfirst(elist),
precedence);
if (length(left_expr) != length(right_expr)) if (length(left_list) > 1 &&
elog(ERROR, "parser: Subselect has too many or too few fields.");
if (length(left_expr) > 1 &&
strcmp(op, "=") != 0 && strcmp(op, "<>") != 0) strcmp(op, "=") != 0 && strcmp(op, "<>") != 0)
elog(ERROR, "parser: '%s' is not relational operator", op); elog(ERROR, "parser: '%s' is not relational operator",
op);
sublink->oper = NIL; sublink->oper = NIL;
foreach(elist, left_expr)
/* Scan subquery's targetlist to find values that will be
* matched against lefthand values. We need to ignore
* resjunk targets, so doing the outer iteration over
* right_list is easier than doing it over left_list.
*/
while (right_list != NIL)
{ {
Node *lexpr = lfirst(elist); TargetEntry *tent = (TargetEntry *) lfirst(right_list);
Node *rexpr = lfirst(right_expr); Node *lexpr;
TargetEntry *tent = (TargetEntry *) rexpr;
Expr *op_expr; Expr *op_expr;
op_expr = make_op(op, lexpr, tent->expr); if (! tent->resdom->resjunk)
{
if (op_expr->typeOid != BOOLOID && if (left_list == NIL)
sublink->subLinkType != EXPR_SUBLINK) elog(ERROR, "parser: Subselect has too many fields.");
elog(ERROR, "parser: '%s' must return 'bool' to be used with quantified predicate subquery", op); lexpr = lfirst(left_list);
sublink->oper = lappend(sublink->oper, op_expr); left_list = lnext(left_list);
right_expr = lnext(right_expr); op_expr = make_op(op, lexpr, tent->expr);
if (op_expr->typeOid != BOOLOID &&
sublink->subLinkType != EXPR_SUBLINK)
elog(ERROR, "parser: '%s' must return 'bool' to be used with quantified predicate subquery", op);
sublink->oper = lappend(sublink->oper, op_expr);
}
right_list = lnext(right_list);
} }
if (left_list != NIL)
elog(ERROR, "parser: Subselect has too few fields.");
} }
else else
sublink->oper = NIL; sublink->oper = NIL;