1998-05-10 01:31:34 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* parse_coerce.c
|
2000-02-20 22:32:16 +01:00
|
|
|
* handle type coercions/conversions for parser
|
1998-05-10 01:31:34 +02:00
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1998-05-10 01:31:34 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-06-25 23:30:34 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.100 2003/06/25 21:30:31 momjian Exp $
|
1998-05-10 01:31:34 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2002-07-19 01:11:32 +02:00
|
|
|
#include "catalog/pg_cast.h"
|
2000-03-19 01:15:39 +01:00
|
|
|
#include "catalog/pg_proc.h"
|
2002-03-20 20:45:13 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
2003-04-30 00:13:11 +02:00
|
|
|
#include "nodes/params.h"
|
1999-10-03 01:29:19 +02:00
|
|
|
#include "optimizer/clauses.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
|
|
|
#include "parser/parse_expr.h"
|
1999-06-18 00:21:41 +02:00
|
|
|
#include "parser/parse_func.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "parser/parse_type.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/builtins.h"
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
#include "utils/fmgroids.h"
|
2002-03-20 20:45:13 +01:00
|
|
|
#include "utils/lsyscache.h"
|
1998-05-10 01:31:34 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
static Node *coerce_type_typmod(Node *node,
|
|
|
|
Oid targetTypeId, int32 targetTypMod,
|
2003-02-03 22:15:45 +01:00
|
|
|
CoercionForm cformat, bool isExplicit);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
static Node *build_func_call(Oid funcid, Oid rettype, List *args,
|
|
|
|
CoercionForm fformat);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* coerce_to_target_type()
|
|
|
|
* Convert an expression to a target type and typmod.
|
|
|
|
*
|
|
|
|
* This is the general-purpose entry point for arbitrary type coercion
|
|
|
|
* operations. Direct use of the component operations can_coerce_type,
|
|
|
|
* coerce_type, and coerce_type_typmod should be restricted to special
|
|
|
|
* cases (eg, when the conversion is expected to succeed).
|
|
|
|
*
|
|
|
|
* Returns the possibly-transformed expression tree, or NULL if the type
|
|
|
|
* conversion is not possible. (We do this, rather than elog'ing directly,
|
|
|
|
* so that callers can generate custom error messages indicating context.)
|
|
|
|
*
|
2003-04-30 00:13:11 +02:00
|
|
|
* pstate - parse state (can be NULL, see coerce_type)
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* expr - input expression tree (already transformed by transformExpr)
|
|
|
|
* exprtype - result type of expr
|
|
|
|
* targettype - desired result type
|
|
|
|
* targettypmod - desired result typmod
|
|
|
|
* ccontext, cformat - context indicators to control coercions
|
|
|
|
*/
|
|
|
|
Node *
|
2003-04-30 00:13:11 +02:00
|
|
|
coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Oid targettype, int32 targettypmod,
|
|
|
|
CoercionContext ccontext,
|
|
|
|
CoercionForm cformat)
|
|
|
|
{
|
|
|
|
if (can_coerce_type(1, &exprtype, &targettype, ccontext))
|
2003-04-30 00:13:11 +02:00
|
|
|
expr = coerce_type(pstate, expr, exprtype, targettype,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
ccontext, cformat);
|
2003-05-26 02:11:29 +02:00
|
|
|
else if (ccontext >= COERCION_ASSIGNMENT)
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
/*
|
|
|
|
* 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);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
if (targetbasetype == BPCHAROID || targetbasetype == VARCHAROID)
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
Oid text_id = TEXTOID;
|
|
|
|
|
|
|
|
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;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
expr = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
expr = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the target is a fixed-length type, it may need a length coercion
|
|
|
|
* as well as a type coercion.
|
|
|
|
*/
|
|
|
|
if (expr != NULL)
|
2003-02-03 22:15:45 +01:00
|
|
|
expr = coerce_type_typmod(expr, targettype, targettypmod,
|
|
|
|
cformat,
|
|
|
|
(cformat != COERCE_IMPLICIT_CAST));
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
2002-09-01 00:10:48 +02:00
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
/*
|
|
|
|
* coerce_type()
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* Convert an expression to a different type.
|
2002-09-01 04:27:32 +02:00
|
|
|
*
|
|
|
|
* The caller should already have determined that the coercion is possible;
|
|
|
|
* see can_coerce_type.
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
*
|
|
|
|
* 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
|
2003-02-03 22:15:45 +01:00
|
|
|
* typmod constraint, which will be applied inside coerce_to_domain.)
|
2003-04-30 00:13:11 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* caller does not want type information updated for Params.
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
|
|
|
Node *
|
2003-04-30 00:13:11 +02:00
|
|
|
coerce_type(ParseState *pstate, Node *node,
|
|
|
|
Oid inputTypeId, Oid targetTypeId,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
CoercionContext ccontext, CoercionForm cformat)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2000-02-20 22:32:16 +01:00
|
|
|
Node *result;
|
2002-09-01 04:27:32 +02:00
|
|
|
Oid funcId;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2000-03-23 08:36:03 +01:00
|
|
|
if (targetTypeId == inputTypeId ||
|
|
|
|
node == NULL)
|
1999-08-05 04:33:54 +02:00
|
|
|
{
|
2002-09-01 00:10:48 +02:00
|
|
|
/* no conversion needed */
|
2003-04-30 00:13:11 +02:00
|
|
|
return node;
|
1999-08-05 04:33:54 +02:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
if (inputTypeId == UNKNOWNOID && IsA(node, Const))
|
1999-08-05 04:33:54 +02:00
|
|
|
{
|
1999-10-03 01:29:19 +02:00
|
|
|
/*
|
|
|
|
* Input is a string constant with previously undetermined type.
|
2000-04-12 19:17:23 +02:00
|
|
|
* Apply the target type's typinput function to it to produce a
|
|
|
|
* constant of the target type.
|
1999-08-05 04:33:54 +02:00
|
|
|
*
|
|
|
|
* NOTE: this case cannot be folded together with the other
|
|
|
|
* constant-input case, since the typinput function does not
|
2000-04-12 19:17:23 +02:00
|
|
|
* necessarily behave the same as a type conversion function. For
|
|
|
|
* example, int4's typinput function will reject "1.2", whereas
|
|
|
|
* float-to-int type conversion will round to integer.
|
1999-10-03 01:29:19 +02:00
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* XXX if the typinput function is not immutable, we really ought to
|
2000-04-12 19:17:23 +02:00
|
|
|
* postpone evaluation of the function call until runtime. But
|
|
|
|
* there is no way to represent a typinput function call as an
|
2002-09-04 22:31:48 +02:00
|
|
|
* expression tree, because C-string values are not Datums. (XXX
|
|
|
|
* This *is* possible as of 7.3, do we want to do it?)
|
1998-09-01 06:40:42 +02:00
|
|
|
*/
|
1999-08-05 04:33:54 +02:00
|
|
|
Const *con = (Const *) node;
|
1999-08-24 02:09:56 +02:00
|
|
|
Const *newcon = makeNode(Const);
|
1999-08-05 04:33:54 +02:00
|
|
|
Type targetType = typeidType(targetTypeId);
|
2002-09-01 00:10:48 +02:00
|
|
|
char targetTyptype = typeTypType(targetType);
|
1999-08-05 04:33:54 +02:00
|
|
|
|
1999-08-24 02:09:56 +02:00
|
|
|
newcon->consttype = targetTypeId;
|
|
|
|
newcon->constlen = typeLen(targetType);
|
|
|
|
newcon->constbyval = typeByVal(targetType);
|
|
|
|
newcon->constisnull = con->constisnull;
|
1999-08-05 04:33:54 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!con->constisnull)
|
1999-08-24 02:09:56 +02:00
|
|
|
{
|
2002-04-25 04:56:56 +02:00
|
|
|
char *val = DatumGetCString(DirectFunctionCall1(unknownout,
|
2001-03-22 05:01:46 +01:00
|
|
|
con->constvalue));
|
2002-09-01 00:10:48 +02:00
|
|
|
|
|
|
|
/*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* 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...
|
2002-09-01 00:10:48 +02:00
|
|
|
*/
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
newcon->constvalue = stringTypeDatum(targetType, val, -1);
|
1999-08-24 02:09:56 +02:00
|
|
|
pfree(val);
|
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1999-08-24 02:09:56 +02:00
|
|
|
result = (Node *) newcon;
|
2002-09-01 00:10:48 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* If target is a domain, apply constraints. */
|
2002-09-01 00:10:48 +02:00
|
|
|
if (targetTyptype == 'd')
|
2003-02-03 22:15:45 +01:00
|
|
|
result = coerce_to_domain(result, InvalidOid, targetTypeId,
|
|
|
|
cformat);
|
2002-09-01 00:10:48 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(targetType);
|
2003-04-30 00:13:11 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (inputTypeId == UNKNOWNOID && IsA(node, Param) &&
|
|
|
|
((Param *) node)->paramkind == PARAM_NUM &&
|
|
|
|
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
|
|
|
|
* ParseState and update the state.
|
|
|
|
*/
|
|
|
|
Param *param = (Param *) node;
|
|
|
|
int paramno = param->paramid;
|
|
|
|
ParseState *toppstate;
|
|
|
|
|
|
|
|
toppstate = pstate;
|
|
|
|
while (toppstate->parentParseState != NULL)
|
|
|
|
toppstate = toppstate->parentParseState;
|
|
|
|
|
|
|
|
if (paramno <= 0 || /* shouldn't happen, but... */
|
|
|
|
paramno > toppstate->p_numparams)
|
|
|
|
elog(ERROR, "Parameter '$%d' is out of range", paramno);
|
|
|
|
|
|
|
|
if (toppstate->p_paramtypes[paramno-1] == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
/* We've successfully resolved the type */
|
|
|
|
toppstate->p_paramtypes[paramno-1] = targetTypeId;
|
|
|
|
}
|
|
|
|
else if (toppstate->p_paramtypes[paramno-1] == targetTypeId)
|
|
|
|
{
|
|
|
|
/* We previously resolved the type, and it matches */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Ooops */
|
|
|
|
elog(ERROR, "Inconsistent types deduced for parameter '$%d'"
|
|
|
|
"\n\tCould be either %s or %s",
|
|
|
|
paramno,
|
|
|
|
format_type_be(toppstate->p_paramtypes[paramno-1]),
|
|
|
|
format_type_be(targetTypeId));
|
|
|
|
}
|
|
|
|
|
|
|
|
param->paramtype = targetTypeId;
|
|
|
|
return (Node *) param;
|
1999-08-05 04:33:54 +02:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
if (targetTypeId == ANYOID ||
|
|
|
|
targetTypeId == ANYARRAYOID ||
|
|
|
|
targetTypeId == ANYELEMENTOID)
|
2002-08-22 02:01:51 +02:00
|
|
|
{
|
|
|
|
/* assume can_coerce_type verified that implicit coercion is okay */
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* NB: we do NOT want a RelabelType here */
|
2003-04-30 00:13:11 +02:00
|
|
|
return node;
|
2002-08-22 02:01:51 +02:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
if (find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
|
|
|
|
&funcId))
|
2000-02-20 22:32:16 +01:00
|
|
|
{
|
2002-09-01 04:27:32 +02:00
|
|
|
if (OidIsValid(funcId))
|
|
|
|
{
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* 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.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
Oid baseTypeId = getBaseType(targetTypeId);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
result = build_func_call(funcId, baseTypeId, makeList1(node),
|
|
|
|
cformat);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
|
|
|
/*
|
2003-02-03 22:15:45 +01:00
|
|
|
* If domain, coerce to the domain type and relabel with
|
2002-09-01 04:27:32 +02:00
|
|
|
* domain type ID
|
|
|
|
*/
|
|
|
|
if (targetTypeId != baseTypeId)
|
2003-02-03 22:15:45 +01:00
|
|
|
result = coerce_to_domain(result, baseTypeId, targetTypeId,
|
|
|
|
cformat);
|
2002-09-01 04:27:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* 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.
|
2002-09-01 04:27:32 +02:00
|
|
|
*
|
|
|
|
* Also, domains may have value restrictions beyond the base type
|
2003-02-03 22:15:45 +01:00
|
|
|
* that must be accounted for. If the destination is a domain
|
|
|
|
* then we won't need a RelabelType node.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
2003-02-03 22:15:45 +01:00
|
|
|
result = coerce_to_domain(node, InvalidOid, targetTypeId,
|
|
|
|
cformat);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
result = (Node *) makeRelabelType((Expr *) result,
|
|
|
|
targetTypeId, -1,
|
|
|
|
cformat);
|
|
|
|
}
|
2002-09-01 04:27:32 +02:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
return result;
|
2000-02-20 22:32:16 +01:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
if (typeInheritsFrom(inputTypeId, targetTypeId))
|
2000-03-16 07:35:07 +01:00
|
|
|
{
|
2002-03-20 20:45:13 +01:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Input class type is a subclass of target, so nothing to do ---
|
|
|
|
* except relabel the type. This is binary compatibility for
|
|
|
|
* complex types.
|
2002-03-20 20:45:13 +01:00
|
|
|
*/
|
2003-04-30 00:13:11 +02:00
|
|
|
return (Node *) makeRelabelType((Expr *) node,
|
|
|
|
targetTypeId, -1,
|
|
|
|
cformat);
|
2000-03-16 07:35:07 +01:00
|
|
|
}
|
2003-04-30 00:13:11 +02:00
|
|
|
/* If we get here, caller blew it */
|
|
|
|
elog(ERROR, "coerce_type: no conversion function from %s to %s",
|
|
|
|
format_type_be(inputTypeId), format_type_be(targetTypeId));
|
|
|
|
return NULL; /* keep compiler quiet */
|
1999-05-22 06:12:29 +02:00
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
/*
|
|
|
|
* can_coerce_type()
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* Can input_typeids be coerced to target_typeids?
|
2002-04-11 22:00:18 +02:00
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* We must be told the context (CAST construct, assignment, implicit coercion)
|
|
|
|
* as this determines the set of available casts.
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
|
|
|
bool
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids,
|
|
|
|
CoercionContext ccontext)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2003-04-09 01:20:04 +02:00
|
|
|
bool have_generics = false;
|
1998-05-10 01:31:34 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* run through argument list... */
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Oid inputTypeId = input_typeids[i];
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Oid targetTypeId = target_typeids[i];
|
2002-03-20 20:45:13 +01:00
|
|
|
Oid funcId;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2000-03-16 07:35:07 +01:00
|
|
|
/* no problem if same type */
|
|
|
|
if (inputTypeId == targetTypeId)
|
|
|
|
continue;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
/* don't choke on references to no-longer-existing types */
|
|
|
|
if (!typeidIsValid(inputTypeId))
|
2000-03-16 07:35:07 +01:00
|
|
|
return false;
|
2002-08-22 02:01:51 +02:00
|
|
|
if (!typeidIsValid(targetTypeId))
|
2000-03-16 07:35:07 +01:00
|
|
|
return false;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-03-16 07:35:07 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* If input is an untyped string constant, assume we can convert
|
|
|
|
* it to anything except a class type.
|
2000-03-16 07:35:07 +01:00
|
|
|
*/
|
|
|
|
if (inputTypeId == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
if (ISCOMPLEX(targetTypeId))
|
1998-05-10 01:31:34 +02:00
|
|
|
return false;
|
2000-03-16 07:35:07 +01:00
|
|
|
continue;
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
2000-03-16 07:35:07 +01:00
|
|
|
|
2002-08-22 02:01:51 +02:00
|
|
|
/* accept if target is ANY */
|
|
|
|
if (targetTypeId == ANYOID)
|
|
|
|
continue;
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/* accept if target is ANYARRAY or ANYELEMENT, for now */
|
|
|
|
if (targetTypeId == ANYARRAYOID ||
|
|
|
|
targetTypeId == ANYELEMENTOID)
|
2002-08-22 02:01:51 +02:00
|
|
|
{
|
2003-04-09 01:20:04 +02:00
|
|
|
have_generics = true; /* do more checking later */
|
|
|
|
continue;
|
2002-08-22 02:01:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-01 04:27:32 +02:00
|
|
|
* If pg_cast shows that we can coerce, accept. This test now
|
|
|
|
* covers both binary-compatible and coercion-function cases.
|
2002-08-22 02:01:51 +02:00
|
|
|
*/
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
|
2002-09-01 04:27:32 +02:00
|
|
|
&funcId))
|
2002-08-22 02:01:51 +02:00
|
|
|
continue;
|
|
|
|
|
2000-03-16 07:35:07 +01:00
|
|
|
/*
|
2002-09-01 04:27:32 +02:00
|
|
|
* If input is a class type that inherits from target, accept
|
2000-03-16 07:35:07 +01:00
|
|
|
*/
|
|
|
|
if (typeInheritsFrom(inputTypeId, targetTypeId))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2002-09-01 04:27:32 +02:00
|
|
|
* Else, cannot coerce at this argument position
|
2000-03-16 07:35:07 +01:00
|
|
|
*/
|
2002-09-01 04:27:32 +02:00
|
|
|
return false;
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/* If we found any generic argument types, cross-check them */
|
|
|
|
if (have_generics)
|
|
|
|
{
|
|
|
|
if (!check_generic_type_consistency(input_typeids, target_typeids,
|
|
|
|
nargs))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
return true;
|
1999-05-22 06:12:29 +02:00
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
|
|
|
|
/*
|
2003-02-03 22:15:45 +01:00
|
|
|
* Create an expression tree to represent coercion to a domain type.
|
|
|
|
*
|
|
|
|
* 'arg': input expression
|
|
|
|
* 'baseTypeId': base type of domain, if known (pass InvalidOid if caller
|
|
|
|
* has not bothered to look this up)
|
|
|
|
* 'typeId': target type to coerce to
|
|
|
|
* 'cformat': coercion format
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
*
|
2003-02-03 22:15:45 +01:00
|
|
|
* If the target type isn't a domain, the given 'arg' is returned as-is.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
|
|
|
Node *
|
2003-02-03 22:15:45 +01:00
|
|
|
coerce_to_domain(Node *arg, Oid baseTypeId, Oid typeId, CoercionForm cformat)
|
2002-09-01 04:27:32 +02:00
|
|
|
{
|
2003-02-03 22:15:45 +01:00
|
|
|
CoerceToDomain *result;
|
|
|
|
int32 typmod;
|
2002-09-01 04:27:32 +02:00
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
/* Get the base type if it hasn't been supplied */
|
|
|
|
if (baseTypeId == InvalidOid)
|
|
|
|
baseTypeId = getBaseType(typeId);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
/* If it isn't a domain, return the node as it was passed in */
|
|
|
|
if (baseTypeId == typeId)
|
|
|
|
return arg;
|
2002-09-01 04:27:32 +02:00
|
|
|
|
|
|
|
/*
|
2003-02-03 22:15:45 +01:00
|
|
|
* 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.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
2003-02-03 22:15:45 +01:00
|
|
|
typmod = get_typtypmod(typeId);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (typmod >= 0)
|
2003-02-03 22:15:45 +01:00
|
|
|
arg = coerce_type_typmod(arg, baseTypeId, typmod,
|
|
|
|
COERCE_IMPLICIT_CAST,
|
|
|
|
(cformat != COERCE_IMPLICIT_CAST));
|
2002-09-01 04:27:32 +02:00
|
|
|
|
|
|
|
/*
|
2003-02-03 22:15:45 +01:00
|
|
|
* 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.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
2003-02-03 22:15:45 +01:00
|
|
|
result = makeNode(CoerceToDomain);
|
|
|
|
result->arg = (Expr *) arg;
|
|
|
|
result->resulttype = typeId;
|
|
|
|
result->resulttypmod = -1; /* currently, always -1 for domains */
|
|
|
|
result->coercionformat = cformat;
|
2002-09-01 04:27:32 +02:00
|
|
|
|
2003-02-03 22:15:45 +01:00
|
|
|
return (Node *) result;
|
2002-09-01 04:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/*
|
|
|
|
* coerce_type_typmod()
|
|
|
|
* Force a value to a particular typmod, if meaningful and possible.
|
2000-01-17 01:14:49 +01:00
|
|
|
*
|
|
|
|
* This is applied to values that are going to be stored in a relation
|
|
|
|
* (where we have an atttypmod for the column) as well as values being
|
|
|
|
* explicitly CASTed (where the typmod comes from the target type spec).
|
|
|
|
*
|
|
|
|
* The caller must have already ensured that the value is of the correct
|
|
|
|
* type, typically by applying coerce_type.
|
|
|
|
*
|
2002-09-01 00:10:48 +02:00
|
|
|
* 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
|
2002-09-01 04:27:32 +02:00
|
|
|
* needed to produce the domain value in the first place. So, no getBaseType.
|
2000-01-17 01:14:49 +01:00
|
|
|
*/
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
static Node *
|
|
|
|
coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod,
|
2003-02-03 22:15:45 +01:00
|
|
|
CoercionForm cformat, bool isExplicit)
|
2000-01-17 01:14:49 +01:00
|
|
|
{
|
2002-03-20 20:45:13 +01:00
|
|
|
Oid funcId;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
int nargs;
|
2000-01-17 01:14:49 +01:00
|
|
|
|
|
|
|
/*
|
2001-10-04 19:52:24 +02:00
|
|
|
* A negative typmod is assumed to mean that no coercion is wanted.
|
2000-01-17 01:14:49 +01:00
|
|
|
*/
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (targetTypMod < 0 || targetTypMod == exprTypmod(node))
|
2000-01-17 01:14:49 +01:00
|
|
|
return node;
|
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
funcId = find_typmod_coercion_function(targetTypeId, &nargs);
|
2002-09-01 00:10:48 +02:00
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
if (OidIsValid(funcId))
|
2000-01-17 01:14:49 +01:00
|
|
|
{
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
List *args;
|
2002-03-20 20:45:13 +01:00
|
|
|
Const *cons;
|
2000-01-17 01:14:49 +01:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* Pass given value, plus target typmod as an int4 constant */
|
2002-03-20 20:45:13 +01:00
|
|
|
cons = makeConst(INT4OID,
|
|
|
|
sizeof(int32),
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Int32GetDatum(targetTypMod),
|
2002-03-20 20:45:13 +01:00
|
|
|
false,
|
2002-11-25 22:29:42 +01:00
|
|
|
true);
|
2000-01-17 01:14:49 +01:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
args = makeList2(node, cons);
|
|
|
|
|
|
|
|
if (nargs == 3)
|
|
|
|
{
|
|
|
|
/* Pass it a boolean isExplicit parameter, too */
|
|
|
|
cons = makeConst(BOOLOID,
|
|
|
|
sizeof(bool),
|
2003-02-03 22:15:45 +01:00
|
|
|
BoolGetDatum(isExplicit),
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
false,
|
2002-11-25 22:29:42 +01:00
|
|
|
true);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
|
|
|
|
args = lappend(args, cons);
|
|
|
|
}
|
|
|
|
|
2003-04-10 04:47:46 +02:00
|
|
|
node = build_func_call(funcId, targetTypeId, args, cformat);
|
2000-01-17 01:14:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2001-06-20 00:39:12 +02:00
|
|
|
/* coerce_to_boolean()
|
|
|
|
* Coerce an argument of a construct that requires boolean input
|
2002-05-13 01:43:04 +02:00
|
|
|
* (AND, OR, NOT, etc). Also check that input is not a set.
|
2001-06-20 00:39:12 +02:00
|
|
|
*
|
2002-05-13 01:43:04 +02:00
|
|
|
* Returns the possibly-transformed node tree.
|
2003-04-30 00:13:11 +02:00
|
|
|
*
|
|
|
|
* As with coerce_type, pstate may be NULL if no special unknown-Param
|
|
|
|
* processing is wanted.
|
2001-06-20 00:39:12 +02:00
|
|
|
*/
|
2002-05-13 01:43:04 +02:00
|
|
|
Node *
|
2003-04-30 00:13:11 +02:00
|
|
|
coerce_to_boolean(ParseState *pstate, Node *node,
|
|
|
|
const char *constructName)
|
2001-06-20 00:39:12 +02:00
|
|
|
{
|
2002-05-13 01:43:04 +02:00
|
|
|
Oid inputTypeId = exprType(node);
|
2001-06-20 00:39:12 +02:00
|
|
|
|
2002-05-13 01:43:04 +02:00
|
|
|
if (inputTypeId != BOOLOID)
|
|
|
|
{
|
2003-04-30 00:13:11 +02:00
|
|
|
node = coerce_to_target_type(pstate, node, inputTypeId,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
BOOLOID, -1,
|
|
|
|
COERCION_ASSIGNMENT,
|
|
|
|
COERCE_IMPLICIT_CAST);
|
|
|
|
if (node == NULL)
|
2002-05-13 01:43:04 +02:00
|
|
|
{
|
|
|
|
/* translator: first %s is name of a SQL construct, eg WHERE */
|
|
|
|
elog(ERROR, "Argument of %s must be type boolean, not type %s",
|
|
|
|
constructName, format_type_be(inputTypeId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expression_returns_set(node))
|
|
|
|
{
|
|
|
|
/* translator: %s is name of a SQL construct, eg WHERE */
|
|
|
|
elog(ERROR, "Argument of %s must not be a set function",
|
|
|
|
constructName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2001-06-20 00:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
/* select_common_type()
|
|
|
|
* Determine the common supertype of a list of input expression types.
|
|
|
|
* This is used for determining the output type of CASE and UNION
|
|
|
|
* constructs.
|
|
|
|
*
|
2003-02-09 07:56:28 +01:00
|
|
|
* typeids is a nonempty list of type OIDs. Note that earlier items
|
2000-10-05 21:11:39 +02:00
|
|
|
* in the list will be preferred if there is doubt.
|
|
|
|
* 'context' is a phrase to use in the error message if we fail to select
|
|
|
|
* a usable type.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
select_common_type(List *typeids, const char *context)
|
|
|
|
{
|
|
|
|
Oid ptype;
|
|
|
|
CATEGORY pcategory;
|
|
|
|
List *l;
|
|
|
|
|
|
|
|
Assert(typeids != NIL);
|
2003-06-25 06:32:03 +02:00
|
|
|
ptype = getBaseType(lfirsto(typeids));
|
2000-10-05 21:11:39 +02:00
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
foreach(l, lnext(typeids))
|
|
|
|
{
|
2003-06-25 06:32:03 +02:00
|
|
|
Oid ntype = getBaseType(lfirsto(l));
|
2000-10-05 21:11:39 +02:00
|
|
|
|
|
|
|
/* move on to next one if no new information... */
|
2001-10-03 07:29:27 +02:00
|
|
|
if ((ntype != InvalidOid) && (ntype != UNKNOWNOID) && (ntype != ptype))
|
2000-10-05 21:11:39 +02:00
|
|
|
{
|
2001-10-03 07:29:27 +02:00
|
|
|
if ((ptype == InvalidOid) || ptype == UNKNOWNOID)
|
2000-10-05 21:11:39 +02:00
|
|
|
{
|
|
|
|
/* so far, only nulls so take anything... */
|
|
|
|
ptype = ntype;
|
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
}
|
|
|
|
else if (TypeCategory(ntype) != pcategory)
|
|
|
|
{
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* both types in different categories? then not much
|
|
|
|
* hope...
|
2000-10-05 21:11:39 +02:00
|
|
|
*/
|
2002-05-18 00:35:13 +02:00
|
|
|
elog(ERROR, "%s types '%s' and '%s' not matched",
|
|
|
|
context, format_type_be(ptype), format_type_be(ntype));
|
2000-10-05 21:11:39 +02:00
|
|
|
}
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
else if (!IsPreferredType(pcategory, ptype) &&
|
|
|
|
can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
|
|
|
|
!can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
|
2000-10-05 21:11:39 +02:00
|
|
|
{
|
|
|
|
/*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* take new type if can coerce to it implicitly but not the
|
|
|
|
* other way; but if we have a preferred type, stay on it.
|
2000-10-05 21:11:39 +02:00
|
|
|
*/
|
|
|
|
ptype = ntype;
|
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-11-09 05:14:32 +01:00
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* 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.
|
2000-11-09 05:14:32 +01:00
|
|
|
*/
|
|
|
|
if (ptype == UNKNOWNOID)
|
|
|
|
ptype = TEXTOID;
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
return ptype;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* coerce_to_common_type()
|
|
|
|
* Coerce an expression to the given type.
|
|
|
|
*
|
|
|
|
* This is used following select_common_type() to coerce the individual
|
|
|
|
* expressions to the desired type. 'context' is a phrase to use in the
|
|
|
|
* error message if we fail to coerce.
|
2003-04-30 00:13:11 +02:00
|
|
|
*
|
|
|
|
* As with coerce_type, pstate may be NULL if no special unknown-Param
|
|
|
|
* processing is wanted.
|
2000-10-05 21:11:39 +02:00
|
|
|
*/
|
|
|
|
Node *
|
2003-04-30 00:13:11 +02:00
|
|
|
coerce_to_common_type(ParseState *pstate, Node *node,
|
|
|
|
Oid targetTypeId, const char *context)
|
2000-10-05 21:11:39 +02:00
|
|
|
{
|
|
|
|
Oid inputTypeId = exprType(node);
|
|
|
|
|
|
|
|
if (inputTypeId == targetTypeId)
|
|
|
|
return node; /* no work */
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
|
2003-04-30 00:13:11 +02:00
|
|
|
node = coerce_type(pstate, node, inputTypeId, targetTypeId,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
|
2000-10-05 21:11:39 +02:00
|
|
|
else
|
2002-05-18 00:35:13 +02:00
|
|
|
elog(ERROR, "%s unable to convert to type %s",
|
|
|
|
context, format_type_be(targetTypeId));
|
2000-10-05 21:11:39 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/*
|
|
|
|
* check_generic_type_consistency()
|
|
|
|
* Are the actual arguments potentially compatible with a
|
|
|
|
* polymorphic function?
|
|
|
|
*
|
|
|
|
* The argument consistency rules are:
|
|
|
|
*
|
|
|
|
* 1) All arguments declared ANYARRAY must have matching datatypes,
|
|
|
|
* and must in fact be varlena arrays.
|
|
|
|
* 2) All arguments declared ANYELEMENT must have matching datatypes.
|
|
|
|
* 3) If there are arguments of both ANYELEMENT and ANYARRAY, make sure
|
|
|
|
* the actual ANYELEMENT datatype is in fact the element type for
|
|
|
|
* the actual ANYARRAY datatype.
|
|
|
|
*
|
|
|
|
* If we have UNKNOWN input (ie, an untyped literal) for any ANYELEMENT
|
|
|
|
* or ANYARRAY argument, assume it is okay.
|
|
|
|
*
|
|
|
|
* We do not elog here, but just return FALSE if a rule is violated.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
check_generic_type_consistency(Oid *actual_arg_types,
|
|
|
|
Oid *declared_arg_types,
|
|
|
|
int nargs)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
Oid elem_typeid = InvalidOid;
|
|
|
|
Oid array_typeid = InvalidOid;
|
|
|
|
Oid array_typelem;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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++)
|
|
|
|
{
|
|
|
|
Oid actual_type = actual_arg_types[j];
|
|
|
|
|
|
|
|
if (declared_arg_types[j] == ANYELEMENTOID)
|
|
|
|
{
|
|
|
|
if (actual_type == UNKNOWNOID)
|
|
|
|
continue;
|
|
|
|
if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
|
|
|
|
return false;
|
|
|
|
elem_typeid = actual_type;
|
|
|
|
}
|
|
|
|
else if (declared_arg_types[j] == ANYARRAYOID)
|
|
|
|
{
|
|
|
|
if (actual_type == UNKNOWNOID)
|
|
|
|
continue;
|
|
|
|
if (OidIsValid(array_typeid) && actual_type != array_typeid)
|
|
|
|
return false;
|
|
|
|
array_typeid = actual_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the element type based on the array type, if we have one */
|
|
|
|
if (OidIsValid(array_typeid))
|
|
|
|
{
|
|
|
|
array_typelem = get_element_type(array_typeid);
|
|
|
|
if (!OidIsValid(array_typelem))
|
|
|
|
return false; /* should be an array, but isn't */
|
|
|
|
|
|
|
|
if (!OidIsValid(elem_typeid))
|
|
|
|
{
|
|
|
|
/* if we don't have an element type yet, use the one we just got */
|
|
|
|
elem_typeid = array_typelem;
|
|
|
|
}
|
|
|
|
else if (array_typelem != elem_typeid)
|
|
|
|
{
|
|
|
|
/* otherwise, they better match */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks valid */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* enforce_generic_type_consistency()
|
|
|
|
* Make sure a polymorphic function is legally callable, and
|
|
|
|
* deduce actual argument and result types.
|
|
|
|
*
|
|
|
|
* If ANYARRAY or ANYELEMENT is used for a function's arguments or
|
|
|
|
* return type, we make sure the actual data types are consistent with
|
|
|
|
* each other. The argument consistency rules are shown above for
|
|
|
|
* check_generic_type_consistency().
|
|
|
|
*
|
|
|
|
* If we have UNKNOWN input (ie, an untyped literal) for any ANYELEMENT
|
|
|
|
* or ANYARRAY argument, we attempt to deduce the actual type it should
|
|
|
|
* have. If successful, we alter that position of declared_arg_types[]
|
|
|
|
* so that make_fn_arguments will coerce the literal to the right thing.
|
|
|
|
*
|
|
|
|
* Rules are applied to the function's return type (possibly altering it)
|
|
|
|
* if it is declared ANYARRAY or ANYELEMENT:
|
|
|
|
*
|
|
|
|
* 1) If return type is ANYARRAY, and any argument is ANYARRAY, use the
|
|
|
|
* argument's actual type as the function's return type.
|
|
|
|
* 2) If return type is ANYARRAY, no argument is ANYARRAY, but any argument
|
|
|
|
* is ANYELEMENT, use the actual type of the argument to determine
|
|
|
|
* the function's return type, i.e. the element type's corresponding
|
|
|
|
* array type.
|
|
|
|
* 3) If return type is ANYARRAY, no argument is ANYARRAY or ANYELEMENT,
|
|
|
|
* generate an ERROR. This condition is prevented by CREATE FUNCTION
|
|
|
|
* and is therefore not expected here.
|
|
|
|
* 4) If return type is ANYELEMENT, and any argument is ANYELEMENT, use the
|
|
|
|
* argument's actual type as the function's return type.
|
|
|
|
* 5) If return type is ANYELEMENT, no argument is ANYELEMENT, but any
|
|
|
|
* argument is ANYARRAY, use the actual type of the argument to determine
|
|
|
|
* the function's return type, i.e. the array type's corresponding
|
|
|
|
* element type.
|
|
|
|
* 6) If return type is ANYELEMENT, no argument is ANYARRAY or ANYELEMENT,
|
|
|
|
* generate an ERROR. This condition is prevented by CREATE FUNCTION
|
|
|
|
* and is therefore not expected here.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
enforce_generic_type_consistency(Oid *actual_arg_types,
|
|
|
|
Oid *declared_arg_types,
|
|
|
|
int nargs,
|
|
|
|
Oid rettype)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
bool have_generics = false;
|
|
|
|
bool have_unknowns = false;
|
|
|
|
Oid elem_typeid = InvalidOid;
|
|
|
|
Oid array_typeid = InvalidOid;
|
|
|
|
Oid array_typelem = InvalidOid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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++)
|
|
|
|
{
|
|
|
|
Oid actual_type = actual_arg_types[j];
|
|
|
|
|
|
|
|
if (declared_arg_types[j] == ANYELEMENTOID)
|
|
|
|
{
|
|
|
|
have_generics = true;
|
|
|
|
if (actual_type == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
have_unknowns = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
|
|
|
|
elog(ERROR, "Arguments declared ANYELEMENT are not all alike: %s vs %s",
|
|
|
|
format_type_be(elem_typeid),
|
|
|
|
format_type_be(actual_type));
|
|
|
|
elem_typeid = actual_type;
|
|
|
|
}
|
|
|
|
else if (declared_arg_types[j] == ANYARRAYOID)
|
|
|
|
{
|
|
|
|
have_generics = true;
|
|
|
|
if (actual_type == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
have_unknowns = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (OidIsValid(array_typeid) && actual_type != array_typeid)
|
|
|
|
elog(ERROR, "Arguments declared ANYARRAY are not all alike: %s vs %s",
|
|
|
|
format_type_be(array_typeid),
|
|
|
|
format_type_be(actual_type));
|
|
|
|
array_typeid = actual_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fast Track: if none of the arguments are ANYARRAY or ANYELEMENT,
|
|
|
|
* return the unmodified rettype.
|
|
|
|
*/
|
|
|
|
if (!have_generics)
|
|
|
|
return rettype;
|
|
|
|
|
|
|
|
/* Get the element type based on the array type, if we have one */
|
|
|
|
if (OidIsValid(array_typeid))
|
|
|
|
{
|
2003-06-25 23:30:34 +02:00
|
|
|
array_typelem = get_element_type(array_typeid);
|
2003-04-09 01:20:04 +02:00
|
|
|
if (!OidIsValid(array_typelem))
|
|
|
|
elog(ERROR, "Argument declared ANYARRAY is not an array: %s",
|
|
|
|
format_type_be(array_typeid));
|
|
|
|
|
|
|
|
if (!OidIsValid(elem_typeid))
|
|
|
|
{
|
|
|
|
/* if we don't have an element type yet, use the one we just got */
|
|
|
|
elem_typeid = array_typelem;
|
|
|
|
}
|
|
|
|
else if (array_typelem != elem_typeid)
|
|
|
|
{
|
|
|
|
/* otherwise, they better match */
|
|
|
|
elog(ERROR, "Argument declared ANYARRAY is not consistent with "
|
|
|
|
"argument declared ANYELEMENT: %s vs %s",
|
|
|
|
format_type_be(array_typeid),
|
|
|
|
format_type_be(elem_typeid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!OidIsValid(elem_typeid))
|
|
|
|
{
|
|
|
|
/* Only way to get here is if all the generic args are UNKNOWN */
|
|
|
|
elog(ERROR, "Cannot determine ANYARRAY/ANYELEMENT type because input is UNKNOWN");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we had any unknown inputs, re-scan to assign correct types
|
|
|
|
*/
|
|
|
|
if (have_unknowns)
|
|
|
|
{
|
|
|
|
for (j = 0; j < nargs; j++)
|
|
|
|
{
|
|
|
|
Oid actual_type = actual_arg_types[j];
|
|
|
|
|
|
|
|
if (actual_type != UNKNOWNOID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (declared_arg_types[j] == ANYELEMENTOID)
|
|
|
|
{
|
|
|
|
declared_arg_types[j] = elem_typeid;
|
|
|
|
}
|
|
|
|
else if (declared_arg_types[j] == ANYARRAYOID)
|
|
|
|
{
|
|
|
|
if (!OidIsValid(array_typeid))
|
|
|
|
{
|
|
|
|
array_typeid = get_array_type(elem_typeid);
|
|
|
|
if (!OidIsValid(array_typeid))
|
|
|
|
elog(ERROR, "Cannot find array type for datatype %s",
|
|
|
|
format_type_be(elem_typeid));
|
|
|
|
}
|
|
|
|
declared_arg_types[j] = array_typeid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we return ANYARRAYOID use the appropriate argument type */
|
|
|
|
if (rettype == ANYARRAYOID)
|
|
|
|
{
|
|
|
|
if (!OidIsValid(array_typeid))
|
|
|
|
{
|
2003-06-25 23:30:34 +02:00
|
|
|
array_typeid = get_array_type(elem_typeid);
|
2003-04-09 01:20:04 +02:00
|
|
|
if (!OidIsValid(array_typeid))
|
|
|
|
elog(ERROR, "Cannot find array type for datatype %s",
|
|
|
|
format_type_be(elem_typeid));
|
|
|
|
}
|
|
|
|
return array_typeid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we return ANYELEMENTOID use the appropriate argument type */
|
|
|
|
if (rettype == ANYELEMENTOID)
|
|
|
|
return elem_typeid;
|
|
|
|
|
|
|
|
/* we don't return a generic type; send back the original return type */
|
|
|
|
return rettype;
|
|
|
|
}
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
/* TypeCategory()
|
2003-05-26 02:11:29 +02:00
|
|
|
* Assign a category to the specified type OID.
|
|
|
|
*
|
|
|
|
* NB: this must not return INVALID_TYPE.
|
|
|
|
*
|
2001-10-03 07:29:27 +02:00
|
|
|
* XXX This should be moved to system catalog lookups
|
|
|
|
* to allow for better type extensibility.
|
|
|
|
* - thomas 2001-09-30
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
|
|
|
CATEGORY
|
|
|
|
TypeCategory(Oid inType)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
CATEGORY result;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
switch (inType)
|
|
|
|
{
|
|
|
|
case (BOOLOID):
|
|
|
|
result = BOOLEAN_TYPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (CHAROID):
|
2000-03-12 00:19:50 +01:00
|
|
|
case (NAMEOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (BPCHAROID):
|
|
|
|
case (VARCHAROID):
|
|
|
|
case (TEXTOID):
|
|
|
|
result = STRING_TYPE;
|
|
|
|
break;
|
|
|
|
|
2001-05-22 18:37:17 +02:00
|
|
|
case (BITOID):
|
2000-04-08 21:29:40 +02:00
|
|
|
case (VARBITOID):
|
2000-11-17 20:57:48 +01:00
|
|
|
result = BITSTRING_TYPE;
|
2000-04-08 21:29:40 +02:00
|
|
|
break;
|
|
|
|
|
1998-08-14 18:07:00 +02:00
|
|
|
case (OIDOID):
|
2000-02-27 19:54:43 +01:00
|
|
|
case (REGPROCOID):
|
2002-04-25 04:56:56 +02:00
|
|
|
case (REGPROCEDUREOID):
|
|
|
|
case (REGOPEROID):
|
|
|
|
case (REGOPERATOROID):
|
|
|
|
case (REGCLASSOID):
|
|
|
|
case (REGTYPEOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (INT2OID):
|
|
|
|
case (INT4OID):
|
1998-07-08 16:04:11 +02:00
|
|
|
case (INT8OID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (FLOAT4OID):
|
|
|
|
case (FLOAT8OID):
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 18:26:26 +01:00
|
|
|
case (NUMERICOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (CASHOID):
|
|
|
|
result = NUMERIC_TYPE;
|
|
|
|
break;
|
|
|
|
|
2000-02-20 07:28:42 +01:00
|
|
|
case (DATEOID):
|
|
|
|
case (TIMEOID):
|
2000-03-15 00:06:59 +01:00
|
|
|
case (TIMETZOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (ABSTIMEOID):
|
|
|
|
case (TIMESTAMPOID):
|
2001-09-28 10:09:14 +02:00
|
|
|
case (TIMESTAMPTZOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
result = DATETIME_TYPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (RELTIMEOID):
|
2000-02-20 07:28:42 +01:00
|
|
|
case (TINTERVALOID):
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 18:26:26 +01:00
|
|
|
case (INTERVALOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
result = TIMESPAN_TYPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (POINTOID):
|
|
|
|
case (LSEGOID):
|
|
|
|
case (PATHOID):
|
2000-03-12 00:19:50 +01:00
|
|
|
case (BOXOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
case (POLYGONOID):
|
2000-03-12 00:19:50 +01:00
|
|
|
case (LINEOID):
|
|
|
|
case (CIRCLEOID):
|
1998-05-10 01:31:34 +02:00
|
|
|
result = GEOMETRIC_TYPE;
|
|
|
|
break;
|
|
|
|
|
1998-10-22 15:51:07 +02:00
|
|
|
case (INETOID):
|
|
|
|
case (CIDROID):
|
|
|
|
result = NETWORK_TYPE;
|
|
|
|
break;
|
|
|
|
|
2000-03-12 00:19:50 +01:00
|
|
|
case (UNKNOWNOID):
|
|
|
|
case (InvalidOid):
|
|
|
|
result = UNKNOWN_TYPE;
|
|
|
|
break;
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
case (RECORDOID):
|
|
|
|
case (CSTRINGOID):
|
|
|
|
case (ANYOID):
|
|
|
|
case (ANYARRAYOID):
|
|
|
|
case (VOIDOID):
|
|
|
|
case (TRIGGEROID):
|
|
|
|
case (LANGUAGE_HANDLEROID):
|
|
|
|
case (INTERNALOID):
|
|
|
|
case (OPAQUEOID):
|
|
|
|
case (ANYELEMENTOID):
|
|
|
|
result = GENERIC_TYPE;
|
|
|
|
break;
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
default:
|
|
|
|
result = USER_TYPE;
|
|
|
|
break;
|
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return result;
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* TypeCategory() */
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* IsPreferredType()
|
2003-05-26 02:11:29 +02:00
|
|
|
* Check if this type is a preferred type for the given category.
|
|
|
|
*
|
|
|
|
* If category is INVALID_TYPE, then we'll return TRUE for preferred types
|
|
|
|
* of any category; otherwise, only for preferred types of that category.
|
|
|
|
*
|
2001-10-03 07:29:27 +02:00
|
|
|
* XXX This should be moved to system catalog lookups
|
|
|
|
* to allow for better type extensibility.
|
|
|
|
* - thomas 2001-09-30
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsPreferredType(CATEGORY category, Oid type)
|
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
Oid preftype;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
if (category == INVALID_TYPE)
|
|
|
|
category = TypeCategory(type);
|
|
|
|
else if (category != TypeCategory(type))
|
|
|
|
return false;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
/*
|
|
|
|
* This switch should agree with TypeCategory(), above. Note that
|
|
|
|
* at this point, category certainly matches the type.
|
|
|
|
*/
|
1998-05-10 01:31:34 +02:00
|
|
|
switch (category)
|
|
|
|
{
|
2003-04-09 01:20:04 +02:00
|
|
|
case (UNKNOWN_TYPE):
|
|
|
|
case (GENERIC_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = UNKNOWNOID;
|
2003-04-09 01:20:04 +02:00
|
|
|
break;
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
case (BOOLEAN_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = BOOLOID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case (STRING_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = TEXTOID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
2000-11-17 20:57:48 +01:00
|
|
|
case (BITSTRING_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = VARBITOID;
|
2000-11-17 20:57:48 +01:00
|
|
|
break;
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
case (NUMERIC_TYPE):
|
2002-09-01 04:27:32 +02:00
|
|
|
if (type == OIDOID ||
|
|
|
|
type == REGPROCOID ||
|
|
|
|
type == REGPROCEDUREOID ||
|
|
|
|
type == REGOPEROID ||
|
|
|
|
type == REGOPERATOROID ||
|
|
|
|
type == REGCLASSOID ||
|
|
|
|
type == REGTYPEOID)
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = OIDOID;
|
1998-08-14 18:07:00 +02:00
|
|
|
else
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = FLOAT8OID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case (DATETIME_TYPE):
|
2001-09-28 10:09:14 +02:00
|
|
|
if (type == DATEOID)
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = TIMESTAMPOID;
|
2001-09-28 10:09:14 +02:00
|
|
|
else
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = TIMESTAMPTZOID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case (TIMESPAN_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = INTERVALOID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
case (GEOMETRIC_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = type;
|
2003-04-09 01:20:04 +02:00
|
|
|
break;
|
|
|
|
|
1998-10-22 15:51:07 +02:00
|
|
|
case (NETWORK_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = INETOID;
|
1998-10-22 15:51:07 +02:00
|
|
|
break;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
case (USER_TYPE):
|
2003-05-26 02:11:29 +02:00
|
|
|
preftype = type;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-05-26 02:11:29 +02:00
|
|
|
elog(ERROR, "IsPreferredType: unknown category");
|
|
|
|
preftype = UNKNOWNOID;
|
1998-05-10 01:31:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-05-26 02:11:29 +02:00
|
|
|
|
|
|
|
return (type == preftype);
|
|
|
|
} /* IsPreferredType() */
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* IsBinaryCoercible()
|
|
|
|
* Check if srctype is binary-coercible to targettype.
|
2002-03-20 20:45:13 +01:00
|
|
|
*
|
2002-09-01 04:27:32 +02:00
|
|
|
* This notion allows us to cheat and directly exchange values without
|
2003-05-26 02:11:29 +02:00
|
|
|
* going through the trouble of calling a conversion function. Note that
|
|
|
|
* in general, this should only be an implementation shortcut. Before 7.4,
|
|
|
|
* this was also used as a heuristic for resolving overloaded functions and
|
|
|
|
* operators, but that's basically a bad idea.
|
2002-03-20 20:45:13 +01:00
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* As of 7.3, binary coercibility isn't hardwired into the code anymore.
|
|
|
|
* We consider two types binary-coercible if there is an implicitly
|
2003-05-26 02:11:29 +02:00
|
|
|
* invokable, no-function-needed pg_cast entry. Also, a domain is always
|
|
|
|
* binary-coercible to its base type, though *not* vice versa (in the other
|
|
|
|
* direction, one must apply domain constraint checks before accepting the
|
|
|
|
* value as legitimate).
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
*
|
|
|
|
* This function replaces IsBinaryCompatible(), which was an inherently
|
|
|
|
* symmetric test. Since the pg_cast entries aren't necessarily symmetric,
|
|
|
|
* the order of the operands is now significant.
|
2002-09-01 04:27:32 +02:00
|
|
|
*/
|
|
|
|
bool
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
IsBinaryCoercible(Oid srctype, Oid targettype)
|
2002-09-01 04:27:32 +02:00
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_cast castForm;
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
/* Fast path if same type */
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (srctype == targettype)
|
2002-09-01 04:27:32 +02:00
|
|
|
return true;
|
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
/* If srctype is a domain, reduce to its base type */
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (OidIsValid(srctype))
|
|
|
|
srctype = getBaseType(srctype);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
/* Somewhat-fast path for domain -> base type case */
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (srctype == targettype)
|
2002-09-01 04:27:32 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Else look in pg_cast */
|
|
|
|
tuple = SearchSysCache(CASTSOURCETARGET,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
ObjectIdGetDatum(srctype),
|
|
|
|
ObjectIdGetDatum(targettype),
|
2002-09-01 04:27:32 +02:00
|
|
|
0, 0);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
return false; /* no cast */
|
|
|
|
castForm = (Form_pg_cast) GETSTRUCT(tuple);
|
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
result = (castForm->castfunc == InvalidOid &&
|
|
|
|
castForm->castcontext == COERCION_CODE_IMPLICIT);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_coercion_pathway
|
|
|
|
* Look for a coercion pathway between two types.
|
2002-03-20 20:45:13 +01:00
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* ccontext determines the set of available casts.
|
|
|
|
*
|
|
|
|
* If we find a suitable entry in pg_cast, return TRUE, and set *funcid
|
2003-05-26 02:11:29 +02:00
|
|
|
* to the castfunc value, which may be InvalidOid for a binary-compatible
|
|
|
|
* coercion.
|
|
|
|
*
|
|
|
|
* NOTE: *funcid == InvalidOid does not necessarily mean that no work is
|
|
|
|
* needed to do the coercion; if the target is a domain then we may need to
|
|
|
|
* apply domain constraint checking. If you want to check for a zero-effort
|
|
|
|
* conversion then use IsBinaryCoercible().
|
2002-03-19 03:18:25 +01:00
|
|
|
*/
|
2002-10-25 00:09:00 +02:00
|
|
|
bool
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId,
|
|
|
|
CoercionContext ccontext,
|
2002-09-01 04:27:32 +02:00
|
|
|
Oid *funcid)
|
2002-07-19 01:11:32 +02:00
|
|
|
{
|
2002-09-01 04:27:32 +02:00
|
|
|
bool result = false;
|
2002-07-19 01:11:32 +02:00
|
|
|
HeapTuple tuple;
|
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
*funcid = InvalidOid;
|
|
|
|
|
|
|
|
/* Perhaps the types are domains; if so, look at their base types */
|
|
|
|
if (OidIsValid(sourceTypeId))
|
|
|
|
sourceTypeId = getBaseType(sourceTypeId);
|
|
|
|
if (OidIsValid(targetTypeId))
|
|
|
|
targetTypeId = getBaseType(targetTypeId);
|
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
/* Domains are always coercible to and from their base type */
|
2002-09-01 04:27:32 +02:00
|
|
|
if (sourceTypeId == targetTypeId)
|
|
|
|
return true;
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/* Look in pg_cast */
|
2002-07-19 01:11:32 +02:00
|
|
|
tuple = SearchSysCache(CASTSOURCETARGET,
|
|
|
|
ObjectIdGetDatum(sourceTypeId),
|
|
|
|
ObjectIdGetDatum(targetTypeId),
|
|
|
|
0, 0);
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(tuple))
|
|
|
|
{
|
2002-09-01 04:27:32 +02:00
|
|
|
Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
CoercionContext castcontext;
|
|
|
|
|
|
|
|
/* convert char value for castcontext to CoercionContext enum */
|
|
|
|
switch (castForm->castcontext)
|
|
|
|
{
|
|
|
|
case COERCION_CODE_IMPLICIT:
|
|
|
|
castcontext = COERCION_IMPLICIT;
|
|
|
|
break;
|
|
|
|
case COERCION_CODE_ASSIGNMENT:
|
|
|
|
castcontext = COERCION_ASSIGNMENT;
|
|
|
|
break;
|
|
|
|
case COERCION_CODE_EXPLICIT:
|
|
|
|
castcontext = COERCION_EXPLICIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "find_coercion_pathway: bogus castcontext %c",
|
|
|
|
castForm->castcontext);
|
|
|
|
castcontext = 0; /* keep compiler quiet */
|
|
|
|
break;
|
|
|
|
}
|
2002-07-19 01:11:32 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* Rely on ordering of enum for correct behavior here */
|
|
|
|
if (ccontext >= castcontext)
|
2002-09-01 04:27:32 +02:00
|
|
|
{
|
|
|
|
*funcid = castForm->castfunc;
|
|
|
|
result = true;
|
|
|
|
}
|
2002-07-19 01:11:32 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
}
|
2003-04-09 01:20:04 +02:00
|
|
|
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().
|
|
|
|
*/
|
|
|
|
Oid targetElemType;
|
|
|
|
Oid sourceElemType;
|
|
|
|
Oid elemfuncid;
|
|
|
|
|
|
|
|
if ((targetElemType = get_element_type(targetTypeId)) != InvalidOid &&
|
|
|
|
(sourceElemType = get_element_type(sourceTypeId)) != InvalidOid)
|
|
|
|
{
|
|
|
|
if (find_coercion_pathway(targetElemType, sourceElemType,
|
|
|
|
ccontext, &elemfuncid))
|
|
|
|
{
|
|
|
|
*funcid = F_ARRAY_TYPE_COERCE;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-19 01:11:32 +02:00
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
return result;
|
2002-07-19 01:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-01 04:27:32 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* Alternatively, the length-coercing function may have the signature
|
|
|
|
* (targettype, int4, bool). On success, *nargs is set to report which
|
|
|
|
* signature we found.
|
|
|
|
*
|
2002-09-01 04:27:32 +02:00
|
|
|
* "bpchar" (ie, char(N)) and "numeric" are examples of such types.
|
|
|
|
*
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
* If the given type is a varlena array type, we do not look for a coercion
|
|
|
|
* 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.
|
|
|
|
*
|
2002-09-01 04:27:32 +02:00
|
|
|
* 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 ...
|
|
|
|
*/
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Oid
|
|
|
|
find_typmod_coercion_function(Oid typeId, int *nargs)
|
2002-03-19 03:18:25 +01:00
|
|
|
{
|
2002-04-11 22:00:18 +02:00
|
|
|
Oid funcid = InvalidOid;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
bool isArray = false;
|
2002-04-09 22:35:55 +02:00
|
|
|
Type targetType;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
Form_pg_type typeForm;
|
2002-04-09 22:35:55 +02:00
|
|
|
char *typname;
|
|
|
|
Oid typnamespace;
|
2002-03-20 20:45:13 +01:00
|
|
|
Oid oid_array[FUNC_MAX_ARGS];
|
|
|
|
HeapTuple ftup;
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-07-19 01:11:32 +02:00
|
|
|
targetType = typeidType(typeId);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
typeForm = (Form_pg_type) GETSTRUCT(targetType);
|
2002-04-09 22:35:55 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/* Check for a varlena array type (and not a domain) */
|
|
|
|
if (typeForm->typelem != InvalidOid &&
|
|
|
|
typeForm->typlen == -1 &&
|
|
|
|
typeForm->typtype != 'd')
|
|
|
|
{
|
|
|
|
/* Yes, switch our attention to the element type */
|
|
|
|
typeId = typeForm->typelem;
|
|
|
|
ReleaseSysCache(targetType);
|
|
|
|
targetType = typeidType(typeId);
|
|
|
|
typeForm = (Form_pg_type) GETSTRUCT(targetType);
|
|
|
|
isArray = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) */
|
2002-03-20 20:45:13 +01:00
|
|
|
MemSet(oid_array, 0, FUNC_MAX_ARGS * sizeof(Oid));
|
2002-07-19 01:11:32 +02:00
|
|
|
oid_array[0] = typeId;
|
|
|
|
oid_array[1] = INT4OID;
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
*nargs = 2;
|
2002-03-20 20:45:13 +01:00
|
|
|
|
2002-04-09 22:35:55 +02:00
|
|
|
ftup = SearchSysCache(PROCNAMENSP,
|
|
|
|
CStringGetDatum(typname),
|
2002-07-19 01:11:32 +02:00
|
|
|
Int16GetDatum(2),
|
2002-03-20 20:45:13 +01:00
|
|
|
PointerGetDatum(oid_array),
|
2002-04-09 22:35:55 +02:00
|
|
|
ObjectIdGetDatum(typnamespace));
|
2002-04-11 22:00:18 +02:00
|
|
|
if (HeapTupleIsValid(ftup))
|
2002-03-20 20:45:13 +01:00
|
|
|
{
|
2002-04-11 22:00:18 +02:00
|
|
|
Form_pg_proc pform = (Form_pg_proc) GETSTRUCT(ftup);
|
|
|
|
|
|
|
|
/* Make sure the function's result type is as expected */
|
2002-07-19 01:11:32 +02:00
|
|
|
if (pform->prorettype == typeId && !pform->proretset &&
|
2002-04-11 22:00:18 +02:00
|
|
|
!pform->proisagg)
|
|
|
|
{
|
2002-07-19 01:11:32 +02:00
|
|
|
/* Okay to use it */
|
2002-07-20 07:29:01 +02:00
|
|
|
funcid = HeapTupleGetOid(ftup);
|
2002-04-11 22:00:18 +02:00
|
|
|
}
|
2002-03-20 20:45:13 +01:00
|
|
|
ReleaseSysCache(ftup);
|
|
|
|
}
|
2002-04-11 22:00:18 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (!OidIsValid(funcid))
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-09 22:35:55 +02:00
|
|
|
ReleaseSysCache(targetType);
|
2002-09-01 04:27:32 +02:00
|
|
|
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (isArray && OidIsValid(funcid))
|
|
|
|
{
|
|
|
|
funcid = F_ARRAY_LENGTH_COERCE;
|
|
|
|
*nargs = 3;
|
|
|
|
}
|
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
return funcid;
|
|
|
|
}
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
/*
|
|
|
|
* Build an expression tree representing a function call.
|
|
|
|
*
|
|
|
|
* The argument expressions must have been transformed already.
|
|
|
|
*/
|
|
|
|
static Node *
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
build_func_call(Oid funcid, Oid rettype, List *args, CoercionForm fformat)
|
2002-03-20 20:45:13 +01:00
|
|
|
{
|
2002-12-12 16:49:42 +01:00
|
|
|
FuncExpr *funcexpr;
|
|
|
|
|
|
|
|
funcexpr = makeNode(FuncExpr);
|
|
|
|
funcexpr->funcid = funcid;
|
|
|
|
funcexpr->funcresulttype = rettype;
|
|
|
|
funcexpr->funcretset = false; /* only possible case here */
|
|
|
|
funcexpr->funcformat = fformat;
|
|
|
|
funcexpr->args = args;
|
|
|
|
|
|
|
|
return (Node *) funcexpr;
|
2002-03-19 03:18:25 +01:00
|
|
|
}
|