Skip to content

Commit

Permalink
Merge pull request #61767 from ajwerner/backport21.1-61713
Browse files Browse the repository at this point in the history
release-21.1: sql: minor fixes for virtual computed columns
  • Loading branch information
ajwerner authored Mar 11, 2021
2 parents 25b911e + ae9190d commit 5d84e07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@ func applyColumnMutation(
return pgerror.Newf(pgcode.InvalidColumnDefinition,
"column %q is not a computed column", col.Name)
}
if col.Virtual {
return pgerror.Newf(pgcode.InvalidColumnDefinition,
"column %q is not a stored computed column", col.Name)
}
col.ComputeExpr = nil
}
return nil
Expand Down
6 changes: 5 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ CREATE TABLE constraint_db.t1 (
i VARCHAR(20)[],
j "char",
k VARCHAR(10) COLLATE sv,
l INT AS (a * b) VIRTUAL,
UNIQUE INDEX index_key(b, c)
)

Expand Down Expand Up @@ -699,7 +700,7 @@ JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = 'public'
----
relname relistemp relkind relnatts relchecks relhasoids relhaspkey
t1 false r 12 0 false true
t1 false r 13 0 false true
primary false i 1 0 false false
t1_a_key false i 1 0 false false
index_key false i 2 0 false false
Expand Down Expand Up @@ -764,6 +765,7 @@ attrelid relname attname atttypid attstattarget attlen attnum att
55 t1 i 1015 0 -1 10 0 -1
55 t1 j 18 0 1 11 0 -1
55 t1 k 1043 0 -1 12 0 -1
55 t1 l 20 0 8 13 0 -1
450499963 primary p 701 0 8 1 0 -1
450499960 t1_a_key a 20 0 8 2 0 -1
450499961 index_key b 20 0 8 3 0 -1
Expand Down Expand Up @@ -817,6 +819,7 @@ t1 h 16 NULL NULL NULL false f
t1 i 24 NULL NULL NULL false false · ·
t1 j -1 NULL NULL NULL false false · ·
t1 k 14 NULL NULL NULL false false · ·
t1 l -1 NULL NULL NULL false false · v
primary p -1 NULL NULL NULL true false · ·
t1_a_key a -1 NULL NULL NULL false false · ·
index_key b -1 NULL NULL NULL false false · ·
Expand Down Expand Up @@ -870,6 +873,7 @@ t1 h false true 0 NULL NULL
t1 i false true 0 NULL NULL NULL
t1 j false true 0 NULL NULL NULL
t1 k false true 0 NULL NULL NULL
t1 l false true 0 NULL NULL NULL
primary p false true 0 NULL NULL NULL
t1_a_key a false true 0 NULL NULL NULL
index_key b false true 0 NULL NULL NULL
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/virtual_columns
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ CREATE TABLE t (
v INT AS (a+b) VIRTUAL
)

statement error pgcode 42611 column "v" is not a stored computed column
ALTER TABLE t ALTER COLUMN v DROP STORED

statement ok
INSERT INTO t VALUES (1, 1)

Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,12 @@ https://www.postgresql.org/docs/12/catalog-pg-attribute.html`,
addColumn := func(column *descpb.ColumnDescriptor, attRelID tree.Datum, attNum uint32) error {
colTyp := column.Type
// Sets the attgenerated column to 's' if the column is generated/
// computed, zero byte otherwise.
// computed stored, "v" if virtual, zero byte otherwise.
var isColumnComputed string
if column.IsComputed() {
if column.IsComputed() && !column.Virtual {
isColumnComputed = "s"
} else if column.IsComputed() {
isColumnComputed = "v"
} else {
isColumnComputed = ""
}
Expand Down

0 comments on commit 5d84e07

Please sign in to comment.