From b419fbcae157e5364cd412c58d2b277f020d91ec Mon Sep 17 00:00:00 2001 From: cc_db_dev Date: Fri, 10 Mar 2023 15:07:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dconnect=20by=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E4=B8=AD=E4=BD=BF=E7=94=A8=E5=90=8C=E4=B9=89=E8=AF=8D?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 函数ColumnRefFindRelname中,当没有显式指定别名时,应该采用 rte->eref->aliasname作为查找的表名,以此适配同义词场景 --- src/common/backend/parser/parse_expr.cpp | 10 ++- src/test/regress/expected/sw_bugfix-2.out | 89 +++++++++++++++++++++++ src/test/regress/sql/sw_bugfix-2.sql | 69 ++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) diff --git a/src/common/backend/parser/parse_expr.cpp b/src/common/backend/parser/parse_expr.cpp index c188cbdad2..6990806b08 100644 --- a/src/common/backend/parser/parse_expr.cpp +++ b/src/common/backend/parser/parse_expr.cpp @@ -3566,8 +3566,14 @@ static char *ColumnRefFindRelname(ParseState *pstate, const char *colname) Value *col = (Value *)lfirst(lc2); if (strcmp(colname, strVal(col)) == 0) { if (rte->rtekind == RTE_RELATION) { - relname = (rte->alias && rte->alias->aliasname) ? - rte->alias->aliasname : rte->relname; + if (rte->alias && rte->alias->aliasname) { + relname = rte->alias->aliasname; + } else if (rte->eref && rte->eref->aliasname) { + /* should use eref->aliasname for SYNONYM*/ + relname = rte->eref->aliasname; + } else { + relname = rte->relname; + } } else if (rte->rtekind == RTE_SUBQUERY) { relname = rte->alias->aliasname; } else if (rte->rtekind == RTE_CTE) { diff --git a/src/test/regress/expected/sw_bugfix-2.out b/src/test/regress/expected/sw_bugfix-2.out index 33c1e7d4ea..d418c28c9e 100755 --- a/src/test/regress/expected/sw_bugfix-2.out +++ b/src/test/regress/expected/sw_bugfix-2.out @@ -2031,3 +2031,92 @@ ORDER BY I.ZB_CODE; DROP TABLE zb_layer; DROP TABLE rtms_dict; DROP TABLE zb_model; +--test SYNONYM case + create table swtest.pf_org_rela_test +( + org_parent_no varchar2(32), + org_no varchar2(32) not null , + org_rela_type varchar2(32) not null +); + INSERT INTO swtest.pf_org_rela_test (org_no,org_parent_no,org_rela_type) VALUES + ('201855','201844','ADMINISTRATION'), + ('201856','201844','ADMINISTRATION'), + ('119208','119200','ADMINISTRATION'), + ('201953','201932','ADMINISTRATION'), + ('201954','201932','ADMINISTRATION'), + ('201955','201932','ADMINISTRATION'), + ('201956','201932','ADMINISTRATION'), + ('120301','120300','ADMINISTRATION'), + ('201957','202573','ADMINISTRATION'), + ('201958','201957','ADMINISTRATION'); + create synonym sy_pf for swtest.pf_org_rela_test; +--normal case +select + org_no +from + swtest.pf_org_rela_test +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; + org_no +-------- + 201957 + 201958 +(2 rows) + +--SYNONYM case +select + org_no +from + sy_pf +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; + org_no +-------- + 201957 + 201958 +(2 rows) + +--SYNONYM alias +select + aak.org_no +from + sy_pf aak +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; + org_no +-------- + 201957 + 201958 +(2 rows) + +--SYNONYM case +select + org_no +from + sy_pf +start with + sy_pf.org_no = '201957' + and sy_pf.org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and sy_pf.org_rela_type = 'ADMINISTRATION'; + org_no +-------- + 201957 + 201958 +(2 rows) + +drop synonym sy_pf; +drop table pf_org_rela_test; diff --git a/src/test/regress/sql/sw_bugfix-2.sql b/src/test/regress/sql/sw_bugfix-2.sql index ffdde15c65..e6fe11a524 100644 --- a/src/test/regress/sql/sw_bugfix-2.sql +++ b/src/test/regress/sql/sw_bugfix-2.sql @@ -740,3 +740,72 @@ ORDER BY I.ZB_CODE; DROP TABLE zb_layer; DROP TABLE rtms_dict; DROP TABLE zb_model; + +--test SYNONYM case + create table swtest.pf_org_rela_test +( + org_parent_no varchar2(32), + org_no varchar2(32) not null , + org_rela_type varchar2(32) not null +); + + INSERT INTO swtest.pf_org_rela_test (org_no,org_parent_no,org_rela_type) VALUES + ('201855','201844','ADMINISTRATION'), + ('201856','201844','ADMINISTRATION'), + ('119208','119200','ADMINISTRATION'), + ('201953','201932','ADMINISTRATION'), + ('201954','201932','ADMINISTRATION'), + ('201955','201932','ADMINISTRATION'), + ('201956','201932','ADMINISTRATION'), + ('120301','120300','ADMINISTRATION'), + ('201957','202573','ADMINISTRATION'), + ('201958','201957','ADMINISTRATION'); + + create synonym sy_pf for swtest.pf_org_rela_test; +--normal case +select + org_no +from + swtest.pf_org_rela_test +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; +--SYNONYM case +select + org_no +from + sy_pf +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; +--SYNONYM alias +select + aak.org_no +from + sy_pf aak +start with + org_no = '201957' + and org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and org_rela_type = 'ADMINISTRATION'; +--SYNONYM case +select + org_no +from + sy_pf +start with + sy_pf.org_no = '201957' + and sy_pf.org_rela_type = 'ADMINISTRATION' +connect by + prior org_no = org_parent_no + and sy_pf.org_rela_type = 'ADMINISTRATION'; + +drop synonym sy_pf; +drop table pf_org_rela_test; \ No newline at end of file -- Gitee