PostgreSQL Source Code  git master
clauses.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_class.h"
#include "catalog/pg_language.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "executor/functions.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_coerce.h"
#include "parser/parse_func.h"
#include "rewrite/rewriteManip.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for clauses.c:

Go to the source code of this file.

Data Structures

struct  get_agg_clause_costs_context
 
struct  eval_const_expressions_context
 
struct  substitute_actual_parameters_context
 
struct  substitute_actual_srf_parameters_context
 
struct  inline_error_callback_arg
 
struct  max_parallel_hazard_context
 

Macros

#define CCDN_CASETESTEXPR_OK   0x0001 /* CaseTestExpr okay here? */
 
#define ece_generic_processing(node)
 
#define ece_all_arguments_const(node)   (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))
 
#define ece_evaluate_expr(node)
 

Functions

static bool contain_agg_clause_walker (Node *node, void *context)
 
static bool get_agg_clause_costs_walker (Node *node, get_agg_clause_costs_context *context)
 
static bool find_window_functions_walker (Node *node, WindowFuncLists *lists)
 
static bool contain_subplans_walker (Node *node, void *context)
 
static bool contain_mutable_functions_walker (Node *node, void *context)
 
static bool contain_volatile_functions_walker (Node *node, void *context)
 
static bool contain_volatile_functions_not_nextval_walker (Node *node, void *context)
 
static bool max_parallel_hazard_walker (Node *node, max_parallel_hazard_context *context)
 
static bool contain_nonstrict_functions_walker (Node *node, void *context)
 
static bool contain_context_dependent_node (Node *clause)
 
static bool contain_context_dependent_node_walker (Node *node, int *flags)
 
static bool contain_leaked_vars_walker (Node *node, void *context)
 
static Relids find_nonnullable_rels_walker (Node *node, bool top_level)
 
static Listfind_nonnullable_vars_walker (Node *node, bool top_level)
 
static bool is_strict_saop (ScalarArrayOpExpr *expr, bool falseOK)
 
static Nodeeval_const_expressions_mutator (Node *node, eval_const_expressions_context *context)
 
static bool contain_non_const_walker (Node *node, void *context)
 
static bool ece_function_is_safe (Oid funcid, eval_const_expressions_context *context)
 
static Listsimplify_or_arguments (List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceTrue)
 
static Listsimplify_and_arguments (List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceFalse)
 
static Nodesimplify_boolean_equality (Oid opno, List *args)
 
static Exprsimplify_function (Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List **args_p, bool funcvariadic, bool process_args, bool allow_non_const, eval_const_expressions_context *context)
 
static Listreorder_function_arguments (List *args, HeapTuple func_tuple)
 
static Listadd_function_defaults (List *args, HeapTuple func_tuple)
 
static Listfetch_function_defaults (HeapTuple func_tuple)
 
static void recheck_cast_function_args (List *args, Oid result_type, HeapTuple func_tuple)
 
static Exprevaluate_function (Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List *args, bool funcvariadic, HeapTuple func_tuple, eval_const_expressions_context *context)
 
static Exprinline_function (Oid funcid, Oid result_type, Oid result_collid, Oid input_collid, List *args, bool funcvariadic, HeapTuple func_tuple, eval_const_expressions_context *context)
 
static Nodesubstitute_actual_parameters (Node *expr, int nargs, List *args, int *usecounts)
 
static Nodesubstitute_actual_parameters_mutator (Node *node, substitute_actual_parameters_context *context)
 
static void sql_inline_error_callback (void *arg)
 
static Querysubstitute_actual_srf_parameters (Query *expr, int nargs, List *args)
 
static Nodesubstitute_actual_srf_parameters_mutator (Node *node, substitute_actual_srf_parameters_context *context)
 
static bool tlist_matches_coltypelist (List *tlist, List *coltypelist)
 
bool contain_agg_clause (Node *clause)
 
void get_agg_clause_costs (PlannerInfo *root, Node *clause, AggSplit aggsplit, AggClauseCosts *costs)
 
bool contain_window_function (Node *clause)
 
WindowFuncListsfind_window_functions (Node *clause, Index maxWinRef)
 
double expression_returns_set_rows (PlannerInfo *root, Node *clause)
 
bool contain_subplans (Node *clause)
 
bool contain_mutable_functions (Node *clause)
 
static bool contain_mutable_functions_checker (Oid func_id, void *context)
 
bool contain_volatile_functions (Node *clause)
 
static bool contain_volatile_functions_checker (Oid func_id, void *context)
 
bool contain_volatile_functions_not_nextval (Node *clause)
 
static bool contain_volatile_functions_not_nextval_checker (Oid func_id, void *context)
 
char max_parallel_hazard (Query *parse)
 
bool is_parallel_safe (PlannerInfo *root, Node *node)
 
static bool max_parallel_hazard_test (char proparallel, max_parallel_hazard_context *context)
 
static bool max_parallel_hazard_checker (Oid func_id, void *context)
 
bool contain_nonstrict_functions (Node *clause)
 
static bool contain_nonstrict_functions_checker (Oid func_id, void *context)
 
bool contain_leaked_vars (Node *clause)
 
static bool contain_leaked_vars_checker (Oid func_id, void *context)
 
Relids find_nonnullable_rels (Node *clause)
 
Listfind_nonnullable_vars (Node *clause)
 
Listfind_forced_null_vars (Node *node)
 
Varfind_forced_null_var (Node *node)
 
bool is_pseudo_constant_clause (Node *clause)
 
bool is_pseudo_constant_clause_relids (Node *clause, Relids relids)
 
int NumRelids (Node *clause)
 
void CommuteOpExpr (OpExpr *clause)
 
static bool rowtype_field_matches (Oid rowtypeid, int fieldnum, Oid expectedtype, int32 expectedtypmod, Oid expectedcollation)
 
Nodeeval_const_expressions (PlannerInfo *root, Node *node)
 
Nodeestimate_expression_value (PlannerInfo *root, Node *node)
 
Listexpand_function_arguments (List *args, Oid result_type, HeapTuple func_tuple)
 
Exprevaluate_expr (Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
 
Queryinline_set_returning_function (PlannerInfo *root, RangeTblEntry *rte)
 

Macro Definition Documentation

◆ CCDN_CASETESTEXPR_OK

#define CCDN_CASETESTEXPR_OK   0x0001 /* CaseTestExpr okay here? */

Definition at line 1250 of file clauses.c.

Referenced by contain_context_dependent_node_walker().

◆ ece_all_arguments_const

#define ece_all_arguments_const (   node)    (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))

Definition at line 2317 of file clauses.c.

Referenced by eval_const_expressions_mutator().

◆ ece_evaluate_expr

#define ece_evaluate_expr (   node)
Value:
((Node *) evaluate_expr((Expr *) (node), \
exprType((Node *) (node)), \
exprTypmod((Node *) (node)), \
exprCollation((Node *) (node))))
Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
Definition: clauses.c:4778
Definition: nodes.h:525
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42

Definition at line 2321 of file clauses.c.

Referenced by eval_const_expressions_mutator().

◆ ece_generic_processing

#define ece_generic_processing (   node)
Value:
(void *) context)
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2502
Definition: nodes.h:525
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2331

Definition at line 2308 of file clauses.c.

Referenced by eval_const_expressions_mutator().

Function Documentation

◆ add_function_defaults()

static List * add_function_defaults ( List args,
HeapTuple  func_tuple 
)
static

Definition at line 4161 of file clauses.c.

References elog, ERROR, fetch_function_defaults(), GETSTRUCT, list_concat_copy(), list_copy_tail(), and list_length().

Referenced by expand_function_arguments().

4162 {
4163  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4164  int nargsprovided = list_length(args);
4165  List *defaults;
4166  int ndelete;
4167 
4168  /* Get all the default expressions from the pg_proc tuple */
4169  defaults = fetch_function_defaults(func_tuple);
4170 
4171  /* Delete any unused defaults from the list */
4172  ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
4173  if (ndelete < 0)
4174  elog(ERROR, "not enough default arguments");
4175  if (ndelete > 0)
4176  defaults = list_copy_tail(defaults, ndelete);
4177 
4178  /* And form the combined argument list, not modifying the input list */
4179  return list_concat_copy(args, defaults);
4180 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1423
#define ERROR
Definition: elog.h:43
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:553
static List * fetch_function_defaults(HeapTuple func_tuple)
Definition: clauses.c:4186
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:226
Definition: pg_list.h:50

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 2146 of file clauses.c.

References OpExpr::args, elog, ERROR, get_commutator(), InvalidOid, is_opclause(), linitial, list_length(), lsecond, OidIsValid, OpExpr::opfuncid, and OpExpr::opno.

Referenced by get_switched_clauses().

2147 {
2148  Oid opoid;
2149  Node *temp;
2150 
2151  /* Sanity checks: caller is at fault if these fail */
2152  if (!is_opclause(clause) ||
2153  list_length(clause->args) != 2)
2154  elog(ERROR, "cannot commute non-binary-operator clause");
2155 
2156  opoid = get_commutator(clause->opno);
2157 
2158  if (!OidIsValid(opoid))
2159  elog(ERROR, "could not find commutator for operator %u",
2160  clause->opno);
2161 
2162  /*
2163  * modify the clause in-place!
2164  */
2165  clause->opno = opoid;
2166  clause->opfuncid = InvalidOid;
2167  /* opresulttype, opretset, opcollid, inputcollid need not change */
2168 
2169  temp = linitial(clause->args);
2170  linitial(clause->args) = lsecond(clause->args);
2171  lsecond(clause->args) = temp;
2172 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Oid opfuncid
Definition: primnodes.h:503
#define InvalidOid
Definition: postgres_ext.h:36
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:226
Oid opno
Definition: primnodes.h:502
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * args
Definition: primnodes.h:508

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 179 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

180 {
181  return contain_agg_clause_walker(clause, NULL);
182 }
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:185

◆ contain_agg_clause_walker()

static bool contain_agg_clause_walker ( Node node,
void *  context 
)
static

Definition at line 185 of file clauses.c.

References Assert, expression_tree_walker(), and IsA.

Referenced by contain_agg_clause().

186 {
187  if (node == NULL)
188  return false;
189  if (IsA(node, Aggref))
190  {
191  Assert(((Aggref *) node)->agglevelsup == 0);
192  return true; /* abort the tree traversal and return true */
193  }
194  if (IsA(node, GroupingFunc))
195  {
196  Assert(((GroupingFunc *) node)->agglevelsup == 0);
197  return true; /* abort the tree traversal and return true */
198  }
199  Assert(!IsA(node, SubLink));
200  return expression_tree_walker(node, contain_agg_clause_walker, context);
201 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:185
#define Assert(condition)
Definition: c.h:732
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840

◆ contain_context_dependent_node()

static bool contain_context_dependent_node ( Node clause)
static

Definition at line 1243 of file clauses.c.

References contain_context_dependent_node_walker().

Referenced by inline_function().

1244 {
1245  int flags = 0;
1246 
1247  return contain_context_dependent_node_walker(clause, &flags);
1248 }
static bool contain_context_dependent_node_walker(Node *node, int *flags)
Definition: clauses.c:1253

◆ contain_context_dependent_node_walker()

static bool contain_context_dependent_node_walker ( Node node,
int *  flags 
)
static

Definition at line 1253 of file clauses.c.

References ArrayCoerceExpr::arg, CaseExpr::arg, CCDN_CASETESTEXPR_OK, ArrayCoerceExpr::elemexpr, expression_tree_walker(), and IsA.

Referenced by contain_context_dependent_node().

1254 {
1255  if (node == NULL)
1256  return false;
1257  if (IsA(node, CaseTestExpr))
1258  return !(*flags & CCDN_CASETESTEXPR_OK);
1259  else if (IsA(node, CaseExpr))
1260  {
1261  CaseExpr *caseexpr = (CaseExpr *) node;
1262 
1263  /*
1264  * If this CASE doesn't have a test expression, then it doesn't create
1265  * a context in which CaseTestExprs should appear, so just fall
1266  * through and treat it as a generic expression node.
1267  */
1268  if (caseexpr->arg)
1269  {
1270  int save_flags = *flags;
1271  bool res;
1272 
1273  /*
1274  * Note: in principle, we could distinguish the various sub-parts
1275  * of a CASE construct and set the flag bit only for some of them,
1276  * since we are only expecting CaseTestExprs to appear in the
1277  * "expr" subtree of the CaseWhen nodes. But it doesn't really
1278  * seem worth any extra code. If there are any bare CaseTestExprs
1279  * elsewhere in the CASE, something's wrong already.
1280  */
1281  *flags |= CCDN_CASETESTEXPR_OK;
1282  res = expression_tree_walker(node,
1284  (void *) flags);
1285  *flags = save_flags;
1286  return res;
1287  }
1288  }
1289  else if (IsA(node, ArrayCoerceExpr))
1290  {
1291  ArrayCoerceExpr *ac = (ArrayCoerceExpr *) node;
1292  int save_flags;
1293  bool res;
1294 
1295  /* Check the array expression */
1296  if (contain_context_dependent_node_walker((Node *) ac->arg, flags))
1297  return true;
1298 
1299  /* Check the elemexpr, which is allowed to contain CaseTestExpr */
1300  save_flags = *flags;
1301  *flags |= CCDN_CASETESTEXPR_OK;
1303  flags);
1304  *flags = save_flags;
1305  return res;
1306  }
1308  (void *) flags);
1309 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
#define CCDN_CASETESTEXPR_OK
Definition: clauses.c:1250
Expr * elemexpr
Definition: primnodes.h:845
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
Expr * arg
Definition: primnodes.h:918
static bool contain_context_dependent_node_walker(Node *node, int *flags)
Definition: clauses.c:1253

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1327 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1328 {
1329  return contain_leaked_vars_walker(clause, NULL);
1330 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1339

◆ contain_leaked_vars_checker()

static bool contain_leaked_vars_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 1333 of file clauses.c.

References get_func_leakproof().

Referenced by contain_leaked_vars_walker().

1334 {
1335  return !get_func_leakproof(func_id);
1336 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1639

◆ contain_leaked_vars_walker()

static bool contain_leaked_vars_walker ( Node node,
void *  context 
)
static

Definition at line 1339 of file clauses.c.

References MinMaxExpr::args, check_functions_in_node(), TypeCacheEntry::cmp_proc, contain_leaked_vars_checker(), contain_var_clause(), expression_tree_walker(), forthree, get_func_leakproof(), get_opcode(), RowCompareExpr::largs, lfirst, lfirst_oid, lookup_type_cache(), MinMaxExpr::minmaxtype, nodeTag, OidIsValid, RowCompareExpr::opnos, RowCompareExpr::rargs, T_ArrayCoerceExpr, T_ArrayExpr, T_BooleanTest, T_BoolExpr, T_CaseExpr, T_CaseTestExpr, T_CoerceViaIO, T_CollateExpr, T_Const, T_CurrentOfExpr, T_DistinctExpr, T_FieldSelect, T_FieldStore, T_FuncExpr, T_List, T_MinMaxExpr, T_NamedArgExpr, T_NextValueExpr, T_NullIfExpr, T_NullTest, T_OpExpr, T_Param, T_RelabelType, T_RowCompareExpr, T_RowExpr, T_ScalarArrayOpExpr, T_SQLValueFunction, T_SubscriptingRef, T_Var, and TYPECACHE_CMP_PROC.

Referenced by contain_leaked_vars().

1340 {
1341  if (node == NULL)
1342  return false;
1343 
1344  switch (nodeTag(node))
1345  {
1346  case T_Var:
1347  case T_Const:
1348  case T_Param:
1349  case T_ArrayExpr:
1350  case T_FieldSelect:
1351  case T_FieldStore:
1352  case T_NamedArgExpr:
1353  case T_BoolExpr:
1354  case T_RelabelType:
1355  case T_CollateExpr:
1356  case T_CaseExpr:
1357  case T_CaseTestExpr:
1358  case T_RowExpr:
1359  case T_SQLValueFunction:
1360  case T_NullTest:
1361  case T_BooleanTest:
1362  case T_NextValueExpr:
1363  case T_List:
1364 
1365  /*
1366  * We know these node types don't contain function calls; but
1367  * something further down in the node tree might.
1368  */
1369  break;
1370 
1371  case T_FuncExpr:
1372  case T_OpExpr:
1373  case T_DistinctExpr:
1374  case T_NullIfExpr:
1375  case T_ScalarArrayOpExpr:
1376  case T_CoerceViaIO:
1377  case T_ArrayCoerceExpr:
1378  case T_SubscriptingRef:
1379 
1380  /*
1381  * If node contains a leaky function call, and there's any Var
1382  * underneath it, reject.
1383  */
1385  context) &&
1386  contain_var_clause(node))
1387  return true;
1388  break;
1389 
1390  case T_RowCompareExpr:
1391  {
1392  /*
1393  * It's worth special-casing this because a leaky comparison
1394  * function only compromises one pair of row elements, which
1395  * might not contain Vars while others do.
1396  */
1397  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1398  ListCell *opid;
1399  ListCell *larg;
1400  ListCell *rarg;
1401 
1402  forthree(opid, rcexpr->opnos,
1403  larg, rcexpr->largs,
1404  rarg, rcexpr->rargs)
1405  {
1406  Oid funcid = get_opcode(lfirst_oid(opid));
1407 
1408  if (!get_func_leakproof(funcid) &&
1409  (contain_var_clause((Node *) lfirst(larg)) ||
1410  contain_var_clause((Node *) lfirst(rarg))))
1411  return true;
1412  }
1413  }
1414  break;
1415 
1416  case T_MinMaxExpr:
1417  {
1418  /*
1419  * MinMaxExpr is leakproof if the comparison function it calls
1420  * is leakproof.
1421  */
1422  MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
1423  TypeCacheEntry *typentry;
1424  bool leakproof;
1425 
1426  /* Look up the btree comparison function for the datatype */
1427  typentry = lookup_type_cache(minmaxexpr->minmaxtype,
1429  if (OidIsValid(typentry->cmp_proc))
1430  leakproof = get_func_leakproof(typentry->cmp_proc);
1431  else
1432  {
1433  /*
1434  * The executor will throw an error, but here we just
1435  * treat the missing function as leaky.
1436  */
1437  leakproof = false;
1438  }
1439 
1440  if (!leakproof &&
1441  contain_var_clause((Node *) minmaxexpr->args))
1442  return true;
1443  }
1444  break;
1445 
1446  case T_CurrentOfExpr:
1447 
1448  /*
1449  * WHERE CURRENT OF doesn't contain leaky function calls.
1450  * Moreover, it is essential that this is considered non-leaky,
1451  * since the planner must always generate a TID scan when CURRENT
1452  * OF is present -- cf. cost_tidscan.
1453  */
1454  return false;
1455 
1456  default:
1457 
1458  /*
1459  * If we don't recognize the node tag, assume it might be leaky.
1460  * This prevents an unexpected security hole if someone adds a new
1461  * node type that can call a function.
1462  */
1463  return true;
1464  }
1466  context);
1467 }
Oid minmaxtype
Definition: primnodes.h:1088
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1639
List * args
Definition: primnodes.h:1092
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:464
Definition: nodes.h:525
bool contain_var_clause(Node *node)
Definition: var.c:331
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1657
Definition: nodes.h:297
Definition: nodes.h:152
Definition: nodes.h:151
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:322
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
#define TYPECACHE_CMP_PROC
Definition: typcache.h:131
#define lfirst(lc)
Definition: pg_list.h:190
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
#define nodeTag(nodeptr)
Definition: nodes.h:530
static bool contain_leaked_vars_checker(Oid func_id, void *context)
Definition: clauses.c:1333
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1339
#define lfirst_oid(lc)
Definition: pg_list.h:192
Definition: nodes.h:153

◆ contain_mutable_functions()

◆ contain_mutable_functions_checker()

static bool contain_mutable_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 651 of file clauses.c.

References func_volatile().

Referenced by contain_mutable_functions_walker().

652 {
653  return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
654 }
char func_volatile(Oid funcid)
Definition: lsyscache.c:1582

◆ contain_mutable_functions_walker()

static bool contain_mutable_functions_walker ( Node node,
void *  context 
)
static

Definition at line 657 of file clauses.c.

References check_functions_in_node(), contain_mutable_functions_checker(), expression_tree_walker(), IsA, and query_tree_walker().

Referenced by contain_mutable_functions().

658 {
659  if (node == NULL)
660  return false;
661  /* Check for mutable functions in node itself */
663  context))
664  return true;
665 
666  if (IsA(node, SQLValueFunction))
667  {
668  /* all variants of SQLValueFunction are stable */
669  return true;
670  }
671 
672  if (IsA(node, NextValueExpr))
673  {
674  /* NextValueExpr is volatile */
675  return true;
676  }
677 
678  /*
679  * It should be safe to treat MinMaxExpr as immutable, because it will
680  * depend on a non-cross-type btree comparison function, and those should
681  * always be immutable. Treating XmlExpr as immutable is more dubious,
682  * and treating CoerceToDomain as immutable is outright dangerous. But we
683  * have done so historically, and changing this would probably cause more
684  * problems than it would fix. In practice, if you have a non-immutable
685  * domain constraint you are in for pain anyhow.
686  */
687 
688  /* Recurse to check arguments */
689  if (IsA(node, Query))
690  {
691  /* Recurse into subselects */
692  return query_tree_walker((Query *) node,
694  context, 0);
695  }
697  context);
698 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2274
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool contain_mutable_functions_walker(Node *node, void *context)
Definition: clauses.c:657
static bool contain_mutable_functions_checker(Oid func_id, void *context)
Definition: clauses.c:651
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1657
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840

◆ contain_non_const_walker()

static bool contain_non_const_walker ( Node node,
void *  context 
)
static

Definition at line 3618 of file clauses.c.

References expression_tree_walker(), and IsA.

3619 {
3620  if (node == NULL)
3621  return false;
3622  if (IsA(node, Const))
3623  return false;
3624  if (IsA(node, List))
3625  return expression_tree_walker(node, contain_non_const_walker, context);
3626  /* Otherwise, abort the tree traversal and return true */
3627  return true;
3628 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
Definition: pg_list.h:50
static bool contain_non_const_walker(Node *node, void *context)
Definition: clauses.c:3618

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 1094 of file clauses.c.

References contain_nonstrict_functions_walker().

Referenced by inline_function(), process_equivalence(), and pullup_replace_vars_callback().

1095 {
1096  return contain_nonstrict_functions_walker(clause, NULL);
1097 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1106

◆ contain_nonstrict_functions_checker()

static bool contain_nonstrict_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 1100 of file clauses.c.

References func_strict().

Referenced by contain_nonstrict_functions_walker().

1101 {
1102  return !func_strict(func_id);
1103 }
bool func_strict(Oid funcid)
Definition: lsyscache.c:1563

◆ contain_nonstrict_functions_walker()

static bool contain_nonstrict_functions_walker ( Node node,
void *  context 
)
static

Definition at line 1106 of file clauses.c.

References AND_EXPR, arg, BoolExpr::boolop, check_functions_in_node(), contain_nonstrict_functions_checker(), expression_tree_walker(), IsA, and OR_EXPR.

Referenced by contain_nonstrict_functions().

1107 {
1108  if (node == NULL)
1109  return false;
1110  if (IsA(node, Aggref))
1111  {
1112  /* an aggregate could return non-null with null input */
1113  return true;
1114  }
1115  if (IsA(node, GroupingFunc))
1116  {
1117  /*
1118  * A GroupingFunc doesn't evaluate its arguments, and therefore must
1119  * be treated as nonstrict.
1120  */
1121  return true;
1122  }
1123  if (IsA(node, WindowFunc))
1124  {
1125  /* a window function could return non-null with null input */
1126  return true;
1127  }
1128  if (IsA(node, SubscriptingRef))
1129  {
1130  /*
1131  * subscripting assignment is nonstrict, but subscripting itself is
1132  * strict
1133  */
1134  if (((SubscriptingRef *) node)->refassgnexpr != NULL)
1135  return true;
1136 
1137  /* else fall through to check args */
1138  }
1139  if (IsA(node, DistinctExpr))
1140  {
1141  /* IS DISTINCT FROM is inherently non-strict */
1142  return true;
1143  }
1144  if (IsA(node, NullIfExpr))
1145  {
1146  /* NULLIF is inherently non-strict */
1147  return true;
1148  }
1149  if (IsA(node, BoolExpr))
1150  {
1151  BoolExpr *expr = (BoolExpr *) node;
1152 
1153  switch (expr->boolop)
1154  {
1155  case AND_EXPR:
1156  case OR_EXPR:
1157  /* AND, OR are inherently non-strict */
1158  return true;
1159  default:
1160  break;
1161  }
1162  }
1163  if (IsA(node, SubLink))
1164  {
1165  /* In some cases a sublink might be strict, but in general not */
1166  return true;
1167  }
1168  if (IsA(node, SubPlan))
1169  return true;
1170  if (IsA(node, AlternativeSubPlan))
1171  return true;
1172  if (IsA(node, FieldStore))
1173  return true;
1174  if (IsA(node, CoerceViaIO))
1175  {
1176  /*
1177  * CoerceViaIO is strict regardless of whether the I/O functions are,
1178  * so just go look at its argument; asking check_functions_in_node is
1179  * useless expense and could deliver the wrong answer.
1180  */
1181  return contain_nonstrict_functions_walker((Node *) ((CoerceViaIO *) node)->arg,
1182  context);
1183  }
1184  if (IsA(node, ArrayCoerceExpr))
1185  {
1186  /*
1187  * ArrayCoerceExpr is strict at the array level, regardless of what
1188  * the per-element expression is; so we should ignore elemexpr and
1189  * recurse only into the arg.
1190  */
1192  context);
1193  }
1194  if (IsA(node, CaseExpr))
1195  return true;
1196  if (IsA(node, ArrayExpr))
1197  return true;
1198  if (IsA(node, RowExpr))
1199  return true;
1200  if (IsA(node, RowCompareExpr))
1201  return true;
1202  if (IsA(node, CoalesceExpr))
1203  return true;
1204  if (IsA(node, MinMaxExpr))
1205  return true;
1206  if (IsA(node, XmlExpr))
1207  return true;
1208  if (IsA(node, NullTest))
1209  return true;
1210  if (IsA(node, BooleanTest))
1211  return true;
1212 
1213  /* Check other function-containing nodes */
1215  context))
1216  return true;
1217 
1219  context);
1220 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1106
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1657
BoolExprType boolop
Definition: primnodes.h:568
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
void * arg
static bool contain_nonstrict_functions_checker(Oid func_id, void *context)
Definition: clauses.c:1100

◆ contain_subplans()

bool contain_subplans ( Node clause)

Definition at line 610 of file clauses.c.

References contain_subplans_walker().

Referenced by convert_EXISTS_to_ANY(), inline_function(), inline_set_returning_function(), qual_is_pushdown_safe(), and subquery_planner().

611 {
612  return contain_subplans_walker(clause, NULL);
613 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:616

◆ contain_subplans_walker()

static bool contain_subplans_walker ( Node node,
void *  context 
)
static

Definition at line 616 of file clauses.c.

References expression_tree_walker(), and IsA.

Referenced by contain_subplans().

617 {
618  if (node == NULL)
619  return false;
620  if (IsA(node, SubPlan) ||
621  IsA(node, AlternativeSubPlan) ||
622  IsA(node, SubLink))
623  return true; /* abort the tree traversal and return true */
624  return expression_tree_walker(node, contain_subplans_walker, context);
625 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:616

◆ contain_volatile_functions()

◆ contain_volatile_functions_checker()

static bool contain_volatile_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 730 of file clauses.c.

References func_volatile().

Referenced by contain_volatile_functions_walker().

731 {
732  return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
733 }
char func_volatile(Oid funcid)
Definition: lsyscache.c:1582

◆ contain_volatile_functions_not_nextval()

bool contain_volatile_functions_not_nextval ( Node clause)

Definition at line 774 of file clauses.c.

References contain_volatile_functions_not_nextval_walker().

Referenced by BeginCopyFrom().

775 {
777 }
static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context)
Definition: clauses.c:787

◆ contain_volatile_functions_not_nextval_checker()

static bool contain_volatile_functions_not_nextval_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 780 of file clauses.c.

References func_volatile().

Referenced by contain_volatile_functions_not_nextval_walker().

781 {
782  return (func_id != F_NEXTVAL_OID &&
783  func_volatile(func_id) == PROVOLATILE_VOLATILE);
784 }
char func_volatile(Oid funcid)
Definition: lsyscache.c:1582

◆ contain_volatile_functions_not_nextval_walker()

static bool contain_volatile_functions_not_nextval_walker ( Node node,
void *  context 
)
static

Definition at line 787 of file clauses.c.

References check_functions_in_node(), contain_volatile_functions_not_nextval_checker(), expression_tree_walker(), IsA, and query_tree_walker().

Referenced by contain_volatile_functions_not_nextval().

788 {
789  if (node == NULL)
790  return false;
791  /* Check for volatile functions in node itself */
792  if (check_functions_in_node(node,
794  context))
795  return true;
796 
797  /*
798  * See notes in contain_mutable_functions_walker about why we treat
799  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
800  * SQLValueFunction is stable. Hence, none of them are of interest here.
801  * Also, since we're intentionally ignoring nextval(), presumably we
802  * should ignore NextValueExpr.
803  */
804 
805  /* Recurse to check arguments */
806  if (IsA(node, Query))
807  {
808  /* Recurse into subselects */
809  return query_tree_walker((Query *) node,
811  context, 0);
812  }
813  return expression_tree_walker(node,
815  context);
816 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2274
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context)
Definition: clauses.c:787
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1657
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
static bool contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
Definition: clauses.c:780

◆ contain_volatile_functions_walker()

static bool contain_volatile_functions_walker ( Node node,
void *  context 
)
static

Definition at line 736 of file clauses.c.

References check_functions_in_node(), contain_volatile_functions_checker(), expression_tree_walker(), IsA, and query_tree_walker().

Referenced by contain_volatile_functions().

737 {
738  if (node == NULL)
739  return false;
740  /* Check for volatile functions in node itself */
742  context))
743  return true;
744 
745  if (IsA(node, NextValueExpr))
746  {
747  /* NextValueExpr is volatile */
748  return true;
749  }
750 
751  /*
752  * See notes in contain_mutable_functions_walker about why we treat
753  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
754  * SQLValueFunction is stable. Hence, none of them are of interest here.
755  */
756 
757  /* Recurse to check arguments */
758  if (IsA(node, Query))
759  {
760  /* Recurse into subselects */
761  return query_tree_walker((Query *) node,
763  context, 0);
764  }
766  context);
767 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2274
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1657
static bool contain_volatile_functions_walker(Node *node, void *context)
Definition: clauses.c:736
static bool contain_volatile_functions_checker(Oid func_id, void *context)
Definition: clauses.c:730
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 494 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

495 {
496  return contain_windowfuncs(clause);
497 }
bool contain_windowfuncs(Node *node)
Definition: rewriteManip.c:197

◆ ece_function_is_safe()

static bool ece_function_is_safe ( Oid  funcid,
eval_const_expressions_context context 
)
static

Definition at line 3634 of file clauses.c.

References eval_const_expressions_context::estimate, and func_volatile().

Referenced by eval_const_expressions_mutator().

3635 {
3636  char provolatile = func_volatile(funcid);
3637 
3638  /*
3639  * Ordinarily we are only allowed to simplify immutable functions. But for
3640  * purposes of estimation, we consider it okay to simplify functions that
3641  * are merely stable; the risk that the result might change from planning
3642  * time to execution time is worth taking in preference to not being able
3643  * to estimate the value at all.
3644  */
3645  if (provolatile == PROVOLATILE_IMMUTABLE)
3646  return true;
3647  if (context->estimate && provolatile == PROVOLATILE_STABLE)
3648  return true;
3649  return false;
3650 }
char func_volatile(Oid funcid)
Definition: lsyscache.c:1582

◆ estimate_expression_value()

Node* estimate_expression_value ( PlannerInfo root,
Node node 
)

Definition at line 2286 of file clauses.c.

References eval_const_expressions_context::active_fns, eval_const_expressions_context::boundParams, PlannerGlobal::boundParams, eval_const_expressions_context::case_val, eval_const_expressions_context::estimate, eval_const_expressions_mutator(), PlannerInfo::glob, NIL, and eval_const_expressions_context::root.

Referenced by array_unnest_support(), bernoulli_samplescangetsamplesize(), clause_selectivity(), generate_series_int4_support(), generate_series_int8_support(), get_restriction_variable(), gincost_opexpr(), gincost_scalararrayopexpr(), preprocess_limit(), scalararraysel(), system_rows_samplescangetsamplesize(), system_samplescangetsamplesize(), and system_time_samplescangetsamplesize().

2287 {
2289 
2290  context.boundParams = root->glob->boundParams; /* bound Params */
2291  /* we do not need to mark the plan as depending on inlined functions */
2292  context.root = NULL;
2293  context.active_fns = NIL; /* nothing being recursively simplified */
2294  context.case_val = NULL; /* no CASE being examined */
2295  context.estimate = true; /* unsafe transformations OK */
2296  return eval_const_expressions_mutator(node, &context);
2297 }
#define NIL
Definition: pg_list.h:65
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2331
ParamListInfo boundParams
Definition: clauses.c:66
PlannerGlobal * glob
Definition: pathnodes.h:179
ParamListInfo boundParams
Definition: pathnodes.h:109

◆ eval_const_expressions()

Node* eval_const_expressions ( PlannerInfo root,
Node node 
)

Definition at line 2253 of file clauses.c.

References eval_const_expressions_context::active_fns, eval_const_expressions_context::boundParams, PlannerGlobal::boundParams, eval_const_expressions_context::case_val, eval_const_expressions_context::estimate, eval_const_expressions_mutator(), PlannerInfo::glob, NIL, and eval_const_expressions_context::root.

Referenced by apply_child_basequals(), ATExecAttachPartition(), ConstraintImpliedByRelConstraint(), convert_EXISTS_to_ANY(), DoCopy(), expression_planner(), expression_planner_with_deps(), get_proposed_default_constraint(), get_relation_constraints(), preprocess_expression(), preprocess_function_rtes(), process_implied_equality(), RelationBuildPartitionKey(), RelationGetIndexExpressions(), RelationGetIndexPredicate(), and simplify_EXISTS_query().

2254 {
2256 
2257  if (root)
2258  context.boundParams = root->glob->boundParams; /* bound Params */
2259  else
2260  context.boundParams = NULL;
2261  context.root = root; /* for inlined-function dependencies */
2262  context.active_fns = NIL; /* nothing being recursively simplified */
2263  context.case_val = NULL; /* no CASE being examined */
2264  context.estimate = false; /* safe transformations only */
2265  return eval_const_expressions_mutator(node, &context);
2266 }
#define NIL
Definition: pg_list.h:65
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2331
ParamListInfo boundParams
Definition: clauses.c:66
PlannerGlobal * glob
Definition: pathnodes.h:179
ParamListInfo boundParams
Definition: pathnodes.h:109

◆ eval_const_expressions_mutator()

static Node * eval_const_expressions_mutator ( Node node,
eval_const_expressions_context context 
)
static

Definition at line 2331 of file clauses.c.

References WindowFunc::aggfilter, AND_EXPR, arg, FieldSelect::arg, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, CaseExpr::arg, NullTest::arg, BooleanTest::arg, CoerceToDomain::arg, NullTest::argisrow, generate_unaccent_rules::args, WindowFunc::args, FuncExpr::args, OpExpr::args, BoolExpr::args, CaseExpr::args, RowExpr::args, CoalesceExpr::args, Assert, BoolGetDatum, BoolExpr::boolop, BooleanTest::booltesttype, eval_const_expressions_context::boundParams, eval_const_expressions_context::case_val, CaseExpr::casecollid, CaseExpr::casetype, castNode, CoalesceExpr::coalescecollid, CoalesceExpr::coalescetype, COERCE_IMPLICIT_CAST, CoerceViaIO::coerceformat, CoerceToDomain::coercionformat, CollateExpr::collOid, Const::constcollid, Const::constisnull, Const::consttype, Const::consttypmod, Const::constvalue, contain_mutable_functions(), ConvertRowtypeExpr::convertformat, copyObject, datumCopy(), DatumGetBool, CaseExpr::defresult, DomainHasConstraints(), ece_all_arguments_const, ece_evaluate_expr, ece_function_is_safe(), ece_generic_processing, ArrayCoerceExpr::elemexpr, elog, ERROR, eval_const_expressions_context::estimate, evaluate_expr(), expand_function_arguments(), CaseWhen::expr, exprCollation(), expression_tree_mutator(), exprType(), exprTypmod(), FieldSelect::fieldnum, FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, get_typlenbyval(), getTypeInputInfo(), getTypeOutputInfo(), HeapTupleIsValid, WindowFunc::inputcollid, FuncExpr::inputcollid, OpExpr::inputcollid, Int32GetDatum, InvalidAttrNumber, InvalidOid, IS_FALSE, IS_NOT_FALSE, IS_NOT_NULL, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_NULL, IS_TRUE, IS_UNKNOWN, IsA, ParamExternData::isnull, lappend(), lfirst, lfirst_node, linitial, list_length(), list_make1, list_make3, list_nth(), WindowFunc::location, FuncExpr::location, OpExpr::location, RelabelType::location, CoerceViaIO::location, ConvertRowtypeExpr::location, CollateExpr::location, CaseExpr::location, CaseWhen::location, CoalesceExpr::location, NullTest::location, BooleanTest::location, CoerceToDomain::location, make_andclause(), make_orclause(), makeBoolConst(), makeConst(), makeNode, makeNullConst(), makeVar(), negate_clause(), NIL, nodeTag, NOT_EXPR, NullTest::nulltesttype, ParamListInfoData::numParams, ObjectIdGetDatum, OidIsValid, OpExpr::opcollid, OpExpr::opfuncid, ScalarArrayOpExpr::opfuncid, OpExpr::opno, OpExpr::opresulttype, OpExpr::opretset, OR_EXPR, PARAM_EXTERN, PARAM_FLAG_CONST, Param::paramcollid, ParamListInfoData::paramFetch, Param::paramid, Param::paramkind, ParamListInfoData::params, Param::paramtype, Param::paramtypmod, ParamExternData::pflags, PlaceHolderVar::phexpr, PROCOID, ParamExternData::ptype, record_plan_type_dependency(), RelabelType::relabelformat, ReleaseSysCache(), CaseWhen::result, FieldSelect::resultcollid, RelabelType::resultcollid, CoerceViaIO::resultcollid, CoerceToDomain::resultcollid, FieldSelect::resulttype, RelabelType::resulttype, CoerceViaIO::resulttype, ConvertRowtypeExpr::resulttype, CoerceToDomain::resulttype, FieldSelect::resulttypmod, RelabelType::resulttypmod, CoerceToDomain::resulttypmod, eval_const_expressions_context::root, RowExpr::row_typeid, rowtype_field_matches(), SearchSysCache1(), set_opfuncid(), set_sa_opfuncid(), simplify_and_arguments(), simplify_boolean_equality(), simplify_function(), simplify_or_arguments(), T_AlternativeSubPlan, T_ArrayCoerceExpr, T_ArrayExpr, T_BooleanTest, T_BoolExpr, T_CaseExpr, T_CaseTestExpr, T_CoalesceExpr, T_CoerceToDomain, T_CoerceViaIO, T_CollateExpr, T_ConvertRowtypeExpr, T_DistinctExpr, T_FieldSelect, T_FuncExpr, T_MinMaxExpr, T_NullTest, T_OpExpr, T_Param, T_PlaceHolderVar, T_RelabelType, T_RowExpr, T_ScalarArrayOpExpr, T_SQLValueFunction, T_SubPlan, T_SubscriptingRef, T_WindowFunc, Expr::type, ParamExternData::value, WindowFunc::winagg, WindowFunc::wincollid, WindowFunc::winfnoid, WindowFunc::winref, WindowFunc::winstar, and WindowFunc::wintype.

Referenced by estimate_expression_value(), eval_const_expressions(), inline_function(), simplify_and_arguments(), simplify_function(), and simplify_or_arguments().

2333 {
2334  if (node == NULL)
2335  return NULL;
2336  switch (nodeTag(node))
2337  {
2338  case T_Param:
2339  {
2340  Param *param = (Param *) node;
2341  ParamListInfo paramLI = context->boundParams;
2342 
2343  /* Look to see if we've been given a value for this Param */
2344  if (param->paramkind == PARAM_EXTERN &&
2345  paramLI != NULL &&
2346  param->paramid > 0 &&
2347  param->paramid <= paramLI->numParams)
2348  {
2349  ParamExternData *prm;
2350  ParamExternData prmdata;
2351 
2352  /*
2353  * Give hook a chance in case parameter is dynamic. Tell
2354  * it that this fetch is speculative, so it should avoid
2355  * erroring out if parameter is unavailable.
2356  */
2357  if (paramLI->paramFetch != NULL)
2358  prm = paramLI->paramFetch(paramLI, param->paramid,
2359  true, &prmdata);
2360  else
2361  prm = &paramLI->params[param->paramid - 1];
2362 
2363  /*
2364  * We don't just check OidIsValid, but insist that the
2365  * fetched type match the Param, just in case the hook did
2366  * something unexpected. No need to throw an error here
2367  * though; leave that for runtime.
2368  */
2369  if (OidIsValid(prm->ptype) &&
2370  prm->ptype == param->paramtype)
2371  {
2372  /* OK to substitute parameter value? */
2373  if (context->estimate ||
2374  (prm->pflags & PARAM_FLAG_CONST))
2375  {
2376  /*
2377  * Return a Const representing the param value.
2378  * Must copy pass-by-ref datatypes, since the
2379  * Param might be in a memory context
2380  * shorter-lived than our output plan should be.
2381  */
2382  int16 typLen;
2383  bool typByVal;
2384  Datum pval;
2385 
2386  get_typlenbyval(param->paramtype,
2387  &typLen, &typByVal);
2388  if (prm->isnull || typByVal)
2389  pval = prm->value;
2390  else
2391  pval = datumCopy(prm->value, typByVal, typLen);
2392  return (Node *) makeConst(param->paramtype,
2393  param->paramtypmod,
2394  param->paramcollid,
2395  (int) typLen,
2396  pval,
2397  prm->isnull,
2398  typByVal);
2399  }
2400  }
2401  }
2402 
2403  /*
2404  * Not replaceable, so just copy the Param (no need to
2405  * recurse)
2406  */
2407  return (Node *) copyObject(param);
2408  }
2409  case T_WindowFunc:
2410  {
2411  WindowFunc *expr = (WindowFunc *) node;
2412  Oid funcid = expr->winfnoid;
2413  List *args;
2414  Expr *aggfilter;
2415  HeapTuple func_tuple;
2416  WindowFunc *newexpr;
2417 
2418  /*
2419  * We can't really simplify a WindowFunc node, but we mustn't
2420  * just fall through to the default processing, because we
2421  * have to apply expand_function_arguments to its argument
2422  * list. That takes care of inserting default arguments and
2423  * expanding named-argument notation.
2424  */
2425  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2426  if (!HeapTupleIsValid(func_tuple))
2427  elog(ERROR, "cache lookup failed for function %u", funcid);
2428 
2429  args = expand_function_arguments(expr->args, expr->wintype,
2430  func_tuple);
2431 
2432  ReleaseSysCache(func_tuple);
2433 
2434  /* Now, recursively simplify the args (which are a List) */
2435  args = (List *)
2438  (void *) context);
2439  /* ... and the filter expression, which isn't */
2440  aggfilter = (Expr *)
2442  context);
2443 
2444  /* And build the replacement WindowFunc node */
2445  newexpr = makeNode(WindowFunc);
2446  newexpr->winfnoid = expr->winfnoid;
2447  newexpr->wintype = expr->wintype;
2448  newexpr->wincollid = expr->wincollid;
2449  newexpr->inputcollid = expr->inputcollid;
2450  newexpr->args = args;
2451  newexpr->aggfilter = aggfilter;
2452  newexpr->winref = expr->winref;
2453  newexpr->winstar = expr->winstar;
2454  newexpr->winagg = expr->winagg;
2455  newexpr->location = expr->location;
2456 
2457  return (Node *) newexpr;
2458  }
2459  case T_FuncExpr:
2460  {
2461  FuncExpr *expr = (FuncExpr *) node;
2462  List *args = expr->args;
2463  Expr *simple;
2464  FuncExpr *newexpr;
2465 
2466  /*
2467  * Code for op/func reduction is pretty bulky, so split it out
2468  * as a separate function. Note: exprTypmod normally returns
2469  * -1 for a FuncExpr, but not when the node is recognizably a
2470  * length coercion; we want to preserve the typmod in the
2471  * eventual Const if so.
2472  */
2473  simple = simplify_function(expr->funcid,
2474  expr->funcresulttype,
2475  exprTypmod(node),
2476  expr->funccollid,
2477  expr->inputcollid,
2478  &args,
2479  expr->funcvariadic,
2480  true,
2481  true,
2482  context);
2483  if (simple) /* successfully simplified it */
2484  return (Node *) simple;
2485 
2486  /*
2487  * The expression cannot be simplified any further, so build
2488  * and return a replacement FuncExpr node using the
2489  * possibly-simplified arguments. Note that we have also
2490  * converted the argument list to positional notation.
2491  */
2492  newexpr = makeNode(FuncExpr);
2493  newexpr->funcid = expr->funcid;
2494  newexpr->funcresulttype = expr->funcresulttype;
2495  newexpr->funcretset = expr->funcretset;
2496  newexpr->funcvariadic = expr->funcvariadic;
2497  newexpr->funcformat = expr->funcformat;
2498  newexpr->funccollid = expr->funccollid;
2499  newexpr->inputcollid = expr->inputcollid;
2500  newexpr->args = args;
2501  newexpr->location = expr->location;
2502  return (Node *) newexpr;
2503  }
2504  case T_OpExpr:
2505  {
2506  OpExpr *expr = (OpExpr *) node;
2507  List *args = expr->args;
2508  Expr *simple;
2509  OpExpr *newexpr;
2510 
2511  /*
2512  * Need to get OID of underlying function. Okay to scribble
2513  * on input to this extent.
2514  */
2515  set_opfuncid(expr);
2516 
2517  /*
2518  * Code for op/func reduction is pretty bulky, so split it out
2519  * as a separate function.
2520  */
2521  simple = simplify_function(expr->opfuncid,
2522  expr->opresulttype, -1,
2523  expr->opcollid,
2524  expr->inputcollid,
2525  &args,
2526  false,
2527  true,
2528  true,
2529  context);
2530  if (simple) /* successfully simplified it */
2531  return (Node *) simple;
2532 
2533  /*
2534  * If the operator is boolean equality or inequality, we know
2535  * how to simplify cases involving one constant and one
2536  * non-constant argument.
2537  */
2538  if (expr->opno == BooleanEqualOperator ||
2539  expr->opno == BooleanNotEqualOperator)
2540  {
2541  simple = (Expr *) simplify_boolean_equality(expr->opno,
2542  args);
2543  if (simple) /* successfully simplified it */
2544  return (Node *) simple;
2545  }
2546 
2547  /*
2548  * The expression cannot be simplified any further, so build
2549  * and return a replacement OpExpr node using the
2550  * possibly-simplified arguments.
2551  */
2552  newexpr = makeNode(OpExpr);
2553  newexpr->opno = expr->opno;
2554  newexpr->opfuncid = expr->opfuncid;
2555  newexpr->opresulttype = expr->opresulttype;
2556  newexpr->opretset = expr->opretset;
2557  newexpr->opcollid = expr->opcollid;
2558  newexpr->inputcollid = expr->inputcollid;
2559  newexpr->args = args;
2560  newexpr->location = expr->location;
2561  return (Node *) newexpr;
2562  }
2563  case T_DistinctExpr:
2564  {
2565  DistinctExpr *expr = (DistinctExpr *) node;
2566  List *args;
2567  ListCell *arg;
2568  bool has_null_input = false;
2569  bool all_null_input = true;
2570  bool has_nonconst_input = false;
2571  Expr *simple;
2572  DistinctExpr *newexpr;
2573 
2574  /*
2575  * Reduce constants in the DistinctExpr's arguments. We know
2576  * args is either NIL or a List node, so we can call
2577  * expression_tree_mutator directly rather than recursing to
2578  * self.
2579  */
2580  args = (List *) expression_tree_mutator((Node *) expr->args,
2582  (void *) context);
2583 
2584  /*
2585  * We must do our own check for NULLs because DistinctExpr has
2586  * different results for NULL input than the underlying
2587  * operator does.
2588  */
2589  foreach(arg, args)
2590  {
2591  if (IsA(lfirst(arg), Const))
2592  {
2593  has_null_input |= ((Const *) lfirst(arg))->constisnull;
2594  all_null_input &= ((Const *) lfirst(arg))->constisnull;
2595  }
2596  else
2597  has_nonconst_input = true;
2598  }
2599 
2600  /* all constants? then can optimize this out */
2601  if (!has_nonconst_input)
2602  {
2603  /* all nulls? then not distinct */
2604  if (all_null_input)
2605  return makeBoolConst(false, false);
2606 
2607  /* one null? then distinct */
2608  if (has_null_input)
2609  return makeBoolConst(true, false);
2610 
2611  /* otherwise try to evaluate the '=' operator */
2612  /* (NOT okay to try to inline it, though!) */
2613 
2614  /*
2615  * Need to get OID of underlying function. Okay to
2616  * scribble on input to this extent.
2617  */
2618  set_opfuncid((OpExpr *) expr); /* rely on struct
2619  * equivalence */
2620 
2621  /*
2622  * Code for op/func reduction is pretty bulky, so split it
2623  * out as a separate function.
2624  */
2625  simple = simplify_function(expr->opfuncid,
2626  expr->opresulttype, -1,
2627  expr->opcollid,
2628  expr->inputcollid,
2629  &args,
2630  false,
2631  false,
2632  false,
2633  context);
2634  if (simple) /* successfully simplified it */
2635  {
2636  /*
2637  * Since the underlying operator is "=", must negate
2638  * its result
2639  */
2640  Const *csimple = castNode(Const, simple);
2641 
2642  csimple->constvalue =
2643  BoolGetDatum(!DatumGetBool(csimple->constvalue));
2644  return (Node *) csimple;
2645  }
2646  }
2647 
2648  /*
2649  * The expression cannot be simplified any further, so build
2650  * and return a replacement DistinctExpr node using the
2651  * possibly-simplified arguments.
2652  */
2653  newexpr = makeNode(DistinctExpr);
2654  newexpr->opno = expr->opno;
2655  newexpr->opfuncid = expr->opfuncid;
2656  newexpr->opresulttype = expr->opresulttype;
2657  newexpr->opretset = expr->opretset;
2658  newexpr->opcollid = expr->opcollid;
2659  newexpr->inputcollid = expr->inputcollid;
2660  newexpr->args = args;
2661  newexpr->location = expr->location;
2662  return (Node *) newexpr;
2663  }
2664  case T_ScalarArrayOpExpr:
2665  {
2666  ScalarArrayOpExpr *saop;
2667 
2668  /* Copy the node and const-simplify its arguments */
2669  saop = (ScalarArrayOpExpr *) ece_generic_processing(node);
2670 
2671  /* Make sure we know underlying function */
2672  set_sa_opfuncid(saop);
2673 
2674  /*
2675  * If all arguments are Consts, and it's a safe function, we
2676  * can fold to a constant
2677  */
2678  if (ece_all_arguments_const(saop) &&
2679  ece_function_is_safe(saop->opfuncid, context))
2680  return ece_evaluate_expr(saop);
2681  return (Node *) saop;
2682  }
2683  case T_BoolExpr:
2684  {
2685  BoolExpr *expr = (BoolExpr *) node;
2686 
2687  switch (expr->boolop)
2688  {
2689  case OR_EXPR:
2690  {
2691  List *newargs;
2692  bool haveNull = false;
2693  bool forceTrue = false;
2694 
2695  newargs = simplify_or_arguments(expr->args,
2696  context,
2697  &haveNull,
2698  &forceTrue);
2699  if (forceTrue)
2700  return makeBoolConst(true, false);
2701  if (haveNull)
2702  newargs = lappend(newargs,
2703  makeBoolConst(false, true));
2704  /* If all the inputs are FALSE, result is FALSE */
2705  if (newargs == NIL)
2706  return makeBoolConst(false, false);
2707 
2708  /*
2709  * If only one nonconst-or-NULL input, it's the
2710  * result
2711  */
2712  if (list_length(newargs) == 1)
2713  return (Node *) linitial(newargs);
2714  /* Else we still need an OR node */
2715  return (Node *) make_orclause(newargs);
2716  }
2717  case AND_EXPR:
2718  {
2719  List *newargs;
2720  bool haveNull = false;
2721  bool forceFalse = false;
2722 
2723  newargs = simplify_and_arguments(expr->args,
2724  context,
2725  &haveNull,
2726  &forceFalse);
2727  if (forceFalse)
2728  return makeBoolConst(false, false);
2729  if (haveNull)
2730  newargs = lappend(newargs,
2731  makeBoolConst(false, true));
2732  /* If all the inputs are TRUE, result is TRUE */
2733  if (newargs == NIL)
2734  return makeBoolConst(true, false);
2735 
2736  /*
2737  * If only one nonconst-or-NULL input, it's the
2738  * result
2739  */
2740  if (list_length(newargs) == 1)
2741  return (Node *) linitial(newargs);
2742  /* Else we still need an AND node */
2743  return (Node *) make_andclause(newargs);
2744  }
2745  case NOT_EXPR:
2746  {
2747  Node *arg;
2748 
2749  Assert(list_length(expr->args) == 1);
2751  context);
2752 
2753  /*
2754  * Use negate_clause() to see if we can simplify
2755  * away the NOT.
2756  */
2757  return negate_clause(arg);
2758  }
2759  default:
2760  elog(ERROR, "unrecognized boolop: %d",
2761  (int) expr->boolop);
2762  break;
2763  }
2764  break;
2765  }
2766  case T_SubPlan:
2767  case T_AlternativeSubPlan:
2768 
2769  /*
2770  * Return a SubPlan unchanged --- too late to do anything with it.
2771  *
2772  * XXX should we ereport() here instead? Probably this routine
2773  * should never be invoked after SubPlan creation.
2774  */
2775  return node;
2776  case T_RelabelType:
2777  {
2778  /*
2779  * If we can simplify the input to a constant, then we don't
2780  * need the RelabelType node anymore: just change the type
2781  * field of the Const node. Otherwise, must copy the
2782  * RelabelType node.
2783  */
2784  RelabelType *relabel = (RelabelType *) node;
2785  Node *arg;
2786 
2787  arg = eval_const_expressions_mutator((Node *) relabel->arg,
2788  context);
2789 
2790  /*
2791  * If we find stacked RelabelTypes (eg, from foo :: int ::
2792  * oid) we can discard all but the top one.
2793  */
2794  while (arg && IsA(arg, RelabelType))
2795  arg = (Node *) ((RelabelType *) arg)->arg;
2796 
2797  if (arg && IsA(arg, Const))
2798  {
2799  Const *con = (Const *) arg;
2800 
2801  con->consttype = relabel->resulttype;
2802  con->consttypmod = relabel->resulttypmod;
2803  con->constcollid = relabel->resultcollid;
2804  return (Node *) con;
2805  }
2806  else
2807  {
2808  RelabelType *newrelabel = makeNode(RelabelType);
2809 
2810  newrelabel->arg = (Expr *) arg;
2811  newrelabel->resulttype = relabel->resulttype;
2812  newrelabel->resulttypmod = relabel->resulttypmod;
2813  newrelabel->resultcollid = relabel->resultcollid;
2814  newrelabel->relabelformat = relabel->relabelformat;
2815  newrelabel->location = relabel->location;
2816  return (Node *) newrelabel;
2817  }
2818  }
2819  case T_CoerceViaIO:
2820  {
2821  CoerceViaIO *expr = (CoerceViaIO *) node;
2822  List *args;
2823  Oid outfunc;
2824  bool outtypisvarlena;
2825  Oid infunc;
2826  Oid intypioparam;
2827  Expr *simple;
2828  CoerceViaIO *newexpr;
2829 
2830  /* Make a List so we can use simplify_function */
2831  args = list_make1(expr->arg);
2832 
2833  /*
2834  * CoerceViaIO represents calling the source type's output
2835  * function then the result type's input function. So, try to
2836  * simplify it as though it were a stack of two such function
2837  * calls. First we need to know what the functions are.
2838  *
2839  * Note that the coercion functions are assumed not to care
2840  * about input collation, so we just pass InvalidOid for that.
2841  */
2842  getTypeOutputInfo(exprType((Node *) expr->arg),
2843  &outfunc, &outtypisvarlena);
2845  &infunc, &intypioparam);
2846 
2847  simple = simplify_function(outfunc,
2848  CSTRINGOID, -1,
2849  InvalidOid,
2850  InvalidOid,
2851  &args,
2852  false,
2853  true,
2854  true,
2855  context);
2856  if (simple) /* successfully simplified output fn */
2857  {
2858  /*
2859  * Input functions may want 1 to 3 arguments. We always
2860  * supply all three, trusting that nothing downstream will
2861  * complain.
2862  */
2863  args = list_make3(simple,
2864  makeConst(OIDOID,
2865  -1,
2866  InvalidOid,
2867  sizeof(Oid),
2868  ObjectIdGetDatum(intypioparam),
2869  false,
2870  true),
2871  makeConst(INT4OID,
2872  -1,
2873  InvalidOid,
2874  sizeof(int32),
2875  Int32GetDatum(-1),
2876  false,
2877  true));
2878 
2879  simple = simplify_function(infunc,
2880  expr->resulttype, -1,
2881  expr->resultcollid,
2882  InvalidOid,
2883  &args,
2884  false,
2885  false,
2886  true,
2887  context);
2888  if (simple) /* successfully simplified input fn */
2889  return (Node *) simple;
2890  }
2891 
2892  /*
2893  * The expression cannot be simplified any further, so build
2894  * and return a replacement CoerceViaIO node using the
2895  * possibly-simplified argument.
2896  */
2897  newexpr = makeNode(CoerceViaIO);
2898  newexpr->arg = (Expr *) linitial(args);
2899  newexpr->resulttype = expr->resulttype;
2900  newexpr->resultcollid = expr->resultcollid;
2901  newexpr->coerceformat = expr->coerceformat;
2902  newexpr->location = expr->location;
2903  return (Node *) newexpr;
2904  }
2905  case T_ArrayCoerceExpr:
2906  {
2908  Node *save_case_val;
2909 
2910  /*
2911  * Copy the node and const-simplify its arguments. We can't
2912  * use ece_generic_processing() here because we need to mess
2913  * with case_val only while processing the elemexpr.
2914  */
2915  memcpy(ac, node, sizeof(ArrayCoerceExpr));
2916  ac->arg = (Expr *)
2918  context);
2919 
2920  /*
2921  * Set up for the CaseTestExpr node contained in the elemexpr.
2922  * We must prevent it from absorbing any outer CASE value.
2923  */
2924  save_case_val = context->case_val;
2925  context->case_val = NULL;
2926 
2927  ac->elemexpr = (Expr *)
2929  context);
2930 
2931  context->case_val = save_case_val;
2932 
2933  /*
2934  * If constant argument and the per-element expression is
2935  * immutable, we can simplify the whole thing to a constant.
2936  * Exception: although contain_mutable_functions considers
2937  * CoerceToDomain immutable for historical reasons, let's not
2938  * do so here; this ensures coercion to an array-over-domain
2939  * does not apply the domain's constraints until runtime.
2940  */
2941  if (ac->arg && IsA(ac->arg, Const) &&
2942  ac->elemexpr && !IsA(ac->elemexpr, CoerceToDomain) &&
2944  return ece_evaluate_expr(ac);
2945 
2946  return (Node *) ac;
2947  }
2948  case T_CollateExpr:
2949  {
2950  /*
2951  * If we can simplify the input to a constant, then we don't
2952  * need the CollateExpr node at all: just change the
2953  * constcollid field of the Const node. Otherwise, replace
2954  * the CollateExpr with a RelabelType. (We do that so as to
2955  * improve uniformity of expression representation and thus
2956  * simplify comparison of expressions.)
2957  */
2958  CollateExpr *collate = (CollateExpr *) node;
2959  Node *arg;
2960 
2961  arg = eval_const_expressions_mutator((Node *) collate->arg,
2962  context);
2963 
2964  if (arg && IsA(arg, Const))
2965  {
2966  Const *con = (Const *) arg;
2967 
2968  con->constcollid = collate->collOid;
2969  return (Node *) con;
2970  }
2971  else if (collate->collOid == exprCollation(arg))
2972  {
2973  /* Don't need a RelabelType either... */
2974  return arg;
2975  }
2976  else
2977  {
2978  RelabelType *relabel = makeNode(RelabelType);
2979 
2980  relabel->resulttype = exprType(arg);
2981  relabel->resulttypmod = exprTypmod(arg);
2982  relabel->resultcollid = collate->collOid;
2984  relabel->location = collate->location;
2985 
2986  /* Don't create stacked RelabelTypes */
2987  while (arg && IsA(arg, RelabelType))
2988  arg = (Node *) ((RelabelType *) arg)->arg;
2989  relabel->arg = (Expr *) arg;
2990 
2991  return (Node *) relabel;
2992  }
2993  }
2994  case T_CaseExpr:
2995  {
2996  /*----------
2997  * CASE expressions can be simplified if there are constant
2998  * condition clauses:
2999  * FALSE (or NULL): drop the alternative
3000  * TRUE: drop all remaining alternatives
3001  * If the first non-FALSE alternative is a constant TRUE,
3002  * we can simplify the entire CASE to that alternative's
3003  * expression. If there are no non-FALSE alternatives,
3004  * we simplify the entire CASE to the default result (ELSE).
3005  *
3006  * If we have a simple-form CASE with constant test
3007  * expression, we substitute the constant value for contained
3008  * CaseTestExpr placeholder nodes, so that we have the
3009  * opportunity to reduce constant test conditions. For
3010  * example this allows
3011  * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
3012  * to reduce to 1 rather than drawing a divide-by-0 error.
3013  * Note that when the test expression is constant, we don't
3014  * have to include it in the resulting CASE; for example
3015  * CASE 0 WHEN x THEN y ELSE z END
3016  * is transformed by the parser to
3017  * CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
3018  * which we can simplify to
3019  * CASE WHEN 0 = x THEN y ELSE z END
3020  * It is not necessary for the executor to evaluate the "arg"
3021  * expression when executing the CASE, since any contained
3022  * CaseTestExprs that might have referred to it will have been
3023  * replaced by the constant.
3024  *----------
3025  */
3026  CaseExpr *caseexpr = (CaseExpr *) node;
3027  CaseExpr *newcase;
3028  Node *save_case_val;
3029  Node *newarg;
3030  List *newargs;
3031  bool const_true_cond;
3032  Node *defresult = NULL;
3033  ListCell *arg;
3034 
3035  /* Simplify the test expression, if any */
3036  newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
3037  context);
3038 
3039  /* Set up for contained CaseTestExpr nodes */
3040  save_case_val = context->case_val;
3041  if (newarg && IsA(newarg, Const))
3042  {
3043  context->case_val = newarg;
3044  newarg = NULL; /* not needed anymore, see above */
3045  }
3046  else
3047  context->case_val = NULL;
3048 
3049  /* Simplify the WHEN clauses */
3050  newargs = NIL;
3051  const_true_cond = false;
3052  foreach(arg, caseexpr->args)
3053  {
3054  CaseWhen *oldcasewhen = lfirst_node(CaseWhen, arg);
3055  Node *casecond;
3056  Node *caseresult;
3057 
3058  /* Simplify this alternative's test condition */
3059  casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
3060  context);
3061 
3062  /*
3063  * If the test condition is constant FALSE (or NULL), then
3064  * drop this WHEN clause completely, without processing
3065  * the result.
3066  */
3067  if (casecond && IsA(casecond, Const))
3068  {
3069  Const *const_input = (Const *) casecond;
3070 
3071  if (const_input->constisnull ||
3072  !DatumGetBool(const_input->constvalue))
3073  continue; /* drop alternative with FALSE cond */
3074  /* Else it's constant TRUE */
3075  const_true_cond = true;
3076  }
3077 
3078  /* Simplify this alternative's result value */
3079  caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
3080  context);
3081 
3082  /* If non-constant test condition, emit a new WHEN node */
3083  if (!const_true_cond)
3084  {
3085  CaseWhen *newcasewhen = makeNode(CaseWhen);
3086 
3087  newcasewhen->expr = (Expr *) casecond;
3088  newcasewhen->result = (Expr *) caseresult;
3089  newcasewhen->location = oldcasewhen->location;
3090  newargs = lappend(newargs, newcasewhen);
3091  continue;
3092  }
3093 
3094  /*
3095  * Found a TRUE condition, so none of the remaining
3096  * alternatives can be reached. We treat the result as
3097  * the default result.
3098  */
3099  defresult = caseresult;
3100  break;
3101  }
3102 
3103  /* Simplify the default result, unless we replaced it above */
3104  if (!const_true_cond)
3105  defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
3106  context);
3107 
3108  context->case_val = save_case_val;
3109 
3110  /*
3111  * If no non-FALSE alternatives, CASE reduces to the default
3112  * result
3113  */
3114  if (newargs == NIL)
3115  return defresult;
3116  /* Otherwise we need a new CASE node */
3117  newcase = makeNode(CaseExpr);
3118  newcase->casetype = caseexpr->casetype;
3119  newcase->casecollid = caseexpr->casecollid;
3120  newcase->arg = (Expr *) newarg;
3121  newcase->args = newargs;
3122  newcase->defresult = (Expr *) defresult;
3123  newcase->location = caseexpr->location;
3124  return (Node *) newcase;
3125  }
3126  case T_CaseTestExpr:
3127  {
3128  /*
3129  * If we know a constant test value for the current CASE
3130  * construct, substitute it for the placeholder. Else just
3131  * return the placeholder as-is.
3132  */
3133  if (context->case_val)
3134  return copyObject(context->case_val);
3135  else
3136  return copyObject(node);
3137  }
3138  case T_SubscriptingRef:
3139  case T_ArrayExpr:
3140  case T_RowExpr:
3141  case T_MinMaxExpr:
3142  {
3143  /*
3144  * Generic handling for node types whose own processing is
3145  * known to be immutable, and for which we need no smarts
3146  * beyond "simplify if all inputs are constants".
3147  *
3148  * Treating MinMaxExpr this way amounts to assuming that the
3149  * btree comparison function it calls is immutable; see the
3150  * reasoning in contain_mutable_functions_walker.
3151  */
3152 
3153  /* Copy the node and const-simplify its arguments */
3154  node = ece_generic_processing(node);
3155  /* If all arguments are Consts, we can fold to a constant */
3156  if (ece_all_arguments_const(node))
3157  return ece_evaluate_expr(node);
3158  return node;
3159  }
3160  case T_CoalesceExpr:
3161  {
3162  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3163  CoalesceExpr *newcoalesce;
3164  List *newargs;
3165  ListCell *arg;
3166 
3167  newargs = NIL;
3168  foreach(arg, coalesceexpr->args)
3169  {
3170  Node *e;
3171 
3173  context);
3174 
3175  /*
3176  * We can remove null constants from the list. For a
3177  * non-null constant, if it has not been preceded by any
3178  * other non-null-constant expressions then it is the
3179  * result. Otherwise, it's the next argument, but we can
3180  * drop following arguments since they will never be
3181  * reached.
3182  */
3183  if (IsA(e, Const))
3184  {
3185  if (((Const *) e)->constisnull)
3186  continue; /* drop null constant */
3187  if (newargs == NIL)
3188  return e; /* first expr */
3189  newargs = lappend(newargs, e);
3190  break;
3191  }
3192  newargs = lappend(newargs, e);
3193  }
3194 
3195  /*
3196  * If all the arguments were constant null, the result is just
3197  * null
3198  */
3199  if (newargs == NIL)
3200  return (Node *) makeNullConst(coalesceexpr->coalescetype,
3201  -1,
3202  coalesceexpr->coalescecollid);
3203 
3204  newcoalesce = makeNode(CoalesceExpr);
3205  newcoalesce->coalescetype = coalesceexpr->coalescetype;
3206  newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
3207  newcoalesce->args = newargs;
3208  newcoalesce->location = coalesceexpr->location;
3209  return (Node *) newcoalesce;
3210  }
3211  case T_SQLValueFunction:
3212  {
3213  /*
3214  * All variants of SQLValueFunction are stable, so if we are
3215  * estimating the expression's value, we should evaluate the
3216  * current function value. Otherwise just copy.
3217  */
3218  SQLValueFunction *svf = (SQLValueFunction *) node;
3219 
3220  if (context->estimate)
3221  return (Node *) evaluate_expr((Expr *) svf,
3222  svf->type,
3223  svf->typmod,
3224  InvalidOid);
3225  else
3226  return copyObject((Node *) svf);
3227  }
3228  case T_FieldSelect:
3229  {
3230  /*
3231  * We can optimize field selection from a whole-row Var into a
3232  * simple Var. (This case won't be generated directly by the
3233  * parser, because ParseComplexProjection short-circuits it.
3234  * But it can arise while simplifying functions.) Also, we
3235  * can optimize field selection from a RowExpr construct, or
3236  * of course from a constant.
3237  *
3238  * However, replacing a whole-row Var in this way has a
3239  * pitfall: if we've already built the rel targetlist for the
3240  * source relation, then the whole-row Var is scheduled to be
3241  * produced by the relation scan, but the simple Var probably
3242  * isn't, which will lead to a failure in setrefs.c. This is
3243  * not a problem when handling simple single-level queries, in
3244  * which expression simplification always happens first. It
3245  * is a risk for lateral references from subqueries, though.
3246  * To avoid such failures, don't optimize uplevel references.
3247  *
3248  * We must also check that the declared type of the field is
3249  * still the same as when the FieldSelect was created --- this
3250  * can change if someone did ALTER COLUMN TYPE on the rowtype.
3251  * If it isn't, we skip the optimization; the case will
3252  * probably fail at runtime, but that's not our problem here.
3253  */
3254  FieldSelect *fselect = (FieldSelect *) node;
3255  FieldSelect *newfselect;
3256  Node *arg;
3257 
3258  arg = eval_const_expressions_mutator((Node *) fselect->arg,
3259  context);
3260  if (arg && IsA(arg, Var) &&
3261  ((Var *) arg)->varattno == InvalidAttrNumber &&
3262  ((Var *) arg)->varlevelsup == 0)
3263  {
3264  if (rowtype_field_matches(((Var *) arg)->vartype,
3265  fselect->fieldnum,
3266  fselect->resulttype,
3267  fselect->resulttypmod,
3268  fselect->resultcollid))
3269  return (Node *) makeVar(((Var *) arg)->varno,
3270  fselect->fieldnum,
3271  fselect->resulttype,
3272  fselect->resulttypmod,
3273  fselect->resultcollid,
3274  ((Var *) arg)->varlevelsup);
3275  }
3276  if (arg && IsA(arg, RowExpr))
3277  {
3278  RowExpr *rowexpr = (RowExpr *) arg;
3279 
3280  if (fselect->fieldnum > 0 &&
3281  fselect->fieldnum <= list_length(rowexpr->args))
3282  {
3283  Node *fld = (Node *) list_nth(rowexpr->args,
3284  fselect->fieldnum - 1);
3285 
3286  if (rowtype_field_matches(rowexpr->row_typeid,
3287  fselect->fieldnum,
3288  fselect->resulttype,
3289  fselect->resulttypmod,
3290  fselect->resultcollid) &&
3291  fselect->resulttype == exprType(fld) &&
3292  fselect->resulttypmod == exprTypmod(fld) &&
3293  fselect->resultcollid == exprCollation(fld))
3294  return fld;
3295  }
3296  }
3297  newfselect = makeNode(FieldSelect);
3298  newfselect->arg = (Expr *) arg;
3299  newfselect->fieldnum = fselect->fieldnum;
3300  newfselect->resulttype = fselect->resulttype;
3301  newfselect->resulttypmod = fselect->resulttypmod;
3302  newfselect->resultcollid = fselect->resultcollid;
3303  if (arg && IsA(arg, Const))
3304  {
3305  Const *con = (Const *) arg;
3306 
3308  newfselect->fieldnum,
3309  newfselect->resulttype,
3310  newfselect->resulttypmod,
3311  newfselect->resultcollid))
3312  return ece_evaluate_expr(newfselect);
3313  }
3314  return (Node *) newfselect;
3315  }
3316  case T_NullTest:
3317  {
3318  NullTest *ntest = (NullTest *) node;
3319  NullTest *newntest;
3320  Node *arg;
3321 
3322  arg = eval_const_expressions_mutator((Node *) ntest->arg,
3323  context);
3324  if (ntest->argisrow && arg && IsA(arg, RowExpr))
3325  {
3326  /*
3327  * We break ROW(...) IS [NOT] NULL into separate tests on
3328  * its component fields. This form is usually more
3329  * efficient to evaluate, as well as being more amenable
3330  * to optimization.
3331  */
3332  RowExpr *rarg = (RowExpr *) arg;
3333  List *newargs = NIL;
3334  ListCell *l;
3335 
3336  foreach(l, rarg->args)
3337  {
3338  Node *relem = (Node *) lfirst(l);
3339 
3340  /*
3341  * A constant field refutes the whole NullTest if it's
3342  * of the wrong nullness; else we can discard it.
3343  */
3344  if (relem && IsA(relem, Const))
3345  {
3346  Const *carg = (Const *) relem;
3347 
3348  if (carg->constisnull ?
3349  (ntest->nulltesttype == IS_NOT_NULL) :
3350  (ntest->nulltesttype == IS_NULL))
3351  return makeBoolConst(false, false);
3352  continue;
3353  }
3354 
3355  /*
3356  * Else, make a scalar (argisrow == false) NullTest
3357  * for this field. Scalar semantics are required
3358  * because IS [NOT] NULL doesn't recurse; see comments
3359  * in ExecEvalRowNullInt().
3360  */
3361  newntest = makeNode(NullTest);
3362  newntest->arg = (Expr *) relem;
3363  newntest->nulltesttype = ntest->nulltesttype;
3364  newntest->argisrow = false;
3365  newntest->location = ntest->location;
3366  newargs = lappend(newargs, newntest);
3367  }
3368  /* If all the inputs were constants, result is TRUE */
3369  if (newargs == NIL)
3370  return makeBoolConst(true, false);
3371  /* If only one nonconst input, it's the result */
3372  if (list_length(newargs) == 1)
3373  return (Node *) linitial(newargs);
3374  /* Else we need an AND node */
3375  return (Node *) make_andclause(newargs);
3376  }
3377  if (!ntest->argisrow && arg && IsA(arg, Const))
3378  {
3379  Const *carg = (Const *) arg;
3380  bool result;
3381 
3382  switch (ntest->nulltesttype)
3383  {
3384  case IS_NULL:
3385  result = carg->constisnull;
3386  break;
3387  case IS_NOT_NULL:
3388  result = !carg->constisnull;
3389  break;
3390  default:
3391  elog(ERROR, "unrecognized nulltesttype: %d",
3392  (int) ntest->nulltesttype);
3393  result = false; /* keep compiler quiet */
3394  break;
3395  }
3396 
3397  return makeBoolConst(result, false);
3398  }
3399 
3400  newntest = makeNode(NullTest);
3401  newntest->arg = (Expr *) arg;
3402  newntest->nulltesttype = ntest->nulltesttype;
3403  newntest->argisrow = ntest->argisrow;
3404  newntest->location = ntest->location;
3405  return (Node *) newntest;
3406  }
3407  case T_BooleanTest:
3408  {
3409  /*
3410  * This case could be folded into the generic handling used
3411  * for SubscriptingRef etc. But because the simplification
3412  * logic is so trivial, applying evaluate_expr() to perform it
3413  * would be a heavy overhead. BooleanTest is probably common
3414  * enough to justify keeping this bespoke implementation.
3415  */
3416  BooleanTest *btest = (BooleanTest *) node;
3417  BooleanTest *newbtest;
3418  Node *arg;
3419 
3420  arg = eval_const_expressions_mutator((Node *) btest->arg,
3421  context);
3422  if (arg && IsA(arg, Const))
3423  {
3424  Const *carg = (Const *) arg;
3425  bool result;
3426 
3427  switch (btest->booltesttype)
3428  {
3429  case IS_TRUE:
3430  result = (!carg->constisnull &&
3431  DatumGetBool(carg->constvalue));
3432  break;
3433  case IS_NOT_TRUE:
3434  result = (carg->constisnull ||
3435  !DatumGetBool(carg->constvalue));
3436  break;
3437  case IS_FALSE:
3438  result = (!carg->constisnull &&
3439  !DatumGetBool(carg->constvalue));
3440  break;
3441  case IS_NOT_FALSE:
3442  result = (carg->constisnull ||
3443  DatumGetBool(carg->constvalue));
3444  break;
3445  case IS_UNKNOWN:
3446  result = carg->constisnull;
3447  break;
3448  case IS_NOT_UNKNOWN:
3449  result = !carg->constisnull;
3450  break;
3451  default:
3452  elog(ERROR, "unrecognized booltesttype: %d",
3453  (int) btest->booltesttype);
3454  result = false; /* keep compiler quiet */
3455  break;
3456  }
3457 
3458  return makeBoolConst(result, false);
3459  }
3460 
3461  newbtest = makeNode(BooleanTest);
3462  newbtest->arg = (Expr *) arg;
3463  newbtest->booltesttype = btest->booltesttype;
3464  newbtest->location = btest->location;
3465  return (Node *) newbtest;
3466  }
3467  case T_CoerceToDomain:
3468  {
3469  /*
3470  * If the domain currently has no constraints, we replace the
3471  * CoerceToDomain node with a simple RelabelType, which is
3472  * both far faster to execute and more amenable to later
3473  * optimization. We must then mark the plan as needing to be
3474  * rebuilt if the domain's constraints change.
3475  *
3476  * Also, in estimation mode, always replace CoerceToDomain
3477  * nodes, effectively assuming that the coercion will succeed.
3478  */
3479  CoerceToDomain *cdomain = (CoerceToDomain *) node;
3480  CoerceToDomain *newcdomain;
3481  Node *arg;
3482 
3483  arg = eval_const_expressions_mutator((Node *) cdomain->arg,
3484  context);
3485  if (context->estimate ||
3486  !DomainHasConstraints(cdomain->resulttype))
3487  {
3488  /* Record dependency, if this isn't estimation mode */
3489  if (context->root && !context->estimate)
3490  record_plan_type_dependency(context->root,
3491  cdomain->resulttype);
3492 
3493  /* Generate RelabelType to substitute for CoerceToDomain */
3494  /* This should match the RelabelType logic above */
3495 
3496  while (arg && IsA(arg, RelabelType))
3497  arg = (Node *) ((RelabelType *) arg)->arg;
3498 
3499  if (arg && IsA(arg, Const))
3500  {
3501  Const *con = (Const *) arg;
3502 
3503  con->consttype = cdomain->resulttype;
3504  con->consttypmod = cdomain->resulttypmod;
3505  con->constcollid = cdomain->resultcollid;
3506  return (Node *) con;
3507  }
3508  else
3509  {
3510  RelabelType *newrelabel = makeNode(RelabelType);
3511 
3512  newrelabel->arg = (Expr *) arg;
3513  newrelabel->resulttype = cdomain->resulttype;
3514  newrelabel->resulttypmod = cdomain->resulttypmod;
3515  newrelabel->resultcollid = cdomain->resultcollid;
3516  newrelabel->relabelformat = cdomain->coercionformat;
3517  newrelabel->location = cdomain->location;
3518  return (Node *) newrelabel;
3519  }
3520  }
3521 
3522  newcdomain = makeNode(CoerceToDomain);
3523  newcdomain->arg = (Expr *) arg;
3524  newcdomain->resulttype = cdomain->resulttype;
3525  newcdomain->resulttypmod = cdomain->resulttypmod;
3526  newcdomain->resultcollid = cdomain->resultcollid;
3527  newcdomain->coercionformat = cdomain->coercionformat;
3528  newcdomain->location = cdomain->location;
3529  return (Node *) newcdomain;
3530  }
3531  case T_PlaceHolderVar:
3532 
3533  /*
3534  * In estimation mode, just strip the PlaceHolderVar node
3535  * altogether; this amounts to estimating that the contained value
3536  * won't be forced to null by an outer join. In regular mode we
3537  * just use the default behavior (ie, simplify the expression but
3538  * leave the PlaceHolderVar node intact).
3539  */
3540  if (context->estimate)
3541  {
3542  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3543 
3544  return eval_const_expressions_mutator((Node *) phv->phexpr,
3545  context);
3546  }
3547  break;
3548  case T_ConvertRowtypeExpr:
3549  {
3551  Node *arg;
3552  ConvertRowtypeExpr *newcre;
3553 
3554  arg = eval_const_expressions_mutator((Node *) cre->arg,
3555  context);
3556 
3557  newcre = makeNode(ConvertRowtypeExpr);
3558  newcre->resulttype = cre->resulttype;
3559  newcre->convertformat = cre->convertformat;
3560  newcre->location = cre->location;
3561 
3562  /*
3563  * In case of a nested ConvertRowtypeExpr, we can convert the
3564  * leaf row directly to the topmost row format without any
3565  * intermediate conversions. (This works because
3566  * ConvertRowtypeExpr is used only for child->parent
3567  * conversion in inheritance trees, which works by exact match
3568  * of column name, and a column absent in an intermediate
3569  * result can't be present in the final result.)
3570  *
3571  * No need to check more than one level deep, because the
3572  * above recursion will have flattened anything else.
3573  */
3574  if (arg != NULL && IsA(arg, ConvertRowtypeExpr))
3575  {
3576  ConvertRowtypeExpr *argcre = (ConvertRowtypeExpr *) arg;
3577 
3578  arg = (Node *) argcre->arg;
3579 
3580  /*
3581  * Make sure an outer implicit conversion can't hide an
3582  * inner explicit one.
3583  */
3584  if (newcre->convertformat == COERCE_IMPLICIT_CAST)
3585  newcre->convertformat = argcre->convertformat;
3586  }
3587 
3588  newcre->arg = (Expr *) arg;
3589 
3590  if (arg != NULL && IsA(arg, Const))
3591  return ece_evaluate_expr((Node *) newcre);
3592  return (Node *) newcre;
3593  }
3594  default:
3595  break;
3596  }
3597 
3598  /*
3599  * For any node type not handled above, copy the node unchanged but
3600  * const-simplify its subexpressions. This is the correct thing for node
3601  * types whose behavior might change between planning and execution, such
3602  * as CurrentOfExpr. It's also a safe default for new node types not
3603  * known to this routine.
3604  */
3605  return ece_generic_processing(node);
3606 }
Datum constvalue
Definition: primnodes.h:200
#define list_make3(x1, x2, x3)
Definition: pg_list.h:231
Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
Definition: clauses.c:4778
signed short int16
Definition: c.h:345
Oid funcresulttype
Definition: primnodes.h:456
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:124
#define NIL
Definition: pg_list.h:65
Datum value
Definition: params.h:92
#define ece_generic_processing(node)
Definition: clauses.c:2308
List * args
Definition: primnodes.h:1008
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static Expr * simplify_function(Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List **args_p, bool funcvariadic, bool process_args, bool allow_non_const, eval_const_expressions_context *context)
Definition: clauses.c:3941
Node * negate_clause(Node *node)
Definition: prepqual.c:74
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2502
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
List * expand_function_arguments(List *args, Oid result_type, HeapTuple func_tuple)
Definition: clauses.c:4048
List * args
Definition: primnodes.h:363
List * args
Definition: primnodes.h:463
Oid wincollid
Definition: primnodes.h:361
int32 resulttypmod
Definition: primnodes.h:1247
Oid resulttype
Definition: primnodes.h:750
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Oid funccollid
Definition: primnodes.h:461
Oid resulttype
Definition: primnodes.h:821
Oid casecollid
Definition: primnodes.h:917
Expr * arg
Definition: primnodes.h:800
ParamKind paramkind
Definition: primnodes.h:248
Definition: nodes.h:525
CoercionForm coercionformat
Definition: primnodes.h:1249
Expr * arg
Definition: primnodes.h:748
bool funcretset
Definition: primnodes.h:457
Oid casetype
Definition: primnodes.h:916
unsigned int Oid
Definition: postgres_ext.h:31
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:649
Index winref
Definition: primnodes.h:365
Definition: primnodes.h:167
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:297
#define OidIsValid(objectId)
Definition: c.h:638
#define ece_all_arguments_const(node)
Definition: clauses.c:2317
int location
Definition: primnodes.h:932
signed int int32
Definition: c.h:346
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
bool DomainHasConstraints(Oid type_id)
Definition: typcache.c:1289
#define list_make1(x1)
Definition: pg_list.h:227
Oid consttype
Definition: primnodes.h:196
CoercionForm funcformat
Definition: primnodes.h:460
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2331
Oid opresulttype
Definition: primnodes.h:504
ParamFetchHook paramFetch
Definition: params.h:112
ParamListInfo boundParams
Definition: clauses.c:66
#define linitial(l)
Definition: pg_list.h:195
Oid funcid
Definition: primnodes.h:455
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static bool rowtype_field_matches(Oid rowtypeid, int fieldnum, Oid expectedtype, int32 expectedtypmod, Oid expectedcollation)
Definition: clauses.c:2185
Oid paramcollid
Definition: primnodes.h:252
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
List * args
Definition: primnodes.h:1072
BoolExprType boolop
Definition: primnodes.h:568
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:355
Expr * arg
Definition: primnodes.h:1205
Oid constcollid
Definition: primnodes.h:198
Oid resultcollid
Definition: primnodes.h:753
#define lfirst_node(type, lc)
Definition: pg_list.h:193
struct Const Const
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:633
int location
Definition: primnodes.h:509
Expr * arg
Definition: primnodes.h:1228
#define DatumGetBool(X)
Definition: postgres.h:393
Oid winfnoid
Definition: primnodes.h:359
Expr * arg
Definition: primnodes.h:820
Expr * elemexpr
Definition: primnodes.h:845
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
Oid opcollid
Definition: primnodes.h:506
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
List * lappend(List *list, void *datum)
Definition: list.c:322
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
List * args
Definition: primnodes.h:919
BoolTestType booltesttype
Definition: primnodes.h:1229
uintptr_t Datum
Definition: postgres.h:367
CoercionForm convertformat
Definition: primnodes.h:872
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Oid resultcollid
Definition: primnodes.h:823
Oid opfuncid
Definition: primnodes.h:503
Oid resulttype
Definition: primnodes.h:801
NullTestType nulltesttype
Definition: primnodes.h:1206
#define BoolGetDatum(X)
Definition: postgres.h:402
Oid resultcollid
Definition: primnodes.h:803
#define InvalidOid
Definition: postgres_ext.h:36
int32 paramtypmod
Definition: primnodes.h:251
#define makeNode(_type_)
Definition: nodes.h:573
static Node * simplify_boolean_equality(Oid opno, List *args)
Definition: clauses.c:3872
int location
Definition: primnodes.h:825
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int location
Definition: primnodes.h:368
Oid inputcollid
Definition: primnodes.h:462
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
Expr * aggfilter
Definition: primnodes.h:364
int paramid
Definition: primnodes.h:249
uint16 pflags
Definition: params.h:94
int location
Definition: primnodes.h:1208
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static List * simplify_and_arguments(List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceFalse)
Definition: clauses.c:3778
Oid row_typeid
Definition: primnodes.h:1009
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Expr * arg
Definition: primnodes.h:886
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2029
int location
Definition: primnodes.h:921
Oid inputcollid
Definition: primnodes.h:507
static bool ece_function_is_safe(Oid funcid, eval_const_expressions_context *context)
Definition: clauses.c:3634
Oid inputcollid
Definition: primnodes.h:362
List * args
Definition: primnodes.h:569
#define InvalidAttrNumber
Definition: attnum.h:23
#define nodeTag(nodeptr)
Definition: nodes.h:530
int32 consttypmod
Definition: primnodes.h:197
Oid wintype
Definition: primnodes.h:360
CoercionForm coerceformat
Definition: primnodes.h:824
static List * simplify_or_arguments(List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceTrue)
Definition: clauses.c:3672
#define Int32GetDatum(X)
Definition: postgres.h:479
e
Definition: preproc-init.c:82
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1619
bool winagg
Definition: primnodes.h:367
#define elog(elevel,...)
Definition: elog.h:226
Oid coalescetype
Definition: primnodes.h:1070
void * arg
NodeTag type
Definition: primnodes.h:138
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:645
bool argisrow
Definition: primnodes.h:1207
int32 resulttypmod
Definition: primnodes.h:802
Expr * arg
Definition: primnodes.h:918
int location
Definition: primnodes.h:464
Oid opno
Definition: primnodes.h:502
Expr * result
Definition: primnodes.h:931
#define copyObject(obj)
Definition: nodes.h:641
List * args
Definition: primnodes.h:508
CoercionForm relabelformat
Definition: primnodes.h:804
Expr * defresult
Definition: primnodes.h:920
Expr * expr
Definition: primnodes.h:930
int location
Definition: primnodes.h:888
Definition: pg_list.h:50
bool isnull
Definition: params.h:93
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1630
Oid paramtype
Definition: primnodes.h:250
int location
Definition: primnodes.h:805
bool constisnull
Definition: primnodes.h:201
Oid coalescecollid
Definition: primnodes.h:1071
bool funcvariadic
Definition: primnodes.h:458
#define PARAM_FLAG_CONST
Definition: params.h:88
bool opretset
Definition: primnodes.h:505
int32 resulttypmod
Definition: primnodes.h:752
bool winstar
Definition: primnodes.h:366
void record_plan_type_dependency(PlannerInfo *root, Oid typid)
Definition: setrefs.c:2717
Definition: nodes.h:153
AttrNumber fieldnum
Definition: primnodes.h:749
#define ece_evaluate_expr(node)
Definition: clauses.c:2321

◆ evaluate_expr()

Expr* evaluate_expr ( Expr expr,
Oid  result_type,
int32  result_typmod,
Oid  result_collation 
)

Definition at line 4778 of file clauses.c.

References CreateExecutorState(), datumCopy(), EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fix_opfuncids(), FreeExecutorState(), get_typlenbyval(), GetPerTupleExprContext, makeConst(), MemoryContextSwitchTo(), PG_DETOAST_DATUM_COPY, and PointerGetDatum.

Referenced by eval_const_expressions_mutator(), evaluate_function(), and transformPartitionBoundValue().

4780 {
4781  EState *estate;
4782  ExprState *exprstate;
4783  MemoryContext oldcontext;
4784  Datum const_val;
4785  bool const_is_null;
4786  int16 resultTypLen;
4787  bool resultTypByVal;
4788 
4789  /*
4790  * To use the executor, we need an EState.
4791  */
4792  estate = CreateExecutorState();
4793 
4794  /* We can use the estate's working context to avoid memory leaks. */
4795  oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4796 
4797  /* Make sure any opfuncids are filled in. */
4798  fix_opfuncids((Node *) expr);
4799 
4800  /*
4801  * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4802  * because it'd result in recursively invoking eval_const_expressions.)
4803  */
4804  exprstate = ExecInitExpr(expr, NULL);
4805 
4806  /*
4807  * And evaluate it.
4808  *
4809  * It is OK to use a default econtext because none of the ExecEvalExpr()
4810  * code used in this situation will use econtext. That might seem
4811  * fortuitous, but it's not so unreasonable --- a constant expression does
4812  * not depend on context, by definition, n'est ce pas?
4813  */
4814  const_val = ExecEvalExprSwitchContext(exprstate,
4815  GetPerTupleExprContext(estate),
4816  &const_is_null);
4817 
4818  /* Get info needed about result datatype */
4819  get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4820 
4821  /* Get back to outer memory context */
4822  MemoryContextSwitchTo(oldcontext);
4823 
4824  /*
4825  * Must copy result out of sub-context used by expression eval.
4826  *
4827  * Also, if it's varlena, forcibly detoast it. This protects us against
4828  * storing TOAST pointers into plans that might outlive the referenced
4829  * data. (makeConst would handle detoasting anyway, but it's worth a few
4830  * extra lines here so that we can do the copy and detoast in one step.)
4831  */
4832  if (!const_is_null)
4833  {
4834  if (resultTypLen == -1)
4835  const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4836  else
4837  const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4838  }
4839 
4840  /* Release all the junk we just created */
4841  FreeExecutorState(estate);
4842 
4843  /*
4844  * Make the constant result node.
4845  */
4846  return (Expr *) makeConst(result_type, result_typmod, result_collation,
4847  resultTypLen,
4848  const_val, const_is_null,
4849  resultTypByVal);
4850 }
signed short int16
Definition: c.h:345
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:300
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:237
#define PointerGetDatum(X)
Definition: postgres.h:556
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1588
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:525
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:297
void FreeExecutorState(EState *estate)
Definition: execUtils.c:190
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
MemoryContext es_query_cxt
Definition: execnodes.h:550
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
EState * CreateExecutorState(void)
Definition: execUtils.c:88
uintptr_t Datum
Definition: postgres.h:367
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2029
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:121

◆ evaluate_function()

static Expr * evaluate_function ( Oid  funcid,
Oid  result_type,
int32  result_typmod,
Oid  result_collid,
Oid  input_collid,
List args,
bool  funcvariadic,
HeapTuple  func_tuple,
eval_const_expressions_context context 
)
static

Definition at line 4265 of file clauses.c.

References arg, generate_unaccent_rules::args, FuncExpr::args, COERCE_EXPLICIT_CALL, eval_const_expressions_context::estimate, evaluate_expr(), FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, GETSTRUCT, FuncExpr::inputcollid, IsA, lfirst, FuncExpr::location, makeNode, and makeNullConst().

Referenced by simplify_function().

4270 {
4271  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4272  bool has_nonconst_input = false;
4273  bool has_null_input = false;
4274  ListCell *arg;
4275  FuncExpr *newexpr;
4276 
4277  /*
4278  * Can't simplify if it returns a set.
4279  */
4280  if (funcform->proretset)
4281  return NULL;
4282 
4283  /*
4284  * Can't simplify if it returns RECORD. The immediate problem is that it
4285  * will be needing an expected tupdesc which we can't supply here.
4286  *
4287  * In the case where it has OUT parameters, it could get by without an
4288  * expected tupdesc, but we still have issues: get_expr_result_type()
4289  * doesn't know how to extract type info from a RECORD constant, and in
4290  * the case of a NULL function result there doesn't seem to be any clean
4291  * way to fix that. In view of the likelihood of there being still other
4292  * gotchas, seems best to leave the function call unreduced.
4293  */
4294  if (funcform->prorettype == RECORDOID)
4295  return NULL;
4296 
4297  /*
4298  * Check for constant inputs and especially constant-NULL inputs.
4299  */
4300  foreach(arg, args)
4301  {
4302  if (IsA(lfirst(arg), Const))
4303  has_null_input |= ((Const *) lfirst(arg))->constisnull;
4304  else
4305  has_nonconst_input = true;
4306  }
4307 
4308  /*
4309  * If the function is strict and has a constant-NULL input, it will never
4310  * be called at all, so we can replace the call by a NULL constant, even
4311  * if there are other inputs that aren't constant, and even if the
4312  * function is not otherwise immutable.
4313  */
4314  if (funcform->proisstrict && has_null_input)
4315  return (Expr *) makeNullConst(result_type, result_typmod,
4316  result_collid);
4317 
4318  /*
4319  * Otherwise, can simplify only if all inputs are constants. (For a
4320  * non-strict function, constant NULL inputs are treated the same as
4321  * constant non-NULL inputs.)
4322  */
4323  if (has_nonconst_input)
4324  return NULL;
4325 
4326  /*
4327  * Ordinarily we are only allowed to simplify immutable functions. But for
4328  * purposes of estimation, we consider it okay to simplify functions that
4329  * are merely stable; the risk that the result might change from planning
4330  * time to execution time is worth taking in preference to not being able
4331  * to estimate the value at all.
4332  */
4333  if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
4334  /* okay */ ;
4335  else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
4336  /* okay */ ;
4337  else
4338  return NULL;
4339 
4340  /*
4341  * OK, looks like we can simplify this operator/function.
4342  *
4343  * Build a new FuncExpr node containing the already-simplified arguments.
4344  */
4345  newexpr = makeNode(FuncExpr);
4346  newexpr->funcid = funcid;
4347  newexpr->funcresulttype = result_type;
4348  newexpr->funcretset = false;
4349  newexpr->funcvariadic = funcvariadic;
4350  newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4351  newexpr->funccollid = result_collid; /* doesn't matter */
4352  newexpr->inputcollid = input_collid;
4353  newexpr->args = args;
4354  newexpr->location = -1;
4355 
4356  return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
4357  result_collid);
4358 }
Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
Definition: clauses.c:4778
Oid funcresulttype
Definition: primnodes.h:456
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
List * args
Definition: primnodes.h:463
Oid funccollid
Definition: primnodes.h:461
bool funcretset
Definition: primnodes.h:457
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
CoercionForm funcformat
Definition: primnodes.h:460
Oid funcid
Definition: primnodes.h:455
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define makeNode(_type_)
Definition: nodes.h:573
Oid inputcollid
Definition: primnodes.h:462
#define lfirst(lc)
Definition: pg_list.h:190
void * arg
int location
Definition: primnodes.h:464
bool funcvariadic
Definition: primnodes.h:458

◆ expand_function_arguments()

List* expand_function_arguments ( List args,
Oid  result_type,
HeapTuple  func_tuple 
)

Definition at line 4048 of file clauses.c.

References add_function_defaults(), arg, generate_unaccent_rules::args, GETSTRUCT, IsA, lfirst, list_length(), recheck_cast_function_args(), and reorder_function_arguments().

Referenced by eval_const_expressions_mutator(), exec_stmt_call(), ExecuteCallStmt(), and simplify_function().

4049 {
4050  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4051  bool has_named_args = false;
4052  ListCell *lc;
4053 
4054  /* Do we have any named arguments? */
4055  foreach(lc, args)
4056  {
4057  Node *arg = (Node *) lfirst(lc);
4058 
4059  if (IsA(arg, NamedArgExpr))
4060  {
4061  has_named_args = true;
4062  break;
4063  }
4064  }
4065 
4066  /* If so, we must apply reorder_function_arguments */
4067  if (has_named_args)
4068  {
4069  args = reorder_function_arguments(args, func_tuple);
4070  /* Recheck argument types and add casts if needed */
4071  recheck_cast_function_args(args, result_type, func_tuple);
4072  }
4073  else if (list_length(args) < funcform->pronargs)
4074  {
4075  /* No named args, but we seem to be short some defaults */
4076  args = add_function_defaults(args, func_tuple);
4077  /* Recheck argument types and add casts if needed */
4078  recheck_cast_function_args(args, result_type, func_tuple);
4079  }
4080 
4081  return args;
4082 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Definition: nodes.h:525
static void recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
Definition: clauses.c:4221
static List * add_function_defaults(List *args, HeapTuple func_tuple)
Definition: clauses.c:4161
static List * reorder_function_arguments(List *args, HeapTuple func_tuple)
Definition: clauses.c:4091
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
void * arg

◆ expression_returns_set_rows()

double expression_returns_set_rows ( PlannerInfo root,
Node clause 
)

Definition at line 569 of file clauses.c.

References clamp_row_est(), FuncExpr::funcid, FuncExpr::funcretset, get_function_rows(), IsA, OpExpr::opfuncid, OpExpr::opretset, and set_opfuncid().

Referenced by create_set_projection_path(), estimate_num_groups(), and set_function_size_estimates().

570 {
571  if (clause == NULL)
572  return 1.0;
573  if (IsA(clause, FuncExpr))
574  {
575  FuncExpr *expr = (FuncExpr *) clause;
576 
577  if (expr->funcretset)
578  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
579  }
580  if (IsA(clause, OpExpr))
581  {
582  OpExpr *expr = (OpExpr *) clause;
583 
584  if (expr->opretset)
585  {
586  set_opfuncid(expr);
587  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
588  }
589  }
590  return 1.0;
591 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool funcretset
Definition: primnodes.h:457
Oid funcid
Definition: primnodes.h:455
Oid opfuncid
Definition: primnodes.h:503
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1619
double clamp_row_est(double nrows)
Definition: costsize.c:187
bool opretset
Definition: primnodes.h:505
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:1967

◆ fetch_function_defaults()

static List * fetch_function_defaults ( HeapTuple  func_tuple)
static

Definition at line 4186 of file clauses.c.

References castNode, elog, ERROR, pfree(), PROCOID, generate_unaccent_rules::str, stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by add_function_defaults(), and reorder_function_arguments().

4187 {
4188  List *defaults;
4189  Datum proargdefaults;
4190  bool isnull;
4191  char *str;
4192 
4193  /* The error cases here shouldn't happen, but check anyway */
4194  proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
4195  Anum_pg_proc_proargdefaults,
4196  &isnull);
4197  if (isnull)
4198  elog(ERROR, "not enough default arguments");
4199  str = TextDatumGetCString(proargdefaults);
4200  defaults = castNode(List, stringToNode(str));
4201  pfree(str);
4202  return defaults;
4203 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
void * stringToNode(const char *str)
Definition: read.c:89
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
#define elog(elevel,...)
Definition: elog.h:226
Definition: pg_list.h:50

◆ find_forced_null_var()

Var* find_forced_null_var ( Node node)

Definition at line 1978 of file clauses.c.

References NullTest::arg, BooleanTest::arg, NullTest::argisrow, BooleanTest::booltesttype, IS_NULL, IS_UNKNOWN, IsA, NullTest::nulltesttype, and Var::varlevelsup.

Referenced by check_redundant_nullability_qual(), and find_forced_null_vars().

1979 {
1980  if (node == NULL)
1981  return NULL;
1982  if (IsA(node, NullTest))
1983  {
1984  /* check for var IS NULL */
1985  NullTest *expr = (NullTest *) node;
1986 
1987  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1988  {
1989  Var *var = (Var *) expr->arg;
1990 
1991  if (var && IsA(var, Var) &&
1992  var->varlevelsup == 0)
1993  return var;
1994  }
1995  }
1996  else if (IsA(node, BooleanTest))
1997  {
1998  /* var IS UNKNOWN is equivalent to var IS NULL */
1999  BooleanTest *expr = (BooleanTest *) node;
2000 
2001  if (expr->booltesttype == IS_UNKNOWN)
2002  {
2003  Var *var = (Var *) expr->arg;
2004 
2005  if (var && IsA(var, Var) &&
2006  var->varlevelsup == 0)
2007  return var;
2008  }
2009  }
2010  return NULL;
2011 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:177
Definition: primnodes.h:167
Expr * arg
Definition: primnodes.h:1205
Expr * arg
Definition: primnodes.h:1228
BoolTestType booltesttype
Definition: primnodes.h:1229
NullTestType nulltesttype
Definition: primnodes.h:1206
bool argisrow
Definition: primnodes.h:1207

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node node)

Definition at line 1919 of file clauses.c.

References AND_EXPR, BoolExpr::args, BoolExpr::boolop, find_forced_null_var(), find_forced_null_vars(), IsA, lfirst, list_concat(), list_make1, and NIL.

Referenced by find_forced_null_vars(), and reduce_outer_joins_pass2().

1920 {
1921  List *result = NIL;
1922  Var *var;
1923  ListCell *l;
1924 
1925  if (node == NULL)
1926  return NIL;
1927  /* Check single-clause cases using subroutine */
1928  var = find_forced_null_var(node);
1929  if (var)
1930  {
1931  result = list_make1(var);
1932  }
1933  /* Otherwise, handle AND-conditions */
1934  else if (IsA(node, List))
1935  {
1936  /*
1937  * At top level, we are examining an implicit-AND list: if any of the
1938  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1939  */
1940  foreach(l, (List *) node)
1941  {
1942  result = list_concat(result,
1944  }
1945  }
1946  else if (IsA(node, BoolExpr))
1947  {
1948  BoolExpr *expr = (BoolExpr *) node;
1949 
1950  /*
1951  * We don't bother considering the OR case, because it's fairly
1952  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1953  * the NOT case isn't worth expending code on.
1954  */
1955  if (expr->boolop == AND_EXPR)
1956  {
1957  /* At top level we can just recurse (to the List case) */
1958  result = find_forced_null_vars((Node *) expr->args);
1959  }
1960  }
1961  return result;
1962 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Definition: primnodes.h:167
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1919
#define list_make1(x1)
Definition: pg_list.h:227
BoolExprType boolop
Definition: primnodes.h:568
#define lfirst(lc)
Definition: pg_list.h:190
List * args
Definition: primnodes.h:569
Definition: pg_list.h:50
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1978

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1501 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1502 {
1503  return find_nonnullable_rels_walker(clause, true);
1504 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1507

◆ find_nonnullable_rels_walker()

static Relids find_nonnullable_rels_walker ( Node node,
bool  top_level 
)
static

Definition at line 1507 of file clauses.c.

References AND_EXPR, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, NullTest::arg, BooleanTest::arg, NullTest::argisrow, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, bms_add_members(), bms_int_members(), bms_is_empty(), bms_join(), bms_make_singleton(), bms_membership(), BMS_SINGLETON, BoolExpr::boolop, BooleanTest::booltesttype, elog, ERROR, func_strict(), FuncExpr::funcid, IS_FALSE, IS_NOT_NULL, IS_NOT_UNKNOWN, is_strict_saop(), IS_TRUE, IsA, lfirst, NOT_EXPR, NullTest::nulltesttype, OpExpr::opfuncid, OR_EXPR, PlaceHolderVar::phexpr, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, set_opfuncid(), Var::varlevelsup, and Var::varno.

Referenced by find_nonnullable_rels().

1508 {
1509  Relids result = NULL;
1510  ListCell *l;
1511 
1512  if (node == NULL)
1513  return NULL;
1514  if (IsA(node, Var))
1515  {
1516  Var *var = (Var *) node;
1517 
1518  if (var->varlevelsup == 0)
1519  result = bms_make_singleton(var->varno);
1520  }
1521  else if (IsA(node, List))
1522  {
1523  /*
1524  * At top level, we are examining an implicit-AND list: if any of the
1525  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1526  * not at top level, we are examining the arguments of a strict
1527  * function: if any of them produce NULL then the result of the
1528  * function must be NULL. So in both cases, the set of nonnullable
1529  * rels is the union of those found in the arms, and we pass down the
1530  * top_level flag unmodified.
1531  */
1532  foreach(l, (List *) node)
1533  {
1534  result = bms_join(result,
1536  top_level));
1537  }
1538  }
1539  else if (IsA(node, FuncExpr))
1540  {
1541  FuncExpr *expr = (FuncExpr *) node;
1542 
1543  if (func_strict(expr->funcid))
1544  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1545  }
1546  else if (IsA(node, OpExpr))
1547  {
1548  OpExpr *expr = (OpExpr *) node;
1549 
1550  set_opfuncid(expr);
1551  if (func_strict(expr->opfuncid))
1552  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1553  }
1554  else if (IsA(node, ScalarArrayOpExpr))
1555  {
1556  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1557 
1558  if (is_strict_saop(expr, true))
1559  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1560  }
1561  else if (IsA(node, BoolExpr))
1562  {
1563  BoolExpr *expr = (BoolExpr *) node;
1564 
1565  switch (expr->boolop)
1566  {
1567  case AND_EXPR:
1568  /* At top level we can just recurse (to the List case) */
1569  if (top_level)
1570  {
1571  result = find_nonnullable_rels_walker((Node *) expr->args,
1572  top_level);
1573  break;
1574  }
1575 
1576  /*
1577  * Below top level, even if one arm produces NULL, the result
1578  * could be FALSE (hence not NULL). However, if *all* the
1579  * arms produce NULL then the result is NULL, so we can take
1580  * the intersection of the sets of nonnullable rels, just as
1581  * for OR. Fall through to share code.
1582  */
1583  /* FALL THRU */
1584  case OR_EXPR:
1585 
1586  /*
1587  * OR is strict if all of its arms are, so we can take the
1588  * intersection of the sets of nonnullable rels for each arm.
1589  * This works for both values of top_level.
1590  */
1591  foreach(l, expr->args)
1592  {
1593  Relids subresult;
1594 
1595  subresult = find_nonnullable_rels_walker(lfirst(l),
1596  top_level);
1597  if (result == NULL) /* first subresult? */
1598  result = subresult;
1599  else
1600  result = bms_int_members(result, subresult);
1601 
1602  /*
1603  * If the intersection is empty, we can stop looking. This
1604  * also justifies the test for first-subresult above.
1605  */
1606  if (bms_is_empty(result))
1607  break;
1608  }
1609  break;
1610  case NOT_EXPR:
1611  /* NOT will return null if its arg is null */
1612  result = find_nonnullable_rels_walker((Node *) expr->args,
1613  false);
1614  break;
1615  default:
1616  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1617  break;
1618  }
1619  }
1620  else if (IsA(node, RelabelType))
1621  {
1622  RelabelType *expr = (RelabelType *) node;
1623 
1624  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1625  }
1626  else if (IsA(node, CoerceViaIO))
1627  {
1628  /* not clear this is useful, but it can't hurt */
1629  CoerceViaIO *expr = (CoerceViaIO *) node;
1630 
1631  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1632  }
1633  else if (IsA(node, ArrayCoerceExpr))
1634  {
1635  /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
1636  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1637 
1638  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1639  }
1640  else if (IsA(node, ConvertRowtypeExpr))
1641  {
1642  /* not clear this is useful, but it can't hurt */
1643  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1644 
1645  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1646  }
1647  else if (IsA(node, CollateExpr))
1648  {
1649  CollateExpr *expr = (CollateExpr *) node;
1650 
1651  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1652  }
1653  else if (IsA(node, NullTest))
1654  {
1655  /* IS NOT NULL can be considered strict, but only at top level */
1656  NullTest *expr = (NullTest *) node;
1657 
1658  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1659  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1660  }
1661  else if (IsA(node, BooleanTest))
1662  {
1663  /* Boolean tests that reject NULL are strict at top level */
1664  BooleanTest *expr = (BooleanTest *) node;
1665 
1666  if (top_level &&
1667  (expr->booltesttype == IS_TRUE ||
1668  expr->booltesttype == IS_FALSE ||
1669  expr->booltesttype == IS_NOT_UNKNOWN))
1670  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1671  }
1672  else if (IsA(node, PlaceHolderVar))
1673  {
1674  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1675 
1676  /*
1677  * If the contained expression forces any rels non-nullable, so does
1678  * the PHV.
1679  */
1680  result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1681 
1682  /*
1683  * If the PHV's syntactic scope is exactly one rel, it will be forced
1684  * to be evaluated at that rel, and so it will behave like a Var of
1685  * that rel: if the rel's entire output goes to null, so will the PHV.
1686  * (If the syntactic scope is a join, we know that the PHV will go to
1687  * null if the whole join does; but that is AND semantics while we
1688  * need OR semantics for find_nonnullable_rels' result, so we can't do
1689  * anything with the knowledge.)
1690  */
1691  if (phv->phlevelsup == 0 &&
1693  result = bms_add_members(result, phv->phrels);
1694  }
1695  return result;
1696 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:177
List * args
Definition: primnodes.h:463
Expr * arg
Definition: primnodes.h:800
Definition: nodes.h:525
Definition: primnodes.h:167
Oid funcid
Definition: primnodes.h:455
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
BoolExprType boolop
Definition: primnodes.h:568
Expr * arg
Definition: primnodes.h:1205
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
Expr * arg
Definition: primnodes.h:1228
Expr * arg
Definition: primnodes.h:820
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Index varno
Definition: primnodes.h:170
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
BoolTestType booltesttype
Definition: primnodes.h:1229
Oid opfuncid
Definition: primnodes.h:503
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1507
NullTestType nulltesttype
Definition: primnodes.h:1206
static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
Definition: clauses.c:2027
#define lfirst(lc)
Definition: pg_list.h:190
Expr * arg
Definition: primnodes.h:886
List * args
Definition: primnodes.h:569
bool func_strict(Oid funcid)
Definition: lsyscache.c:1563
Index phlevelsup
Definition: pathnodes.h:2065
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1619
#define elog(elevel,...)
Definition: elog.h:226
bool argisrow
Definition: primnodes.h:1207
List * args
Definition: primnodes.h:508
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
Definition: pg_list.h:50
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1726 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1727 {
1728  return find_nonnullable_vars_walker(clause, true);
1729 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1732

◆ find_nonnullable_vars_walker()

static List * find_nonnullable_vars_walker ( Node node,
bool  top_level 
)
static

Definition at line 1732 of file clauses.c.

References AND_EXPR, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, NullTest::arg, BooleanTest::arg, NullTest::argisrow, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, BoolExpr::boolop, BooleanTest::booltesttype, elog, ERROR, func_strict(), FuncExpr::funcid, IS_FALSE, IS_NOT_NULL, IS_NOT_UNKNOWN, is_strict_saop(), IS_TRUE, IsA, lfirst, list_concat(), list_intersection(), list_make1, NIL, NOT_EXPR, NullTest::nulltesttype, OpExpr::opfuncid, OR_EXPR, PlaceHolderVar::phexpr, set_opfuncid(), and Var::varlevelsup.

Referenced by find_nonnullable_vars().

1733 {
1734  List *result = NIL;
1735  ListCell *l;
1736 
1737  if (node == NULL)
1738  return NIL;
1739  if (IsA(node, Var))
1740  {
1741  Var *var = (Var *) node;
1742 
1743  if (var->varlevelsup == 0)
1744  result = list_make1(var);
1745  }
1746  else if (IsA(node, List))
1747  {
1748  /*
1749  * At top level, we are examining an implicit-AND list: if any of the
1750  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1751  * not at top level, we are examining the arguments of a strict
1752  * function: if any of them produce NULL then the result of the
1753  * function must be NULL. So in both cases, the set of nonnullable
1754  * vars is the union of those found in the arms, and we pass down the
1755  * top_level flag unmodified.
1756  */
1757  foreach(l, (List *) node)
1758  {
1759  result = list_concat(result,
1761  top_level));
1762  }
1763  }
1764  else if (IsA(node, FuncExpr))
1765  {
1766  FuncExpr *expr = (FuncExpr *) node;
1767 
1768  if (func_strict(expr->funcid))
1769  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1770  }
1771  else if (IsA(node, OpExpr))
1772  {
1773  OpExpr *expr = (OpExpr *) node;
1774 
1775  set_opfuncid(expr);
1776  if (func_strict(expr->opfuncid))
1777  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1778  }
1779  else if (IsA(node, ScalarArrayOpExpr))
1780  {
1781  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1782 
1783  if (is_strict_saop(expr, true))
1784  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1785  }
1786  else if (IsA(node, BoolExpr))
1787  {
1788  BoolExpr *expr = (BoolExpr *) node;
1789 
1790  switch (expr->boolop)
1791  {
1792  case AND_EXPR:
1793  /* At top level we can just recurse (to the List case) */
1794  if (top_level)
1795  {
1796  result = find_nonnullable_vars_walker((Node *) expr->args,
1797  top_level);
1798  break;
1799  }
1800 
1801  /*
1802  * Below top level, even if one arm produces NULL, the result
1803  * could be FALSE (hence not NULL). However, if *all* the
1804  * arms produce NULL then the result is NULL, so we can take
1805  * the intersection of the sets of nonnullable vars, just as
1806  * for OR. Fall through to share code.
1807  */
1808  /* FALL THRU */
1809  case OR_EXPR:
1810 
1811  /*
1812  * OR is strict if all of its arms are, so we can take the
1813  * intersection of the sets of nonnullable vars for each arm.
1814  * This works for both values of top_level.
1815  */
1816  foreach(l, expr->args)
1817  {
1818  List *subresult;
1819 
1820  subresult = find_nonnullable_vars_walker(lfirst(l),
1821  top_level);
1822  if (result == NIL) /* first subresult? */
1823  result = subresult;
1824  else
1825  result = list_intersection(result, subresult);
1826 
1827  /*
1828  * If the intersection is empty, we can stop looking. This
1829  * also justifies the test for first-subresult above.
1830  */
1831  if (result == NIL)
1832  break;
1833  }
1834  break;
1835  case NOT_EXPR:
1836  /* NOT will return null if its arg is null */
1837  result = find_nonnullable_vars_walker((Node *) expr->args,
1838  false);
1839  break;
1840  default:
1841  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1842  break;
1843  }
1844  }
1845  else if (IsA(node, RelabelType))
1846  {
1847  RelabelType *expr = (RelabelType *) node;
1848 
1849  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1850  }
1851  else if (IsA(node, CoerceViaIO))
1852  {
1853  /* not clear this is useful, but it can't hurt */
1854  CoerceViaIO *expr = (CoerceViaIO *) node;
1855 
1856  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1857  }
1858  else if (IsA(node, ArrayCoerceExpr))
1859  {
1860  /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
1861  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1862 
1863  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1864  }
1865  else if (IsA(node, ConvertRowtypeExpr))
1866  {
1867  /* not clear this is useful, but it can't hurt */
1868  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1869 
1870  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1871  }
1872  else if (IsA(node, CollateExpr))
1873  {
1874  CollateExpr *expr = (CollateExpr *) node;
1875 
1876  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1877  }
1878  else if (IsA(node, NullTest))
1879  {
1880  /* IS NOT NULL can be considered strict, but only at top level */
1881  NullTest *expr = (NullTest *) node;
1882 
1883  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1884  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1885  }
1886  else if (IsA(node, BooleanTest))
1887  {
1888  /* Boolean tests that reject NULL are strict at top level */
1889  BooleanTest *expr = (BooleanTest *) node;
1890 
1891  if (top_level &&
1892  (expr->booltesttype == IS_TRUE ||
1893  expr->booltesttype == IS_FALSE ||
1894  expr->booltesttype == IS_NOT_UNKNOWN))
1895  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1896  }
1897  else if (IsA(node, PlaceHolderVar))
1898  {
1899  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1900 
1901  result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
1902  }
1903  return result;
1904 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:177
List * args
Definition: primnodes.h:463
Expr * arg
Definition: primnodes.h:800
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Definition: primnodes.h:167
#define list_make1(x1)
Definition: pg_list.h:227
Oid funcid
Definition: primnodes.h:455
#define ERROR
Definition: elog.h:43
BoolExprType boolop
Definition: primnodes.h:568
Expr * arg
Definition: primnodes.h:1205
Expr * arg
Definition: primnodes.h:1228
List * list_intersection(const List *list1, const List *list2)
Definition: list.c:1019
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1732
Expr * arg
Definition: primnodes.h:820
BoolTestType booltesttype
Definition: primnodes.h:1229
Oid opfuncid
Definition: primnodes.h:503
NullTestType nulltesttype
Definition: primnodes.h:1206
static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
Definition: clauses.c:2027
#define lfirst(lc)
Definition: pg_list.h:190
Expr * arg
Definition: primnodes.h:886
List * args
Definition: primnodes.h:569
bool func_strict(Oid funcid)
Definition: lsyscache.c:1563
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1619
#define elog(elevel,...)
Definition: elog.h:226
bool argisrow
Definition: primnodes.h:1207
List * args
Definition: primnodes.h:508
Definition: pg_list.h:50

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 507 of file clauses.c.

References find_window_functions_walker(), WindowFuncLists::maxWinRef, WindowFuncLists::numWindowFuncs, palloc(), palloc0(), and WindowFuncLists::windowFuncs.

Referenced by grouping_planner().

508 {
509  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
510 
511  lists->numWindowFuncs = 0;
512  lists->maxWinRef = maxWinRef;
513  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
514  (void) find_window_functions_walker(clause, lists);
515  return lists;
516 }
Index maxWinRef
Definition: clauses.h:23
int numWindowFuncs
Definition: clauses.h:22
void * palloc0(Size size)
Definition: mcxt.c:980
void * palloc(Size size)
Definition: mcxt.c:949
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:519
Definition: pg_list.h:50
List ** windowFuncs
Definition: clauses.h:24

◆ find_window_functions_walker()

static bool find_window_functions_walker ( Node node,
WindowFuncLists lists 
)
static

Definition at line 519 of file clauses.c.

References Assert, elog, ERROR, expression_tree_walker(), IsA, lappend(), list_member(), WindowFuncLists::maxWinRef, WindowFuncLists::numWindowFuncs, WindowFuncLists::windowFuncs, and WindowFunc::winref.

Referenced by find_window_functions().

520 {
521  if (node == NULL)
522  return false;
523  if (IsA(node, WindowFunc))
524  {
525  WindowFunc *wfunc = (WindowFunc *) node;
526 
527  /* winref is unsigned, so one-sided test is OK */
528  if (wfunc->winref > lists->maxWinRef)
529  elog(ERROR, "WindowFunc contains out-of-range winref %u",
530  wfunc->winref);
531  /* eliminate duplicates, so that we avoid repeated computation */
532  if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
533  {
534  lists->windowFuncs[wfunc->winref] =
535  lappend(lists->windowFuncs[wfunc->winref], wfunc);
536  lists->numWindowFuncs++;
537  }
538 
539  /*
540  * We assume that the parser checked that there are no window
541  * functions in the arguments or filter clause. Hence, we need not
542  * recurse into them. (If either the parser or the planner screws up
543  * on this point, the executor will still catch it; see ExecInitExpr.)
544  */
545  return false;
546  }
547  Assert(!IsA(node, SubLink));
549  (void *) lists);
550 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index maxWinRef
Definition: clauses.h:23
int numWindowFuncs
Definition: clauses.h:22
Index winref
Definition: primnodes.h:365
#define ERROR
Definition: elog.h:43
bool list_member(const List *list, const void *datum)
Definition: list.c:614
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:732
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
#define elog(elevel,...)
Definition: elog.h:226
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:519
List ** windowFuncs
Definition: clauses.h:24

◆ get_agg_clause_costs()

void get_agg_clause_costs ( PlannerInfo root,
Node clause,
AggSplit  aggsplit,
AggClauseCosts costs 
)

Definition at line 229 of file clauses.c.

References get_agg_clause_costs_context::aggsplit, get_agg_clause_costs_context::costs, get_agg_clause_costs_walker(), and get_agg_clause_costs_context::root.

Referenced by create_partial_grouping_paths(), estimate_path_cost_size(), and grouping_planner().

231 {
233 
234  context.root = root;
235  context.aggsplit = aggsplit;
236  context.costs = costs;
237  (void) get_agg_clause_costs_walker(clause, &context);
238 }
AggClauseCosts * costs
Definition: clauses.c:61
static bool get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
Definition: clauses.c:241

◆ get_agg_clause_costs_walker()

static bool get_agg_clause_costs_walker ( Node node,
get_agg_clause_costs_context context 
)
static

Definition at line 241 of file clauses.c.

References add_function_cost(), Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, AGGFNOID, Aggref::aggfnoid, Aggref::agglevelsup, Aggref::aggorder, get_agg_clause_costs_context::aggsplit, Aggref::aggtranstype, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_SMALL_INITSIZE, Aggref::args, Assert, cost_qual_eval_node(), get_agg_clause_costs_context::costs, DO_AGGSPLIT_COMBINE, DO_AGGSPLIT_DESERIALIZE, DO_AGGSPLIT_SERIALIZE, DO_AGGSPLIT_SKIPFINAL, elog, ERROR, TargetEntry::expr, expression_tree_walker(), exprType(), exprTypmod(), AggClauseCosts::finalCost, FUNC_MAX_ARGS, get_aggregate_argtypes(), get_typavgwidth(), get_typbyval(), GETSTRUCT, AggClauseCosts::hasNonPartial, AggClauseCosts::hasNonSerial, HeapTupleIsValid, IsA, linitial, MAXALIGN, NIL, AggClauseCosts::numAggs, AggClauseCosts::numOrderedAggs, ObjectIdGetDatum, OidIsValid, QualCost::per_tuple, ReleaseSysCache(), resolve_aggregate_transtype(), get_agg_clause_costs_context::root, SearchSysCache1(), QualCost::startup, AggClauseCosts::transCost, and AggClauseCosts::transitionSpace.

Referenced by get_agg_clause_costs().

242 {
243  if (node == NULL)
244  return false;
245  if (IsA(node, Aggref))
246  {
247  Aggref *aggref = (Aggref *) node;
248  AggClauseCosts *costs = context->costs;
249  HeapTuple aggTuple;
250  Form_pg_aggregate aggform;
251  Oid aggtransfn;
252  Oid aggfinalfn;
253  Oid aggcombinefn;
254  Oid aggserialfn;
255  Oid aggdeserialfn;
256  Oid aggtranstype;
257  int32 aggtransspace;
258  QualCost argcosts;
259 
260  Assert(aggref->agglevelsup == 0);
261 
262  /*
263  * Fetch info about aggregate from pg_aggregate. Note it's correct to
264  * ignore the moving-aggregate variant, since what we're concerned
265  * with here is aggregates not window functions.
266  */
267  aggTuple = SearchSysCache1(AGGFNOID,
268  ObjectIdGetDatum(aggref->aggfnoid));
269  if (!HeapTupleIsValid(aggTuple))
270  elog(ERROR, "cache lookup failed for aggregate %u",
271  aggref->aggfnoid);
272  aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
273  aggtransfn = aggform->aggtransfn;
274  aggfinalfn = aggform->aggfinalfn;
275  aggcombinefn = aggform->aggcombinefn;
276  aggserialfn = aggform->aggserialfn;
277  aggdeserialfn = aggform->aggdeserialfn;
278  aggtranstype = aggform->aggtranstype;
279  aggtransspace = aggform->aggtransspace;
280  ReleaseSysCache(aggTuple);
281 
282  /*
283  * Resolve the possibly-polymorphic aggregate transition type, unless
284  * already done in a previous pass over the expression.
285  */
286  if (OidIsValid(aggref->aggtranstype))
287  aggtranstype = aggref->aggtranstype;
288  else
289  {
290  Oid inputTypes[FUNC_MAX_ARGS];
291  int numArguments;
292 
293  /* extract argument types (ignoring any ORDER BY expressions) */
294  numArguments = get_aggregate_argtypes(aggref, inputTypes);
295 
296  /* resolve actual type of transition state, if polymorphic */
297  aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
298  aggtranstype,
299  inputTypes,
300  numArguments);
301  aggref->aggtranstype = aggtranstype;
302  }
303 
304  /*
305  * Count it, and check for cases requiring ordered input. Note that
306  * ordered-set aggs always have nonempty aggorder. Any ordered-input
307  * case also defeats partial aggregation.
308  */
309  costs->numAggs++;
310  if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
311  {
312  costs->numOrderedAggs++;
313  costs->hasNonPartial = true;
314  }
315 
316  /*
317  * Check whether partial aggregation is feasible, unless we already
318  * found out that we can't do it.
319  */
320  if (!costs->hasNonPartial)
321  {
322  /*
323  * If there is no combine function, then partial aggregation is
324  * not possible.
325  */
326  if (!OidIsValid(aggcombinefn))
327  costs->hasNonPartial = true;
328 
329  /*
330  * If we have any aggs with transtype INTERNAL then we must check
331  * whether they have serialization/deserialization functions; if
332  * not, we can't serialize partial-aggregation results.
333  */
334  else if (aggtranstype == INTERNALOID &&
335  (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
336  costs->hasNonSerial = true;
337  }
338 
339  /*
340  * Add the appropriate component function execution costs to
341  * appropriate totals.
342  */
343  if (DO_AGGSPLIT_COMBINE(context->aggsplit))
344  {
345  /* charge for combining previously aggregated states */
346  add_function_cost(context->root, aggcombinefn, NULL,
347  &costs->transCost);
348  }
349  else
350  add_function_cost(context->root, aggtransfn, NULL,
351  &costs->transCost);
352  if (DO_AGGSPLIT_DESERIALIZE(context->aggsplit) &&
353  OidIsValid(aggdeserialfn))
354  add_function_cost(context->root, aggdeserialfn, NULL,
355  &costs->transCost);
356  if (DO_AGGSPLIT_SERIALIZE(context->aggsplit) &&
357  OidIsValid(aggserialfn))
358  add_function_cost(context->root, aggserialfn, NULL,
359  &costs->finalCost);
360  if (!DO_AGGSPLIT_SKIPFINAL(context->aggsplit) &&
361  OidIsValid(aggfinalfn))
362  add_function_cost(context->root, aggfinalfn, NULL,
363  &costs->finalCost);
364 
365  /*
366  * These costs are incurred only by the initial aggregate node, so we
367  * mustn't include them again at upper levels.
368  */
369  if (!DO_AGGSPLIT_COMBINE(context->aggsplit))
370  {
371  /* add the input expressions' cost to per-input-row costs */
372  cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
373  costs->transCost.startup += argcosts.startup;
374  costs->transCost.per_tuple += argcosts.per_tuple;
375 
376  /*
377  * Add any filter's cost to per-input-row costs.
378  *
379  * XXX Ideally we should reduce input expression costs according
380  * to filter selectivity, but it's not clear it's worth the
381  * trouble.
382  */
383  if (aggref->aggfilter)
384  {
385  cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
386  context->root);
387  costs->transCost.startup += argcosts.startup;
388  costs->transCost.per_tuple += argcosts.per_tuple;
389  }
390  }
391 
392  /*
393  * If there are direct arguments, treat their evaluation cost like the
394  * cost of the finalfn.
395  */
396  if (aggref->aggdirectargs)
397  {
398  cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
399  context->root);
400  costs->finalCost.startup += argcosts.startup;
401  costs->finalCost.per_tuple += argcosts.per_tuple;
402  }
403 
404  /*
405  * If the transition type is pass-by-value then it doesn't add
406  * anything to the required size of the hashtable. If it is
407  * pass-by-reference then we have to add the estimated size of the
408  * value itself, plus palloc overhead.
409  */
410  if (!get_typbyval(aggtranstype))
411  {
412  int32 avgwidth;
413 
414  /* Use average width if aggregate definition gave one */
415  if (aggtransspace > 0)
416  avgwidth = aggtransspace;
417  else if (aggtransfn == F_ARRAY_APPEND)
418  {
419  /*
420  * If the transition function is array_append(), it'll use an
421  * expanded array as transvalue, which will occupy at least
422  * ALLOCSET_SMALL_INITSIZE and possibly more. Use that as the
423  * estimate for lack of a better idea.
424  */
425  avgwidth = ALLOCSET_SMALL_INITSIZE;
426  }
427  else
428  {
429  /*
430  * If transition state is of same type as first aggregated
431  * input, assume it's the same typmod (same width) as well.
432  * This works for cases like MAX/MIN and is probably somewhat
433  * reasonable otherwise.
434  */
435  int32 aggtranstypmod = -1;
436 
437  if (aggref->args)
438  {
439  TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
440 
441  if (aggtranstype == exprType((Node *) tle->expr))
442  aggtranstypmod = exprTypmod((Node *) tle->expr);
443  }
444 
445  avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
446  }
447 
448  avgwidth = MAXALIGN(avgwidth);
449  costs->transitionSpace += avgwidth + 2 * sizeof(void *);
450  }
451  else if (aggtranstype == INTERNALOID)
452  {
453  /*
454  * INTERNAL transition type is a special case: although INTERNAL
455  * is pass-by-value, it's almost certainly being used as a pointer
456  * to some large data structure. The aggregate definition can
457  * provide an estimate of the size. If it doesn't, then we assume
458  * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
459  * being kept in a private memory context, as is done by
460  * array_agg() for instance.
461  */
462  if (aggtransspace > 0)
463  costs->transitionSpace += aggtransspace;
464  else
466  }
467 
468  /*
469  * We assume that the parser checked that there are no aggregates (of
470  * this level anyway) in the aggregated arguments, direct arguments,
471  * or filter clause. Hence, we need not recurse into any of them.
472  */
473  return false;
474  }
475  Assert(!IsA(node, SubLink));
477  (void *) context);
478 }
List * aggdistinct
Definition: primnodes.h:307
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3845
#define NIL
Definition: pg_list.h:65
AggClauseCosts * costs
Definition: clauses.c:61
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
QualCost finalCost
Definition: pathnodes.h:63
Definition: nodes.h:525
List * args
Definition: primnodes.h:305
bool hasNonSerial
Definition: pathnodes.h:61
void add_function_cost(PlannerInfo *root, Oid funcid, Node *node, QualCost *cost)
Definition: plancat.c:1906
bool hasNonPartial
Definition: pathnodes.h:60
QualCost transCost
Definition: pathnodes.h:62
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:787
Cost startup
Definition: pathnodes.h:45
static bool get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
Definition: clauses.c:241
signed int int32
Definition: c.h:346
#define FUNC_MAX_ARGS
bool get_typbyval(Oid typid)
Definition: lsyscache.c:2000
Cost per_tuple
Definition: pathnodes.h:46
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:789
#define linitial(l)
Definition: pg_list.h:195
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
List * aggorder
Definition: primnodes.h:306
Index agglevelsup
Definition: primnodes.h:313
List * aggdirectargs
Definition: primnodes.h:304
int numOrderedAggs
Definition: pathnodes.h:59
#define ALLOCSET_SMALL_INITSIZE
Definition: memutils.h:200
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2356
Oid aggfnoid
Definition: primnodes.h:298
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
FormData_pg_aggregate * Form_pg_aggregate
Definition: pg_aggregate.h:109
Expr * expr
Definition: primnodes.h:1393
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1840
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:788
Expr * aggfilter
Definition: primnodes.h:308
#define MAXALIGN(LEN)
Definition: c.h:685
#define DO_AGGSPLIT_DESERIALIZE(as)
Definition: nodes.h:790
#define elog(elevel,...)
Definition: elog.h:226
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:190
Oid aggtranstype
Definition: primnodes.h:302
Size transitionSpace
Definition: pathnodes.h:64
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
Definition: parse_agg.c:1819
Oid resolve_aggregate_transtype(Oid aggfuncid, Oid aggtranstype, Oid *inputTypes, int numArguments)
Definition: parse_agg.c:1845

◆ inline_function()

static Expr * inline_function ( Oid  funcid,
Oid  result_type,
Oid  result_collid,
Oid  input_collid,
List args,
bool  funcvariadic,
HeapTuple  func_tuple,
eval_const_expressions_context context 
)
static

Definition at line 4392 of file clauses.c.

References ACL_EXECUTE, ACLCHECK_OK, eval_const_expressions_context::active_fns, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, arg, ErrorContextCallback::arg, CollateExpr::arg, generate_unaccent_rules::args, FuncExpr::args, Assert, ErrorContextCallback::callback, check_sql_fn_retval(), CMD_SELECT, COERCE_EXPLICIT_CALL, CollateExpr::collOid, Query::commandType, contain_context_dependent_node(), contain_mutable_functions(), contain_nonstrict_functions(), contain_subplans(), contain_volatile_functions(), copyObject, cost_qual_eval(), cpu_operator_cost, Query::cteList, CurrentMemoryContext, Query::distinctClause, elog, ERROR, error_context_stack, eval_const_expressions_mutator(), exprCollation(), exprType(), FmgrHookIsNeeded, free_parsestate(), FromExpr::fromlist, FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, GETSTRUCT, GetUserId(), Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasSubLinks, Query::hasTargetSRFs, Query::hasWindowFuncs, Query::havingQual, heap_attisnull(), i, FuncExpr::inputcollid, IsA, Query::jointree, lappend_oid(), lfirst, Query::limitCount, Query::limitOffset, linitial, list_delete_last(), list_length(), list_make1, list_member_oid(), FuncExpr::location, CollateExpr::location, make_parsestate(), makeNode, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, OidIsValid, ParseState::p_sourcetext, palloc0(), QualCost::per_tuple, pg_parse_query(), pg_proc_aclcheck(), prepare_sql_fn_parse_info(), ErrorContextCallback::previous, PROCOID, inline_error_callback_arg::proname, inline_error_callback_arg::prosrc, FromExpr::quals, querytree(), record_plan_function_dependency(), eval_const_expressions_context::root, Query::rtable, Query::setOperations, Query::sortClause, sql_fn_parser_setup(), sql_inline_error_callback(), QualCost::startup, substitute_actual_parameters(), SysCacheGetAttr(), Query::targetList, TextDatumGetCString, transformTopLevelStmt(), and Query::windowClause.

Referenced by simplify_function().

4397 {
4398  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4399  char *src;
4400  Datum tmp;
4401  bool isNull;
4402  bool modifyTargetList;
4403  MemoryContext oldcxt;
4404  MemoryContext mycxt;
4405  inline_error_callback_arg callback_arg;
4406  ErrorContextCallback sqlerrcontext;
4407  FuncExpr *fexpr;
4409  ParseState *pstate;
4410  List *raw_parsetree_list;
4411  Query *querytree;
4412  Node *newexpr;
4413  int *usecounts;
4414  ListCell *arg;
4415  int i;
4416 
4417  /*
4418  * Forget it if the function is not SQL-language or has other showstopper
4419  * properties. (The prokind and nargs checks are just paranoia.)
4420  */
4421  if (funcform->prolang != SQLlanguageId ||
4422  funcform->prokind != PROKIND_FUNCTION ||
4423  funcform->prosecdef ||
4424  funcform->proretset ||
4425  funcform->prorettype == RECORDOID ||
4426  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL) ||
4427  funcform->pronargs != list_length(args))
4428  return NULL;
4429 
4430  /* Check for recursive function, and give up trying to expand if so */
4431  if (list_member_oid(context->active_fns, funcid))
4432  return NULL;
4433 
4434  /* Check permission to call function (fail later, if not) */
4436  return NULL;
4437 
4438  /* Check whether a plugin wants to hook function entry/exit */
4439  if (FmgrHookIsNeeded(funcid))
4440  return NULL;
4441 
4442  /*
4443  * Make a temporary memory context, so that we don't leak all the stuff
4444  * that parsing might create.
4445  */
4447  "inline_function",
4449  oldcxt = MemoryContextSwitchTo(mycxt);
4450 
4451  /* Fetch the function body */
4452  tmp = SysCacheGetAttr(PROCOID,
4453  func_tuple,
4454  Anum_pg_proc_prosrc,
4455  &isNull);
4456  if (isNull)
4457  elog(ERROR, "null prosrc for function %u", funcid);
4458  src = TextDatumGetCString(tmp);
4459 
4460  /*
4461  * Setup error traceback support for ereport(). This is so that we can
4462  * finger the function that bad information came from.
4463  */
4464  callback_arg.proname = NameStr(funcform->proname);
4465  callback_arg.prosrc = src;
4466 
4467  sqlerrcontext.callback = sql_inline_error_callback;
4468  sqlerrcontext.arg = (void *) &callback_arg;
4469  sqlerrcontext.previous = error_context_stack;
4470  error_context_stack = &sqlerrcontext;
4471 
4472  /*
4473  * Set up to handle parameters while parsing the function body. We need a
4474  * dummy FuncExpr node containing the already-simplified arguments to pass
4475  * to prepare_sql_fn_parse_info. (It is really only needed if there are
4476  * some polymorphic arguments, but for simplicity we always build it.)
4477  */
4478  fexpr = makeNode(FuncExpr);
4479  fexpr->funcid = funcid;
4480  fexpr->funcresulttype = result_type;
4481  fexpr->funcretset = false;
4482  fexpr->funcvariadic = funcvariadic;
4483  fexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4484  fexpr->funccollid = result_collid; /* doesn't matter */
4485  fexpr->inputcollid = input_collid;
4486  fexpr->args = args;
4487  fexpr->location = -1;
4488 
4489  pinfo = prepare_sql_fn_parse_info(func_tuple,
4490  (Node *) fexpr,
4491  input_collid);
4492 
4493  /*
4494  * We just do parsing and parse analysis, not rewriting, because rewriting
4495  * will not affect table-free-SELECT-only queries, which is all that we
4496  * care about. Also, we can punt as soon as we detect more than one
4497  * command in the function body.
4498  */
4499  raw_parsetree_list = pg_parse_query(src);
4500  if (list_length(raw_parsetree_list) != 1)
4501  goto fail;
4502 
4503  pstate = make_parsestate(NULL);
4504  pstate->p_sourcetext = src;
4505  sql_fn_parser_setup(pstate, pinfo);
4506 
4507  querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
4508 
4509  free_parsestate(pstate);
4510 
4511  /*
4512  * The single command must be a simple "SELECT expression".
4513  *
4514  * Note: if you change the tests involved in this, see also plpgsql's
4515  * exec_simple_check_plan(). That generally needs to have the same idea
4516  * of what's a "simple expression", so that inlining a function that
4517  * previously wasn't inlined won't change plpgsql's conclusion.
4518  */
4519  if (!IsA(querytree, Query) ||
4520  querytree->commandType != CMD_SELECT ||
4521  querytree->hasAggs ||
4522  querytree->hasWindowFuncs ||
4523  querytree->hasTargetSRFs ||
4524  querytree->hasSubLinks ||
4525  querytree->cteList ||
4526  querytree->rtable ||
4527  querytree->jointree->fromlist ||
4528  querytree->jointree->quals ||
4529  querytree->groupClause ||
4530  querytree->groupingSets ||
4531  querytree->havingQual ||
4532  querytree->windowClause ||
4533  querytree->distinctClause ||
4534  querytree->sortClause ||
4535  querytree->limitOffset ||
4536  querytree->limitCount ||
4537  querytree->setOperations ||
4538  list_length(querytree->targetList) != 1)
4539  goto fail;
4540 
4541  /*
4542  * Make sure the function (still) returns what it's declared to. This
4543  * will raise an error if wrong, but that's okay since the function would
4544  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4545  * a RelabelType if needed to make the tlist expression match the declared
4546  * type of the function.
4547  *
4548  * Note: we do not try this until we have verified that no rewriting was
4549  * needed; that's probably not important, but let's be careful.
4550  */
4551  if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
4552  &modifyTargetList, NULL))
4553  goto fail; /* reject whole-tuple-result cases */
4554 
4555  /* Now we can grab the tlist expression */
4556  newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
4557 
4558  /*
4559  * If the SQL function returns VOID, we can only inline it if it is a
4560  * SELECT of an expression returning VOID (ie, it's just a redirection to
4561  * another VOID-returning function). In all non-VOID-returning cases,
4562  * check_sql_fn_retval should ensure that newexpr returns the function's
4563  * declared result type, so this test shouldn't fail otherwise; but we may
4564  * as well cope gracefully if it does.
4565  */
4566  if (exprType(newexpr) != result_type)
4567  goto fail;
4568 
4569  /* check_sql_fn_retval couldn't have made any dangerous tlist changes */
4570  Assert(!modifyTargetList);
4571 
4572  /*
4573  * Additional validity checks on the expression. It mustn't be more
4574  * volatile than the surrounding function (this is to avoid breaking hacks
4575  * that involve pretending a function is immutable when it really ain't).
4576  * If the surrounding function is declared strict, then the expression
4577  * must contain only strict constructs and must use all of the function
4578  * parameters (this is overkill, but an exact analysis is hard).
4579  */
4580  if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
4581  contain_mutable_functions(newexpr))
4582  goto fail;
4583  else if (funcform->provolatile == PROVOLATILE_STABLE &&
4584  contain_volatile_functions(newexpr))
4585  goto fail;
4586 
4587  if (funcform->proisstrict &&
4588  contain_nonstrict_functions(newexpr))
4589  goto fail;
4590 
4591  /*
4592  * If any parameter expression contains a context-dependent node, we can't
4593  * inline, for fear of putting such a node into the wrong context.
4594  */
4595  if (contain_context_dependent_node((Node *) args))
4596  goto fail;
4597 
4598  /*
4599  * We may be able to do it; there are still checks on parameter usage to
4600  * make, but those are most easily done in combination with the actual
4601  * substitution of the inputs. So start building expression with inputs
4602  * substituted.
4603  */
4604  usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
4605  newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
4606  args, usecounts);
4607 
4608  /* Now check for parameter usage */
4609  i = 0;
4610  foreach(arg, args)
4611  {
4612  Node *param = lfirst(arg);
4613 
4614  if (usecounts[i] == 0)
4615  {
4616  /* Param not used at all: uncool if func is strict */
4617  if (funcform->proisstrict)
4618  goto fail;
4619  }
4620  else if (usecounts[i] != 1)
4621  {
4622  /* Param used multiple times: uncool if expensive or volatile */
4623  QualCost eval_cost;
4624 
4625  /*
4626  * We define "expensive" as "contains any subplan or more than 10
4627  * operators". Note that the subplan search has to be done
4628  * explicitly, since cost_qual_eval() will barf on unplanned
4629  * subselects.
4630  */
4631  if (contain_subplans(param))
4632  goto fail;
4633  cost_qual_eval(&eval_cost, list_make1(param), NULL);
4634  if (eval_cost.startup + eval_cost.per_tuple >
4635  10 * cpu_operator_cost)
4636  goto fail;
4637 
4638  /*
4639  * Check volatility last since this is more expensive than the
4640  * above tests
4641  */
4642  if (contain_volatile_functions(param))
4643  goto fail;
4644  }
4645  i++;
4646  }
4647 
4648  /*
4649  * Whew --- we can make the substitution. Copy the modified expression
4650  * out of the temporary memory context, and clean up.
4651  */
4652  MemoryContextSwitchTo(oldcxt);
4653 
4654  newexpr = copyObject(newexpr);
4655 
4656  MemoryContextDelete(mycxt);
4657 
4658  /*
4659  * If the result is of a collatable type, force the result to expose the
4660  * correct collation. In most cases this does not matter, but it's
4661  * possible that the function result is used directly as a sort key or in
4662  * other places where we expect exprCollation() to tell the truth.
4663  */
4664  if (OidIsValid(result_collid))
4665  {
4666  Oid exprcoll = exprCollation(newexpr);
4667 
4668  if (OidIsValid(exprcoll) && exprcoll != result_collid)
4669  {
4670  CollateExpr *newnode = makeNode(CollateExpr);
4671 
4672  newnode->arg = (Expr *) newexpr;
4673  newnode->collOid = result_collid;
4674  newnode->location = -1;
4675 
4676  newexpr = (Node *) newnode;
4677  }
4678  }
4679 
4680  /*
4681  * Since there is now no trace of the function in the plan tree, we must
4682  * explicitly record the plan's dependency on the function.
4683  */
4684  if (context->root)
4685  record_plan_function_dependency(context->root, funcid);
4686 
4687  /*
4688  * Recursively try to simplify the modified expression. Here we must add
4689  * the current function to the context list of active functions.
4690  */
4691  context->active_fns = lappend_oid(context->active_fns, funcid);
4692  newexpr = eval_const_expressions_mutator(newexpr, context);
4693  context->active_fns = list_delete_last(context->active_fns);
4694 
4695  error_context_stack = sqlerrcontext.previous;
4696 
4697  return (Expr *) newexpr;
4698 
4699  /* Here if func is not inlinable: release temp memory and return NULL */
4700 fail:
4701  MemoryContextSwitchTo(oldcxt);
4702  MemoryContextDelete(mycxt);
4703  error_context_stack = sqlerrcontext.previous;
4704 
4705  return NULL;
4706 }
Node * limitOffset
Definition: parsenodes.h:160
Oid funcresulttype
Definition: primnodes.h:456
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
List * sortClause
Definition: parsenodes.h:158
List * args
Definition: primnodes.h:463
FromExpr * jointree
Definition: parsenodes.h:138
Oid GetUserId(void)
Definition: miscinit.c:380
Oid funccollid
Definition: primnodes.h:461
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:273
bool hasAggs
Definition: parsenodes.h:125
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:525
bool check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList, bool *modifyTargetList, JunkFilter **junkFilter)
Definition: functions.c:1574
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
bool funcretset
Definition: primnodes.h:457
List * fromlist
Definition: primnodes.h:1496
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
unsigned int Oid
Definition: postgres_ext.h:31
void(* callback)(void *arg)
Definition: elog.h:254
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
struct ErrorContextCallback * previous
Definition: elog.h:253
#define OidIsValid(objectId)
Definition: c.h:638
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:762
Node * quals
Definition: primnodes.h:1497
Cost startup
Definition: pathnodes.h:45
List * windowClause
Definition: parsenodes.h:154
List * targetList
Definition: parsenodes.h:140
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define list_make1(x1)
Definition: pg_list.h:227
bool contain_subplans(Node *clause)
Definition: clauses.c:610
CoercionForm funcformat
Definition: primnodes.h:460
Cost per_tuple
Definition: pathnodes.h:46
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2331
#define linitial(l)
Definition: pg_list.h:195
List * rtable
Definition: parsenodes.h:137
List * distinctClause
Definition: parsenodes.h:156
Oid funcid
Definition: primnodes.h:455
#define ERROR
Definition: elog.h:43
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3819
List * pg_parse_query(const char *query_string)
Definition: postgres.c:632
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
Node * limitCount
Definition: parsenodes.h:161
List * list_delete_last(List *list)
Definition: list.c:878
double cpu_operator_cost
Definition: costsize.c:114
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
const char * p_sourcetext
Definition: parse_node.h:176
static Node * substitute_actual_parameters(Node *expr, int nargs, List *args, int *usecounts)
Definition: clauses.c:4712
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4754
#define TextDatumGetCString(d)
Definition: builtins.h:84
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:2677
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
CmdType commandType
Definition: parsenodes.h:112
bool hasTargetSRFs
Definition: parsenodes.h:127
#define makeNode(_type_)
Definition: nodes.h:573
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
Oid inputcollid
Definition: primnodes.h:462
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
bool hasWindowFuncs
Definition: parsenodes.h:126
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition: analyze.c:191
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Expr * arg
Definition: primnodes.h:886
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:665
static bool contain_context_dependent_node(Node *clause)
Definition: clauses.c:1243
List * cteList
Definition: parsenodes.h:135
Node * setOperations
Definition: parsenodes.h:165
List * groupClause
Definition: parsenodes.h:148
bool hasSubLinks
Definition: parsenodes.h:128
#define ACL_EXECUTE
Definition: parsenodes.h:81
#define elog(elevel,...)
Definition: elog.h:226
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4655
int i