mirror of
https://github.com/postgres/postgres.git
synced 2025-05-03 22:24:49 +03:00
Fix WITH attached to a nested set operation (UNION/INTERSECT/EXCEPT).
Parse analysis neglected to cover the case of a WITH clause attached to an intermediate-level set operation; it only handled WITH at the top level or WITH attached to a leaf-level SELECT. Per report from Adam Mackler. In HEAD, I rearranged the order of SelectStmt's fields to put withClause with the other fields that can appear on non-leaf SelectStmts. In back branches, leave it alone to avoid a possible ABI break for third-party code. Back-patch to 8.4 where WITH support was added.
This commit is contained in:
parent
63aba79c7f
commit
3786b9b466
@ -1322,6 +1322,7 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
|||||||
Node *limitOffset;
|
Node *limitOffset;
|
||||||
Node *limitCount;
|
Node *limitCount;
|
||||||
List *lockingClause;
|
List *lockingClause;
|
||||||
|
WithClause *withClause;
|
||||||
Node *node;
|
Node *node;
|
||||||
ListCell *left_tlist,
|
ListCell *left_tlist,
|
||||||
*lct,
|
*lct,
|
||||||
@ -1338,14 +1339,6 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
|||||||
|
|
||||||
qry->commandType = CMD_SELECT;
|
qry->commandType = CMD_SELECT;
|
||||||
|
|
||||||
/* process the WITH clause independently of all else */
|
|
||||||
if (stmt->withClause)
|
|
||||||
{
|
|
||||||
qry->hasRecursive = stmt->withClause->recursive;
|
|
||||||
qry->cteList = transformWithClause(pstate, stmt->withClause);
|
|
||||||
qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find leftmost leaf SelectStmt. We currently only need to do this in
|
* Find leftmost leaf SelectStmt. We currently only need to do this in
|
||||||
* order to deliver a suitable error message if there's an INTO clause
|
* order to deliver a suitable error message if there's an INTO clause
|
||||||
@ -1375,11 +1368,13 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
|||||||
limitOffset = stmt->limitOffset;
|
limitOffset = stmt->limitOffset;
|
||||||
limitCount = stmt->limitCount;
|
limitCount = stmt->limitCount;
|
||||||
lockingClause = stmt->lockingClause;
|
lockingClause = stmt->lockingClause;
|
||||||
|
withClause = stmt->withClause;
|
||||||
|
|
||||||
stmt->sortClause = NIL;
|
stmt->sortClause = NIL;
|
||||||
stmt->limitOffset = NULL;
|
stmt->limitOffset = NULL;
|
||||||
stmt->limitCount = NULL;
|
stmt->limitCount = NULL;
|
||||||
stmt->lockingClause = NIL;
|
stmt->lockingClause = NIL;
|
||||||
|
stmt->withClause = NULL;
|
||||||
|
|
||||||
/* We don't support FOR UPDATE/SHARE with set ops at the moment. */
|
/* We don't support FOR UPDATE/SHARE with set ops at the moment. */
|
||||||
if (lockingClause)
|
if (lockingClause)
|
||||||
@ -1387,6 +1382,14 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
|
|||||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||||
errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
||||||
|
|
||||||
|
/* Process the WITH clause independently of all else */
|
||||||
|
if (withClause)
|
||||||
|
{
|
||||||
|
qry->hasRecursive = withClause->recursive;
|
||||||
|
qry->cteList = transformWithClause(pstate, withClause);
|
||||||
|
qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Recursively transform the components of the tree.
|
* Recursively transform the components of the tree.
|
||||||
*/
|
*/
|
||||||
@ -1572,10 +1575,10 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
|
|||||||
errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If an internal node of a set-op tree has ORDER BY, LIMIT, or FOR UPDATE
|
* If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
|
||||||
* clauses attached, we need to treat it like a leaf node to generate an
|
* or WITH clauses attached, we need to treat it like a leaf node to
|
||||||
* independent sub-Query tree. Otherwise, it can be represented by a
|
* generate an independent sub-Query tree. Otherwise, it can be
|
||||||
* SetOperationStmt node underneath the parent Query.
|
* represented by a SetOperationStmt node underneath the parent Query.
|
||||||
*/
|
*/
|
||||||
if (stmt->op == SETOP_NONE)
|
if (stmt->op == SETOP_NONE)
|
||||||
{
|
{
|
||||||
@ -1586,7 +1589,7 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
|
|||||||
{
|
{
|
||||||
Assert(stmt->larg != NULL && stmt->rarg != NULL);
|
Assert(stmt->larg != NULL && stmt->rarg != NULL);
|
||||||
if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
|
if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
|
||||||
stmt->lockingClause)
|
stmt->lockingClause || stmt->withClause)
|
||||||
isLeaf = true;
|
isLeaf = true;
|
||||||
else
|
else
|
||||||
isLeaf = false;
|
isLeaf = false;
|
||||||
|
@ -678,6 +678,18 @@ checkWellFormedRecursion(CteState *cstate)
|
|||||||
if (cstate->selfrefcount != 1) /* shouldn't happen */
|
if (cstate->selfrefcount != 1) /* shouldn't happen */
|
||||||
elog(ERROR, "missing recursive reference");
|
elog(ERROR, "missing recursive reference");
|
||||||
|
|
||||||
|
/* WITH mustn't contain self-reference, either */
|
||||||
|
if (stmt->withClause)
|
||||||
|
{
|
||||||
|
cstate->curitem = i;
|
||||||
|
cstate->innerwiths = NIL;
|
||||||
|
cstate->selfrefcount = 0;
|
||||||
|
cstate->context = RECURSION_SUBLINK;
|
||||||
|
checkWellFormedRecursionWalker((Node *) stmt->withClause->ctes,
|
||||||
|
cstate);
|
||||||
|
Assert(cstate->innerwiths == NIL);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Disallow ORDER BY and similar decoration atop the UNION. These
|
* Disallow ORDER BY and similar decoration atop the UNION. These
|
||||||
* don't make sense because it's impossible to figure out what they
|
* don't make sense because it's impossible to figure out what they
|
||||||
@ -933,7 +945,7 @@ checkWellFormedSelectStmt(SelectStmt *stmt, CteState *cstate)
|
|||||||
cstate);
|
cstate);
|
||||||
checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
|
checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
|
||||||
cstate);
|
cstate);
|
||||||
break;
|
/* stmt->withClause is intentionally ignored here */
|
||||||
break;
|
break;
|
||||||
case SETOP_EXCEPT:
|
case SETOP_EXCEPT:
|
||||||
if (stmt->all)
|
if (stmt->all)
|
||||||
@ -952,6 +964,7 @@ checkWellFormedSelectStmt(SelectStmt *stmt, CteState *cstate)
|
|||||||
cstate);
|
cstate);
|
||||||
checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
|
checkWellFormedRecursionWalker((Node *) stmt->lockingClause,
|
||||||
cstate);
|
cstate);
|
||||||
|
/* stmt->withClause is intentionally ignored here */
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
elog(ERROR, "unrecognized set op: %d",
|
elog(ERROR, "unrecognized set op: %d",
|
||||||
|
@ -1160,6 +1160,57 @@ SELECT * FROM t;
|
|||||||
10
|
10
|
||||||
(55 rows)
|
(55 rows)
|
||||||
|
|
||||||
|
--
|
||||||
|
-- test WITH attached to intermediate-level set operation
|
||||||
|
--
|
||||||
|
WITH outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM innermost
|
||||||
|
UNION SELECT 3)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
x
|
||||||
|
---
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
(3 rows)
|
||||||
|
|
||||||
|
WITH outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM outermost -- fail
|
||||||
|
UNION SELECT * FROM innermost)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
ERROR: relation "outermost" does not exist
|
||||||
|
LINE 4: SELECT * FROM outermost
|
||||||
|
^
|
||||||
|
DETAIL: There is a WITH item named "outermost", but it cannot be referenced from this part of the query.
|
||||||
|
HINT: Use WITH RECURSIVE, or re-order the WITH items to remove forward references.
|
||||||
|
WITH RECURSIVE outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM outermost
|
||||||
|
UNION SELECT * FROM innermost)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
x
|
||||||
|
---
|
||||||
|
1
|
||||||
|
2
|
||||||
|
(2 rows)
|
||||||
|
|
||||||
|
WITH RECURSIVE outermost(x) AS (
|
||||||
|
WITH innermost as (SELECT 2 FROM outermost) -- fail
|
||||||
|
SELECT * FROM innermost
|
||||||
|
UNION SELECT * from outermost
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
ERROR: recursive reference to query "outermost" must not appear within a subquery
|
||||||
|
LINE 2: WITH innermost as (SELECT 2 FROM outermost)
|
||||||
|
^
|
||||||
--
|
--
|
||||||
-- Data-modifying statements in WITH
|
-- Data-modifying statements in WITH
|
||||||
--
|
--
|
||||||
|
@ -539,6 +539,41 @@ WITH RECURSIVE t(j) AS (
|
|||||||
)
|
)
|
||||||
SELECT * FROM t;
|
SELECT * FROM t;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- test WITH attached to intermediate-level set operation
|
||||||
|
--
|
||||||
|
|
||||||
|
WITH outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM innermost
|
||||||
|
UNION SELECT 3)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
|
||||||
|
WITH outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM outermost -- fail
|
||||||
|
UNION SELECT * FROM innermost)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
|
||||||
|
WITH RECURSIVE outermost(x) AS (
|
||||||
|
SELECT 1
|
||||||
|
UNION (WITH innermost as (SELECT 2)
|
||||||
|
SELECT * FROM outermost
|
||||||
|
UNION SELECT * FROM innermost)
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
|
||||||
|
WITH RECURSIVE outermost(x) AS (
|
||||||
|
WITH innermost as (SELECT 2 FROM outermost) -- fail
|
||||||
|
SELECT * FROM innermost
|
||||||
|
UNION SELECT * from outermost
|
||||||
|
)
|
||||||
|
SELECT * FROM outermost;
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Data-modifying statements in WITH
|
-- Data-modifying statements in WITH
|
||||||
--
|
--
|
||||||
|
Loading…
x
Reference in New Issue
Block a user