This is an automated email from the ASF dual-hosted git repository.

maxyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 0aabbe2b607f79b9081347957cf31a34491a57b6
Author: Dean Rasheed <[email protected]>
AuthorDate: Thu Jul 7 13:07:57 2022 +0100

    Fix alias matching in transformLockingClause().
    
    When locking a specific named relation for a FOR [KEY] UPDATE/SHARE
    clause, transformLockingClause() finds the relation to lock by
    scanning the rangetable for an RTE with a matching eref->aliasname.
    However, it failed to account for the visibility rules of a join RTE.
    
    If a join RTE doesn't have a user-supplied alias, it will have a
    generated eref->aliasname of "unnamed_join" that is not visible as a
    relation name in the parse namespace. Such an RTE needs to be skipped,
    otherwise it might be found in preference to a regular base relation
    with a user-supplied alias of "unnamed_join", preventing it from being
    locked.
    
    In addition, if a join RTE doesn't have a user-supplied alias, but
    does have a join_using_alias, then the RTE needs to be matched using
    that alias rather than the generated eref->aliasname, otherwise a
    misleading "relation not found" error will be reported rather than a
    "join cannot be locked" error.
    
    Backpatch all the way, except for the second part which only goes back
    to 14, where JOIN USING aliases were added.
    
    Dean Rasheed, reviewed by Tom Lane.
    
    Discussion: 
https://postgr.es/m/CAEZATCUY_KOBnqxbTSPf=7fz9HWPnZ5Xgb9SwYzZ8rFXe7nb=w...@mail.gmail.com
    
    fix join
---
 src/backend/parser/analyze.c                 | 9 +++++++++
 src/test/regress/expected/join.out           | 8 ++++++++
 src/test/regress/expected/join_optimizer.out | 8 ++++++++
 src/test/regress/sql/join.sql                | 6 ++++++
 4 files changed, 31 insertions(+)

diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index a5e4e800b1..60dd13744a 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -3753,6 +3753,15 @@ transformLockingClause(ParseState *pstate, Query *qry, 
LockingClause *lc,
                                ++i;
                                if (!rte->inFromCl)
                                        continue;
+
+                               /*
+                                * A join RTE without an alias is not visible 
as a relation
+                                * name and needs to be skipped (otherwise it 
might hide a
+                                * base relation with the same name).
+                                */
+                               if (rte->rtekind == RTE_JOIN && rte->alias == 
NULL)
+                                       continue;
+
                                if (strcmp(rte->eref->aliasname, 
thisrel->relname) == 0)
                                {
                                        switch (rte->rtekind)
diff --git a/src/test/regress/expected/join.out 
b/src/test/regress/expected/join.out
index a074c6f457..43e4fb3823 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -2552,6 +2552,14 @@ ERROR:  column t1.x does not exist
 LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
                ^
 HINT:  Perhaps you meant to reference the column "t3.x".
+-- Test matching of locking clause with wrong alias
+select t1.*, t2.*, unnamed_join.* from
+  t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
+  for update of unnamed_join;
+ a | b | a | b | x | y 
+---+---+---+---+---+---
+(0 rows)
+
 --
 -- regression test for 8.1 merge right join bug
 --
diff --git a/src/test/regress/expected/join_optimizer.out 
b/src/test/regress/expected/join_optimizer.out
index 154d3d6b6d..e9d8c38557 100644
--- a/src/test/regress/expected/join_optimizer.out
+++ b/src/test/regress/expected/join_optimizer.out
@@ -2576,6 +2576,14 @@ ERROR:  column t1.x does not exist
 LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
                ^
 HINT:  Perhaps you meant to reference the column "t3.x".
+-- Test matching of locking clause with wrong alias
+select t1.*, t2.*, unnamed_join.* from
+  t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
+  for update of unnamed_join;
+ a | b | a | b | x | y 
+---+---+---+---+---+---
+(0 rows)
+
 --
 -- regression test for 8.1 merge right join bug
 --
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 55e4ecf00f..4df0d8fdab 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -534,6 +534,12 @@ select * from t1 left join t2 on (t1.a = t2.a);
 
 select t1.x from t1 join t3 on (t1.a = t3.x);
 
+-- Test matching of locking clause with wrong alias
+
+select t1.*, t2.*, unnamed_join.* from
+  t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
+  for update of unnamed_join;
+
 --
 -- regression test for 8.1 merge right join bug
 --


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to