mirror of
https://github.com/postgres/postgres.git
synced 2025-06-13 07:41:39 +03:00
Downgrade implicit casts to text to be assignment-only, except for the ones
from the other string-category types; this eliminates a lot of surprising interpretations that the parser could formerly make when there was no directly applicable operator. Create a general mechanism that supports casts to and from the standard string types (text,varchar,bpchar) for *every* datatype, by invoking the datatype's I/O functions. These new casts are assignment-only in the to-string direction, explicit-only in the other, and therefore should create no surprising behavior. Remove a bunch of thereby-obsoleted datatype-specific casting functions. The "general mechanism" is a new expression node type CoerceViaIO that can actually convert between *any* two datatypes if their external text representations are compatible. This is more general than needed for the immediate feature, but might be useful in plpgsql or other places in future. This commit does nothing about the issue that applying the concatenation operator || to non-text types will now fail, often with strange error messages due to misinterpreting the operator as array concatenation. Since it often (not always) worked before, we should either make it succeed or at least give a more user-friendly error; but details are still under debate. Peter Eisentraut and Tom Lane
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.176 2007/04/30 21:01:52 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.177 2007/06/05 21:31:06 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -3229,187 +3229,6 @@ timestamptz_age(PG_FUNCTION_ARGS)
|
||||
*---------------------------------------------------------*/
|
||||
|
||||
|
||||
/* timestamp_text()
|
||||
* Convert timestamp to text data type.
|
||||
*/
|
||||
Datum
|
||||
timestamp_text(PG_FUNCTION_ARGS)
|
||||
{
|
||||
/* Input is a Timestamp, but may as well leave it in Datum form */
|
||||
Datum timestamp = PG_GETARG_DATUM(0);
|
||||
text *result;
|
||||
char *str;
|
||||
int len;
|
||||
|
||||
str = DatumGetCString(DirectFunctionCall1(timestamp_out, timestamp));
|
||||
|
||||
len = (strlen(str) + VARHDRSZ);
|
||||
|
||||
result = palloc(len);
|
||||
|
||||
SET_VARSIZE(result, len);
|
||||
memcpy(VARDATA(result), str, len - VARHDRSZ);
|
||||
|
||||
pfree(str);
|
||||
|
||||
PG_RETURN_TEXT_P(result);
|
||||
}
|
||||
|
||||
|
||||
/* text_timestamp()
|
||||
* Convert text string to timestamp.
|
||||
* Text type is not null terminated, so use temporary string
|
||||
* then call the standard input routine.
|
||||
*/
|
||||
Datum
|
||||
text_timestamp(PG_FUNCTION_ARGS)
|
||||
{
|
||||
text *str = PG_GETARG_TEXT_P(0);
|
||||
int i;
|
||||
char *sp,
|
||||
*dp,
|
||||
dstr[MAXDATELEN + 1];
|
||||
|
||||
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
||||
errmsg("invalid input syntax for type timestamp: \"%s\"",
|
||||
DatumGetCString(DirectFunctionCall1(textout,
|
||||
PointerGetDatum(str))))));
|
||||
|
||||
sp = VARDATA(str);
|
||||
dp = dstr;
|
||||
for (i = 0; i < VARSIZE(str) - VARHDRSZ; i++)
|
||||
*dp++ = *sp++;
|
||||
*dp = '\0';
|
||||
|
||||
return DirectFunctionCall3(timestamp_in,
|
||||
CStringGetDatum(dstr),
|
||||
ObjectIdGetDatum(InvalidOid),
|
||||
Int32GetDatum(-1));
|
||||
}
|
||||
|
||||
|
||||
/* timestamptz_text()
|
||||
* Convert timestamp with time zone to text data type.
|
||||
*/
|
||||
Datum
|
||||
timestamptz_text(PG_FUNCTION_ARGS)
|
||||
{
|
||||
/* Input is a Timestamp, but may as well leave it in Datum form */
|
||||
Datum timestamp = PG_GETARG_DATUM(0);
|
||||
text *result;
|
||||
char *str;
|
||||
int len;
|
||||
|
||||
str = DatumGetCString(DirectFunctionCall1(timestamptz_out, timestamp));
|
||||
|
||||
len = strlen(str) + VARHDRSZ;
|
||||
|
||||
result = palloc(len);
|
||||
|
||||
SET_VARSIZE(result, len);
|
||||
memcpy(VARDATA(result), str, len - VARHDRSZ);
|
||||
|
||||
pfree(str);
|
||||
|
||||
PG_RETURN_TEXT_P(result);
|
||||
}
|
||||
|
||||
/* text_timestamptz()
|
||||
* Convert text string to timestamp with time zone.
|
||||
* Text type is not null terminated, so use temporary string
|
||||
* then call the standard input routine.
|
||||
*/
|
||||
Datum
|
||||
text_timestamptz(PG_FUNCTION_ARGS)
|
||||
{
|
||||
text *str = PG_GETARG_TEXT_P(0);
|
||||
int i;
|
||||
char *sp,
|
||||
*dp,
|
||||
dstr[MAXDATELEN + 1];
|
||||
|
||||
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
||||
errmsg("invalid input syntax for type timestamp with time zone: \"%s\"",
|
||||
DatumGetCString(DirectFunctionCall1(textout,
|
||||
PointerGetDatum(str))))));
|
||||
|
||||
sp = VARDATA(str);
|
||||
dp = dstr;
|
||||
for (i = 0; i < VARSIZE(str) - VARHDRSZ; i++)
|
||||
*dp++ = *sp++;
|
||||
*dp = '\0';
|
||||
|
||||
return DirectFunctionCall3(timestamptz_in,
|
||||
CStringGetDatum(dstr),
|
||||
ObjectIdGetDatum(InvalidOid),
|
||||
Int32GetDatum(-1));
|
||||
}
|
||||
|
||||
|
||||
/* interval_text()
|
||||
* Convert interval to text data type.
|
||||
*/
|
||||
Datum
|
||||
interval_text(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Interval *interval = PG_GETARG_INTERVAL_P(0);
|
||||
text *result;
|
||||
char *str;
|
||||
int len;
|
||||
|
||||
str = DatumGetCString(DirectFunctionCall1(interval_out,
|
||||
IntervalPGetDatum(interval)));
|
||||
|
||||
len = strlen(str) + VARHDRSZ;
|
||||
|
||||
result = palloc(len);
|
||||
|
||||
SET_VARSIZE(result, len);
|
||||
memcpy(VARDATA(result), str, len - VARHDRSZ);
|
||||
|
||||
pfree(str);
|
||||
|
||||
PG_RETURN_TEXT_P(result);
|
||||
}
|
||||
|
||||
|
||||
/* text_interval()
|
||||
* Convert text string to interval.
|
||||
* Text type may not be null terminated, so copy to temporary string
|
||||
* then call the standard input routine.
|
||||
*/
|
||||
Datum
|
||||
text_interval(PG_FUNCTION_ARGS)
|
||||
{
|
||||
text *str = PG_GETARG_TEXT_P(0);
|
||||
int i;
|
||||
char *sp,
|
||||
*dp,
|
||||
dstr[MAXDATELEN + 1];
|
||||
|
||||
if (VARSIZE(str) - VARHDRSZ > MAXDATELEN)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
|
||||
errmsg("invalid input syntax for type interval: \"%s\"",
|
||||
DatumGetCString(DirectFunctionCall1(textout,
|
||||
PointerGetDatum(str))))));
|
||||
|
||||
sp = VARDATA(str);
|
||||
dp = dstr;
|
||||
for (i = 0; i < (VARSIZE(str) - VARHDRSZ); i++)
|
||||
*dp++ = *sp++;
|
||||
*dp = '\0';
|
||||
|
||||
return DirectFunctionCall3(interval_in,
|
||||
CStringGetDatum(dstr),
|
||||
ObjectIdGetDatum(InvalidOid),
|
||||
Int32GetDatum(-1));
|
||||
}
|
||||
|
||||
/* timestamp_trunc()
|
||||
* Truncate timestamp to specified units.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user