Skip to content

Commit

Permalink
sql: fix bug where type annotation was lost after SET DEFAULT
Browse files Browse the repository at this point in the history
Fixes cockroachdb#47825.

This PR fixes a small bug where changing the default value of a column
would cause it to lose the type annotation it had in the pg_attrdef
catalog table.

Release note: None
  • Loading branch information
rohany committed Jun 23, 2020
1 parent 60ecb57 commit fda6715
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ func applyColumnMutation(
if err != nil {
return err
}
s := tree.Serialize(t.Default)
s := tree.Serialize(expr)
col.DefaultExpr = &s

// Add references to the sequence descriptors this column is now using.
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -2564,3 +2564,24 @@ UPDATE pg_catalog.pg_tables SET a = 'abc'
statement error pg_tables is a system catalog
TRUNCATE TABLE pg_catalog.pg_tables

# Regression for #47285.
statement ok
CREATE TABLE t47285 (x STRING DEFAULT 'hello');
ALTER TABLE t47285 ALTER COLUMN x SET DEFAULT 'howdy'

query T
SELECT
pg_get_expr(d.adbin, d.adrelid)
FROM
pg_attribute AS a
LEFT JOIN pg_attrdef AS d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type AS t ON a.atttypid = t.oid
LEFT JOIN pg_collation AS c ON
a.attcollation = c.oid AND a.attcollation != t.typcollation
WHERE
a.attrelid = 't47285'::REGCLASS
AND a.attnum > 0
AND NOT a.attisdropped
AND attname = 'x'
----
'howdy'::STRING

0 comments on commit fda6715

Please sign in to comment.