1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-11 01:42:22 +03:00

The query flattener should not run if the subquery is a compound that contains

a RIGHT JOIN in any arm and the subquery is not the first element of the
outer query.  Otherwise, prior elements of the outer query will not have
the JT_LTORJ flag set.  Fix for the problem reported in
[forum:/forumpost/174afeae5734d42d|forum post 174afeae5734d42d].

FossilOrigin-Name: 274e244c85935084b2f0f85176283f018bf9b74e7703f985bd5a2f6f8bdcff5d
This commit is contained in:
drh
2022-07-13 15:52:15 +00:00
parent 4413ec7255
commit b88bf865c3
4 changed files with 40 additions and 12 deletions

View File

@@ -4120,6 +4120,9 @@ static void renumberCursors(
** (17d2) DISTINCT
** (17e) the subquery may not contain window functions, and
** (17f) the subquery must not be the RHS of a LEFT JOIN.
** (17g) either the subquery is the first element of the outer
** query or there are no RIGHT or FULL JOINs in any arm
** of the subquery. (This is a duplicate of condition (27b).)
**
** The parent and sub-query may contain WHERE clauses. Subject to
** rules (11), (13) and (14), they may also contain ORDER BY,
@@ -4171,7 +4174,11 @@ static void renumberCursors(
** See also (3) for restrictions on LEFT JOIN.
**
** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
** is the first element of the parent query.
** is the first element of the parent query. This must be the
** the case if:
** (27a) the subquery is not compound query, and
** (27b) the subquery is a compound query and the RIGHT JOIN occurs
** in any arm of the compound query. (See also (17g).)
**
** (28) The subquery is not a MATERIALIZED CTE.
**
@@ -4296,7 +4303,7 @@ static int flattenSubquery(
assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
return 0; /* Restriction (27) */
return 0; /* Restriction (27a) */
}
if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
return 0; /* (28) */
@@ -4316,7 +4323,7 @@ static int flattenSubquery(
** NATURAL join or a join that as an ON or USING clause.
**
** These conditions are sufficient to keep an EP_OuterON from being
** flattened into an EP_InnerON. Restrictions (3a) and (27) prevent
** flattened into an EP_InnerON. Restrictions (3a) and (27a) prevent
** an EP_InnerON from being flattened into an EP_OuterON.
*/
if( pSubSrc->nSrc>=2
@@ -4358,6 +4365,12 @@ static int flattenSubquery(
){
return 0;
}
if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
/* Without this restriction, the JT_LTORJ flag would end up being
** omitted on left-hand tables of the right join that is being
** flattened. */
return 0; /* Restrictions (17g), (27b) */
}
testcase( pSub1->pSrc->nSrc>1 );
}