8000 Change post-rewriter representation of dropped columns in joinaliasvars. · sqlparser/postgres@a8d59a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8d59a9

Browse files
committed
Change post-rewriter representation of dropped columns in joinaliasvars.
It's possible to drop a column from an input table of a JOIN clause in a view, if that column is nowhere actually referenced in the view. But it will still be there in the JOIN clause's joinaliasvars list. We used to replace such entries with NULL Const nodes, which is handy for generation of RowExpr expansion of a whole-row reference to the view. The trouble with that is that it can't be distinguished from the situation after subquery pull-up of a constant subquery output expression below the JOIN. Instead, replace such joinaliasvars with null pointers (empty expression trees), which can't be confused with pulled-up expressions. expandRTE() still emits the old convention, though, for convenience of RowExpr generation and to reduce the risk of breaking extension code. In HEAD and 9.3, this patch also fixes a problem with some new code in ruleutils.c that was failing to cope with implicitly-casted joinaliasvars entries, as per recent report from Feike Steenbergen. That oversight was because of an inadequate description of the data structure in parsenodes.h, which I've now corrected. There were some pre-existing oversights of the same ilk elsewhere, which I believe are now all fixed.
1 parent 3b35f1c commit a8d59a9

File tree

6 files changed

+58
-35
lines changed

6 files changed

+58
-35
lines changed

src/backend/optimizer/util/var.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ flatten_join_alias_vars_mutator(Node *node,
748748
newvar = (Node *) lfirst(l);
749749
attnum++;
750750
/* Ignore dropped columns */
751-
if (IsA(newvar, Const))
751+
if (newvar == NULL)
752752
continue;
753753

754754
/*
@@ -778,6 +778,7 @@ flatten_join_alias_vars_mutator(Node *node,
778778
/* Expand join alias reference */
779779
Assert(var->varattno > 0);
780780
newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
781+
Assert(newvar != NULL);
781782

782783
/*
783784
* If we are expanding an alias carried down from an upper query, must

src/backend/parser/parse_relation.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "funcapi.h"
2525
#include "nodes/makefuncs.h"
2626
#include "nodes/nodeFuncs.h"
27+
#include "optimizer/clauses.h"
2728
#include "parser/parsetree.h"
2829
#include "parser/parse_relation.h"
2930
#include "parser/parse_type.h"
@@ -682,14 +683,15 @@ markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte,
682683
* The aliasvar could be either a Var or a COALESCE expression,
683684
* but in the latter case we should already have marked the two
684685
* referent variables as being selected, due to their use in the
685-
* JOIN clause. So we need only be concerned with the simple Var
686-
* case.
686+
* JOIN clause. So we need only be concerned with the Var case.
687+
* But we do need to drill down through implicit coercions.
687688
*/
688689
Var *aliasvar;
689690

690691
Assert(col > 0 && col <= list_length(rte->joinaliasvars));
691692
aliasvar = (Var *) list_nth(rte->joinaliasvars, col - 1);
692-
if (IsA(aliasvar, Var))
693+
aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
694+
if (aliasvar && IsA(aliasvar, Var))
693695
markVarForSelectPriv(pstate, aliasvar, NULL);
694696
}
695697
}
@@ -1779,19 +1781,26 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
17791781
* deleted columns in the join; but we have to check since
17801782
* this routine is also used by the rewriter, and joins
17811783
* found in stored rules might have join columns for
1782-
* since-deleted columns. This will be signaled by a NULL
1783-
* Const in the alias-vars list.
1784+
* since-deleted columns. This will be signaled by a null
1785+
* pointer in the alias-vars list.
17841786
*/
1785-
if (IsA(avar, Const))
1787+
if (avar == NULL)
17861788
{
17871789
if (include_dropped)
17881790
{
17891791
if (colnames)
17901792
*colnames = lappend(*colnames,
17911793
makeString(pstrdup("")));
17921794
if (colvars)
1795+
{
1796+
/*
1797+
* Can't use join's column type here (it might
1798+
* be dropped!); but it doesn't really matter
1799+
* what type the Const claims to be.
1800+
*/
17931801
*colvars = lappend(*colvars,
1794-
copyObject(avar));
1802+
makeNullConst(INT4OID, -1));
1803+
}
17951804
}
17961805
continue;
17971806
}
@@ -2167,6 +2176,7 @@ get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum,
21672176

21682177
Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
21692178
aliasvar = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
2179+
Assert(aliasvar != NULL);
21702180
*vartype = exprType(aliasvar);
21712181
*vartypmod = exprTypmod(aliasvar);
21722182
}
@@ -2228,7 +2238,7 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
22282238
* but one in a stored rule might contain columns that were
22292239
* dropped from the underlying tables, if said columns are
22302240
* nowhere explicitly referenced in the rule. This will be
2231-
* signaled to us by a NULL Const in the joinaliasvars list.
2241+
* signaled to us by a null pointer in the joinaliasvars list.
22322242
*/
22332243
Var *aliasvar;
22342244

@@ -2237,7 +2247,7 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
22372247
elog(ERROR, "invalid varattno %d", attnum);
22382248
aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
22392249

2240-
result = IsA(aliasvar, Const);
2250+
result = (aliasvar == NULL);
22412251
}
22422252
break;
22432253
case RTE_FUNCTION:

src/backend/parser/parse_target.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
288288

289289
Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
290290
aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
291+
/* We intentionally don't strip implicit coercions here */
291292
markTargetListOrigin(pstate, tle, aliasvar, netlevelsup);
292293
}
293294
break;
@@ -1216,6 +1217,8 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
12161217
/* Join RTE --- recursively inspect the alias variable */
12171218
Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
12181219
expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1220+
Assert(expr != NULL);
1221+
/* We intentionally don't strip implicit coercions here */
12191222
if (IsA(expr, Var))
12201223
return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
12211224
/* else fall through to inspect the expression */

src/backend/rewrite/rewriteHandler.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "catalog/pg_type.h"
1818
#include "nodes/makefuncs.h"
1919
#include "nodes/nodeFuncs.h"
20+
#include "optimizer/clauses.h"
2021
#include "parser/analyze.h"
2122
#include "parser/parse_coerce.h"
2223
#include "parser/parsetree.h"
@@ -81,9 +82,8 @@ static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
8182
* such a list in a stored rule to include references to dropped columns.
8283
* (If the column is not explicitly referenced anywhere else in the query,
8384
* the dependency mechanism won't consider it used by the rule and so won't
84-
* prevent the column drop.) To support get_rte_attribute_is_dropped(),
85-
* we replace join alias vars that reference dropped columns with NULL Const
86-
* nodes.
85+
* prevent the column drop.) To support get_rte_attribute_is_dropped(), we
86+
* replace join alias vars that reference dropped columns with null pointers.
8787
*
8888
* (In PostgreSQL 8.0, we did not do this processing but instead had
8989
* get_rte_attribute_is_dropped() recurse to detect dropped columns in joins.
@@ -142,8 +142,8 @@ AcquireRewriteLocks(Query *parsetree)
142142

143143
/*
144144
* Scan the join's alias var list to see if any columns have
145-
* been dropped, and if so replace those Vars with NULL
146-
* Consts.
145+
* been dropped, and if so replace those Vars with null
146+
* pointers.
147147
*
148148
* Since a join has only two inputs, we can expect to see
149149
* multiple references to the same input RTE; optimize away
@@ -154,16 +154,20 @@ AcquireRewriteLocks(Query *parsetree)
154154
curinputrte = NULL;
155155
foreach(ll, rte->joinaliasvars)
156156
{
157-
Var *aliasvar = (Var *) lfirst(ll);
157+
Var *aliasitem = (Var *) lfirst(ll);
158+
Var *aliasvar = aliasitem;
159+
160+
/* Look through any implicit coercion */
161+
aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
158162

159163
/*
160164
* If the list item isn't a simple Var, then it must
161165
* represent a merged column, ie a USING column, and so it
162166
* couldn't possibly be dropped, since it's referenced in
163-
* the join clause. (Conceivably it could also be a NULL
164-
* constant already? But that's OK too.)
167+
* the join clause. (Conceivably it could also be a null
168+
* pointer already? But that's OK too.)
165169
*/
166-
if (IsA(aliasvar, Var))
170+
if (aliasvar && IsA(aliasvar, Var))
167171
{
168172
/*
169173
* The elements of an alias list have to refer to
@@ -187,15 +191,11 @@ AcquireRewriteLocks(Query *parsetree)
187191
if (get_rte_attribute_is_dropped(curinputrte,
188192
aliasvar->varattno))
189193
{
190-
/*
191-
* can't use vartype here, since that might be a
192-
* now-dropped type OID, but it doesn't really
193-
* matter what type the Const claims to be.
194-
*/
195-
aliasvar = (Var *) makeNullConst(INT4OID, -1);
194+
/* Replace the join alias item with a NULL */
195+
aliasitem = NULL;
196196
}
197197
}
198-
newaliasvars = lappend(newaliasvars, aliasvar);
198+
newaliasvars = lappend(newaliasvars, aliasitem);
199199
}
200200
rte->joinaliasvars = newaliasvars;
201201
break;

src/backend/utils/adt/ruleutils.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3505,7 +3505,8 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
35053505
Var *aliasvar;
35063506

35073507
aliasvar = (Var *) list_nth(rte->joinaliasvars, attnum - 1);
3508-
if (IsA(aliasvar, Var))
3508+
/* we intentionally don't strip implicit coercions here */
3509+
if (aliasvar && IsA(aliasvar, Var))
35093510
{
35103511
return get_variable(aliasvar, var->varlevelsup + levelsup,
35113512
istoplevel, context);
@@ -3789,6 +3790,8 @@ get_name_for_var_field(Var *var, int fieldno,
37893790
elog(ERROR, "cannot decompile join alias var in plan tree");
37903791
Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
37913792
expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
3793+
Assert(expr != NULL);
3794+
/* we intentionally don't strip implicit coercions here */
37923795
if (IsA(expr, Var))
37933796
return get_name_for_var_field((Var *) expr, fieldno,
37943797
var->varlevelsup + levelsup,

src/include/nodes/parsenodes.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ typedef struct XmlSerialize
597597
* a stored rule might contain entries for columns dropped since the rule
598598
* was created. (This is only possible for columns not actually referenced
599599
* in the rule.) When loading a stored rule, we replace the joinaliasvars
600-
* items for any such columns with NULL Consts. (We can't simply delete
600+
* items for any such columns with null pointers. (We can't simply delete
601601
* them from the joinaliasvars list, because that would affect the attnums
602602
* of Vars referencing the rest of the list.)
603603
*
@@ -667,13 +667,19 @@ typedef struct RangeTblEntry
667667
/*
668668
* Fields valid for a join RTE (else NULL/zero):
669669
*
670-
* joinaliasvars is a list of Vars or COALESCE expressions corresponding
671-
* to the columns of the join result. An alias Var referencing column K
672-
* of the join result can be replaced by the K'th element of joinaliasvars
673-
* --- but to simplify the task of reverse-listing aliases correctly, we
674-
* do not do that until planning time. In a Query loaded from a stored
675-
* rule, it is also possible for joinaliasvars items to be NULL Consts,
676-
* denoting columns dropped since the rule was made.
670+
* joinaliasvars is a list of (usually) Vars corresponding to the columns
671+
* of the join result. An alias Var referencing column K of the join
672+
* result can be replaced by the K'th element of joinaliasvars --- but to
673+
* simplify the task of reverse-listing aliases correctly, we do not do
674+
* that until planning time. In detail: an element of joinaliasvars can
675+
* be a Var of one of the join's input relations, or such a Var with an
676+
* implicit coercion to the join's output column type, or a COALESCE
677+
* expression containing the two input column Vars (possibly coerced).
678+
* Within a Query loaded from a stored rule, it is also possible for
679+
* joinaliasvars items to be null pointers, which are placeholders for
680+
* (necessarily unreferenced) columns dropped since the rule was made.
681+
* Also, once planning begins, joinaliasvars items can be almost anything,
682+
* as a result of subquery-flattening substitutions.
677683
*/
678684
JoinType jointype; /* type of join */
679685
List *joinaliasvars; /* list of alias-var expansions */

0 commit comments

Comments
 (0)
0