Casts to or from a domain type are ignored; warn and document.
authorRobert Haas <rhaas@postgresql.org>
Tue, 24 Apr 2012 13:20:53 +0000 (09:20 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 24 Apr 2012 13:20:53 +0000 (09:20 -0400)
Prohibiting this outright would break dumps taken from older versions
that contain such casts, which would create far more pain than is
justified here.

Per report by Jaime Casanova and subsequent discussion.

doc/src/sgml/ref/create_cast.sgml
src/backend/commands/functioncmds.c
src/test/regress/expected/privileges.out

index 964cbf4d41a2366e1735d43f456e97575df300d6..29ea298a468bd9b71707645d2081d0f4bf874692 100644 (file)
@@ -288,6 +288,11 @@ SELECT CAST ( 2 AS numeric ) + 4.0;
    convert between data types and a second to apply the modifier.
   </para>
 
+  <para>
+   A cast to or from a domain type currently has no effect.  Casting
+   to or from a domain uses the casts associated with its underlying type.
+  </para>
+
  </refsect1>
 
  <refsect1 id="sql-createcast-notes">
index 4125b97e89eb24e00b95f100024ff2b36b0f94ec..5f1c19eb3755c52cebd9082cb3e5b3d1f426544a 100644 (file)
@@ -1517,6 +1517,17 @@ CreateCast(CreateCastStmt *stmt)
        aclcheck_error(aclresult, ACL_KIND_TYPE,
                       format_type_be(targettypeid));
 
+   /* Domains are allowed for historical reasons, but we warn */
+   if (sourcetyptype == TYPTYPE_DOMAIN)
+       ereport(WARNING,
+               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                errmsg("cast will be ignored because the source data type is a domain")));
+
+   else if (targettyptype == TYPTYPE_DOMAIN)
+       ereport(WARNING,
+               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                errmsg("cast will be ignored because the target data type is a domain")));
+
    /* Detemine the cast method */
    if (stmt->func != NULL)
        castmethod = COERCION_METHOD_FUNCTION;
index a816cce1b115955f251e8ddd9814caad569f3848..dfaf3a7a7e01146f711edad112e47c035219bae7 100644 (file)
@@ -571,6 +571,7 @@ CREATE DOMAIN testdomain2b AS testdomain1;
 CREATE DOMAIN testdomain3b AS int;
 CREATE FUNCTION castfunc(int) RETURNS testdomain3b AS $$ SELECT $1::testdomain3b $$ LANGUAGE SQL;
 CREATE CAST (testdomain1 AS testdomain3b) WITH FUNCTION castfunc(int);
+WARNING:  cast will be ignored because the source data type is a domain
 CREATE FUNCTION testfunc5b(a testdomain1) RETURNS int LANGUAGE SQL AS $$ SELECT $1 $$;
 CREATE FUNCTION testfunc6b(b int) RETURNS testdomain1 LANGUAGE SQL AS $$ SELECT $1::testdomain1 $$;
 CREATE OPERATOR !! (PROCEDURE = testfunc5b, RIGHTARG = testdomain1);