Add test case for CREATE CAST.
[PostgreSQL.git] / src / test / regress / sql / create_cast.sql
blobad348daa0978777cb6019ec378a4ab8743eaf669
1 --
2 -- CREATE_CAST
3 --
5 -- Create some types to test with
6 CREATE TYPE casttesttype;
8 CREATE FUNCTION casttesttype_in(cstring)
9    RETURNS casttesttype
10    AS 'textin'
11    LANGUAGE internal STRICT;
12 CREATE FUNCTION casttesttype_out(casttesttype)
13    RETURNS cstring
14    AS 'textout'
15    LANGUAGE internal STRICT;
17 CREATE TYPE casttesttype (
18    internallength = variable,
19    input = casttesttype_in,
20    output = casttesttype_out,
21    alignment = int4
24 -- a dummy function to test with
25 CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS
26 $$ SELECT 1; $$;
28 SELECT casttestfunc('foo'::text); -- fails, as there's no cast
30 -- Try binary coercion cast
31 CREATE CAST (text AS casttesttype) WITHOUT FUNCTION;
32 SELECT casttestfunc('foo'::text); -- doesn't work, as the cast is explicit
33 SELECT casttestfunc('foo'::text::casttesttype); -- should work
34 DROP CAST (text AS casttesttype); -- cleanup
36 -- Try IMPLICIT binary coercion cast
37 CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT;
38 SELECT casttestfunc('foo'::text); -- Should work now
40 -- Try I/O conversion cast.
41 SELECT 1234::int4::casttesttype; -- No cast yet, should fail
43 CREATE CAST (int4 AS casttesttype) WITH INOUT;
44 SELECT 1234::int4::casttesttype; -- Should work now
46 DROP CAST (int4 AS casttesttype);
48 -- Try cast with a function
50 CREATE FUNCTION int4_casttesttype(int4) RETURNS casttesttype LANGUAGE SQL AS
51 $$ SELECT ('foo'::text || $1::text)::casttesttype; $$;
53 CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype(int4) AS IMPLICIT;
54 SELECT 1234::int4::casttesttype; -- Should work now