* Add an entry for a subquery to the pstate's range table (p_rtable). * Then, construct and return a ParseNamespaceItem for the new RTE. * * This is much like addRangeTableEntry() except that it makes a subquery RTE. * Note that an alias clause *must* be supplied. */
| 1652 | * Note that an alias clause *must* be supplied. |
| 1653 | */ |
| 1654 | ParseNamespaceItem * |
| 1655 | addRangeTableEntryForSubquery(ParseState *pstate, |
| 1656 | Query *subquery, |
| 1657 | Alias *alias, |
| 1658 | bool lateral, |
| 1659 | bool inFromCl) |
| 1660 | { |
| 1661 | RangeTblEntry *rte = makeNode(RangeTblEntry); |
| 1662 | char *refname = alias->aliasname; |
| 1663 | Alias *eref; |
| 1664 | int numaliases; |
| 1665 | List *coltypes, |
| 1666 | *coltypmods, |
| 1667 | *colcollations; |
| 1668 | int varattno; |
| 1669 | ListCell *tlistitem; |
| 1670 | |
| 1671 | rte->rtekind = RTE_SUBQUERY; |
| 1672 | rte->subquery = subquery; |
| 1673 | rte->alias = alias; |
| 1674 | |
| 1675 | eref = copyObject(alias); |
| 1676 | numaliases = list_length(eref->colnames); |
| 1677 | |
| 1678 | /* fill in any unspecified alias columns, and extract column type info */ |
| 1679 | coltypes = coltypmods = colcollations = NIL; |
| 1680 | varattno = 0; |
| 1681 | foreach(tlistitem, subquery->targetList) |
| 1682 | { |
| 1683 | TargetEntry *te = (TargetEntry *) lfirst(tlistitem); |
| 1684 | |
| 1685 | if (te->resjunk) |
| 1686 | continue; |
| 1687 | varattno++; |
| 1688 | Assert(varattno == te->resno); |
| 1689 | if (varattno > numaliases) |
| 1690 | { |
| 1691 | char *attrname; |
| 1692 | |
| 1693 | attrname = pstrdup(te->resname); |
| 1694 | eref->colnames = lappend(eref->colnames, makeString(attrname)); |
| 1695 | } |
| 1696 | coltypes = lappend_oid(coltypes, |
| 1697 | exprType((Node *) te->expr)); |
| 1698 | coltypmods = lappend_int(coltypmods, |
| 1699 | exprTypmod((Node *) te->expr)); |
| 1700 | colcollations = lappend_oid(colcollations, |
| 1701 | exprCollation((Node *) te->expr)); |
| 1702 | } |
| 1703 | if (varattno < numaliases) |
| 1704 | ereport(ERROR, |
| 1705 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
| 1706 | errmsg("table \"%s\" has %d columns available but %d columns specified", |
| 1707 | refname, varattno, numaliases))); |
| 1708 | |
| 1709 | rte->eref = eref; |
| 1710 | |
| 1711 | /* |
no test coverage detected