diff options
| author | Tom Lane | 2020-03-19 15:43:11 +0000 |
|---|---|---|
| committer | Tom Lane | 2020-03-19 15:43:11 +0000 |
| commit | 24e2885ee304cb6a94fdfc25a1a108344ed9f4f7 (patch) | |
| tree | 040c3eead18de05e313c808e07aee262ef2de336 /src/test/regress/sql/polymorphism.sql | |
| parent | fab13dc50ba5e7a12b474a7366024681bc169ac8 (diff) | |
Introduce "anycompatible" family of polymorphic types.
This patch adds the pseudo-types anycompatible, anycompatiblearray,
anycompatiblenonarray, and anycompatiblerange. They work much like
anyelement, anyarray, anynonarray, and anyrange respectively, except
that the actual input values need not match precisely in type.
Instead, if we can find a common supertype (using the same rules
as for UNION/CASE type resolution), then the parser automatically
promotes the input values to that type. For example,
"myfunc(anycompatible, anycompatible)" can match a call with one
integer and one bigint argument, with the integer automatically
promoted to bigint. With anyelement in the definition, the user
would have had to cast the integer explicitly.
The new types also provide a second, independent set of type variables
for function matching; thus with "myfunc(anyelement, anyelement,
anycompatible) returns anycompatible" the first two arguments are
constrained to be the same type, but the third can be some other
type, and the result has the type of the third argument. The need
for more than one set of type variables was foreseen back when we
first invented the polymorphic types, but we never did anything
about it.
Pavel Stehule, revised a bit by me
Discussion: https://postgr.es/m/CAFj8pRDna7VqNi8gR+Tt2Ktmz0cq5G93guc3Sbn_NVPLdXAkqA@mail.gmail.com
Diffstat (limited to 'src/test/regress/sql/polymorphism.sql')
| -rw-r--r-- | src/test/regress/sql/polymorphism.sql | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/test/regress/sql/polymorphism.sql b/src/test/regress/sql/polymorphism.sql index 9c7b43efebb..b57591f69f6 100644 --- a/src/test/regress/sql/polymorphism.sql +++ b/src/test/regress/sql/polymorphism.sql @@ -53,6 +53,56 @@ select polyf(int4range(42, 49)) as int, polyf(float8range(4.5, 7.8)) as num; drop function polyf(x anyrange); +create function polyf(x anycompatible, y anycompatible) returns anycompatiblearray as $$ + select array[x, y] +$$ language sql; + +select polyf(2, 4) as int, polyf(2, 4.5) as num; + +drop function polyf(x anycompatible, y anycompatible); + +create function polyf(x anycompatiblerange, y anycompatible, z anycompatible) returns anycompatiblearray as $$ + select array[lower(x), upper(x), y, z] +$$ language sql; + +select polyf(int4range(42, 49), 11, 2::smallint) as int, polyf(float8range(4.5, 7.8), 7.8, 11::real) as num; + +select polyf(int4range(42, 49), 11, 4.5) as fail; -- range type doesn't fit + +drop function polyf(x anycompatiblerange, y anycompatible, z anycompatible); + +-- fail, can't infer type: +create function polyf(x anycompatible) returns anycompatiblerange as $$ + select array[x + 1, x + 2] +$$ language sql; + +create function polyf(x anycompatiblerange, y anycompatiblearray) returns anycompatiblerange as $$ + select x +$$ language sql; + +select polyf(int4range(42, 49), array[11]) as int, polyf(float8range(4.5, 7.8), array[7]) as num; + +drop function polyf(x anycompatiblerange, y anycompatiblearray); + +create function polyf(a anyelement, b anyarray, + c anycompatible, d anycompatible, + OUT x anyarray, OUT y anycompatiblearray) +as $$ + select a || b, array[c, d] +$$ language sql; + +select x, pg_typeof(x), y, pg_typeof(y) + from polyf(11, array[1, 2], 42, 34.5); +select x, pg_typeof(x), y, pg_typeof(y) + from polyf(11, array[1, 2], point(1,2), point(3,4)); +select x, pg_typeof(x), y, pg_typeof(y) + from polyf(11, '{1,2}', point(1,2), '(3,4)'); +select x, pg_typeof(x), y, pg_typeof(y) + from polyf(11, array[1, 2.2], 42, 34.5); -- fail + +drop function polyf(a anyelement, b anyarray, + c anycompatible, d anycompatible); + -- -- Polymorphic aggregate tests @@ -868,3 +918,112 @@ select * from dfview; drop view dfview; drop function dfunc(anyelement, anyelement, bool); + +-- +-- Tests for ANYCOMPATIBLE polymorphism family +-- + +create function anyctest(anycompatible, anycompatible) +returns anycompatible as $$ + select greatest($1, $2) +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, 12) x; +select x, pg_typeof(x) from anyctest(11, 12.3) x; +select x, pg_typeof(x) from anyctest(11, point(1,2)) x; -- fail +select x, pg_typeof(x) from anyctest('11', '12.3') x; -- defaults to text + +drop function anyctest(anycompatible, anycompatible); + +create function anyctest(anycompatible, anycompatible) +returns anycompatiblearray as $$ + select array[$1, $2] +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, 12) x; +select x, pg_typeof(x) from anyctest(11, 12.3) x; +select x, pg_typeof(x) from anyctest(11, array[1,2]) x; -- fail + +drop function anyctest(anycompatible, anycompatible); + +create function anyctest(anycompatible, anycompatiblearray) +returns anycompatiblearray as $$ + select array[$1] || $2 +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, array[12]) x; +select x, pg_typeof(x) from anyctest(11, array[12.3]) x; +select x, pg_typeof(x) from anyctest(12.3, array[13]) x; +select x, pg_typeof(x) from anyctest(12.3, '{13,14.4}') x; +select x, pg_typeof(x) from anyctest(11, array[point(1,2)]) x; -- fail +select x, pg_typeof(x) from anyctest(11, 12) x; -- fail + +drop function anyctest(anycompatible, anycompatiblearray); + +create function anyctest(anycompatible, anycompatiblerange) +returns anycompatiblerange as $$ + select $2 +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, int4range(4,7)) x; +select x, pg_typeof(x) from anyctest(11, numrange(4,7)) x; +select x, pg_typeof(x) from anyctest(11, 12) x; -- fail +select x, pg_typeof(x) from anyctest(11.2, int4range(4,7)) x; -- fail +select x, pg_typeof(x) from anyctest(11.2, '[4,7)') x; -- fail + +drop function anyctest(anycompatible, anycompatiblerange); + +create function anyctest(anycompatiblerange, anycompatiblerange) +returns anycompatible as $$ + select lower($1) + upper($2) +$$ language sql; + +select x, pg_typeof(x) from anyctest(int4range(11,12), int4range(4,7)) x; +select x, pg_typeof(x) from anyctest(int4range(11,12), numrange(4,7)) x; -- fail + +drop function anyctest(anycompatiblerange, anycompatiblerange); + +-- fail, can't infer result type: +create function anyctest(anycompatible) +returns anycompatiblerange as $$ + select $1 +$$ language sql; + +create function anyctest(anycompatiblenonarray, anycompatiblenonarray) +returns anycompatiblearray as $$ + select array[$1, $2] +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, 12) x; +select x, pg_typeof(x) from anyctest(11, 12.3) x; +select x, pg_typeof(x) from anyctest(array[11], array[1,2]) x; -- fail + +drop function anyctest(anycompatiblenonarray, anycompatiblenonarray); + +create function anyctest(a anyelement, b anyarray, + c anycompatible, d anycompatible) +returns anycompatiblearray as $$ + select array[c, d] +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, array[1, 2], 42, 34.5) x; +select x, pg_typeof(x) from anyctest(11, array[1, 2], point(1,2), point(3,4)) x; +select x, pg_typeof(x) from anyctest(11, '{1,2}', point(1,2), '(3,4)') x; +select x, pg_typeof(x) from anyctest(11, array[1, 2.2], 42, 34.5) x; -- fail + +drop function anyctest(a anyelement, b anyarray, + c anycompatible, d anycompatible); + +create function anyctest(variadic anycompatiblearray) +returns anycompatiblearray as $$ + select $1 +$$ language sql; + +select x, pg_typeof(x) from anyctest(11, 12) x; +select x, pg_typeof(x) from anyctest(11, 12.2) x; +select x, pg_typeof(x) from anyctest(11, '12') x; +select x, pg_typeof(x) from anyctest(11, '12.2') x; -- fail +select x, pg_typeof(x) from anyctest(variadic array[11, 12]) x; +select x, pg_typeof(x) from anyctest(variadic array[11, 12.2]) x; + +drop function anyctest(variadic anycompatiblearray); |
