Skip to content

Commit b40ac42

Browse files
committed
Teach parse_coerce about non-cachable functions (actually,
make it call eval_const_expressions() so that it doesn't have to know).
1 parent 6eb8d25 commit b40ac42

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

src/backend/parser/parse_coerce.c

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.23 1999/08/24 00:09:56 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.24 1999/10/02 23:29:12 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616

17+
#include "optimizer/clauses.h"
1718
#include "parser/parse_coerce.h"
1819
#include "parser/parse_expr.h"
1920
#include "parser/parse_func.h"
@@ -49,7 +50,8 @@ coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId,
4950
}
5051
else if (inputTypeId == UNKNOWNOID && IsA(node, Const))
5152
{
52-
/* Input is a string constant with previously undetermined type.
53+
/*
54+
* Input is a string constant with previously undetermined type.
5355
* Apply the target type's typinput function to it to produce
5456
* a constant of the target type.
5557
*
@@ -58,6 +60,11 @@ coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId,
5860
* necessarily behave the same as a type conversion function.
5961
* For example, int4's typinput function will reject "1.2",
6062
* whereas float-to-int type conversion will round to integer.
63+
*
64+
* XXX if the typinput function is not cachable, we really ought
65+
* to postpone evaluation of the function call until runtime.
66+
* But there is no way to represent a typinput function call as
67+
* an expression tree, because C-string values are not Datums.
6168
*/
6269
Const *con = (Const *) node;
6370
Const *newcon = makeNode(Const);
@@ -97,38 +104,16 @@ coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId,
97104

98105
/*
99106
* If the input is a constant, apply the type conversion function
100-
* now instead of delaying to runtime. (This could someday be
101-
* done in a downstream constant-expression-simplifier, but we
102-
* can save cycles in the rewriter if we do it here.)
107+
* now instead of delaying to runtime. (We could, of course,
108+
* just leave this to be done during planning/optimization;
109+
* but it's a very frequent special case, and we save cycles
110+
* in the rewriter if we fold the expression now.)
103111
*
104-
* XXX there are cases where we probably shouldn't do this,
105-
* such as coercing text 'now' to datetime? Need a way to
106-
* know whether type conversion function is cacheable...
112+
* Note that no folding will occur if the conversion function is
113+
* not marked 'iscachable'.
107114
*/
108-
if (IsA(node, Const) && ! ((Const *) node)->constisnull)
109-
{
110-
Const *con = (Const *) node;
111-
Oid convertFuncid;
112-
Datum val;
113-
114-
Assert(IsA(result, Expr) &&
115-
((Expr *) result)->opType == FUNC_EXPR);
116-
117-
/* Convert the given constant */
118-
convertFuncid = ((Func *) (((Expr *) result)->oper))->funcid;
119-
val = (Datum) fmgr(convertFuncid, con->constvalue);
120-
121-
/* now make a new const node */
122-
con = makeNode(Const);
123-
con->consttype = targetTypeId;
124-
con->constlen = typeLen(targetType);
125-
con->constbyval = typeByVal(targetType);
126-
con->constvalue = val;
127-
con->constisnull = false;
128-
con->constisset = false;
129-
130-
result = (Node *) con;
131-
}
115+
if (IsA(node, Const))
116+
result = eval_const_expressions(result);
132117
}
133118

134119
return result;

src/test/regress/expected/rules.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ rtest_v1 |rtest_v1_upd |CREATE RULE "rtest_v1_upd" AS ON UPDATE TO "rtest_
11181118
shoelace |shoelace_del |CREATE RULE "shoelace_del" AS ON DELETE TO "shoelace" DO INSTEAD DELETE FROM "shoelace_data" WHERE ("shoelace_data"."sl_name" = old."sl_name");
11191119
shoelace |shoelace_ins |CREATE RULE "shoelace_ins" AS ON INSERT TO "shoelace" DO INSTEAD INSERT INTO "shoelace_data" ("sl_name", "sl_avail", "sl_color", "sl_len", "sl_unit") VALUES (new."sl_name", new."sl_avail", new."sl_color", new."sl_len", new."sl_unit");
11201120
shoelace |shoelace_upd |CREATE RULE "shoelace_upd" AS ON UPDATE TO "shoelace" DO INSTEAD UPDATE "shoelace_data" SET "sl_name" = new."sl_name", "sl_avail" = new."sl_avail", "sl_color" = new."sl_color", "sl_len" = new."sl_len", "sl_unit" = new."sl_unit" WHERE ("shoelace_data"."sl_name" = old."sl_name");
1121-
shoelace_data|log_shoelace |CREATE RULE "log_shoelace" AS ON UPDATE TO "shoelace_data" WHERE (new."sl_avail" <> old."sl_avail") DO INSERT INTO "shoelace_log" ("sl_name", "sl_avail", "log_who", "log_when") VALUES (new."sl_name", new."sl_avail", 'Al Bundy'::"name", 'epoch'::"datetime");
1121+
shoelace_data|log_shoelace |CREATE RULE "log_shoelace" AS ON UPDATE TO "shoelace_data" WHERE (new."sl_avail" <> old."sl_avail") DO INSERT INTO "shoelace_log" ("sl_name", "sl_avail", "log_who", "log_when") VALUES (new."sl_name", new."sl_avail", 'Al Bundy'::"name", "datetime"('epoch'::"text"));
11221122
shoelace_ok |shoelace_ok_ins|CREATE RULE "shoelace_ok_ins" AS ON INSERT TO "shoelace_ok" DO INSTEAD UPDATE "shoelace" SET "sl_avail" = ("shoelace"."sl_avail" + new."ok_quant") WHERE ("shoelace"."sl_name" = new."ok_name");
11231123
(27 rows)
11241124

0 commit comments

Comments
 (0)