Skip to content

Commit b5e52b0

Browse files
committed
Tweak findTargetlistEntry so that bare names occurring in GROUP BY clauses
are sought first as local FROM columns, then as local SELECT-list aliases, and finally as outer FROM columns; the former behavior made outer FROM columns take precedence over aliases. This does not change spec conformance because SQL99 allows only the first case anyway, and it seems more useful and self-consistent. Per gripe from Dennis Bjorklund 2004-04-05.
1 parent 3e2aef5 commit b5e52b0

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/backend/parser/parse_clause.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.127 2004/01/23 02:13:12 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.128 2004/04/18 18:12:57 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1140,10 +1140,18 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
11401140
* is a matching column. If so, fall through to let
11411141
* transformExpr() do the rest. NOTE: if name could refer
11421142
* ambiguously to more than one column name exposed by FROM,
1143-
* colnameToVar will ereport(ERROR). That's just what we want
1143+
* colNameToVar will ereport(ERROR). That's just what we want
11441144
* here.
1145+
*
1146+
* Small tweak for 7.4.3: ignore matches in upper query levels.
1147+
* This effectively changes the search order for bare names to
1148+
* (1) local FROM variables, (2) local targetlist aliases,
1149+
* (3) outer FROM variables, whereas before it was (1) (3) (2).
1150+
* SQL92 and SQL99 do not allow GROUPing BY an outer reference,
1151+
* so this breaks no cases that are legal per spec, and it
1152+
* seems a more self-consistent behavior.
11451153
*/
1146-
if (colnameToVar(pstate, name) != NULL)
1154+
if (colNameToVar(pstate, name, true) != NULL)
11471155
name = NULL;
11481156
}
11491157

src/backend/parser/parse_expr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_expr.c,v 1.168 2004/04/02 19:06:58 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_expr.c,v 1.169 2004/04/18 18:12:58 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -966,7 +966,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
966966
char *name = strVal(lfirst(cref->fields));
967967

968968
/* Try to identify as an unqualified column */
969-
node = colnameToVar(pstate, name);
969+
node = colNameToVar(pstate, name, false);
970970

971971
if (node == NULL)
972972
{

src/backend/parser/parse_relation.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.93 2004/04/02 19:06:58 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.94 2004/04/18 18:12:58 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -531,13 +531,14 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname)
531531
}
532532

533533
/*
534-
* colnameToVar
534+
* colNameToVar
535535
* Search for an unqualified column name.
536536
* If found, return the appropriate Var node (or expression).
537537
* If not found, return NULL. If the name proves ambiguous, raise error.
538+
* If localonly is true, only names in the innermost query are considered.
538539
*/
539540
Node *
540-
colnameToVar(ParseState *pstate, char *colname)
541+
colNameToVar(ParseState *pstate, char *colname, bool localonly)
541542
{
542543
Node *result = NULL;
543544
ParseState *orig_pstate = pstate;
@@ -594,8 +595,8 @@ colnameToVar(ParseState *pstate, char *colname)
594595
}
595596
}
596597

597-
if (result != NULL)
598-
break; /* found */
598+
if (result != NULL || localonly)
599+
break; /* found, or don't want to look at parent */
599600

600601
pstate = pstate->parentParseState;
601602
levels_up++;

src/include/parser/parse_relation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.43 2004/04/02 19:07:02 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.44 2004/04/18 18:12:58 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -32,7 +32,7 @@ extern RangeTblEntry *GetRTEByRangeTablePosn(ParseState *pstate,
3232
int sublevels_up);
3333
extern Node *scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
3434
char *colname);
35-
extern Node *colnameToVar(ParseState *pstate, char *colname);
35+
extern Node *colNameToVar(ParseState *pstate, char *colname, bool localonly);
3636
extern Node *qualifiedNameToVar(ParseState *pstate,
3737
char *schemaname,
3838
char *refname,

0 commit comments

Comments
 (0)