Earlier detection of a misplaced ORDER BY or LIMIT clause in a compound

SELECT.  This prevents problems in recursive CTEs with multiple recursive
terms in which there is an ORDER BY or LIMIT clause on the last non-recursive
term.

FossilOrigin-Name: e893f88750ea64d45922429e022c585748974016404f2a2b7952f5a227865246
This commit is contained in:
drh 2021-03-04 16:03:32 +00:00
parent 50f9f6c649
commit aae0f74e64
5 changed files with 32 additions and 24 deletions

View File

@ -1,5 +1,5 @@
C Fix\sa\scrash\sthat\scould\soccur\sif\sa\ssub-select\sinside\san\sEXISTS\sexpression\sfeatures\san\sORDER\sBY\sclause\scontaining\sone\sor\smore\swindow\sfunctions.
D 2021-03-04T14:18:22.305
C Earlier\sdetection\sof\sa\smisplaced\sORDER\sBY\sor\sLIMIT\sclause\sin\sa\scompound\nSELECT.\s\sThis\sprevents\sproblems\sin\srecursive\sCTEs\swith\smultiple\srecursive\nterms\sin\swhich\sthere\sis\san\sORDER\sBY\sor\sLIMIT\sclause\son\sthe\slast\snon-recursive\nterm.
D 2021-03-04T16:03:32.382
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -531,7 +531,7 @@ F src/os_win.c 77d39873836f1831a9b0b91894fec45ab0e9ca8e067dc8c549e1d1eca1566fe9
F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a
F src/pager.c 970691daea03f9f15e34de671bd8675c1e136232b529e21bfd36d4dba6d41753
F src/pager.h 4bf9b3213a4b2bebbced5eaa8b219cf25d4a82f385d093cd64b7e93e5285f66f
F src/parse.y f3e8d7978c10495850c0bb502fe2669b55cf2841c4670b1f7261782e82069471
F src/parse.y 2107aff88e361bbf8388fdede3fed20fda875f23ba7585ec83b20e3e16a95670
F src/pcache.c 385ff064bca69789d199a98e2169445dc16e4291fa807babd61d4890c3b34177
F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586
F src/pcache1.c 388304fd2d91c39591080b5e0f3c62cfba87db20370e7e0554062bfb29740e9f
@ -542,12 +542,12 @@ F src/printf.c 2b03a80d7c11bb422115dca175a18bf430e9c9dbaa0eee63b758f0c022f8f34f
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
F src/resolve.c 889469e6980181ce77ee8ab3fc84bd52ed6c1c3577fd102d52623d66cc65a3d0
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c 1a7931e67fea84f5cb5432c14a334deac72c6a9b25f351607d56bef163707773
F src/select.c ec87ae50ab868872aca4c92abd9ed9bd0f5f5c76ceb95b66de32d4012f1b255b
F src/shell.c.in 844417f84df1f6c4fce1c815629a888cfdcf219e86513e9c332bbcc38832f477
F src/sqlite.h.in 8855a19f37ade8dad189a9e48233a2ebe1b46faf469c7eb0906a654e252dcc57
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 61b38c073d5e1e96a3d45271b257aef27d0d13da2bea5347692ae579475cd95e
F src/sqliteInt.h 11b9d47e9b9520b123bc75254da7a4e215463e811e62b89518fc5e9a192a0222
F src/sqliteInt.h 7c4679b3b068149f497fe50203a4b04be6b17b8aba581cdbc75da45329cad915
F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
F src/status.c 4b8bc2a6905163a38b739854a35b826c737333fab5b1f8e03fa7eb9a4799c4c1
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@ -1908,7 +1908,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 4da40620ac8557aba6e1e06e720418b55e9950f406288b03749ef1b117041cd9
R dbbe9c3309db800b2bcddd55fa5a9e76
U dan
Z bd8c7ff2ba0ee91419f8449d3502d963
P 9768e7db624fbfe688d61ac06bffca14a169d439d8f65f5a14529af5660d3d2e
R b7b7ec94c5553295fe0ea94425307f6e
U drh
Z 0c82ce08ac9825cd2a29c91b8daceab1

View File

@ -1 +1 @@
9768e7db624fbfe688d61ac06bffca14a169d439d8f65f5a14529af5660d3d2e
e893f88750ea64d45922429e022c585748974016404f2a2b7952f5a227865246

View File

@ -497,11 +497,21 @@ cmd ::= select(X). {
static void parserDoubleLinkSelect(Parse *pParse, Select *p){
assert( p!=0 );
if( p->pPrior ){
Select *pNext = 0, *pLoop;
int mxSelect, cnt = 0;
for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
Select *pNext = 0, *pLoop = p;
int mxSelect, cnt = 1;
while(1){
pLoop->pNext = pNext;
pLoop->selFlags |= SF_Compound;
pNext = pLoop;
pLoop = pLoop->pPrior;
if( pLoop==0 ) break;
cnt++;
if( pLoop->pOrderBy || pLoop->pLimit ){
sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
sqlite3SelectOpName(pNext->op));
break;
}
}
if( (p->selFlags & SF_MultiValue)==0 &&
(mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&

View File

@ -1390,7 +1390,7 @@ KeyInfo *sqlite3KeyInfoFromExprList(
/*
** Name of the connection operator, used for error messages.
*/
static const char *selectOpName(int id){
const char *sqlite3SelectOpName(int id){
char *z;
switch( id ){
case TK_ALL: z = "UNION ALL"; break;
@ -2609,12 +2609,8 @@ static int multiSelect(
db = pParse->db;
pPrior = p->pPrior;
dest = *pDest;
if( pPrior->pOrderBy || pPrior->pLimit ){
sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
rc = 1;
goto multi_select_end;
}
assert( pPrior->pOrderBy==0 );
assert( pPrior->pLimit==0 );
v = sqlite3GetVdbe(pParse);
assert( v!=0 ); /* The VDBE already created by calling function */
@ -2757,7 +2753,7 @@ static int multiSelect(
p->pLimit = 0;
uniondest.eDest = op;
ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
selectOpName(p->op)));
sqlite3SelectOpName(p->op)));
rc = sqlite3Select(pParse, p, &uniondest);
testcase( rc!=SQLITE_OK );
assert( p->pOrderBy==0 );
@ -2833,7 +2829,7 @@ static int multiSelect(
p->pLimit = 0;
intersectdest.iSDParm = tab2;
ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
selectOpName(p->op)));
sqlite3SelectOpName(p->op)));
rc = sqlite3Select(pParse, p, &intersectdest);
testcase( rc!=SQLITE_OK );
pDelete = p->pPrior;
@ -2942,7 +2938,8 @@ void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
}else{
sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
" do not have the same number of result columns", selectOpName(p->op));
" do not have the same number of result columns",
sqlite3SelectOpName(p->op));
}
}
@ -3334,7 +3331,7 @@ static int multiSelectOrderBy(
sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
/* Generate a coroutine to evaluate the SELECT statement to the
** left of the compound operator - the "A" select.

View File

@ -4795,6 +4795,7 @@ void sqlite3KeyInfoUnref(KeyInfo*);
KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
const char *sqlite3SelectOpName(int);
int sqlite3HasExplicitNulls(Parse*, ExprList*);
#ifdef SQLITE_DEBUG