aboutsummaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_target.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2008-10-04 21:56:55 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2008-10-04 21:56:55 +0000
commit44d5be0e5308e951c0c5dc522b4bcacf2bcbc476 (patch)
tree516f1c70436225751f631e7e686f7ea61b3db9df /src/backend/parser/parse_target.c
parent607b2be7bb230ea4c558cb3101794f94de35ab85 (diff)
downloadpostgresql-44d5be0e5308e951c0c5dc522b4bcacf2bcbc476.tar.gz
postgresql-44d5be0e5308e951c0c5dc522b4bcacf2bcbc476.zip
Implement SQL-standard WITH clauses, including WITH RECURSIVE.
There are some unimplemented aspects: recursive queries must use UNION ALL (should allow UNION too), and we don't have SEARCH or CYCLE clauses. These might or might not get done for 8.4, but even without them it's a pretty useful feature. There are also a couple of small loose ends and definitional quibbles, which I'll send a memo about to pgsql-hackers shortly. But let's land the patch now so we can get on with other development. Yoshiyuki Asaba, with lots of help from Tatsuo Ishii and Tom Lane
Diffstat (limited to 'src/backend/parser/parse_target.c')
-rw-r--r--src/backend/parser/parse_target.c58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c
index 20d91a142f5..3ead26194e3 100644
--- a/src/backend/parser/parse_target.c
+++ b/src/backend/parser/parse_target.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.164 2008/09/01 20:42:44 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.165 2008/10/04 21:56:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -296,6 +296,24 @@ markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
case RTE_VALUES:
/* not a simple relation, leave it unmarked */
break;
+ case RTE_CTE:
+ /* CTE reference: copy up from the subquery */
+ if (attnum != InvalidAttrNumber)
+ {
+ CommonTableExpr *cte = GetCTEForRTE(pstate, rte);
+ TargetEntry *ste;
+
+ /* should be analyzed by now */
+ Assert(IsA(cte->ctequery, Query));
+ ste = get_tle_by_resno(((Query *) cte->ctequery)->targetList,
+ attnum);
+ if (ste == NULL || ste->resjunk)
+ elog(ERROR, "subquery %s does not have attribute %d",
+ rte->eref->aliasname, attnum);
+ tle->resorigtbl = ste->resorigtbl;
+ tle->resorigcol = ste->resorigcol;
+ }
+ break;
}
}
@@ -1176,6 +1194,44 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
* its result columns as RECORD, which is not allowed.
*/
break;
+ case RTE_CTE:
+ {
+ /* CTE reference: examine subquery's output expr */
+ CommonTableExpr *cte = GetCTEForRTE(pstate, rte);
+ TargetEntry *ste;
+
+ /* should be analyzed by now */
+ Assert(IsA(cte->ctequery, Query));
+ ste = get_tle_by_resno(((Query *) cte->ctequery)->targetList,
+ attnum);
+ if (ste == NULL || ste->resjunk)
+ elog(ERROR, "subquery %s does not have attribute %d",
+ rte->eref->aliasname, attnum);
+ expr = (Node *) ste->expr;
+ if (IsA(expr, Var))
+ {
+ /*
+ * Recurse into the CTE to see what its Var refers to. We
+ * have to build an additional level of ParseState to keep
+ * in step with varlevelsup in the CTE; furthermore it
+ * could be an outer CTE.
+ */
+ ParseState mypstate;
+ Index levelsup;
+
+ MemSet(&mypstate, 0, sizeof(mypstate));
+ /* this loop must work, since GetCTEForRTE did */
+ for (levelsup = 0; levelsup < rte->ctelevelsup; levelsup++)
+ pstate = pstate->parentParseState;
+ mypstate.parentParseState = pstate;
+ mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
+ /* don't bother filling the rest of the fake pstate */
+
+ return expandRecordVariable(&mypstate, (Var *) expr, 0);
+ }
+ /* else fall through to inspect the expression */
+ }
+ break;
}
/*