Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c7aba7c

Browse files
committedDec 9, 2020
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
1 parent 8b069ef commit c7aba7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1552
-711
lines changed
 

‎contrib/postgres_fdw/deparse.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,28 @@ foreign_expr_walker(Node *node,
426426
return false;
427427

428428
/*
429-
* Recurse to remaining subexpressions. Since the container
430-
* subscripts must yield (noncollatable) integers, they won't
431-
* affect the inner_cxt state.
429+
* Recurse into the remaining subexpressions. The container
430+
* subscripts will not affect collation of the SubscriptingRef
431+
* result, so do those first and reset inner_cxt afterwards.
432432
*/
433433
if (!foreign_expr_walker((Node *) sr->refupperindexpr,
434434
glob_cxt, &inner_cxt))
435435
return false;
436+
inner_cxt.collation = InvalidOid;
437+
inner_cxt.state = FDW_COLLATE_NONE;
436438
if (!foreign_expr_walker((Node *) sr->reflowerindexpr,
437439
glob_cxt, &inner_cxt))
438440
return false;
441+
inner_cxt.collation = InvalidOid;
442+
inner_cxt.state = FDW_COLLATE_NONE;
439443
if (!foreign_expr_walker((Node *) sr->refexpr,
440444
glob_cxt, &inner_cxt))
441445
return false;
442446

443447
/*
444-
* Container subscripting should yield same collation as
445-
* input, but for safety use same logic as for function nodes.
448+
* Container subscripting typically yields same collation as
449+
* refexpr's, but in case it doesn't, use same logic as for
450+
* function nodes.
446451
*/
447452
collation = sr->refcollid;
448453
if (collation == InvalidOid)

‎doc/src/sgml/catalogs.sgml

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8740,26 +8740,38 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
87408740
</para></entry>
87418741
</row>
87428742

8743+
<row>
8744+
<entry role="catalog_table_entry"><para role="column_definition">
8745+
<structfield>typsubscript</structfield> <type>regproc</type>
8746+
(references <link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.<structfield>oid</structfield>)
8747+
</para>
8748+
<para>
8749+
Subscripting handler function's OID, or zero if this type doesn't
8750+
support subscripting. Types that are <quote>true</quote> array
8751+
types have <structfield>typsubscript</structfield>
8752+
= <function>array_subscript_handler</function>, but other types may
8753+
have other handler functions to implement specialized subscripting
8754+
behavior.
8755+
</para></entry>
8756+
</row>
8757+
87438758
<row>
87448759
<entry role="catalog_table_entry"><para role="column_definition">
87458760
<structfield>typelem</structfield> <type>oid</type>
87468761
(references <link linkend="catalog-pg-type"><structname>pg_type</structname></link>.<structfield>oid</structfield>)
87478762
</para>
87488763
<para>
87498764
If <structfield>typelem</structfield> is not 0 then it
8750-
identifies another row in <structname>pg_type</structname>.
8751-
The current type can then be subscripted like an array yielding
8752-
values of type <structfield>typelem</structfield>. A
8753-
<quote>true</quote> array type is variable length
8754-
(<structfield>typlen</structfield> = -1),
8755-
but some fixed-length (<structfield>typlen</structfield> &gt; 0) types
8756-
also have nonzero <structfield>typelem</structfield>, for example
8757-
<type>name</type> and <type>point</type>.
8758-
If a fixed-length type has a <structfield>typelem</structfield> then
8759-
its internal representation must be some number of values of the
8760-
<structfield>typelem</structfield> data type with no other data.
8761-
Variable-length array types have a header defined by the array
8762-
subroutines.
8765+
identifies another row in <structname>pg_type</structname>,
8766+
defining the type yielded by subscripting. This should be 0
8767+
if <structfield>typsubscript</structfield> is 0. However, it can
8768+
be 0 when <structfield>typsubscript</structfield> isn't 0, if the
8769+
handler doesn't need <structfield>typelem</structfield> to
8770+
determine the subscripting result type.
8771+
Note that a <structfield>typelem</structfield> dependency is
8772+
considered to imply physical containment of the element type in
8773+
this type; so DDL changes on the element type might be restricted
8774+
by the presence of this type.
87638775
</para></entry>
87648776
</row>
87658777

0 commit comments

Comments
 (0)
Please sign in to comment.