diff --git a/src/gausskernel/optimizer/prep/prepjointree.cpp b/src/gausskernel/optimizer/prep/prepjointree.cpp index ff6b3193..2187138e 100755 --- a/src/gausskernel/optimizer/prep/prepjointree.cpp +++ b/src/gausskernel/optimizer/prep/prepjointree.cpp @@ -1181,6 +1181,17 @@ static Node* pull_up_simple_subquery(PlannerInfo* root, Node* jtnode, RangeTblEn return jtnode; } + /* + * We must flatten any join alias Vars in the subquery's targetlist, + * because pulling up the subquery's subqueries might have changed their + * expansions into arbitrary expressions, which could affect + * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers + * are needed for tlist entries. (Likely it'd be better to do + * flatten_join_alias_vars on the whole query tree at some earlier stage, + * maybe even in the rewriter; but for now let's just fix this case here.) + */ + subquery->targetList = (List *) flatten_join_alias_vars(subroot, (Node *) subquery->targetList); + /* * Adjust level-0 varnos in subquery so that we can append its rangetable * to upper query's. We have to fix the subquery's append_rel_list as diff --git a/src/test/regress/expected/join_test_alias.out b/src/test/regress/expected/join_test_alias.out new file mode 100644 index 00000000..da549477 --- /dev/null +++ b/src/test/regress/expected/join_test_alias.out @@ -0,0 +1,55 @@ +CREATE TABLE t11 ( +sec_code character(6) NOT NULL, +issue_type character(3) NOT NULL +); +CREATE TABLE t22 ( +company_code character(6) NOT NULL, +list_profit_flag character(1) NOT NULL +); +CREATE TABLE t33 ( +company_code character(6) NOT NULL, +issue_flag character(6) NOT NULL +); +insert into t11 values(1,'S04'); +insert into t22 values(1,'Y'); +insert into t33 values(1,'1'); +insert into t33 values(2,'2'); +select +T5.issue_type +,T6.is_type +from +( +select T1.sec_code +,issue_type +--,case when list_profit_flag='Y' then '是' else '否' end as list_profit_flag +from +( +select sec_code +,case when issue_type in ('S04','S05','S06','S09') then '增发' +when issue_type in ('S01','S02') then '首发' +else '其他' end as issue_type +from t11 +)T1 +left join +( +select list_profit_flag +,company_code +from t22 +)T2 +on T1.sec_code=T2.company_code +)T5 +full join +( +SELECT CASE +WHEN issue_flag = ANY (ARRAY['1'::bpchar, '3'::bpchar, '11'::bpchar]) THEN '首发'::text +ELSE '增发'::text +END AS is_type from t33 +)T6 +on T5.issue_type=T6.is_type +; + issue_type | is_type +------------+--------- + 增发 | 增发 + | 首发 +(2 rows) + diff --git a/src/test/regress/parallel_schedule0 b/src/test/regress/parallel_schedule0 index 52dc7869..67b1c3fc 100644 --- a/src/test/regress/parallel_schedule0 +++ b/src/test/regress/parallel_schedule0 @@ -916,3 +916,4 @@ test: fdw_audit test: gs_global_config_audit test: detail test: gs_dump_encrypt +test: join_test_alias diff --git a/src/test/regress/sql/join_test_alias.sql b/src/test/regress/sql/join_test_alias.sql new file mode 100644 index 00000000..e5776147 --- /dev/null +++ b/src/test/regress/sql/join_test_alias.sql @@ -0,0 +1,53 @@ +CREATE TABLE t11 ( +sec_code character(6) NOT NULL, +issue_type character(3) NOT NULL +); + +CREATE TABLE t22 ( +company_code character(6) NOT NULL, +list_profit_flag character(1) NOT NULL +); + +CREATE TABLE t33 ( +company_code character(6) NOT NULL, +issue_flag character(6) NOT NULL +); + +insert into t11 values(1,'S04'); +insert into t22 values(1,'Y'); +insert into t33 values(1,'1'); +insert into t33 values(2,'2'); + +select +T5.issue_type +,T6.is_type +from +( +select T1.sec_code +,issue_type +--,case when list_profit_flag='Y' then '是' else '否' end as list_profit_flag +from +( +select sec_code +,case when issue_type in ('S04','S05','S06','S09') then '增发' +when issue_type in ('S01','S02') then '首发' +else '其他' end as issue_type +from t11 +)T1 +left join +( +select list_profit_flag +,company_code +from t22 +)T2 +on T1.sec_code=T2.company_code +)T5 +full join +( +SELECT CASE +WHEN issue_flag = ANY (ARRAY['1'::bpchar, '3'::bpchar, '11'::bpchar]) THEN '首发'::text +ELSE '增发'::text +END AS is_type from t33 +)T6 +on T5.issue_type=T6.is_type +; \ No newline at end of file