mirror of
https://github.com/postgres/postgres.git
synced 2025-10-22 14:32:25 +03:00
Improve parsetree representation of special functions such as CURRENT_DATE.
We implement a dozen or so parameterless functions that the SQL standard defines special syntax for. Up to now, that was done by converting them into more or less ad-hoc constructs such as "'now'::text::date". That's messy for multiple reasons: it exposes what should be implementation details to users, and performance is worse than it needs to be in several cases. To improve matters, invent a new expression node type SQLValueFunction that can represent any of these parameterless functions. Bump catversion because this changes stored parsetrees for rules. Discussion: <30058.1463091294@sss.pgh.pa.us>
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "access/hash.h"
|
||||
#include "access/xact.h"
|
||||
#include "libpq/pqformat.h"
|
||||
#include "miscadmin.h"
|
||||
#include "parser/scansup.h"
|
||||
@@ -51,7 +52,6 @@ static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
|
||||
static int32
|
||||
anytime_typmodin(bool istz, ArrayType *ta)
|
||||
{
|
||||
int32 typmod;
|
||||
int32 *tl;
|
||||
int n;
|
||||
|
||||
@@ -66,22 +66,27 @@ anytime_typmodin(bool istz, ArrayType *ta)
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("invalid type modifier")));
|
||||
|
||||
if (*tl < 0)
|
||||
return anytime_typmod_check(istz, tl[0]);
|
||||
}
|
||||
|
||||
/* exported so parse_expr.c can use it */
|
||||
int32
|
||||
anytime_typmod_check(bool istz, int32 typmod)
|
||||
{
|
||||
if (typmod < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("TIME(%d)%s precision must not be negative",
|
||||
*tl, (istz ? " WITH TIME ZONE" : ""))));
|
||||
if (*tl > MAX_TIME_PRECISION)
|
||||
typmod, (istz ? " WITH TIME ZONE" : ""))));
|
||||
if (typmod > MAX_TIME_PRECISION)
|
||||
{
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
|
||||
*tl, (istz ? " WITH TIME ZONE" : ""),
|
||||
typmod, (istz ? " WITH TIME ZONE" : ""),
|
||||
MAX_TIME_PRECISION)));
|
||||
typmod = MAX_TIME_PRECISION;
|
||||
}
|
||||
else
|
||||
typmod = *tl;
|
||||
|
||||
return typmod;
|
||||
}
|
||||
@@ -298,6 +303,80 @@ EncodeSpecialDate(DateADT dt, char *str)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* GetSQLCurrentDate -- implements CURRENT_DATE
|
||||
*/
|
||||
DateADT
|
||||
GetSQLCurrentDate(void)
|
||||
{
|
||||
TimestampTz ts;
|
||||
struct pg_tm tt,
|
||||
*tm = &tt;
|
||||
fsec_t fsec;
|
||||
int tz;
|
||||
|
||||
ts = GetCurrentTransactionStartTimestamp();
|
||||
|
||||
if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
||||
errmsg("timestamp out of range")));
|
||||
|
||||
return date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSQLCurrentTime -- implements CURRENT_TIME, CURRENT_TIME(n)
|
||||
*/
|
||||
TimeTzADT *
|
||||
GetSQLCurrentTime(int32 typmod)
|
||||
{
|
||||
TimeTzADT *result;
|
||||
TimestampTz ts;
|
||||
struct pg_tm tt,
|
||||
*tm = &tt;
|
||||
fsec_t fsec;
|
||||
int tz;
|
||||
|
||||
ts = GetCurrentTransactionStartTimestamp();
|
||||
|
||||
if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
||||
errmsg("timestamp out of range")));
|
||||
|
||||
result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
|
||||
tm2timetz(tm, fsec, tz, result);
|
||||
AdjustTimeForTypmod(&(result->time), typmod);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSQLLocalTime -- implements LOCALTIME, LOCALTIME(n)
|
||||
*/
|
||||
TimeADT
|
||||
GetSQLLocalTime(int32 typmod)
|
||||
{
|
||||
TimeADT result;
|
||||
TimestampTz ts;
|
||||
struct pg_tm tt,
|
||||
*tm = &tt;
|
||||
fsec_t fsec;
|
||||
int tz;
|
||||
|
||||
ts = GetCurrentTransactionStartTimestamp();
|
||||
|
||||
if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
||||
errmsg("timestamp out of range")));
|
||||
|
||||
tm2time(tm, fsec, &result);
|
||||
AdjustTimeForTypmod(&result, typmod);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Comparison functions for dates
|
||||
*/
|
||||
|
@@ -6884,6 +6884,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
|
||||
case T_RowExpr:
|
||||
case T_CoalesceExpr:
|
||||
case T_MinMaxExpr:
|
||||
case T_SQLValueFunction:
|
||||
case T_XmlExpr:
|
||||
case T_NullIfExpr:
|
||||
case T_Aggref:
|
||||
@@ -7871,6 +7872,67 @@ get_rule_expr(Node *node, deparse_context *context,
|
||||
}
|
||||
break;
|
||||
|
||||
case T_SQLValueFunction:
|
||||
{
|
||||
SQLValueFunction *svf = (SQLValueFunction *) node;
|
||||
|
||||
/*
|
||||
* Note: this code knows that typmod for time, timestamp, and
|
||||
* timestamptz just prints as integer.
|
||||
*/
|
||||
switch (svf->op)
|
||||
{
|
||||
case SVFOP_CURRENT_DATE:
|
||||
appendStringInfoString(buf, "CURRENT_DATE");
|
||||
break;
|
||||
case SVFOP_CURRENT_TIME:
|
||||
appendStringInfoString(buf, "CURRENT_TIME");
|
||||
break;
|
||||
case SVFOP_CURRENT_TIME_N:
|
||||
appendStringInfo(buf, "CURRENT_TIME(%d)", svf->typmod);
|
||||
break;
|
||||
case SVFOP_CURRENT_TIMESTAMP:
|
||||
appendStringInfoString(buf, "CURRENT_TIMESTAMP");
|
||||
break;
|
||||
case SVFOP_CURRENT_TIMESTAMP_N:
|
||||
appendStringInfo(buf, "CURRENT_TIMESTAMP(%d)",
|
||||
svf->typmod);
|
||||
break;
|
||||
case SVFOP_LOCALTIME:
|
||||
appendStringInfoString(buf, "LOCALTIME");
|
||||
break;
|
||||
case SVFOP_LOCALTIME_N:
|
||||
appendStringInfo(buf, "LOCALTIME(%d)", svf->typmod);
|
||||
break;
|
||||
case SVFOP_LOCALTIMESTAMP:
|
||||
appendStringInfoString(buf, "LOCALTIMESTAMP");
|
||||
break;
|
||||
case SVFOP_LOCALTIMESTAMP_N:
|
||||
appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
|
||||
svf->typmod);
|
||||
break;
|
||||
case SVFOP_CURRENT_ROLE:
|
||||
appendStringInfoString(buf, "CURRENT_ROLE");
|
||||
break;
|
||||
case SVFOP_CURRENT_USER:
|
||||
appendStringInfoString(buf, "CURRENT_USER");
|
||||
break;
|
||||
case SVFOP_USER:
|
||||
appendStringInfoString(buf, "USER");
|
||||
break;
|
||||
case SVFOP_SESSION_USER:
|
||||
appendStringInfoString(buf, "SESSION_USER");
|
||||
break;
|
||||
case SVFOP_CURRENT_CATALOG:
|
||||
appendStringInfoString(buf, "CURRENT_CATALOG");
|
||||
break;
|
||||
case SVFOP_CURRENT_SCHEMA:
|
||||
appendStringInfoString(buf, "CURRENT_SCHEMA");
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case T_XmlExpr:
|
||||
{
|
||||
XmlExpr *xexpr = (XmlExpr *) node;
|
||||
|
@@ -72,13 +72,13 @@ static Timestamp dt2local(Timestamp dt, int timezone);
|
||||
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod);
|
||||
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod);
|
||||
static TimestampTz timestamp2timestamptz(Timestamp timestamp);
|
||||
static Timestamp timestamptz2timestamp(TimestampTz timestamp);
|
||||
|
||||
|
||||
/* common code for timestamptypmodin and timestamptztypmodin */
|
||||
static int32
|
||||
anytimestamp_typmodin(bool istz, ArrayType *ta)
|
||||
{
|
||||
int32 typmod;
|
||||
int32 *tl;
|
||||
int n;
|
||||
|
||||
@@ -93,22 +93,27 @@ anytimestamp_typmodin(bool istz, ArrayType *ta)
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("invalid type modifier")));
|
||||
|
||||
if (*tl < 0)
|
||||
return anytimestamp_typmod_check(istz, tl[0]);
|
||||
}
|
||||
|
||||
/* exported so parse_expr.c can use it */
|
||||
int32
|
||||
anytimestamp_typmod_check(bool istz, int32 typmod)
|
||||
{
|
||||
if (typmod < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("TIMESTAMP(%d)%s precision must not be negative",
|
||||
*tl, (istz ? " WITH TIME ZONE" : ""))));
|
||||
if (*tl > MAX_TIMESTAMP_PRECISION)
|
||||
typmod, (istz ? " WITH TIME ZONE" : ""))));
|
||||
if (typmod > MAX_TIMESTAMP_PRECISION)
|
||||
{
|
||||
ereport(WARNING,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
|
||||
*tl, (istz ? " WITH TIME ZONE" : ""),
|
||||
typmod, (istz ? " WITH TIME ZONE" : ""),
|
||||
MAX_TIMESTAMP_PRECISION)));
|
||||
typmod = MAX_TIMESTAMP_PRECISION;
|
||||
}
|
||||
else
|
||||
typmod = *tl;
|
||||
|
||||
return typmod;
|
||||
}
|
||||
@@ -336,6 +341,10 @@ timestamp_scale(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_TIMESTAMP(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* AdjustTimestampForTypmod --- round off a timestamp to suit given typmod
|
||||
* Works for either timestamp or timestamptz.
|
||||
*/
|
||||
static void
|
||||
AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
|
||||
{
|
||||
@@ -1686,6 +1695,34 @@ IntegerTimestampToTimestampTz(int64 timestamp)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* GetSQLCurrentTimestamp -- implements CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(n)
|
||||
*/
|
||||
TimestampTz
|
||||
GetSQLCurrentTimestamp(int32 typmod)
|
||||
{
|
||||
TimestampTz ts;
|
||||
|
||||
ts = GetCurrentTransactionStartTimestamp();
|
||||
if (typmod >= 0)
|
||||
AdjustTimestampForTypmod(&ts, typmod);
|
||||
return ts;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSQLLocalTimestamp -- implements LOCALTIMESTAMP, LOCALTIMESTAMP(n)
|
||||
*/
|
||||
Timestamp
|
||||
GetSQLLocalTimestamp(int32 typmod)
|
||||
{
|
||||
Timestamp ts;
|
||||
|
||||
ts = timestamptz2timestamp(GetCurrentTransactionStartTimestamp());
|
||||
if (typmod >= 0)
|
||||
AdjustTimestampForTypmod(&ts, typmod);
|
||||
return ts;
|
||||
}
|
||||
|
||||
/*
|
||||
* TimestampDifference -- convert the difference between two timestamps
|
||||
* into integer seconds and microseconds
|
||||
@@ -5415,6 +5452,13 @@ Datum
|
||||
timestamptz_timestamp(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
|
||||
|
||||
PG_RETURN_TIMESTAMP(timestamptz2timestamp(timestamp));
|
||||
}
|
||||
|
||||
static Timestamp
|
||||
timestamptz2timestamp(TimestampTz timestamp)
|
||||
{
|
||||
Timestamp result;
|
||||
struct pg_tm tt,
|
||||
*tm = &tt;
|
||||
@@ -5434,7 +5478,7 @@ timestamptz_timestamp(PG_FUNCTION_ARGS)
|
||||
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
|
||||
errmsg("timestamp out of range")));
|
||||
}
|
||||
PG_RETURN_TIMESTAMP(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* timestamptz_zone()
|
||||
|
Reference in New Issue
Block a user