LCOV - code coverage report
Current view: top level - src/backend/nodes - outfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17beta1 Lines: 294 363 81.0 %
Date: 2024-05-29 10:10:37 Functions: 24 30 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * outfuncs.c
       4             :  *    Output functions for Postgres tree nodes.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/nodes/outfuncs.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include <ctype.h>
      18             : 
      19             : #include "access/attnum.h"
      20             : #include "common/shortest_dec.h"
      21             : #include "lib/stringinfo.h"
      22             : #include "miscadmin.h"
      23             : #include "nodes/bitmapset.h"
      24             : #include "nodes/nodes.h"
      25             : #include "nodes/pg_list.h"
      26             : #include "utils/datum.h"
      27             : 
      28             : /* State flag that determines how nodeToStringInternal() should treat location fields */
      29             : static bool write_location_fields = false;
      30             : 
      31             : static void outChar(StringInfo str, char c);
      32             : static void outDouble(StringInfo str, double d);
      33             : 
      34             : 
      35             : /*
      36             :  * Macros to simplify output of different kinds of fields.  Use these
      37             :  * wherever possible to reduce the chance for silly typos.  Note that these
      38             :  * hard-wire conventions about the names of the local variables in an Out
      39             :  * routine.
      40             :  */
      41             : 
      42             : /* Write the label for the node type */
      43             : #define WRITE_NODE_TYPE(nodelabel) \
      44             :     appendStringInfoString(str, nodelabel)
      45             : 
      46             : /* Write an integer field (anything written as ":fldname %d") */
      47             : #define WRITE_INT_FIELD(fldname) \
      48             :     appendStringInfo(str, " :" CppAsString(fldname) " %d", node->fldname)
      49             : 
      50             : /* Write an unsigned integer field (anything written as ":fldname %u") */
      51             : #define WRITE_UINT_FIELD(fldname) \
      52             :     appendStringInfo(str, " :" CppAsString(fldname) " %u", node->fldname)
      53             : 
      54             : /* Write an unsigned integer field (anything written with UINT64_FORMAT) */
      55             : #define WRITE_UINT64_FIELD(fldname) \
      56             :     appendStringInfo(str, " :" CppAsString(fldname) " " UINT64_FORMAT, \
      57             :                      node->fldname)
      58             : 
      59             : /* Write an OID field (don't hard-wire assumption that OID is same as uint) */
      60             : #define WRITE_OID_FIELD(fldname) \
      61             :     appendStringInfo(str, " :" CppAsString(fldname) " %u", node->fldname)
      62             : 
      63             : /* Write a long-integer field */
      64             : #define WRITE_LONG_FIELD(fldname) \
      65             :     appendStringInfo(str, " :" CppAsString(fldname) " %ld", node->fldname)
      66             : 
      67             : /* Write a char field (ie, one ascii character) */
      68             : #define WRITE_CHAR_FIELD(fldname) \
      69             :     (appendStringInfo(str, " :" CppAsString(fldname) " "), \
      70             :      outChar(str, node->fldname))
      71             : 
      72             : /* Write an enumerated-type field as an integer code */
      73             : #define WRITE_ENUM_FIELD(fldname, enumtype) \
      74             :     appendStringInfo(str, " :" CppAsString(fldname) " %d", \
      75             :                      (int) node->fldname)
      76             : 
      77             : /* Write a float field (actually, they're double) */
      78             : #define WRITE_FLOAT_FIELD(fldname) \
      79             :     (appendStringInfo(str, " :" CppAsString(fldname) " "), \
      80             :      outDouble(str, node->fldname))
      81             : 
      82             : /* Write a boolean field */
      83             : #define WRITE_BOOL_FIELD(fldname) \
      84             :     appendStringInfo(str, " :" CppAsString(fldname) " %s", \
      85             :                      booltostr(node->fldname))
      86             : 
      87             : /* Write a character-string (possibly NULL) field */
      88             : #define WRITE_STRING_FIELD(fldname) \
      89             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
      90             :      outToken(str, node->fldname))
      91             : 
      92             : /* Write a parse location field (actually same as INT case) */
      93             : #define WRITE_LOCATION_FIELD(fldname) \
      94             :     appendStringInfo(str, " :" CppAsString(fldname) " %d", write_location_fields ? node->fldname : -1)
      95             : 
      96             : /* Write a Node field */
      97             : #define WRITE_NODE_FIELD(fldname) \
      98             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
      99             :      outNode(str, node->fldname))
     100             : 
     101             : /* Write a bitmapset field */
     102             : #define WRITE_BITMAPSET_FIELD(fldname) \
     103             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     104             :      outBitmapset(str, node->fldname))
     105             : 
     106             : /* Write a variable-length array (not a List) of Node pointers */
     107             : #define WRITE_NODE_ARRAY(fldname, len) \
     108             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     109             :      writeNodeArray(str, (const Node * const *) node->fldname, len))
     110             : 
     111             : /* Write a variable-length array of AttrNumber */
     112             : #define WRITE_ATTRNUMBER_ARRAY(fldname, len) \
     113             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     114             :      writeAttrNumberCols(str, node->fldname, len))
     115             : 
     116             : /* Write a variable-length array of Oid */
     117             : #define WRITE_OID_ARRAY(fldname, len) \
     118             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     119             :      writeOidCols(str, node->fldname, len))
     120             : 
     121             : /* Write a variable-length array of Index */
     122             : #define WRITE_INDEX_ARRAY(fldname, len) \
     123             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     124             :      writeIndexCols(str, node->fldname, len))
     125             : 
     126             : /* Write a variable-length array of int */
     127             : #define WRITE_INT_ARRAY(fldname, len) \
     128             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     129             :      writeIntCols(str, node->fldname, len))
     130             : 
     131             : /* Write a variable-length array of bool */
     132             : #define WRITE_BOOL_ARRAY(fldname, len) \
     133             :     (appendStringInfoString(str, " :" CppAsString(fldname) " "), \
     134             :      writeBoolCols(str, node->fldname, len))
     135             : 
     136             : #define booltostr(x)  ((x) ? "true" : "false")
     137             : 
     138             : 
     139             : /*
     140             :  * outToken
     141             :  *    Convert an ordinary string (eg, an identifier) into a form that
     142             :  *    will be decoded back to a plain token by read.c's functions.
     143             :  *
     144             :  *    If a null string pointer is given, it is encoded as '<>'.
     145             :  *    An empty string is encoded as '""'.  To avoid ambiguity, input
     146             :  *    strings beginning with '<' or '"' receive a leading backslash.
     147             :  */
     148             : void
     149    38475810 : outToken(StringInfo str, const char *s)
     150             : {
     151    38475810 :     if (s == NULL)
     152             :     {
     153     6451858 :         appendStringInfoString(str, "<>");
     154     6451858 :         return;
     155             :     }
     156    32023952 :     if (*s == '\0')
     157             :     {
     158         156 :         appendStringInfoString(str, "\"\"");
     159         156 :         return;
     160             :     }
     161             : 
     162             :     /*
     163             :      * Look for characters or patterns that are treated specially by read.c
     164             :      * (either in pg_strtok() or in nodeRead()), and therefore need a
     165             :      * protective backslash.
     166             :      */
     167             :     /* These characters only need to be quoted at the start of the string */
     168    32023796 :     if (*s == '<' ||
     169    31952618 :         *s == '"' ||
     170    31946540 :         isdigit((unsigned char) *s) ||
     171    31812046 :         ((*s == '+' || *s == '-') &&
     172       26878 :          (isdigit((unsigned char) s[1]) || s[1] == '.')))
     173      215268 :         appendStringInfoChar(str, '\\');
     174   286432448 :     while (*s)
     175             :     {
     176             :         /* These chars must be backslashed anywhere in the string */
     177   254408652 :         if (*s == ' ' || *s == '\n' || *s == '\t' ||
     178   253250992 :             *s == '(' || *s == ')' || *s == '{' || *s == '}' ||
     179   253054550 :             *s == '\\')
     180     1363880 :             appendStringInfoChar(str, '\\');
     181   254408652 :         appendStringInfoChar(str, *s++);
     182             :     }
     183             : }
     184             : 
     185             : /*
     186             :  * Convert one char.  Goes through outToken() so that special characters are
     187             :  * escaped.
     188             :  */
     189             : static void
     190     2447130 : outChar(StringInfo str, char c)
     191             : {
     192             :     char        in[2];
     193             : 
     194             :     /* Traditionally, we've represented \0 as <>, so keep doing that */
     195     2447130 :     if (c == '\0')
     196             :     {
     197      686470 :         appendStringInfoString(str, "<>");
     198      686470 :         return;
     199             :     }
     200             : 
     201     1760660 :     in[0] = c;
     202     1760660 :     in[1] = '\0';
     203             : 
     204     1760660 :     outToken(str, in);
     205             : }
     206             : 
     207             : /*
     208             :  * Convert a double value, attempting to ensure the value is preserved exactly.
     209             :  */
     210             : static void
     211     3097668 : outDouble(StringInfo str, double d)
     212             : {
     213             :     char        buf[DOUBLE_SHORTEST_DECIMAL_LEN];
     214             : 
     215     3097668 :     double_to_shortest_decimal_buf(d, buf);
     216     3097668 :     appendStringInfoString(str, buf);
     217     3097668 : }
     218             : 
     219             : /*
     220             :  * common implementation for scalar-array-writing functions
     221             :  *
     222             :  * The data format is either "<>" for a NULL pointer or "(item item item)".
     223             :  * fmtstr must include a leading space, and the rest of it must produce
     224             :  * something that will be seen as a single simple token by pg_strtok().
     225             :  * convfunc can be empty, or the name of a conversion macro or function.
     226             :  */
     227             : #define WRITE_SCALAR_ARRAY(fnname, datatype, fmtstr, convfunc) \
     228             : static void \
     229             : fnname(StringInfo str, const datatype *arr, int len) \
     230             : { \
     231             :     if (arr != NULL) \
     232             :     { \
     233             :         appendStringInfoChar(str, '('); \
     234             :         for (int i = 0; i < len; i++) \
     235             :             appendStringInfo(str, fmtstr, convfunc(arr[i])); \
     236             :         appendStringInfoChar(str, ')'); \
     237             :     } \
     238             :     else \
     239             :         appendStringInfoString(str, "<>"); \
     240             : }
     241             : 
     242      384764 : WRITE_SCALAR_ARRAY(writeAttrNumberCols, AttrNumber, " %d",)
     243      485480 : WRITE_SCALAR_ARRAY(writeOidCols, Oid, " %u",)
     244           0 : WRITE_SCALAR_ARRAY(writeIndexCols, Index, " %u",)
     245       21338 : WRITE_SCALAR_ARRAY(writeIntCols, int, " %d",)
     246      159576 : WRITE_SCALAR_ARRAY(writeBoolCols, bool, " %s", booltostr)
     247             : 
     248             : /*
     249             :  * Print an array (not a List) of Node pointers.
     250             :  *
     251             :  * The decoration is identical to that of scalar arrays, but we can't
     252             :  * quite use appendStringInfo() in the loop.
     253             :  */
     254             : static void
     255           0 : writeNodeArray(StringInfo str, const Node *const *arr, int len)
     256             : {
     257           0 :     if (arr != NULL)
     258             :     {
     259           0 :         appendStringInfoChar(str, '(');
     260           0 :         for (int i = 0; i < len; i++)
     261             :         {
     262           0 :             appendStringInfoChar(str, ' ');
     263           0 :             outNode(str, arr[i]);
     264             :         }
     265           0 :         appendStringInfoChar(str, ')');
     266             :     }
     267             :     else
     268           0 :         appendStringInfoString(str, "<>");
     269           0 : }
     270             : 
     271             : /*
     272             :  * Print a List.
     273             :  */
     274             : static void
     275    15433468 : _outList(StringInfo str, const List *node)
     276             : {
     277             :     const ListCell *lc;
     278             : 
     279    15433468 :     appendStringInfoChar(str, '(');
     280             : 
     281    15433468 :     if (IsA(node, IntList))
     282      429926 :         appendStringInfoChar(str, 'i');
     283    15003542 :     else if (IsA(node, OidList))
     284      610842 :         appendStringInfoChar(str, 'o');
     285    14392700 :     else if (IsA(node, XidList))
     286           0 :         appendStringInfoChar(str, 'x');
     287             : 
     288    64878744 :     foreach(lc, node)
     289             :     {
     290             :         /*
     291             :          * For the sake of backward compatibility, we emit a slightly
     292             :          * different whitespace format for lists of nodes vs. other types of
     293             :          * lists. XXX: is this necessary?
     294             :          */
     295    49445276 :         if (IsA(node, List))
     296             :         {
     297    44578114 :             outNode(str, lfirst(lc));
     298    44578114 :             if (lnext(node, lc))
     299    30185414 :                 appendStringInfoChar(str, ' ');
     300             :         }
     301     4867162 :         else if (IsA(node, IntList))
     302     3709272 :             appendStringInfo(str, " %d", lfirst_int(lc));
     303     1157890 :         else if (IsA(node, OidList))
     304     1157890 :             appendStringInfo(str, " %u", lfirst_oid(lc));
     305           0 :         else if (IsA(node, XidList))
     306           0 :             appendStringInfo(str, " %u", lfirst_xid(lc));
     307             :         else
     308           0 :             elog(ERROR, "unrecognized list node type: %d",
     309             :                  (int) node->type);
     310             :     }
     311             : 
     312    15433468 :     appendStringInfoChar(str, ')');
     313    15433468 : }
     314             : 
     315             : /*
     316             :  * outBitmapset -
     317             :  *     converts a bitmap set of integers
     318             :  *
     319             :  * Note: the output format is "(b int int ...)", similar to an integer List.
     320             :  *
     321             :  * We export this function for use by extensions that define extensible nodes.
     322             :  * That's somewhat historical, though, because calling outNode() will work.
     323             :  */
     324             : void
     325    15179414 : outBitmapset(StringInfo str, const Bitmapset *bms)
     326             : {
     327             :     int         x;
     328             : 
     329    15179414 :     appendStringInfoChar(str, '(');
     330    15179414 :     appendStringInfoChar(str, 'b');
     331    15179414 :     x = -1;
     332    19296888 :     while ((x = bms_next_member(bms, x)) >= 0)
     333     4117474 :         appendStringInfo(str, " %d", x);
     334    15179414 :     appendStringInfoChar(str, ')');
     335    15179414 : }
     336             : 
     337             : /*
     338             :  * Print the value of a Datum given its type.
     339             :  */
     340             : void
     341     2062460 : outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
     342             : {
     343             :     Size        length,
     344             :                 i;
     345             :     char       *s;
     346             : 
     347     2062460 :     length = datumGetSize(value, typbyval, typlen);
     348             : 
     349     2062460 :     if (typbyval)
     350             :     {
     351     1366624 :         s = (char *) (&value);
     352     1366624 :         appendStringInfo(str, "%u [ ", (unsigned int) length);
     353    12299616 :         for (i = 0; i < (Size) sizeof(Datum); i++)
     354    10932992 :             appendStringInfo(str, "%d ", (int) (s[i]));
     355     1366624 :         appendStringInfoChar(str, ']');
     356             :     }
     357             :     else
     358             :     {
     359      695836 :         s = (char *) DatumGetPointer(value);
     360      695836 :         if (!PointerIsValid(s))
     361           0 :             appendStringInfoString(str, "0 [ ]");
     362             :         else
     363             :         {
     364      695836 :             appendStringInfo(str, "%u [ ", (unsigned int) length);
     365    64710282 :             for (i = 0; i < length; i++)
     366    64014446 :                 appendStringInfo(str, "%d ", (int) (s[i]));
     367      695836 :             appendStringInfoChar(str, ']');
     368             :         }
     369             :     }
     370     2062460 : }
     371             : 
     372             : 
     373             : #include "outfuncs.funcs.c"
     374             : 
     375             : 
     376             : /*
     377             :  * Support functions for nodes with custom_read_write attribute or
     378             :  * special_read_write attribute
     379             :  */
     380             : 
     381             : static void
     382     2228114 : _outConst(StringInfo str, const Const *node)
     383             : {
     384     2228114 :     WRITE_NODE_TYPE("CONST");
     385             : 
     386     2228114 :     WRITE_OID_FIELD(consttype);
     387     2228114 :     WRITE_INT_FIELD(consttypmod);
     388     2228114 :     WRITE_OID_FIELD(constcollid);
     389     2228114 :     WRITE_INT_FIELD(constlen);
     390     2228114 :     WRITE_BOOL_FIELD(constbyval);
     391     2228114 :     WRITE_BOOL_FIELD(constisnull);
     392     2228114 :     WRITE_LOCATION_FIELD(location);
     393             : 
     394     2228114 :     appendStringInfoString(str, " :constvalue ");
     395     2228114 :     if (node->constisnull)
     396      165654 :         appendStringInfoString(str, "<>");
     397             :     else
     398     2062460 :         outDatum(str, node->constvalue, node->constlen, node->constbyval);
     399     2228114 : }
     400             : 
     401             : static void
     402      308866 : _outBoolExpr(StringInfo str, const BoolExpr *node)
     403             : {
     404      308866 :     char       *opstr = NULL;
     405             : 
     406      308866 :     WRITE_NODE_TYPE("BOOLEXPR");
     407             : 
     408             :     /* do-it-yourself enum representation */
     409      308866 :     switch (node->boolop)
     410             :     {
     411      232744 :         case AND_EXPR:
     412      232744 :             opstr = "and";
     413      232744 :             break;
     414       39218 :         case OR_EXPR:
     415       39218 :             opstr = "or";
     416       39218 :             break;
     417       36904 :         case NOT_EXPR:
     418       36904 :             opstr = "not";
     419       36904 :             break;
     420             :     }
     421      308866 :     appendStringInfoString(str, " :boolop ");
     422      308866 :     outToken(str, opstr);
     423             : 
     424      308866 :     WRITE_NODE_FIELD(args);
     425      308866 :     WRITE_LOCATION_FIELD(location);
     426      308866 : }
     427             : 
     428             : static void
     429           0 : _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
     430             : {
     431             :     int         i;
     432             : 
     433           0 :     WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
     434             : 
     435           0 :     WRITE_UINT_FIELD(con_relid);
     436           0 :     WRITE_UINT_FIELD(ref_relid);
     437           0 :     WRITE_INT_FIELD(nkeys);
     438           0 :     WRITE_ATTRNUMBER_ARRAY(conkey, node->nkeys);
     439           0 :     WRITE_ATTRNUMBER_ARRAY(confkey, node->nkeys);
     440           0 :     WRITE_OID_ARRAY(conpfeqop, node->nkeys);
     441           0 :     WRITE_INT_FIELD(nmatched_ec);
     442           0 :     WRITE_INT_FIELD(nconst_ec);
     443           0 :     WRITE_INT_FIELD(nmatched_rcols);
     444           0 :     WRITE_INT_FIELD(nmatched_ri);
     445             :     /* for compactness, just print the number of matches per column: */
     446           0 :     appendStringInfoString(str, " :eclass");
     447           0 :     for (i = 0; i < node->nkeys; i++)
     448           0 :         appendStringInfo(str, " %d", (node->eclass[i] != NULL));
     449           0 :     appendStringInfoString(str, " :rinfos");
     450           0 :     for (i = 0; i < node->nkeys; i++)
     451           0 :         appendStringInfo(str, " %d", list_length(node->rinfos[i]));
     452           0 : }
     453             : 
     454             : static void
     455           0 : _outEquivalenceClass(StringInfo str, const EquivalenceClass *node)
     456             : {
     457             :     /*
     458             :      * To simplify reading, we just chase up to the topmost merged EC and
     459             :      * print that, without bothering to show the merge-ees separately.
     460             :      */
     461           0 :     while (node->ec_merged)
     462           0 :         node = node->ec_merged;
     463             : 
     464           0 :     WRITE_NODE_TYPE("EQUIVALENCECLASS");
     465             : 
     466           0 :     WRITE_NODE_FIELD(ec_opfamilies);
     467           0 :     WRITE_OID_FIELD(ec_collation);
     468           0 :     WRITE_NODE_FIELD(ec_members);
     469           0 :     WRITE_NODE_FIELD(ec_sources);
     470           0 :     WRITE_NODE_FIELD(ec_derives);
     471           0 :     WRITE_BITMAPSET_FIELD(ec_relids);
     472           0 :     WRITE_BOOL_FIELD(ec_has_const);
     473           0 :     WRITE_BOOL_FIELD(ec_has_volatile);
     474           0 :     WRITE_BOOL_FIELD(ec_broken);
     475           0 :     WRITE_UINT_FIELD(ec_sortref);
     476           0 :     WRITE_UINT_FIELD(ec_min_security);
     477           0 :     WRITE_UINT_FIELD(ec_max_security);
     478           0 : }
     479             : 
     480             : static void
     481           0 : _outExtensibleNode(StringInfo str, const ExtensibleNode *node)
     482             : {
     483             :     const ExtensibleNodeMethods *methods;
     484             : 
     485           0 :     methods = GetExtensibleNodeMethods(node->extnodename, false);
     486             : 
     487           0 :     WRITE_NODE_TYPE("EXTENSIBLENODE");
     488             : 
     489           0 :     WRITE_STRING_FIELD(extnodename);
     490             : 
     491             :     /* serialize the private fields */
     492           0 :     methods->nodeOut(str, node);
     493           0 : }
     494             : 
     495             : static void
     496     1448446 : _outRangeTblEntry(StringInfo str, const RangeTblEntry *node)
     497             : {
     498     1448446 :     WRITE_NODE_TYPE("RANGETBLENTRY");
     499             : 
     500     1448446 :     WRITE_NODE_FIELD(alias);
     501     1448446 :     WRITE_NODE_FIELD(eref);
     502     1448446 :     WRITE_ENUM_FIELD(rtekind, RTEKind);
     503             : 
     504     1448446 :     switch (node->rtekind)
     505             :     {
     506      874970 :         case RTE_RELATION:
     507      874970 :             WRITE_OID_FIELD(relid);
     508      874970 :             WRITE_BOOL_FIELD(inh);
     509      874970 :             WRITE_CHAR_FIELD(relkind);
     510      874970 :             WRITE_INT_FIELD(rellockmode);
     511      874970 :             WRITE_UINT_FIELD(perminfoindex);
     512      874970 :             WRITE_NODE_FIELD(tablesample);
     513      874970 :             break;
     514      103558 :         case RTE_SUBQUERY:
     515      103558 :             WRITE_NODE_FIELD(subquery);
     516      103558 :             WRITE_BOOL_FIELD(security_barrier);
     517             :             /* we re-use these RELATION fields, too: */
     518      103558 :             WRITE_OID_FIELD(relid);
     519      103558 :             WRITE_BOOL_FIELD(inh);
     520      103558 :             WRITE_CHAR_FIELD(relkind);
     521      103558 :             WRITE_INT_FIELD(rellockmode);
     522      103558 :             WRITE_UINT_FIELD(perminfoindex);
     523      103558 :             break;
     524      150212 :         case RTE_JOIN:
     525      150212 :             WRITE_ENUM_FIELD(jointype, JoinType);
     526      150212 :             WRITE_INT_FIELD(joinmergedcols);
     527      150212 :             WRITE_NODE_FIELD(joinaliasvars);
     528      150212 :             WRITE_NODE_FIELD(joinleftcols);
     529      150212 :             WRITE_NODE_FIELD(joinrightcols);
     530      150212 :             WRITE_NODE_FIELD(join_using_alias);
     531      150212 :             break;
     532       82384 :         case RTE_FUNCTION:
     533       82384 :             WRITE_NODE_FIELD(functions);
     534       82384 :             WRITE_BOOL_FIELD(funcordinality);
     535       82384 :             break;
     536        1112 :         case RTE_TABLEFUNC:
     537        1112 :             WRITE_NODE_FIELD(tablefunc);
     538        1112 :             break;
     539       17312 :         case RTE_VALUES:
     540       17312 :             WRITE_NODE_FIELD(values_lists);
     541       17312 :             WRITE_NODE_FIELD(coltypes);
     542       17312 :             WRITE_NODE_FIELD(coltypmods);
     543       17312 :             WRITE_NODE_FIELD(colcollations);
     544       17312 :             break;
     545        9582 :         case RTE_CTE:
     546        9582 :             WRITE_STRING_FIELD(ctename);
     547        9582 :             WRITE_UINT_FIELD(ctelevelsup);
     548        9582 :             WRITE_BOOL_FIELD(self_reference);
     549        9582 :             WRITE_NODE_FIELD(coltypes);
     550        9582 :             WRITE_NODE_FIELD(coltypmods);
     551        9582 :             WRITE_NODE_FIELD(colcollations);
     552        9582 :             break;
     553         876 :         case RTE_NAMEDTUPLESTORE:
     554         876 :             WRITE_STRING_FIELD(enrname);
     555         876 :             WRITE_FLOAT_FIELD(enrtuples);
     556         876 :             WRITE_NODE_FIELD(coltypes);
     557         876 :             WRITE_NODE_FIELD(coltypmods);
     558         876 :             WRITE_NODE_FIELD(colcollations);
     559             :             /* we re-use these RELATION fields, too: */
     560         876 :             WRITE_OID_FIELD(relid);
     561         876 :             break;
     562      208440 :         case RTE_RESULT:
     563             :             /* no extra fields */
     564      208440 :             break;
     565           0 :         default:
     566           0 :             elog(ERROR, "unrecognized RTE kind: %d", (int) node->rtekind);
     567             :             break;
     568             :     }
     569             : 
     570     1448446 :     WRITE_BOOL_FIELD(lateral);
     571     1448446 :     WRITE_BOOL_FIELD(inFromCl);
     572     1448446 :     WRITE_NODE_FIELD(securityQuals);
     573     1448446 : }
     574             : 
     575             : static void
     576      569964 : _outA_Expr(StringInfo str, const A_Expr *node)
     577             : {
     578      569964 :     WRITE_NODE_TYPE("A_EXPR");
     579             : 
     580      569964 :     switch (node->kind)
     581             :     {
     582      531188 :         case AEXPR_OP:
     583      531188 :             WRITE_NODE_FIELD(name);
     584      531188 :             break;
     585       12646 :         case AEXPR_OP_ANY:
     586       12646 :             appendStringInfoString(str, " ANY");
     587       12646 :             WRITE_NODE_FIELD(name);
     588       12646 :             break;
     589         144 :         case AEXPR_OP_ALL:
     590         144 :             appendStringInfoString(str, " ALL");
     591         144 :             WRITE_NODE_FIELD(name);
     592         144 :             break;
     593         862 :         case AEXPR_DISTINCT:
     594         862 :             appendStringInfoString(str, " DISTINCT");
     595         862 :             WRITE_NODE_FIELD(name);
     596         862 :             break;
     597          68 :         case AEXPR_NOT_DISTINCT:
     598          68 :             appendStringInfoString(str, " NOT_DISTINCT");
     599          68 :             WRITE_NODE_FIELD(name);
     600          68 :             break;
     601         346 :         case AEXPR_NULLIF:
     602         346 :             appendStringInfoString(str, " NULLIF");
     603         346 :             WRITE_NODE_FIELD(name);
     604         346 :             break;
     605       21690 :         case AEXPR_IN:
     606       21690 :             appendStringInfoString(str, " IN");
     607       21690 :             WRITE_NODE_FIELD(name);
     608       21690 :             break;
     609        2152 :         case AEXPR_LIKE:
     610        2152 :             appendStringInfoString(str, " LIKE");
     611        2152 :             WRITE_NODE_FIELD(name);
     612        2152 :             break;
     613         204 :         case AEXPR_ILIKE:
     614         204 :             appendStringInfoString(str, " ILIKE");
     615         204 :             WRITE_NODE_FIELD(name);
     616         204 :             break;
     617          70 :         case AEXPR_SIMILAR:
     618          70 :             appendStringInfoString(str, " SIMILAR");
     619          70 :             WRITE_NODE_FIELD(name);
     620          70 :             break;
     621         558 :         case AEXPR_BETWEEN:
     622         558 :             appendStringInfoString(str, " BETWEEN");
     623         558 :             WRITE_NODE_FIELD(name);
     624         558 :             break;
     625          12 :         case AEXPR_NOT_BETWEEN:
     626          12 :             appendStringInfoString(str, " NOT_BETWEEN");
     627          12 :             WRITE_NODE_FIELD(name);
     628          12 :             break;
     629          12 :         case AEXPR_BETWEEN_SYM:
     630          12 :             appendStringInfoString(str, " BETWEEN_SYM");
     631          12 :             WRITE_NODE_FIELD(name);
     632          12 :             break;
     633          12 :         case AEXPR_NOT_BETWEEN_SYM:
     634          12 :             appendStringInfoString(str, " NOT_BETWEEN_SYM");
     635          12 :             WRITE_NODE_FIELD(name);
     636          12 :             break;
     637           0 :         default:
     638           0 :             elog(ERROR, "unrecognized A_Expr_Kind: %d", (int) node->kind);
     639             :             break;
     640             :     }
     641             : 
     642      569964 :     WRITE_NODE_FIELD(lexpr);
     643      569964 :     WRITE_NODE_FIELD(rexpr);
     644      569964 :     WRITE_LOCATION_FIELD(location);
     645      569964 : }
     646             : 
     647             : static void
     648      478544 : _outInteger(StringInfo str, const Integer *node)
     649             : {
     650      478544 :     appendStringInfo(str, "%d", node->ival);
     651      478544 : }
     652             : 
     653             : static void
     654       11308 : _outFloat(StringInfo str, const Float *node)
     655             : {
     656             :     /*
     657             :      * We assume the value is a valid numeric literal and so does not need
     658             :      * quoting.
     659             :      */
     660       11308 :     appendStringInfoString(str, node->fval);
     661       11308 : }
     662             : 
     663             : static void
     664       82042 : _outBoolean(StringInfo str, const Boolean *node)
     665             : {
     666       82042 :     appendStringInfoString(str, node->boolval ? "true" : "false");
     667       82042 : }
     668             : 
     669             : static void
     670    22780724 : _outString(StringInfo str, const String *node)
     671             : {
     672             :     /*
     673             :      * We use outToken to provide escaping of the string's content, but we
     674             :      * don't want it to convert an empty string to '""', because we're putting
     675             :      * double quotes around the string already.
     676             :      */
     677    22780724 :     appendStringInfoChar(str, '"');
     678    22780724 :     if (node->sval[0] != '\0')
     679    22753318 :         outToken(str, node->sval);
     680    22780724 :     appendStringInfoChar(str, '"');
     681    22780724 : }
     682             : 
     683             : static void
     684        4068 : _outBitString(StringInfo str, const BitString *node)
     685             : {
     686             :     /*
     687             :      * The lexer will always produce a string starting with 'b' or 'x'.  There
     688             :      * might be characters following that that need escaping, but outToken
     689             :      * won't escape the 'b' or 'x'.  This is relied on by nodeTokenType.
     690             :      */
     691             :     Assert(node->bsval[0] == 'b' || node->bsval[0] == 'x');
     692        4068 :     outToken(str, node->bsval);
     693        4068 : }
     694             : 
     695             : static void
     696     1228596 : _outA_Const(StringInfo str, const A_Const *node)
     697             : {
     698     1228596 :     WRITE_NODE_TYPE("A_CONST");
     699             : 
     700     1228596 :     if (node->isnull)
     701       68538 :         appendStringInfoString(str, " NULL");
     702             :     else
     703             :     {
     704     1160058 :         appendStringInfoString(str, " :val ");
     705     1160058 :         outNode(str, &node->val);
     706             :     }
     707     1228596 :     WRITE_LOCATION_FIELD(location);
     708     1228596 : }
     709             : 
     710             : 
     711             : /*
     712             :  * outNode -
     713             :  *    converts a Node into ascii string and append it to 'str'
     714             :  */
     715             : void
     716   125200862 : outNode(StringInfo str, const void *obj)
     717             : {
     718             :     /* Guard against stack overflow due to overly complex expressions */
     719   125200862 :     check_stack_depth();
     720             : 
     721   125200862 :     if (obj == NULL)
     722    46155070 :         appendStringInfoString(str, "<>");
     723    79045792 :     else if (IsA(obj, List) || IsA(obj, IntList) || IsA(obj, OidList) ||
     724    63612324 :              IsA(obj, XidList))
     725    15433468 :         _outList(str, obj);
     726             :     /* nodeRead does not want to see { } around these! */
     727    63612324 :     else if (IsA(obj, Integer))
     728      478544 :         _outInteger(str, (Integer *) obj);
     729    63133780 :     else if (IsA(obj, Float))
     730       11308 :         _outFloat(str, (Float *) obj);
     731    63122472 :     else if (IsA(obj, Boolean))
     732       82042 :         _outBoolean(str, (Boolean *) obj);
     733    63040430 :     else if (IsA(obj, String))
     734    22780724 :         _outString(str, (String *) obj);
     735    40259706 :     else if (IsA(obj, BitString))
     736        4068 :         _outBitString(str, (BitString *) obj);
     737    40255638 :     else if (IsA(obj, Bitmapset))
     738           0 :         outBitmapset(str, (Bitmapset *) obj);
     739             :     else
     740             :     {
     741    40255638 :         appendStringInfoChar(str, '{');
     742    40255638 :         switch (nodeTag(obj))
     743             :         {
     744             : #include "outfuncs.switch.c"
     745             : 
     746           0 :             default:
     747             : 
     748             :                 /*
     749             :                  * This should be an ERROR, but it's too useful to be able to
     750             :                  * dump structures that outNode only understands part of.
     751             :                  */
     752           0 :                 elog(WARNING, "could not dump unrecognized node type: %d",
     753             :                      (int) nodeTag(obj));
     754           0 :                 break;
     755             :         }
     756    40255638 :         appendStringInfoChar(str, '}');
     757             :     }
     758   125200862 : }
     759             : 
     760             : /*
     761             :  * nodeToString -
     762             :  *     returns the ascii representation of the Node as a palloc'd string
     763             :  *
     764             :  * write_loc_fields determines whether location fields are output with their
     765             :  * actual value rather than -1.  The actual value can be useful for debugging,
     766             :  * but for most uses, the actual value is not useful, since the original query
     767             :  * string is no longer available.
     768             :  */
     769             : static char *
     770     1835848 : nodeToStringInternal(const void *obj, bool write_loc_fields)
     771             : {
     772             :     StringInfoData str;
     773             :     bool        save_write_location_fields;
     774             : 
     775     1835848 :     save_write_location_fields = write_location_fields;
     776     1835848 :     write_location_fields = write_loc_fields;
     777             : 
     778             :     /* see stringinfo.h for an explanation of this maneuver */
     779     1835848 :     initStringInfo(&str);
     780     1835848 :     outNode(&str, obj);
     781             : 
     782     1835848 :     write_location_fields = save_write_location_fields;
     783             : 
     784     1835848 :     return str.data;
     785             : }
     786             : 
     787             : /*
     788             :  * Externally visible entry points
     789             :  */
     790             : char *
     791       58064 : nodeToString(const void *obj)
     792             : {
     793       58064 :     return nodeToStringInternal(obj, false);
     794             : }
     795             : 
     796             : char *
     797     1777784 : nodeToStringWithLocations(const void *obj)
     798             : {
     799     1777784 :     return nodeToStringInternal(obj, true);
     800             : }
     801             : 
     802             : 
     803             : /*
     804             :  * bmsToString -
     805             :  *     returns the ascii representation of the Bitmapset as a palloc'd string
     806             :  */
     807             : char *
     808           0 : bmsToString(const Bitmapset *bms)
     809             : {
     810             :     StringInfoData str;
     811             : 
     812             :     /* see stringinfo.h for an explanation of this maneuver */
     813           0 :     initStringInfo(&str);
     814           0 :     outBitmapset(&str, bms);
     815           0 :     return str.data;
     816             : }

Generated by: LCOV version 1.14