PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_clause.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "access/heapam.h"
#include "access/tsmapi.h"
#include "catalog/catalog.h"
#include "catalog/heap.h"
#include "catalog/pg_am.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_constraint_fn.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "parser/parser.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "rewrite/rewriteManip.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for parse_clause.c:

Go to the source code of this file.

Macros

#define makeDefaultNSItem(rte)   makeNamespaceItem(rte, true, true, false, true)
 

Functions

static void extractRemainingColumns (List *common_colnames, List *src_colnames, List *src_colvars, List **res_colnames, List **res_colvars)
 
static NodetransformJoinUsingClause (ParseState *pstate, RangeTblEntry *leftRTE, RangeTblEntry *rightRTE, List *leftVars, List *rightVars)
 
static NodetransformJoinOnClause (ParseState *pstate, JoinExpr *j, List *namespace)
 
static RangeTblEntrygetRTEForSpecialRelationTypes (ParseState *pstate, RangeVar *rv)
 
static RangeTblEntrytransformTableEntry (ParseState *pstate, RangeVar *r)
 
static RangeTblEntrytransformRangeSubselect (ParseState *pstate, RangeSubselect *r)
 
static RangeTblEntrytransformRangeFunction (ParseState *pstate, RangeFunction *r)
 
static RangeTblEntrytransformRangeTableFunc (ParseState *pstate, RangeTableFunc *t)
 
static TableSampleClausetransformRangeTableSample (ParseState *pstate, RangeTableSample *rts)
 
static NodetransformFromClauseItem (ParseState *pstate, Node *n, RangeTblEntry **top_rte, int *top_rti, List **namespace)
 
static NodebuildMergedJoinVar (ParseState *pstate, JoinType jointype, Var *l_colvar, Var *r_colvar)
 
static ParseNamespaceItemmakeNamespaceItem (RangeTblEntry *rte, bool rel_visible, bool cols_visible, bool lateral_only, bool lateral_ok)
 
static void setNamespaceColumnVisibility (List *namespace, bool cols_visible)
 
static void setNamespaceLateralState (List *namespace, bool lateral_only, bool lateral_ok)
 
static void checkExprIsVarFree (ParseState *pstate, Node *n, const char *constructName)
 
static TargetEntryfindTargetlistEntrySQL92 (ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
 
static TargetEntryfindTargetlistEntrySQL99 (ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
 
static int get_matching_location (int sortgroupref, List *sortgrouprefs, List *exprs)
 
static Listresolve_unique_index_expr (ParseState *pstate, InferClause *infer, Relation heapRel)
 
static ListaddTargetToGroupList (ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
 
static WindowClausefindWindowClause (List *wclist, const char *name)
 
static NodetransformFrameOffset (ParseState *pstate, int frameOptions, Node *clause)
 
void transformFromClause (ParseState *pstate, List *frmList)
 
int setTargetTable (ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
 
bool interpretOidsOption (List *defList, bool allowOids)
 
NodetransformWhereClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
 
NodetransformLimitClause (ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
 
static void checkTargetlistEntrySQL92 (ParseState *pstate, TargetEntry *tle, ParseExprKind exprKind)
 
static Nodeflatten_grouping_sets (Node *expr, bool toplevel, bool *hasGroupingSets)
 
static Index transformGroupClauseExpr (List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
 
static ListtransformGroupClauseList (List **flatresult, ParseState *pstate, List *list, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
 
static NodetransformGroupingSet (List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
 
ListtransformGroupClause (ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
 
ListtransformSortClause (ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
 
ListtransformWindowDefinitions (ParseState *pstate, List *windowdefs, List **targetlist)
 
ListtransformDistinctClause (ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
 
ListtransformDistinctOnClause (ParseState *pstate, List *distinctlist, List **targetlist, List *sortClause)
 
void transformOnConflictArbiter (ParseState *pstate, OnConflictClause *onConflictClause, List **arbiterExpr, Node **arbiterWhere, Oid *constraint)
 
ListaddTargetToSortList (ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
 
Index assignSortGroupRef (TargetEntry *tle, List *tlist)
 
bool targetIsInSortList (TargetEntry *tle, Oid sortop, List *sortList)
 

Macro Definition Documentation

#define makeDefaultNSItem (   rte)    makeNamespaceItem(rte, true, true, false, true)

Definition at line 52 of file parse_clause.c.

Function Documentation

static List * addTargetToGroupList ( ParseState pstate,
TargetEntry tle,
List grouplist,
List targetlist,
int  location 
)
static

Definition at line 3334 of file parse_clause.c.

References assignSortGroupRef(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, SortGroupClause::eqop, TargetEntry::expr, exprType(), get_sort_group_operators(), SortGroupClause::hashable, InvalidOid, lappend(), makeNode, SortGroupClause::nulls_first, setup_parser_errposition_callback(), SortGroupClause::sortop, targetIsInSortList(), TEXTOID, SortGroupClause::tleSortGroupRef, and UNKNOWNOID.

Referenced by transformDistinctClause(), transformDistinctOnClause(), and transformGroupClauseExpr().

3336 {
3337  Oid restype = exprType((Node *) tle->expr);
3338 
3339  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3340  if (restype == UNKNOWNOID)
3341  {
3342  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3343  restype, TEXTOID, -1,
3346  -1);
3347  restype = TEXTOID;
3348  }
3349 
3350  /* avoid making duplicate grouplist entries */
3351  if (!targetIsInSortList(tle, InvalidOid, grouplist))
3352  {
3354  Oid sortop;
3355  Oid eqop;
3356  bool hashable;
3357  ParseCallbackState pcbstate;
3358 
3359  setup_parser_errposition_callback(&pcbstate, pstate, location);
3360 
3361  /* determine the eqop and optional sortop */
3362  get_sort_group_operators(restype,
3363  false, true, false,
3364  &sortop, &eqop, NULL,
3365  &hashable);
3366 
3368 
3369  grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3370  grpcl->eqop = eqop;
3371  grpcl->sortop = sortop;
3372  grpcl->nulls_first = false; /* OK with or without sortop */
3373  grpcl->hashable = hashable;
3374 
3375  grouplist = lappend(grouplist, grpcl);
3376  }
3377 
3378  return grouplist;
3379 }
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
#define TEXTOID
Definition: pg_type.h:324
Index tleSortGroupRef
Definition: parsenodes.h:1190
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:510
unsigned int Oid
Definition: postgres_ext.h:31
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define makeNode(_type_)
Definition: nodes.h:558
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431
List* addTargetToSortList ( ParseState pstate,
TargetEntry tle,
List sortlist,
List targetlist,
SortBy sortby 
)

Definition at line 3191 of file parse_clause.c.

References Assert, assignSortGroupRef(), cancel_parser_errposition_callback(), COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, compatible_oper_opid(), elog, SortGroupClause::eqop, ereport, errcode(), errhint(), errmsg(), ERROR, TargetEntry::expr, exprLocation(), exprType(), get_equality_op_for_ordering_op(), get_sort_group_operators(), SortGroupClause::hashable, InvalidOid, lappend(), llast, SortBy::location, makeNode, NIL, SortBy::node, SortGroupClause::nulls_first, OidIsValid, op_hashjoinable(), setup_parser_errposition_callback(), SORTBY_ASC, SORTBY_DEFAULT, SORTBY_DESC, SortBy::sortby_dir, SortBy::sortby_nulls, SORTBY_NULLS_DEFAULT, SORTBY_NULLS_FIRST, SORTBY_NULLS_LAST, SORTBY_USING, SortGroupClause::sortop, strVal, targetIsInSortList(), TEXTOID, SortGroupClause::tleSortGroupRef, UNKNOWNOID, and SortBy::useOp.

Referenced by transformAggregateCall(), and transformSortClause().

3193 {
3194  Oid restype = exprType((Node *) tle->expr);
3195  Oid sortop;
3196  Oid eqop;
3197  bool hashable;
3198  bool reverse;
3199  int location;
3200  ParseCallbackState pcbstate;
3201 
3202  /* if tlist item is an UNKNOWN literal, change it to TEXT */
3203  if (restype == UNKNOWNOID)
3204  {
3205  tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3206  restype, TEXTOID, -1,
3209  -1);
3210  restype = TEXTOID;
3211  }
3212 
3213  /*
3214  * Rather than clutter the API of get_sort_group_operators and the other
3215  * functions we're about to use, make use of error context callback to
3216  * mark any error reports with a parse position. We point to the operator
3217  * location if present, else to the expression being sorted. (NB: use the
3218  * original untransformed expression here; the TLE entry might well point
3219  * at a duplicate expression in the regular SELECT list.)
3220  */
3221  location = sortby->location;
3222  if (location < 0)
3223  location = exprLocation(sortby->node);
3224  setup_parser_errposition_callback(&pcbstate, pstate, location);
3225 
3226  /* determine the sortop, eqop, and directionality */
3227  switch (sortby->sortby_dir)
3228  {
3229  case SORTBY_DEFAULT:
3230  case SORTBY_ASC:
3231  get_sort_group_operators(restype,
3232  true, true, false,
3233  &sortop, &eqop, NULL,
3234  &hashable);
3235  reverse = false;
3236  break;
3237  case SORTBY_DESC:
3238  get_sort_group_operators(restype,
3239  false, true, true,
3240  NULL, &eqop, &sortop,
3241  &hashable);
3242  reverse = true;
3243  break;
3244  case SORTBY_USING:
3245  Assert(sortby->useOp != NIL);
3246  sortop = compatible_oper_opid(sortby->useOp,
3247  restype,
3248  restype,
3249  false);
3250 
3251  /*
3252  * Verify it's a valid ordering operator, fetch the corresponding
3253  * equality operator, and determine whether to consider it like
3254  * ASC or DESC.
3255  */
3256  eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3257  if (!OidIsValid(eqop))
3258  ereport(ERROR,
3259  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3260  errmsg("operator %s is not a valid ordering operator",
3261  strVal(llast(sortby->useOp))),
3262  errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3263 
3264  /*
3265  * Also see if the equality operator is hashable.
3266  */
3267  hashable = op_hashjoinable(eqop, restype);
3268  break;
3269  default:
3270  elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3271  sortop = InvalidOid; /* keep compiler quiet */
3272  eqop = InvalidOid;
3273  hashable = false;
3274  reverse = false;
3275  break;
3276  }
3277 
3279 
3280  /* avoid making duplicate sortlist entries */
3281  if (!targetIsInSortList(tle, sortop, sortlist))
3282  {
3284 
3285  sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3286 
3287  sortcl->eqop = eqop;
3288  sortcl->sortop = sortop;
3289  sortcl->hashable = hashable;
3290 
3291  switch (sortby->sortby_nulls)
3292  {
3293  case SORTBY_NULLS_DEFAULT:
3294  /* NULLS FIRST is default for DESC; other way for ASC */
3295  sortcl->nulls_first = reverse;
3296  break;
3297  case SORTBY_NULLS_FIRST:
3298  sortcl->nulls_first = true;
3299  break;
3300  case SORTBY_NULLS_LAST:
3301  sortcl->nulls_first = false;
3302  break;
3303  default:
3304  elog(ERROR, "unrecognized sortby_nulls: %d",
3305  sortby->sortby_nulls);
3306  break;
3307  }
3308 
3309  sortlist = lappend(sortlist, sortcl);
3310  }
3311 
3312  return sortlist;
3313 }
#define NIL
Definition: pg_list.h:69
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
SortByDir sortby_dir
Definition: parsenodes.h:469
#define TEXTOID
Definition: pg_type.h:324
List * useOp
Definition: parsenodes.h:471
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:264
#define llast(l)
Definition: pg_list.h:131
Index tleSortGroupRef
Definition: parsenodes.h:1190
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:510
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1246
#define ERROR
Definition: elog.h:43
SortByNulls sortby_nulls
Definition: parsenodes.h:470
#define ereport(elevel, rest)
Definition: elog.h:122
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define UNKNOWNOID
Definition: pg_type.h:431
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition: parse_oper.c:494
#define elog
Definition: elog.h:219
Node * node
Definition: parsenodes.h:468
int location
Definition: parsenodes.h:472
Index assignSortGroupRef ( TargetEntry tle,
List tlist 
)

Definition at line 3389 of file parse_clause.c.

References lfirst, and TargetEntry::ressortgroupref.

Referenced by addTargetToGroupList(), addTargetToSortList(), build_minmax_path(), create_unique_plan(), and transformDistinctOnClause().

3390 {
3391  Index maxRef;
3392  ListCell *l;
3393 
3394  if (tle->ressortgroupref) /* already has one? */
3395  return tle->ressortgroupref;
3396 
3397  /* easiest way to pick an unused refnumber: max used + 1 */
3398  maxRef = 0;
3399  foreach(l, tlist)
3400  {
3401  Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3402 
3403  if (ref > maxRef)
3404  maxRef = ref;
3405  }
3406  tle->ressortgroupref = maxRef + 1;
3407  return tle->ressortgroupref;
3408 }
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1371
static Node * buildMergedJoinVar ( ParseState pstate,
JoinType  jointype,
Var l_colvar,
Var r_colvar 
)
static

Definition at line 1538 of file parse_clause.c.

References CoalesceExpr::args, assign_expr_collations(), CoalesceExpr::coalescetype, COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, elog, ERROR, InvalidOid, IsA, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, list_make2, CoalesceExpr::location, makeNode, makeRelabelType(), select_common_type(), Var::vartype, and Var::vartypmod.

Referenced by transformFromClauseItem().

1540 {
1541  Oid outcoltype;
1542  int32 outcoltypmod;
1543  Node *l_node,
1544  *r_node,
1545  *res_node;
1546 
1547  /*
1548  * Choose output type if input types are dissimilar.
1549  */
1550  outcoltype = l_colvar->vartype;
1551  outcoltypmod = l_colvar->vartypmod;
1552  if (outcoltype != r_colvar->vartype)
1553  {
1554  outcoltype = select_common_type(pstate,
1555  list_make2(l_colvar, r_colvar),
1556  "JOIN/USING",
1557  NULL);
1558  outcoltypmod = -1; /* ie, unknown */
1559  }
1560  else if (outcoltypmod != r_colvar->vartypmod)
1561  {
1562  /* same type, but not same typmod */
1563  outcoltypmod = -1; /* ie, unknown */
1564  }
1565 
1566  /*
1567  * Insert coercion functions if needed. Note that a difference in typmod
1568  * can only happen if input has typmod but outcoltypmod is -1. In that
1569  * case we insert a RelabelType to clearly mark that result's typmod is
1570  * not same as input. We never need coerce_type_typmod.
1571  */
1572  if (l_colvar->vartype != outcoltype)
1573  l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1574  outcoltype, outcoltypmod,
1576  else if (l_colvar->vartypmod != outcoltypmod)
1577  l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1578  outcoltype, outcoltypmod,
1579  InvalidOid, /* fixed below */
1581  else
1582  l_node = (Node *) l_colvar;
1583 
1584  if (r_colvar->vartype != outcoltype)
1585  r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1586  outcoltype, outcoltypmod,
1588  else if (r_colvar->vartypmod != outcoltypmod)
1589  r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1590  outcoltype, outcoltypmod,
1591  InvalidOid, /* fixed below */
1593  else
1594  r_node = (Node *) r_colvar;
1595 
1596  /*
1597  * Choose what to emit
1598  */
1599  switch (jointype)
1600  {
1601  case JOIN_INNER:
1602 
1603  /*
1604  * We can use either var; prefer non-coerced one if available.
1605  */
1606  if (IsA(l_node, Var))
1607  res_node = l_node;
1608  else if (IsA(r_node, Var))
1609  res_node = r_node;
1610  else
1611  res_node = l_node;
1612  break;
1613  case JOIN_LEFT:
1614  /* Always use left var */
1615  res_node = l_node;
1616  break;
1617  case JOIN_RIGHT:
1618  /* Always use right var */
1619  res_node = r_node;
1620  break;
1621  case JOIN_FULL:
1622  {
1623  /*
1624  * Here we must build a COALESCE expression to ensure that the
1625  * join output is non-null if either input is.
1626  */
1628 
1629  c->coalescetype = outcoltype;
1630  /* coalescecollid will get set below */
1631  c->args = list_make2(l_node, r_node);
1632  c->location = -1;
1633  res_node = (Node *) c;
1634  break;
1635  }
1636  default:
1637  elog(ERROR, "unrecognized join type: %d", (int) jointype);
1638  res_node = NULL; /* keep compiler quiet */
1639  break;
1640  }
1641 
1642  /*
1643  * Apply assign_expr_collations to fix up the collation info in the
1644  * coercion and CoalesceExpr nodes, if we made any. This must be done now
1645  * so that the join node's alias vars show correct collation info.
1646  */
1647  assign_expr_collations(pstate, res_node);
1648 
1649  return res_node;
1650 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:156
Definition: nodes.h:510
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
signed int int32
Definition: c.h:246
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:170
List * args
Definition: primnodes.h:1047
char * c
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:558
Oid coalescetype
Definition: primnodes.h:1045
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define elog
Definition: elog.h:219
int32 vartypmod
Definition: primnodes.h:171
static void checkExprIsVarFree ( ParseState pstate,
Node n,
const char *  constructName 
)
static

Definition at line 1773 of file parse_clause.c.

References contain_vars_of_level(), ereport, errcode(), errmsg(), ERROR, locate_var_of_level(), and parser_errposition().

Referenced by transformFrameOffset(), and transformLimitClause().

1774 {
1775  if (contain_vars_of_level(n, 0))
1776  {
1777  ereport(ERROR,
1778  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1779  /* translator: %s is name of a SQL construct, eg LIMIT */
1780  errmsg("argument of %s must not contain variables",
1781  constructName),
1782  parser_errposition(pstate,
1783  locate_var_of_level(n, 0))));
1784  }
1785 }
int errcode(int sqlerrcode)
Definition: elog.c:575
int locate_var_of_level(Node *node, int levelsup)
Definition: var.c:437
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void checkTargetlistEntrySQL92 ( ParseState pstate,
TargetEntry tle,
ParseExprKind  exprKind 
)
static

Definition at line 1798 of file parse_clause.c.

References contain_aggs_of_level(), contain_windowfuncs(), elog, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_DISTINCT_ON, EXPR_KIND_GROUP_BY, EXPR_KIND_ORDER_BY, locate_agg_of_level(), locate_windowfunc(), ParseState::p_hasAggs, ParseState::p_hasWindowFuncs, ParseExprKindName(), and parser_errposition().

Referenced by findTargetlistEntrySQL92().

1800 {
1801  switch (exprKind)
1802  {
1803  case EXPR_KIND_GROUP_BY:
1804  /* reject aggregates and window functions */
1805  if (pstate->p_hasAggs &&
1806  contain_aggs_of_level((Node *) tle->expr, 0))
1807  ereport(ERROR,
1808  (errcode(ERRCODE_GROUPING_ERROR),
1809  /* translator: %s is name of a SQL construct, eg GROUP BY */
1810  errmsg("aggregate functions are not allowed in %s",
1811  ParseExprKindName(exprKind)),
1812  parser_errposition(pstate,
1813  locate_agg_of_level((Node *) tle->expr, 0))));
1814  if (pstate->p_hasWindowFuncs &&
1815  contain_windowfuncs((Node *) tle->expr))
1816  ereport(ERROR,
1817  (errcode(ERRCODE_WINDOWING_ERROR),
1818  /* translator: %s is name of a SQL construct, eg GROUP BY */
1819  errmsg("window functions are not allowed in %s",
1820  ParseExprKindName(exprKind)),
1821  parser_errposition(pstate,
1822  locate_windowfunc((Node *) tle->expr))));
1823  break;
1824  case EXPR_KIND_ORDER_BY:
1825  /* no extra checks needed */
1826  break;
1827  case EXPR_KIND_DISTINCT_ON:
1828  /* no extra checks needed */
1829  break;
1830  default:
1831  elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1832  break;
1833  }
1834 }
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
bool p_hasAggs
Definition: parse_node.h:198
int locate_windowfunc(Node *node)
Definition: rewriteManip.c:235
const char * ParseExprKindName(ParseExprKind exprKind)
Definition: parse_expr.c:3393
bool p_hasWindowFuncs
Definition: parse_node.h:199
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int locate_agg_of_level(Node *node, int levelsup)
Definition: rewriteManip.c:131
Expr * expr
Definition: primnodes.h:1368
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
bool contain_aggs_of_level(Node *node, int levelsup)
Definition: rewriteManip.c:67
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
bool contain_windowfuncs(Node *node)
Definition: rewriteManip.c:197
static void extractRemainingColumns ( List common_colnames,
List src_colnames,
List src_colvars,
List **  res_colnames,
List **  res_colvars 
)
static

Definition at line 288 of file parse_clause.c.

References Assert, cnames, forboth, lappend(), lfirst, list_length(), NIL, and strVal.

Referenced by transformFromClauseItem().

291 {
292  List *new_colnames = NIL;
293  List *new_colvars = NIL;
294  ListCell *lnames,
295  *lvars;
296 
297  Assert(list_length(src_colnames) == list_length(src_colvars));
298 
299  forboth(lnames, src_colnames, lvars, src_colvars)
300  {
301  char *colname = strVal(lfirst(lnames));
302  bool match = false;
303  ListCell *cnames;
304 
305  foreach(cnames, common_colnames)
306  {
307  char *ccolname = strVal(lfirst(cnames));
308 
309  if (strcmp(colname, ccolname) == 0)
310  {
311  match = true;
312  break;
313  }
314  }
315 
316  if (!match)
317  {
318  new_colnames = lappend(new_colnames, lfirst(lnames));
319  new_colvars = lappend(new_colvars, lfirst(lvars));
320  }
321  }
322 
323  *res_colnames = new_colnames;
324  *res_colvars = new_colvars;
325 }
#define NIL
Definition: pg_list.h:69
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
#define strVal(v)
Definition: value.h:54
List * lappend(List *list, void *datum)
Definition: list.c:128
HashCompareFunc match
Definition: dynahash.c:213
static const struct cname cnames[]
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Definition: pg_list.h:45
static TargetEntry * findTargetlistEntrySQL92 ( ParseState pstate,
Node node,
List **  tlist,
ParseExprKind  exprKind 
)
static

Definition at line 1854 of file parse_clause.c.

References checkTargetlistEntrySQL92(), colNameToVar(), equal(), ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_GROUP_BY, findTargetlistEntrySQL99(), intVal, IsA, lfirst, linitial, list_length(), name, ParseExprKindName(), parser_errposition(), TargetEntry::resjunk, TargetEntry::resname, strVal, and val.

Referenced by transformDistinctOnClause(), transformGroupClauseExpr(), and transformSortClause().

1856 {
1857  ListCell *tl;
1858 
1859  /*----------
1860  * Handle two special cases as mandated by the SQL92 spec:
1861  *
1862  * 1. Bare ColumnName (no qualifier or subscripts)
1863  * For a bare identifier, we search for a matching column name
1864  * in the existing target list. Multiple matches are an error
1865  * unless they refer to identical values; for example,
1866  * we allow SELECT a, a FROM table ORDER BY a
1867  * but not SELECT a AS b, b FROM table ORDER BY b
1868  * If no match is found, we fall through and treat the identifier
1869  * as an expression.
1870  * For GROUP BY, it is incorrect to match the grouping item against
1871  * targetlist entries: according to SQL92, an identifier in GROUP BY
1872  * is a reference to a column name exposed by FROM, not to a target
1873  * list column. However, many implementations (including pre-7.0
1874  * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1875  * to see if the identifier matches any FROM column name, and only
1876  * try for a targetlist name if it doesn't. This ensures that we
1877  * adhere to the spec in the case where the name could be both.
1878  * DISTINCT ON isn't in the standard, so we can do what we like there;
1879  * we choose to make it work like ORDER BY, on the rather flimsy
1880  * grounds that ordinary DISTINCT works on targetlist entries.
1881  *
1882  * 2. IntegerConstant
1883  * This means to use the n'th item in the existing target list.
1884  * Note that it would make no sense to order/group/distinct by an
1885  * actual constant, so this does not create a conflict with SQL99.
1886  * GROUP BY column-number is not allowed by SQL92, but since
1887  * the standard has no other behavior defined for this syntax,
1888  * we may as well accept this common extension.
1889  *
1890  * Note that pre-existing resjunk targets must not be used in either case,
1891  * since the user didn't write them in his SELECT list.
1892  *
1893  * If neither special case applies, fall through to treat the item as
1894  * an expression per SQL99.
1895  *----------
1896  */
1897  if (IsA(node, ColumnRef) &&
1898  list_length(((ColumnRef *) node)->fields) == 1 &&
1899  IsA(linitial(((ColumnRef *) node)->fields), String))
1900  {
1901  char *name = strVal(linitial(((ColumnRef *) node)->fields));
1902  int location = ((ColumnRef *) node)->location;
1903 
1904  if (exprKind == EXPR_KIND_GROUP_BY)
1905  {
1906  /*
1907  * In GROUP BY, we must prefer a match against a FROM-clause
1908  * column to one against the targetlist. Look to see if there is
1909  * a matching column. If so, fall through to use SQL99 rules.
1910  * NOTE: if name could refer ambiguously to more than one column
1911  * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1912  * just what we want here.
1913  *
1914  * Small tweak for 7.4.3: ignore matches in upper query levels.
1915  * This effectively changes the search order for bare names to (1)
1916  * local FROM variables, (2) local targetlist aliases, (3) outer
1917  * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1918  * SQL99 do not allow GROUPing BY an outer reference, so this
1919  * breaks no cases that are legal per spec, and it seems a more
1920  * self-consistent behavior.
1921  */
1922  if (colNameToVar(pstate, name, true, location) != NULL)
1923  name = NULL;
1924  }
1925 
1926  if (name != NULL)
1927  {
1928  TargetEntry *target_result = NULL;
1929 
1930  foreach(tl, *tlist)
1931  {
1932  TargetEntry *tle = (TargetEntry *) lfirst(tl);
1933 
1934  if (!tle->resjunk &&
1935  strcmp(tle->resname, name) == 0)
1936  {
1937  if (target_result != NULL)
1938  {
1939  if (!equal(target_result->expr, tle->expr))
1940  ereport(ERROR,
1941  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1942 
1943  /*------
1944  translator: first %s is name of a SQL construct, eg ORDER BY */
1945  errmsg("%s \"%s\" is ambiguous",
1946  ParseExprKindName(exprKind),
1947  name),
1948  parser_errposition(pstate, location)));
1949  }
1950  else
1951  target_result = tle;
1952  /* Stay in loop to check for ambiguity */
1953  }
1954  }
1955  if (target_result != NULL)
1956  {
1957  /* return the first match, after suitable validation */
1958  checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1959  return target_result;
1960  }
1961  }
1962  }
1963  if (IsA(node, A_Const))
1964  {
1965  Value *val = &((A_Const *) node)->val;
1966  int location = ((A_Const *) node)->location;
1967  int targetlist_pos = 0;
1968  int target_pos;
1969 
1970  if (!IsA(val, Integer))
1971  ereport(ERROR,
1972  (errcode(ERRCODE_SYNTAX_ERROR),
1973  /* translator: %s is name of a SQL construct, eg ORDER BY */
1974  errmsg("non-integer constant in %s",
1975  ParseExprKindName(exprKind)),
1976  parser_errposition(pstate, location)));
1977 
1978  target_pos = intVal(val);
1979  foreach(tl, *tlist)
1980  {
1981  TargetEntry *tle = (TargetEntry *) lfirst(tl);
1982 
1983  if (!tle->resjunk)
1984  {
1985  if (++targetlist_pos == target_pos)
1986  {
1987  /* return the unique match, after suitable validation */
1988  checkTargetlistEntrySQL92(pstate, tle, exprKind);
1989  return tle;
1990  }
1991  }
1992  }
1993  ereport(ERROR,
1994  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1995  /* translator: %s is name of a SQL construct, eg ORDER BY */
1996  errmsg("%s position %d is not in select list",
1997  ParseExprKindName(exprKind), target_pos),
1998  parser_errposition(pstate, location)));
1999  }
2000 
2001  /*
2002  * Otherwise, we have an expression, so process it per SQL99 rules.
2003  */
2004  return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
2005 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2972
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
char * resname
Definition: primnodes.h:1370
const char * ParseExprKindName(ParseExprKind exprKind)
Definition: parse_expr.c:3393
bool resjunk
Definition: primnodes.h:1375
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Node * colNameToVar(ParseState *pstate, char *colname, bool localonly, int location)
#define lfirst(lc)
Definition: pg_list.h:106
Definition: value.h:42
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static void checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle, ParseExprKind exprKind)
const char * name
Definition: encode.c:521
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
static TargetEntry * findTargetlistEntrySQL99 ( ParseState pstate,
Node node,
List **  tlist,
ParseExprKind  exprKind 
)
static

Definition at line 2021 of file parse_clause.c.

References equal(), TargetEntry::expr, lappend(), lfirst, strip_implicit_coercions(), transformExpr(), and transformTargetEntry().

Referenced by findTargetlistEntrySQL92(), transformGroupClauseExpr(), and transformSortClause().

2023 {
2024  TargetEntry *target_result;
2025  ListCell *tl;
2026  Node *expr;
2027 
2028  /*
2029  * Convert the untransformed node to a transformed expression, and search
2030  * for a match in the tlist. NOTE: it doesn't really matter whether there
2031  * is more than one match. Also, we are willing to match an existing
2032  * resjunk target here, though the SQL92 cases above must ignore resjunk
2033  * targets.
2034  */
2035  expr = transformExpr(pstate, node, exprKind);
2036 
2037  foreach(tl, *tlist)
2038  {
2039  TargetEntry *tle = (TargetEntry *) lfirst(tl);
2040  Node *texpr;
2041 
2042  /*
2043  * Ignore any implicit cast on the existing tlist expression.
2044  *
2045  * This essentially allows the ORDER/GROUP/etc item to adopt the same
2046  * datatype previously selected for a textually-equivalent tlist item.
2047  * There can't be any implicit cast at top level in an ordinary SELECT
2048  * tlist at this stage, but the case does arise with ORDER BY in an
2049  * aggregate function.
2050  */
2051  texpr = strip_implicit_coercions((Node *) tle->expr);
2052 
2053  if (equal(expr, texpr))
2054  return tle;
2055  }
2056 
2057  /*
2058  * If no matches, construct a new target entry which is appended to the
2059  * end of the target list. This target is given resjunk = TRUE so that it
2060  * will not be projected into the final tuple.
2061  */
2062  target_result = transformTargetEntry(pstate, node, expr, exprKind,
2063  NULL, true);
2064 
2065  *tlist = lappend(*tlist, target_result);
2066 
2067  return target_result;
2068 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2972
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
TargetEntry * transformTargetEntry(ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
Definition: parse_target.c:85
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:612
static WindowClause * findWindowClause ( List wclist,
const char *  name 
)
static

Definition at line 3457 of file parse_clause.c.

References lfirst, and WindowClause::name.

Referenced by transformWindowDefinitions().

3458 {
3459  ListCell *l;
3460 
3461  foreach(l, wclist)
3462  {
3463  WindowClause *wc = (WindowClause *) lfirst(l);
3464 
3465  if (wc->name && strcmp(wc->name, name) == 0)
3466  return wc;
3467  }
3468 
3469  return NULL;
3470 }
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
static Node* flatten_grouping_sets ( Node expr,
bool  toplevel,
bool hasGroupingSets 
)
static

Definition at line 2107 of file parse_clause.c.

References RowExpr::args, check_stack_depth(), COERCE_IMPLICIT_CAST, GroupingSet::content, GROUPING_SET_EMPTY, GROUPING_SET_SETS, IsA, GroupingSet::kind, lappend(), lfirst, list_concat(), GroupingSet::location, makeGroupingSet(), NIL, RowExpr::row_format, T_GroupingSet, T_List, T_RowExpr, and Node::type.

Referenced by transformGroupClause().

2108 {
2109  /* just in case of pathological input */
2111 
2112  if (expr == (Node *) NIL)
2113  return (Node *) NIL;
2114 
2115  switch (expr->type)
2116  {
2117  case T_RowExpr:
2118  {
2119  RowExpr *r = (RowExpr *) expr;
2120 
2121  if (r->row_format == COERCE_IMPLICIT_CAST)
2122  return flatten_grouping_sets((Node *) r->args,
2123  false, NULL);
2124  }
2125  break;
2126  case T_GroupingSet:
2127  {
2128  GroupingSet *gset = (GroupingSet *) expr;
2129  ListCell *l2;
2130  List *result_set = NIL;
2131 
2132  if (hasGroupingSets)
2133  *hasGroupingSets = true;
2134 
2135  /*
2136  * at the top level, we skip over all empty grouping sets; the
2137  * caller can supply the canonical GROUP BY () if nothing is
2138  * left.
2139  */
2140 
2141  if (toplevel && gset->kind == GROUPING_SET_EMPTY)
2142  return (Node *) NIL;
2143 
2144  foreach(l2, gset->content)
2145  {
2146  Node *n1 = lfirst(l2);
2147  Node *n2 = flatten_grouping_sets(n1, false, NULL);
2148 
2149  if (IsA(n1, GroupingSet) &&
2150  ((GroupingSet *) n1)->kind == GROUPING_SET_SETS)
2151  {
2152  result_set = list_concat(result_set, (List *) n2);
2153  }
2154  else
2155  result_set = lappend(result_set, n2);
2156  }
2157 
2158  /*
2159  * At top level, keep the grouping set node; but if we're in a
2160  * nested grouping set, then we need to concat the flattened
2161  * result into the outer list if it's simply nested.
2162  */
2163 
2164  if (toplevel || (gset->kind != GROUPING_SET_SETS))
2165  {
2166  return (Node *) makeGroupingSet(gset->kind, result_set, gset->location);
2167  }
2168  else
2169  return (Node *) result_set;
2170  }
2171  case T_List:
2172  {
2173  List *result = NIL;
2174  ListCell *l;
2175 
2176  foreach(l, (List *) expr)
2177  {
2178  Node *n = flatten_grouping_sets(lfirst(l), toplevel, hasGroupingSets);
2179 
2180  if (n != (Node *) NIL)
2181  {
2182  if (IsA(n, List))
2183  result = list_concat(result, (List *) n);
2184  else
2185  result = lappend(result, n);
2186  }
2187  }
2188 
2189  return (Node *) result;
2190  }
2191  default:
2192  break;
2193  }
2194 
2195  return expr;
2196 }
#define NIL
Definition: pg_list.h:69
List * args
Definition: primnodes.h:986
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
List * content
Definition: parsenodes.h:1260
Definition: nodes.h:510
List * list_concat(List *list1, List *list2)
Definition: list.c:321
GroupingSetKind kind
Definition: parsenodes.h:1259
NodeTag type
Definition: nodes.h:512
void check_stack_depth(void)
Definition: postgres.c:3150
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
Definition: nodes.h:291
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:605
CoercionForm row_format
Definition: primnodes.h:998
Definition: pg_list.h:45
static int get_matching_location ( int  sortgroupref,
List sortgrouprefs,
List exprs 
)
static

Definition at line 2978 of file parse_clause.c.

References elog, ERROR, exprLocation(), forboth, lfirst, and lfirst_int.

Referenced by transformDistinctOnClause().

2979 {
2980  ListCell *lcs;
2981  ListCell *lce;
2982 
2983  forboth(lcs, sortgrouprefs, lce, exprs)
2984  {
2985  if (lfirst_int(lcs) == sortgroupref)
2986  return exprLocation((Node *) lfirst(lce));
2987  }
2988  /* if no match, caller blew it */
2989  elog(ERROR, "get_matching_location: no matching sortgroupref");
2990  return -1; /* keep compiler quiet */
2991 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:510
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
#define lfirst(lc)
Definition: pg_list.h:106
#define elog
Definition: elog.h:219
static RangeTblEntry * getRTEForSpecialRelationTypes ( ParseState pstate,
RangeVar rv 
)
static

Definition at line 1052 of file parse_clause.c.

References addRangeTableEntryForCTE(), addRangeTableEntryForENR(), RangeVar::relname, scanNameSpaceForCTE(), scanNameSpaceForENR(), and RangeVar::schemaname.

Referenced by transformFromClauseItem().

1053 {
1054  CommonTableExpr *cte;
1055  Index levelsup;
1056  RangeTblEntry *rte;
1057 
1058  /*
1059  * if it is a qualified name, it can't be a CTE or tuplestore reference
1060  */
1061  if (rv->schemaname)
1062  return NULL;
1063 
1064  cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
1065  if (cte)
1066  rte = addRangeTableEntryForCTE(pstate, cte, levelsup, rv, true);
1067  else if (scanNameSpaceForENR(pstate, rv->relname))
1068  rte = addRangeTableEntryForENR(pstate, rv, true);
1069  else
1070  rte = NULL;
1071 
1072  return rte;
1073 }
RangeTblEntry * addRangeTableEntryForENR(ParseState *pstate, RangeVar *rv, bool inFromCl)
char * schemaname
Definition: primnodes.h:67
char * relname
Definition: primnodes.h:68
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)
unsigned int Index
Definition: c.h:359
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
RangeTblEntry * addRangeTableEntryForCTE(ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
bool interpretOidsOption ( List defList,
bool  allowOids 
)

Definition at line 255 of file parse_clause.c.

References default_with_oids, defGetBoolean(), DefElem::defname, DefElem::defnamespace, ereport, errcode(), errmsg(), ERROR, lfirst, and pg_strcasecmp().

Referenced by DefineRelation(), GetIntoRelEFlags(), and transformCreateStmt().

256 {
257  ListCell *cell;
258 
259  /* Scan list to see if OIDS was included */
260  foreach(cell, defList)
261  {
262  DefElem *def = (DefElem *) lfirst(cell);
263 
264  if (def->defnamespace == NULL &&
265  pg_strcasecmp(def->defname, "oids") == 0)
266  {
267  if (!allowOids)
268  ereport(ERROR,
269  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
270  errmsg("unrecognized parameter \"%s\"",
271  def->defname)));
272  return defGetBoolean(def);
273  }
274  }
275 
276  /* Force no-OIDS result if caller disallows OIDS. */
277  if (!allowOids)
278  return false;
279 
280  /* OIDS option was not specified, so use default. */
281  return default_with_oids;
282 }
char * defnamespace
Definition: parsenodes.h:718
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool default_with_oids
Definition: guc.c:448
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
static ParseNamespaceItem * makeNamespaceItem ( RangeTblEntry rte,
bool  rel_visible,
bool  cols_visible,
bool  lateral_only,
bool  lateral_ok 
)
static

Definition at line 1657 of file parse_clause.c.

References ParseNamespaceItem::p_cols_visible, ParseNamespaceItem::p_lateral_ok, ParseNamespaceItem::p_lateral_only, ParseNamespaceItem::p_rel_visible, ParseNamespaceItem::p_rte, and palloc().

1659 {
1660  ParseNamespaceItem *nsitem;
1661 
1662  nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1663  nsitem->p_rte = rte;
1664  nsitem->p_rel_visible = rel_visible;
1665  nsitem->p_cols_visible = cols_visible;
1666  nsitem->p_lateral_only = lateral_only;
1667  nsitem->p_lateral_ok = lateral_ok;
1668  return nsitem;
1669 }
RangeTblEntry * p_rte
Definition: parse_node.h:246
void * palloc(Size size)
Definition: mcxt.c:848
static List * resolve_unique_index_expr ( ParseState pstate,
InferClause infer,
Relation  heapRel 
)
static

Definition at line 3003 of file parse_clause.c.

References BTREE_AM_OID, IndexElem::collation, ereport, errcode(), errmsg(), ERROR, IndexElem::expr, InferenceElem::expr, EXPR_KIND_INDEX_EXPRESSION, exprLocation(), ColumnRef::fields, get_opclass_oid(), InferClause::indexElems, InferenceElem::infercollid, InferenceElem::inferopclass, InvalidOid, lappend(), lfirst, list_make1, ColumnRef::location, InferClause::location, LookupCollation(), makeNode, makeString(), IndexElem::name, NIL, IndexElem::nulls_ordering, IndexElem::opclass, IndexElem::ordering, parse(), parser_errposition(), SORTBY_DEFAULT, SORTBY_NULLS_DEFAULT, and transformExpr().

Referenced by transformOnConflictArbiter().

3005 {
3006  List *result = NIL;
3007  ListCell *l;
3008 
3009  foreach(l, infer->indexElems)
3010  {
3011  IndexElem *ielem = (IndexElem *) lfirst(l);
3013  Node *parse;
3014 
3015  /*
3016  * Raw grammar re-uses CREATE INDEX infrastructure for unique index
3017  * inference clause, and so will accept opclasses by name and so on.
3018  *
3019  * Make no attempt to match ASC or DESC ordering or NULLS FIRST/NULLS
3020  * LAST ordering, since those are not significant for inference
3021  * purposes (any unique index matching the inference specification in
3022  * other regards is accepted indifferently). Actively reject this as
3023  * wrong-headed.
3024  */
3025  if (ielem->ordering != SORTBY_DEFAULT)
3026  ereport(ERROR,
3027  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3028  errmsg("ASC/DESC is not allowed in ON CONFLICT clause"),
3029  parser_errposition(pstate,
3030  exprLocation((Node *) infer))));
3031  if (ielem->nulls_ordering != SORTBY_NULLS_DEFAULT)
3032  ereport(ERROR,
3033  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3034  errmsg("NULLS FIRST/LAST is not allowed in ON CONFLICT clause"),
3035  parser_errposition(pstate,
3036  exprLocation((Node *) infer))));
3037 
3038  if (!ielem->expr)
3039  {
3040  /* Simple index attribute */
3041  ColumnRef *n;
3042 
3043  /*
3044  * Grammar won't have built raw expression for us in event of
3045  * plain column reference. Create one directly, and perform
3046  * expression transformation. Planner expects this, and performs
3047  * its own normalization for the purposes of matching against
3048  * pg_index.
3049  */
3050  n = makeNode(ColumnRef);
3051  n->fields = list_make1(makeString(ielem->name));
3052  /* Location is approximately that of inference specification */
3053  n->location = infer->location;
3054  parse = (Node *) n;
3055  }
3056  else
3057  {
3058  /* Do parse transformation of the raw expression */
3059  parse = (Node *) ielem->expr;
3060  }
3061 
3062  /*
3063  * transformExpr() should have already rejected subqueries,
3064  * aggregates, and window functions, based on the EXPR_KIND_ for an
3065  * index expression. Expressions returning sets won't have been
3066  * rejected, but don't bother doing so here; there should be no
3067  * available expression unique index to match any such expression
3068  * against anyway.
3069  */
3070  pInfer->expr = transformExpr(pstate, parse, EXPR_KIND_INDEX_EXPRESSION);
3071 
3072  /* Perform lookup of collation and operator class as required */
3073  if (!ielem->collation)
3074  pInfer->infercollid = InvalidOid;
3075  else
3076  pInfer->infercollid = LookupCollation(pstate, ielem->collation,
3077  exprLocation(pInfer->expr));
3078 
3079  if (!ielem->opclass)
3080  pInfer->inferopclass = InvalidOid;
3081  else
3083  ielem->opclass, false);
3084 
3085  result = lappend(result, pInfer);
3086  }
3087 
3088  return result;
3089 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
SortByDir ordering
Definition: parsenodes.h:693
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
#define BTREE_AM_OID
Definition: pg_am.h:70
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
List * indexElems
Definition: parsenodes.h:1337
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid get_opclass_oid(Oid amID, List *opclassname, bool missing_ok)
Definition: opclasscmds.c:221
int location
Definition: parsenodes.h:235
#define list_make1(x1)
Definition: pg_list.h:139
Node * expr
Definition: parsenodes.h:689
SortByNulls nulls_ordering
Definition: parsenodes.h:694
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:496
#define InvalidOid
Definition: postgres_ext.h:36
List * opclass
Definition: parsenodes.h:692
#define makeNode(_type_)
Definition: nodes.h:558
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
char * name
Definition: parsenodes.h:688
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * collation
Definition: parsenodes.h:691
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:234
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649
static void setNamespaceColumnVisibility ( List namespace,
bool  cols_visible 
)
static

Definition at line 1676 of file parse_clause.c.

References lfirst, and ParseNamespaceItem::p_cols_visible.

Referenced by transformFromClauseItem().

1677 {
1678  ListCell *lc;
1679 
1680  foreach(lc, namespace)
1681  {
1682  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1683 
1684  nsitem->p_cols_visible = cols_visible;
1685  }
1686 }
#define lfirst(lc)
Definition: pg_list.h:106
static void setNamespaceLateralState ( List namespace,
bool  lateral_only,
bool  lateral_ok 
)
static

Definition at line 1693 of file parse_clause.c.

References lfirst, ParseNamespaceItem::p_lateral_ok, and ParseNamespaceItem::p_lateral_only.

Referenced by transformFromClause(), transformFromClauseItem(), and transformJoinOnClause().

1694 {
1695  ListCell *lc;
1696 
1697  foreach(lc, namespace)
1698  {
1699  ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1700 
1701  nsitem->p_lateral_only = lateral_only;
1702  nsitem->p_lateral_ok = lateral_ok;
1703  }
1704 }
#define lfirst(lc)
Definition: pg_list.h:106
int setTargetTable ( ParseState pstate,
RangeVar relation,
bool  inh,
bool  alsoSource,
AclMode  requiredPerms 
)

Definition at line 178 of file parse_clause.c.

References addRangeTableEntryForRelation(), addRTEtoQuery(), RangeVar::alias, Assert, ereport, errcode(), errmsg(), ERROR, heap_close, list_length(), NoLock, ParseState::p_rtable, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parserOpenTable(), RangeVar::relname, RangeTblEntry::requiredPerms, RowExclusiveLock, rt_fetch, scanNameSpaceForENR(), and RangeVar::schemaname.

Referenced by transformDeleteStmt(), transformInsertStmt(), and transformUpdateStmt().

180 {
181  RangeTblEntry *rte;
182  int rtindex;
183 
184  /*
185  * ENRs hide tables of the same name, so we need to check for them first.
186  * In contrast, CTEs don't hide tables (for this purpose).
187  */
188  if (relation->schemaname == NULL &&
189  scanNameSpaceForENR(pstate, relation->relname))
190  ereport(ERROR,
191  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
192  errmsg("relation \"%s\" cannot be the target of a modifying statement",
193  relation->relname)));
194 
195  /* Close old target; this could only happen for multi-action rules */
196  if (pstate->p_target_relation != NULL)
198 
199  /*
200  * Open target rel and grab suitable lock (which we will hold till end of
201  * transaction).
202  *
203  * free_parsestate() will eventually do the corresponding heap_close(),
204  * but *not* release the lock.
205  */
206  pstate->p_target_relation = parserOpenTable(pstate, relation,
208 
209  /*
210  * Now build an RTE.
211  */
212  rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
213  relation->alias, inh, false);
214  pstate->p_target_rangetblentry = rte;
215 
216  /* assume new rte is at end */
217  rtindex = list_length(pstate->p_rtable);
218  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
219 
220  /*
221  * Override addRangeTableEntry's default ACL_SELECT permissions check, and
222  * instead mark target table as requiring exactly the specified
223  * permissions.
224  *
225  * If we find an explicit reference to the rel later during parse
226  * analysis, we will add the ACL_SELECT bit back again; see
227  * markVarForSelectPriv and its callers.
228  */
229  rte->requiredPerms = requiredPerms;
230 
231  /*
232  * If UPDATE/DELETE, add table to joinlist and namespace.
233  *
234  * Note: some callers know that they can find the new ParseNamespaceItem
235  * at the end of the pstate->p_namespace list. This is a bit ugly but not
236  * worth complicating this function's signature for.
237  */
238  if (alsoSource)
239  addRTEtoQuery(pstate, rte, true, true, true);
240 
241  return rtindex;
242 }
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
int errcode(int sqlerrcode)
Definition: elog.c:575
AclMode requiredPerms
Definition: parsenodes.h:1053
#define heap_close(r, l)
Definition: heapam.h:97
char * schemaname
Definition: primnodes.h:67
char * relname
Definition: primnodes.h:68
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
bool scanNameSpaceForENR(ParseState *pstate, const char *refname)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:183
#define Assert(condition)
Definition: c.h:681
static int list_length(const List *l)
Definition: pg_list.h:89
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:182
Alias * alias
Definition: primnodes.h:72
List * p_rtable
Definition: parse_node.h:172
bool targetIsInSortList ( TargetEntry tle,
Oid  sortop,
List sortList 
)

Definition at line 3430 of file parse_clause.c.

References get_commutator(), InvalidOid, lfirst, TargetEntry::ressortgroupref, SortGroupClause::sortop, and SortGroupClause::tleSortGroupRef.

Referenced by addTargetToGroupList(), addTargetToSortList(), check_output_expressions(), examine_simple_variable(), targetIsInAllPartitionLists(), transformDistinctOnClause(), and transformGroupClauseExpr().

3431 {
3432  Index ref = tle->ressortgroupref;
3433  ListCell *l;
3434 
3435  /* no need to scan list if tle has no marker */
3436  if (ref == 0)
3437  return false;
3438 
3439  foreach(l, sortList)
3440  {
3441  SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3442 
3443  if (scl->tleSortGroupRef == ref &&
3444  (sortop == InvalidOid ||
3445  sortop == scl->sortop ||
3446  sortop == get_commutator(scl->sortop)))
3447  return true;
3448  }
3449  return false;
3450 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
Index tleSortGroupRef
Definition: parsenodes.h:1190
unsigned int Index
Definition: c.h:359
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1371
List* transformDistinctClause ( ParseState pstate,
List **  targetlist,
List sortClause,
bool  is_agg 
)

Definition at line 2787 of file parse_clause.c.

References addTargetToGroupList(), copyObject, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, exprLocation(), get_sortgroupclause_tle(), lappend(), lfirst, NIL, parser_errposition(), and TargetEntry::resjunk.

Referenced by transformAggregateCall(), and transformSelectStmt().

2789 {
2790  List *result = NIL;
2791  ListCell *slitem;
2792  ListCell *tlitem;
2793 
2794  /*
2795  * The distinctClause should consist of all ORDER BY items followed by all
2796  * other non-resjunk targetlist items. There must not be any resjunk
2797  * ORDER BY items --- that would imply that we are sorting by a value that
2798  * isn't necessarily unique within a DISTINCT group, so the results
2799  * wouldn't be well-defined. This construction ensures we follow the rule
2800  * that sortClause and distinctClause match; in fact the sortClause will
2801  * always be a prefix of distinctClause.
2802  *
2803  * Note a corner case: the same TLE could be in the ORDER BY list multiple
2804  * times with different sortops. We have to include it in the
2805  * distinctClause the same way to preserve the prefix property. The net
2806  * effect will be that the TLE value will be made unique according to both
2807  * sortops.
2808  */
2809  foreach(slitem, sortClause)
2810  {
2811  SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2812  TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2813 
2814  if (tle->resjunk)
2815  ereport(ERROR,
2816  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2817  is_agg ?
2818  errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2819  errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2820  parser_errposition(pstate,
2821  exprLocation((Node *) tle->expr))));
2822  result = lappend(result, copyObject(scl));
2823  }
2824 
2825  /*
2826  * Now add any remaining non-resjunk tlist items, using default sort/group
2827  * semantics for their data types.
2828  */
2829  foreach(tlitem, *targetlist)
2830  {
2831  TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2832 
2833  if (tle->resjunk)
2834  continue; /* ignore junk */
2835  result = addTargetToGroupList(pstate, tle,
2836  result, *targetlist,
2837  exprLocation((Node *) tle->expr));
2838  }
2839 
2840  /*
2841  * Complain if we found nothing to make DISTINCT. Returning an empty list
2842  * would cause the parsed Query to look like it didn't have DISTINCT, with
2843  * results that would probably surprise the user. Note: this case is
2844  * presently impossible for aggregates because of grammar restrictions,
2845  * but we check anyway.
2846  */
2847  if (result == NIL)
2848  ereport(ERROR,
2849  (errcode(ERRCODE_SYNTAX_ERROR),
2850  is_agg ?
2851  errmsg("an aggregate with DISTINCT must have at least one argument") :
2852  errmsg("SELECT DISTINCT must have at least one column")));
2853 
2854  return result;
2855 }
#define NIL
Definition: pg_list.h:69
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
bool resjunk
Definition: primnodes.h:1375
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define copyObject(obj)
Definition: nodes.h:623
Definition: pg_list.h:45
List* transformDistinctOnClause ( ParseState pstate,
List distinctlist,
List **  targetlist,
List sortClause 
)

Definition at line 2871 of file parse_clause.c.

References addTargetToGroupList(), Assert, assignSortGroupRef(), copyObject, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_DISTINCT_ON, exprLocation(), findTargetlistEntrySQL92(), forboth, get_matching_location(), get_sortgroupref_tle(), InvalidOid, lappend(), lappend_int(), lfirst, lfirst_int, list_member_int(), NIL, parser_errposition(), targetIsInSortList(), and SortGroupClause::tleSortGroupRef.

Referenced by transformSelectStmt().

2873 {
2874  List *result = NIL;
2875  List *sortgrouprefs = NIL;
2876  bool skipped_sortitem;
2877  ListCell *lc;
2878  ListCell *lc2;
2879 
2880  /*
2881  * Add all the DISTINCT ON expressions to the tlist (if not already
2882  * present, they are added as resjunk items). Assign sortgroupref numbers
2883  * to them, and make a list of these numbers. (NB: we rely below on the
2884  * sortgrouprefs list being one-for-one with the original distinctlist.
2885  * Also notice that we could have duplicate DISTINCT ON expressions and
2886  * hence duplicate entries in sortgrouprefs.)
2887  */
2888  foreach(lc, distinctlist)
2889  {
2890  Node *dexpr = (Node *) lfirst(lc);
2891  int sortgroupref;
2892  TargetEntry *tle;
2893 
2894  tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2896  sortgroupref = assignSortGroupRef(tle, *targetlist);
2897  sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2898  }
2899 
2900  /*
2901  * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2902  * semantics from ORDER BY items that match DISTINCT ON items, and also
2903  * adopt their column sort order. We insist that the distinctClause and
2904  * sortClause match, so throw error if we find the need to add any more
2905  * distinctClause items after we've skipped an ORDER BY item that wasn't
2906  * in DISTINCT ON.
2907  */
2908  skipped_sortitem = false;
2909  foreach(lc, sortClause)
2910  {
2911  SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2912 
2913  if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2914  {
2915  if (skipped_sortitem)
2916  ereport(ERROR,
2917  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2918  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2919  parser_errposition(pstate,
2921  sortgrouprefs,
2922  distinctlist))));
2923  else
2924  result = lappend(result, copyObject(scl));
2925  }
2926  else
2927  skipped_sortitem = true;
2928  }
2929 
2930  /*
2931  * Now add any remaining DISTINCT ON items, using default sort/group
2932  * semantics for their data types. (Note: this is pretty questionable; if
2933  * the ORDER BY list doesn't include all the DISTINCT ON items and more
2934  * besides, you certainly aren't using DISTINCT ON in the intended way,
2935  * and you probably aren't going to get consistent results. It might be
2936  * better to throw an error or warning here. But historically we've
2937  * allowed it, so keep doing so.)
2938  */
2939  forboth(lc, distinctlist, lc2, sortgrouprefs)
2940  {
2941  Node *dexpr = (Node *) lfirst(lc);
2942  int sortgroupref = lfirst_int(lc2);
2943  TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2944 
2945  if (targetIsInSortList(tle, InvalidOid, result))
2946  continue; /* already in list (with some semantics) */
2947  if (skipped_sortitem)
2948  ereport(ERROR,
2949  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2950  errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2951  parser_errposition(pstate, exprLocation(dexpr))));
2952  result = addTargetToGroupList(pstate, tle,
2953  result, *targetlist,
2954  exprLocation(dexpr));
2955  }
2956 
2957  /*
2958  * An empty result list is impossible here because of grammar
2959  * restrictions.
2960  */
2961  Assert(result != NIL);
2962 
2963  return result;
2964 }
#define NIL
Definition: pg_list.h:69
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Index tleSortGroupRef
Definition: parsenodes.h:1190
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
bool list_member_int(const List *list, int datum)
Definition: list.c:485
#define ereport(elevel, rest)
Definition: elog.h:122
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:348
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define copyObject(obj)
Definition: nodes.h:623
Definition: pg_list.h:45
static Node * transformFrameOffset ( ParseState pstate,
int  frameOptions,
Node clause 
)
static

Definition at line 3477 of file parse_clause.c.

References Assert, checkExprIsVarFree(), coerce_to_specific_type(), elog, ERROR, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, FRAMEOPTION_RANGE, FRAMEOPTION_ROWS, INT8OID, and transformExpr().

Referenced by transformWindowDefinitions().

3478 {
3479  const char *constructName = NULL;
3480  Node *node;
3481 
3482  /* Quick exit if no offset expression */
3483  if (clause == NULL)
3484  return NULL;
3485 
3486  if (frameOptions & FRAMEOPTION_ROWS)
3487  {
3488  /* Transform the raw expression tree */
3489  node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
3490 
3491  /*
3492  * Like LIMIT clause, simply coerce to int8
3493  */
3494  constructName = "ROWS";
3495  node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3496  }
3497  else if (frameOptions & FRAMEOPTION_RANGE)
3498  {
3499  /* Transform the raw expression tree */
3500  node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
3501 
3502  /*
3503  * this needs a lot of thought to decide how to support in the context
3504  * of Postgres' extensible datatype framework
3505  */
3506  constructName = "RANGE";
3507  /* error was already thrown by gram.y, this is just a backstop */
3508  elog(ERROR, "window frame with value offset is not implemented");
3509  }
3510  else
3511  {
3512  Assert(false);
3513  node = NULL;
3514  }
3515 
3516  /* Disallow variables in frame offsets */
3517  checkExprIsVarFree(pstate, node, constructName);
3518 
3519  return node;
3520 }
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
#define ERROR
Definition: elog.h:43
#define INT8OID
Definition: pg_type.h:304
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:505
#define Assert(condition)
Definition: c.h:681
#define FRAMEOPTION_ROWS
Definition: parsenodes.h:506
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
#define elog
Definition: elog.h:219
void transformFromClause ( ParseState pstate,
List frmList 
)

Definition at line 112 of file parse_clause.c.

References checkNameSpaceConflicts(), lappend(), lfirst, list_concat(), ParseState::p_joinlist, ParseState::p_namespace, setNamespaceLateralState(), and transformFromClauseItem().

Referenced by transformDeleteStmt(), transformSelectStmt(), and transformUpdateStmt().

113 {
114  ListCell *fl;
115 
116  /*
117  * The grammar will have produced a list of RangeVars, RangeSubselects,
118  * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
119  * entries to the rtable), check for duplicate refnames, and then add it
120  * to the joinlist and namespace.
121  *
122  * Note we must process the items left-to-right for proper handling of
123  * LATERAL references.
124  */
125  foreach(fl, frmList)
126  {
127  Node *n = lfirst(fl);
128  RangeTblEntry *rte;
129  int rtindex;
130  List *namespace;
131 
132  n = transformFromClauseItem(pstate, n,
133  &rte,
134  &rtindex,
135  &namespace);
136 
137  checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
138 
139  /* Mark the new namespace items as visible only to LATERAL */
140  setNamespaceLateralState(namespace, true, true);
141 
142  pstate->p_joinlist = lappend(pstate->p_joinlist, n);
143  pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
144  }
145 
146  /*
147  * We're done parsing the FROM list, so make all namespace items
148  * unconditionally visible. Note that this will also reset lateral_only
149  * for any namespace items that were already present when we were called;
150  * but those should have been that way already.
151  */
152  setNamespaceLateralState(pstate->p_namespace, false, true);
153 }
Definition: nodes.h:510
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * p_namespace
Definition: parse_node.h:176
static void setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
void checkNameSpaceConflicts(ParseState *pstate, List *namespace1, List *namespace2)
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
static Node * transformFromClauseItem(ParseState *pstate, Node *n, RangeTblEntry **top_rte, int *top_rti, List **namespace)
List * p_joinlist
Definition: parse_node.h:174
Definition: pg_list.h:45
static Node * transformFromClauseItem ( ParseState pstate,
Node n,
RangeTblEntry **  top_rte,
int *  top_rti,
List **  namespace 
)
static

Definition at line 1097 of file parse_clause.c.

References addRangeTableEntryForJoin(), JoinExpr::alias, Alias::aliasname, Assert, buildMergedJoinVar(), castNode, checkNameSpaceConflicts(), Alias::colnames, elog, ereport, errcode(), errmsg(), ERROR, expandRTE(), exprLocation(), extractRemainingColumns(), getRTEForSpecialRelationTypes(), IsA, JoinExpr::isNatural, JOIN_INNER, JOIN_LEFT, JoinExpr::jointype, lappend(), JoinExpr::larg, lfirst, list_concat(), list_length(), list_make1, list_nth(), list_truncate(), makeNode, makeString(), NIL, nodeTag, ParseState::p_joinexprs, ParseState::p_namespace, ParseState::p_rtable, parser_errposition(), JoinExpr::quals, JoinExpr::rarg, RangeTableSample::relation, RangeTblEntry::relkind, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, rt_fetch, RangeTblRef::rtindex, JoinExpr::rtindex, setNamespaceColumnVisibility(), setNamespaceLateralState(), strVal, RangeTblEntry::tablesample, transformJoinOnClause(), transformJoinUsingClause(), transformRangeFunction(), transformRangeSubselect(), transformRangeTableFunc(), transformRangeTableSample(), transformTableEntry(), and JoinExpr::usingClause.

Referenced by transformFromClause().

1100 {
1101  if (IsA(n, RangeVar))
1102  {
1103  /* Plain relation reference, or perhaps a CTE reference */
1104  RangeVar *rv = (RangeVar *) n;
1105  RangeTblRef *rtr;
1106  RangeTblEntry *rte;
1107  int rtindex;
1108 
1109  /* Check if it's a CTE or tuplestore reference */
1110  rte = getRTEForSpecialRelationTypes(pstate, rv);
1111 
1112  /* if not found above, must be a table reference */
1113  if (!rte)
1114  rte = transformTableEntry(pstate, rv);
1115 
1116  /* assume new rte is at end */
1117  rtindex = list_length(pstate->p_rtable);
1118  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1119  *top_rte = rte;
1120  *top_rti = rtindex;
1121  *namespace = list_make1(makeDefaultNSItem(rte));
1122  rtr = makeNode(RangeTblRef);
1123  rtr->rtindex = rtindex;
1124  return (Node *) rtr;
1125  }
1126  else if (IsA(n, RangeSubselect))
1127  {
1128  /* sub-SELECT is like a plain relation */
1129  RangeTblRef *rtr;
1130  RangeTblEntry *rte;
1131  int rtindex;
1132 
1133  rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
1134  /* assume new rte is at end */
1135  rtindex = list_length(pstate->p_rtable);
1136  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1137  *top_rte = rte;
1138  *top_rti = rtindex;
1139  *namespace = list_make1(makeDefaultNSItem(rte));
1140  rtr = makeNode(RangeTblRef);
1141  rtr->rtindex = rtindex;
1142  return (Node *) rtr;
1143  }
1144  else if (IsA(n, RangeFunction))
1145  {
1146  /* function is like a plain relation */
1147  RangeTblRef *rtr;
1148  RangeTblEntry *rte;
1149  int rtindex;
1150 
1151  rte = transformRangeFunction(pstate, (RangeFunction *) n);
1152  /* assume new rte is at end */
1153  rtindex = list_length(pstate->p_rtable);
1154  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1155  *top_rte = rte;
1156  *top_rti = rtindex;
1157  *namespace = list_make1(makeDefaultNSItem(rte));
1158  rtr = makeNode(RangeTblRef);
1159  rtr->rtindex = rtindex;
1160  return (Node *) rtr;
1161  }
1162  else if (IsA(n, RangeTableFunc))
1163  {
1164  /* table function is like a plain relation */
1165  RangeTblRef *rtr;
1166  RangeTblEntry *rte;
1167  int rtindex;
1168 
1169  rte = transformRangeTableFunc(pstate, (RangeTableFunc *) n);
1170  /* assume new rte is at end */
1171  rtindex = list_length(pstate->p_rtable);
1172  Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1173  *top_rte = rte;
1174  *top_rti = rtindex;
1175  *namespace = list_make1(makeDefaultNSItem(rte));
1176  rtr = makeNode(RangeTblRef);
1177  rtr->rtindex = rtindex;
1178  return (Node *) rtr;
1179  }
1180  else if (IsA(n, RangeTableSample))
1181  {
1182  /* TABLESAMPLE clause (wrapping some other valid FROM node) */
1183  RangeTableSample *rts = (RangeTableSample *) n;
1184  Node *rel;
1185  RangeTblRef *rtr;
1186  RangeTblEntry *rte;
1187 
1188  /* Recursively transform the contained relation */
1189  rel = transformFromClauseItem(pstate, rts->relation,
1190  top_rte, top_rti, namespace);
1191  /* Currently, grammar could only return a RangeVar as contained rel */
1192  rtr = castNode(RangeTblRef, rel);
1193  rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
1194  /* We only support this on plain relations and matviews */
1195  if (rte->relkind != RELKIND_RELATION &&
1196  rte->relkind != RELKIND_MATVIEW &&
1198  ereport(ERROR,
1199  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1200  errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
1201  parser_errposition(pstate, exprLocation(rts->relation))));
1202 
1203  /* Transform TABLESAMPLE details and attach to the RTE */
1204  rte->tablesample = transformRangeTableSample(pstate, rts);
1205  return (Node *) rtr;
1206  }
1207  else if (IsA(n, JoinExpr))
1208  {
1209  /* A newfangled join expression */
1210  JoinExpr *j = (JoinExpr *) n;
1211  RangeTblEntry *l_rte;
1212  RangeTblEntry *r_rte;
1213  int l_rtindex;
1214  int r_rtindex;
1215  List *l_namespace,
1216  *r_namespace,
1217  *my_namespace,
1218  *l_colnames,
1219  *r_colnames,
1220  *res_colnames,
1221  *l_colvars,
1222  *r_colvars,
1223  *res_colvars;
1224  bool lateral_ok;
1225  int sv_namespace_length;
1226  RangeTblEntry *rte;
1227  int k;
1228 
1229  /*
1230  * Recursively process the left subtree, then the right. We must do
1231  * it in this order for correct visibility of LATERAL references.
1232  */
1233  j->larg = transformFromClauseItem(pstate, j->larg,
1234  &l_rte,
1235  &l_rtindex,
1236  &l_namespace);
1237 
1238  /*
1239  * Make the left-side RTEs available for LATERAL access within the
1240  * right side, by temporarily adding them to the pstate's namespace
1241  * list. Per SQL:2008, if the join type is not INNER or LEFT then the
1242  * left-side names must still be exposed, but it's an error to
1243  * reference them. (Stupid design, but that's what it says.) Hence,
1244  * we always push them into the namespace, but mark them as not
1245  * lateral_ok if the jointype is wrong.
1246  *
1247  * Notice that we don't require the merged namespace list to be
1248  * conflict-free. See the comments for scanNameSpaceForRefname().
1249  *
1250  * NB: this coding relies on the fact that list_concat is not
1251  * destructive to its second argument.
1252  */
1253  lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
1254  setNamespaceLateralState(l_namespace, true, lateral_ok);
1255 
1256  sv_namespace_length = list_length(pstate->p_namespace);
1257  pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
1258 
1259  /* And now we can process the RHS */
1260  j->rarg = transformFromClauseItem(pstate, j->rarg,
1261  &r_rte,
1262  &r_rtindex,
1263  &r_namespace);
1264 
1265  /* Remove the left-side RTEs from the namespace list again */
1266  pstate->p_namespace = list_truncate(pstate->p_namespace,
1267  sv_namespace_length);
1268 
1269  /*
1270  * Check for conflicting refnames in left and right subtrees. Must do
1271  * this because higher levels will assume I hand back a self-
1272  * consistent namespace list.
1273  */
1274  checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
1275 
1276  /*
1277  * Generate combined namespace info for possible use below.
1278  */
1279  my_namespace = list_concat(l_namespace, r_namespace);
1280 
1281  /*
1282  * Extract column name and var lists from both subtrees
1283  *
1284  * Note: expandRTE returns new lists, safe for me to modify
1285  */
1286  expandRTE(l_rte, l_rtindex, 0, -1, false,
1287  &l_colnames, &l_colvars);
1288  expandRTE(r_rte, r_rtindex, 0, -1, false,
1289  &r_colnames, &r_colvars);
1290 
1291  /*
1292  * Natural join does not explicitly specify columns; must generate
1293  * columns to join. Need to run through the list of columns from each
1294  * table or join result and match up the column names. Use the first
1295  * table, and check every column in the second table for a match.
1296  * (We'll check that the matches were unique later on.) The result of
1297  * this step is a list of column names just like an explicitly-written
1298  * USING list.
1299  */
1300  if (j->isNatural)
1301  {
1302  List *rlist = NIL;
1303  ListCell *lx,
1304  *rx;
1305 
1306  Assert(j->usingClause == NIL); /* shouldn't have USING() too */
1307 
1308  foreach(lx, l_colnames)
1309  {
1310  char *l_colname = strVal(lfirst(lx));
1311  Value *m_name = NULL;
1312 
1313  foreach(rx, r_colnames)
1314  {
1315  char *r_colname = strVal(lfirst(rx));
1316 
1317  if (strcmp(l_colname, r_colname) == 0)
1318  {
1319  m_name = makeString(l_colname);
1320  break;
1321  }
1322  }
1323 
1324  /* matched a right column? then keep as join column... */
1325  if (m_name != NULL)
1326  rlist = lappend(rlist, m_name);
1327  }
1328 
1329  j->usingClause = rlist;
1330  }
1331 
1332  /*
1333  * Now transform the join qualifications, if any.
1334  */
1335  res_colnames = NIL;
1336  res_colvars = NIL;
1337 
1338  if (j->usingClause)
1339  {
1340  /*
1341  * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
1342  * the list into an explicit ON-condition, and generate a list of
1343  * merged result columns.
1344  */
1345  List *ucols = j->usingClause;
1346  List *l_usingvars = NIL;
1347  List *r_usingvars = NIL;
1348  ListCell *ucol;
1349 
1350  Assert(j->quals == NULL); /* shouldn't have ON() too */
1351 
1352  foreach(ucol, ucols)
1353  {
1354  char *u_colname = strVal(lfirst(ucol));
1355  ListCell *col;
1356  int ndx;
1357  int l_index = -1;
1358  int r_index = -1;
1359  Var *l_colvar,
1360  *r_colvar;
1361 
1362  /* Check for USING(foo,foo) */
1363  foreach(col, res_colnames)
1364  {
1365  char *res_colname = strVal(lfirst(col));
1366 
1367  if (strcmp(res_colname, u_colname) == 0)
1368  ereport(ERROR,
1369  (errcode(ERRCODE_DUPLICATE_COLUMN),
1370  errmsg("column name \"%s\" appears more than once in USING clause",
1371  u_colname)));
1372  }
1373 
1374  /* Find it in left input */
1375  ndx = 0;
1376  foreach(col, l_colnames)
1377  {
1378  char *l_colname = strVal(lfirst(col));
1379 
1380  if (strcmp(l_colname, u_colname) == 0)
1381  {
1382  if (l_index >= 0)
1383  ereport(ERROR,
1384  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1385  errmsg("common column name \"%s\" appears more than once in left table",
1386  u_colname)));
1387  l_index = ndx;
1388  }
1389  ndx++;
1390  }
1391  if (l_index < 0)
1392  ereport(ERROR,
1393  (errcode(ERRCODE_UNDEFINED_COLUMN),
1394  errmsg("column \"%s\" specified in USING clause does not exist in left table",
1395  u_colname)));
1396 
1397  /* Find it in right input */
1398  ndx = 0;
1399  foreach(col, r_colnames)
1400  {
1401  char *r_colname = strVal(lfirst(col));
1402 
1403  if (strcmp(r_colname, u_colname) == 0)
1404  {
1405  if (r_index >= 0)
1406  ereport(ERROR,
1407  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1408  errmsg("common column name \"%s\" appears more than once in right table",
1409  u_colname)));
1410  r_index = ndx;
1411  }
1412  ndx++;
1413  }
1414  if (r_index < 0)
1415  ereport(ERROR,
1416  (errcode(ERRCODE_UNDEFINED_COLUMN),
1417  errmsg("column \"%s\" specified in USING clause does not exist in right table",
1418  u_colname)));
1419 
1420  l_colvar = list_nth(l_colvars, l_index);
1421  l_usingvars = lappend(l_usingvars, l_colvar);
1422  r_colvar = list_nth(r_colvars, r_index);
1423  r_usingvars = lappend(r_usingvars, r_colvar);
1424 
1425  res_colnames = lappend(res_colnames, lfirst(ucol));
1426  res_colvars = lappend(res_colvars,
1427  buildMergedJoinVar(pstate,
1428  j->jointype,
1429  l_colvar,
1430  r_colvar));
1431  }
1432 
1433  j->quals = transformJoinUsingClause(pstate,
1434  l_rte,
1435  r_rte,
1436  l_usingvars,
1437  r_usingvars);
1438  }
1439  else if (j->quals)
1440  {
1441  /* User-written ON-condition; transform it */
1442  j->quals = transformJoinOnClause(pstate, j, my_namespace);
1443  }
1444  else
1445  {
1446  /* CROSS JOIN: no quals */
1447  }
1448 
1449  /* Add remaining columns from each side to the output columns */
1450  extractRemainingColumns(res_colnames,
1451  l_colnames, l_colvars,
1452  &l_colnames, &l_colvars);
1453  extractRemainingColumns(res_colnames,
1454  r_colnames, r_colvars,
1455  &r_colnames, &r_colvars);
1456  res_colnames = list_concat(res_colnames, l_colnames);
1457  res_colvars = list_concat(res_colvars, l_colvars);
1458  res_colnames = list_concat(res_colnames, r_colnames);
1459  res_colvars = list_concat(res_colvars, r_colvars);
1460 
1461  /*
1462  * Check alias (AS clause), if any.
1463  */
1464  if (j->alias)
1465  {
1466  if (j->alias->colnames != NIL)
1467  {
1468  if (list_length(j->alias->colnames) > list_length(res_colnames))
1469  ereport(ERROR,
1470  (errcode(ERRCODE_SYNTAX_ERROR),
1471  errmsg("column alias list for \"%s\" has too many entries",
1472  j->alias->aliasname)));
1473  }
1474  }
1475 
1476  /*
1477  * Now build an RTE for the result of the join
1478  */
1479  rte = addRangeTableEntryForJoin(pstate,
1480  res_colnames,
1481  j->jointype,
1482  res_colvars,
1483  j->alias,
1484  true);
1485 
1486  /* assume new rte is at end */
1487  j->rtindex = list_length(pstate->p_rtable);
1488  Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
1489 
1490  *top_rte = rte;
1491  *top_rti = j->rtindex;
1492 
1493  /* make a matching link to the JoinExpr for later use */
1494  for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
1495  pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
1496  pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
1497  Assert(list_length(pstate->p_joinexprs) == j->rtindex);
1498 
1499  /*
1500  * Prepare returned namespace list. If the JOIN has an alias then it
1501  * hides the contained RTEs completely; otherwise, the contained RTEs
1502  * are still visible as table names, but are not visible for
1503  * unqualified column-name access.
1504  *
1505  * Note: if there are nested alias-less JOINs, the lower-level ones
1506  * will remain in the list although they have neither p_rel_visible
1507  * nor p_cols_visible set. We could delete such list items, but it's
1508  * unclear that it's worth expending cycles to do so.
1509  */
1510  if (j->alias != NULL)
1511  my_namespace = NIL;
1512  else
1513  setNamespaceColumnVisibility(my_namespace, false);
1514 
1515  /*
1516  * The join RTE itself is always made visible for unqualified column
1517  * names. It's visible as a relation name only if it has an alias.
1518  */
1519  *namespace = lappend(my_namespace,
1520  makeNamespaceItem(rte,
1521  (j->alias != NULL),
1522  true,
1523  false,
1524  true));
1525 
1526  return (Node *) j;
1527  }
1528  else
1529  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1530  return NULL; /* can't get here, keep compiler quiet */
1531 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
static RangeTblEntry * transformRangeTableFunc(ParseState *pstate, RangeTableFunc *t)
Definition: parse_clause.c:729
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
RangeTblEntry * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
List * colnames
Definition: primnodes.h:43
#define castNode(_type_, nodeptr)
Definition: nodes.h:579
#define RELKIND_MATVIEW
Definition: pg_class.h:165
List * list_truncate(List *list, int new_size)
Definition: list.c:350
static Node * transformJoinUsingClause(ParseState *pstate, RangeTblEntry *leftRTE, RangeTblEntry *rightRTE, List *leftVars, List *rightVars)
Definition: parse_clause.c:333
Definition: nodes.h:510
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
Definition: primnodes.h:163
static void extractRemainingColumns(List *common_colnames, List *src_colnames, List *src_colvars, List **res_colnames, List **res_colvars)
Definition: parse_clause.c:288
Node * larg
Definition: primnodes.h:1451
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
static RangeTblEntry * transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
Definition: parse_clause.c:437
static RangeTblEntry * transformTableEntry(ParseState *pstate, RangeVar *r)
Definition: parse_clause.c:423
List * p_namespace
Definition: parse_node.h:176
static void setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
void * list_nth(const List *list, int n)
Definition: list.c:410
#define makeDefaultNSItem(rte)
Definition: parse_clause.c:52
void checkNameSpaceConflicts(ParseState *pstate, List *namespace1, List *namespace2)
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:128
bool isNatural
Definition: primnodes.h:1450
static Node * buildMergedJoinVar(ParseState *pstate, JoinType jointype, Var *l_colvar, Var *r_colvar)
List * usingClause
Definition: primnodes.h:1453
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
static TableSampleClause * transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
Definition: parse_clause.c:949
static void setNamespaceColumnVisibility(List *namespace, bool cols_visible)
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
Node * quals
Definition: primnodes.h:1454
static RangeTblEntry * getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
#define makeNode(_type_)
Definition: nodes.h:558
Node * rarg
Definition: primnodes.h:1452
Alias * alias
Definition: primnodes.h:1455
JoinType jointype
Definition: primnodes.h:1449
#define Assert(condition)
Definition: c.h:681
static RangeTblEntry * transformRangeFunction(ParseState *pstate, RangeFunction *r)
Definition: parse_clause.c:504
#define lfirst(lc)
Definition: pg_list.h:106
static Node * transformFromClauseItem(ParseState *pstate, Node *n, RangeTblEntry **top_rte, int *top_rti, List **namespace)
char * aliasname
Definition: primnodes.h:42
Definition: value.h:42
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define nodeTag(nodeptr)
Definition: nodes.h:515
static ParseNamespaceItem * makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible, bool lateral_only, bool lateral_ok)
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Node * transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
Definition: parse_clause.c:393
#define elog
Definition: elog.h:219
#define RELKIND_RELATION
Definition: pg_class.h:160
int rtindex
Definition: primnodes.h:1456
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:963
List * p_joinexprs
Definition: parse_node.h:173
List * p_rtable
Definition: parse_node.h:172
List* transformGroupClause ( ParseState pstate,
List grouplist,
List **  groupingSets,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2483 of file parse_clause.c.

References Assert, bms_add_member(), exprLocation(), flatten_grouping_sets(), GROUPING_SET_CUBE, GROUPING_SET_EMPTY, GROUPING_SET_ROLLUP, GROUPING_SET_SETS, GROUPING_SET_SIMPLE, IsA, GroupingSet::kind, lappend(), lfirst, list_make1, list_make1_int, makeGroupingSet(), NIL, transformGroupClauseExpr(), and transformGroupingSet().

Referenced by transformSelectStmt(), and transformWindowDefinitions().

2486 {
2487  List *result = NIL;
2488  List *flat_grouplist;
2489  List *gsets = NIL;
2490  ListCell *gl;
2491  bool hasGroupingSets = false;
2492  Bitmapset *seen_local = NULL;
2493 
2494  /*
2495  * Recursively flatten implicit RowExprs. (Technically this is only needed
2496  * for GROUP BY, per the syntax rules for grouping sets, but we do it
2497  * anyway.)
2498  */
2499  flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2500  true,
2501  &hasGroupingSets);
2502 
2503  /*
2504  * If the list is now empty, but hasGroupingSets is true, it's because we
2505  * elided redundant empty grouping sets. Restore a single empty grouping
2506  * set to leave a canonical form: GROUP BY ()
2507  */
2508 
2509  if (flat_grouplist == NIL && hasGroupingSets)
2510  {
2512  NIL,
2513  exprLocation((Node *) grouplist)));
2514  }
2515 
2516  foreach(gl, flat_grouplist)
2517  {
2518  Node *gexpr = (Node *) lfirst(gl);
2519 
2520  if (IsA(gexpr, GroupingSet))
2521  {
2522  GroupingSet *gset = (GroupingSet *) gexpr;
2523 
2524  switch (gset->kind)
2525  {
2526  case GROUPING_SET_EMPTY:
2527  gsets = lappend(gsets, gset);
2528  break;
2529  case GROUPING_SET_SIMPLE:
2530  /* can't happen */
2531  Assert(false);
2532  break;
2533  case GROUPING_SET_SETS:
2534  case GROUPING_SET_CUBE:
2535  case GROUPING_SET_ROLLUP:
2536  gsets = lappend(gsets,
2537  transformGroupingSet(&result,
2538  pstate, gset,
2539  targetlist, sortClause,
2540  exprKind, useSQL99, true));
2541  break;
2542  }
2543  }
2544  else
2545  {
2546  Index ref = transformGroupClauseExpr(&result, seen_local,
2547  pstate, gexpr,
2548  targetlist, sortClause,
2549  exprKind, useSQL99, true);
2550 
2551  if (ref > 0)
2552  {
2553  seen_local = bms_add_member(seen_local, ref);
2554  if (hasGroupingSets)
2555  gsets = lappend(gsets,
2557  list_make1_int(ref),
2558  exprLocation(gexpr)));
2559  }
2560  }
2561  }
2562 
2563  /* parser should prevent this */
2564  Assert(gsets == NIL || groupingSets != NULL);
2565 
2566  if (groupingSets)
2567  *groupingSets = gsets;
2568 
2569  return result;
2570 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:510
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1259
#define list_make1(x1)
Definition: pg_list.h:139
static Node * flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
#define list_make1_int(x1)
Definition: pg_list.h:145
List * lappend(List *list, void *datum)
Definition: list.c:128
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:605
Definition: pg_list.h:45
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
static Index transformGroupClauseExpr ( List **  flatresult,
Bitmapset seen_local,
ParseState pstate,
Node gexpr,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99,
bool  toplevel 
)
static

Definition at line 2218 of file parse_clause.c.

References addTargetToGroupList(), bms_is_member(), copyObject, exprLocation(), findTargetlistEntrySQL92(), findTargetlistEntrySQL99(), InvalidOid, lappend(), lfirst, SortGroupClause::nulls_first, TargetEntry::ressortgroupref, targetIsInSortList(), and SortGroupClause::tleSortGroupRef.

Referenced by transformGroupClause(), transformGroupClauseList(), and transformGroupingSet().

2222 {
2223  TargetEntry *tle;
2224  bool found = false;
2225 
2226  if (useSQL99)
2227  tle = findTargetlistEntrySQL99(pstate, gexpr,
2228  targetlist, exprKind);
2229  else
2230  tle = findTargetlistEntrySQL92(pstate, gexpr,
2231  targetlist, exprKind);
2232 
2233  if (tle->ressortgroupref > 0)
2234  {
2235  ListCell *sl;
2236 
2237  /*
2238  * Eliminate duplicates (GROUP BY x, x) but only at local level.
2239  * (Duplicates in grouping sets can affect the number of returned
2240  * rows, so can't be dropped indiscriminately.)
2241  *
2242  * Since we don't care about anything except the sortgroupref, we can
2243  * use a bitmapset rather than scanning lists.
2244  */
2245  if (bms_is_member(tle->ressortgroupref, seen_local))
2246  return 0;
2247 
2248  /*
2249  * If we're already in the flat clause list, we don't need to consider
2250  * adding ourselves again.
2251  */
2252  found = targetIsInSortList(tle, InvalidOid, *flatresult);
2253  if (found)
2254  return tle->ressortgroupref;
2255 
2256  /*
2257  * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
2258  * info from the (first) matching ORDER BY item. This means that if
2259  * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
2260  * GROUP BY operation silently takes on the equality semantics implied
2261  * by the ORDER BY. There are two reasons to do this: it improves the
2262  * odds that we can implement both GROUP BY and ORDER BY with a single
2263  * sort step, and it allows the user to choose the equality semantics
2264  * used by GROUP BY, should she be working with a datatype that has
2265  * more than one equality operator.
2266  *
2267  * If we're in a grouping set, though, we force our requested ordering
2268  * to be NULLS LAST, because if we have any hope of using a sorted agg
2269  * for the job, we're going to be tacking on generated NULL values
2270  * after the corresponding groups. If the user demands nulls first,
2271  * another sort step is going to be inevitable, but that's the
2272  * planner's problem.
2273  */
2274 
2275  foreach(sl, sortClause)
2276  {
2277  SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
2278 
2279  if (sc->tleSortGroupRef == tle->ressortgroupref)
2280  {
2281  SortGroupClause *grpc = copyObject(sc);
2282 
2283  if (!toplevel)
2284  grpc->nulls_first = false;
2285  *flatresult = lappend(*flatresult, grpc);
2286  found = true;
2287  break;
2288  }
2289  }
2290  }
2291 
2292  /*
2293  * If no match in ORDER BY, just add it to the result using default
2294  * sort/group semantics.
2295  */
2296  if (!found)
2297  *flatresult = addTargetToGroupList(pstate, tle,
2298  *flatresult, *targetlist,
2299  exprLocation(gexpr));
2300 
2301  /*
2302  * _something_ must have assigned us a sortgroupref by now...
2303  */
2304 
2305  return tle->ressortgroupref;
2306 }
static List * addTargetToGroupList(ParseState *pstate, TargetEntry *tle, List *grouplist, List *targetlist, int location)
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
Index tleSortGroupRef
Definition: parsenodes.h:1190
List * lappend(List *list, void *datum)
Definition: list.c:128
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define lfirst(lc)
Definition: pg_list.h:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
Index ressortgroupref
Definition: primnodes.h:1371
#define copyObject(obj)
Definition: nodes.h:623
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
static List* transformGroupClauseList ( List **  flatresult,
ParseState pstate,
List list,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99,
bool  toplevel 
)
static

Definition at line 2326 of file parse_clause.c.

References bms_add_member(), lappend_int(), lfirst, NIL, and transformGroupClauseExpr().

Referenced by transformGroupingSet().

2330 {
2331  Bitmapset *seen_local = NULL;
2332  List *result = NIL;
2333  ListCell *gl;
2334 
2335  foreach(gl, list)
2336  {
2337  Node *gexpr = (Node *) lfirst(gl);
2338 
2339  Index ref = transformGroupClauseExpr(flatresult,
2340  seen_local,
2341  pstate,
2342  gexpr,
2343  targetlist,
2344  sortClause,
2345  exprKind,
2346  useSQL99,
2347  toplevel);
2348 
2349  if (ref > 0)
2350  {
2351  seen_local = bms_add_member(seen_local, ref);
2352  result = lappend_int(result, ref);
2353  }
2354  }
2355 
2356  return result;
2357 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:510
List * lappend_int(List *list, int datum)
Definition: list.c:146
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
Definition: pg_list.h:45
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
static Node* transformGroupingSet ( List **  flatresult,
ParseState pstate,
GroupingSet gset,
List **  targetlist,
List sortClause,
ParseExprKind  exprKind,
bool  useSQL99,
bool  toplevel 
)
static

Definition at line 2379 of file parse_clause.c.

References Assert, GroupingSet::content, ereport, errcode(), errmsg(), ERROR, exprLocation(), GROUPING_SET_CUBE, GROUPING_SET_SETS, GROUPING_SET_SIMPLE, IsA, GroupingSet::kind, lappend(), lfirst, list_length(), list_make1_int, GroupingSet::location, makeGroupingSet(), NIL, parser_errposition(), transformGroupClauseExpr(), and transformGroupClauseList().

Referenced by transformGroupClause().

2383 {
2384  ListCell *gl;
2385  List *content = NIL;
2386 
2387  Assert(toplevel || gset->kind != GROUPING_SET_SETS);
2388 
2389  foreach(gl, gset->content)
2390  {
2391  Node *n = lfirst(gl);
2392 
2393  if (IsA(n, List))
2394  {
2395  List *l = transformGroupClauseList(flatresult,
2396  pstate, (List *) n,
2397  targetlist, sortClause,
2398  exprKind, useSQL99, false);
2399 
2400  content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2401  l,
2402  exprLocation(n)));
2403  }
2404  else if (IsA(n, GroupingSet))
2405  {
2406  GroupingSet *gset2 = (GroupingSet *) lfirst(gl);
2407 
2408  content = lappend(content, transformGroupingSet(flatresult,
2409  pstate, gset2,
2410  targetlist, sortClause,
2411  exprKind, useSQL99, false));
2412  }
2413  else
2414  {
2415  Index ref = transformGroupClauseExpr(flatresult,
2416  NULL,
2417  pstate,
2418  n,
2419  targetlist,
2420  sortClause,
2421  exprKind,
2422  useSQL99,
2423  false);
2424 
2425  content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2426  list_make1_int(ref),
2427  exprLocation(n)));
2428  }
2429  }
2430 
2431  /* Arbitrarily cap the size of CUBE, which has exponential growth */
2432  if (gset->kind == GROUPING_SET_CUBE)
2433  {
2434  if (list_length(content) > 12)
2435  ereport(ERROR,
2436  (errcode(ERRCODE_TOO_MANY_COLUMNS),
2437  errmsg("CUBE is limited to 12 elements"),
2438  parser_errposition(pstate, gset->location)));
2439  }
2440 
2441  return (Node *) makeGroupingSet(gset->kind, content, gset->location);
2442 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
List * content
Definition: parsenodes.h:1260
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
static Node * transformGroupingSet(List **flatresult, ParseState *pstate, GroupingSet *gset, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
GroupingSetKind kind
Definition: parsenodes.h:1259
#define ERROR
Definition: elog.h:43
static List * transformGroupClauseList(List **flatresult, ParseState *pstate, List *list, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
#define list_make1_int(x1)
Definition: pg_list.h:145
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
Definition: makefuncs.c:605
Definition: pg_list.h:45
static Index transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local, ParseState *pstate, Node *gexpr, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99, bool toplevel)
static Node * transformJoinOnClause ( ParseState pstate,
JoinExpr j,
List namespace 
)
static

Definition at line 393 of file parse_clause.c.

References EXPR_KIND_JOIN_ON, ParseState::p_namespace, JoinExpr::quals, setNamespaceLateralState(), and transformWhereClause().

Referenced by transformFromClauseItem().

394 {
395  Node *result;
396  List *save_namespace;
397 
398  /*
399  * The namespace that the join expression should see is just the two
400  * subtrees of the JOIN plus any outer references from upper pstate
401  * levels. Temporarily set this pstate's namespace accordingly. (We need
402  * not check for refname conflicts, because transformFromClauseItem()
403  * already did.) All namespace items are marked visible regardless of
404  * LATERAL state.
405  */
406  setNamespaceLateralState(namespace, false, true);
407 
408  save_namespace = pstate->p_namespace;
409  pstate->p_namespace = namespace;
410 
411  result = transformWhereClause(pstate, j->quals,
412  EXPR_KIND_JOIN_ON, "JOIN/ON");
413 
414  pstate->p_namespace = save_namespace;
415 
416  return result;
417 }
Definition: nodes.h:510
List * p_namespace
Definition: parse_node.h:176
static void setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
Node * quals
Definition: primnodes.h:1454
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
Definition: pg_list.h:45
static Node * transformJoinUsingClause ( ParseState pstate,
RangeTblEntry leftRTE,
RangeTblEntry rightRTE,
List leftVars,
List rightVars 
)
static

Definition at line 333 of file parse_clause.c.

References AEXPR_OP, AND_EXPR, coerce_to_boolean(), copyObject, EXPR_KIND_JOIN_USING, forboth, lappend(), lfirst, linitial, list_length(), makeBoolExpr(), makeSimpleA_Expr(), markVarForSelectPriv(), NIL, and transformExpr().

Referenced by transformFromClauseItem().

336 {
337  Node *result;
338  List *andargs = NIL;
339  ListCell *lvars,
340  *rvars;
341 
342  /*
343  * We cheat a little bit here by building an untransformed operator tree
344  * whose leaves are the already-transformed Vars. This requires collusion
345  * from transformExpr(), which normally could be expected to complain
346  * about already-transformed subnodes. However, this does mean that we
347  * have to mark the columns as requiring SELECT privilege for ourselves;
348  * transformExpr() won't do it.
349  */
350  forboth(lvars, leftVars, rvars, rightVars)
351  {
352  Var *lvar = (Var *) lfirst(lvars);
353  Var *rvar = (Var *) lfirst(rvars);
354  A_Expr *e;
355 
356  /* Require read access to the join variables */
357  markVarForSelectPriv(pstate, lvar, leftRTE);
358  markVarForSelectPriv(pstate, rvar, rightRTE);
359 
360  /* Now create the lvar = rvar join condition */
361  e = makeSimpleA_Expr(AEXPR_OP, "=",
362  (Node *) copyObject(lvar), (Node *) copyObject(rvar),
363  -1);
364 
365  /* Prepare to combine into an AND clause, if multiple join columns */
366  andargs = lappend(andargs, e);
367  }
368 
369  /* Only need an AND if there's more than one join column */
370  if (list_length(andargs) == 1)
371  result = (Node *) linitial(andargs);
372  else
373  result = (Node *) makeBoolExpr(AND_EXPR, andargs, -1);
374 
375  /*
376  * Since the references are already Vars, and are certainly from the input
377  * relations, we don't have to go through the same pushups that
378  * transformJoinOnClause() does. Just invoke transformExpr() to fix up
379  * the operators, and we're done.
380  */
381  result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
382 
383  result = coerce_to_boolean(pstate, result, "JOIN/USING");
384 
385  return result;
386 }
#define NIL
Definition: pg_list.h:69
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
Definition: primnodes.h:163
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:366
#define linitial(l)
Definition: pg_list.h:111
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
e
Definition: preproc-init.c:82
#define copyObject(obj)
Definition: nodes.h:623
Definition: pg_list.h:45
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
Node* transformLimitClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1742 of file parse_clause.c.

References checkExprIsVarFree(), coerce_to_specific_type(), INT8OID, and transformExpr().

Referenced by transformSelectStmt(), transformSetOperationStmt(), and transformValuesClause().

1744 {
1745  Node *qual;
1746 
1747  if (clause == NULL)
1748  return NULL;
1749 
1750  qual = transformExpr(pstate, clause, exprKind);
1751 
1752  qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1753 
1754  /* LIMIT can't refer to any variables of the current query */
1755  checkExprIsVarFree(pstate, qual, constructName);
1756 
1757  return qual;
1758 }
static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
#define INT8OID
Definition: pg_type.h:304
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
void transformOnConflictArbiter ( ParseState pstate,
OnConflictClause onConflictClause,
List **  arbiterExpr,
Node **  arbiterWhere,
Oid constraint 
)

Definition at line 3100 of file parse_clause.c.

References OnConflictClause::action, addRTEtoQuery(), InferClause::conname, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_INDEX_PREDICATE, exprLocation(), get_relation_constraint_oid(), InferClause::indexElems, OnConflictClause::infer, InvalidOid, IsCatalogRelation(), NIL, ONCONFLICT_UPDATE, ParseState::p_namespace, ParseState::p_target_rangetblentry, ParseState::p_target_relation, parser_errposition(), RelationGetRelationName, RelationGetRelid, RelationIsUsedAsCatalogTable, resolve_unique_index_expr(), transformExpr(), and InferClause::whereClause.

Referenced by transformOnConflictClause().

3104 {
3105  InferClause *infer = onConflictClause->infer;
3106 
3107  *arbiterExpr = NIL;
3108  *arbiterWhere = NULL;
3109  *constraint = InvalidOid;
3110 
3111  if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3112  ereport(ERROR,
3113  (errcode(ERRCODE_SYNTAX_ERROR),
3114  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3115  errhint("For example, ON CONFLICT (column_name)."),
3116  parser_errposition(pstate,
3117  exprLocation((Node *) onConflictClause))));
3118 
3119  /*
3120  * To simplify certain aspects of its design, speculative insertion into
3121  * system catalogs is disallowed
3122  */
3123  if (IsCatalogRelation(pstate->p_target_relation))
3124  ereport(ERROR,
3125  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3126  errmsg("ON CONFLICT is not supported with system catalog tables"),
3127  parser_errposition(pstate,
3128  exprLocation((Node *) onConflictClause))));
3129 
3130  /* Same applies to table used by logical decoding as catalog table */
3132  ereport(ERROR,
3133  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3134  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3136  parser_errposition(pstate,
3137  exprLocation((Node *) onConflictClause))));
3138 
3139  /* ON CONFLICT DO NOTHING does not require an inference clause */
3140  if (infer)
3141  {
3142  List *save_namespace;
3143 
3144  /*
3145  * While we process the arbiter expressions, accept only non-qualified
3146  * references to the target table. Hide any other relations.
3147  */
3148  save_namespace = pstate->p_namespace;
3149  pstate->p_namespace = NIL;
3150  addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3151  false, false, true);
3152 
3153  if (infer->indexElems)
3154  *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3155  pstate->p_target_relation);
3156 
3157  /*
3158  * Handling inference WHERE clause (for partial unique index
3159  * inference)
3160  */
3161  if (infer->whereClause)
3162  *arbiterWhere = transformExpr(pstate, infer->whereClause,
3164 
3165  pstate->p_namespace = save_namespace;
3166 
3167  if (infer->conname)
3169  infer->conname, false);
3170  }
3171 
3172  /*
3173  * It's convenient to form a list of expressions based on the
3174  * representation used by CREATE INDEX, since the same restrictions are
3175  * appropriate (e.g. on subqueries). However, from here on, a dedicated
3176  * primnode representation is used for inference elements, and so
3177  * assign_query_collations() can be trusted to do the right thing with the
3178  * post parse analysis query tree inference clause representation.
3179  */
3180 }
#define NIL
Definition: pg_list.h:69
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:92
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
List * indexElems
Definition: parsenodes.h:1337
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
List * p_namespace
Definition: parse_node.h:176
char * conname
Definition: parsenodes.h:1339
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define ereport(elevel, rest)
Definition: elog.h:122
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:183
InferClause * infer
Definition: parsenodes.h:1353
#define RelationIsUsedAsCatalogTable(relation)
Definition: rel.h:315
#define InvalidOid
Definition: postgres_ext.h:36
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
Node * whereClause
Definition: parsenodes.h:1338
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:182
OnConflictAction action
Definition: parsenodes.h:1352
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:416
static List * resolve_unique_index_expr(ParseState *pstate, InferClause *infer, Relation heapRel)
static RangeTblEntry * transformRangeFunction ( ParseState pstate,
RangeFunction r 
)
static

Definition at line 504 of file parse_clause.c.

References addRangeTableEntryForFunction(), FuncCall::agg_distinct, FuncCall::agg_filter, FuncCall::agg_order, FuncCall::agg_star, arg, FuncCall::args, Assert, assign_list_collations(), RangeFunction::coldeflist, contain_vars_of_level(), ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_FROM_FUNCTION, exprLocation(), fc(), FigureColname(), FuncCall::func_variadic, FuncCall::funcname, RangeFunction::functions, RangeFunction::is_rowsfrom, IsA, lappend(), RangeFunction::lateral, lfirst, linitial, list_length(), list_make1, FuncCall::location, lsecond, makeFuncCall(), NIL, RangeFunction::ordinality, FuncCall::over, ParseState::p_last_srf, ParseState::p_lateral_active, parser_errposition(), strVal, SystemFuncName(), and transformExpr().

Referenced by transformFromClauseItem().

505 {
506  List *funcexprs = NIL;
507  List *funcnames = NIL;
508  List *coldeflists = NIL;
509  bool is_lateral;
510  RangeTblEntry *rte;
511  ListCell *lc;
512 
513  /*
514  * We make lateral_only names of this level visible, whether or not the
515  * RangeFunction is explicitly marked LATERAL. This is needed for SQL
516  * spec compliance in the case of UNNEST(), and seems useful on
517  * convenience grounds for all functions in FROM.
518  *
519  * (LATERAL can't nest within a single pstate level, so we don't need
520  * save/restore logic here.)
521  */
522  Assert(!pstate->p_lateral_active);
523  pstate->p_lateral_active = true;
524 
525  /*
526  * Transform the raw expressions.
527  *
528  * While transforming, also save function names for possible use as alias
529  * and column names. We use the same transformation rules as for a SELECT
530  * output expression. For a FuncCall node, the result will be the
531  * function name, but it is possible for the grammar to hand back other
532  * node types.
533  *
534  * We have to get this info now, because FigureColname only works on raw
535  * parsetrees. Actually deciding what to do with the names is left up to
536  * addRangeTableEntryForFunction.
537  *
538  * Likewise, collect column definition lists if there were any. But
539  * complain if we find one here and the RangeFunction has one too.
540  */
541  foreach(lc, r->functions)
542  {
543  List *pair = (List *) lfirst(lc);
544  Node *fexpr;
545  List *coldeflist;
546  Node *newfexpr;
547  Node *last_srf;
548 
549  /* Disassemble the function-call/column-def-list pairs */
550  Assert(list_length(pair) == 2);
551  fexpr = (Node *) linitial(pair);
552  coldeflist = (List *) lsecond(pair);
553 
554  /*
555  * If we find a function call unnest() with more than one argument and
556  * no special decoration, transform it into separate unnest() calls on
557  * each argument. This is a kluge, for sure, but it's less nasty than
558  * other ways of implementing the SQL-standard UNNEST() syntax.
559  *
560  * If there is any decoration (including a coldeflist), we don't
561  * transform, which probably means a no-such-function error later. We
562  * could alternatively throw an error right now, but that doesn't seem
563  * tremendously helpful. If someone is using any such decoration,
564  * then they're not using the SQL-standard syntax, and they're more
565  * likely expecting an un-tweaked function call.
566  *
567  * Note: the transformation changes a non-schema-qualified unnest()
568  * function name into schema-qualified pg_catalog.unnest(). This
569  * choice is also a bit debatable, but it seems reasonable to force
570  * use of built-in unnest() when we make this transformation.
571  */
572  if (IsA(fexpr, FuncCall))
573  {
574  FuncCall *fc = (FuncCall *) fexpr;
575 
576  if (list_length(fc->funcname) == 1 &&
577  strcmp(strVal(linitial(fc->funcname)), "unnest") == 0 &&
578  list_length(fc->args) > 1 &&
579  fc->agg_order == NIL &&
580  fc->agg_filter == NULL &&
581  !fc->agg_star &&
582  !fc->agg_distinct &&
583  !fc->func_variadic &&
584  fc->over == NULL &&
585  coldeflist == NIL)
586  {
587  ListCell *lc;
588 
589  foreach(lc, fc->args)
590  {
591  Node *arg = (Node *) lfirst(lc);
592  FuncCall *newfc;
593 
594  last_srf = pstate->p_last_srf;
595 
596  newfc = makeFuncCall(SystemFuncName("unnest"),
597  list_make1(arg),
598  fc->location);
599 
600  newfexpr = transformExpr(pstate, (Node *) newfc,
602 
603  /* nodeFunctionscan.c requires SRFs to be at top level */
604  if (pstate->p_last_srf != last_srf &&
605  pstate->p_last_srf != newfexpr)
606  ereport(ERROR,
607  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
608  errmsg("set-returning functions must appear at top level of FROM"),
609  parser_errposition(pstate,
610  exprLocation(pstate->p_last_srf))));
611 
612  funcexprs = lappend(funcexprs, newfexpr);
613 
614  funcnames = lappend(funcnames,
615  FigureColname((Node *) newfc));
616 
617  /* coldeflist is empty, so no error is possible */
618 
619  coldeflists = lappend(coldeflists, coldeflist);
620  }
621  continue; /* done with this function item */
622  }
623  }
624 
625  /* normal case ... */
626  last_srf = pstate->p_last_srf;
627 
628  newfexpr = transformExpr(pstate, fexpr,
630 
631  /* nodeFunctionscan.c requires SRFs to be at top level */
632  if (pstate->p_last_srf != last_srf &&
633  pstate->p_last_srf != newfexpr)
634  ereport(ERROR,
635  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
636  errmsg("set-returning functions must appear at top level of FROM"),
637  parser_errposition(pstate,
638  exprLocation(pstate->p_last_srf))));
639 
640  funcexprs = lappend(funcexprs, newfexpr);
641 
642  funcnames = lappend(funcnames,
643  FigureColname(fexpr));
644 
645  if (coldeflist && r->coldeflist)
646  ereport(ERROR,
647  (errcode(ERRCODE_SYNTAX_ERROR),
648  errmsg("multiple column definition lists are not allowed for the same function"),
649  parser_errposition(pstate,
650  exprLocation((Node *) r->coldeflist))));
651 
652  coldeflists = lappend(coldeflists, coldeflist);
653  }
654 
655  pstate->p_lateral_active = false;
656 
657  /*
658  * We must assign collations now so that the RTE exposes correct collation
659  * info for Vars created from it.
660  */
661  assign_list_collations(pstate, funcexprs);
662 
663  /*
664  * Install the top-level coldeflist if there was one (we already checked
665  * that there was no conflicting per-function coldeflist).
666  *
667  * We only allow this when there's a single function (even after UNNEST
668  * expansion) and no WITH ORDINALITY. The reason for the latter
669  * restriction is that it's not real clear whether the ordinality column
670  * should be in the coldeflist, and users are too likely to make mistakes
671  * in one direction or the other. Putting the coldeflist inside ROWS
672  * FROM() is much clearer in this case.
673  */
674  if (r->coldeflist)
675  {
676  if (list_length(funcexprs) != 1)
677  {
678  if (r->is_rowsfrom)
679  ereport(ERROR,
680  (errcode(ERRCODE_SYNTAX_ERROR),
681  errmsg("ROWS FROM() with multiple functions cannot have a column definition list"),
682  errhint("Put a separate column definition list for each function inside ROWS FROM()."),
683  parser_errposition(pstate,
684  exprLocation((Node *) r->coldeflist))));
685  else
686  ereport(ERROR,
687  (errcode(ERRCODE_SYNTAX_ERROR),
688  errmsg("UNNEST() with multiple arguments cannot have a column definition list"),
689  errhint("Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one."),
690  parser_errposition(pstate,
691  exprLocation((Node *) r->coldeflist))));
692  }
693  if (r->ordinality)
694  ereport(ERROR,
695  (errcode(ERRCODE_SYNTAX_ERROR),
696  errmsg("WITH ORDINALITY cannot be used with a column definition list"),
697  errhint("Put the column definition list inside ROWS FROM()."),
698  parser_errposition(pstate,
699  exprLocation((Node *) r->coldeflist))));
700 
701  coldeflists = list_make1(r->coldeflist);
702  }
703 
704  /*
705  * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
706  * there are any lateral cross-references in it.
707  */
708  is_lateral = r->lateral || contain_vars_of_level((Node *) funcexprs, 0);
709 
710  /*
711  * OK, build an RTE for the function.
712  */
713  rte = addRangeTableEntryForFunction(pstate,
714  funcnames, funcexprs, coldeflists,
715  r, is_lateral, true);
716 
717  return rte;
718 }
#define NIL
Definition: pg_list.h:69
List * SystemFuncName(char *name)
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1186
Node * agg_filter
Definition: parsenodes.h:352
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fc(const char *x)
Definition: preproc-init.c:99
int location
Definition: parsenodes.h:358
List * agg_order
Definition: parsenodes.h:351
#define lsecond(l)
Definition: pg_list.h:116
#define list_make1(x1)
Definition: pg_list.h:139
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
struct WindowDef * over
Definition: parsenodes.h:357
List * coldeflist
Definition: parsenodes.h:561
char * FigureColname(Node *node)
void assign_list_collations(ParseState *pstate, List *exprs)
List * functions
Definition: parsenodes.h:559
#define ereport(elevel, rest)
Definition: elog.h:122
bool is_rowsfrom
Definition: parsenodes.h:558
List * lappend(List *list, void *datum)
Definition: list.c:128
bool func_variadic
Definition: parsenodes.h:356
FuncCall * makeFuncCall(List *name, List *args, int location)
Definition: makefuncs.c:583
Node * p_last_srf
Definition: parse_node.h:204
bool p_lateral_active
Definition: parse_node.h:178
RangeTblEntry * addRangeTableEntryForFunction(ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
List * args
Definition: parsenodes.h:350
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
List * funcname
Definition: parsenodes.h:349
bool agg_distinct
Definition: parsenodes.h:355
bool agg_star
Definition: parsenodes.h:354
Definition: pg_list.h:45
static RangeTblEntry * transformRangeSubselect ( ParseState pstate,
RangeSubselect r 
)
static

Definition at line 437 of file parse_clause.c.

References addRangeTableEntryForSubquery(), RangeSubselect::alias, Alias::aliasname, Assert, CMD_SELECT, Query::commandType, elog, ERROR, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_NONE, IsA, isLockedRefname(), RangeSubselect::lateral, ParseState::p_expr_kind, ParseState::p_lateral_active, parse_sub_analyze(), and RangeSubselect::subquery.

Referenced by transformFromClauseItem().

438 {
439  Query *query;
440  RangeTblEntry *rte;
441 
442  /*
443  * We require user to supply an alias for a subselect, per SQL92. To relax
444  * this, we'd have to be prepared to gin up a unique alias for an
445  * unlabeled subselect. (This is just elog, not ereport, because the
446  * grammar should have enforced it already. It'd probably be better to
447  * report the error here, but we don't have a good error location here.)
448  */
449  if (r->alias == NULL)
450  elog(ERROR, "subquery in FROM must have an alias");
451 
452  /*
453  * Set p_expr_kind to show this parse level is recursing to a subselect.
454  * We can't be nested within any expression, so don't need save-restore
455  * logic here.
456  */
457  Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
459 
460  /*
461  * If the subselect is LATERAL, make lateral_only names of this level
462  * visible to it. (LATERAL can't nest within a single pstate level, so we
463  * don't need save/restore logic here.)
464  */
465  Assert(!pstate->p_lateral_active);
466  pstate->p_lateral_active = r->lateral;
467 
468  /*
469  * Analyze and transform the subquery.
470  */
471  query = parse_sub_analyze(r->subquery, pstate, NULL,
472  isLockedRefname(pstate, r->alias->aliasname),
473  true);
474 
475  /* Restore state */
476  pstate->p_lateral_active = false;
477  pstate->p_expr_kind = EXPR_KIND_NONE;
478 
479  /*
480  * Check that we got a SELECT. Anything else should be impossible given
481  * restrictions of the grammar, but check anyway.
482  */
483  if (!IsA(query, Query) ||
484  query->commandType != CMD_SELECT)
485  elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
486 
487  /*
488  * OK, build an RTE for the subquery.
489  */
490  rte = addRangeTableEntryForSubquery(pstate,
491  query,
492  r->alias,
493  r->lateral,
494  true);
495 
496  return rte;
497 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Node * subquery
Definition: parsenodes.h:535
#define ERROR
Definition: elog.h:43
RangeTblEntry * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
ParseExprKind p_expr_kind
Definition: parse_node.h:186
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:160
bool p_lateral_active
Definition: parse_node.h:178
CmdType commandType
Definition: parsenodes.h:110
#define Assert(condition)
Definition: c.h:681
char * aliasname
Definition: primnodes.h:42
Alias * alias
Definition: parsenodes.h:536
bool isLockedRefname(ParseState *pstate, const char *refname)
#define elog
Definition: elog.h:219
static RangeTblEntry * transformRangeTableFunc ( ParseState pstate,
RangeTableFunc t 
)
static

Definition at line 729 of file parse_clause.c.

References addRangeTableEntryForTableFunc(), RangeTableFunc::alias, Assert, assign_expr_collations(), bms_add_member(), coerce_to_specific_type(), coerce_to_specific_type_typmod(), TableFunc::colcollations, RangeTableFuncCol::coldefexpr, TableFunc::coldefexprs, RangeTableFuncCol::colexpr, TableFunc::colexprs, RangeTableFuncCol::colname, TableFunc::colnames, TableFunc::coltypes, TableFunc::coltypmods, RangeTableFunc::columns, contain_vars_of_level(), DEFAULT_COLLATION_OID, TableFunc::docexpr, RangeTableFunc::docexpr, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_FROM_FUNCTION, RangeTableFuncCol::for_ordinality, INT4OID, InvalidOid, RangeTableFuncCol::is_not_null, IsA, lappend(), lappend_int(), lappend_oid(), RangeTableFunc::lateral, lfirst, list_length(), TableFunc::location, ResTarget::location, RangeTableFunc::location, RangeTableFuncCol::location, makeNode, makeString(), ResTarget::name, name, RangeTableFunc::namespaces, NIL, TableFunc::notnulls, TableFunc::ns_names, TableFunc::ns_uris, TableFunc::ordinalitycol, ParseState::p_lateral_active, palloc(), parser_errposition(), pfree(), pstrdup(), TableFunc::rowexpr, RangeTableFunc::rowexpr, TypeName::setof, strVal, TEXTOID, transformExpr(), type_is_collatable(), RangeTableFuncCol::typeName, typenameTypeIdAndMod(), ResTarget::val, and XMLOID.

Referenced by transformFromClauseItem().

730 {
732  const char *constructName;
733  Oid docType;
734  RangeTblEntry *rte;
735  bool is_lateral;
736  ListCell *col;
737  char **names;
738  int colno;
739 
740  /* Currently only XMLTABLE is supported */
741  constructName = "XMLTABLE";
742  docType = XMLOID;
743 
744  /*
745  * We make lateral_only names of this level visible, whether or not the
746  * RangeTableFunc is explicitly marked LATERAL. This is needed for SQL
747  * spec compliance and seems useful on convenience grounds for all
748  * functions in FROM.
749  *
750  * (LATERAL can't nest within a single pstate level, so we don't need
751  * save/restore logic here.)
752  */
753  Assert(!pstate->p_lateral_active);
754  pstate->p_lateral_active = true;
755 
756  /* Transform and apply typecast to the row-generating expression ... */
757  Assert(rtf->rowexpr != NULL);
758  tf->rowexpr = coerce_to_specific_type(pstate,
759  transformExpr(pstate, rtf->rowexpr, EXPR_KIND_FROM_FUNCTION),
760  TEXTOID,
761  constructName);
762  assign_expr_collations(pstate, tf->rowexpr);
763 
764  /* ... and to the document itself */
765  Assert(rtf->docexpr != NULL);
766  tf->docexpr = coerce_to_specific_type(pstate,
767  transformExpr(pstate, rtf->docexpr, EXPR_KIND_FROM_FUNCTION),
768  docType,
769  constructName);
770  assign_expr_collations(pstate, tf->docexpr);
771 
772  /* undef ordinality column number */
773  tf->ordinalitycol = -1;
774 
775 
776  names = palloc(sizeof(char *) * list_length(rtf->columns));
777 
778  colno = 0;
779  foreach(col, rtf->columns)
780  {
781  RangeTableFuncCol *rawc = (RangeTableFuncCol *) lfirst(col);
782  Oid typid;
783  int32 typmod;
784  Node *colexpr;
785  Node *coldefexpr;
786  int j;
787 
788  tf->colnames = lappend(tf->colnames,
789  makeString(pstrdup(rawc->colname)));
790 
791  /*
792  * Determine the type and typmod for the new column. FOR ORDINALITY
793  * columns are INTEGER per spec; the others are user-specified.
794  */
795  if (rawc->for_ordinality)
796  {
797  if (tf->ordinalitycol != -1)
798  ereport(ERROR,
799  (errcode(ERRCODE_SYNTAX_ERROR),
800  errmsg("only one FOR ORDINALITY column is allowed"),
801  parser_errposition(pstate, rawc->location)));
802 
803  typid = INT4OID;
804  typmod = -1;
805  tf->ordinalitycol = colno;
806  }
807  else
808  {
809  if (rawc->typeName->setof)
810  ereport(ERROR,
811  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
812  errmsg("column \"%s\" cannot be declared SETOF",
813  rawc->colname),
814  parser_errposition(pstate, rawc->location)));
815 
816  typenameTypeIdAndMod(pstate, rawc->typeName,
817  &typid, &typmod);
818  }
819 
820  tf->coltypes = lappend_oid(tf->coltypes, typid);
821  tf->coltypmods = lappend_int(tf->coltypmods, typmod);
824 
825  /* Transform the PATH and DEFAULT expressions */
826  if (rawc->colexpr)
827  {
828  colexpr = coerce_to_specific_type(pstate,
829  transformExpr(pstate, rawc->colexpr,
831  TEXTOID,
832  constructName);
833  assign_expr_collations(pstate, colexpr);
834  }
835  else
836  colexpr = NULL;
837 
838  if (rawc->coldefexpr)
839  {
840  coldefexpr = coerce_to_specific_type_typmod(pstate,
841  transformExpr(pstate, rawc->coldefexpr,
843  typid, typmod,
844  constructName);
845  assign_expr_collations(pstate, coldefexpr);
846  }
847  else
848  coldefexpr = NULL;
849 
850  tf->colexprs = lappend(tf->colexprs, colexpr);
851  tf->coldefexprs = lappend(tf->coldefexprs, coldefexpr);
852 
853  if (rawc->is_not_null)
854  tf->notnulls = bms_add_member(tf->notnulls, colno);
855 
856  /* make sure column names are unique */
857  for (j = 0; j < colno; j++)
858  if (strcmp(names[j], rawc->colname) == 0)
859  ereport(ERROR,
860  (errcode(ERRCODE_SYNTAX_ERROR),
861  errmsg("column name \"%s\" is not unique",
862  rawc->colname),
863  parser_errposition(pstate, rawc->location)));
864  names[colno] = rawc->colname;
865 
866  colno++;
867  }
868  pfree(names);
869 
870  /* Namespaces, if any, also need to be transformed */
871  if (rtf->namespaces != NIL)
872  {
873  ListCell *ns;
874  ListCell *lc2;
875  List *ns_uris = NIL;
876  List *ns_names = NIL;
877  bool default_ns_seen = false;
878 
879  foreach(ns, rtf->namespaces)
880  {
881  ResTarget *r = (ResTarget *) lfirst(ns);
882  Node *ns_uri;
883 
884  Assert(IsA(r, ResTarget));
885  ns_uri = transformExpr(pstate, r->val, EXPR_KIND_FROM_FUNCTION);
886  ns_uri = coerce_to_specific_type(pstate, ns_uri,
887  TEXTOID, constructName);
888  assign_expr_collations(pstate, ns_uri);
889  ns_uris = lappend(ns_uris, ns_uri);
890 
891  /* Verify consistency of name list: no dupes, only one DEFAULT */
892  if (r->name != NULL)
893  {
894  foreach(lc2, ns_names)
895  {
896  char *name = strVal(lfirst(lc2));
897 
898  if (name == NULL)
899  continue;
900  if (strcmp(name, r->name) == 0)
901  ereport(ERROR,
902  (errcode(ERRCODE_SYNTAX_ERROR),
903  errmsg("namespace name \"%s\" is not unique",
904  name),
905  parser_errposition(pstate, r->location)));
906  }
907  }
908  else
909  {
910  if (default_ns_seen)
911  ereport(ERROR,
912  (errcode(ERRCODE_SYNTAX_ERROR),
913  errmsg("only one default namespace is allowed"),
914  parser_errposition(pstate, r->location)));
915  default_ns_seen = true;
916  }
917 
918  /* Note the string may be NULL */
919  ns_names = lappend(ns_names, makeString(r->name));
920  }
921 
922  tf->ns_uris = ns_uris;
923  tf->ns_names = ns_names;
924  }
925 
926  tf->location = rtf->location;
927 
928  pstate->p_lateral_active = false;
929 
930  /*
931  * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
932  * there are any lateral cross-references in it.
933  */
934  is_lateral = rtf->lateral || contain_vars_of_level((Node *) tf, 0);
935 
936  rte = addRangeTableEntryForTableFunc(pstate,
937  tf, rtf->alias, is_lateral, true);
938 
939  return rte;
940 }
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Node * docexpr
Definition: primnodes.h:84
Node * val
Definition: parsenodes.h:441
List * colnames
Definition: primnodes.h:86
char * name
Definition: parsenodes.h:439
List * coltypmods
Definition: primnodes.h:88
#define TEXTOID
Definition: pg_type.h:324
char * pstrdup(const char *in)
Definition: mcxt.c:1076
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:510
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
List * colcollations
Definition: primnodes.h:89
signed int int32
Definition: c.h:246
int location
Definition: parsenodes.h:442
void assign_expr_collations(ParseState *pstate, Node *expr)
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
bool setof
Definition: parsenodes.h:210
List * coldefexprs
Definition: primnodes.h:91
List * ns_names
Definition: primnodes.h:83
Node * rowexpr
Definition: primnodes.h:85
#define XMLOID
Definition: pg_type.h:359
TypeName * typeName
Definition: parsenodes.h:590
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
Bitmapset * notnulls
Definition: primnodes.h:92
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
int ordinalitycol
Definition: primnodes.h:93
List * coltypes
Definition: primnodes.h:87
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:293
#define InvalidOid
Definition: postgres_ext.h:36
List * colexprs
Definition: primnodes.h:90
bool p_lateral_active
Definition: parse_node.h:178
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
List * ns_uris
Definition: primnodes.h:82
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
RangeTblEntry * addRangeTableEntryForTableFunc(ParseState *pstate, TableFunc *tf, Alias *alias, bool lateral, bool inFromCl)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
const char * name
Definition: encode.c:521
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
int location
Definition: primnodes.h:94
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:2806
Definition: pg_list.h:45
Node * coerce_to_specific_type_typmod(ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName)
static TableSampleClause * transformRangeTableSample ( ParseState pstate,
RangeTableSample rts 
)
static

Definition at line 949 of file parse_clause.c.

References arg, RangeTableSample::args, TableSampleClause::args, assign_expr_collations(), coerce_to_specific_type(), ereport, errcode(), errmsg(), errmsg_plural(), ERROR, EXPR_KIND_FROM_FUNCTION, FLOAT8OID, forboth, get_func_rettype(), GetTsmRoutine(), INTERNALOID, lappend(), lfirst, lfirst_oid, list_length(), RangeTableSample::location, LookupFuncName(), makeNode, RangeTableSample::method, NameListToString(), NIL, OidIsValid, TsmRoutine::parameterTypes, parser_errposition(), RangeTableSample::repeatable, TableSampleClause::repeatable, TsmRoutine::repeatable_across_queries, transformExpr(), TSM_HANDLEROID, and TableSampleClause::tsmhandler.

Referenced by transformFromClauseItem().

950 {
951  TableSampleClause *tablesample;
952  Oid handlerOid;
953  Oid funcargtypes[1];
954  TsmRoutine *tsm;
955  List *fargs;
956  ListCell *larg,
957  *ltyp;
958 
959  /*
960  * To validate the sample method name, look up the handler function, which
961  * has the same name, one dummy INTERNAL argument, and a result type of
962  * tsm_handler. (Note: tablesample method names are not schema-qualified
963  * in the SQL standard; but since they are just functions to us, we allow
964  * schema qualification to resolve any potential ambiguity.)
965  */
966  funcargtypes[0] = INTERNALOID;
967 
968  handlerOid = LookupFuncName(rts->method, 1, funcargtypes, true);
969 
970  /* we want error to complain about no-such-method, not no-such-function */
971  if (!OidIsValid(handlerOid))
972  ereport(ERROR,
973  (errcode(ERRCODE_UNDEFINED_OBJECT),
974  errmsg("tablesample method %s does not exist",
975  NameListToString(rts->method)),
976  parser_errposition(pstate, rts->location)));
977 
978  /* check that handler has correct return type */
979  if (get_func_rettype(handlerOid) != TSM_HANDLEROID)
980  ereport(ERROR,
981  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
982  errmsg("function %s must return type %s",
983  NameListToString(rts->method), "tsm_handler"),
984  parser_errposition(pstate, rts->location)));
985 
986  /* OK, run the handler to get TsmRoutine, for argument type info */
987  tsm = GetTsmRoutine(handlerOid);
988 
989  tablesample = makeNode(TableSampleClause);
990  tablesample->tsmhandler = handlerOid;
991 
992  /* check user provided the expected number of arguments */
993  if (list_length(rts->args) != list_length(tsm->parameterTypes))
994  ereport(ERROR,
995  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
996  errmsg_plural("tablesample method %s requires %d argument, not %d",
997  "tablesample method %s requires %d arguments, not %d",
999  NameListToString(rts->method),
1001  list_length(rts->args)),
1002  parser_errposition(pstate, rts->location)));
1003 
1004  /*
1005  * Transform the arguments, typecasting them as needed. Note we must also
1006  * assign collations now, because assign_query_collations() doesn't
1007  * examine any substructure of RTEs.
1008  */
1009  fargs = NIL;
1010  forboth(larg, rts->args, ltyp, tsm->parameterTypes)
1011  {
1012  Node *arg = (Node *) lfirst(larg);
1013  Oid argtype = lfirst_oid(ltyp);
1014 
1015  arg = transformExpr(pstate, arg, EXPR_KIND_FROM_FUNCTION);
1016  arg = coerce_to_specific_type(pstate, arg, argtype, "TABLESAMPLE");
1017  assign_expr_collations(pstate, arg);
1018  fargs = lappend(fargs, arg);
1019  }
1020  tablesample->args = fargs;
1021 
1022  /* Process REPEATABLE (seed) */
1023  if (rts->repeatable != NULL)
1024  {
1025  Node *arg;
1026 
1027  if (!tsm->repeatable_across_queries)
1028  ereport(ERROR,
1029  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1030  errmsg("tablesample method %s does not support REPEATABLE",
1031  NameListToString(rts->method)),
1032  parser_errposition(pstate, rts->location)));
1033 
1034  arg = transformExpr(pstate, rts->repeatable, EXPR_KIND_FROM_FUNCTION);
1035  arg = coerce_to_specific_type(pstate, arg, FLOAT8OID, "REPEATABLE");
1036  assign_expr_collations(pstate, arg);
1037  tablesample->repeatable = (Expr *) arg;
1038  }
1039  else
1040  tablesample->repeatable = NULL;
1041 
1042  return tablesample;
1043 }
#define NIL
Definition: pg_list.h:69
bool repeatable_across_queries
Definition: tsmapi.h:63
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:850
#define TSM_HANDLEROID
Definition: pg_type.h:712
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1459
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
char * NameListToString(List *names)
Definition: namespace.c:3063
#define INTERNALOID
Definition: pg_type.h:698
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
#define makeNode(_type_)
Definition: nodes.h:558
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
#define FLOAT8OID
Definition: pg_type.h:419
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:1929
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
void * arg
List * parameterTypes
Definition: tsmapi.h:60
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
List* transformSortClause ( ParseState pstate,
List orderlist,
List **  targetlist,
ParseExprKind  exprKind,
bool  useSQL99 
)

Definition at line 2583 of file parse_clause.c.

References addTargetToSortList(), findTargetlistEntrySQL92(), findTargetlistEntrySQL99(), lfirst, NIL, and SortBy::node.

Referenced by transformAggregateCall(), transformSelectStmt(), transformSetOperationStmt(), transformValuesClause(), and transformWindowDefinitions().

2588 {
2589  List *sortlist = NIL;
2590  ListCell *olitem;
2591 
2592  foreach(olitem, orderlist)
2593  {
2594  SortBy *sortby = (SortBy *) lfirst(olitem);
2595  TargetEntry *tle;
2596 
2597  if (useSQL99)
2598  tle = findTargetlistEntrySQL99(pstate, sortby->node,
2599  targetlist, exprKind);
2600  else
2601  tle = findTargetlistEntrySQL92(pstate, sortby->node,
2602  targetlist, exprKind);
2603 
2604  sortlist = addTargetToSortList(pstate, tle,
2605  sortlist, *targetlist, sortby);
2606  }
2607 
2608  return sortlist;
2609 }
#define NIL
Definition: pg_list.h:69
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
static TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
#define lfirst(lc)
Definition: pg_list.h:106
static TargetEntry * findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind)
Node * node
Definition: parsenodes.h:468
Definition: pg_list.h:45
static RangeTblEntry * transformTableEntry ( ParseState pstate,
RangeVar r 
)
static

Definition at line 423 of file parse_clause.c.

References addRangeTableEntry(), RangeVar::alias, and RangeVar::inh.

Referenced by transformFromClauseItem().

424 {
425  RangeTblEntry *rte;
426 
427  /* We need only build a range table entry */
428  rte = addRangeTableEntry(pstate, r, r->alias, r->inh, true);
429 
430  return rte;
431 }
RangeTblEntry * addRangeTableEntry(ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
bool inh
Definition: primnodes.h:69
Alias * alias
Definition: primnodes.h:72
Node* transformWhereClause ( ParseState pstate,
Node clause,
ParseExprKind  exprKind,
const char *  constructName 
)

Definition at line 1715 of file parse_clause.c.

References coerce_to_boolean(), and transformExpr().

Referenced by AlterPolicy(), CreatePolicy(), CreateTrigger(), ParseFuncOrColumn(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformDeleteStmt(), transformIndexStmt(), transformJoinOnClause(), transformOnConflictClause(), transformRuleStmt(), transformSelectStmt(), and transformUpdateStmt().

1717 {
1718  Node *qual;
1719 
1720  if (clause == NULL)
1721  return NULL;
1722 
1723  qual = transformExpr(pstate, clause, exprKind);
1724 
1725  qual = coerce_to_boolean(pstate, qual, constructName);
1726 
1727  return qual;
1728 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:146
Definition: nodes.h:510
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
List* transformWindowDefinitions ( ParseState pstate,
List windowdefs,
List **  targetlist 
)

Definition at line 2616 of file parse_clause.c.

References WindowClause::copiedOrder, copyObject, WindowDef::endOffset, WindowClause::endOffset, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, findWindowClause(), FRAMEOPTION_DEFAULTS, WindowDef::frameOptions, WindowClause::frameOptions, lappend(), lfirst, WindowDef::location, makeNode, WindowDef::name, WindowClause::name, NIL, WindowDef::orderClause, WindowClause::orderClause, parser_errposition(), WindowDef::partitionClause, WindowClause::partitionClause, WindowDef::refname, WindowClause::refname, WindowDef::startOffset, WindowClause::startOffset, transformFrameOffset(), transformGroupClause(), transformSortClause(), and WindowClause::winref.

Referenced by transformSelectStmt().

2619 {
2620  List *result = NIL;
2621  Index winref = 0;
2622  ListCell *lc;
2623 
2624  foreach(lc, windowdefs)
2625  {
2626  WindowDef *windef = (WindowDef *) lfirst(lc);
2627  WindowClause *refwc = NULL;
2628  List *partitionClause;
2629  List *orderClause;
2630  WindowClause *wc;
2631 
2632  winref++;
2633 
2634  /*
2635  * Check for duplicate window names.
2636  */
2637  if (windef->name &&
2638  findWindowClause(result, windef->name) != NULL)
2639  ereport(ERROR,
2640  (errcode(ERRCODE_WINDOWING_ERROR),
2641  errmsg("window \"%s\" is already defined", windef->name),
2642  parser_errposition(pstate, windef->location)));
2643 
2644  /*
2645  * If it references a previous window, look that up.
2646  */
2647  if (windef->refname)
2648  {
2649  refwc = findWindowClause(result, windef->refname);
2650  if (refwc == NULL)
2651  ereport(ERROR,
2652  (errcode(ERRCODE_UNDEFINED_OBJECT),
2653  errmsg("window \"%s\" does not exist",
2654  windef->refname),
2655  parser_errposition(pstate, windef->location)));
2656  }
2657 
2658  /*
2659  * Transform PARTITION and ORDER specs, if any. These are treated
2660  * almost exactly like top-level GROUP BY and ORDER BY clauses,
2661  * including the special handling of nondefault operator semantics.
2662  */
2663  orderClause = transformSortClause(pstate,
2664  windef->orderClause,
2665  targetlist,
2667  true /* force SQL99 rules */ );
2668  partitionClause = transformGroupClause(pstate,
2669  windef->partitionClause,
2670  NULL,
2671  targetlist,
2672  orderClause,
2674  true /* force SQL99 rules */ );
2675 
2676  /*
2677  * And prepare the new WindowClause.
2678  */
2679  wc = makeNode(WindowClause);
2680  wc->name = windef->name;
2681  wc->refname = windef->refname;
2682 
2683  /*
2684  * Per spec, a windowdef that references a previous one copies the
2685  * previous partition clause (and mustn't specify its own). It can
2686  * specify its own ordering clause, but only if the previous one had
2687  * none. It always specifies its own frame clause, and the previous
2688  * one must not have a frame clause. Yeah, it's bizarre that each of
2689  * these cases works differently, but SQL:2008 says so; see 7.11
2690  * <window clause> syntax rule 10 and general rule 1. The frame
2691  * clause rule is especially bizarre because it makes "OVER foo"
2692  * different from "OVER (foo)", and requires the latter to throw an
2693  * error if foo has a nondefault frame clause. Well, ours not to
2694  * reason why, but we do go out of our way to throw a useful error
2695  * message for such cases.
2696  */
2697  if (refwc)
2698  {
2699  if (partitionClause)
2700  ereport(ERROR,
2701  (errcode(ERRCODE_WINDOWING_ERROR),
2702  errmsg("cannot override PARTITION BY clause of window \"%s\"",
2703  windef->refname),
2704  parser_errposition(pstate, windef->location)));
2706  }
2707  else
2708  wc->partitionClause = partitionClause;
2709  if (refwc)
2710  {
2711  if (orderClause && refwc->orderClause)
2712  ereport(ERROR,
2713  (errcode(ERRCODE_WINDOWING_ERROR),
2714  errmsg("cannot override ORDER BY clause of window \"%s\"",
2715  windef->refname),
2716  parser_errposition(pstate, windef->location)));
2717  if (orderClause)
2718  {
2719  wc->orderClause = orderClause;
2720  wc->copiedOrder = false;
2721  }
2722  else
2723  {
2724  wc->orderClause = copyObject(refwc->orderClause);
2725  wc->copiedOrder = true;
2726  }
2727  }
2728  else
2729  {
2730  wc->orderClause = orderClause;
2731  wc->copiedOrder = false;
2732  }
2733  if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2734  {
2735  /*
2736  * Use this message if this is a WINDOW clause, or if it's an OVER
2737  * clause that includes ORDER BY or framing clauses. (We already
2738  * rejected PARTITION BY above, so no need to check that.)
2739  */
2740  if (windef->name ||
2741  orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2742  ereport(ERROR,
2743  (errcode(ERRCODE_WINDOWING_ERROR),
2744  errmsg("cannot copy window \"%s\" because it has a frame clause",
2745  windef->refname),
2746  parser_errposition(pstate, windef->location)));
2747  /* Else this clause is just OVER (foo), so say this: */
2748  ereport(ERROR,
2749  (errcode(ERRCODE_WINDOWING_ERROR),
2750  errmsg("cannot copy window \"%s\" because it has a frame clause",
2751  windef->refname),
2752  errhint("Omit the parentheses in this OVER clause."),
2753  parser_errposition(pstate, windef->location)));
2754  }
2755  wc->frameOptions = windef->frameOptions;
2756  /* Process frame offset expressions */
2757  wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2758  windef->startOffset);
2759  wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2760  windef->endOffset);
2761  wc->winref = winref;
2762 
2763  result = lappend(result, wc);
2764  }
2765 
2766  return result;
2767 }
List * partitionClause
Definition: parsenodes.h:488
char * refname
Definition: parsenodes.h:1282
#define NIL
Definition: pg_list.h:69
bool copiedOrder
Definition: parsenodes.h:1289
int errhint(const char *fmt,...)
Definition: elog.c:987
#define FRAMEOPTION_DEFAULTS
Definition: parsenodes.h:524
int frameOptions
Definition: parsenodes.h:490
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
char * name
Definition: parsenodes.h:486
int errcode(int sqlerrcode)
Definition: elog.c:575
static Node * transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
Node * startOffset
Definition: parsenodes.h:491
#define ERROR
Definition: elog.h:43
List * partitionClause
Definition: parsenodes.h:1283
int location
Definition: parsenodes.h:493
Node * endOffset
Definition: parsenodes.h:492
List * transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * startOffset
Definition: parsenodes.h:1286
List * orderClause
Definition: parsenodes.h:489
unsigned int Index
Definition: c.h:359
#define makeNode(_type_)
Definition: nodes.h:558
#define lfirst(lc)
Definition: pg_list.h:106
Node * endOffset
Definition: parsenodes.h:1287
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
List * orderClause
Definition: parsenodes.h:1284
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * refname
Definition: parsenodes.h:487
#define copyObject(obj)
Definition: nodes.h:623
Definition: pg_list.h:45
static WindowClause * findWindowClause(List *wclist, const char *name)