Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9e86bc2

Browse files
committed
Improve error message and hint for ALTER COLUMN TYPE can't-cast failure.
We already tried to improve this once, but the "improved" text was rather off-target if you had provided a USING clause. Also, it seems helpful to provide the exact text of a suggested USING clause, so users can just copy-and-paste it when needed. Per complaint from Keith Rarick and a suggestion from Merlin Moncure. Back-patch to 9.2 where the current wording was adopted.
1 parent 553e576 commit 9e86bc2

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/backend/commands/tablecmds.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7321,11 +7321,26 @@ ATPrepAlterColumnType(List **wqueue,
73217321
COERCE_IMPLICIT_CAST,
73227322
-1);
73237323
if (transform == NULL)
7324-
ereport(ERROR,
7325-
(errcode(ERRCODE_DATATYPE_MISMATCH),
7326-
errmsg("column \"%s\" cannot be cast automatically to type %s",
7327-
colName, format_type_be(targettype)),
7328-
errhint("Specify a USING expression to perform the conversion.")));
7324+
{
7325+
/* error text depends on whether USING was specified or not */
7326+
if (def->raw_default != NULL)
7327+
ereport(ERROR,
7328+
(errcode(ERRCODE_DATATYPE_MISMATCH),
7329+
errmsg("result of USING clause for column \"%s\""
7330+
" cannot be cast automatically to type %s",
7331+
colName, format_type_be(targettype)),
7332+
errhint("You might need to add an explicit cast.")));
7333+
else
7334+
ereport(ERROR,
7335+
(errcode(ERRCODE_DATATYPE_MISMATCH),
7336+
errmsg("column \"%s\" cannot be cast automatically to type %s",
7337+
colName, format_type_be(targettype)),
7338+
/* translator: USING is SQL, don't translate it */
7339+
errhint("You might need to specify \"USING %s::%s\".",
7340+
quote_identifier(colName),
7341+
format_type_with_typemod(targettype,
7342+
targettypmod))));
7343+
}
73297344

73307345
/* Fix collations after all else */
73317346
assign_expr_collations(pstate, transform);

src/test/regress/expected/alter_table.out

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ select f3,max(f1) from foo group by f3;
16601660
-- Simple tests for alter table column type
16611661
alter table foo alter f1 TYPE integer; -- fails
16621662
ERROR: column "f1" cannot be cast automatically to type integer
1663-
HINT: Specify a USING expression to perform the conversion.
1663+
HINT: You might need to specify "USING f1::integer".
16641664
alter table foo alter f1 TYPE varchar(10);
16651665
create table anothertab (atcol1 serial8, atcol2 boolean,
16661666
constraint anothertab_chk check (atcol1 <= 3));
@@ -1675,7 +1675,10 @@ select * from anothertab;
16751675

16761676
alter table anothertab alter column atcol1 type boolean; -- fails
16771677
ERROR: column "atcol1" cannot be cast automatically to type boolean
1678-
HINT: Specify a USING expression to perform the conversion.
1678+
HINT: You might need to specify "USING atcol1::boolean".
1679+
alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
1680+
ERROR: result of USING clause for column "atcol1" cannot be cast automatically to type boolean
1681+
HINT: You might need to add an explicit cast.
16791682
alter table anothertab alter column atcol1 type integer;
16801683
select * from anothertab;
16811684
atcol1 | atcol2

src/test/regress/sql/alter_table.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ insert into anothertab (atcol1, atcol2) values (default, false);
11741174
select * from anothertab;
11751175

11761176
alter table anothertab alter column atcol1 type boolean; -- fails
1177+
alter table anothertab alter column atcol1 type boolean using atcol1::int; -- fails
11771178
alter table anothertab alter column atcol1 type integer;
11781179

11791180
select * from anothertab;

0 commit comments

Comments
 (0)