Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backend/optimizer/plan/orca.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ push_down_expr_mutator(Node *node, List *child_tlist)
{
((Const *) child_tle->expr)->consttypmod = ((Var *) node)->vartypmod;
}
else if (IsA(child_tle->expr, Var))
{
((Var *) child_tle->expr)->vartypmod = ((Var *) node)->vartypmod;
}

return (Node *) child_tle->expr;
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/regress/expected/union_gp.out
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,36 @@ with result as (update r_1240 set a = a +1 where a < 5 returning *) select * fro
drop table r_1240;
drop table p1_1240;
--
-- Test CTAS with UNION ALL when branches have different typmods (issue #1431).
-- ORCA should resolve the output column type to character varying (no length),
-- same as the Postgres planner, instead of picking the first branch's typmod.
--
create table union_ctas_t1(id int, name varchar(1));
create table union_ctas_t2(id int, name varchar(2));
insert into union_ctas_t1 values (1, 'a');
insert into union_ctas_t2 values (1, 'ab');
create table union_ctas_result as
(select id, name from union_ctas_t1)
union all
(select id, name from union_ctas_t2);
-- name column should be "character varying" without length, not varchar(1)
select atttypmod from pg_attribute
where attrelid = 'union_ctas_result'::regclass and attname = 'name';
atttypmod
-----------
-1
(1 row)

-- data should not be truncated
select * from union_ctas_result order by name;
id | name
----+------
1 | a
1 | ab
(2 rows)

drop table union_ctas_t1, union_ctas_t2, union_ctas_result;
--
-- Clean up
--
DROP TABLE IF EXISTS T_a1 CASCADE;
Expand Down
Loading
Loading