Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 00f1a41

Browse files
committed
Make flatten_join_alias_vars() do the right thing when expanding an alias
referenced from a subquery. Per example from Stefanos Harhalakis.
1 parent 77ede89 commit 00f1a41

File tree

1 file changed

+14
-2
lines changed
  • src/backend/optimizer/util

1 file changed

+14
-2
lines changed

src/backend/optimizer/util/var.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* 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 $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -19,6 +19,7 @@
1919
#include "optimizer/prep.h"
2020
#include "optimizer/var.h"
2121
#include "parser/parsetree.h"
22+
#include "rewrite/rewriteManip.h"
2223

2324

2425
typedef struct
@@ -400,14 +401,25 @@ flatten_join_alias_vars_mutator(Node *node,
400401
RangeTblEntry *rte;
401402
Node *newvar;
402403

404+
/* No change unless Var belongs to a JOIN of the target level */
403405
if (var->varlevelsup != context->sublevels_up)
404406
return node; /* no need to copy, really */
405407
rte = rt_fetch(var->varno, context->root->rtable);
406408
if (rte->rtekind != RTE_JOIN)
407409
return node;
408410
Assert(var->varattno > 0);
411+
/* Okay, must expand it */
409412
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 */
411423
return flatten_join_alias_vars_mutator(newvar, context);
412424
}
413425
if (IsA(node, InClauseInfo))

0 commit comments

Comments
 (0)