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

Commit 66392d3

Browse files
committed
Add p_names field to ParseNamespaceItem
ParseNamespaceItem had a wired-in assumption that p_rte->eref describes the table and column aliases exposed by the nsitem. This relaxes this by creating a separate p_names field in an nsitem. This is mainly preparation for a patch for JOIN USING aliases, but it saves one indirection in common code paths, so it's possibly a win on its own. Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/785329.1616455091@sss.pgh.pa.us
1 parent 91c5a8c commit 66392d3

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/backend/parser/parse_clause.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,9 @@ transformFromClauseItem(ParseState *pstate, Node *n,
12171217
* input column numbers more easily.
12181218
*/
12191219
l_nscolumns = l_nsitem->p_nscolumns;
1220-
l_colnames = l_nsitem->p_rte->eref->colnames;
1220+
l_colnames = l_nsitem->p_names->colnames;
12211221
r_nscolumns = r_nsitem->p_nscolumns;
1222-
r_colnames = r_nsitem->p_rte->eref->colnames;
1222+
r_colnames = r_nsitem->p_names->colnames;
12231223

12241224
/*
12251225
* Natural join does not explicitly specify columns; must generate
@@ -1469,7 +1469,7 @@ transformFromClauseItem(ParseState *pstate, Node *n,
14691469
* Now that we know the join RTE's rangetable index, we can fix up the
14701470
* res_nscolumns data in places where it should contain that.
14711471
*/
1472-
Assert(res_colindex == list_length(nsitem->p_rte->eref->colnames));
1472+
Assert(res_colindex == list_length(nsitem->p_names->colnames));
14731473
for (k = 0; k < res_colindex; k++)
14741474
{
14751475
ParseNamespaceColumn *nscol = res_nscolumns + k;

src/backend/parser/parse_relation.c

+17-12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static ParseNamespaceItem *scanNameSpaceForRelid(ParseState *pstate, Oid relid,
6565
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
6666
int location);
6767
static int scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
68+
Alias *eref,
6869
const char *colname, int location,
6970
int fuzzy_rte_penalty,
7071
FuzzyAttrMatchState *fuzzystate);
@@ -184,7 +185,6 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
184185
foreach(l, pstate->p_namespace)
185186
{
186187
ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(l);
187-
RangeTblEntry *rte = nsitem->p_rte;
188188

189189
/* Ignore columns-only items */
190190
if (!nsitem->p_rel_visible)
@@ -193,7 +193,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
193193
if (nsitem->p_lateral_only && !pstate->p_lateral_active)
194194
continue;
195195

196-
if (strcmp(rte->eref->aliasname, refname) == 0)
196+
if (strcmp(nsitem->p_names->aliasname, refname) == 0)
197197
{
198198
if (result)
199199
ereport(ERROR,
@@ -420,7 +420,7 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
420420
{
421421
ParseNamespaceItem *nsitem1 = (ParseNamespaceItem *) lfirst(l1);
422422
RangeTblEntry *rte1 = nsitem1->p_rte;
423-
const char *aliasname1 = rte1->eref->aliasname;
423+
const char *aliasname1 = nsitem1->p_names->aliasname;
424424
ListCell *l2;
425425

426426
if (!nsitem1->p_rel_visible)
@@ -430,10 +430,11 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
430430
{
431431
ParseNamespaceItem *nsitem2 = (ParseNamespaceItem *) lfirst(l2);
432432
RangeTblEntry *rte2 = nsitem2->p_rte;
433+
const char *aliasname2 = nsitem2->p_names->aliasname;
433434

434435
if (!nsitem2->p_rel_visible)
435436
continue;
436-
if (strcmp(rte2->eref->aliasname, aliasname1) != 0)
437+
if (strcmp(aliasname2, aliasname1) != 0)
437438
continue; /* definitely no conflict */
438439
if (rte1->rtekind == RTE_RELATION && rte1->alias == NULL &&
439440
rte2->rtekind == RTE_RELATION && rte2->alias == NULL &&
@@ -466,7 +467,7 @@ check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
466467
{
467468
/* SQL:2008 demands this be an error, not an invisible item */
468469
RangeTblEntry *rte = nsitem->p_rte;
469-
char *refname = rte->eref->aliasname;
470+
char *refname = nsitem->p_names->aliasname;
470471

471472
ereport(ERROR,
472473
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
@@ -672,10 +673,10 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
672673
Var *var;
673674

674675
/*
675-
* Scan the RTE's column names (or aliases) for a match. Complain if
676+
* Scan the nsitem's column names (or aliases) for a match. Complain if
676677
* multiple matches.
677678
*/
678-
attnum = scanRTEForColumn(pstate, rte,
679+
attnum = scanRTEForColumn(pstate, rte, nsitem->p_names,
679680
colname, location,
680681
0, NULL);
681682

@@ -712,7 +713,7 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
712713
(errcode(ERRCODE_UNDEFINED_COLUMN),
713714
errmsg("column \"%s\" of relation \"%s\" does not exist",
714715
colname,
715-
rte->eref->aliasname)));
716+
nsitem->p_names->aliasname)));
716717

717718
var = makeVar(nscol->p_varno,
718719
nscol->p_varattno,
@@ -765,6 +766,7 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
765766
*/
766767
static int
767768
scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
769+
Alias *eref,
768770
const char *colname, int location,
769771
int fuzzy_rte_penalty,
770772
FuzzyAttrMatchState *fuzzystate)
@@ -786,7 +788,7 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
786788
* Callers interested in finding match with shortest distance need to
787789
* defend against this directly, though.
788790
*/
789-
foreach(c, rte->eref->colnames)
791+
foreach(c, eref->colnames)
790792
{
791793
const char *attcolname = strVal(lfirst(c));
792794

@@ -970,7 +972,7 @@ searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colnam
970972
* Scan for a matching column; if we find an exact match, we're
971973
* done. Otherwise, update fuzzystate.
972974
*/
973-
if (scanRTEForColumn(orig_pstate, rte, colname, location,
975+
if (scanRTEForColumn(orig_pstate, rte, rte->eref, colname, location,
974976
fuzzy_rte_penalty, fuzzystate)
975977
&& fuzzy_rte_penalty == 0)
976978
{
@@ -1252,6 +1254,7 @@ buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
12521254

12531255
/* ... and build the nsitem */
12541256
nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1257+
nsitem->p_names = rte->eref;
12551258
nsitem->p_rte = rte;
12561259
nsitem->p_rtindex = rtindex;
12571260
nsitem->p_nscolumns = nscolumns;
@@ -1313,6 +1316,7 @@ buildNSItemFromLists(RangeTblEntry *rte, Index rtindex,
13131316

13141317
/* ... and build the nsitem */
13151318
nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1319+
nsitem->p_names = rte->eref;
13161320
nsitem->p_rte = rte;
13171321
nsitem->p_rtindex = rtindex;
13181322
nsitem->p_nscolumns = nscolumns;
@@ -2198,6 +2202,7 @@ addRangeTableEntryForJoin(ParseState *pstate,
21982202
* list --- caller must do that if appropriate.
21992203
*/
22002204
nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
2205+
nsitem->p_names = rte->eref;
22012206
nsitem->p_rte = rte;
22022207
nsitem->p_rtindex = list_length(pstate->p_rtable);
22032208
nsitem->p_nscolumns = nscolumns;
@@ -2356,7 +2361,7 @@ addRangeTableEntryForCTE(ParseState *pstate,
23562361
*/
23572362
if (rte->ctelevelsup > 0)
23582363
for (int i = 0; i < n_dontexpand_columns; i++)
2359-
psi->p_nscolumns[list_length(psi->p_rte->eref->colnames) - 1 - i].p_dontexpand = true;
2364+
psi->p_nscolumns[list_length(psi->p_names->colnames) - 1 - i].p_dontexpand = true;
23602365

23612366
return psi;
23622367
}
@@ -3037,7 +3042,7 @@ expandNSItemVars(ParseNamespaceItem *nsitem,
30373042
if (colnames)
30383043
*colnames = NIL;
30393044
colindex = 0;
3040-
foreach(lc, nsitem->p_rte->eref->colnames)
3045+
foreach(lc, nsitem->p_names->colnames)
30413046
{
30423047
Value *colnameval = (Value *) lfirst(lc);
30433048
const char *colname = strVal(colnameval);

src/include/parser/parse_node.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ struct ParseState
227227
/*
228228
* An element of a namespace list.
229229
*
230+
* p_names contains the table name and column names exposed by this nsitem.
231+
* (Currently, it's always equal to p_rte->eref.)
232+
*
233+
* p_rte and p_rtindex link to the underlying rangetable entry.
234+
*
230235
* The p_nscolumns array contains info showing how to construct Vars
231-
* referencing corresponding elements of the RTE's colnames list.
236+
* referencing the names appearing in the p_names->colnames list.
232237
*
233238
* Namespace items with p_rel_visible set define which RTEs are accessible by
234239
* qualified names, while those with p_cols_visible set define which RTEs are
@@ -256,9 +261,10 @@ struct ParseState
256261
*/
257262
struct ParseNamespaceItem
258263
{
264+
Alias *p_names; /* Table and column names */
259265
RangeTblEntry *p_rte; /* The relation's rangetable entry */
260266
int p_rtindex; /* The relation's index in the rangetable */
261-
/* array of same length as p_rte->eref->colnames: */
267+
/* array of same length as p_names->colnames: */
262268
ParseNamespaceColumn *p_nscolumns; /* per-column data */
263269
bool p_rel_visible; /* Relation name is visible? */
264270
bool p_cols_visible; /* Column names visible as unqualified refs? */

0 commit comments

Comments
 (0)