Fix a problem with using scalar sub-selects in window function queries.

FossilOrigin-Name: 687fe532c274265ca77451f97829743fcb8a714d0f6b1ceb9a147ab9babdc5b5
This commit is contained in:
dan 2018-07-10 17:26:12 +00:00
parent 6ccbd2787b
commit b556f26145
4 changed files with 70 additions and 14 deletions

View File

@ -1,5 +1,5 @@
C Further\simprovements\sto\sbytecode\sbranch\stesting.\s\sFix\scases\swhere\sthe\nmacros\ssaid\sa\sbranch\scould\snot\sbe\staken\swhen\sin\sfact\sit\scould\sbe.\s\sAlter\nsome\swindow\sfunction\sbranch\scoverage\smacros\sto\sindicate\sthat\scomparison\noperands\scannot\sbe\sNULL.
D 2018-07-10T17:10:44.627
C Fix\sa\sproblem\swith\susing\sscalar\ssub-selects\sin\swindow\sfunction\squeries.
D 2018-07-10T17:26:12.992
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F Makefile.in 0a3a6c81e6fcb969ff9106e882f0a08547014ba463cb6beca4c4efaecc924ee6
@ -584,7 +584,7 @@ F src/where.c 0bcbf9e191ca07f9ea2008aa80e70ded46bcdffd26560c83397da501f00aece6
F src/whereInt.h b90ef9b9707ef750eab2a7a080c48fb4900315033274689def32d0cf5a81ebe4
F src/wherecode.c 3317f2b083a66d3e65a03edf316ade4ccb0a99c9956273282ebb579b95d4ba96
F src/whereexpr.c 571618c67a3eb5ce0f1158c2792c1aee9b4a4a264392fc4fb1b35467f80abf9a
F src/window.c 74d56384a0ad3b8a3038c8087dd9fb012c512041da4e57028526f4e0d4277f1c
F src/window.c 8d53b6178212deab779624b1fd12b32c4581d33a8a0dfe29f869a81356a7acdd
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d
@ -1628,7 +1628,7 @@ F test/window3.test 87fb18021903fc4d1659b8b2092aea55d611a9606cfa7272686234e5197c
F test/window4.tcl 871364059b7d320d556ec6ef804d604a4e8cc1547a3102c5d56067371bb200af
F test/window4.test 323b118eb592932036388643ca6dcaead87f699bbea2984bbca49ba4ad6c2509
F test/window5.test 8187f46597c90b73e8f96659e893353cbda337479cc582f7a488eab351ba08d3
F test/window6.test 63a7176e01aef5cc202563bbd754a88131404bf1aa455d7232f29a4740e3ddde
F test/window6.test 7574778c79cae89f1781df237bf9ff5063886deca91a36efc53934315f0e7612
F test/windowfault.test 23abad97b72c6f609002255ddd41ef5c8922408f918f9b98ad6005ab316e482f
F test/with1.test 58475190cd8caaeebea8cfeb2a264ec97a0c492b8ffe9ad20cefbb23df462f96
F test/with2.test e0030e2f0267a910d6c0e4f46f2dfe941c1cc0d4f659ba69b3597728e7e8f1ab
@ -1746,7 +1746,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P cd2da7e1ba4e78e68ccf65d4969df963c1e3085930e74419450bda2799381e05
R 3821f5f33f5e3df6ad28f8fb0dfc6e52
U drh
Z 3fd854bc2517e9ebe99971d61a4896ac
P 76e42b7071a71137c0da4f10db4e653ae94a89b56e95cd3f036bff08c9500d08
R 5a928f31195aa92936976582dda769cc
T +closed dcac190277fab9c6a9bdf651d18b25d97e0db49b3cc709ca145403c8b556bce3
U dan
Z 21618f80bf768e7efd03a702d30b034d

View File

@ -1 +1 @@
76e42b7071a71137c0da4f10db4e653ae94a89b56e95cd3f036bff08c9500d08
687fe532c274265ca77451f97829743fcb8a714d0f6b1ceb9a147ab9babdc5b5

View File

@ -589,7 +589,9 @@ void sqlite3WindowUpdate(
typedef struct WindowRewrite WindowRewrite;
struct WindowRewrite {
Window *pWin;
SrcList *pSrc;
ExprList *pSub;
Select *pSubSelect; /* Current sub-select, if any */
};
/*
@ -601,6 +603,24 @@ static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
struct WindowRewrite *p = pWalker->u.pRewrite;
Parse *pParse = pWalker->pParse;
/* If this function is being called from within a scalar sub-select
** that used by the SELECT statement being processed, only process
** TK_COLUMN expressions that refer to it (the outer SELECT). Do
** not process aggregates or window functions at all, as they belong
** to the scalar sub-select. */
if( p->pSubSelect ){
if( pExpr->op!=TK_COLUMN ){
return WRC_Continue;
}else{
int nSrc = p->pSrc->nSrc;
int i;
for(i=0; i<nSrc; i++){
if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
}
if( i==nSrc ) return WRC_Continue;
}
}
switch( pExpr->op ){
case TK_FUNCTION:
@ -643,8 +663,15 @@ static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
return WRC_Continue;
}
static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
UNUSED_PARAMETER(pWalker);
UNUSED_PARAMETER(pSelect);
struct WindowRewrite *p = pWalker->u.pRewrite;
Select *pSave = p->pSubSelect;
if( pSave==pSelect ){
return WRC_Continue;
}else{
p->pSubSelect = pSelect;
sqlite3WalkSelect(pWalker, pSelect);
p->pSubSelect = pSave;
}
return WRC_Prune;
}
@ -655,7 +682,7 @@ static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
** * TK_COLUMN,
** * aggregate function, or
** * window function with a Window object that is not a member of the
** linked list passed as the second argument (pWin)
** Window list passed as the second argument (pWin).
**
** Append the node to output expression-list (*ppSub). And replace it
** with a TK_COLUMN that reads the (N-1)th element of table
@ -665,6 +692,7 @@ static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
static void selectWindowRewriteEList(
Parse *pParse,
Window *pWin,
SrcList *pSrc,
ExprList *pEList, /* Rewrite expressions in this list */
ExprList **ppSub /* IN/OUT: Sub-select expression-list */
){
@ -676,6 +704,7 @@ static void selectWindowRewriteEList(
sRewrite.pSub = *ppSub;
sRewrite.pWin = pWin;
sRewrite.pSrc = pSrc;
sWalker.pParse = pParse;
sWalker.xExprCallback = selectWindowRewriteExprCb;
@ -753,8 +782,8 @@ int sqlite3WindowRewrite(Parse *pParse, Select *p){
** many columns the table will have. */
pMWin->iEphCsr = pParse->nTab++;
selectWindowRewriteEList(pParse, pMWin, p->pEList, &pSublist);
selectWindowRewriteEList(pParse, pMWin, p->pOrderBy, &pSublist);
selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
/* Append the PARTITION BY and ORDER BY expressions to the to the

View File

@ -310,4 +310,30 @@ foreach {tn stmt res} {
" $res
}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 11.0 {
CREATE TABLE t1(a INT);
INSERT INTO t1 VALUES(10),(15),(20),(20),(25),(30),(30),(50);
CREATE TABLE t3(x INT, y VARCHAR);
INSERT INTO t3(x,y) VALUES(10,'ten'),('15','fifteen'),(30,'thirty');
}
do_execsql_test 11.1 {
SELECT a, (SELECT y FROM t3 WHERE x=a) FROM t1 ORDER BY a;
} {
10 ten 15 fifteen 20 {} 20 {} 25 {} 30 thirty 30 thirty 50 {}
}
do_execsql_test 11.2 {
SELECT a, (SELECT y FROM t3 WHERE x=a), sum(a) OVER (ORDER BY a)
FROM t1 ORDER BY a;
} {
10 ten 10 15 fifteen 25 20 {} 65 20 {} 65
25 {} 90 30 thirty 150 30 thirty 150 50 {} 200
}
finish_test