Skip to content

Commit 391af9f

Browse files
committed
Preserve Var location information during flatten_join_alias_vars.
This allows us to give correct syntax error pointers when complaining about ungrouped variables in a join query with aggregates or GROUP BY. It's pretty much irrelevant for the planner's use of the function, though perhaps it might aid debugging sometimes.
1 parent 08e261c commit 391af9f

File tree

1 file changed

+10
-7
lines changed
  • src/backend/optimizer/util

1 file changed

+10
-7
lines changed

src/backend/optimizer/util/var.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,17 @@ flatten_join_alias_vars_mutator(Node *node,
794794
/* Ignore dropped columns */
795795
if (IsA(newvar, Const))
796796
continue;
797+
newvar = copyObject(newvar);
797798

798799
/*
799800
* If we are expanding an alias carried down from an upper
800801
* query, must adjust its varlevelsup fields.
801802
*/
802803
if (context->sublevels_up != 0)
803-
{
804-
newvar = copyObject(newvar);
805804
IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
806-
}
805+
/* Preserve original Var's location, if possible */
806+
if (IsA(newvar, Var))
807+
((Var *) newvar)->location = var->location;
807808
/* Recurse in case join input is itself a join */
808809
/* (also takes care of setting inserted_sublink if needed) */
809810
newvar = flatten_join_alias_vars_mutator(newvar, context);
@@ -814,24 +815,26 @@ flatten_join_alias_vars_mutator(Node *node,
814815
rowexpr->row_typeid = var->vartype;
815816
rowexpr->row_format = COERCE_IMPLICIT_CAST;
816817
rowexpr->colnames = NIL;
817-
rowexpr->location = -1;
818+
rowexpr->location = var->location;
818819

819820
return (Node *) rowexpr;
820821
}
821822

822823
/* Expand join alias reference */
823824
Assert(var->varattno > 0);
824825
newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
826+
newvar = copyObject(newvar);
825827

826828
/*
827829
* If we are expanding an alias carried down from an upper query, must
828830
* adjust its varlevelsup fields.
829831
*/
830832
if (context->sublevels_up != 0)
831-
{
832-
newvar = copyObject(newvar);
833833
IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
834-
}
834+
835+
/* Preserve original Var's location, if possible */
836+
if (IsA(newvar, Var))
837+
((Var *) newvar)->location = var->location;
835838

836839
/* Recurse in case join input is itself a join */
837840
newvar = flatten_join_alias_vars_mutator(newvar, context);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy