From c0ab0b37c1dccb1e6ef29383cc6d9d9c59671076 Mon Sep 17 00:00:00 2001 From: laishenghao Date: Thu, 1 Dec 2022 16:19:18 +0800 Subject: [PATCH] =?UTF-8?q?B=E5=BA=93=E4=B8=8Binsert=E3=80=81upsert?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E5=8F=98=E9=87=8F=E4=B8=8E=E5=88=97?= =?UTF-8?q?=E5=90=8D=E7=9B=B8=E5=90=8C=E6=97=B6=EF=BC=8C=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=8F=98=E9=87=8F=E4=BC=98=E5=85=88=E7=BA=A7=E6=9C=80?= =?UTF-8?q?=E9=AB=98=EF=BC=8C=E4=B8=8EMySQL=E4=BF=9D=E6=8C=81=E4=B8=80?= =?UTF-8?q?=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/parser/parse_expr.cpp | 13 ++++++---- src/common/pl/plpgsql/src/pl_comp.cpp | 2 +- .../regress/expected/insert_right_ref.out | 24 +++++++++++++++++++ src/test/regress/sql/insert_right_ref.sql | 17 +++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/common/backend/parser/parse_expr.cpp b/src/common/backend/parser/parse_expr.cpp index c70aabb16..c66a9dad8 100644 --- a/src/common/backend/parser/parse_expr.cpp +++ b/src/common/backend/parser/parse_expr.cpp @@ -966,10 +966,15 @@ Node* transformColumnRef(ParseState* pstate, ColumnRef* cref) if (node == NULL) { node = hookresult; } else if (hookresult != NULL) { - ereport(ERROR, - (errcode(ERRCODE_AMBIGUOUS_COLUMN), - errmsg("column reference \"%s\" is ambiguous", NameListToString(cref->fields)), - parser_errposition(pstate, cref->location))); + if (IS_SUPPORT_RIGHT_REF(pstate->rightRefState)) { + node = hookresult; + } else { + ereport(ERROR, + (errcode(ERRCODE_AMBIGUOUS_COLUMN), + errmsg("column reference \"%s\" is ambiguous", NameListToString(cref->fields)), + parser_errposition(pstate, cref->location))); + } + } } diff --git a/src/common/pl/plpgsql/src/pl_comp.cpp b/src/common/pl/plpgsql/src/pl_comp.cpp index 23c69f27f..241a081d9 100644 --- a/src/common/pl/plpgsql/src/pl_comp.cpp +++ b/src/common/pl/plpgsql/src/pl_comp.cpp @@ -1621,7 +1621,7 @@ static Node* plpgsql_post_column_ref(ParseState* pstate, ColumnRef* cref, Node* */ Node* myvar = resolve_column_ref(pstate, expr, cref, (var == NULL)); - if (myvar != NULL && var != NULL) { + if (myvar != NULL && var != NULL && !IS_SUPPORT_RIGHT_REF(pstate->rightRefState)) { /* * We could leave it to the core parser to throw this error, but we * can add a more useful detail message than the core could. diff --git a/src/test/regress/expected/insert_right_ref.out b/src/test/regress/expected/insert_right_ref.out index 644e14a90..ea1fe5bb8 100644 --- a/src/test/regress/expected/insert_right_ref.out +++ b/src/test/regress/expected/insert_right_ref.out @@ -148,5 +148,29 @@ select * from upser order by c1; (15 rows) drop table upser; +-- test var +create table with_var(a int default 999); +create function with_var_func() return int as +declare + a int := 666; +begin + insert into with_var values(a); + return a; +end; +/ +call with_var_func(); + with_var_func +--------------- + 666 +(1 row) + +select * from with_var; + a +----- + 666 +(1 row) + +drop function with_var_func; +drop table with_var; \c postgres drop database rightref; diff --git a/src/test/regress/sql/insert_right_ref.sql b/src/test/regress/sql/insert_right_ref.sql index d34c43682..b5fddab49 100644 --- a/src/test/regress/sql/insert_right_ref.sql +++ b/src/test/regress/sql/insert_right_ref.sql @@ -65,6 +65,23 @@ select * from upser order by c1; drop table upser; +-- test var +create table with_var(a int default 999); +create function with_var_func() return int as +declare + a int := 666; +begin + insert into with_var values(a); + return a; +end; +/ + +call with_var_func(); +select * from with_var; + +drop function with_var_func; +drop table with_var; + \c postgres drop database rightref;