Skip to content

[pull] master from postgres:master #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
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
3 changes: 3 additions & 0 deletions src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -14484,6 +14484,9 @@ ATPrepAlterColumnType(List **wqueue,
/* Fix collations after all else */
assign_expr_collations(pstate, transform);

/* Expand virtual generated columns in the expr. */
transform = expand_generated_columns_in_expr(transform, rel, 1);

/* Plan the expr now so we can accurately assess the need to rewrite. */
transform = (Node *) expression_planner((Expr *) transform);

Expand Down
36 changes: 20 additions & 16 deletions src/test/regress/expected/generated_virtual.out
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,8 @@ create table gtest32 (
a int primary key,
b int generated always as (a * 2),
c int generated always as (10 + 10),
d int generated always as (coalesce(a, 100))
d int generated always as (coalesce(a, 100)),
e int
);
insert into gtest32 values (1), (2);
analyze gtest32;
Expand Down Expand Up @@ -1563,41 +1564,44 @@ select t2.* from gtest32 t1 left join gtest32 t2 on false;
QUERY PLAN
------------------------------------------------------
Nested Loop Left Join
Output: a, (a * 2), (20), (COALESCE(a, 100))
Output: a, (a * 2), (20), (COALESCE(a, 100)), e
Join Filter: false
-> Seq Scan on generated_virtual_tests.gtest32 t1
Output: t1.a, t1.b, t1.c, t1.d
Output: t1.a, t1.b, t1.c, t1.d, t1.e
-> Result
Output: a, 20, COALESCE(a, 100)
Output: a, e, 20, COALESCE(a, 100)
One-Time Filter: false
(8 rows)

select t2.* from gtest32 t1 left join gtest32 t2 on false;
a | b | c | d
---+---+---+---
| | |
| | |
a | b | c | d | e
---+---+---+---+---
| | | |
| | | |
(2 rows)

explain (verbose, costs off)
select * from gtest32 t group by grouping sets (a, b, c, d) having c = 20;
select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;
QUERY PLAN
-----------------------------------------------------
HashAggregate
Output: a, ((a * 2)), (20), (COALESCE(a, 100))
Output: a, ((a * 2)), (20), (COALESCE(a, 100)), e
Hash Key: t.a
Hash Key: (t.a * 2)
Hash Key: 20
Hash Key: COALESCE(t.a, 100)
Hash Key: t.e
Filter: ((20) = 20)
-> Seq Scan on generated_virtual_tests.gtest32 t
Output: a, (a * 2), 20, COALESCE(a, 100)
(9 rows)
Output: a, (a * 2), 20, COALESCE(a, 100), e
(10 rows)

select * from gtest32 t group by grouping sets (a, b, c, d) having c = 20;
a | b | c | d
---+---+----+---
| | 20 |
select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;
a | b | c | d | e
---+---+----+---+---
| | 20 | |
(1 row)

-- Ensure that the virtual generated columns in ALTER COLUMN TYPE USING expression are expanded
alter table gtest32 alter column e type bigint using b;
drop table gtest32;
10 changes: 7 additions & 3 deletions src/test/regress/sql/generated_virtual.sql
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,8 @@ create table gtest32 (
a int primary key,
b int generated always as (a * 2),
c int generated always as (10 + 10),
d int generated always as (coalesce(a, 100))
d int generated always as (coalesce(a, 100)),
e int
);

insert into gtest32 values (1), (2);
Expand Down Expand Up @@ -838,7 +839,10 @@ select t2.* from gtest32 t1 left join gtest32 t2 on false;
select t2.* from gtest32 t1 left join gtest32 t2 on false;

explain (verbose, costs off)
select * from gtest32 t group by grouping sets (a, b, c, d) having c = 20;
select * from gtest32 t group by grouping sets (a, b, c, d) having c = 20;
select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;
select * from gtest32 t group by grouping sets (a, b, c, d, e) having c = 20;

-- Ensure that the virtual generated columns in ALTER COLUMN TYPE USING expression are expanded
alter table gtest32 alter column e type bigint using b;

drop table gtest32;