]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_node.c
Reduce hash size for compute_array_stats, compute_tsvector_stats.
[postgresql] / src / backend / parser / parse_node.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_node.c
4  *        various routines that make nodes for querytrees
5  *
6  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/parser/parse_node.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "catalog/pg_type.h"
19 #include "mb/pg_wchar.h"
20 #include "nodes/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "parser/parsetree.h"
23 #include "parser/parse_coerce.h"
24 #include "parser/parse_expr.h"
25 #include "parser/parse_relation.h"
26 #include "utils/builtins.h"
27 #include "utils/int8.h"
28 #include "utils/lsyscache.h"
29 #include "utils/syscache.h"
30 #include "utils/varbit.h"
31
32
33 static void pcb_error_callback(void *arg);
34
35
36 /*
37  * make_parsestate
38  *              Allocate and initialize a new ParseState.
39  *
40  * Caller should eventually release the ParseState via free_parsestate().
41  */
42 ParseState *
43 make_parsestate(ParseState *parentParseState)
44 {
45         ParseState *pstate;
46
47         pstate = palloc0(sizeof(ParseState));
48
49         pstate->parentParseState = parentParseState;
50
51         /* Fill in fields that don't start at null/false/zero */
52         pstate->p_next_resno = 1;
53
54         if (parentParseState)
55         {
56                 pstate->p_sourcetext = parentParseState->p_sourcetext;
57                 /* all hooks are copied from parent */
58                 pstate->p_pre_columnref_hook = parentParseState->p_pre_columnref_hook;
59                 pstate->p_post_columnref_hook = parentParseState->p_post_columnref_hook;
60                 pstate->p_paramref_hook = parentParseState->p_paramref_hook;
61                 pstate->p_coerce_param_hook = parentParseState->p_coerce_param_hook;
62                 pstate->p_ref_hook_state = parentParseState->p_ref_hook_state;
63         }
64
65         return pstate;
66 }
67
68 /*
69  * free_parsestate
70  *              Release a ParseState and any subsidiary resources.
71  */
72 void
73 free_parsestate(ParseState *pstate)
74 {
75         /*
76          * Check that we did not produce too many resnos; at the very least we
77          * cannot allow more than 2^16, since that would exceed the range of a
78          * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
79          */
80         if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
81                 ereport(ERROR,
82                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
83                                  errmsg("target lists can have at most %d entries",
84                                                 MaxTupleAttributeNumber)));
85
86         if (pstate->p_target_relation != NULL)
87                 heap_close(pstate->p_target_relation, NoLock);
88
89         pfree(pstate);
90 }
91
92
93 /*
94  * parser_errposition
95  *              Report a parse-analysis-time cursor position, if possible.
96  *
97  * This is expected to be used within an ereport() call.  The return value
98  * is a dummy (always 0, in fact).
99  *
100  * The locations stored in raw parsetrees are byte offsets into the source
101  * string.      We have to convert them to 1-based character indexes for reporting
102  * to clients.  (We do things this way to avoid unnecessary overhead in the
103  * normal non-error case: computing character indexes would be much more
104  * expensive than storing token offsets.)
105  */
106 int
107 parser_errposition(ParseState *pstate, int location)
108 {
109         int                     pos;
110
111         /* No-op if location was not provided */
112         if (location < 0)
113                 return 0;
114         /* Can't do anything if source text is not available */
115         if (pstate == NULL || pstate->p_sourcetext == NULL)
116                 return 0;
117         /* Convert offset to character number */
118         pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
119         /* And pass it to the ereport mechanism */
120         return errposition(pos);
121 }
122
123
124 /*
125  * setup_parser_errposition_callback
126  *              Arrange for non-parser errors to report an error position
127  *
128  * Sometimes the parser calls functions that aren't part of the parser
129  * subsystem and can't reasonably be passed a ParseState; yet we would
130  * like any errors thrown in those functions to be tagged with a parse
131  * error location.      Use this function to set up an error context stack
132  * entry that will accomplish that.  Usage pattern:
133  *
134  *              declare a local variable "ParseCallbackState pcbstate"
135  *              ...
136  *              setup_parser_errposition_callback(&pcbstate, pstate, location);
137  *              call function that might throw error;
138  *              cancel_parser_errposition_callback(&pcbstate);
139  */
140 void
141 setup_parser_errposition_callback(ParseCallbackState *pcbstate,
142                                                                   ParseState *pstate, int location)
143 {
144         /* Setup error traceback support for ereport() */
145         pcbstate->pstate = pstate;
146         pcbstate->location = location;
147         pcbstate->errcontext.callback = pcb_error_callback;
148         pcbstate->errcontext.arg = (void *) pcbstate;
149         pcbstate->errcontext.previous = error_context_stack;
150         error_context_stack = &pcbstate->errcontext;
151 }
152
153 /*
154  * Cancel a previously-set-up errposition callback.
155  */
156 void
157 cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
158 {
159         /* Pop the error context stack */
160         error_context_stack = pcbstate->errcontext.previous;
161 }
162
163 /*
164  * Error context callback for inserting parser error location.
165  *
166  * Note that this will be called for *any* error occurring while the
167  * callback is installed.  We avoid inserting an irrelevant error location
168  * if the error is a query cancel --- are there any other important cases?
169  */
170 static void
171 pcb_error_callback(void *arg)
172 {
173         ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
174
175         if (geterrcode() != ERRCODE_QUERY_CANCELED)
176                 (void) parser_errposition(pcbstate->pstate, pcbstate->location);
177 }
178
179
180 /*
181  * make_var
182  *              Build a Var node for an attribute identified by RTE and attrno
183  */
184 Var *
185 make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
186 {
187         Var                *result;
188         int                     vnum,
189                                 sublevels_up;
190         Oid                     vartypeid;
191         int32           type_mod;
192         Oid                     varcollid;
193
194         vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
195         get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod, &varcollid);
196         result = makeVar(vnum, attrno, vartypeid, type_mod, varcollid, sublevels_up);
197         result->location = location;
198         return result;
199 }
200
201 /*
202  * transformArrayType()
203  *              Identify the types involved in a subscripting operation
204  *
205  * On entry, arrayType/arrayTypmod identify the type of the input value
206  * to be subscripted (which could be a domain type).  These are modified
207  * if necessary to identify the actual array type and typmod, and the
208  * array's element type is returned.  An error is thrown if the input isn't
209  * an array type.
210  */
211 Oid
212 transformArrayType(Oid *arrayType, int32 *arrayTypmod)
213 {
214         Oid                     origArrayType = *arrayType;
215         Oid                     elementType;
216         HeapTuple       type_tuple_array;
217         Form_pg_type type_struct_array;
218
219         /*
220          * If the input is a domain, smash to base type, and extract the actual
221          * typmod to be applied to the base type.  Subscripting a domain is an
222          * operation that necessarily works on the base array type, not the domain
223          * itself.      (Note that we provide no method whereby the creator of a
224          * domain over an array type could hide its ability to be subscripted.)
225          */
226         *arrayType = getBaseTypeAndTypmod(*arrayType, arrayTypmod);
227
228         /* Get the type tuple for the array */
229         type_tuple_array = SearchSysCache1(TYPEOID, ObjectIdGetDatum(*arrayType));
230         if (!HeapTupleIsValid(type_tuple_array))
231                 elog(ERROR, "cache lookup failed for type %u", *arrayType);
232         type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
233
234         /* needn't check typisdefined since this will fail anyway */
235
236         elementType = type_struct_array->typelem;
237         if (elementType == InvalidOid)
238                 ereport(ERROR,
239                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
240                                  errmsg("cannot subscript type %s because it is not an array",
241                                                 format_type_be(origArrayType))));
242
243         ReleaseSysCache(type_tuple_array);
244
245         return elementType;
246 }
247
248 /*
249  * transformArraySubscripts()
250  *              Transform array subscripting.  This is used for both
251  *              array fetch and array assignment.
252  *
253  * In an array fetch, we are given a source array value and we produce an
254  * expression that represents the result of extracting a single array element
255  * or an array slice.
256  *
257  * In an array assignment, we are given a destination array value plus a
258  * source value that is to be assigned to a single element or a slice of
259  * that array.  We produce an expression that represents the new array value
260  * with the source data inserted into the right part of the array.
261  *
262  * For both cases, if the source array is of a domain-over-array type,
263  * the result is of the base array type or its element type; essentially,
264  * we must fold a domain to its base type before applying subscripting.
265  *
266  * pstate               Parse state
267  * arrayBase    Already-transformed expression for the array as a whole
268  * arrayType    OID of array's datatype (should match type of arrayBase,
269  *                              or be the base type of arrayBase's domain type)
270  * elementType  OID of array's element type (fetch with transformArrayType,
271  *                              or pass InvalidOid to do it here)
272  * arrayTypMod  typmod for the array (which is also typmod for the elements)
273  * indirection  Untransformed list of subscripts (must not be NIL)
274  * assignFrom   NULL for array fetch, else transformed expression for source.
275  */
276 ArrayRef *
277 transformArraySubscripts(ParseState *pstate,
278                                                  Node *arrayBase,
279                                                  Oid arrayType,
280                                                  Oid elementType,
281                                                  int32 arrayTypMod,
282                                                  List *indirection,
283                                                  Node *assignFrom)
284 {
285         bool            isSlice = false;
286         List       *upperIndexpr = NIL;
287         List       *lowerIndexpr = NIL;
288         ListCell   *idx;
289         ArrayRef   *aref;
290
291         /*
292          * Caller may or may not have bothered to determine elementType.  Note
293          * that if the caller did do so, arrayType/arrayTypMod must be as modified
294          * by transformArrayType, ie, smash domain to base type.
295          */
296         if (!OidIsValid(elementType))
297                 elementType = transformArrayType(&arrayType, &arrayTypMod);
298
299         /*
300          * A list containing only single subscripts refers to a single array
301          * element.  If any of the items are double subscripts (lower:upper), then
302          * the subscript expression means an array slice operation. In this case,
303          * we supply a default lower bound of 1 for any items that contain only a
304          * single subscript.  We have to prescan the indirection list to see if
305          * there are any double subscripts.
306          */
307         foreach(idx, indirection)
308         {
309                 A_Indices  *ai = (A_Indices *) lfirst(idx);
310
311                 if (ai->lidx != NULL)
312                 {
313                         isSlice = true;
314                         break;
315                 }
316         }
317
318         /*
319          * Transform the subscript expressions.
320          */
321         foreach(idx, indirection)
322         {
323                 A_Indices  *ai = (A_Indices *) lfirst(idx);
324                 Node       *subexpr;
325
326                 Assert(IsA(ai, A_Indices));
327                 if (isSlice)
328                 {
329                         if (ai->lidx)
330                         {
331                                 subexpr = transformExpr(pstate, ai->lidx);
332                                 /* If it's not int4 already, try to coerce */
333                                 subexpr = coerce_to_target_type(pstate,
334                                                                                                 subexpr, exprType(subexpr),
335                                                                                                 INT4OID, -1,
336                                                                                                 COERCION_ASSIGNMENT,
337                                                                                                 COERCE_IMPLICIT_CAST,
338                                                                                                 -1);
339                                 if (subexpr == NULL)
340                                         ereport(ERROR,
341                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
342                                                          errmsg("array subscript must have type integer"),
343                                                 parser_errposition(pstate, exprLocation(ai->lidx))));
344                         }
345                         else
346                         {
347                                 /* Make a constant 1 */
348                                 subexpr = (Node *) makeConst(INT4OID,
349                                                                                          -1,
350                                                                                          InvalidOid,
351                                                                                          sizeof(int32),
352                                                                                          Int32GetDatum(1),
353                                                                                          false,
354                                                                                          true);         /* pass by value */
355                         }
356                         lowerIndexpr = lappend(lowerIndexpr, subexpr);
357                 }
358                 subexpr = transformExpr(pstate, ai->uidx);
359                 /* If it's not int4 already, try to coerce */
360                 subexpr = coerce_to_target_type(pstate,
361                                                                                 subexpr, exprType(subexpr),
362                                                                                 INT4OID, -1,
363                                                                                 COERCION_ASSIGNMENT,
364                                                                                 COERCE_IMPLICIT_CAST,
365                                                                                 -1);
366                 if (subexpr == NULL)
367                         ereport(ERROR,
368                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
369                                          errmsg("array subscript must have type integer"),
370                                          parser_errposition(pstate, exprLocation(ai->uidx))));
371                 upperIndexpr = lappend(upperIndexpr, subexpr);
372         }
373
374         /*
375          * If doing an array store, coerce the source value to the right type.
376          * (This should agree with the coercion done by transformAssignedExpr.)
377          */
378         if (assignFrom != NULL)
379         {
380                 Oid                     typesource = exprType(assignFrom);
381                 Oid                     typeneeded = isSlice ? arrayType : elementType;
382                 Node       *newFrom;
383
384                 newFrom = coerce_to_target_type(pstate,
385                                                                                 assignFrom, typesource,
386                                                                                 typeneeded, arrayTypMod,
387                                                                                 COERCION_ASSIGNMENT,
388                                                                                 COERCE_IMPLICIT_CAST,
389                                                                                 -1);
390                 if (newFrom == NULL)
391                         ereport(ERROR,
392                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
393                                          errmsg("array assignment requires type %s"
394                                                         " but expression is of type %s",
395                                                         format_type_be(typeneeded),
396                                                         format_type_be(typesource)),
397                                  errhint("You will need to rewrite or cast the expression."),
398                                          parser_errposition(pstate, exprLocation(assignFrom))));
399                 assignFrom = newFrom;
400         }
401
402         /*
403          * Ready to build the ArrayRef node.
404          */
405         aref = makeNode(ArrayRef);
406         aref->refarraytype = arrayType;
407         aref->refelemtype = elementType;
408         aref->reftypmod = arrayTypMod;
409         /* refcollid will be set by parse_collate.c */
410         aref->refupperindexpr = upperIndexpr;
411         aref->reflowerindexpr = lowerIndexpr;
412         aref->refexpr = (Expr *) arrayBase;
413         aref->refassgnexpr = (Expr *) assignFrom;
414
415         return aref;
416 }
417
418 /*
419  * make_const
420  *
421  *      Convert a Value node (as returned by the grammar) to a Const node
422  *      of the "natural" type for the constant.  Note that this routine is
423  *      only used when there is no explicit cast for the constant, so we
424  *      have to guess what type is wanted.
425  *
426  *      For string literals we produce a constant of type UNKNOWN ---- whose
427  *      representation is the same as cstring, but it indicates to later type
428  *      resolution that we're not sure yet what type it should be considered.
429  *      Explicit "NULL" constants are also typed as UNKNOWN.
430  *
431  *      For integers and floats we produce int4, int8, or numeric depending
432  *      on the value of the number.  XXX We should produce int2 as well,
433  *      but additional cleanup is needed before we can do that; there are
434  *      too many examples that fail if we try.
435  */
436 Const *
437 make_const(ParseState *pstate, Value *value, int location)
438 {
439         Const      *con;
440         Datum           val;
441         int64           val64;
442         Oid                     typeid;
443         int                     typelen;
444         bool            typebyval;
445         ParseCallbackState pcbstate;
446
447         switch (nodeTag(value))
448         {
449                 case T_Integer:
450                         val = Int32GetDatum(intVal(value));
451
452                         typeid = INT4OID;
453                         typelen = sizeof(int32);
454                         typebyval = true;
455                         break;
456
457                 case T_Float:
458                         /* could be an oversize integer as well as a float ... */
459                         if (scanint8(strVal(value), true, &val64))
460                         {
461                                 /*
462                                  * It might actually fit in int32. Probably only INT_MIN can
463                                  * occur, but we'll code the test generally just to be sure.
464                                  */
465                                 int32           val32 = (int32) val64;
466
467                                 if (val64 == (int64) val32)
468                                 {
469                                         val = Int32GetDatum(val32);
470
471                                         typeid = INT4OID;
472                                         typelen = sizeof(int32);
473                                         typebyval = true;
474                                 }
475                                 else
476                                 {
477                                         val = Int64GetDatum(val64);
478
479                                         typeid = INT8OID;
480                                         typelen = sizeof(int64);
481                                         typebyval = FLOAT8PASSBYVAL;            /* int8 and float8 alike */
482                                 }
483                         }
484                         else
485                         {
486                                 /* arrange to report location if numeric_in() fails */
487                                 setup_parser_errposition_callback(&pcbstate, pstate, location);
488                                 val = DirectFunctionCall3(numeric_in,
489                                                                                   CStringGetDatum(strVal(value)),
490                                                                                   ObjectIdGetDatum(InvalidOid),
491                                                                                   Int32GetDatum(-1));
492                                 cancel_parser_errposition_callback(&pcbstate);
493
494                                 typeid = NUMERICOID;
495                                 typelen = -1;   /* variable len */
496                                 typebyval = false;
497                         }
498                         break;
499
500                 case T_String:
501
502                         /*
503                          * We assume here that UNKNOWN's internal representation is the
504                          * same as CSTRING
505                          */
506                         val = CStringGetDatum(strVal(value));
507
508                         typeid = UNKNOWNOID;    /* will be coerced later */
509                         typelen = -2;           /* cstring-style varwidth type */
510                         typebyval = false;
511                         break;
512
513                 case T_BitString:
514                         /* arrange to report location if bit_in() fails */
515                         setup_parser_errposition_callback(&pcbstate, pstate, location);
516                         val = DirectFunctionCall3(bit_in,
517                                                                           CStringGetDatum(strVal(value)),
518                                                                           ObjectIdGetDatum(InvalidOid),
519                                                                           Int32GetDatum(-1));
520                         cancel_parser_errposition_callback(&pcbstate);
521                         typeid = BITOID;
522                         typelen = -1;
523                         typebyval = false;
524                         break;
525
526                 case T_Null:
527                         /* return a null const */
528                         con = makeConst(UNKNOWNOID,
529                                                         -1,
530                                                         InvalidOid,
531                                                         -2,
532                                                         (Datum) 0,
533                                                         true,
534                                                         false);
535                         con->location = location;
536                         return con;
537
538                 default:
539                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
540                         return NULL;            /* keep compiler quiet */
541         }
542
543         con = makeConst(typeid,
544                                         -1,                     /* typmod -1 is OK for all cases */
545                                         InvalidOid, /* all cases are uncollatable types */
546                                         typelen,
547                                         val,
548                                         false,
549                                         typebyval);
550         con->location = location;
551
552         return con;
553 }