|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.47 2003/01/20 18:54:58 tgl Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.48 2003/02/06 22:21:11 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
|
19 | 19 | #include "optimizer/prep.h"
|
20 | 20 | #include "optimizer/var.h"
|
21 | 21 | #include "parser/parsetree.h"
|
| 22 | +#include "rewrite/rewriteManip.h" |
22 | 23 |
|
23 | 24 |
|
24 | 25 | typedef struct
|
@@ -400,14 +401,25 @@ flatten_join_alias_vars_mutator(Node *node,
|
400 | 401 | RangeTblEntry *rte;
|
401 | 402 | Node *newvar;
|
402 | 403 |
|
| 404 | + /* No change unless Var belongs to a JOIN of the target level */ |
403 | 405 | if (var->varlevelsup != context->sublevels_up)
|
404 | 406 | return node; /* no need to copy, really */
|
405 | 407 | rte = rt_fetch(var->varno, context->root->rtable);
|
406 | 408 | if (rte->rtekind != RTE_JOIN)
|
407 | 409 | return node;
|
408 | 410 | Assert(var->varattno > 0);
|
| 411 | + /* Okay, must expand it */ |
409 | 412 | newvar = (Node *) nth(var->varattno - 1, rte->joinaliasvars);
|
410 |
| - /* expand it; recurse in case join input is itself a join */ |
| 413 | + /* |
| 414 | + * If we are expanding an alias carried down from an upper query, |
| 415 | + * must adjust its varlevelsup fields. |
| 416 | + */ |
| 417 | + if (context->sublevels_up != 0) |
| 418 | + { |
| 419 | + newvar = copyObject(newvar); |
| 420 | + IncrementVarSublevelsUp(newvar, context->sublevels_up, 0); |
| 421 | + } |
| 422 | + /* Recurse in case join input is itself a join */ |
411 | 423 | return flatten_join_alias_vars_mutator(newvar, context);
|
412 | 424 | }
|
413 | 425 | if (IsA(node, InClauseInfo))
|
|
0 commit comments