* ExecEvalFunctionArgToConst * * Evaluates an argument of function expression and returns the result. * This is assumed to be used in the parser stage, where * dynamic evaluation such like Var is not available, though we put it * here so that we can extend it to be useful in other places later. */
| 4408 | * here so that we can extend it to be useful in other places later. |
| 4409 | */ |
| 4410 | Datum |
| 4411 | ExecEvalFunctionArgToConst(FuncExpr *fexpr, int argno, bool *isnull) |
| 4412 | { |
| 4413 | Expr *aexpr; |
| 4414 | Oid argtype; |
| 4415 | int32 argtypmod; |
| 4416 | Oid argcollation; |
| 4417 | Const *result; |
| 4418 | |
| 4419 | /* argument number sanity check */ |
| 4420 | if (argno < 0 || list_length(fexpr->args) <= argno) |
| 4421 | elog(ERROR, "invalid argument number found during evaluating function argument"); |
| 4422 | |
| 4423 | aexpr = (Expr *) list_nth(fexpr->args, argno); |
| 4424 | /* |
| 4425 | * Check if the expression can be evaluated in the Const fasion. |
| 4426 | */ |
| 4427 | if (ExecIsExprUnsafeToConst((Node *) aexpr)) |
| 4428 | ereport(ERROR, |
| 4429 | (errcode(ERRCODE_DATA_EXCEPTION), |
| 4430 | errmsg("unable to resolve function argument"), |
| 4431 | errposition(exprLocation((Node *) aexpr)))); |
| 4432 | |
| 4433 | argtype = exprType((Node *) aexpr); |
| 4434 | if (!OidIsValid(argtype)) |
| 4435 | ereport(ERROR, |
| 4436 | (errcode(ERRCODE_INDETERMINATE_DATATYPE), |
| 4437 | errmsg("unable to resolve function argument type"), |
| 4438 | errposition(exprLocation((Node *) aexpr)))); |
| 4439 | argtypmod = exprTypmod((Node *) aexpr); |
| 4440 | argcollation = exprCollation((Node *) aexpr); |
| 4441 | |
| 4442 | result = (Const *) evaluate_expr(aexpr, argtype, argtypmod, argcollation); |
| 4443 | /* evaluate_expr always returns Const */ |
| 4444 | Assert(IsA(result, Const)); |
| 4445 | |
| 4446 | if (isnull) |
| 4447 | *isnull = result->constisnull; |
| 4448 | |
| 4449 | return result->constvalue; |
| 4450 | } |