diff --git a/src/common/backend/parser/parse_expr.cpp b/src/common/backend/parser/parse_expr.cpp index c188cbdad2121afc1e04e27b2f27d9a10c26393c..6990806b086a0e80d3d5b5f0d156d4083c94a3f3 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 33c1e7d4ea2e6fe44565bb8322256d5d2cda1917..d418c28c9e2ef423c9d1135632b6f4d1f5bc20d2 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 ffdde15c6535544f0e84b40ed2f0d7dcdf4d9d45..e6fe11a5244c71dac005d02537937032c31de736 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