* parse_coerce.c
* handle type coercions/conversions for parser
*
- * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.111 2003/09/29 00:05:25 petere Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.136 2006/04/04 19:35:34 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "nodes/makefuncs.h"
#include "nodes/params.h"
#include "optimizer/clauses.h"
+#include "parser/parsetree.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
+#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
+#include "utils/typcache.h"
static Node *coerce_type_typmod(Node *node,
Oid targetTypeId, int32 targetTypMod,
- CoercionForm cformat, bool isExplicit);
+ CoercionForm cformat, bool isExplicit,
+ bool hideInputCoercion);
+static void hide_coercion_node(Node *node);
+static Node *build_coercion_expression(Node *node, Oid funcId,
+ Oid targetTypeId, int32 targetTypMod,
+ CoercionForm cformat, bool isExplicit);
+static Node *coerce_record_to_complex(ParseState *pstate, Node *node,
+ Oid targetTypeId,
+ CoercionContext ccontext,
+ CoercionForm cformat);
/*
CoercionContext ccontext,
CoercionForm cformat)
{
- if (can_coerce_type(1, &exprtype, &targettype, ccontext))
- expr = coerce_type(pstate, expr, exprtype, targettype,
- ccontext, cformat);
- else if (ccontext >= COERCION_ASSIGNMENT)
- {
- /*
- * String hacks to get transparent conversions for char and
- * varchar: if a coercion to text is available, use it for forced
- * coercions to char(n) or varchar(n) or domains thereof.
- *
- * This is pretty grotty, but seems easier to maintain than providing
- * entries in pg_cast that parallel all the ones for text.
- */
- Oid targetbasetype = getBaseType(targettype);
+ Node *result;
- if (targetbasetype == BPCHAROID || targetbasetype == VARCHAROID)
- {
- Oid text_id = TEXTOID;
+ if (!can_coerce_type(1, &exprtype, &targettype, ccontext))
+ return NULL;
- if (can_coerce_type(1, &exprtype, &text_id, ccontext))
- {
- expr = coerce_type(pstate, expr, exprtype, text_id,
- ccontext, cformat);
- if (targetbasetype != targettype)
- {
- /* need to coerce to domain over char or varchar */
- expr = coerce_to_domain(expr, targetbasetype, targettype,
- cformat);
- }
- else
- {
- /*
- * need a RelabelType if no typmod coercion will be
- * performed
- */
- if (targettypmod < 0)
- expr = (Node *) makeRelabelType((Expr *) expr,
- targettype, -1,
- cformat);
- }
- }
- else
- expr = NULL;
- }
- else
- expr = NULL;
- }
- else
- expr = NULL;
+ result = coerce_type(pstate, expr, exprtype,
+ targettype, targettypmod,
+ ccontext, cformat);
/*
- * If the target is a fixed-length type, it may need a length coercion
- * as well as a type coercion.
+ * If the target is a fixed-length type, it may need a length coercion as
+ * well as a type coercion. If we find ourselves adding both, force the
+ * inner coercion node to implicit display form.
*/
- if (expr != NULL)
- expr = coerce_type_typmod(expr, targettype, targettypmod,
- cformat,
- (cformat != COERCE_IMPLICIT_CAST));
+ result = coerce_type_typmod(result,
+ targettype, targettypmod,
+ cformat,
+ (cformat != COERCE_IMPLICIT_CAST),
+ (result != expr && !IsA(result, Const)));
- return expr;
+ return result;
}
* The caller should already have determined that the coercion is possible;
* see can_coerce_type.
*
- * No coercion to a typmod (length) is performed here. The caller must
- * call coerce_type_typmod as well, if a typmod constraint is wanted.
+ * Normally, no coercion to a typmod (length) is performed here. The caller
+ * must call coerce_type_typmod as well, if a typmod constraint is wanted.
* (But if the target type is a domain, it may internally contain a
* typmod constraint, which will be applied inside coerce_to_domain.)
+ * In some cases pg_cast specifies a type coercion function that also
+ * applies length conversion, and in those cases only, the result will
+ * already be properly coerced to the specified typmod.
*
* pstate is only used in the case that we are able to resolve the type of
* a previously UNKNOWN Param. It is okay to pass pstate = NULL if the
*/
Node *
coerce_type(ParseState *pstate, Node *node,
- Oid inputTypeId, Oid targetTypeId,
+ Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod,
CoercionContext ccontext, CoercionForm cformat)
{
Node *result;
/* no conversion needed */
return node;
}
+ if (targetTypeId == ANYOID ||
+ targetTypeId == ANYARRAYOID ||
+ targetTypeId == ANYELEMENTOID)
+ {
+ /* assume can_coerce_type verified that implicit coercion is okay */
+ /* NB: we do NOT want a RelabelType here */
+ return node;
+ }
if (inputTypeId == UNKNOWNOID && IsA(node, Const))
{
/*
- * Input is a string constant with previously undetermined type.
- * Apply the target type's typinput function to it to produce a
- * constant of the target type.
+ * Input is a string constant with previously undetermined type. Apply
+ * the target type's typinput function to it to produce a constant of
+ * the target type.
*
* NOTE: this case cannot be folded together with the other
* constant-input case, since the typinput function does not
* float-to-int type conversion will round to integer.
*
* XXX if the typinput function is not immutable, we really ought to
- * postpone evaluation of the function call until runtime. But
- * there is no way to represent a typinput function call as an
- * expression tree, because C-string values are not Datums. (XXX
- * This *is* possible as of 7.3, do we want to do it?)
+ * postpone evaluation of the function call until runtime. But there
+ * is no way to represent a typinput function call as an expression
+ * tree, because C-string values are not Datums. (XXX This *is*
+ * possible as of 7.3, do we want to do it?)
*/
Const *con = (Const *) node;
Const *newcon = makeNode(Const);
newcon->constbyval = typeByVal(targetType);
newcon->constisnull = con->constisnull;
+ /*
+ * We pass typmod -1 to the input routine, primarily because
+ * existing input routines follow implicit-coercion semantics for
+ * length checks, which is not always what we want here. Any
+ * length constraint will be applied later by our caller.
+ *
+ * We assume here that UNKNOWN's internal representation is the
+ * same as CSTRING.
+ */
if (!con->constisnull)
- {
- char *val = DatumGetCString(DirectFunctionCall1(unknownout,
- con->constvalue));
-
- /*
- * We pass typmod -1 to the input routine, primarily because
- * existing input routines follow implicit-coercion semantics
- * for length checks, which is not always what we want here.
- * Any length constraint will be applied later by our caller.
- *
- * Note that we call stringTypeDatum using the domain's pg_type
- * row, if it's a domain. This works because the domain row
- * has the same typinput and typelem as the base type ---
- * ugly...
- */
- newcon->constvalue = stringTypeDatum(targetType, val, -1);
- pfree(val);
- }
+ newcon->constvalue = stringTypeDatum(targetType,
+ DatumGetCString(con->constvalue),
+ -1);
+ else
+ newcon->constvalue = stringTypeDatum(targetType, NULL, -1);
result = (Node *) newcon;
/* If target is a domain, apply constraints. */
if (targetTyptype == 'd')
result = coerce_to_domain(result, InvalidOid, targetTypeId,
- cformat);
+ cformat, false, false);
ReleaseSysCache(targetType);
pstate != NULL && pstate->p_variableparams)
{
/*
- * Input is a Param of previously undetermined type, and we want
- * to update our knowledge of the Param's type. Find the topmost
+ * Input is a Param of previously undetermined type, and we want to
+ * update our knowledge of the Param's type. Find the topmost
* ParseState and update the state.
*/
Param *param = (Param *) node;
/* Ooops */
ereport(ERROR,
(errcode(ERRCODE_AMBIGUOUS_PARAMETER),
- errmsg("inconsistent types deduced for parameter $%d",
- paramno),
+ errmsg("inconsistent types deduced for parameter $%d",
+ paramno),
errdetail("%s versus %s",
- format_type_be(toppstate->p_paramtypes[paramno - 1]),
+ format_type_be(toppstate->p_paramtypes[paramno - 1]),
format_type_be(targetTypeId))));
}
param->paramtype = targetTypeId;
- return (Node *) param;
- }
- if (targetTypeId == ANYOID ||
- targetTypeId == ANYARRAYOID ||
- targetTypeId == ANYELEMENTOID)
- {
- /* assume can_coerce_type verified that implicit coercion is okay */
- /* NB: we do NOT want a RelabelType here */
- return node;
+
+ /* Apply domain constraints, if necessary */
+ return coerce_to_domain((Node *) param, InvalidOid, targetTypeId,
+ cformat, false, false);
}
if (find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
&funcId))
if (OidIsValid(funcId))
{
/*
- * Generate an expression tree representing run-time
- * application of the conversion function. If we are dealing
- * with a domain target type, the conversion function will
- * yield the base type.
+ * Generate an expression tree representing run-time application
+ * of the conversion function. If we are dealing with a domain
+ * target type, the conversion function will yield the base type,
+ * and we need to extract the correct typmod to use from the
+ * domain's typtypmod.
*/
Oid baseTypeId = getBaseType(targetTypeId);
+ int32 baseTypeMod;
+
+ if (targetTypeId != baseTypeId)
+ baseTypeMod = get_typtypmod(targetTypeId);
+ else
+ baseTypeMod = targetTypeMod;
- result = (Node *) makeFuncExpr(funcId, baseTypeId,
- makeList1(node),
- cformat);
+ result = build_coercion_expression(node, funcId,
+ baseTypeId, baseTypeMod,
+ cformat,
+ (cformat != COERCE_IMPLICIT_CAST));
/*
- * If domain, coerce to the domain type and relabel with
- * domain type ID
+ * If domain, coerce to the domain type and relabel with domain
+ * type ID. We can skip the internal length-coercion step if the
+ * selected coercion function was a type-and-length coercion.
*/
if (targetTypeId != baseTypeId)
result = coerce_to_domain(result, baseTypeId, targetTypeId,
- cformat);
+ cformat, true,
+ exprIsLengthCoercion(result,
+ NULL));
}
else
{
/*
- * We don't need to do a physical conversion, but we do need
- * to attach a RelabelType node so that the expression will be
- * seen to have the intended type when inspected by
- * higher-level code.
+ * We don't need to do a physical conversion, but we do need to
+ * attach a RelabelType node so that the expression will be seen
+ * to have the intended type when inspected by higher-level code.
*
* Also, domains may have value restrictions beyond the base type
* that must be accounted for. If the destination is a domain
* then we won't need a RelabelType node.
*/
result = coerce_to_domain(node, InvalidOid, targetTypeId,
- cformat);
+ cformat, false, false);
if (result == node)
{
/*
- * XXX could we label result with exprTypmod(node) instead
- * of default -1 typmod, to save a possible
- * length-coercion later? Would work if both types have
- * same interpretation of typmod, which is likely but not
- * certain.
+ * XXX could we label result with exprTypmod(node) instead of
+ * default -1 typmod, to save a possible length-coercion
+ * later? Would work if both types have same interpretation of
+ * typmod, which is likely but not certain.
*/
result = (Node *) makeRelabelType((Expr *) result,
targetTypeId, -1,
}
return result;
}
+ if (inputTypeId == RECORDOID &&
+ ISCOMPLEX(targetTypeId))
+ {
+ /* Coerce a RECORD to a specific complex type */
+ return coerce_record_to_complex(pstate, node, targetTypeId,
+ ccontext, cformat);
+ }
+ if (targetTypeId == RECORDOID &&
+ ISCOMPLEX(inputTypeId))
+ {
+ /* Coerce a specific complex type to RECORD */
+ /* NB: we do NOT want a RelabelType here */
+ return node;
+ }
if (typeInheritsFrom(inputTypeId, targetTypeId))
{
/*
- * Input class type is a subclass of target, so nothing to do ---
- * except relabel the type. This is binary compatibility for
- * complex types.
+ * Input class type is a subclass of target, so generate an
+ * appropriate runtime conversion (removing unneeded columns and
+ * possibly rearranging the ones that are wanted).
*/
- return (Node *) makeRelabelType((Expr *) node,
- targetTypeId, -1,
- cformat);
+ ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
+
+ r->arg = (Expr *) node;
+ r->resulttype = targetTypeId;
+ r->convertformat = cformat;
+ return (Node *) r;
}
/* If we get here, caller blew it */
elog(ERROR, "failed to find conversion function from %s to %s",
if (inputTypeId == targetTypeId)
continue;
- /* don't choke on references to no-longer-existing types */
- if (!typeidIsValid(inputTypeId))
- return false;
- if (!typeidIsValid(targetTypeId))
- return false;
-
- /*
- * If input is an untyped string constant, assume we can convert
- * it to anything except a class type.
- */
- if (inputTypeId == UNKNOWNOID)
- {
- if (ISCOMPLEX(targetTypeId))
- return false;
- continue;
- }
-
/* accept if target is ANY */
if (targetTypeId == ANYOID)
continue;
}
/*
- * If pg_cast shows that we can coerce, accept. This test now
- * covers both binary-compatible and coercion-function cases.
+ * If input is an untyped string constant, assume we can convert it to
+ * anything.
+ */
+ if (inputTypeId == UNKNOWNOID)
+ continue;
+
+ /*
+ * If pg_cast shows that we can coerce, accept. This test now covers
+ * both binary-compatible and coercion-function cases.
*/
if (find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
&funcId))
continue;
+ /*
+ * If input is RECORD and target is a composite type, assume we can
+ * coerce (may need tighter checking here)
+ */
+ if (inputTypeId == RECORDOID &&
+ ISCOMPLEX(targetTypeId))
+ continue;
+
+ /*
+ * If input is a composite type and target is RECORD, accept
+ */
+ if (targetTypeId == RECORDOID &&
+ ISCOMPLEX(inputTypeId))
+ continue;
+
/*
* If input is a class type that inherits from target, accept
*/
* has not bothered to look this up)
* 'typeId': target type to coerce to
* 'cformat': coercion format
+ * 'hideInputCoercion': if true, hide the input coercion under this one.
+ * 'lengthCoercionDone': if true, caller already accounted for length.
*
* If the target type isn't a domain, the given 'arg' is returned as-is.
*/
Node *
-coerce_to_domain(Node *arg, Oid baseTypeId, Oid typeId, CoercionForm cformat)
+coerce_to_domain(Node *arg, Oid baseTypeId, Oid typeId,
+ CoercionForm cformat, bool hideInputCoercion,
+ bool lengthCoercionDone)
{
CoerceToDomain *result;
- int32 typmod;
/* Get the base type if it hasn't been supplied */
if (baseTypeId == InvalidOid)
if (baseTypeId == typeId)
return arg;
+ /* Suppress display of nested coercion steps */
+ if (hideInputCoercion)
+ hide_coercion_node(arg);
+
/*
- * If the domain applies a typmod to its base type, build the
- * appropriate coercion step. Mark it implicit for display purposes,
- * because we don't want it shown separately by ruleutils.c; but the
- * isExplicit flag passed to the conversion function depends on the
- * manner in which the domain coercion is invoked, so that the
- * semantics of implicit and explicit coercion differ. (Is that
- * really the behavior we want?)
+ * If the domain applies a typmod to its base type, build the appropriate
+ * coercion step. Mark it implicit for display purposes, because we don't
+ * want it shown separately by ruleutils.c; but the isExplicit flag passed
+ * to the conversion function depends on the manner in which the domain
+ * coercion is invoked, so that the semantics of implicit and explicit
+ * coercion differ. (Is that really the behavior we want?)
*
* NOTE: because we apply this as part of the fixed expression structure,
- * ALTER DOMAIN cannot alter the typtypmod. But it's unclear that
- * that would be safe to do anyway, without lots of knowledge about
- * what the base type thinks the typmod means.
+ * ALTER DOMAIN cannot alter the typtypmod. But it's unclear that that
+ * would be safe to do anyway, without lots of knowledge about what the
+ * base type thinks the typmod means.
*/
- typmod = get_typtypmod(typeId);
- if (typmod >= 0)
- arg = coerce_type_typmod(arg, baseTypeId, typmod,
- COERCE_IMPLICIT_CAST,
- (cformat != COERCE_IMPLICIT_CAST));
+ if (!lengthCoercionDone)
+ {
+ int32 typmod = get_typtypmod(typeId);
+
+ if (typmod >= 0)
+ arg = coerce_type_typmod(arg, baseTypeId, typmod,
+ COERCE_IMPLICIT_CAST,
+ (cformat != COERCE_IMPLICIT_CAST),
+ false);
+ }
/*
- * Now build the domain coercion node. This represents run-time
- * checking of any constraints currently attached to the domain. This
- * also ensures that the expression is properly labeled as to result
- * type.
+ * Now build the domain coercion node. This represents run-time checking
+ * of any constraints currently attached to the domain. This also ensures
+ * that the expression is properly labeled as to result type.
*/
result = makeNode(CoerceToDomain);
result->arg = (Expr *) arg;
* The caller must have already ensured that the value is of the correct
* type, typically by applying coerce_type.
*
+ * cformat determines the display properties of the generated node (if any),
+ * while isExplicit may affect semantics. If hideInputCoercion is true
+ * *and* we generate a node, the input node is forced to IMPLICIT display
+ * form, so that only the typmod coercion node will be visible when
+ * displaying the expression.
+ *
* NOTE: this does not need to work on domain types, because any typmod
* coercion for a domain is considered to be part of the type coercion
* needed to produce the domain value in the first place. So, no getBaseType.
*/
static Node *
coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod,
- CoercionForm cformat, bool isExplicit)
+ CoercionForm cformat, bool isExplicit,
+ bool hideInputCoercion)
{
Oid funcId;
- int nargs;
/*
- * A negative typmod is assumed to mean that no coercion is wanted.
+ * A negative typmod is assumed to mean that no coercion is wanted. Also,
+ * skip coercion if already done.
*/
if (targetTypMod < 0 || targetTypMod == exprTypmod(node))
return node;
- funcId = find_typmod_coercion_function(targetTypeId, &nargs);
+ funcId = find_typmod_coercion_function(targetTypeId);
if (OidIsValid(funcId))
{
- List *args;
- Const *cons;
+ /* Suppress display of nested coercion steps */
+ if (hideInputCoercion)
+ hide_coercion_node(node);
+
+ node = build_coercion_expression(node, funcId,
+ targetTypeId, targetTypMod,
+ cformat, isExplicit);
+ }
+
+ return node;
+}
+
+/*
+ * Mark a coercion node as IMPLICIT so it will never be displayed by
+ * ruleutils.c. We use this when we generate a nest of coercion nodes
+ * to implement what is logically one conversion; the inner nodes are
+ * forced to IMPLICIT_CAST format. This does not change their semantics,
+ * only display behavior.
+ *
+ * It is caller error to call this on something that doesn't have a
+ * CoercionForm field.
+ */
+static void
+hide_coercion_node(Node *node)
+{
+ if (IsA(node, FuncExpr))
+ ((FuncExpr *) node)->funcformat = COERCE_IMPLICIT_CAST;
+ else if (IsA(node, RelabelType))
+ ((RelabelType *) node)->relabelformat = COERCE_IMPLICIT_CAST;
+ else if (IsA(node, ConvertRowtypeExpr))
+ ((ConvertRowtypeExpr *) node)->convertformat = COERCE_IMPLICIT_CAST;
+ else if (IsA(node, RowExpr))
+ ((RowExpr *) node)->row_format = COERCE_IMPLICIT_CAST;
+ else if (IsA(node, CoerceToDomain))
+ ((CoerceToDomain *) node)->coercionformat = COERCE_IMPLICIT_CAST;
+ else
+ elog(ERROR, "unsupported node type: %d", (int) nodeTag(node));
+}
+
+/*
+ * build_coercion_expression()
+ * Construct a function-call expression for applying a pg_cast entry.
+ *
+ * This is used for both type-coercion and length-coercion functions,
+ * since there is no difference in terms of the calling convention.
+ */
+static Node *
+build_coercion_expression(Node *node, Oid funcId,
+ Oid targetTypeId, int32 targetTypMod,
+ CoercionForm cformat, bool isExplicit)
+{
+ HeapTuple tp;
+ Form_pg_proc procstruct;
+ int nargs;
+ List *args;
+ Const *cons;
+
+ tp = SearchSysCache(PROCOID,
+ ObjectIdGetDatum(funcId),
+ 0, 0, 0);
+ if (!HeapTupleIsValid(tp))
+ elog(ERROR, "cache lookup failed for function %u", funcId);
+ procstruct = (Form_pg_proc) GETSTRUCT(tp);
- /* Pass given value, plus target typmod as an int4 constant */
+ /*
+ * Asserts essentially check that function is a legal coercion function.
+ * We can't make the seemingly obvious tests on prorettype and
+ * proargtypes[0], because of various binary-compatibility cases.
+ */
+ /* Assert(targetTypeId == procstruct->prorettype); */
+ Assert(!procstruct->proretset);
+ Assert(!procstruct->proisagg);
+ nargs = procstruct->pronargs;
+ Assert(nargs >= 1 && nargs <= 3);
+ /* Assert(procstruct->proargtypes.values[0] == exprType(node)); */
+ Assert(nargs < 2 || procstruct->proargtypes.values[1] == INT4OID);
+ Assert(nargs < 3 || procstruct->proargtypes.values[2] == BOOLOID);
+
+ ReleaseSysCache(tp);
+
+ args = list_make1(node);
+
+ if (nargs >= 2)
+ {
+ /* Pass target typmod as an int4 constant */
cons = makeConst(INT4OID,
sizeof(int32),
Int32GetDatum(targetTypMod),
false,
true);
- args = makeList2(node, cons);
+ args = lappend(args, cons);
+ }
- if (nargs == 3)
- {
- /* Pass it a boolean isExplicit parameter, too */
- cons = makeConst(BOOLOID,
- sizeof(bool),
- BoolGetDatum(isExplicit),
- false,
- true);
-
- args = lappend(args, cons);
- }
+ if (nargs == 3)
+ {
+ /* Pass it a boolean isExplicit parameter, too */
+ cons = makeConst(BOOLOID,
+ sizeof(bool),
+ BoolGetDatum(isExplicit),
+ false,
+ true);
- node = (Node *) makeFuncExpr(funcId, targetTypeId, args, cformat);
+ args = lappend(args, cons);
}
- return node;
+ return (Node *) makeFuncExpr(funcId, targetTypeId, args, cformat);
}
+/*
+ * coerce_record_to_complex
+ * Coerce a RECORD to a specific composite type.
+ *
+ * Currently we only support this for inputs that are RowExprs or whole-row
+ * Vars.
+ */
+static Node *
+coerce_record_to_complex(ParseState *pstate, Node *node,
+ Oid targetTypeId,
+ CoercionContext ccontext,
+ CoercionForm cformat)
+{
+ RowExpr *rowexpr;
+ TupleDesc tupdesc;
+ List *args = NIL;
+ List *newargs;
+ int i;
+ int ucolno;
+ ListCell *arg;
+
+ if (node && IsA(node, RowExpr))
+ {
+ /*
+ * Since the RowExpr must be of type RECORD, we needn't worry about it
+ * containing any dropped columns.
+ */
+ args = ((RowExpr *) node)->args;
+ }
+ else if (node && IsA(node, Var) &&
+ ((Var *) node)->varattno == InvalidAttrNumber)
+ {
+ int rtindex = ((Var *) node)->varno;
+ int sublevels_up = ((Var *) node)->varlevelsup;
+ RangeTblEntry *rte;
+
+ rte = GetRTEByRangeTablePosn(pstate, rtindex, sublevels_up);
+ expandRTE(rte, rtindex, sublevels_up, false,
+ NULL, &args);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId))));
+
+ tupdesc = lookup_rowtype_tupdesc(targetTypeId, -1);
+ newargs = NIL;
+ ucolno = 1;
+ arg = list_head(args);
+ for (i = 0; i < tupdesc->natts; i++)
+ {
+ Node *expr;
+ Oid exprtype;
+
+ /* Fill in NULLs for dropped columns in rowtype */
+ if (tupdesc->attrs[i]->attisdropped)
+ {
+ /*
+ * can't use atttypid here, but it doesn't really matter what type
+ * the Const claims to be.
+ */
+ newargs = lappend(newargs, makeNullConst(INT4OID));
+ continue;
+ }
+
+ if (arg == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId)),
+ errdetail("Input has too few columns.")));
+ expr = (Node *) lfirst(arg);
+ exprtype = exprType(expr);
+
+ expr = coerce_to_target_type(pstate,
+ expr, exprtype,
+ tupdesc->attrs[i]->atttypid,
+ tupdesc->attrs[i]->atttypmod,
+ ccontext,
+ COERCE_IMPLICIT_CAST);
+ if (expr == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId)),
+ errdetail("Cannot cast type %s to %s in column %d.",
+ format_type_be(exprtype),
+ format_type_be(tupdesc->attrs[i]->atttypid),
+ ucolno)));
+ newargs = lappend(newargs, expr);
+ ucolno++;
+ arg = lnext(arg);
+ }
+ if (arg != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId)),
+ errdetail("Input has too many columns.")));
+
+ rowexpr = makeNode(RowExpr);
+ rowexpr->args = newargs;
+ rowexpr->row_typeid = targetTypeId;
+ rowexpr->row_format = cformat;
+ return (Node *) rowexpr;
+}
+
/* coerce_to_boolean()
* Coerce an argument of a construct that requires boolean input
* (AND, OR, NOT, etc). Also check that input is not a set.
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
/* translator: first %s is name of a SQL construct, eg WHERE */
- errmsg("argument of %s must be type boolean, not type %s",
- constructName, format_type_be(inputTypeId))));
+ errmsg("argument of %s must be type boolean, not type %s",
+ constructName, format_type_be(inputTypeId))));
}
if (expression_returns_set(node))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
/* translator: first %s is name of a SQL construct, eg LIMIT */
- errmsg("argument of %s must be type integer, not type %s",
- constructName, format_type_be(inputTypeId))));
+ errmsg("argument of %s must be type integer, not type %s",
+ constructName, format_type_be(inputTypeId))));
}
if (expression_returns_set(node))
{
Oid ptype;
CATEGORY pcategory;
- List *l;
+ ListCell *type_item;
Assert(typeids != NIL);
- ptype = getBaseType(lfirsto(typeids));
+ ptype = getBaseType(linitial_oid(typeids));
pcategory = TypeCategory(ptype);
- foreach(l, lnext(typeids))
+
+ for_each_cell(type_item, lnext(list_head(typeids)))
{
- Oid ntype = getBaseType(lfirsto(l));
+ Oid ntype = getBaseType(lfirst_oid(type_item));
/* move on to next one if no new information... */
if ((ntype != InvalidOid) && (ntype != UNKNOWNOID) && (ntype != ptype))
else if (TypeCategory(ntype) != pcategory)
{
/*
- * both types in different categories? then not much
- * hope...
+ * both types in different categories? then not much hope...
*/
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
/*
- * translator: first %s is name of a SQL construct, eg
- * CASE
+ * translator: first %s is name of a SQL construct, eg CASE
*/
errmsg("%s types %s and %s cannot be matched",
context,
format_type_be(ntype))));
}
else if (!IsPreferredType(pcategory, ptype) &&
- can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
- !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
+ can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
+ !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
{
/*
- * take new type if can coerce to it implicitly but not
- * the other way; but if we have a preferred type, stay on
- * it.
+ * take new type if can coerce to it implicitly but not the
+ * other way; but if we have a preferred type, stay on it.
*/
ptype = ntype;
pcategory = TypeCategory(ptype);
}
/*
- * If all the inputs were UNKNOWN type --- ie, unknown-type literals
- * --- then resolve as type TEXT. This situation comes up with
- * constructs like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END);
- * SELECT 'foo' UNION SELECT 'bar'; It might seem desirable to leave
- * the construct's output type as UNKNOWN, but that really doesn't
- * work, because we'd probably end up needing a runtime coercion from
- * UNKNOWN to something else, and we usually won't have it. We need
- * to coerce the unknown literals while they are still literals, so a
- * decision has to be made now.
+ * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
+ * then resolve as type TEXT. This situation comes up with constructs
+ * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
+ * UNION SELECT 'bar'; It might seem desirable to leave the construct's
+ * output type as UNKNOWN, but that really doesn't work, because we'd
+ * probably end up needing a runtime coercion from UNKNOWN to something
+ * else, and we usually won't have it. We need to coerce the unknown
+ * literals while they are still literals, so a decision has to be made
+ * now.
*/
if (ptype == UNKNOWNOID)
ptype = TEXTOID;
if (inputTypeId == targetTypeId)
return node; /* no work */
if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
- node = coerce_type(pstate, node, inputTypeId, targetTypeId,
+ node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
else
ereport(ERROR,
bool have_anyelement = false;
/*
- * Loop through the arguments to see if we have any that are ANYARRAY
- * or ANYELEMENT. If so, require the actual types to be
- * self-consistent
+ * Loop through the arguments to see if we have any that are ANYARRAY or
+ * ANYELEMENT. If so, require the actual types to be self-consistent
*/
for (j = 0; j < nargs; j++)
{
if (!OidIsValid(elem_typeid))
{
/*
- * if we don't have an element type yet, use the one we just
- * got
+ * if we don't have an element type yet, use the one we just got
*/
elem_typeid = array_typelem;
}
bool have_anyelement = (rettype == ANYELEMENTOID);
/*
- * Loop through the arguments to see if we have any that are ANYARRAY
- * or ANYELEMENT. If so, require the actual types to be
- * self-consistent
+ * Loop through the arguments to see if we have any that are ANYARRAY or
+ * ANYELEMENT. If so, require the actual types to be self-consistent
*/
for (j = 0; j < nargs; j++)
{
}
/*
- * Fast Track: if none of the arguments are ANYARRAY or ANYELEMENT,
- * return the unmodified rettype.
+ * Fast Track: if none of the arguments are ANYARRAY or ANYELEMENT, return
+ * the unmodified rettype.
*/
if (!have_generics)
return rettype;
if (!OidIsValid(elem_typeid))
{
/*
- * if we don't have an element type yet, use the one we just
- * got
+ * if we don't have an element type yet, use the one we just got
*/
elem_typeid = array_typelem;
}
if (!OidIsValid(array_typeid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("could not find array type for data type %s",
- format_type_be(elem_typeid))));
+ errmsg("could not find array type for data type %s",
+ format_type_be(elem_typeid))));
}
declared_arg_types[j] = array_typeid;
}
if (!OidIsValid(array_typeid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("could not find array type for data type %s",
- format_type_be(elem_typeid))));
+ errmsg("could not find array type for data type %s",
+ format_type_be(elem_typeid))));
}
return array_typeid;
}
if (!OidIsValid(array_typeid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("could not find array type for data type %s",
- format_type_be(context_actual_type))));
+ errmsg("could not find array type for data type %s",
+ format_type_be(context_actual_type))));
return array_typeid;
}
}
return false;
/*
- * This switch should agree with TypeCategory(), above. Note that at
- * this point, category certainly matches the type.
+ * This switch should agree with TypeCategory(), above. Note that at this
+ * point, category certainly matches the type.
*/
switch (category)
{
else
{
/*
- * If there's no pg_cast entry, perhaps we are dealing with a pair
- * of array types. If so, and if the element types have a
- * suitable cast, use array_type_coerce().
+ * If there's no pg_cast entry, perhaps we are dealing with a pair of
+ * array types. If so, and if the element types have a suitable cast,
+ * use array_type_coerce() or array_type_length_coerce().
+ *
+ * Hack: disallow coercions to oidvector and int2vector, which
+ * otherwise tend to capture coercions that should go to "real" array
+ * types. We want those types to be considered "real" arrays for many
+ * purposes, but not this one. (Also, array_type_coerce isn't
+ * guaranteed to produce an output that meets the restrictions of
+ * these datatypes, such as being 1-dimensional.)
*/
Oid targetElemType;
Oid sourceElemType;
Oid elemfuncid;
+ if (targetTypeId == OIDVECTOROID || targetTypeId == INT2VECTOROID)
+ return false;
+
if ((targetElemType = get_element_type(targetTypeId)) != InvalidOid &&
- (sourceElemType = get_element_type(sourceTypeId)) != InvalidOid)
+ (sourceElemType = get_element_type(sourceTypeId)) != InvalidOid)
{
if (find_coercion_pathway(targetElemType, sourceElemType,
ccontext, &elemfuncid))
{
- *funcid = F_ARRAY_TYPE_COERCE;
+ if (!OidIsValid(elemfuncid))
+ {
+ /* binary-compatible element type conversion */
+ *funcid = F_ARRAY_TYPE_COERCE;
+ }
+ else
+ {
+ /* does the function take a typmod arg? */
+ if (get_func_nargs(elemfuncid) > 1)
+ *funcid = F_ARRAY_TYPE_LENGTH_COERCE;
+ else
+ *funcid = F_ARRAY_TYPE_COERCE;
+ }
result = true;
}
}
/*
* find_typmod_coercion_function -- does the given type need length coercion?
*
- * If the target type possesses a function named for the type
- * and having parameter signature (targettype, int4), we assume that
- * the type requires coercion to its own length and that the said
- * function should be invoked to do that.
- *
- * Alternatively, the length-coercing function may have the signature
- * (targettype, int4, bool). On success, *nargs is set to report which
- * signature we found.
+ * If the target type possesses a pg_cast function from itself to itself,
+ * it must need length coercion.
*
* "bpchar" (ie, char(N)) and "numeric" are examples of such types.
*
* function associated directly with the array type, but instead look for
* one associated with the element type. If one exists, we report
* array_length_coerce() as the coercion function to use.
- *
- * This mechanism may seem pretty grotty and in need of replacement by
- * something in pg_cast, but since typmod is only interesting for datatypes
- * that have special handling in the grammar, there's not really much
- * percentage in making it any easier to apply such coercions ...
*/
Oid
-find_typmod_coercion_function(Oid typeId, int *nargs)
+find_typmod_coercion_function(Oid typeId)
{
Oid funcid = InvalidOid;
bool isArray = false;
Type targetType;
Form_pg_type typeForm;
- char *typname;
- Oid typnamespace;
- Oid oid_array[FUNC_MAX_ARGS];
- HeapTuple ftup;
+ HeapTuple tuple;
targetType = typeidType(typeId);
typeForm = (Form_pg_type) GETSTRUCT(targetType);
{
/* Yes, switch our attention to the element type */
typeId = typeForm->typelem;
- ReleaseSysCache(targetType);
- targetType = typeidType(typeId);
- typeForm = (Form_pg_type) GETSTRUCT(targetType);
isArray = true;
}
+ ReleaseSysCache(targetType);
- /* Function name is same as type internal name, and in same namespace */
- typname = NameStr(typeForm->typname);
- typnamespace = typeForm->typnamespace;
-
- /* First look for parameters (type, int4) */
- MemSet(oid_array, 0, FUNC_MAX_ARGS * sizeof(Oid));
- oid_array[0] = typeId;
- oid_array[1] = INT4OID;
- *nargs = 2;
-
- ftup = SearchSysCache(PROCNAMENSP,
- CStringGetDatum(typname),
- Int16GetDatum(2),
- PointerGetDatum(oid_array),
- ObjectIdGetDatum(typnamespace));
- if (HeapTupleIsValid(ftup))
- {
- Form_pg_proc pform = (Form_pg_proc) GETSTRUCT(ftup);
-
- /* Make sure the function's result type is as expected */
- if (pform->prorettype == typeId && !pform->proretset &&
- !pform->proisagg)
- {
- /* Okay to use it */
- funcid = HeapTupleGetOid(ftup);
- }
- ReleaseSysCache(ftup);
- }
+ /* Look in pg_cast */
+ tuple = SearchSysCache(CASTSOURCETARGET,
+ ObjectIdGetDatum(typeId),
+ ObjectIdGetDatum(typeId),
+ 0, 0);
- if (!OidIsValid(funcid))
+ if (HeapTupleIsValid(tuple))
{
- /* Didn't find a function, so now try (type, int4, bool) */
- oid_array[2] = BOOLOID;
- *nargs = 3;
-
- ftup = SearchSysCache(PROCNAMENSP,
- CStringGetDatum(typname),
- Int16GetDatum(3),
- PointerGetDatum(oid_array),
- ObjectIdGetDatum(typnamespace));
- if (HeapTupleIsValid(ftup))
- {
- Form_pg_proc pform = (Form_pg_proc) GETSTRUCT(ftup);
+ Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
- /* Make sure the function's result type is as expected */
- if (pform->prorettype == typeId && !pform->proretset &&
- !pform->proisagg)
- {
- /* Okay to use it */
- funcid = HeapTupleGetOid(ftup);
- }
- ReleaseSysCache(ftup);
- }
+ funcid = castForm->castfunc;
+ ReleaseSysCache(tuple);
}
- ReleaseSysCache(targetType);
-
/*
* Now, if we did find a coercion function for an array element type,
- * report array_length_coerce() as the function to use. We know it
- * takes three arguments always.
+ * report array_length_coerce() as the function to use.
*/
if (isArray && OidIsValid(funcid))
- {
funcid = F_ARRAY_LENGTH_COERCE;
- *nargs = 3;
- }
return funcid;
}