1 /*-------------------------------------------------------------------------
4 * handle type operations for parser
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/parser/parse_type.c
13 *-------------------------------------------------------------------------
17 #include "catalog/namespace.h"
18 #include "catalog/pg_type.h"
19 #include "lib/stringinfo.h"
20 #include "nodes/makefuncs.h"
21 #include "parser/parser.h"
22 #include "parser/parse_type.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/datum.h"
26 #include "utils/lsyscache.h"
27 #include "utils/syscache.h"
30 static int32 typenameTypeMod(ParseState *pstate, const TypeName *typeName,
36 * Given a TypeName object, lookup the pg_type syscache entry of the type.
37 * Returns NULL if no such type can be found. If the type is found,
38 * the typmod value represented in the TypeName struct is computed and
39 * stored into *typmod_p.
41 * NB: on success, the caller must ReleaseSysCache the type tuple when done
44 * NB: direct callers of this function MUST check typisdefined before assuming
45 * that the type is fully valid. Most code should go through typenameType
46 * or typenameTypeId instead.
48 * typmod_p can be passed as NULL if the caller does not care to know the
49 * typmod value, but the typmod decoration (if any) will be validated anyway,
50 * except in the case where the type is not found. Note that if the type is
51 * found but is a shell, and there is typmod decoration, an error will be
52 * thrown --- this is intentional.
54 * pstate is only used for error location info, and may be NULL.
57 LookupTypeName(ParseState *pstate, const TypeName *typeName,
64 if (typeName->names == NIL)
66 /* We have the OID already if it's an internally generated TypeName */
67 typoid = typeName->typeOid;
69 else if (typeName->pct_type)
71 /* Handle %TYPE reference to type of an existing field */
72 RangeVar *rel = makeRangeVar(NULL, NULL, typeName->location);
77 /* deconstruct the name list */
78 switch (list_length(typeName->names))
82 (errcode(ERRCODE_SYNTAX_ERROR),
83 errmsg("improper %%TYPE reference (too few dotted names): %s",
84 NameListToString(typeName->names)),
85 parser_errposition(pstate, typeName->location)));
88 rel->relname = strVal(linitial(typeName->names));
89 field = strVal(lsecond(typeName->names));
92 rel->schemaname = strVal(linitial(typeName->names));
93 rel->relname = strVal(lsecond(typeName->names));
94 field = strVal(lthird(typeName->names));
97 rel->catalogname = strVal(linitial(typeName->names));
98 rel->schemaname = strVal(lsecond(typeName->names));
99 rel->relname = strVal(lthird(typeName->names));
100 field = strVal(lfourth(typeName->names));
104 (errcode(ERRCODE_SYNTAX_ERROR),
105 errmsg("improper %%TYPE reference (too many dotted names): %s",
106 NameListToString(typeName->names)),
107 parser_errposition(pstate, typeName->location)));
114 * XXX: As no lock is taken here, this might fail in the presence
115 * of concurrent DDL. But taking a lock would carry a performance
116 * penalty and would also require a permissions check.
118 relid = RangeVarGetRelid(rel, NoLock, false);
119 attnum = get_attnum(relid, field);
120 if (attnum == InvalidAttrNumber)
122 (errcode(ERRCODE_UNDEFINED_COLUMN),
123 errmsg("column \"%s\" of relation \"%s\" does not exist",
124 field, rel->relname),
125 parser_errposition(pstate, typeName->location)));
126 typoid = get_atttype(relid, attnum);
128 /* this construct should never have an array indicator */
129 Assert(typeName->arrayBounds == NIL);
131 /* emit nuisance notice (intentionally not errposition'd) */
133 (errmsg("type reference %s converted to %s",
134 TypeNameToString(typeName),
135 format_type_be(typoid))));
139 /* Normal reference to a type name */
143 /* deconstruct the name list */
144 DeconstructQualifiedName(typeName->names, &schemaname, &typname);
148 /* Look in specific schema only */
151 namespaceId = LookupExplicitNamespace(schemaname);
152 typoid = GetSysCacheOid2(TYPENAMENSP,
153 PointerGetDatum(typname),
154 ObjectIdGetDatum(namespaceId));
158 /* Unqualified type name, so search the search path */
159 typoid = TypenameGetTypid(typname);
162 /* If an array reference, return the array type instead */
163 if (typeName->arrayBounds != NIL)
164 typoid = get_array_type(typoid);
167 if (!OidIsValid(typoid))
174 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
175 if (!HeapTupleIsValid(tup)) /* should not happen */
176 elog(ERROR, "cache lookup failed for type %u", typoid);
178 typmod = typenameTypeMod(pstate, typeName, (Type) tup);
187 * typenameType - given a TypeName, return a Type structure and typmod
189 * This is equivalent to LookupTypeName, except that this will report
190 * a suitable error message if the type cannot be found or is not defined.
191 * Callers of this can therefore assume the result is a fully valid type.
194 typenameType(ParseState *pstate, const TypeName *typeName, int32 *typmod_p)
198 tup = LookupTypeName(pstate, typeName, typmod_p);
201 (errcode(ERRCODE_UNDEFINED_OBJECT),
202 errmsg("type \"%s\" does not exist",
203 TypeNameToString(typeName)),
204 parser_errposition(pstate, typeName->location)));
205 if (!((Form_pg_type) GETSTRUCT(tup))->typisdefined)
207 (errcode(ERRCODE_UNDEFINED_OBJECT),
208 errmsg("type \"%s\" is only a shell",
209 TypeNameToString(typeName)),
210 parser_errposition(pstate, typeName->location)));
215 * typenameTypeId - given a TypeName, return the type's OID
217 * This is similar to typenameType, but we only hand back the type OID
218 * not the syscache entry.
221 typenameTypeId(ParseState *pstate, const TypeName *typeName)
226 tup = typenameType(pstate, typeName, NULL);
227 typoid = HeapTupleGetOid(tup);
228 ReleaseSysCache(tup);
234 * typenameTypeIdAndMod - given a TypeName, return the type's OID and typmod
236 * This is equivalent to typenameType, but we only hand back the type OID
237 * and typmod, not the syscache entry.
240 typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName,
241 Oid *typeid_p, int32 *typmod_p)
245 tup = typenameType(pstate, typeName, typmod_p);
246 *typeid_p = HeapTupleGetOid(tup);
247 ReleaseSysCache(tup);
251 * typenameTypeMod - given a TypeName, return the internal typmod value
253 * This will throw an error if the TypeName includes type modifiers that are
254 * illegal for the data type.
256 * The actual type OID represented by the TypeName must already have been
257 * looked up, and is passed as "typ".
259 * pstate is only used for error location info, and may be NULL.
262 typenameTypeMod(ParseState *pstate, const TypeName *typeName, Type typ)
269 ArrayType *arrtypmod;
270 ParseCallbackState pcbstate;
272 /* Return prespecified typmod if no typmod expressions */
273 if (typeName->typmods == NIL)
274 return typeName->typemod;
277 * Else, type had better accept typmods. We give a special error message
278 * for the shell-type case, since a shell couldn't possibly have a
281 if (!((Form_pg_type) GETSTRUCT(typ))->typisdefined)
283 (errcode(ERRCODE_SYNTAX_ERROR),
284 errmsg("type modifier cannot be specified for shell type \"%s\"",
285 TypeNameToString(typeName)),
286 parser_errposition(pstate, typeName->location)));
288 typmodin = ((Form_pg_type) GETSTRUCT(typ))->typmodin;
290 if (typmodin == InvalidOid)
292 (errcode(ERRCODE_SYNTAX_ERROR),
293 errmsg("type modifier is not allowed for type \"%s\"",
294 TypeNameToString(typeName)),
295 parser_errposition(pstate, typeName->location)));
298 * Convert the list of raw-grammar-output expressions to a cstring array.
299 * Currently, we allow simple numeric constants, string literals, and
300 * identifiers; possibly this list could be extended.
302 datums = (Datum *) palloc(list_length(typeName->typmods) * sizeof(Datum));
304 foreach(l, typeName->typmods)
306 Node *tm = (Node *) lfirst(l);
309 if (IsA(tm, A_Const))
311 A_Const *ac = (A_Const *) tm;
313 if (IsA(&ac->val, Integer))
315 cstr = (char *) palloc(32);
316 snprintf(cstr, 32, "%ld", (long) ac->val.val.ival);
318 else if (IsA(&ac->val, Float) ||
319 IsA(&ac->val, String))
321 /* we can just use the str field directly. */
322 cstr = ac->val.val.str;
325 else if (IsA(tm, ColumnRef))
327 ColumnRef *cr = (ColumnRef *) tm;
329 if (list_length(cr->fields) == 1 &&
330 IsA(linitial(cr->fields), String))
331 cstr = strVal(linitial(cr->fields));
335 (errcode(ERRCODE_SYNTAX_ERROR),
336 errmsg("type modifiers must be simple constants or identifiers"),
337 parser_errposition(pstate, typeName->location)));
338 datums[n++] = CStringGetDatum(cstr);
341 /* hardwired knowledge about cstring's representation details here */
342 arrtypmod = construct_array(datums, n, CSTRINGOID,
345 /* arrange to report location if type's typmodin function fails */
346 setup_parser_errposition_callback(&pcbstate, pstate, typeName->location);
348 result = DatumGetInt32(OidFunctionCall1(typmodin,
349 PointerGetDatum(arrtypmod)));
351 cancel_parser_errposition_callback(&pcbstate);
360 * appendTypeNameToBuffer
361 * Append a string representing the name of a TypeName to a StringInfo.
362 * This is the shared guts of TypeNameToString and TypeNameListToString.
364 * NB: this must work on TypeNames that do not describe any actual type;
365 * it is mostly used for reporting lookup errors.
368 appendTypeNameToBuffer(const TypeName *typeName, StringInfo string)
370 if (typeName->names != NIL)
372 /* Emit possibly-qualified name as-is */
375 foreach(l, typeName->names)
377 if (l != list_head(typeName->names))
378 appendStringInfoChar(string, '.');
379 appendStringInfoString(string, strVal(lfirst(l)));
384 /* Look up internally-specified type */
385 appendStringInfoString(string, format_type_be(typeName->typeOid));
389 * Add decoration as needed, but only for fields considered by
392 if (typeName->pct_type)
393 appendStringInfoString(string, "%TYPE");
395 if (typeName->arrayBounds != NIL)
396 appendStringInfoString(string, "[]");
401 * Produce a string representing the name of a TypeName.
403 * NB: this must work on TypeNames that do not describe any actual type;
404 * it is mostly used for reporting lookup errors.
407 TypeNameToString(const TypeName *typeName)
409 StringInfoData string;
411 initStringInfo(&string);
412 appendTypeNameToBuffer(typeName, &string);
417 * TypeNameListToString
418 * Produce a string representing the name(s) of a List of TypeNames
421 TypeNameListToString(List *typenames)
423 StringInfoData string;
426 initStringInfo(&string);
427 foreach(l, typenames)
429 TypeName *typeName = (TypeName *) lfirst(l);
431 Assert(IsA(typeName, TypeName));
432 if (l != list_head(typenames))
433 appendStringInfoChar(&string, ',');
434 appendTypeNameToBuffer(typeName, &string);
442 * Look up collation by name, return OID, with support for error location.
445 LookupCollation(ParseState *pstate, List *collnames, int location)
448 ParseCallbackState pcbstate;
451 setup_parser_errposition_callback(&pcbstate, pstate, location);
453 colloid = get_collation_oid(collnames, false);
456 cancel_parser_errposition_callback(&pcbstate);
462 * GetColumnDefCollation
464 * Get the collation to be used for a column being defined, given the
465 * ColumnDef node and the previously-determined column type OID.
467 * pstate is only used for error location purposes, and can be NULL.
470 GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
473 Oid typcollation = get_typcollation(typeOid);
476 if (coldef->collClause)
478 /* We have a raw COLLATE clause, so look up the collation */
479 location = coldef->collClause->location;
480 result = LookupCollation(pstate, coldef->collClause->collname,
483 else if (OidIsValid(coldef->collOid))
485 /* Precooked collation spec, use that */
486 result = coldef->collOid;
490 /* Use the type's default collation if any */
491 result = typcollation;
494 /* Complain if COLLATE is applied to an uncollatable type */
495 if (OidIsValid(result) && !OidIsValid(typcollation))
497 (errcode(ERRCODE_DATATYPE_MISMATCH),
498 errmsg("collations are not supported by type %s",
499 format_type_be(typeOid)),
500 parser_errposition(pstate, location)));
505 /* return a Type structure, given a type id */
506 /* NB: caller must ReleaseSysCache the type tuple when done with it */
512 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(id));
513 if (!HeapTupleIsValid(tup))
514 elog(ERROR, "cache lookup failed for type %u", id);
518 /* given type (as type struct), return the type OID */
522 if (tp == NULL) /* probably useless */
523 elog(ERROR, "typeTypeId() called with NULL type struct");
524 return HeapTupleGetOid(tp);
527 /* given type (as type struct), return the length of type */
533 typ = (Form_pg_type) GETSTRUCT(t);
537 /* given type (as type struct), return its 'byval' attribute */
543 typ = (Form_pg_type) GETSTRUCT(t);
544 return typ->typbyval;
547 /* given type (as type struct), return the type's name */
553 typ = (Form_pg_type) GETSTRUCT(t);
554 /* pstrdup here because result may need to outlive the syscache entry */
555 return pstrdup(NameStr(typ->typname));
558 /* given type (as type struct), return its 'typrelid' attribute */
560 typeTypeRelid(Type typ)
564 typtup = (Form_pg_type) GETSTRUCT(typ);
565 return typtup->typrelid;
568 /* given type (as type struct), return its 'typcollation' attribute */
570 typeTypeCollation(Type typ)
574 typtup = (Form_pg_type) GETSTRUCT(typ);
575 return typtup->typcollation;
579 * Given a type structure and a string, returns the internal representation
580 * of that string. The "string" can be NULL to perform conversion of a NULL
581 * (which might result in failure, if the input function rejects NULLs).
584 stringTypeDatum(Type tp, char *string, int32 atttypmod)
586 Form_pg_type typform = (Form_pg_type) GETSTRUCT(tp);
587 Oid typinput = typform->typinput;
588 Oid typioparam = getTypeIOParam(tp);
591 result = OidInputFunctionCall(typinput, string,
592 typioparam, atttypmod);
594 #ifdef RANDOMIZE_ALLOCATED_MEMORY
597 * For pass-by-reference data types, repeat the conversion to see if the
598 * input function leaves any uninitialized bytes in the result. We can
599 * only detect that reliably if RANDOMIZE_ALLOCATED_MEMORY is enabled, so
600 * we don't bother testing otherwise. The reason we don't want any
601 * instability in the input function is that comparison of Const nodes
602 * relies on bytewise comparison of the datums, so if the input function
603 * leaves garbage then subexpressions that should be identical may not get
604 * recognized as such. See pgsql-hackers discussion of 2008-04-04.
606 if (string && !typform->typbyval)
610 result2 = OidInputFunctionCall(typinput, string,
611 typioparam, atttypmod);
612 if (!datumIsEqual(result, result2, typform->typbyval, typform->typlen))
613 elog(WARNING, "type %s has unstable input conversion for \"%s\"",
614 NameStr(typform->typname), string);
621 /* given a typeid, return the type's typrelid (associated relation, if any) */
623 typeidTypeRelid(Oid type_id)
629 typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
630 if (!HeapTupleIsValid(typeTuple))
631 elog(ERROR, "cache lookup failed for type %u", type_id);
633 type = (Form_pg_type) GETSTRUCT(typeTuple);
634 result = type->typrelid;
635 ReleaseSysCache(typeTuple);
640 * error context callback for parse failure during parseTypeString()
643 pts_error_callback(void *arg)
645 const char *str = (const char *) arg;
647 errcontext("invalid type name \"%s\"", str);
650 * Currently we just suppress any syntax error position report, rather
651 * than transforming to an "internal query" error. It's unlikely that a
652 * type name is complex enough to need positioning.
658 * Given a string that is supposed to be a SQL-compatible type declaration,
659 * such as "int4" or "integer" or "character varying(32)", parse
660 * the string and convert it to a type OID and type modifier.
663 parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p)
666 List *raw_parsetree_list;
668 ResTarget *restarget;
671 ErrorContextCallback ptserrcontext;
673 /* make sure we give useful error for empty input */
674 if (strspn(str, " \t\n\r\f") == strlen(str))
677 initStringInfo(&buf);
678 appendStringInfo(&buf, "SELECT NULL::%s", str);
681 * Setup error traceback support in case of ereport() during parse
683 ptserrcontext.callback = pts_error_callback;
684 ptserrcontext.arg = (void *) str;
685 ptserrcontext.previous = error_context_stack;
686 error_context_stack = &ptserrcontext;
688 raw_parsetree_list = raw_parser(buf.data);
690 error_context_stack = ptserrcontext.previous;
693 * Make sure we got back exactly what we expected and no more; paranoia is
694 * justified since the string might contain anything.
696 if (list_length(raw_parsetree_list) != 1)
698 stmt = (SelectStmt *) linitial(raw_parsetree_list);
700 !IsA(stmt, SelectStmt) ||
701 stmt->distinctClause != NIL ||
702 stmt->intoClause != NULL ||
703 stmt->fromClause != NIL ||
704 stmt->whereClause != NULL ||
705 stmt->groupClause != NIL ||
706 stmt->havingClause != NULL ||
707 stmt->windowClause != NIL ||
708 stmt->withClause != NULL ||
709 stmt->valuesLists != NIL ||
710 stmt->sortClause != NIL ||
711 stmt->limitOffset != NULL ||
712 stmt->limitCount != NULL ||
713 stmt->lockingClause != NIL ||
714 stmt->op != SETOP_NONE)
716 if (list_length(stmt->targetList) != 1)
718 restarget = (ResTarget *) linitial(stmt->targetList);
719 if (restarget == NULL ||
720 !IsA(restarget, ResTarget) ||
721 restarget->name != NULL ||
722 restarget->indirection != NIL)
724 typecast = (TypeCast *) restarget->val;
725 if (typecast == NULL ||
726 !IsA(typecast, TypeCast) ||
727 typecast->arg == NULL ||
728 !IsA(typecast->arg, A_Const))
730 typeName = typecast->typeName;
731 if (typeName == NULL ||
732 !IsA(typeName, TypeName))
737 typenameTypeIdAndMod(NULL, typeName, typeid_p, typmod_p);
745 (errcode(ERRCODE_SYNTAX_ERROR),
746 errmsg("invalid type name \"%s\"", str)));