Support subscripting of arbitrary types, not only arrays.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 9 Dec 2020 17:40:37 +0000 (12:40 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 9 Dec 2020 17:40:37 +0000 (12:40 -0500)
commitc7aba7c14efdbd9fc1bb44b4cb83bedee0c6a6fc
treed6980ca2951d353475957a56b58866cd4fafcdd3
parent8b069ef5dca97cd737a5fd64c420df3cd61ec1c9
Support subscripting of arbitrary types, not only arrays.

This patch generalizes the subscripting infrastructure so that any
data type can be subscripted, if it provides a handler function to
define what that means.  Traditional variable-length (varlena) arrays
all use array_subscript_handler(), while the existing fixed-length
types that support subscripting use raw_array_subscript_handler().
It's expected that other types that want to use subscripting notation
will define their own handlers.  (This patch provides no such new
features, though; it only lays the foundation for them.)

To do this, move the parser's semantic processing of subscripts
(including coercion to whatever data type is required) into a
method callback supplied by the handler.  On the execution side,
replace the ExecEvalSubscriptingRef* layer of functions with direct
calls to callback-supplied execution routines.  (Thus, essentially
no new run-time overhead should be caused by this patch.  Indeed,
there is room to remove some overhead by supplying specialized
execution routines.  This patch does a little bit in that line,
but more could be done.)

Additional work is required here and there to remove formerly
hard-wired assumptions about the result type, collation, etc
of a SubscriptingRef expression node; and to remove assumptions
that the subscript values must be integers.

One useful side-effect of this is that we now have a less squishy
mechanism for identifying whether a data type is a "true" array:
instead of wiring in weird rules about typlen, we can look to see
if pg_type.typsubscript == F_ARRAY_SUBSCRIPT_HANDLER.  For this
to be bulletproof, we have to forbid user-defined types from using
that handler directly; but there seems no good reason for them to
do so.

This patch also removes assumptions that the number of subscripts
is limited to MAXDIM (6), or indeed has any hard-wired limit.
That limit still applies to types handled by array_subscript_handler
or raw_array_subscript_handler, but to discourage other dependencies
on this constant, I've moved it from c.h to utils/array.h.

Dmitry Dolgov, reviewed at various times by Tom Lane, Arthur Zakirov,
Peter Eisentraut, Pavel Stehule

Discussion: https://postgr.es/m/CA+q6zcVDuGBv=M0FqBYX8DPebS3F_0KQ6OVFobGJPM507_SZ_w@mail.gmail.com
Discussion: https://postgr.es/m/CA+q6zcVovR+XY4mfk-7oNk-rF91gH0PebnNfuUjuuDsyHjOcVA@mail.gmail.com
52 files changed:
contrib/postgres_fdw/deparse.c
doc/src/sgml/catalogs.sgml
doc/src/sgml/ref/create_type.sgml
src/backend/catalog/aclchk.c
src/backend/catalog/dependency.c
src/backend/catalog/heap.c
src/backend/catalog/pg_type.c
src/backend/commands/typecmds.c
src/backend/executor/execExpr.c
src/backend/executor/execExprInterp.c
src/backend/jit/llvm/llvmjit_expr.c
src/backend/jit/llvm/llvmjit_types.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/nodeFuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/util/clauses.c
src/backend/parser/parse_coerce.c
src/backend/parser/parse_collate.c
src/backend/parser/parse_expr.c
src/backend/parser/parse_node.c
src/backend/parser/parse_target.c
src/backend/utils/adt/Makefile
src/backend/utils/adt/arrayfuncs.c
src/backend/utils/adt/arraysubs.c [new file with mode: 0644]
src/backend/utils/adt/format_type.c
src/backend/utils/adt/jsonfuncs.c
src/backend/utils/cache/lsyscache.c
src/backend/utils/cache/typcache.c
src/bin/pg_dump/pg_dump.c
src/include/c.h
src/include/catalog/catversion.h
src/include/catalog/pg_proc.dat
src/include/catalog/pg_type.dat
src/include/catalog/pg_type.h
src/include/executor/execExpr.h
src/include/nodes/primnodes.h
src/include/nodes/subscripting.h [new file with mode: 0644]
src/include/parser/parse_node.h
src/include/utils/array.h
src/include/utils/lsyscache.h
src/include/utils/typcache.h
src/pl/plperl/plperl.c
src/pl/plpgsql/src/pl_comp.c
src/pl/plpython/plpy_typeio.c
src/test/regress/expected/arrays.out
src/test/regress/expected/opr_sanity.out
src/test/regress/expected/type_sanity.out
src/test/regress/sql/arrays.sql
src/test/regress/sql/opr_sanity.sql
src/test/regress/sql/type_sanity.sql