1997-11-25 23:07:18 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* parse_expr.c
|
|
|
|
* handle expressions in parser
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-05-30 02:49:57 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.79 2000/05/30 00:49:50 momjian Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
1999-08-26 01:21:43 +02:00
|
|
|
|
|
|
|
#include "catalog/pg_operator.h"
|
2000-02-26 22:11:10 +01:00
|
|
|
#include "catalog/pg_proc.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "nodes/params.h"
|
1998-01-19 06:06:41 +01:00
|
|
|
#include "parser/analyze.h"
|
1997-11-26 04:43:18 +01:00
|
|
|
#include "parser/gramparse.h"
|
2000-05-29 07:45:56 +02:00
|
|
|
#include "parser/parse.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_expr.h"
|
|
|
|
#include "parser/parse_func.h"
|
1999-08-26 01:21:43 +02:00
|
|
|
#include "parser/parse_oper.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_relation.h"
|
|
|
|
#include "parser/parse_target.h"
|
|
|
|
#include "utils/builtins.h"
|
2000-02-26 22:11:10 +01:00
|
|
|
#include "utils/syscache.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-17 06:29:07 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
int max_expr_depth = DEFAULT_MAX_EXPR_DEPTH;
|
2000-03-17 06:29:07 +01:00
|
|
|
|
|
|
|
static int expr_depth_counter = 0;
|
|
|
|
|
2000-01-17 01:14:49 +01:00
|
|
|
static Node *parser_typecast_constant(Value *expr, TypeName *typename);
|
|
|
|
static Node *parser_typecast_expression(ParseState *pstate,
|
2000-04-12 19:17:23 +02:00
|
|
|
Node *expr, TypeName *typename);
|
1999-07-17 00:32:25 +02:00
|
|
|
static Node *transformAttr(ParseState *pstate, Attr *att, int precedence);
|
|
|
|
static Node *transformIdent(ParseState *pstate, Ident *ident, int precedence);
|
|
|
|
static Node *transformIndirection(ParseState *pstate, Node *basenode,
|
2000-04-12 19:17:23 +02:00
|
|
|
List *indirection);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-17 06:29:07 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize for parsing a new query.
|
|
|
|
*
|
|
|
|
* We reset the expression depth counter here, in case it was left nonzero
|
|
|
|
* due to elog()'ing out of the last parsing operation.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parse_expr_init(void)
|
|
|
|
{
|
|
|
|
expr_depth_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
|
|
|
* transformExpr -
|
2000-03-19 08:13:58 +01:00
|
|
|
* Analyze and transform expressions. Type checking and type casting is
|
1997-11-25 23:07:18 +01:00
|
|
|
* done here. The optimizer and the executor cannot handle the original
|
|
|
|
* (raw) expressions collected by the parse tree. Hence the transformation
|
|
|
|
* here.
|
2000-03-19 08:13:58 +01:00
|
|
|
*
|
|
|
|
* NOTE: there are various cases in which this routine will get applied to
|
|
|
|
* an already-transformed expression. Some examples:
|
|
|
|
* 1. At least one construct (BETWEEN/AND) puts the same nodes
|
|
|
|
* into two branches of the parse tree; hence, some nodes
|
|
|
|
* are transformed twice.
|
|
|
|
* 2. Another way it can happen is that coercion of an operator or
|
|
|
|
* function argument to the required type (via coerce_type())
|
|
|
|
* can apply transformExpr to an already-transformed subexpression.
|
|
|
|
* An example here is "SELECT count(*) + 1.0 FROM table".
|
|
|
|
* While it might be possible to eliminate these cases, the path of
|
|
|
|
* least resistance so far has been to ensure that transformExpr() does
|
|
|
|
* no damage if applied to an already-transformed tree. This is pretty
|
|
|
|
* easy for cases where the transformation replaces one node type with
|
|
|
|
* another, such as A_Const => Const; we just do nothing when handed
|
|
|
|
* a Const. More care is needed for node types that are used as both
|
|
|
|
* input and output of transformExpr; see SubLink for example.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
|
|
|
Node *
|
|
|
|
transformExpr(ParseState *pstate, Node *expr, int precedence)
|
|
|
|
{
|
|
|
|
Node *result = NULL;
|
|
|
|
|
|
|
|
if (expr == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2000-03-17 06:29:07 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Guard against an overly complex expression leading to coredump due
|
|
|
|
* to stack overflow here, or in later recursive routines that
|
2000-03-17 06:29:07 +01:00
|
|
|
* traverse expression trees. Note that this is very unlikely to
|
|
|
|
* happen except with pathological queries; but we don't want someone
|
|
|
|
* to be able to crash the backend quite that easily...
|
|
|
|
*/
|
|
|
|
if (++expr_depth_counter > max_expr_depth)
|
|
|
|
elog(ERROR, "Expression too complex: nesting depth exceeds max_expr_depth = %d",
|
|
|
|
max_expr_depth);
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
switch (nodeTag(expr))
|
|
|
|
{
|
|
|
|
case T_Attr:
|
|
|
|
{
|
1999-07-17 00:32:25 +02:00
|
|
|
result = transformAttr(pstate, (Attr *) expr, precedence);
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_A_Const:
|
|
|
|
{
|
|
|
|
A_Const *con = (A_Const *) expr;
|
|
|
|
Value *val = &con->val;
|
|
|
|
|
|
|
|
if (con->typename != NULL)
|
2000-01-17 01:14:49 +01:00
|
|
|
result = parser_typecast_constant(val, con->typename);
|
1997-11-25 23:07:18 +01:00
|
|
|
else
|
|
|
|
result = (Node *) make_const(val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_ParamNo:
|
|
|
|
{
|
|
|
|
ParamNo *pno = (ParamNo *) expr;
|
1999-07-17 00:32:25 +02:00
|
|
|
int paramno = pno->number;
|
|
|
|
Oid toid = param_type(paramno);
|
2000-03-08 00:30:53 +01:00
|
|
|
Param *param = makeNode(Param);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
if (!OidIsValid(toid))
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "Parameter '$%d' is out of range", paramno);
|
1997-11-25 23:07:18 +01:00
|
|
|
param->paramkind = PARAM_NUM;
|
|
|
|
param->paramid = (AttrNumber) paramno;
|
|
|
|
param->paramname = "<unnamed>";
|
2000-03-08 00:30:53 +01:00
|
|
|
param->paramtype = toid;
|
|
|
|
param->param_tlist = NIL;
|
1999-07-17 00:32:25 +02:00
|
|
|
result = transformIndirection(pstate, (Node *) param,
|
1999-07-19 02:26:20 +02:00
|
|
|
pno->indirection);
|
2000-03-08 00:30:53 +01:00
|
|
|
/* cope with typecast applied to param */
|
|
|
|
if (pno->typename != NULL)
|
|
|
|
result = parser_typecast_expression(pstate, result,
|
|
|
|
pno->typename);
|
2000-01-17 01:14:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_TypeCast:
|
|
|
|
{
|
|
|
|
TypeCast *tc = (TypeCast *) expr;
|
|
|
|
Node *arg = transformExpr(pstate, tc->arg, precedence);
|
|
|
|
|
|
|
|
result = parser_typecast_expression(pstate, arg, tc->typename);
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_A_Expr:
|
|
|
|
{
|
|
|
|
A_Expr *a = (A_Expr *) expr;
|
|
|
|
|
|
|
|
switch (a->oper)
|
|
|
|
{
|
|
|
|
case OP:
|
|
|
|
{
|
|
|
|
Node *lexpr = transformExpr(pstate, a->lexpr, precedence);
|
|
|
|
Node *rexpr = transformExpr(pstate, a->rexpr, precedence);
|
|
|
|
|
|
|
|
result = (Node *) make_op(a->opname, lexpr, rexpr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISNULL:
|
|
|
|
{
|
|
|
|
Node *lexpr = transformExpr(pstate, a->lexpr, precedence);
|
|
|
|
|
1998-01-20 06:05:08 +01:00
|
|
|
result = ParseFuncOrColumn(pstate,
|
1999-12-10 08:37:35 +01:00
|
|
|
"nullvalue",
|
|
|
|
lcons(lexpr, NIL),
|
|
|
|
false, false,
|
2000-04-12 19:17:23 +02:00
|
|
|
&pstate->p_last_resno,
|
1998-02-26 05:46:47 +01:00
|
|
|
precedence);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NOTNULL:
|
|
|
|
{
|
|
|
|
Node *lexpr = transformExpr(pstate, a->lexpr, precedence);
|
|
|
|
|
1998-01-20 06:05:08 +01:00
|
|
|
result = ParseFuncOrColumn(pstate,
|
1999-12-10 08:37:35 +01:00
|
|
|
"nonnullvalue",
|
|
|
|
lcons(lexpr, NIL),
|
|
|
|
false, false,
|
2000-04-12 19:17:23 +02:00
|
|
|
&pstate->p_last_resno,
|
1998-02-26 05:46:47 +01:00
|
|
|
precedence);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AND:
|
|
|
|
{
|
|
|
|
Expr *expr = makeNode(Expr);
|
|
|
|
Node *lexpr = transformExpr(pstate, a->lexpr, precedence);
|
|
|
|
Node *rexpr = transformExpr(pstate, a->rexpr, precedence);
|
|
|
|
|
|
|
|
if (exprType(lexpr) != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "left-hand side of AND is type '%s', not '%s'",
|
|
|
|
typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
|
1997-12-23 20:36:20 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
if (exprType(rexpr) != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "right-hand side of AND is type '%s', not '%s'",
|
|
|
|
typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
|
1997-12-23 20:36:20 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
expr->typeOid = BOOLOID;
|
|
|
|
expr->opType = AND_EXPR;
|
|
|
|
expr->args = makeList(lexpr, rexpr, -1);
|
|
|
|
result = (Node *) expr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OR:
|
|
|
|
{
|
|
|
|
Expr *expr = makeNode(Expr);
|
|
|
|
Node *lexpr = transformExpr(pstate, a->lexpr, precedence);
|
|
|
|
Node *rexpr = transformExpr(pstate, a->rexpr, precedence);
|
|
|
|
|
|
|
|
if (exprType(lexpr) != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "left-hand side of OR is type '%s', not '%s'",
|
|
|
|
typeidTypeName(exprType(lexpr)), typeidTypeName(BOOLOID));
|
1997-11-25 23:07:18 +01:00
|
|
|
if (exprType(rexpr) != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "right-hand side of OR is type '%s', not '%s'",
|
|
|
|
typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
|
1997-11-25 23:07:18 +01:00
|
|
|
expr->typeOid = BOOLOID;
|
|
|
|
expr->opType = OR_EXPR;
|
|
|
|
expr->args = makeList(lexpr, rexpr, -1);
|
|
|
|
result = (Node *) expr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NOT:
|
|
|
|
{
|
|
|
|
Expr *expr = makeNode(Expr);
|
|
|
|
Node *rexpr = transformExpr(pstate, a->rexpr, precedence);
|
|
|
|
|
|
|
|
if (exprType(rexpr) != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "argument to NOT is type '%s', not '%s'",
|
|
|
|
typeidTypeName(exprType(rexpr)), typeidTypeName(BOOLOID));
|
1997-11-25 23:07:18 +01:00
|
|
|
expr->typeOid = BOOLOID;
|
|
|
|
expr->opType = NOT_EXPR;
|
|
|
|
expr->args = makeList(rexpr, -1);
|
|
|
|
result = (Node *) expr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_Ident:
|
|
|
|
{
|
1999-07-17 00:32:25 +02:00
|
|
|
result = transformIdent(pstate, (Ident *) expr, precedence);
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_FuncCall:
|
|
|
|
{
|
|
|
|
FuncCall *fn = (FuncCall *) expr;
|
|
|
|
List *args;
|
|
|
|
|
|
|
|
/* transform the list of arguments */
|
|
|
|
foreach(args, fn->args)
|
|
|
|
lfirst(args) = transformExpr(pstate, (Node *) lfirst(args), precedence);
|
1998-01-20 06:05:08 +01:00
|
|
|
result = ParseFuncOrColumn(pstate,
|
1998-12-04 16:34:49 +01:00
|
|
|
fn->funcname,
|
|
|
|
fn->args,
|
1999-12-10 08:37:35 +01:00
|
|
|
fn->agg_star,
|
|
|
|
fn->agg_distinct,
|
1998-12-04 16:34:49 +01:00
|
|
|
&pstate->p_last_resno,
|
1998-02-26 05:46:47 +01:00
|
|
|
precedence);
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
1998-01-19 06:06:41 +01:00
|
|
|
case T_SubLink:
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
SubLink *sublink = (SubLink *) expr;
|
1999-05-13 09:29:22 +02:00
|
|
|
List *qtrees;
|
|
|
|
Query *qtree;
|
1998-02-06 17:46:29 +01:00
|
|
|
|
2000-03-19 08:13:58 +01:00
|
|
|
/* If we already transformed this node, do nothing */
|
|
|
|
if (IsA(sublink->subselect, Query))
|
|
|
|
{
|
|
|
|
result = expr;
|
|
|
|
break;
|
|
|
|
}
|
1998-02-06 17:46:29 +01:00
|
|
|
pstate->p_hasSubLinks = true;
|
1999-05-13 09:29:22 +02:00
|
|
|
qtrees = parse_analyze(lcons(sublink->subselect, NIL), pstate);
|
|
|
|
if (length(qtrees) != 1)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Bad query in subselect");
|
1999-05-13 09:29:22 +02:00
|
|
|
qtree = (Query *) lfirst(qtrees);
|
|
|
|
if (qtree->commandType != CMD_SELECT ||
|
|
|
|
qtree->resultRelation != 0)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Bad query in subselect");
|
1999-05-13 09:29:22 +02:00
|
|
|
sublink->subselect = (Node *) qtree;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-08-26 01:21:43 +02:00
|
|
|
if (sublink->subLinkType == EXISTS_SUBLINK)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EXISTS needs no lefthand or combining operator.
|
1999-08-26 01:21:43 +02:00
|
|
|
* These fields should be NIL already, but make sure.
|
|
|
|
*/
|
|
|
|
sublink->lefthand = NIL;
|
|
|
|
sublink->oper = NIL;
|
|
|
|
}
|
1999-11-15 03:00:15 +01:00
|
|
|
else if (sublink->subLinkType == EXPR_SUBLINK)
|
|
|
|
{
|
|
|
|
List *tlist = qtree->targetList;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* Make sure the subselect delivers a single column
|
1999-11-15 03:00:15 +01:00
|
|
|
* (ignoring resjunk targets).
|
|
|
|
*/
|
|
|
|
if (tlist == NIL ||
|
|
|
|
((TargetEntry *) lfirst(tlist))->resdom->resjunk)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Subselect must have a field");
|
1999-11-15 03:00:15 +01:00
|
|
|
while ((tlist = lnext(tlist)) != NIL)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!((TargetEntry *) lfirst(tlist))->resdom->resjunk)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Subselect must have only one field");
|
1999-11-15 03:00:15 +01:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EXPR needs no lefthand or combining operator. These
|
|
|
|
* fields should be NIL already, but make sure.
|
1999-11-15 03:00:15 +01:00
|
|
|
*/
|
|
|
|
sublink->lefthand = NIL;
|
|
|
|
sublink->oper = NIL;
|
|
|
|
}
|
1999-08-26 01:21:43 +02:00
|
|
|
else
|
1998-01-19 06:06:41 +01:00
|
|
|
{
|
1999-11-15 03:00:15 +01:00
|
|
|
/* ALL, ANY, or MULTIEXPR: generate operator list */
|
1999-07-11 04:04:19 +02:00
|
|
|
List *left_list = sublink->lefthand;
|
|
|
|
List *right_list = qtree->targetList;
|
2000-05-26 00:42:19 +02:00
|
|
|
char *op;
|
1998-02-26 05:46:47 +01:00
|
|
|
List *elist;
|
1998-01-20 06:05:08 +01:00
|
|
|
|
1999-07-11 04:04:19 +02:00
|
|
|
foreach(elist, left_list)
|
|
|
|
lfirst(elist) = transformExpr(pstate, lfirst(elist),
|
|
|
|
precedence);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2000-05-26 00:42:19 +02:00
|
|
|
Assert(IsA(sublink->oper, A_Expr));
|
|
|
|
op = ((A_Expr *) sublink->oper)->opname;
|
|
|
|
sublink->oper = NIL;
|
|
|
|
|
1999-11-15 03:00:15 +01:00
|
|
|
/* Combining operators other than =/<> is dubious... */
|
|
|
|
if (length(left_list) != 1 &&
|
1998-09-01 06:40:42 +02:00
|
|
|
strcmp(op, "=") != 0 && strcmp(op, "<>") != 0)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Row comparison cannot use '%s'",
|
1999-07-11 04:04:19 +02:00
|
|
|
op);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* Scan subquery's targetlist to find values that will
|
|
|
|
* be matched against lefthand values. We need to
|
|
|
|
* ignore resjunk targets, so doing the outer
|
|
|
|
* iteration over right_list is easier than doing it
|
|
|
|
* over left_list.
|
1999-07-11 04:04:19 +02:00
|
|
|
*/
|
|
|
|
while (right_list != NIL)
|
1998-01-20 06:05:08 +01:00
|
|
|
{
|
1999-07-11 04:04:19 +02:00
|
|
|
TargetEntry *tent = (TargetEntry *) lfirst(right_list);
|
|
|
|
Node *lexpr;
|
1999-08-26 01:21:43 +02:00
|
|
|
Operator optup;
|
|
|
|
Form_pg_operator opform;
|
|
|
|
Oper *newop;
|
1998-01-20 06:05:08 +01:00
|
|
|
|
1999-07-11 04:04:19 +02:00
|
|
|
right_list = lnext(right_list);
|
1999-08-26 01:21:43 +02:00
|
|
|
if (tent->resdom->resjunk)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (left_list == NIL)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Subselect has too many fields");
|
1999-08-26 01:21:43 +02:00
|
|
|
lexpr = lfirst(left_list);
|
|
|
|
left_list = lnext(left_list);
|
|
|
|
|
|
|
|
optup = oper(op,
|
|
|
|
exprType(lexpr),
|
|
|
|
exprType(tent->expr),
|
|
|
|
FALSE);
|
|
|
|
opform = (Form_pg_operator) GETSTRUCT(optup);
|
|
|
|
|
1999-11-15 03:00:15 +01:00
|
|
|
if (opform->oprresult != BOOLOID)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "'%s' result type of '%s' must return '%s'"
|
|
|
|
" to be used with quantified predicate subquery",
|
|
|
|
op, typeidTypeName(opform->oprresult),
|
|
|
|
typeidTypeName(BOOLOID));
|
1999-08-26 01:21:43 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
newop = makeOper(oprid(optup), /* opno */
|
|
|
|
InvalidOid, /* opid */
|
1999-08-26 01:21:43 +02:00
|
|
|
opform->oprresult,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
sublink->oper = lappend(sublink->oper, newop);
|
1998-01-20 06:05:08 +01:00
|
|
|
}
|
1999-07-11 04:04:19 +02:00
|
|
|
if (left_list != NIL)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Subselect has too few fields");
|
1998-01-19 06:06:41 +01:00
|
|
|
}
|
1998-02-13 04:41:23 +01:00
|
|
|
result = (Node *) expr;
|
1998-01-19 06:06:41 +01:00
|
|
|
break;
|
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1998-12-04 16:34:49 +01:00
|
|
|
case T_CaseExpr:
|
|
|
|
{
|
|
|
|
CaseExpr *c = (CaseExpr *) expr;
|
|
|
|
CaseWhen *w;
|
|
|
|
List *args;
|
|
|
|
Oid ptype;
|
|
|
|
CATEGORY pcategory;
|
|
|
|
|
|
|
|
/* transform the list of arguments */
|
|
|
|
foreach(args, c->args)
|
|
|
|
{
|
|
|
|
w = lfirst(args);
|
|
|
|
if (c->arg != NULL)
|
|
|
|
{
|
1999-04-18 19:35:51 +02:00
|
|
|
/* shorthand form was specified, so expand... */
|
1999-05-25 18:15:34 +02:00
|
|
|
A_Expr *a = makeNode(A_Expr);
|
|
|
|
|
1998-12-04 16:34:49 +01:00
|
|
|
a->oper = OP;
|
|
|
|
a->opname = "=";
|
|
|
|
a->lexpr = c->arg;
|
|
|
|
a->rexpr = w->expr;
|
1999-05-25 18:15:34 +02:00
|
|
|
w->expr = (Node *) a;
|
1998-12-04 16:34:49 +01:00
|
|
|
}
|
|
|
|
lfirst(args) = transformExpr(pstate, (Node *) w, precedence);
|
|
|
|
}
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* It's not shorthand anymore, so drop the implicit
|
|
|
|
* argument. This is necessary to keep the executor from
|
2000-03-19 08:13:58 +01:00
|
|
|
* seeing an untransformed expression... not to mention
|
|
|
|
* keeping a re-application of transformExpr from doing
|
|
|
|
* the wrong thing.
|
1999-04-18 19:35:51 +02:00
|
|
|
*/
|
|
|
|
c->arg = NULL;
|
|
|
|
|
|
|
|
/* transform the default clause */
|
1998-12-04 16:34:49 +01:00
|
|
|
if (c->defresult == NULL)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
A_Const *n = makeNode(A_Const);
|
|
|
|
|
1998-12-04 16:34:49 +01:00
|
|
|
n->val.type = T_Null;
|
1999-05-25 18:15:34 +02:00
|
|
|
c->defresult = (Node *) n;
|
1998-12-04 16:34:49 +01:00
|
|
|
}
|
1999-12-24 07:43:34 +01:00
|
|
|
c->defresult = transformExpr(pstate, c->defresult, precedence);
|
1998-12-04 16:34:49 +01:00
|
|
|
|
|
|
|
/* now check types across result clauses... */
|
1999-04-18 19:35:51 +02:00
|
|
|
c->casetype = exprType(c->defresult);
|
1998-12-04 16:34:49 +01:00
|
|
|
ptype = c->casetype;
|
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
foreach(args, c->args)
|
|
|
|
{
|
|
|
|
Oid wtype;
|
|
|
|
|
|
|
|
w = lfirst(args);
|
|
|
|
wtype = exprType(w->result);
|
|
|
|
/* move on to next one if no new information... */
|
|
|
|
if (wtype && (wtype != UNKNOWNOID)
|
1999-05-25 18:15:34 +02:00
|
|
|
&& (wtype != ptype))
|
1998-12-04 16:34:49 +01:00
|
|
|
{
|
1999-12-24 07:43:34 +01:00
|
|
|
if (!ptype || ptype == UNKNOWNOID)
|
1998-12-04 16:34:49 +01:00
|
|
|
{
|
1999-12-24 07:43:34 +01:00
|
|
|
/* so far, only nulls so take anything... */
|
1998-12-04 16:34:49 +01:00
|
|
|
ptype = wtype;
|
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
}
|
|
|
|
else if ((TypeCategory(wtype) != pcategory)
|
1999-05-25 18:15:34 +02:00
|
|
|
|| ((TypeCategory(wtype) == USER_TYPE)
|
|
|
|
&& (TypeCategory(c->casetype) == USER_TYPE)))
|
1998-12-04 16:34:49 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-12-24 07:43:34 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* both types in different categories? then
|
|
|
|
* not much hope...
|
1999-12-24 07:43:34 +01:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
elog(ERROR, "CASE/WHEN types '%s' and '%s' not matched",
|
1998-12-04 16:34:49 +01:00
|
|
|
typeidTypeName(c->casetype), typeidTypeName(wtype));
|
|
|
|
}
|
|
|
|
else if (IsPreferredType(pcategory, wtype)
|
|
|
|
&& can_coerce_type(1, &ptype, &wtype))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-12-24 07:43:34 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* new one is preferred and can convert? then
|
|
|
|
* take it...
|
1999-12-24 07:43:34 +01:00
|
|
|
*/
|
1998-12-04 16:34:49 +01:00
|
|
|
ptype = wtype;
|
|
|
|
pcategory = TypeCategory(ptype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-14 00:56:44 +01:00
|
|
|
/* Convert default result clause, if necessary */
|
1998-12-04 16:34:49 +01:00
|
|
|
if (c->casetype != ptype)
|
|
|
|
{
|
1999-12-24 07:43:34 +01:00
|
|
|
if (!c->casetype || c->casetype == UNKNOWNOID)
|
1998-12-04 16:34:49 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* default clause is NULL, so assign preferred
|
|
|
|
* type from WHEN clauses...
|
|
|
|
*/
|
1998-12-04 16:34:49 +01:00
|
|
|
c->casetype = ptype;
|
|
|
|
}
|
|
|
|
else if (can_coerce_type(1, &c->casetype, &ptype))
|
|
|
|
{
|
1999-05-22 06:12:29 +02:00
|
|
|
c->defresult = coerce_type(pstate, c->defresult,
|
1999-05-25 18:15:34 +02:00
|
|
|
c->casetype, ptype, -1);
|
1998-12-04 16:34:49 +01:00
|
|
|
c->casetype = ptype;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "CASE/ELSE unable to convert to type '%s'",
|
1998-12-04 16:34:49 +01:00
|
|
|
typeidTypeName(ptype));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert when clauses, if not null and if necessary */
|
|
|
|
foreach(args, c->args)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
Oid wtype;
|
1998-12-04 16:34:49 +01:00
|
|
|
|
|
|
|
w = lfirst(args);
|
|
|
|
wtype = exprType(w->result);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* only bother with conversion if not NULL and
|
|
|
|
* different type...
|
|
|
|
*/
|
1999-09-13 06:14:56 +02:00
|
|
|
if (wtype && (wtype != UNKNOWNOID)
|
|
|
|
&& (wtype != ptype))
|
1998-12-04 16:34:49 +01:00
|
|
|
{
|
|
|
|
if (can_coerce_type(1, &wtype, &ptype))
|
|
|
|
{
|
1999-05-22 06:12:29 +02:00
|
|
|
w->result = coerce_type(pstate, w->result, wtype,
|
|
|
|
ptype, -1);
|
1998-12-04 16:34:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "CASE/WHEN unable to convert to type '%s'",
|
1998-12-04 16:34:49 +01:00
|
|
|
typeidTypeName(ptype));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = expr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_CaseWhen:
|
|
|
|
{
|
|
|
|
CaseWhen *w = (CaseWhen *) expr;
|
|
|
|
|
|
|
|
w->expr = transformExpr(pstate, (Node *) w->expr, precedence);
|
|
|
|
if (exprType(w->expr) != BOOLOID)
|
1999-05-25 18:15:34 +02:00
|
|
|
elog(ERROR, "WHEN clause must have a boolean result");
|
1998-12-04 16:34:49 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* result is NULL for NULLIF() construct - thomas
|
|
|
|
* 1998-11-11
|
|
|
|
*/
|
1998-12-14 00:56:44 +01:00
|
|
|
if (w->result == NULL)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
A_Const *n = makeNode(A_Const);
|
|
|
|
|
1998-12-14 00:56:44 +01:00
|
|
|
n->val.type = T_Null;
|
1999-05-25 18:15:34 +02:00
|
|
|
w->result = (Node *) n;
|
1998-12-14 00:56:44 +01:00
|
|
|
}
|
|
|
|
w->result = transformExpr(pstate, (Node *) w->result, precedence);
|
1998-12-04 16:34:49 +01:00
|
|
|
result = expr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* Quietly accept node types that may be presented when we are
|
|
|
|
* called on an already-transformed tree.
|
|
|
|
*
|
|
|
|
* Do any other node types need to be accepted? For now we are
|
|
|
|
* taking a conservative approach, and only accepting node
|
|
|
|
* types that are demonstrably necessary to accept.
|
|
|
|
*/
|
1998-03-26 22:08:10 +01:00
|
|
|
case T_Expr:
|
|
|
|
case T_Var:
|
|
|
|
case T_Const:
|
1998-07-08 16:04:11 +02:00
|
|
|
case T_Param:
|
1999-05-19 01:40:05 +02:00
|
|
|
case T_Aggref:
|
|
|
|
case T_ArrayRef:
|
2000-02-20 22:32:16 +01:00
|
|
|
case T_RelabelType:
|
1998-03-26 22:08:10 +01:00
|
|
|
{
|
|
|
|
result = (Node *) expr;
|
|
|
|
break;
|
|
|
|
}
|
2000-03-19 08:13:58 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
default:
|
|
|
|
/* should not reach here */
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "transformExpr: does not know how to transform node %d"
|
|
|
|
" (internal error)", nodeTag(expr));
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-03-17 06:29:07 +01:00
|
|
|
expr_depth_counter--;
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-05-26 14:57:23 +02:00
|
|
|
static Node *
|
1999-07-19 02:26:20 +02:00
|
|
|
transformIndirection(ParseState *pstate, Node *basenode, List *indirection)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-07-17 00:32:25 +02:00
|
|
|
if (indirection == NIL)
|
|
|
|
return basenode;
|
1999-07-19 02:26:20 +02:00
|
|
|
return (Node *) transformArraySubscripts(pstate, basenode,
|
|
|
|
indirection, false, NULL);
|
1999-07-17 00:32:25 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-07-17 00:32:25 +02:00
|
|
|
static Node *
|
|
|
|
transformAttr(ParseState *pstate, Attr *att, int precedence)
|
|
|
|
{
|
|
|
|
Node *basenode;
|
|
|
|
|
|
|
|
basenode = ParseNestedFuncOrColumn(pstate, att, &pstate->p_last_resno,
|
|
|
|
precedence);
|
1999-07-19 02:26:20 +02:00
|
|
|
return transformIndirection(pstate, basenode, att->indirection);
|
1999-07-17 00:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Node *
|
|
|
|
transformIdent(ParseState *pstate, Ident *ident, int precedence)
|
|
|
|
{
|
|
|
|
Node *result = NULL;
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* try to find the ident as a relation ... but not if subscripts
|
|
|
|
* appear
|
|
|
|
*/
|
1999-07-17 00:32:25 +02:00
|
|
|
if (ident->indirection == NIL &&
|
|
|
|
refnameRangeTableEntry(pstate, ident->name) != NULL)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
ident->isRel = TRUE;
|
1999-07-17 00:32:25 +02:00
|
|
|
result = (Node *) ident;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
1999-07-17 00:32:25 +02:00
|
|
|
if (result == NULL || precedence == EXPR_COLUMN_FIRST)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-07-17 00:32:25 +02:00
|
|
|
/* try to find the ident as a column */
|
|
|
|
if ((rte = colnameRangeTableEntry(pstate, ident->name)) != NULL)
|
|
|
|
{
|
|
|
|
/* Convert it to a fully qualified Attr, and transform that */
|
2000-03-15 00:06:59 +01:00
|
|
|
Attr *att = makeAttr(rte->eref->relname, ident->name);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-07-17 00:32:25 +02:00
|
|
|
att->indirection = ident->indirection;
|
|
|
|
return transformAttr(pstate, att, precedence);
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL)
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Attribute '%s' not found", ident->name);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exprType -
|
|
|
|
* returns the Oid of the type of the expression. (Used for typechecking.)
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
exprType(Node *expr)
|
|
|
|
{
|
1999-07-19 02:26:20 +02:00
|
|
|
Oid type = (Oid) InvalidOid;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-12-04 16:34:49 +01:00
|
|
|
if (!expr)
|
|
|
|
return type;
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
switch (nodeTag(expr))
|
|
|
|
{
|
|
|
|
case T_Func:
|
|
|
|
type = ((Func *) expr)->functype;
|
|
|
|
break;
|
|
|
|
case T_Iter:
|
|
|
|
type = ((Iter *) expr)->itertype;
|
|
|
|
break;
|
|
|
|
case T_Var:
|
|
|
|
type = ((Var *) expr)->vartype;
|
|
|
|
break;
|
|
|
|
case T_Expr:
|
|
|
|
type = ((Expr *) expr)->typeOid;
|
|
|
|
break;
|
|
|
|
case T_Const:
|
|
|
|
type = ((Const *) expr)->consttype;
|
|
|
|
break;
|
|
|
|
case T_ArrayRef:
|
|
|
|
type = ((ArrayRef *) expr)->refelemtype;
|
|
|
|
break;
|
1999-01-24 01:28:37 +01:00
|
|
|
case T_Aggref:
|
|
|
|
type = ((Aggref *) expr)->aggtype;
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
|
|
|
case T_Param:
|
|
|
|
type = ((Param *) expr)->paramtype;
|
|
|
|
break;
|
2000-02-20 22:32:16 +01:00
|
|
|
case T_RelabelType:
|
|
|
|
type = ((RelabelType *) expr)->resulttype;
|
|
|
|
break;
|
1998-01-19 06:48:55 +01:00
|
|
|
case T_SubLink:
|
1999-04-19 06:17:11 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
SubLink *sublink = (SubLink *) expr;
|
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
if (sublink->subLinkType == EXPR_SUBLINK)
|
|
|
|
{
|
1999-11-15 03:00:15 +01:00
|
|
|
/* get the type of the subselect's first target column */
|
|
|
|
Query *qtree = (Query *) sublink->subselect;
|
|
|
|
TargetEntry *tent;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!qtree || !IsA(qtree, Query))
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Cannot get type for untransformed sublink");
|
1999-11-15 03:00:15 +01:00
|
|
|
tent = (TargetEntry *) lfirst(qtree->targetList);
|
|
|
|
type = tent->resdom->restype;
|
1999-04-19 06:17:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* for all other sublink types, result is boolean */
|
|
|
|
type = BOOLOID;
|
|
|
|
}
|
|
|
|
}
|
1998-01-19 06:48:55 +01:00
|
|
|
break;
|
1998-12-04 16:34:49 +01:00
|
|
|
case T_CaseExpr:
|
|
|
|
type = ((CaseExpr *) expr)->casetype;
|
|
|
|
break;
|
|
|
|
case T_CaseWhen:
|
|
|
|
type = exprType(((CaseWhen *) expr)->result);
|
|
|
|
break;
|
1997-11-25 23:07:18 +01:00
|
|
|
case T_Ident:
|
|
|
|
/* is this right? */
|
|
|
|
type = UNKNOWNOID;
|
|
|
|
break;
|
|
|
|
default:
|
2000-02-15 04:38:29 +01:00
|
|
|
elog(ERROR, "Do not know how to get type for %d node",
|
1997-11-25 23:07:18 +01:00
|
|
|
nodeTag(expr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
1999-07-19 02:26:20 +02:00
|
|
|
/*
|
|
|
|
* exprTypmod -
|
|
|
|
* returns the type-specific attrmod of the expression, if it can be
|
|
|
|
* determined. In most cases, it can't and we return -1.
|
|
|
|
*/
|
|
|
|
int32
|
|
|
|
exprTypmod(Node *expr)
|
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (nodeTag(expr))
|
|
|
|
{
|
|
|
|
case T_Var:
|
|
|
|
return ((Var *) expr)->vartypmod;
|
|
|
|
case T_Const:
|
|
|
|
{
|
|
|
|
/* Be smart about string constants... */
|
2000-04-12 19:17:23 +02:00
|
|
|
Const *con = (Const *) expr;
|
|
|
|
|
1999-07-19 02:26:20 +02:00
|
|
|
switch (con->consttype)
|
|
|
|
{
|
|
|
|
case BPCHAROID:
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!con->constisnull)
|
1999-07-19 02:26:20 +02:00
|
|
|
return VARSIZE(DatumGetPointer(con->constvalue));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-02-26 22:11:10 +01:00
|
|
|
case T_Expr:
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
int32 coercedTypmod;
|
2000-02-26 22:11:10 +01:00
|
|
|
|
|
|
|
/* Be smart about length-coercion functions... */
|
|
|
|
if (exprIsLengthCoercion(expr, &coercedTypmod))
|
|
|
|
return coercedTypmod;
|
|
|
|
}
|
|
|
|
break;
|
2000-02-20 22:32:16 +01:00
|
|
|
case T_RelabelType:
|
|
|
|
return ((RelabelType *) expr)->resulttypmod;
|
|
|
|
break;
|
1999-07-19 02:26:20 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-02-26 22:11:10 +01:00
|
|
|
/*
|
|
|
|
* exprIsLengthCoercion
|
|
|
|
* Detect whether an expression tree is an application of a datatype's
|
|
|
|
* typmod-coercion function. Optionally extract the result's typmod.
|
|
|
|
*
|
|
|
|
* If coercedTypmod is not NULL, the typmod is stored there if the expression
|
|
|
|
* is a length-coercion function, else -1 is stored there.
|
|
|
|
*
|
|
|
|
* We assume that a two-argument function named for a datatype, whose
|
|
|
|
* output and first argument types are that datatype, and whose second
|
|
|
|
* input is an int32 constant, represents a forced length coercion.
|
2000-03-08 00:30:53 +01:00
|
|
|
*
|
2000-02-26 22:11:10 +01:00
|
|
|
* XXX It'd be better if the parsetree retained some explicit indication
|
|
|
|
* of the coercion, so we didn't need these heuristics.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
|
|
|
|
{
|
|
|
|
Func *func;
|
|
|
|
Const *second_arg;
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_proc procStruct;
|
|
|
|
Form_pg_type typeStruct;
|
|
|
|
|
|
|
|
if (coercedTypmod != NULL)
|
|
|
|
*coercedTypmod = -1; /* default result on failure */
|
|
|
|
|
|
|
|
/* Is it a function-call at all? */
|
|
|
|
if (expr == NULL ||
|
2000-04-12 19:17:23 +02:00
|
|
|
!IsA(expr, Expr) ||
|
2000-02-26 22:11:10 +01:00
|
|
|
((Expr *) expr)->opType != FUNC_EXPR)
|
|
|
|
return false;
|
|
|
|
func = (Func *) (((Expr *) expr)->oper);
|
|
|
|
Assert(IsA(func, Func));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's not a two-argument function with the second argument being
|
2000-04-12 19:17:23 +02:00
|
|
|
* an int4 constant, it can't have been created from a length
|
|
|
|
* coercion.
|
2000-02-26 22:11:10 +01:00
|
|
|
*/
|
|
|
|
if (length(((Expr *) expr)->args) != 2)
|
|
|
|
return false;
|
|
|
|
second_arg = (Const *) lsecond(((Expr *) expr)->args);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!IsA(second_arg, Const) ||
|
2000-02-26 22:11:10 +01:00
|
|
|
second_arg->consttype != INT4OID ||
|
|
|
|
second_arg->constisnull)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup the function in pg_proc
|
|
|
|
*/
|
|
|
|
tup = SearchSysCacheTuple(PROCOID,
|
|
|
|
ObjectIdGetDatum(func->funcid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup for proc %u failed", func->funcid);
|
|
|
|
procStruct = (Form_pg_proc) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* It must be a function with two arguments where the first is of the
|
|
|
|
* same type as the return value and the second is an int4. Also, just
|
|
|
|
* to be sure, check return type agrees with expr node.
|
2000-02-26 22:11:10 +01:00
|
|
|
*/
|
|
|
|
if (procStruct->pronargs != 2 ||
|
|
|
|
procStruct->prorettype != procStruct->proargtypes[0] ||
|
|
|
|
procStruct->proargtypes[1] != INT4OID ||
|
|
|
|
procStruct->prorettype != ((Expr *) expr)->typeOid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Furthermore, the name of the function must be the same as the
|
|
|
|
* argument/result type's name.
|
2000-02-26 22:11:10 +01:00
|
|
|
*/
|
|
|
|
tup = SearchSysCacheTuple(TYPEOID,
|
|
|
|
ObjectIdGetDatum(procStruct->prorettype),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "cache lookup for type %u failed",
|
|
|
|
procStruct->prorettype);
|
|
|
|
typeStruct = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
if (strncmp(NameStr(procStruct->proname),
|
|
|
|
NameStr(typeStruct->typname),
|
|
|
|
NAMEDATALEN) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, it is indeed a length-coercion function.
|
|
|
|
*/
|
|
|
|
if (coercedTypmod != NULL)
|
|
|
|
*coercedTypmod = DatumGetInt32(second_arg->constvalue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
1999-08-05 04:33:54 +02:00
|
|
|
/*
|
|
|
|
* Produce an appropriate Const node from a constant value produced
|
|
|
|
* by the parser and an explicit type name to cast to.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
static Node *
|
2000-01-17 01:14:49 +01:00
|
|
|
parser_typecast_constant(Value *expr, TypeName *typename)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-12-24 07:43:34 +01:00
|
|
|
Const *con;
|
1997-11-25 23:07:18 +01:00
|
|
|
Type tp;
|
1999-12-24 07:43:34 +01:00
|
|
|
Datum datum;
|
1997-11-25 23:07:18 +01:00
|
|
|
char *const_string = NULL;
|
|
|
|
bool string_palloced = false;
|
1999-12-24 07:43:34 +01:00
|
|
|
bool isNull = false;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
switch (nodeTag(expr))
|
|
|
|
{
|
|
|
|
case T_Integer:
|
|
|
|
string_palloced = true;
|
1999-05-12 09:14:24 +02:00
|
|
|
const_string = int4out(expr->val.ival);
|
|
|
|
break;
|
|
|
|
case T_Float:
|
2000-02-21 19:47:12 +01:00
|
|
|
case T_String:
|
|
|
|
const_string = expr->val.str;
|
1997-11-25 23:07:18 +01:00
|
|
|
break;
|
1999-12-24 07:43:34 +01:00
|
|
|
case T_Null:
|
|
|
|
isNull = true;
|
|
|
|
break;
|
1997-11-25 23:07:18 +01:00
|
|
|
default:
|
2000-02-21 19:47:12 +01:00
|
|
|
elog(ERROR, "Cannot cast this expression to type '%s'",
|
1997-11-25 23:07:18 +01:00
|
|
|
typename->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typename->arrayBounds != NIL)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char type_string[NAMEDATALEN + 2];
|
1999-08-05 04:33:54 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
sprintf(type_string, "_%s", typename->name);
|
|
|
|
tp = (Type) typenameType(type_string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tp = (Type) typenameType(typename->name);
|
|
|
|
|
1999-12-24 07:43:34 +01:00
|
|
|
if (isNull)
|
|
|
|
datum = (Datum) NULL;
|
|
|
|
else
|
2000-01-17 01:14:49 +01:00
|
|
|
datum = stringTypeDatum(tp, const_string, typename->typmod);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-12-24 07:43:34 +01:00
|
|
|
con = makeConst(typeTypeId(tp),
|
1999-08-05 04:33:54 +02:00
|
|
|
typeLen(tp),
|
1999-12-24 07:43:34 +01:00
|
|
|
datum,
|
|
|
|
isNull,
|
1998-10-02 00:51:22 +02:00
|
|
|
typeByVal(tp),
|
1997-11-25 23:07:18 +01:00
|
|
|
false, /* not a set */
|
|
|
|
true /* is cast */ );
|
|
|
|
|
|
|
|
if (string_palloced)
|
|
|
|
pfree(const_string);
|
|
|
|
|
1999-12-24 07:43:34 +01:00
|
|
|
return (Node *) con;
|
1998-02-26 05:46:47 +01:00
|
|
|
}
|
2000-01-17 01:14:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle an explicit CAST applied to a non-constant expression.
|
|
|
|
* (Actually, this works for constants too, but gram.y won't generate
|
|
|
|
* a TypeCast node if the argument is just a constant.)
|
|
|
|
*
|
|
|
|
* The given expr has already been transformed, but we need to lookup
|
|
|
|
* the type name and then apply any necessary coercion function(s).
|
|
|
|
*/
|
|
|
|
static Node *
|
|
|
|
parser_typecast_expression(ParseState *pstate,
|
|
|
|
Node *expr, TypeName *typename)
|
|
|
|
{
|
|
|
|
Oid inputType = exprType(expr);
|
|
|
|
Type tp;
|
|
|
|
Oid targetType;
|
|
|
|
|
|
|
|
if (typename->arrayBounds != NIL)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char type_string[NAMEDATALEN + 2];
|
2000-01-17 01:14:49 +01:00
|
|
|
|
|
|
|
sprintf(type_string, "_%s", typename->name);
|
|
|
|
tp = (Type) typenameType(type_string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tp = (Type) typenameType(typename->name);
|
|
|
|
targetType = typeTypeId(tp);
|
|
|
|
|
|
|
|
if (inputType == InvalidOid)
|
|
|
|
return expr; /* do nothing if NULL input */
|
|
|
|
|
|
|
|
if (inputType != targetType)
|
|
|
|
{
|
2000-01-17 03:04:16 +01:00
|
|
|
expr = CoerceTargetExpr(pstate, expr, inputType,
|
|
|
|
targetType, typename->typmod);
|
2000-01-17 01:14:49 +01:00
|
|
|
if (expr == NULL)
|
|
|
|
elog(ERROR, "Cannot cast type '%s' to '%s'",
|
|
|
|
typeidTypeName(inputType),
|
|
|
|
typeidTypeName(targetType));
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-01-17 01:14:49 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* If the target is a fixed-length type, it may need a length coercion
|
|
|
|
* as well as a type coercion.
|
2000-01-17 01:14:49 +01:00
|
|
|
*/
|
|
|
|
expr = coerce_type_typmod(pstate, expr,
|
|
|
|
targetType, typename->typmod);
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|