1 /*-------------------------------------------------------------------------
4 * various routines that make nodes for querytrees
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
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/syscache.h"
29 #include "utils/varbit.h"
32 static void pcb_error_callback(void *arg
);
37 * Allocate and initialize a new ParseState.
39 * Caller should eventually release the ParseState via free_parsestate().
42 make_parsestate(ParseState
*parentParseState
)
46 pstate
= palloc0(sizeof(ParseState
));
48 pstate
->parentParseState
= parentParseState
;
50 /* Fill in fields that don't start at null/false/zero */
51 pstate
->p_next_resno
= 1;
55 pstate
->p_sourcetext
= parentParseState
->p_sourcetext
;
56 pstate
->p_variableparams
= parentParseState
->p_variableparams
;
64 * Release a ParseState and any subsidiary resources.
67 free_parsestate(ParseState
*pstate
)
70 * Check that we did not produce too many resnos; at the very least we
71 * cannot allow more than 2^16, since that would exceed the range of a
72 * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
74 if (pstate
->p_next_resno
- 1 > MaxTupleAttributeNumber
)
76 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
77 errmsg("target lists can have at most %d entries",
78 MaxTupleAttributeNumber
)));
80 if (pstate
->p_target_relation
!= NULL
)
81 heap_close(pstate
->p_target_relation
, NoLock
);
89 * Report a parse-analysis-time cursor position, if possible.
91 * This is expected to be used within an ereport() call. The return value
92 * is a dummy (always 0, in fact).
94 * The locations stored in raw parsetrees are byte offsets into the source
95 * string. We have to convert them to 1-based character indexes for reporting
96 * to clients. (We do things this way to avoid unnecessary overhead in the
97 * normal non-error case: computing character indexes would be much more
98 * expensive than storing token offsets.)
101 parser_errposition(ParseState
*pstate
, int location
)
105 /* No-op if location was not provided */
108 /* Can't do anything if source text is not available */
109 if (pstate
== NULL
|| pstate
->p_sourcetext
== NULL
)
111 /* Convert offset to character number */
112 pos
= pg_mbstrlen_with_len(pstate
->p_sourcetext
, location
) + 1;
113 /* And pass it to the ereport mechanism */
114 return errposition(pos
);
119 * setup_parser_errposition_callback
120 * Arrange for non-parser errors to report an error position
122 * Sometimes the parser calls functions that aren't part of the parser
123 * subsystem and can't reasonably be passed a ParseState; yet we would
124 * like any errors thrown in those functions to be tagged with a parse
125 * error location. Use this function to set up an error context stack
126 * entry that will accomplish that. Usage pattern:
128 * declare a local variable "ParseCallbackState pcbstate"
130 * setup_parser_errposition_callback(&pcbstate, pstate, location);
131 * call function that might throw error;
132 * cancel_parser_errposition_callback(&pcbstate);
135 setup_parser_errposition_callback(ParseCallbackState
*pcbstate
,
136 ParseState
*pstate
, int location
)
138 /* Setup error traceback support for ereport() */
139 pcbstate
->pstate
= pstate
;
140 pcbstate
->location
= location
;
141 pcbstate
->errcontext
.callback
= pcb_error_callback
;
142 pcbstate
->errcontext
.arg
= (void *) pcbstate
;
143 pcbstate
->errcontext
.previous
= error_context_stack
;
144 error_context_stack
= &pcbstate
->errcontext
;
148 * Cancel a previously-set-up errposition callback.
151 cancel_parser_errposition_callback(ParseCallbackState
*pcbstate
)
153 /* Pop the error context stack */
154 error_context_stack
= pcbstate
->errcontext
.previous
;
158 * Error context callback for inserting parser error location.
160 * Note that this will be called for *any* error occurring while the
161 * callback is installed. We avoid inserting an irrelevant error location
162 * if the error is a query cancel --- are there any other important cases?
165 pcb_error_callback(void *arg
)
167 ParseCallbackState
*pcbstate
= (ParseCallbackState
*) arg
;
169 if (geterrcode() != ERRCODE_QUERY_CANCELED
)
170 (void) parser_errposition(pcbstate
->pstate
, pcbstate
->location
);
176 * Build a Var node for an attribute identified by RTE and attrno
179 make_var(ParseState
*pstate
, RangeTblEntry
*rte
, int attrno
, int location
)
187 vnum
= RTERangeTablePosn(pstate
, rte
, &sublevels_up
);
188 get_rte_attribute_type(rte
, attrno
, &vartypeid
, &type_mod
);
189 result
= makeVar(vnum
, attrno
, vartypeid
, type_mod
, sublevels_up
);
190 result
->location
= location
;
195 * transformArrayType()
196 * Get the element type of an array type in preparation for subscripting
199 transformArrayType(Oid arrayType
)
202 HeapTuple type_tuple_array
;
203 Form_pg_type type_struct_array
;
205 /* Get the type tuple for the array */
206 type_tuple_array
= SearchSysCache(TYPEOID
,
207 ObjectIdGetDatum(arrayType
),
209 if (!HeapTupleIsValid(type_tuple_array
))
210 elog(ERROR
, "cache lookup failed for type %u", arrayType
);
211 type_struct_array
= (Form_pg_type
) GETSTRUCT(type_tuple_array
);
213 /* needn't check typisdefined since this will fail anyway */
215 elementType
= type_struct_array
->typelem
;
216 if (elementType
== InvalidOid
)
218 (errcode(ERRCODE_DATATYPE_MISMATCH
),
219 errmsg("cannot subscript type %s because it is not an array",
220 format_type_be(arrayType
))));
222 ReleaseSysCache(type_tuple_array
);
228 * transformArraySubscripts()
229 * Transform array subscripting. This is used for both
230 * array fetch and array assignment.
232 * In an array fetch, we are given a source array value and we produce an
233 * expression that represents the result of extracting a single array element
236 * In an array assignment, we are given a destination array value plus a
237 * source value that is to be assigned to a single element or a slice of
238 * that array. We produce an expression that represents the new array value
239 * with the source data inserted into the right part of the array.
242 * arrayBase Already-transformed expression for the array as a whole
243 * arrayType OID of array's datatype (should match type of arrayBase)
244 * elementType OID of array's element type (fetch with transformArrayType,
245 * or pass InvalidOid to do it here)
246 * elementTypMod typmod to be applied to array elements (if storing) or of
247 * the source array (if fetching)
248 * indirection Untransformed list of subscripts (must not be NIL)
249 * assignFrom NULL for array fetch, else transformed expression for source.
252 transformArraySubscripts(ParseState
*pstate
,
260 bool isSlice
= false;
261 List
*upperIndexpr
= NIL
;
262 List
*lowerIndexpr
= NIL
;
266 /* Caller may or may not have bothered to determine elementType */
267 if (!OidIsValid(elementType
))
268 elementType
= transformArrayType(arrayType
);
271 * A list containing only single subscripts refers to a single array
272 * element. If any of the items are double subscripts (lower:upper), then
273 * the subscript expression means an array slice operation. In this case,
274 * we supply a default lower bound of 1 for any items that contain only a
275 * single subscript. We have to prescan the indirection list to see if
276 * there are any double subscripts.
278 foreach(idx
, indirection
)
280 A_Indices
*ai
= (A_Indices
*) lfirst(idx
);
282 if (ai
->lidx
!= NULL
)
290 * Transform the subscript expressions.
292 foreach(idx
, indirection
)
294 A_Indices
*ai
= (A_Indices
*) lfirst(idx
);
297 Assert(IsA(ai
, A_Indices
));
302 subexpr
= transformExpr(pstate
, ai
->lidx
);
303 /* If it's not int4 already, try to coerce */
304 subexpr
= coerce_to_target_type(pstate
,
305 subexpr
, exprType(subexpr
),
308 COERCE_IMPLICIT_CAST
,
312 (errcode(ERRCODE_DATATYPE_MISMATCH
),
313 errmsg("array subscript must have type integer"),
314 parser_errposition(pstate
, exprLocation(ai
->lidx
))));
318 /* Make a constant 1 */
319 subexpr
= (Node
*) makeConst(INT4OID
,
324 true); /* pass by value */
326 lowerIndexpr
= lappend(lowerIndexpr
, subexpr
);
328 subexpr
= transformExpr(pstate
, ai
->uidx
);
329 /* If it's not int4 already, try to coerce */
330 subexpr
= coerce_to_target_type(pstate
,
331 subexpr
, exprType(subexpr
),
334 COERCE_IMPLICIT_CAST
,
338 (errcode(ERRCODE_DATATYPE_MISMATCH
),
339 errmsg("array subscript must have type integer"),
340 parser_errposition(pstate
, exprLocation(ai
->uidx
))));
341 upperIndexpr
= lappend(upperIndexpr
, subexpr
);
345 * If doing an array store, coerce the source value to the right type.
346 * (This should agree with the coercion done by transformAssignedExpr.)
348 if (assignFrom
!= NULL
)
350 Oid typesource
= exprType(assignFrom
);
351 Oid typeneeded
= isSlice
? arrayType
: elementType
;
354 newFrom
= coerce_to_target_type(pstate
,
355 assignFrom
, typesource
,
356 typeneeded
, elementTypMod
,
358 COERCE_IMPLICIT_CAST
,
362 (errcode(ERRCODE_DATATYPE_MISMATCH
),
363 errmsg("array assignment requires type %s"
364 " but expression is of type %s",
365 format_type_be(typeneeded
),
366 format_type_be(typesource
)),
367 errhint("You will need to rewrite or cast the expression."),
368 parser_errposition(pstate
, exprLocation(assignFrom
))));
369 assignFrom
= newFrom
;
373 * Ready to build the ArrayRef node.
375 aref
= makeNode(ArrayRef
);
376 aref
->refarraytype
= arrayType
;
377 aref
->refelemtype
= elementType
;
378 aref
->reftypmod
= elementTypMod
;
379 aref
->refupperindexpr
= upperIndexpr
;
380 aref
->reflowerindexpr
= lowerIndexpr
;
381 aref
->refexpr
= (Expr
*) arrayBase
;
382 aref
->refassgnexpr
= (Expr
*) assignFrom
;
390 * Convert a Value node (as returned by the grammar) to a Const node
391 * of the "natural" type for the constant. Note that this routine is
392 * only used when there is no explicit cast for the constant, so we
393 * have to guess what type is wanted.
395 * For string literals we produce a constant of type UNKNOWN ---- whose
396 * representation is the same as cstring, but it indicates to later type
397 * resolution that we're not sure yet what type it should be considered.
398 * Explicit "NULL" constants are also typed as UNKNOWN.
400 * For integers and floats we produce int4, int8, or numeric depending
401 * on the value of the number. XXX We should produce int2 as well,
402 * but additional cleanup is needed before we can do that; there are
403 * too many examples that fail if we try.
406 make_const(ParseState
*pstate
, Value
*value
, int location
)
414 ParseCallbackState pcbstate
;
416 switch (nodeTag(value
))
419 val
= Int32GetDatum(intVal(value
));
422 typelen
= sizeof(int32
);
427 /* could be an oversize integer as well as a float ... */
428 if (scanint8(strVal(value
), true, &val64
))
431 * It might actually fit in int32. Probably only INT_MIN can
432 * occur, but we'll code the test generally just to be sure.
434 int32 val32
= (int32
) val64
;
436 if (val64
== (int64
) val32
)
438 val
= Int32GetDatum(val32
);
441 typelen
= sizeof(int32
);
446 val
= Int64GetDatum(val64
);
449 typelen
= sizeof(int64
);
450 typebyval
= FLOAT8PASSBYVAL
; /* int8 and float8 alike */
455 /* arrange to report location if numeric_in() fails */
456 setup_parser_errposition_callback(&pcbstate
, pstate
, location
);
457 val
= DirectFunctionCall3(numeric_in
,
458 CStringGetDatum(strVal(value
)),
459 ObjectIdGetDatum(InvalidOid
),
461 cancel_parser_errposition_callback(&pcbstate
);
464 typelen
= -1; /* variable len */
472 * We assume here that UNKNOWN's internal representation is the
475 val
= CStringGetDatum(strVal(value
));
477 typeid = UNKNOWNOID
; /* will be coerced later */
478 typelen
= -2; /* cstring-style varwidth type */
483 /* arrange to report location if bit_in() fails */
484 setup_parser_errposition_callback(&pcbstate
, pstate
, location
);
485 val
= DirectFunctionCall3(bit_in
,
486 CStringGetDatum(strVal(value
)),
487 ObjectIdGetDatum(InvalidOid
),
489 cancel_parser_errposition_callback(&pcbstate
);
496 /* return a null const */
497 con
= makeConst(UNKNOWNOID
,
503 con
->location
= location
;
507 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(value
));
508 return NULL
; /* keep compiler quiet */
511 con
= makeConst(typeid,
512 -1, /* typmod -1 is OK for all cases */
517 con
->location
= location
;