1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Represent type-specific length coercion functions as pg_cast entries,

eliminating the former hard-wired convention about their names.  Allow
pg_cast entries to represent both type coercion and length coercion in
a single step --- this is represented by a function that takes an
extra typmod argument, just like a length coercion function.  This
nicely merges the type and length coercion mechanisms into something
at least a little cleaner than we had before.  Make use of the single-
coercion-step behavior to fix integer-to-bit coercion so that coercing
to bit(n) yields the rightmost n bits of the integer instead of the
leftmost n bits.  This should fix recurrent complaints about the odd
behavior of this coercion.  Clean up the documentation of the bit string
functions, and try to put it where people might actually find it.
Also, get rid of the unreliable heuristics in ruleutils.c about whether
to display nested coercion steps; instead require parse_coerce.c to
label them properly in the first place.
This commit is contained in:
Tom Lane
2004-06-16 01:27:00 +00:00
parent 8e7349b738
commit d70a42e642
23 changed files with 794 additions and 471 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.104 2004/06/08 20:28:21 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.105 2004/06/16 01:26:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -109,6 +109,11 @@ static void array_insert_slice(int ndim, int *dim, int *lb,
int *st, int *endp, char *srcPtr,
int typlen, bool typbyval, char typalign);
static int array_cmp(FunctionCallInfo fcinfo);
static Datum array_type_length_coerce_internal(ArrayType *src,
int32 desttypmod,
bool isExplicit,
FmgrInfo *fmgr_info);
/*---------------------------------------------------------------------
* array_in :
@@ -1174,82 +1179,6 @@ array_send(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*-------------------------------------------------------------------------
* array_length_coerce :
* Apply the element type's length-coercion routine to each element
* of the given array.
*-------------------------------------------------------------------------
*/
Datum
array_length_coerce(PG_FUNCTION_ARGS)
{
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
int32 len = PG_GETARG_INT32(1);
bool isExplicit = PG_GETARG_BOOL(2);
FmgrInfo *fmgr_info = fcinfo->flinfo;
typedef struct
{
Oid elemtype;
FmgrInfo coerce_finfo;
} alc_extra;
alc_extra *my_extra;
FunctionCallInfoData locfcinfo;
/* If no typmod is provided, shortcircuit the whole thing */
if (len < 0)
PG_RETURN_ARRAYTYPE_P(v);
/*
* We arrange to look up the element type's coercion function only
* once per series of calls, assuming the element type doesn't change
* underneath us.
*/
my_extra = (alc_extra *) fmgr_info->fn_extra;
if (my_extra == NULL)
{
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
sizeof(alc_extra));
my_extra = (alc_extra *) fmgr_info->fn_extra;
my_extra->elemtype = InvalidOid;
}
if (my_extra->elemtype != ARR_ELEMTYPE(v))
{
Oid funcId;
int nargs;
funcId = find_typmod_coercion_function(ARR_ELEMTYPE(v), &nargs);
if (OidIsValid(funcId))
fmgr_info_cxt(funcId, &my_extra->coerce_finfo, fmgr_info->fn_mcxt);
else
my_extra->coerce_finfo.fn_oid = InvalidOid;
my_extra->elemtype = ARR_ELEMTYPE(v);
}
/*
* If we didn't find a coercion function, return the array unmodified
* (this should not happen in the normal course of things, but might
* happen if this function is called manually).
*/
if (my_extra->coerce_finfo.fn_oid == InvalidOid)
PG_RETURN_ARRAYTYPE_P(v);
/*
* Use array_map to apply the function to each array element.
*
* Note: we pass isExplicit whether or not the function wants it ...
*/
MemSet(&locfcinfo, 0, sizeof(locfcinfo));
locfcinfo.flinfo = &my_extra->coerce_finfo;
locfcinfo.nargs = 3;
locfcinfo.arg[0] = PointerGetDatum(v);
locfcinfo.arg[1] = Int32GetDatum(len);
locfcinfo.arg[2] = BoolGetDatum(isExplicit);
return array_map(&locfcinfo, ARR_ELEMTYPE(v), ARR_ELEMTYPE(v));
}
/*-----------------------------------------------------------------------------
* array_dims :
* returns the dimensions of the array pointed to by "v", as a "text"
@@ -2879,6 +2808,9 @@ array_insert_slice(int ndim,
* array_type_coerce -- allow explicit or assignment coercion from
* one array type to another.
*
* array_type_length_coerce -- the same, for cases where both type and length
* coercion are done by a single function on the element type.
*
* Caller should have already verified that the source element type can be
* coerced into the target element type.
*/
@@ -2886,8 +2818,30 @@ Datum
array_type_coerce(PG_FUNCTION_ARGS)
{
ArrayType *src = PG_GETARG_ARRAYTYPE_P(0);
Oid src_elem_type = ARR_ELEMTYPE(src);
FmgrInfo *fmgr_info = fcinfo->flinfo;
return array_type_length_coerce_internal(src, -1, false, fmgr_info);
}
Datum
array_type_length_coerce(PG_FUNCTION_ARGS)
{
ArrayType *src = PG_GETARG_ARRAYTYPE_P(0);
int32 desttypmod = PG_GETARG_INT32(1);
bool isExplicit = PG_GETARG_BOOL(2);
FmgrInfo *fmgr_info = fcinfo->flinfo;
return array_type_length_coerce_internal(src, desttypmod,
isExplicit, fmgr_info);
}
static Datum
array_type_length_coerce_internal(ArrayType *src,
int32 desttypmod,
bool isExplicit,
FmgrInfo *fmgr_info)
{
Oid src_elem_type = ARR_ELEMTYPE(src);
typedef struct
{
Oid srctype;
@@ -2946,7 +2900,8 @@ array_type_coerce(PG_FUNCTION_ARGS)
{
/* should never happen, but check anyway */
elog(ERROR, "no conversion function from %s to %s",
format_type_be(src_elem_type), format_type_be(tgt_elem_type));
format_type_be(src_elem_type),
format_type_be(tgt_elem_type));
}
if (OidIsValid(funcId))
fmgr_info_cxt(funcId, &my_extra->coerce_finfo, fmgr_info->fn_mcxt);
@@ -2962,23 +2917,103 @@ array_type_coerce(PG_FUNCTION_ARGS)
*/
if (my_extra->coerce_finfo.fn_oid == InvalidOid)
{
ArrayType *result = DatumGetArrayTypePCopy(PG_GETARG_DATUM(0));
ArrayType *result;
result = (ArrayType *) DatumGetPointer(datumCopy(PointerGetDatum(src),
false, -1));
ARR_ELEMTYPE(result) = my_extra->desttype;
PG_RETURN_ARRAYTYPE_P(result);
}
/*
* Use array_map to apply the function to each array element.
*
* We pass on the desttypmod and isExplicit flags whether or not the
* function wants them.
*/
MemSet(&locfcinfo, 0, sizeof(locfcinfo));
locfcinfo.flinfo = &my_extra->coerce_finfo;
locfcinfo.nargs = 1;
locfcinfo.nargs = 3;
locfcinfo.arg[0] = PointerGetDatum(src);
locfcinfo.arg[1] = Int32GetDatum(desttypmod);
locfcinfo.arg[2] = BoolGetDatum(isExplicit);
return array_map(&locfcinfo, my_extra->srctype, my_extra->desttype);
}
/*
* array_length_coerce -- apply the element type's length-coercion routine
* to each element of the given array.
*/
Datum
array_length_coerce(PG_FUNCTION_ARGS)
{
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
int32 desttypmod = PG_GETARG_INT32(1);
bool isExplicit = PG_GETARG_BOOL(2);
FmgrInfo *fmgr_info = fcinfo->flinfo;
typedef struct
{
Oid elemtype;
FmgrInfo coerce_finfo;
} alc_extra;
alc_extra *my_extra;
FunctionCallInfoData locfcinfo;
/* If no typmod is provided, shortcircuit the whole thing */
if (desttypmod < 0)
PG_RETURN_ARRAYTYPE_P(v);
/*
* We arrange to look up the element type's coercion function only
* once per series of calls, assuming the element type doesn't change
* underneath us.
*/
my_extra = (alc_extra *) fmgr_info->fn_extra;
if (my_extra == NULL)
{
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
sizeof(alc_extra));
my_extra = (alc_extra *) fmgr_info->fn_extra;
my_extra->elemtype = InvalidOid;
}
if (my_extra->elemtype != ARR_ELEMTYPE(v))
{
Oid funcId;
funcId = find_typmod_coercion_function(ARR_ELEMTYPE(v));
if (OidIsValid(funcId))
fmgr_info_cxt(funcId, &my_extra->coerce_finfo, fmgr_info->fn_mcxt);
else
my_extra->coerce_finfo.fn_oid = InvalidOid;
my_extra->elemtype = ARR_ELEMTYPE(v);
}
/*
* If we didn't find a coercion function, return the array unmodified
* (this should not happen in the normal course of things, but might
* happen if this function is called manually).
*/
if (my_extra->coerce_finfo.fn_oid == InvalidOid)
PG_RETURN_ARRAYTYPE_P(v);
/*
* Use array_map to apply the function to each array element.
*
* Note: we pass isExplicit whether or not the function wants it ...
*/
MemSet(&locfcinfo, 0, sizeof(locfcinfo));
locfcinfo.flinfo = &my_extra->coerce_finfo;
locfcinfo.nargs = 3;
locfcinfo.arg[0] = PointerGetDatum(v);
locfcinfo.arg[1] = Int32GetDatum(desttypmod);
locfcinfo.arg[2] = BoolGetDatum(isExplicit);
return array_map(&locfcinfo, ARR_ELEMTYPE(v), ARR_ELEMTYPE(v));
}
/*
* accumArrayResult - accumulate one (more) Datum for an array result
*

View File

@@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.171 2004/06/09 19:08:18 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.172 2004/06/16 01:26:47 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -194,7 +194,6 @@ static void get_oper_expr(OpExpr *expr, deparse_context *context);
static void get_func_expr(FuncExpr *expr, deparse_context *context,
bool showimplicit);
static void get_agg_expr(Aggref *aggref, deparse_context *context);
static Node *strip_type_coercion(Node *expr, Oid resultType);
static void get_const_expr(Const *constval, deparse_context *context);
static void get_sublink_expr(SubLink *sublink, deparse_context *context);
static void get_from_clause(Query *query, deparse_context *context);
@@ -2983,22 +2982,13 @@ get_rule_expr(Node *node, deparse_context *context,
!showimplicit)
{
/* don't show the implicit cast */
get_rule_expr_paren(arg, context, showimplicit, node);
get_rule_expr_paren(arg, context, false, node);
}
else
{
/*
* Strip off any type coercions on the input, so we
* don't print redundancies like
* x::bpchar::character(8).
*
* XXX Are there any cases where this is a bad idea?
*/
arg = strip_type_coercion(arg, relabel->resulttype);
if (!PRETTY_PAREN(context))
appendStringInfoChar(buf, '(');
get_rule_expr_paren(arg, context, showimplicit, node);
get_rule_expr_paren(arg, context, false, node);
if (!PRETTY_PAREN(context))
appendStringInfoChar(buf, ')');
appendStringInfo(buf, "::%s",
@@ -3206,11 +3196,6 @@ get_rule_expr(Node *node, deparse_context *context,
CoerceToDomain *ctest = (CoerceToDomain *) node;
Node *arg = (Node *) ctest->arg;
/*
* Any implicit coercion at the top level of the argument
* is presumably due to the domain's own internal typmod
* coercion, so do not force it to be shown.
*/
if (ctest->coercionformat == COERCE_IMPLICIT_CAST &&
!showimplicit)
{
@@ -3331,7 +3316,7 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
if (expr->funcformat == COERCE_IMPLICIT_CAST && !showimplicit)
{
get_rule_expr_paren((Node *) linitial(expr->args), context,
showimplicit, (Node *) expr);
false, (Node *) expr);
return;
}
@@ -3349,17 +3334,9 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
/* Get the typmod if this is a length-coercion function */
(void) exprIsLengthCoercion((Node *) expr, &coercedTypmod);
/*
* Strip off any type coercions on the input, so we don't print
* redundancies like x::bpchar::character(8).
*
* XXX Are there any cases where this is a bad idea?
*/
arg = strip_type_coercion(arg, rettype);
if (!PRETTY_PAREN(context))
appendStringInfoChar(buf, '(');
get_rule_expr_paren(arg, context, showimplicit, (Node *) expr);
get_rule_expr_paren(arg, context, false, (Node *) expr);
if (!PRETTY_PAREN(context))
appendStringInfoChar(buf, ')');
appendStringInfo(buf, "::%s",
@@ -3413,46 +3390,6 @@ get_agg_expr(Aggref *aggref, deparse_context *context)
}
/*
* strip_type_coercion
* Strip any type coercion at the top of the given expression tree,
* if it is a coercion to the given datatype.
*
* We use this to avoid printing two levels of coercion in situations where
* the expression tree has a length-coercion node atop a type-coercion node.
*
* Note: avoid stripping a length-coercion node, since two successive
* coercions to different lengths aren't a no-op. Also, never strip a
* CoerceToDomain node, even though it might be effectively just RelabelType.
*/
static Node *
strip_type_coercion(Node *expr, Oid resultType)
{
if (expr == NULL || exprType(expr) != resultType)
return expr;
if (IsA(expr, RelabelType) &&
((RelabelType *) expr)->resulttypmod == -1)
return (Node *) ((RelabelType *) expr)->arg;
if (IsA(expr, FuncExpr))
{
FuncExpr *func = (FuncExpr *) expr;
if (func->funcformat != COERCE_EXPLICIT_CAST &&
func->funcformat != COERCE_IMPLICIT_CAST)
return expr; /* don't absorb into upper coercion */
if (exprIsLengthCoercion(expr, NULL))
return expr;
return (Node *) linitial(func->args);
}
return expr;
}
/* ----------
* get_const_expr
*

View File

@@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.38 2003/11/29 19:51:59 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.39 2004/06/16 01:26:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1166,32 +1166,58 @@ bitshiftright(PG_FUNCTION_ARGS)
PG_RETURN_VARBIT_P(result);
}
/* This is not defined in any standard. We retain the natural ordering of
/*
* This is not defined in any standard. We retain the natural ordering of
* bits here, as it just seems more intuitive.
*/
Datum
bitfromint4(PG_FUNCTION_ARGS)
{
int32 a = PG_GETARG_INT32(0);
int32 typmod = PG_GETARG_INT32(1);
VarBit *result;
bits8 *r;
int len;
int rlen;
int destbitsleft,
srcbitsleft;
/* allocate enough space for the bits in an int4 */
len = VARBITTOTALLEN(sizeof(int4) * BITS_PER_BYTE);
result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len;
VARBITLEN(result) = sizeof(int4) * BITS_PER_BYTE;
if (typmod <= 0)
typmod = 1; /* default bit length */
rlen = VARBITTOTALLEN(typmod);
result = (VarBit *) palloc(rlen);
VARATT_SIZEP(result) = rlen;
VARBITLEN(result) = typmod;
/*
* masks and shifts here are just too painful and we know that an int4
* has got 4 bytes
*/
r = VARBITS(result);
r[0] = (bits8) ((a >> (3 * BITS_PER_BYTE)) & BITMASK);
r[1] = (bits8) ((a >> (2 * BITS_PER_BYTE)) & BITMASK);
r[2] = (bits8) ((a >> (1 * BITS_PER_BYTE)) & BITMASK);
r[3] = (bits8) (a & BITMASK);
destbitsleft = typmod;
srcbitsleft = 32;
/* drop any input bits that don't fit */
srcbitsleft = Min(srcbitsleft, destbitsleft);
/* sign-fill any excess bytes in output */
while (destbitsleft >= srcbitsleft + 8)
{
*r++ = (bits8) ((a < 0) ? BITMASK : 0);
destbitsleft -= 8;
}
/* store first fractional byte */
if (destbitsleft > srcbitsleft)
{
*r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK);
destbitsleft -= 8;
}
/* Now srcbitsleft and destbitsleft are the same, need not track both */
/* store whole bytes */
while (destbitsleft >= 8)
{
*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
destbitsleft -= 8;
}
/* store last fractional byte */
if (destbitsleft > 0)
{
*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
}
PG_RETURN_VARBIT_P(result);
}
@@ -1204,7 +1230,7 @@ bittoint4(PG_FUNCTION_ARGS)
bits8 *r;
/* Check that the bit string is not too long */
if (VARBITLEN(arg) > sizeof(int4) * BITS_PER_BYTE)
if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
@@ -1224,46 +1250,62 @@ bittoint4(PG_FUNCTION_ARGS)
Datum
bitfromint8(PG_FUNCTION_ARGS)
{
#ifndef INT64_IS_BUSTED
int64 a = PG_GETARG_INT64(0);
int32 typmod = PG_GETARG_INT32(1);
VarBit *result;
bits8 *r;
int len;
int rlen;
int destbitsleft,
srcbitsleft;
/* allocate enough space for the bits in an int64 */
len = VARBITTOTALLEN(sizeof(a) * BITS_PER_BYTE);
result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len;
VARBITLEN(result) = sizeof(a) * BITS_PER_BYTE;
if (typmod <= 0)
typmod = 1; /* default bit length */
rlen = VARBITTOTALLEN(typmod);
result = (VarBit *) palloc(rlen);
VARATT_SIZEP(result) = rlen;
VARBITLEN(result) = typmod;
/*
* masks and shifts here are just too painful and we know that an
* int64 has got 8 bytes
*/
r = VARBITS(result);
r[0] = (bits8) ((a >> (7 * BITS_PER_BYTE)) & BITMASK);
r[1] = (bits8) ((a >> (6 * BITS_PER_BYTE)) & BITMASK);
r[2] = (bits8) ((a >> (5 * BITS_PER_BYTE)) & BITMASK);
r[3] = (bits8) ((a >> (4 * BITS_PER_BYTE)) & BITMASK);
r[4] = (bits8) ((a >> (3 * BITS_PER_BYTE)) & BITMASK);
r[5] = (bits8) ((a >> (2 * BITS_PER_BYTE)) & BITMASK);
r[6] = (bits8) ((a >> (1 * BITS_PER_BYTE)) & BITMASK);
r[7] = (bits8) (a & BITMASK);
destbitsleft = typmod;
#ifndef INT64_IS_BUSTED
srcbitsleft = 64;
#else
srcbitsleft = 32; /* don't try to shift more than 32 */
#endif
/* drop any input bits that don't fit */
srcbitsleft = Min(srcbitsleft, destbitsleft);
/* sign-fill any excess bytes in output */
while (destbitsleft >= srcbitsleft + 8)
{
*r++ = (bits8) ((a < 0) ? BITMASK : 0);
destbitsleft -= 8;
}
/* store first fractional byte */
if (destbitsleft > srcbitsleft)
{
*r++ = (bits8) ((a >> (srcbitsleft - 8)) & BITMASK);
destbitsleft -= 8;
}
/* Now srcbitsleft and destbitsleft are the same, need not track both */
/* store whole bytes */
while (destbitsleft >= 8)
{
*r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
destbitsleft -= 8;
}
/* store last fractional byte */
if (destbitsleft > 0)
{
*r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
}
PG_RETURN_VARBIT_P(result);
#else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("64-bit integers not supported on this platform")));
PG_RETURN_NULL();
#endif
}
Datum
bittoint8(PG_FUNCTION_ARGS)
{
#ifndef INT64_IS_BUSTED
VarBit *arg = PG_GETARG_VARBIT_P(0);
uint64 result;
bits8 *r;
@@ -1284,13 +1326,6 @@ bittoint8(PG_FUNCTION_ARGS)
result >>= VARBITPAD(arg);
PG_RETURN_INT64(result);
#else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("64-bit integers not supported on this platform")));
PG_RETURN_NULL();
#endif
}