summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/varchar.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2002-09-18 21:35:25 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2002-09-18 21:35:25 +0000
commitb26dfb95222fddd25322bdddf3a5a58d3392d8b1 (patch)
tree757cf0bafab985d38a5c84d3afebe5edd34c4f27 /src/backend/utils/adt/varchar.c
parentcc70ba2e4daa78ba99619770e19beb06de3dfd1c (diff)
downloadpostgresql-b26dfb95222fddd25322bdddf3a5a58d3392d8b1.tar.gz
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.
Diffstat (limited to 'src/backend/utils/adt/varchar.c')
-rw-r--r--src/backend/utils/adt/varchar.c130
1 files changed, 42 insertions, 88 deletions
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index 780daf7565..03579f437b 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.94 2002/09/04 20:31:29 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.95 2002/09/18 21:35:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -165,21 +165,28 @@ bpcharout(PG_FUNCTION_ARGS)
/*
- * Converts a CHARACTER type to the specified size. maxlen is the new
- * declared length plus VARHDRSZ bytes. Truncation
- * rules see bpcharin() above.
+ * Converts a CHARACTER type to the specified size.
+ *
+ * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
+ * isExplicit is true if this is for an explicit cast to char(N).
+ *
+ * Truncation rules: for an explicit cast, silently truncate to the given
+ * length; for an implicit cast, raise error unless extra characters are
+ * all spaces. (This is sort-of per SQL: the spec would actually have us
+ * raise a "completion condition" for the explicit cast case, but Postgres
+ * hasn't got such a concept.)
*/
Datum
bpchar(PG_FUNCTION_ARGS)
{
BpChar *source = PG_GETARG_BPCHAR_P(0);
int32 maxlen = PG_GETARG_INT32(1);
+ bool isExplicit = PG_GETARG_BOOL(2);
BpChar *result;
int32 len;
char *r;
char *s;
int i;
-
int charlen; /* number of charcters in the input string
* + VARHDRSZ */
@@ -188,7 +195,7 @@ bpchar(PG_FUNCTION_ARGS)
charlen = pg_mbstrlen_with_len(VARDATA(source), len - VARHDRSZ) + VARHDRSZ;
/* No work if typmod is invalid or supplied data matches it already */
- if (maxlen < (int32) VARHDRSZ || len == maxlen)
+ if (maxlen < (int32) VARHDRSZ || charlen == maxlen)
PG_RETURN_BPCHAR_P(source);
if (charlen > maxlen)
@@ -199,10 +206,13 @@ bpchar(PG_FUNCTION_ARGS)
maxmblen = pg_mbcharcliplen(VARDATA(source), len - VARHDRSZ,
maxlen - VARHDRSZ) + VARHDRSZ;
- for (i = maxmblen - VARHDRSZ; i < len - VARHDRSZ; i++)
- if (*(VARDATA(source) + i) != ' ')
- elog(ERROR, "value too long for type character(%d)",
- maxlen - VARHDRSZ);
+ if (!isExplicit)
+ {
+ for (i = maxmblen - VARHDRSZ; i < len - VARHDRSZ; i++)
+ if (*(VARDATA(source) + i) != ' ')
+ elog(ERROR, "value too long for type character(%d)",
+ maxlen - VARHDRSZ);
+ }
len = maxmblen;
@@ -238,37 +248,6 @@ bpchar(PG_FUNCTION_ARGS)
}
-/* _bpchar()
- * Converts an array of char() elements to a specific internal length.
- * len is the length specified in () plus VARHDRSZ bytes.
- */
-Datum
-_bpchar(PG_FUNCTION_ARGS)
-{
- ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
- int32 len = PG_GETARG_INT32(1);
- FunctionCallInfoData locfcinfo;
-
- /*
- * Since bpchar() is a built-in function, we should only need to look
- * it up once per run.
- */
- static FmgrInfo bpchar_finfo;
-
- if (bpchar_finfo.fn_oid == InvalidOid)
- fmgr_info_cxt(F_BPCHAR, &bpchar_finfo, TopMemoryContext);
-
- MemSet(&locfcinfo, 0, sizeof(locfcinfo));
- locfcinfo.flinfo = &bpchar_finfo;
- locfcinfo.nargs = 2;
- /* We assume we are "strict" and need not worry about null inputs */
- locfcinfo.arg[0] = PointerGetDatum(v);
- locfcinfo.arg[1] = Int32GetDatum(len);
-
- return array_map(&locfcinfo, BPCHAROID, BPCHAROID);
-}
-
-
/* char_bpchar()
* Convert char to bpchar(1).
*/
@@ -354,9 +333,9 @@ name_bpchar(PG_FUNCTION_ARGS)
* Note that atttypmod is regarded as the number of characters, which
* is not necessarily the same as the number of bytes.
*
- * If the C string is too long,
- * raise an error, unless the extra characters are spaces, in which
- * case they're truncated. (per SQL) */
+ * If the C string is too long, raise an error, unless the extra characters
+ * are spaces, in which case they're truncated. (per SQL)
+ */
Datum
varcharin(PG_FUNCTION_ARGS)
{
@@ -428,17 +407,26 @@ varcharout(PG_FUNCTION_ARGS)
/*
- * Converts a VARCHAR type to the specified size. maxlen is the new
- * declared length plus VARHDRSZ bytes. Truncation
- * rules see varcharin() above.
+ * Converts a VARCHAR type to the specified size.
+ *
+ * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
+ * isExplicit is true if this is for an explicit cast to varchar(N).
+ *
+ * Truncation rules: for an explicit cast, silently truncate to the given
+ * length; for an implicit cast, raise error unless extra characters are
+ * all spaces. (This is sort-of per SQL: the spec would actually have us
+ * raise a "completion condition" for the explicit cast case, but Postgres
+ * hasn't got such a concept.)
*/
Datum
varchar(PG_FUNCTION_ARGS)
{
VarChar *source = PG_GETARG_VARCHAR_P(0);
int32 maxlen = PG_GETARG_INT32(1);
+ bool isExplicit = PG_GETARG_BOOL(2);
VarChar *result;
int32 len;
+ size_t maxmblen;
int i;
len = VARSIZE(source);
@@ -448,21 +436,19 @@ varchar(PG_FUNCTION_ARGS)
/* only reach here if string is too long... */
- {
- size_t maxmblen;
-
- /* truncate multibyte string preserving multibyte boundary */
- maxmblen = pg_mbcharcliplen(VARDATA(source), len - VARHDRSZ,
- maxlen - VARHDRSZ) + VARHDRSZ;
+ /* truncate multibyte string preserving multibyte boundary */
+ maxmblen = pg_mbcharcliplen(VARDATA(source), len - VARHDRSZ,
+ maxlen - VARHDRSZ);
- for (i = maxmblen - VARHDRSZ; i < len - VARHDRSZ; i++)
+ if (!isExplicit)
+ {
+ for (i = maxmblen; i < len - VARHDRSZ; i++)
if (*(VARDATA(source) + i) != ' ')
elog(ERROR, "value too long for type character varying(%d)",
maxlen - VARHDRSZ);
-
- len = maxmblen;
}
+ len = maxmblen + VARHDRSZ;
result = palloc(len);
VARATT_SIZEP(result) = len;
memcpy(VARDATA(result), VARDATA(source), len - VARHDRSZ);
@@ -471,38 +457,6 @@ varchar(PG_FUNCTION_ARGS)
}
-/* _varchar()
- * Converts an array of varchar() elements to the specified size.
- * len is the length specified in () plus VARHDRSZ bytes.
- */
-Datum
-_varchar(PG_FUNCTION_ARGS)
-{
- ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
- int32 len = PG_GETARG_INT32(1);
- FunctionCallInfoData locfcinfo;
-
- /*
- * Since varchar() is a built-in function, we should only need to look
- * it up once per run.
- */
- static FmgrInfo varchar_finfo;
-
- if (varchar_finfo.fn_oid == InvalidOid)
- fmgr_info_cxt(F_VARCHAR, &varchar_finfo, TopMemoryContext);
-
- MemSet(&locfcinfo, 0, sizeof(locfcinfo));
- locfcinfo.flinfo = &varchar_finfo;
- locfcinfo.nargs = 2;
- /* We assume we are "strict" and need not worry about null inputs */
- locfcinfo.arg[0] = PointerGetDatum(v);
- locfcinfo.arg[1] = Int32GetDatum(len);
-
- return array_map(&locfcinfo, VARCHAROID, VARCHAROID);
-}
-
-
-
/*****************************************************************************
* Exported functions
*****************************************************************************/