36 #define MAX_FUZZY_DISTANCE 3
39 const char *refname,
int location);
47 int rtindex,
int sublevels_up,
48 int location,
bool include_dropped,
51 int count,
int offset,
52 int rtindex,
int sublevels_up,
53 int location,
bool include_dropped,
82 const char *schemaname,
92 if (schemaname !=
NULL)
112 while (pstate !=
NULL)
173 (
errcode(ERRCODE_AMBIGUOUS_ALIAS),
174 errmsg(
"table reference \"%s\" is ambiguous",
212 rte->
relid == relid &&
217 (
errcode(ERRCODE_AMBIGUOUS_ALIAS),
218 errmsg(
"table reference %u is ambiguous",
250 if (strcmp(cte->
ctename, refname) == 0)
252 *ctelevelsup = levelsup;
276 if (strcmp(cte->
ctename, refname) == 0)
301 const char *refname = relation->
relname;
304 Index ctelevelsup = 0;
342 strcmp(rte->
ctename, refname) == 0)
372 foreach(l1, namespace1)
382 foreach(l2, namespace2)
396 (
errcode(ERRCODE_DUPLICATE_ALIAS),
397 errmsg(
"table name \"%s\" specified more than once",
423 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
424 errmsg(
"invalid reference to FROM-clause entry for table \"%s\"",
427 errhint(
"There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
429 errdetail(
"The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
449 while (pstate !=
NULL)
465 elog(
ERROR,
"RTE not found (internal error)");
478 while (sublevels_up-- > 0)
506 while (levelsup-- > 0)
531 const char *actual,
const char *match,
int attnum)
537 if (fuzzy_rte_penalty > fuzzystate->
distance)
544 if (actual[0] ==
'\0')
548 matchlen = strlen(match);
560 if (columndistance > matchlen / 2)
567 columndistance += fuzzy_rte_penalty;
573 if (columndistance < fuzzystate->distance)
576 fuzzystate->
distance = columndistance;
578 fuzzystate->
first = attnum;
582 else if (columndistance == fuzzystate->
distance)
599 fuzzystate->
distance = columndistance - 1;
605 fuzzystate->
second = attnum;
615 fuzzystate->
first = attnum;
634 int location,
int fuzzy_rte_penalty,
662 if (strcmp(attcolname, colname) == 0)
666 (
errcode(ERRCODE_AMBIGUOUS_COLUMN),
667 errmsg(
"column reference \"%s\" is ambiguous",
670 var =
make_var(pstate, rte, attnum, location);
673 result = (
Node *) var;
677 if (fuzzystate !=
NULL)
679 rte, attcolname, colname, attnum);
704 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
705 errmsg(
"system column \"%s\" reference in check constraint is invalid",
716 var =
make_var(pstate, rte, attnum, location);
719 result = (
Node *) var;
741 while (pstate !=
NULL)
766 (
errcode(ERRCODE_AMBIGUOUS_COLUMN),
767 errmsg(
"column reference \"%s\" is ambiguous",
775 if (result !=
NULL || localonly)
821 while (pstate !=
NULL)
828 int fuzzy_rte_penalty = 0;
859 fuzzy_rte_penalty, fuzzystate)
860 && fuzzy_rte_penalty == 0)
916 elog(
ERROR,
"could not find JoinExpr for whole-row reference");
933 elog(
ERROR,
"unrecognized node type: %d",
948 elog(
ERROR,
"unrecognized node type: %d",
967 if (aliasvar &&
IsA(aliasvar,
Var))
1011 int maxattrs = tupdesc->
natts;
1032 for (varattno = 0; varattno < maxattrs; varattno++)
1037 if (attr->attisdropped)
1049 aliaslc =
lnext(aliaslc);
1064 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1065 errmsg(
"table \"%s\" has %d columns available but %d columns specified",
1066 eref->
aliasname, maxattrs - numdropped, numaliases)));
1084 Alias *alias,
int nfuncs)
1104 if (nfuncs == 1 && alias)
1137 errmsg(
"relation \"%s.%s\" does not exist",
1150 errmsg(
"relation \"%s\" does not exist",
1152 errdetail(
"There is a WITH item named \"%s\", but it cannot be referenced from this part of the query.",
1154 errhint(
"Use WITH RECURSIVE, or re-order the WITH items to remove forward references.")));
1158 errmsg(
"relation \"%s\" does not exist",
1334 if (varattno > numaliases)
1342 if (varattno < numaliases)
1344 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1345 errmsg(
"table \"%s\" has %d columns available but %d columns specified",
1346 refname, varattno, numaliases)));
1432 forthree(lc1, funcexprs, lc2, funcnames, lc3, coldeflists)
1435 char *funcname = (
char *)
lfirst(lc2);
1460 if (coldeflist !=
NIL)
1464 (
errcode(ERRCODE_SYNTAX_ERROR),
1465 errmsg(
"a column definition list is only allowed for functions returning \"record\""),
1473 (
errcode(ERRCODE_SYNTAX_ERROR),
1474 errmsg(
"a column definition list is required for functions returning \"record\""),
1505 foreach(col, coldeflist)
1516 (
errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1517 errmsg(
"column \"%s\" cannot be declared SETOF",
1521 &attrtype, &attrtypmod);
1552 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1553 errmsg(
"function \"%s\" in FROM has unsupported return type %s",
1562 functupdescs[funcno] = tupdesc;
1563 totalatts += tupdesc->
natts;
1579 for (i = 0; i < nfuncs; i++)
1581 for (j = 1; j <= functupdescs[
i]->
natts; j++)
1594 Assert(natts == totalatts);
1599 tupdesc = functupdescs[0];
1663 while (numaliases < numcolumns)
1668 snprintf(attrname,
sizeof(attrname),
"column%d", numaliases);
1672 if (numcolumns < numaliases)
1674 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1675 errmsg(
"VALUES lists \"%s\" have %d columns available but %d columns specified",
1676 refname, numcolumns, numaliases)));
1729 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1730 errmsg(
"joins can have at most %d columns",
1819 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1820 errmsg(
"WITH query \"%s\" does not have a RETURNING clause",
1841 if (varattno > numaliases)
1844 if (varattno < numaliases)
1846 (
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1847 errmsg(
"table \"%s\" has %d columns available but %d columns specified",
1848 refname, varattno, numaliases)));
1916 if (strcmp(refname, thisrel->
relname) == 0)
1937 bool addToRelNameSpace,
bool addToVarNameSpace)
1947 if (addToRelNameSpace || addToVarNameSpace)
1952 nsitem->
p_rte = rte;
1980 int location,
bool include_dropped,
1995 rtindex, sublevels_up, location,
1996 include_dropped, colnames, colvars);
2020 aliasp_item =
lnext(aliasp_item);
2027 varnode =
makeVar(rtindex, varattno,
2034 *colvars =
lappend(*colvars, varnode);
2061 rtindex, sublevels_up, location,
2062 include_dropped, colnames, colvars);
2068 *colnames =
lappend(*colnames,
2076 varnode =
makeVar(rtindex, atts_done + 1,
2082 *colvars =
lappend(*colvars, varnode);
2104 int attnum = atts_done;
2123 *colvars =
lappend(*colvars, varnode);
2130 elog(
ERROR,
"function in FROM has unsupported return type");
2139 *colnames =
lappend(*colnames,
2151 *colvars =
lappend(*colvars, varnode);
2176 *colnames =
lappend(*colnames,
2178 aliasp_item =
lnext(aliasp_item);
2185 varnode =
makeVar(rtindex, varattno,
2191 *colvars =
lappend(*colvars, varnode);
2221 if (include_dropped)
2224 *colnames =
lappend(*colnames,
2245 *colnames =
lappend(*colnames,
2253 varnode =
makeVar(rtindex, varattno,
2260 *colvars =
lappend(*colvars, varnode);
2289 aliasp_item =
lnext(aliasp_item);
2296 varnode =
makeVar(rtindex, varattno,
2297 coltype, coltypmod, colcoll,
2299 *colvars =
lappend(*colvars, varnode);
2314 int location,
bool include_dropped,
2322 rtindex, sublevels_up,
2323 location, include_dropped,
2339 int rtindex,
int sublevels_up,
2340 int location,
bool include_dropped,
2350 for (i = 0; i < offset; i++)
2353 aliascell =
lnext(aliascell);
2357 Assert(count <= tupdesc->natts);
2358 for (varattno = 0; varattno < count; varattno++)
2362 if (attr->attisdropped)
2364 if (include_dropped)
2379 aliascell =
lnext(aliascell);
2390 aliascell =
lnext(aliascell);
2395 label =
NameStr(attr->attname);
2404 varnode =
makeVar(rtindex, varattno + offset + 1,
2405 attr->atttypid, attr->atttypmod,
2410 *colvars =
lappend(*colvars, varnode);
2427 int rtindex,
int sublevels_up,
int location)
2435 expandRTE(rte, rtindex, sublevels_up, location,
false,
2445 forboth(name, names, var, vars)
2455 te_list =
lappend(te_list, te);
2506 elog(
ERROR,
"invalid attnum %d for rangetable entry %s",
2531 elog(
ERROR,
"cache lookup failed for attribute %d of relation %u",
2532 attnum, rte->
relid);
2539 if (att_tup->attisdropped)
2541 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2542 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2545 *vartype = att_tup->atttypid;
2546 *vartypmod = att_tup->atttypmod;
2547 *varcollid = att_tup->attcollation;
2558 elog(
ERROR,
"subquery %s does not have attribute %d",
2576 if (attnum > atts_done &&
2577 attnum <= atts_done + rtfunc->funccolcount)
2583 attnum -= atts_done;
2594 Assert(attnum <= tupdesc->natts);
2595 att_tup = tupdesc->
attrs[attnum - 1];
2601 if (att_tup->attisdropped)
2603 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2604 errmsg(
"column \"%s\" of relation \"%s\" does not exist",
2607 *vartype = att_tup->atttypid;
2608 *vartypmod = att_tup->atttypmod;
2609 *varcollid = att_tup->attcollation;
2614 *vartype = funcrettype;
2633 elog(
ERROR,
"function in FROM has unsupported return type");
2651 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2652 errmsg(
"column %d of relation \"%s\" does not exist",
2665 elog(
ERROR,
"values list %s does not have attribute %d",
2725 elog(
ERROR,
"cache lookup failed for attribute %d of relation %u",
2726 attnum, rte->
relid);
2728 result = att_tup->attisdropped;
2751 elog(
ERROR,
"invalid varattno %d", attnum);
2754 result = (aliasvar ==
NULL);
2774 if (attnum > atts_done &&
2775 attnum <= atts_done + rtfunc->funccolcount)
2790 Assert(attnum - atts_done <= tupdesc->natts);
2791 att_tup = tupdesc->
attrs[attnum - atts_done - 1];
2792 return att_tup->attisdropped;
2806 (
errcode(ERRCODE_UNDEFINED_COLUMN),
2807 errmsg(
"column %d of relation \"%s\" does not exist",
2838 if (tle->
resno == resno)
2858 if (rc->
rti == rtindex)
2878 for (i = 0; i < rd->
rd_rel->relnatts; i++)
2882 if (
namestrcmp(&(att->attname), attname) == 0 && !att->attisdropped)
2916 return sysatt->attnum;
2936 return &sysatt->attname;
2939 elog(
ERROR,
"invalid attribute number %d", attid);
2958 return sysatt->atttypid;
2961 elog(
ERROR,
"invalid attribute number %d", attid);
2979 elog(
ERROR,
"invalid attribute number %d", attid);
2980 return rd->
rd_att->
attrs[attid - 1]->attcollation;
2994 const char *badAlias =
NULL;
3013 if (rte && rte->
alias &&
3017 &sublevels_up) == rte)
3023 errmsg(
"invalid reference to FROM-clause entry for table \"%s\"",
3026 errhint(
"Perhaps you meant to reference the table alias \"%s\".",
3028 errhint(
"There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
3034 errmsg(
"missing FROM-clause entry for table \"%s\"",
3047 char *
relname,
char *colname,
int location)
3050 char *closestfirst =
NULL;
3081 (
errcode(ERRCODE_UNDEFINED_COLUMN),
3083 errmsg(
"column %s.%s does not exist", relname, colname) :
3084 errmsg(
"column \"%s\" does not exist", colname),
3085 state->
rfirst ? closestfirst ?
3086 errhint(
"Perhaps you meant to reference the column \"%s.%s\".",
3088 errhint(
"There is a column named \"%s\" in table \"%s\", but it cannot be referenced from this part of the query.",
3095 char *closestsecond;
3101 (
errcode(ERRCODE_UNDEFINED_COLUMN),
3103 errmsg(
"column %s.%s does not exist", relname, colname) :
3104 errmsg(
"column \"%s\" does not exist", colname),
3105 errhint(
"Perhaps you meant to reference the column \"%s.%s\" or the column \"%s.%s\".",
3134 foreach(rtable, query->
rtable)
3141 char relpersistence = rel->
rd_rel->relpersistence;
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
Value * makeString(char *str)
Oid list_nth_oid(const List *list, int n)
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
static bool isQueryUsingTempRelation_walker(Node *node, void *context)
static RangeTblEntry * scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
Name attnumAttName(Relation rd, int attid)
#define IsA(nodeptr, _type_)
int errhint(const char *fmt,...)
#define forboth(cell1, list1, cell2, list2)
#define ERRCODE_UNDEFINED_TABLE
bool isQueryUsingTempRelation(Query *query)
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
int exprLocation(const Node *expr)
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, bool allow_system_table_mods)
RangeTblEntry * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, JoinType jointype, List *aliasvars, Alias *alias, bool inFromCl)
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)
void markVarForSelectPriv(ParseState *pstate, Var *var, RangeTblEntry *rte)
#define ObjectIdAttributeNumber
int32 exprTypmod(const Node *expr)
static char * chooseScalarFunctionAlias(Node *funcexpr, char *funcname, Alias *alias, int nfuncs)
static bool isFutureCTE(ParseState *pstate, const char *refname)
#define RangeVarGetRelid(relation, lockmode, missing_ok)
void get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum, Oid *vartype, int32 *vartypmod, Oid *varcollid)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
char * pstrdup(const char *in)
static void updateFuzzyAttrMatchState(int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate, RangeTblEntry *rte, const char *actual, const char *match, int attnum)
Form_pg_attribute * attrs
List * list_truncate(List *list, int new_size)
Form_pg_attribute SystemAttributeByName(const char *attname, bool relhasoids)
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
Relation parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
int errcode(int sqlerrcode)
int namestrcmp(Name name, const char *str)
void relation_close(Relation relation, LOCKMODE lockmode)
List * list_concat(List *list1, List *list2)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define FirstLowInvalidHeapAttributeNumber
static int specialAttNum(const char *attname)
List * list_copy_tail(const List *oldlist, int nskip)
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
List * lappend_oid(List *list, Oid datum)
#define OidIsValid(objectId)
RangeTblEntry * refnameRangeTblEntry(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
#define RELKIND_COMPOSITE_TYPE
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
bool p_locked_from_parent
void * copyObject(const void *from)
static char * relname(char const *dir, char const *base)
Alias * makeAlias(const char *aliasname, List *colnames)
Relation heap_openrv_extended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
static RangeTblEntry * searchRangeTableForRel(ParseState *pstate, RangeVar *relation)
#define ObjectIdGetDatum(X)
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem, int location)
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Oid get_relname_relid(const char *relname, Oid relnamespace)
void * list_nth(const List *list, int n)
#define MAX_FUZZY_DISTANCE
int errdetail(const char *fmt,...)
RangeTblEntry * addRangeTableEntry(ParseState *pstate, RangeVar *relation, Alias *alias, bool inh, bool inFromCl)
Oid attnumTypeId(Relation rd, int attid)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
#define RelationGetRelationName(relation)
static ListCell * list_head(const List *l)
FormData_pg_attribute * Form_pg_attribute
#define TableOidAttributeNumber
void checkNameSpaceConflicts(ParseState *pstate, List *namespace1, List *namespace2)
List * expandRelAttrs(ParseState *pstate, RangeTblEntry *rte, int rtindex, int sublevels_up, int location)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
#define ereport(elevel, rest)
#define rt_fetch(rangetable_index, rangetable)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
List * lappend_int(List *list, int datum)
void errorMissingColumn(ParseState *pstate, char *relname, char *colname, int location)
List * lappend(List *list, void *datum)
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col)
RangeTblEntry * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
char * get_relid_attribute_name(Oid relid, AttrNumber attnum)
struct ParseState * parentParseState
#define AttributeNumberIsValid(attributeNumber)
int list_nth_int(const List *list, int n)
void ReleaseSysCache(HeapTuple tuple)
Oid attnumCollationId(Relation rd, int attid)
bool get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
RangeTblEntry * p_target_rangetblentry
Relation heap_open(Oid relationId, LOCKMODE lockmode)
ParseExprKind p_expr_kind
RangeTblEntry * addRangeTableEntryForValues(ParseState *pstate, List *exprs, List *collations, Alias *alias, bool lateral, bool inFromCl)
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
static RangeTblEntry * scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
RangeTblEntry * addRangeTableEntryForFunction(ParseState *pstate, List *funcnames, List *funcexprs, List *coldeflists, RangeFunction *rangefunc, bool lateral, bool inFromCl)
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Node * colNameToVar(ParseState *pstate, char *colname, bool localonly, int location)
CommonTableExpr * scanNameSpaceForCTE(ParseState *pstate, const char *refname, Index *ctelevelsup)
#define HeapTupleIsValid(tuple)
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
#define Assert(condition)
Oid exprType(const Node *expr)
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
static int list_length(const List *l)
int parser_errposition(ParseState *pstate, int location)
Oid exprCollation(const Node *expr)
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
#define SearchSysCacheExists2(cacheId, key1, key2)
Bitmapset * bms_add_member(Bitmapset *a, int x)
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
#define InvalidAttrNumber
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
int errmsg(const char *fmt,...)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
static void buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref)
RangeTblEntry * addRangeTableEntryForCTE(ParseState *pstate, CommonTableExpr *cte, Index levelsup, RangeVar *rv, bool inFromCl)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
bool isLockedRefname(ParseState *pstate, const char *refname)
#define RELPERSISTENCE_TEMP
char * get_rel_name(Oid relid)
static FuzzyAttrMatchState * searchRangeTableForCol(ParseState *pstate, const char *alias, char *colname, int location)
#define RelationGetRelid(relation)
Oid LookupNamespaceNoError(const char *nspname)
char * get_func_result_name(Oid functionId)
#define QTW_IGNORE_JOINALIASES
Node * strip_implicit_coercions(Node *node)
#define SearchSysCache2(cacheId, key1, key2)