JumbleExpr(jstate, (Node *) mmexpr->args);
}
break;
+ case T_SQLValueFunction:
+ {
+ SQLValueFunction *svf = (SQLValueFunction *) node;
+
+ APP_JUMB(svf->op);
+ /* type is fully determined by op */
+ APP_JUMB(svf->typmod);
+ }
+ break;
case T_XmlExpr:
{
XmlExpr *xexpr = (XmlExpr *) node;
#include "pgstat.h"
#include "utils/acl.h"
#include "utils/builtins.h"
+#include "utils/date.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
+#include "utils/timestamp.h"
#include "utils/typcache.h"
#include "utils/xml.h"
static Datum ExecEvalMinMax(MinMaxExprState *minmaxExpr,
ExprContext *econtext,
bool *isNull, ExprDoneCond *isDone);
+static Datum ExecEvalSQLValueFunction(ExprState *svfExpr,
+ ExprContext *econtext,
+ bool *isNull, ExprDoneCond *isDone);
static Datum ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
bool *isNull, ExprDoneCond *isDone);
static Datum ExecEvalNullIf(FuncExprState *nullIfExpr,
return result;
}
+/* ----------------------------------------------------------------
+ * ExecEvalSQLValueFunction
+ * ----------------------------------------------------------------
+ */
+static Datum
+ExecEvalSQLValueFunction(ExprState *svfExpr,
+ ExprContext *econtext,
+ bool *isNull, ExprDoneCond *isDone)
+{
+ Datum result = (Datum) 0;
+ SQLValueFunction *svf = (SQLValueFunction *) svfExpr->expr;
+ FunctionCallInfoData fcinfo;
+
+ if (isDone)
+ *isDone = ExprSingleResult;
+ *isNull = false;
+
+ /*
+ * Note: current_schema() can return NULL. current_user() etc currently
+ * cannot, but might as well code those cases the same way for safety.
+ */
+ switch (svf->op)
+ {
+ case SVFOP_CURRENT_DATE:
+ result = DateADTGetDatum(GetSQLCurrentDate());
+ break;
+ case SVFOP_CURRENT_TIME:
+ case SVFOP_CURRENT_TIME_N:
+ result = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod));
+ break;
+ case SVFOP_CURRENT_TIMESTAMP:
+ case SVFOP_CURRENT_TIMESTAMP_N:
+ result = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod));
+ break;
+ case SVFOP_LOCALTIME:
+ case SVFOP_LOCALTIME_N:
+ result = TimeADTGetDatum(GetSQLLocalTime(svf->typmod));
+ break;
+ case SVFOP_LOCALTIMESTAMP:
+ case SVFOP_LOCALTIMESTAMP_N:
+ result = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod));
+ break;
+ case SVFOP_CURRENT_ROLE:
+ case SVFOP_CURRENT_USER:
+ case SVFOP_USER:
+ InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL);
+ result = current_user(&fcinfo);
+ *isNull = fcinfo.isnull;
+ break;
+ case SVFOP_SESSION_USER:
+ InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL);
+ result = session_user(&fcinfo);
+ *isNull = fcinfo.isnull;
+ break;
+ case SVFOP_CURRENT_CATALOG:
+ InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL);
+ result = current_database(&fcinfo);
+ *isNull = fcinfo.isnull;
+ break;
+ case SVFOP_CURRENT_SCHEMA:
+ InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL);
+ result = current_schema(&fcinfo);
+ *isNull = fcinfo.isnull;
+ break;
+ }
+
+ return result;
+}
+
/* ----------------------------------------------------------------
* ExecEvalXml
* ----------------------------------------------------------------
state = (ExprState *) mstate;
}
break;
+ case T_SQLValueFunction:
+ state = (ExprState *) makeNode(ExprState);
+ state->evalfunc = ExecEvalSQLValueFunction;
+ break;
case T_XmlExpr:
{
XmlExpr *xexpr = (XmlExpr *) node;
return newnode;
}
+/*
+ * _copySQLValueFunction
+ */
+static SQLValueFunction *
+_copySQLValueFunction(const SQLValueFunction *from)
+{
+ SQLValueFunction *newnode = makeNode(SQLValueFunction);
+
+ COPY_SCALAR_FIELD(op);
+ COPY_SCALAR_FIELD(type);
+ COPY_SCALAR_FIELD(typmod);
+ COPY_LOCATION_FIELD(location);
+
+ return newnode;
+}
+
/*
* _copyXmlExpr
*/
case T_MinMaxExpr:
retval = _copyMinMaxExpr(from);
break;
+ case T_SQLValueFunction:
+ retval = _copySQLValueFunction(from);
+ break;
case T_XmlExpr:
retval = _copyXmlExpr(from);
break;
return true;
}
+static bool
+_equalSQLValueFunction(const SQLValueFunction *a, const SQLValueFunction *b)
+{
+ COMPARE_SCALAR_FIELD(op);
+ COMPARE_SCALAR_FIELD(type);
+ COMPARE_SCALAR_FIELD(typmod);
+ COMPARE_LOCATION_FIELD(location);
+
+ return true;
+}
+
static bool
_equalXmlExpr(const XmlExpr *a, const XmlExpr *b)
{
case T_MinMaxExpr:
retval = _equalMinMaxExpr(a, b);
break;
+ case T_SQLValueFunction:
+ retval = _equalSQLValueFunction(a, b);
+ break;
case T_XmlExpr:
retval = _equalXmlExpr(a, b);
break;
case T_MinMaxExpr:
type = ((const MinMaxExpr *) expr)->minmaxtype;
break;
+ case T_SQLValueFunction:
+ type = ((const SQLValueFunction *) expr)->type;
+ break;
case T_XmlExpr:
if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
type = BOOLOID;
return typmod;
}
break;
+ case T_SQLValueFunction:
+ return ((const SQLValueFunction *) expr)->typmod;
case T_CoerceToDomain:
return ((const CoerceToDomain *) expr)->resulttypmod;
case T_CoerceToDomainValue:
return false;
if (IsA(node, MinMaxExpr))
return false;
+ if (IsA(node, SQLValueFunction))
+ return false;
if (IsA(node, XmlExpr))
return false;
case T_MinMaxExpr:
coll = ((const MinMaxExpr *) expr)->minmaxcollid;
break;
+ case T_SQLValueFunction:
+ coll = InvalidOid; /* all cases return non-collatable types */
+ break;
case T_XmlExpr:
/*
case T_MinMaxExpr:
((MinMaxExpr *) expr)->minmaxcollid = collation;
break;
+ case T_SQLValueFunction:
+ Assert(!OidIsValid(collation)); /* no collatable results */
+ break;
case T_XmlExpr:
Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
(collation == DEFAULT_COLLATION_OID) :
/* GREATEST/LEAST keyword should always be the first thing */
loc = ((const MinMaxExpr *) expr)->location;
break;
+ case T_SQLValueFunction:
+ /* function keyword should always be the first thing */
+ loc = ((const SQLValueFunction *) expr)->location;
+ break;
case T_XmlExpr:
{
const XmlExpr *xexpr = (const XmlExpr *) expr;
* for themselves, in case additional checks should be made, or because they
* have special rules about which parts of the tree need to be visited.
*
- * Note: we ignore MinMaxExpr, XmlExpr, and CoerceToDomain nodes, because they
- * do not contain SQL function OIDs. However, they can invoke SQL-visible
- * functions, so callers should take thought about how to treat them.
+ * Note: we ignore MinMaxExpr, SQLValueFunction, XmlExpr, and CoerceToDomain
+ * nodes, because they do not contain SQL function OIDs. However, they can
+ * invoke SQL-visible functions, so callers should take thought about how to
+ * treat them.
*/
bool
check_functions_in_node(Node *node, check_function_callback checker,
case T_CaseTestExpr:
case T_SetToDefault:
case T_CurrentOfExpr:
+ case T_SQLValueFunction:
case T_RangeTblRef:
case T_SortGroupClause:
/* primitive node types with no expression subnodes */
case T_CaseTestExpr:
case T_SetToDefault:
case T_CurrentOfExpr:
+ case T_SQLValueFunction:
case T_RangeTblRef:
case T_SortGroupClause:
return (Node *) copyObject(node);
{
case T_SetToDefault:
case T_CurrentOfExpr:
+ case T_SQLValueFunction:
case T_Integer:
case T_Float:
case T_String:
WRITE_LOCATION_FIELD(location);
}
+static void
+_outSQLValueFunction(StringInfo str, const SQLValueFunction *node)
+{
+ WRITE_NODE_TYPE("SQLVALUEFUNCTION");
+
+ WRITE_ENUM_FIELD(op, SQLValueFunctionOp);
+ WRITE_OID_FIELD(type);
+ WRITE_INT_FIELD(typmod);
+ WRITE_LOCATION_FIELD(location);
+}
+
static void
_outXmlExpr(StringInfo str, const XmlExpr *node)
{
case T_MinMaxExpr:
_outMinMaxExpr(str, obj);
break;
+ case T_SQLValueFunction:
+ _outSQLValueFunction(str, obj);
+ break;
case T_XmlExpr:
_outXmlExpr(str, obj);
break;
READ_DONE();
}
+/*
+ * _readSQLValueFunction
+ */
+static SQLValueFunction *
+_readSQLValueFunction(void)
+{
+ READ_LOCALS(SQLValueFunction);
+
+ READ_ENUM_FIELD(op, SQLValueFunctionOp);
+ READ_OID_FIELD(type);
+ READ_INT_FIELD(typmod);
+ READ_LOCATION_FIELD(location);
+
+ READ_DONE();
+}
+
/*
* _readXmlExpr
*/
return_value = _readCoalesceExpr();
else if (MATCH("MINMAX", 6))
return_value = _readMinMaxExpr();
+ else if (MATCH("SQLVALUEFUNCTION", 16))
+ return_value = _readSQLValueFunction();
else if (MATCH("XMLEXPR", 7))
return_value = _readXmlExpr();
else if (MATCH("NULLTEST", 8))
context))
return true;
+ if (IsA(node, SQLValueFunction))
+ {
+ /* all variants of SQLValueFunction are stable */
+ return true;
+ }
+
/*
* It should be safe to treat MinMaxExpr as immutable, because it will
* depend on a non-cross-type btree comparison function, and those should
/*
* See notes in contain_mutable_functions_walker about why we treat
- * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable.
+ * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
+ * SQLValueFunction is stable. Hence, none of them are of interest here.
*/
/* Recurse to check arguments */
/*
* See notes in contain_mutable_functions_walker about why we treat
- * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable.
+ * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
+ * SQLValueFunction is stable. Hence, none of them are of interest here.
*/
/* Recurse to check arguments */
* (Note: in principle that's wrong because a domain constraint could
* contain a parallel-unsafe function; but useful constraints probably
* never would have such, and assuming they do would cripple use of
- * parallel query in the presence of domain types.)
+ * parallel query in the presence of domain types.) SQLValueFunction
+ * should be safe in all cases.
*/
if (IsA(node, CoerceToDomain))
{
case T_CaseTestExpr:
case T_RowExpr:
case T_MinMaxExpr:
+ case T_SQLValueFunction:
case T_NullTest:
case T_BooleanTest:
case T_List:
static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location);
static Node *makeNotExpr(Node *expr, int location);
static Node *makeAArrayExpr(List *elements, int location);
+static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod,
+ int location);
static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
List *args, int location);
static List *mergeTableFuncParameters(List *func_args, List *columns);
}
| CURRENT_DATE
{
- /*
- * Translate as "'now'::text::date".
- *
- * We cannot use "'now'::date" because coerce_type() will
- * immediately reduce that to a constant representing
- * today's date. We need to delay the conversion until
- * runtime, else the wrong things will happen when
- * CURRENT_DATE is used in a column default value or rule.
- *
- * This could be simplified if we had a way to generate
- * an expression tree representing runtime application
- * of type-input conversion functions. (As of PG 7.3
- * that is actually possible, but not clear that we want
- * to rely on it.)
- *
- * The token location is attached to the run-time
- * typecast, not to the Const, for the convenience of
- * pg_stat_statements (which doesn't want these constructs
- * to appear to be replaceable constants).
- */
- Node *n;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- $$ = makeTypeCast(n, SystemTypeName("date"), @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_DATE, -1, @1);
}
| CURRENT_TIME
{
- /*
- * Translate as "'now'::text::timetz".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- $$ = makeTypeCast(n, SystemTypeName("timetz"), @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_TIME, -1, @1);
}
| CURRENT_TIME '(' Iconst ')'
{
- /*
- * Translate as "'now'::text::timetz(n)".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- TypeName *d;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- d = SystemTypeName("timetz");
- d->typmods = list_make1(makeIntConst($3, @3));
- $$ = makeTypeCast(n, d, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_TIME_N, $3, @1);
}
| CURRENT_TIMESTAMP
{
- /*
- * Translate as "now()", since we have a function that
- * does exactly what is needed.
- */
- $$ = (Node *) makeFuncCall(SystemFuncName("now"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP, -1, @1);
}
| CURRENT_TIMESTAMP '(' Iconst ')'
{
- /*
- * Translate as "'now'::text::timestamptz(n)".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- TypeName *d;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- d = SystemTypeName("timestamptz");
- d->typmods = list_make1(makeIntConst($3, @3));
- $$ = makeTypeCast(n, d, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_TIMESTAMP_N, $3, @1);
}
| LOCALTIME
{
- /*
- * Translate as "'now'::text::time".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- $$ = makeTypeCast((Node *)n, SystemTypeName("time"), @1);
+ $$ = makeSQLValueFunction(SVFOP_LOCALTIME, -1, @1);
}
| LOCALTIME '(' Iconst ')'
{
- /*
- * Translate as "'now'::text::time(n)".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- TypeName *d;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- d = SystemTypeName("time");
- d->typmods = list_make1(makeIntConst($3, @3));
- $$ = makeTypeCast((Node *)n, d, @1);
+ $$ = makeSQLValueFunction(SVFOP_LOCALTIME_N, $3, @1);
}
| LOCALTIMESTAMP
{
- /*
- * Translate as "'now'::text::timestamp".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- $$ = makeTypeCast(n, SystemTypeName("timestamp"), @1);
+ $$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP, -1, @1);
}
| LOCALTIMESTAMP '(' Iconst ')'
{
- /*
- * Translate as "'now'::text::timestamp(n)".
- * See comments for CURRENT_DATE.
- */
- Node *n;
- TypeName *d;
- n = makeStringConstCast("now", -1, SystemTypeName("text"));
- d = SystemTypeName("timestamp");
- d->typmods = list_make1(makeIntConst($3, @3));
- $$ = makeTypeCast(n, d, @1);
+ $$ = makeSQLValueFunction(SVFOP_LOCALTIMESTAMP_N, $3, @1);
}
| CURRENT_ROLE
{
- $$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
}
| CURRENT_USER
{
- $$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
}
| SESSION_USER
{
- $$ = (Node *) makeFuncCall(SystemFuncName("session_user"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
}
| USER
{
- $$ = (Node *) makeFuncCall(SystemFuncName("current_user"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
}
| CURRENT_CATALOG
{
- $$ = (Node *) makeFuncCall(SystemFuncName("current_database"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
}
| CURRENT_SCHEMA
{
- $$ = (Node *) makeFuncCall(SystemFuncName("current_schema"), NIL, @1);
+ $$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
}
| CAST '(' a_expr AS Typename ')'
{ $$ = makeTypeCast($3, $5, @1); }
return (Node *) n;
}
+static Node *
+makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location)
+{
+ SQLValueFunction *svf = makeNode(SQLValueFunction);
+
+ svf->op = op;
+ /* svf->type will be filled during parse analysis */
+ svf->typmod = typmod;
+ svf->location = location;
+ return (Node *) svf;
+}
+
static Node *
makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
int location)
#include "parser/parse_type.h"
#include "parser/parse_agg.h"
#include "utils/builtins.h"
+#include "utils/date.h"
#include "utils/lsyscache.h"
+#include "utils/timestamp.h"
#include "utils/xml.h"
static Node *transformRowExpr(ParseState *pstate, RowExpr *r);
static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c);
static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
+static Node *transformSQLValueFunction(ParseState *pstate,
+ SQLValueFunction *svf);
static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs);
static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
break;
+ case T_SQLValueFunction:
+ result = transformSQLValueFunction(pstate,
+ (SQLValueFunction *) expr);
+ break;
+
case T_XmlExpr:
result = transformXmlExpr(pstate, (XmlExpr *) expr);
break;
return (Node *) newm;
}
+static Node *
+transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
+{
+ /*
+ * All we need to do is insert the correct result type and (where needed)
+ * validate the typmod, so we just modify the node in-place.
+ */
+ switch (svf->op)
+ {
+ case SVFOP_CURRENT_DATE:
+ svf->type = DATEOID;
+ break;
+ case SVFOP_CURRENT_TIME:
+ svf->type = TIMETZOID;
+ break;
+ case SVFOP_CURRENT_TIME_N:
+ svf->type = TIMETZOID;
+ svf->typmod = anytime_typmod_check(true, svf->typmod);
+ break;
+ case SVFOP_CURRENT_TIMESTAMP:
+ svf->type = TIMESTAMPTZOID;
+ break;
+ case SVFOP_CURRENT_TIMESTAMP_N:
+ svf->type = TIMESTAMPTZOID;
+ svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
+ break;
+ case SVFOP_LOCALTIME:
+ svf->type = TIMEOID;
+ break;
+ case SVFOP_LOCALTIME_N:
+ svf->type = TIMEOID;
+ svf->typmod = anytime_typmod_check(false, svf->typmod);
+ break;
+ case SVFOP_LOCALTIMESTAMP:
+ svf->type = TIMESTAMPOID;
+ break;
+ case SVFOP_LOCALTIMESTAMP_N:
+ svf->type = TIMESTAMPOID;
+ svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
+ break;
+ case SVFOP_CURRENT_ROLE:
+ case SVFOP_CURRENT_USER:
+ case SVFOP_USER:
+ case SVFOP_SESSION_USER:
+ case SVFOP_CURRENT_CATALOG:
+ case SVFOP_CURRENT_SCHEMA:
+ svf->type = NAMEOID;
+ break;
+ }
+
+ return (Node *) svf;
+}
+
static Node *
transformXmlExpr(ParseState *pstate, XmlExpr *x)
{
return 2;
}
break;
+ case T_SQLValueFunction:
+ /* make these act like a function or variable */
+ switch (((SQLValueFunction *) node)->op)
+ {
+ case SVFOP_CURRENT_DATE:
+ *name = "current_date";
+ return 2;
+ case SVFOP_CURRENT_TIME:
+ case SVFOP_CURRENT_TIME_N:
+ *name = "current_time";
+ return 2;
+ case SVFOP_CURRENT_TIMESTAMP:
+ case SVFOP_CURRENT_TIMESTAMP_N:
+ *name = "current_timestamp";
+ return 2;
+ case SVFOP_LOCALTIME:
+ case SVFOP_LOCALTIME_N:
+ *name = "localtime";
+ return 2;
+ case SVFOP_LOCALTIMESTAMP:
+ case SVFOP_LOCALTIMESTAMP_N:
+ *name = "localtimestamp";
+ return 2;
+ case SVFOP_CURRENT_ROLE:
+ *name = "current_role";
+ return 2;
+ case SVFOP_CURRENT_USER:
+ *name = "current_user";
+ return 2;
+ case SVFOP_USER:
+ *name = "user";
+ return 2;
+ case SVFOP_SESSION_USER:
+ *name = "session_user";
+ return 2;
+ case SVFOP_CURRENT_CATALOG:
+ *name = "current_catalog";
+ return 2;
+ case SVFOP_CURRENT_SCHEMA:
+ *name = "current_schema";
+ return 2;
+ }
+ break;
case T_XmlExpr:
/* make SQL/XML functions act like a regular function */
switch (((XmlExpr *) node)->op)
#include <time.h>
#include "access/hash.h"
+#include "access/xact.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "parser/scansup.h"
static int32
anytime_typmodin(bool istz, ArrayType *ta)
{
- int32 typmod;
int32 *tl;
int n;
(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;
}
}
+/*
+ * 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
*/
case T_RowExpr:
case T_CoalesceExpr:
case T_MinMaxExpr:
+ case T_SQLValueFunction:
case T_XmlExpr:
case T_NullIfExpr:
case T_Aggref:
}
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;
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;
(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;
}
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)
{
}
#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
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;
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
}
- PG_RETURN_TIMESTAMP(result);
+ return result;
}
/* timestamptz_zone()
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 201608131
+#define CATALOG_VERSION_NO 201608161
#endif
T_RowCompareExpr,
T_CoalesceExpr,
T_MinMaxExpr,
+ T_SQLValueFunction,
T_XmlExpr,
T_NullTest,
T_BooleanTest,
int location; /* token location, or -1 if unknown */
} MinMaxExpr;
+/*
+ * SQLValueFunction - parameterless functions with special grammar productions
+ *
+ * The SQL standard categorizes some of these as <datetime value function>
+ * and others as <general value specification>. We call 'em SQLValueFunctions
+ * for lack of a better term. We store type and typmod of the result so that
+ * some code doesn't need to know each function individually, and because
+ * we would need to store typmod anyway for some of the datetime functions.
+ * Note that currently, all variants return non-collating datatypes, so we do
+ * not need a collation field; also, all these functions are stable.
+ */
+typedef enum SQLValueFunctionOp
+{
+ SVFOP_CURRENT_DATE,
+ SVFOP_CURRENT_TIME,
+ SVFOP_CURRENT_TIME_N,
+ SVFOP_CURRENT_TIMESTAMP,
+ SVFOP_CURRENT_TIMESTAMP_N,
+ SVFOP_LOCALTIME,
+ SVFOP_LOCALTIME_N,
+ SVFOP_LOCALTIMESTAMP,
+ SVFOP_LOCALTIMESTAMP_N,
+ SVFOP_CURRENT_ROLE,
+ SVFOP_CURRENT_USER,
+ SVFOP_USER,
+ SVFOP_SESSION_USER,
+ SVFOP_CURRENT_CATALOG,
+ SVFOP_CURRENT_SCHEMA
+} SQLValueFunctionOp;
+
+typedef struct SQLValueFunction
+{
+ Expr xpr;
+ SQLValueFunctionOp op; /* which function this is */
+ Oid type; /* result type/typmod */
+ int32 typmod;
+ int location; /* token location, or -1 if unknown */
+} SQLValueFunction;
+
/*
* XmlExpr - various SQL/XML functions requiring special grammar productions
*
/* date.c */
+extern int32 anytime_typmod_check(bool istz, int32 typmod);
extern double date2timestamp_no_overflow(DateADT dateVal);
extern void EncodeSpecialDate(DateADT dt, char *str);
+extern DateADT GetSQLCurrentDate(void);
+extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
+extern TimeADT GetSQLLocalTime(int32 typmod);
extern Datum date_in(PG_FUNCTION_ARGS);
extern Datum date_out(PG_FUNCTION_ARGS);
/* Internal routines (not fmgr-callable) */
+extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
+
extern TimestampTz GetCurrentTimestamp(void);
+extern TimestampTz GetSQLCurrentTimestamp(int32 typmod);
+extern Timestamp GetSQLLocalTimestamp(int32 typmod);
extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
long *secs, int *microsecs);
extern bool TimestampDifferenceExceeds(TimestampTz start_time,
return TRUE;
}
+ case T_SQLValueFunction:
+ return TRUE;
+
case T_XmlExpr:
{
XmlExpr *expr = (XmlExpr *) node;
Filter: (dlevel <= $0)
InitPlan 1 (returns $0)
-> Index Scan using uaccount_pkey on uaccount
- Index Cond: (pguser = "current_user"())
+ Index Cond: (pguser = CURRENT_USER)
(7 rows)
EXPLAIN (COSTS OFF) SELECT * FROM document NATURAL JOIN category WHERE f_leak(dtitle);
Filter: (dlevel <= $0)
InitPlan 1 (returns $0)
-> Index Scan using uaccount_pkey on uaccount
- Index Cond: (pguser = "current_user"())
+ Index Cond: (pguser = CURRENT_USER)
(11 rows)
-- only owner can change policies
(3 rows)
EXPLAIN (COSTS OFF) SELECT * FROM document WHERE f_leak(dtitle);
- QUERY PLAN
-----------------------------------------------
+ QUERY PLAN
+------------------------------------------
Subquery Scan on document
Filter: f_leak(document.dtitle)
-> Seq Scan on document document_1
- Filter: (dauthor = "current_user"())
+ Filter: (dauthor = CURRENT_USER)
(4 rows)
EXPLAIN (COSTS OFF) SELECT * FROM document NATURAL JOIN category WHERE f_leak(dtitle);
- QUERY PLAN
-----------------------------------------------------
+ QUERY PLAN
+--------------------------------------------------
Nested Loop
-> Subquery Scan on document
Filter: f_leak(document.dtitle)
-> Seq Scan on document document_1
- Filter: (dauthor = "current_user"())
+ Filter: (dauthor = CURRENT_USER)
-> Index Scan using category_pkey on category
Index Cond: (cid = document.cid)
(7 rows)
WHERE (new.name = old.name) DO SELECT set_config(old.name, new.setting, false) AS set_config;
rtest_emp|rtest_emp_del|CREATE RULE rtest_emp_del AS
ON DELETE TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, action, newsal, oldsal)
- VALUES (old.ename, "current_user"(), 'fired'::bpchar, '$0.00'::money, old.salary);
+ VALUES (old.ename, CURRENT_USER, 'fired'::bpchar, '$0.00'::money, old.salary);
rtest_emp|rtest_emp_ins|CREATE RULE rtest_emp_ins AS
ON INSERT TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, action, newsal, oldsal)
- VALUES (new.ename, "current_user"(), 'hired'::bpchar, new.salary, '$0.00'::money);
+ VALUES (new.ename, CURRENT_USER, 'hired'::bpchar, new.salary, '$0.00'::money);
rtest_emp|rtest_emp_upd|CREATE RULE rtest_emp_upd AS
ON UPDATE TO rtest_emp
WHERE (new.salary <> old.salary) DO INSERT INTO rtest_emplog (ename, who, action, newsal, oldsal)
- VALUES (new.ename, "current_user"(), 'honored'::bpchar, new.salary, old.salary);
+ VALUES (new.ename, CURRENT_USER, 'honored'::bpchar, new.salary, old.salary);
rtest_nothn1|rtest_nothn_r1|CREATE RULE rtest_nothn_r1 AS
ON INSERT TO rtest_nothn1
WHERE ((new.a >= 10) AND (new.a < 20)) DO INSTEAD NOTHING;
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_property_normal WHERE f_leak(passwd);
- QUERY PLAN
-------------------------------------------------------------------
+ QUERY PLAN
+--------------------------------------------------------------
Seq Scan on customer
- Filter: (f_leak(passwd) AND (name = ("current_user"())::text))
+ Filter: (f_leak(passwd) AND (name = (CURRENT_USER)::text))
(2 rows)
SELECT * FROM my_property_secure WHERE f_leak(passwd);
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_property_secure WHERE f_leak(passwd);
- QUERY PLAN
----------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------
Subquery Scan on my_property_secure
Filter: f_leak(my_property_secure.passwd)
-> Seq Scan on customer
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(4 rows)
--
EXPLAIN (COSTS OFF) SELECT * FROM my_property_normal v
WHERE f_leak('passwd') AND f_leak(passwd);
- QUERY PLAN
----------------------------------------------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------------------------------------------
Seq Scan on customer
- Filter: (f_leak('passwd'::text) AND f_leak(passwd) AND (name = ("current_user"())::text))
+ Filter: (f_leak('passwd'::text) AND f_leak(passwd) AND (name = (CURRENT_USER)::text))
(2 rows)
SELECT * FROM my_property_secure v
EXPLAIN (COSTS OFF) SELECT * FROM my_property_secure v
WHERE f_leak('passwd') AND f_leak(passwd);
- QUERY PLAN
---------------------------------------------------------------------------------
+ QUERY PLAN
+----------------------------------------------------------------------------
Subquery Scan on v
Filter: f_leak(v.passwd)
-> Seq Scan on customer
- Filter: (f_leak('passwd'::text) AND (name = ("current_user"())::text))
+ Filter: (f_leak('passwd'::text) AND (name = (CURRENT_USER)::text))
(4 rows)
--
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_credit_card_normal WHERE f_leak(cnum);
- QUERY PLAN
----------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------
Hash Join
Hash Cond: (r.cid = l.cid)
-> Seq Scan on credit_card r
Filter: f_leak(cnum)
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(7 rows)
SELECT * FROM my_credit_card_secure WHERE f_leak(cnum);
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_credit_card_secure WHERE f_leak(cnum);
- QUERY PLAN
----------------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------------
Subquery Scan on my_credit_card_secure
Filter: f_leak(my_credit_card_secure.cnum)
-> Hash Join
-> Seq Scan on credit_card r
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(8 rows)
--
-> Seq Scan on credit_card r_1
-> Hash
-> Seq Scan on customer l_1
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(13 rows)
SELECT * FROM my_credit_card_usage_secure
-> Seq Scan on credit_card r_1
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(13 rows)
--
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_property_normal WHERE f_leak(passwd);
- QUERY PLAN
-------------------------------------------------------------------
+ QUERY PLAN
+--------------------------------------------------------------
Seq Scan on customer
- Filter: (f_leak(passwd) AND (name = ("current_user"())::text))
+ Filter: (f_leak(passwd) AND (name = (CURRENT_USER)::text))
(2 rows)
SELECT * FROM my_property_secure WHERE f_leak(passwd);
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_property_secure WHERE f_leak(passwd);
- QUERY PLAN
----------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------
Subquery Scan on my_property_secure
Filter: f_leak(my_property_secure.passwd)
-> Seq Scan on customer
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(4 rows)
--
EXPLAIN (COSTS OFF) SELECT * FROM my_property_normal v
WHERE f_leak('passwd') AND f_leak(passwd);
- QUERY PLAN
----------------------------------------------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------------------------------------------
Seq Scan on customer
- Filter: (f_leak('passwd'::text) AND f_leak(passwd) AND (name = ("current_user"())::text))
+ Filter: (f_leak('passwd'::text) AND f_leak(passwd) AND (name = (CURRENT_USER)::text))
(2 rows)
SELECT * FROM my_property_secure v
EXPLAIN (COSTS OFF) SELECT * FROM my_property_secure v
WHERE f_leak('passwd') AND f_leak(passwd);
- QUERY PLAN
---------------------------------------------------------------------------------
+ QUERY PLAN
+----------------------------------------------------------------------------
Subquery Scan on v
Filter: f_leak(v.passwd)
-> Seq Scan on customer
- Filter: (f_leak('passwd'::text) AND (name = ("current_user"())::text))
+ Filter: (f_leak('passwd'::text) AND (name = (CURRENT_USER)::text))
(4 rows)
--
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_credit_card_normal WHERE f_leak(cnum);
- QUERY PLAN
----------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------
Hash Join
Hash Cond: (r.cid = l.cid)
-> Seq Scan on credit_card r
Filter: f_leak(cnum)
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(7 rows)
SELECT * FROM my_credit_card_secure WHERE f_leak(cnum);
(1 row)
EXPLAIN (COSTS OFF) SELECT * FROM my_credit_card_secure WHERE f_leak(cnum);
- QUERY PLAN
----------------------------------------------------------------
+ QUERY PLAN
+-----------------------------------------------------------
Subquery Scan on my_credit_card_secure
Filter: f_leak(my_credit_card_secure.cnum)
-> Hash Join
-> Seq Scan on credit_card r
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(8 rows)
--
-> Seq Scan on credit_card r_1
-> Hash
-> Seq Scan on customer l_1
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(13 rows)
SELECT * FROM my_credit_card_usage_secure
-> Seq Scan on credit_card r_1
-> Hash
-> Seq Scan on customer l
- Filter: (name = ("current_user"())::text)
+ Filter: (name = (CURRENT_USER)::text)
(13 rows)
--