Introduce "anycompatible" family of polymorphic types.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 19 Mar 2020 15:43:11 +0000 (11:43 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 19 Mar 2020 15:43:11 +0000 (11:43 -0400)
commit24e2885ee304cb6a94fdfc25a1a108344ed9f4f7
tree040c3eead18de05e313c808e07aee262ef2de336
parentfab13dc50ba5e7a12b474a7366024681bc169ac8
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
33 files changed:
doc/src/sgml/datatype.sgml
doc/src/sgml/extend.sgml
doc/src/sgml/plpgsql.sgml
doc/src/sgml/xfunc.sgml
src/backend/catalog/index.c
src/backend/catalog/pg_proc.c
src/backend/commands/functioncmds.c
src/backend/parser/parse_coerce.c
src/backend/utils/adt/json.c
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/pseudotypes.c
src/backend/utils/fmgr/funcapi.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.dat
src/include/catalog/pg_type.dat
src/include/catalog/pg_type.h
src/pl/plpgsql/src/pl_comp.c
src/test/regress/expected/aggregates.out
src/test/regress/expected/create_aggregate.out
src/test/regress/expected/opr_sanity.out
src/test/regress/expected/plpgsql.out
src/test/regress/expected/polymorphism.out
src/test/regress/expected/rangefuncs.out
src/test/regress/expected/rangetypes.out
src/test/regress/expected/type_sanity.out
src/test/regress/sql/aggregates.sql
src/test/regress/sql/create_aggregate.sql
src/test/regress/sql/opr_sanity.sql
src/test/regress/sql/plpgsql.sql
src/test/regress/sql/polymorphism.sql
src/test/regress/sql/rangefuncs.sql
src/test/regress/sql/rangetypes.sql
src/test/regress/sql/type_sanity.sql