subscripting.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*-------------------------------------------------------------------------
  2. *
  3. * subscripting.h
  4. * API for generic type subscripting
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/nodes/subscripting.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SUBSCRIPTING_H
  14. #define SUBSCRIPTING_H
  15. #include "nodes/primnodes.h"
  16. /* Forward declarations, to avoid including other headers */
  17. struct ParseState;
  18. struct SubscriptingRefState;
  19. struct SubscriptExecSteps;
  20. /*
  21. * The SQL-visible function that defines a subscripting method is declared
  22. * subscripting_function(internal) returns internal
  23. * but it actually is not passed any parameter. It must return a pointer
  24. * to a "struct SubscriptRoutines" that provides pointers to the individual
  25. * subscript parsing and execution methods. Typically the pointer will point
  26. * to a "static const" variable, but at need it can point to palloc'd space.
  27. * The type (after domain-flattening) of the head variable or expression
  28. * of a subscripting construct determines which subscripting function is
  29. * called for that construct.
  30. *
  31. * In addition to the method pointers, struct SubscriptRoutines includes
  32. * several bool flags that specify properties of the subscripting actions
  33. * this data type can perform:
  34. *
  35. * fetch_strict indicates that a fetch SubscriptRef is strict, i.e., returns
  36. * NULL if any input (either the container or any subscript) is NULL.
  37. *
  38. * fetch_leakproof indicates that a fetch SubscriptRef is leakproof, i.e.,
  39. * will not throw any data-value-dependent errors. Typically this requires
  40. * silently returning NULL for invalid subscripts.
  41. *
  42. * store_leakproof similarly indicates whether an assignment SubscriptRef is
  43. * leakproof. (It is common to prefer throwing errors for invalid subscripts
  44. * in assignments; that's fine, but it makes the operation not leakproof.
  45. * In current usage there is no advantage in making assignments leakproof.)
  46. *
  47. * There is no store_strict flag. Such behavior would generally be
  48. * undesirable, since for example a null subscript in an assignment would
  49. * cause the entire container to become NULL.
  50. *
  51. * Regardless of these flags, all SubscriptRefs are expected to be immutable,
  52. * that is they must always give the same results for the same inputs.
  53. * They are expected to always be parallel-safe, as well.
  54. */
  55. /*
  56. * The transform method is called during parse analysis of a subscripting
  57. * construct. The SubscriptingRef node has been constructed, but some of
  58. * its fields still need to be filled in, and the subscript expression(s)
  59. * are still in raw form. The transform method is responsible for doing
  60. * parse analysis of each subscript expression (using transformExpr),
  61. * coercing the subscripts to whatever type it needs, and building the
  62. * refupperindexpr and reflowerindexpr lists from those results. The
  63. * reflowerindexpr list must be empty for an element operation, or the
  64. * same length as refupperindexpr for a slice operation. Insert NULLs
  65. * (that is, an empty parse tree, not a null Const node) for any omitted
  66. * subscripts in a slice operation. (Of course, if the transform method
  67. * does not care to support slicing, it can just throw an error if isSlice.)
  68. * See array_subscript_transform() for sample code.
  69. *
  70. * The transform method is also responsible for identifying the result type
  71. * of the subscripting operation. At call, refcontainertype and reftypmod
  72. * describe the container type (this will be a base type not a domain), and
  73. * refelemtype is set to the container type's pg_type.typelem value. The
  74. * transform method must set refrestype and reftypmod to describe the result
  75. * of subscripting. For arrays, refrestype is set to refelemtype for an
  76. * element operation or refcontainertype for a slice, while reftypmod stays
  77. * the same in either case; but other types might use other rules. The
  78. * transform method should ignore refcollid, as that's determined later on
  79. * during parsing.
  80. *
  81. * At call, refassgnexpr has not been filled in, so the SubscriptingRef node
  82. * always looks like a fetch; refrestype should be set as though for a
  83. * fetch, too. (The isAssignment parameter is typically only useful if the
  84. * transform method wishes to throw an error for not supporting assignment.)
  85. * To complete processing of an assignment, the core parser will coerce the
  86. * element/slice source expression to the returned refrestype and reftypmod
  87. * before putting it into refassgnexpr. It will then set refrestype and
  88. * reftypmod to again describe the container type, since that's what an
  89. * assignment must return.
  90. */
  91. typedef void (*SubscriptTransform) (SubscriptingRef *sbsref,
  92. List *indirection,
  93. struct ParseState *pstate,
  94. bool isSlice,
  95. bool isAssignment);
  96. /*
  97. * The exec_setup method is called during executor-startup compilation of a
  98. * SubscriptingRef node in an expression. It must fill *methods with pointers
  99. * to functions that can be called for execution of the node. Optionally,
  100. * exec_setup can initialize sbsrefstate->workspace to point to some palloc'd
  101. * workspace for execution. (Typically, such workspace is used to hold
  102. * looked-up catalog data and/or provide space for the check_subscripts step
  103. * to pass data forward to the other step functions.) See executor/execExpr.h
  104. * for the definitions of these structs and other ones used in expression
  105. * execution.
  106. *
  107. * The methods to be provided are:
  108. *
  109. * sbs_check_subscripts: examine the just-computed subscript values available
  110. * in sbsrefstate's arrays, and possibly convert them into another form
  111. * (stored in sbsrefstate->workspace). Return TRUE to continue with
  112. * evaluation of the subscripting construct, or FALSE to skip it and return an
  113. * overall NULL result. If this is a fetch and the data type's fetch_strict
  114. * flag is true, then sbs_check_subscripts must return FALSE if there are any
  115. * NULL subscripts. Otherwise it can choose to throw an error, or return
  116. * FALSE, or let sbs_fetch or sbs_assign deal with the null subscripts.
  117. *
  118. * sbs_fetch: perform a subscripting fetch, using the container value in
  119. * *op->resvalue and the subscripts from sbs_check_subscripts. If
  120. * fetch_strict is true then all these inputs can be assumed non-NULL,
  121. * otherwise sbs_fetch must check for null inputs. Place the result in
  122. * *op->resvalue / *op->resnull.
  123. *
  124. * sbs_assign: perform a subscripting assignment, using the original
  125. * container value in *op->resvalue / *op->resnull, the subscripts from
  126. * sbs_check_subscripts, and the new element/slice value in
  127. * sbsrefstate->replacevalue/replacenull. Any of these inputs might be NULL
  128. * (unless sbs_check_subscripts rejected null subscripts). Place the result
  129. * (an entire new container value) in *op->resvalue / *op->resnull.
  130. *
  131. * sbs_fetch_old: this is only used in cases where an element or slice
  132. * assignment involves an assignment to a sub-field or sub-element
  133. * (i.e., nested containers are involved). It must fetch the existing
  134. * value of the target element or slice. This is exactly the same as
  135. * sbs_fetch except that (a) it must cope with a NULL container, and
  136. * with NULL subscripts if sbs_check_subscripts allows them (typically,
  137. * returning NULL is good enough); and (b) the result must be placed in
  138. * sbsrefstate->prevvalue/prevnull, without overwriting *op->resvalue.
  139. *
  140. * Subscripting implementations that do not support assignment need not
  141. * provide sbs_assign or sbs_fetch_old methods. It might be reasonable
  142. * to also omit sbs_check_subscripts, in which case the sbs_fetch method must
  143. * combine the functionality of sbs_check_subscripts and sbs_fetch. (The
  144. * main reason to have a separate sbs_check_subscripts method is so that
  145. * sbs_fetch_old and sbs_assign need not duplicate subscript processing.)
  146. * Set the relevant pointers to NULL for any omitted methods.
  147. */
  148. typedef void (*SubscriptExecSetup) (const SubscriptingRef *sbsref,
  149. struct SubscriptingRefState *sbsrefstate,
  150. struct SubscriptExecSteps *methods);
  151. /* Struct returned by the SQL-visible subscript handler function */
  152. typedef struct SubscriptRoutines
  153. {
  154. SubscriptTransform transform; /* parse analysis function */
  155. SubscriptExecSetup exec_setup; /* expression compilation function */
  156. bool fetch_strict; /* is fetch SubscriptRef strict? */
  157. bool fetch_leakproof; /* is fetch SubscriptRef leakproof? */
  158. bool store_leakproof; /* is assignment SubscriptRef leakproof? */
  159. } SubscriptRoutines;
  160. #endif /* SUBSCRIPTING_H */