Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Ensure that aggregate functions that (a) are part of SELECT statements with no FROM clause and (b) have one or more scalar sub-selects as arguments are assigned to the correct aggregate context. Fix for ticket [7c6d876f84e6e7e2] |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | branch-3.32 |
Files: | files | file ages | folders |
SHA3-256: |
dafd2466a10f68acbe129010216f017a |
User & Date: | dan 2020-06-10 11:18:28 |
Context
2020-06-17
| ||
12:37 | Merge miscellaneous fixes from trunk into the 3.32 branch. (check-in: d55b8e79 user: drh tags: branch-3.32) | |
2020-06-10
| ||
11:18 | Ensure that aggregate functions that (a) are part of SELECT statements with no FROM clause and (b) have one or more scalar sub-selects as arguments are assigned to the correct aggregate context. Fix for ticket [7c6d876f84e6e7e2] (check-in: dafd2466 user: dan tags: branch-3.32) | |
11:01 | Ensure that the "push-down" optimization does not push constraints down into compound queries if any of the component queries uses window functions. (check-in: d31850fe user: dan tags: branch-3.32) | |
2020-06-09
| ||
23:50 | Ensure that aggregate functions that (a) are part of SELECT statements with no FROM clause and (b) have one or more scalar sub-selects as arguments are assigned to the correct aggregate context. Fix for ticket [7c6d876f84e6e7e2] (check-in: c29a9e48 user: drh tags: trunk) | |
Changes
Changes to src/expr.c.
︙ | ︙ | |||
5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 | ** An instance of the following structure is used by the tree walker ** to count references to table columns in the arguments of an ** aggregate function, in order to implement the ** sqlite3FunctionThisSrc() routine. */ struct SrcCount { SrcList *pSrc; /* One particular FROM clause in a nested query */ int nThis; /* Number of references to columns in pSrcList */ int nOther; /* Number of references to columns in other FROM clauses */ }; /* ** Count the number of references to columns. */ static int exprSrcCount(Walker *pWalker, Expr *pExpr){ /* There was once a NEVER() on the second term on the grounds that ** sqlite3FunctionUsesThisSrc() was always called before | > > > > > > > > > > > > > > > | 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 | ** An instance of the following structure is used by the tree walker ** to count references to table columns in the arguments of an ** aggregate function, in order to implement the ** sqlite3FunctionThisSrc() routine. */ struct SrcCount { SrcList *pSrc; /* One particular FROM clause in a nested query */ int iSrcInner; /* Smallest cursor number in this context */ int nThis; /* Number of references to columns in pSrcList */ int nOther; /* Number of references to columns in other FROM clauses */ }; /* ** xSelect callback for sqlite3FunctionUsesThisSrc(). If this is the first ** SELECT with a FROM clause encountered during this iteration, set ** SrcCount.iSrcInner to the cursor number of the leftmost object in ** the FROM cause. */ static int selectSrcCount(Walker *pWalker, Select *pSel){ struct SrcCount *p = pWalker->u.pSrcCount; if( p->iSrcInner==0x7FFFFFFF && ALWAYS(pSel->pSrc) && pSel->pSrc->nSrc ){ pWalker->u.pSrcCount->iSrcInner = pSel->pSrc->a[0].iCursor; } return WRC_Continue; } /* ** Count the number of references to columns. */ static int exprSrcCount(Walker *pWalker, Expr *pExpr){ /* There was once a NEVER() on the second term on the grounds that ** sqlite3FunctionUsesThisSrc() was always called before |
︙ | ︙ | |||
5675 5676 5677 5678 5679 5680 5681 | SrcList *pSrc = p->pSrc; int nSrc = pSrc ? pSrc->nSrc : 0; for(i=0; i<nSrc; i++){ if( pExpr->iTable==pSrc->a[i].iCursor ) break; } if( i<nSrc ){ p->nThis++; | | | 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 | SrcList *pSrc = p->pSrc; int nSrc = pSrc ? pSrc->nSrc : 0; for(i=0; i<nSrc; i++){ if( pExpr->iTable==pSrc->a[i].iCursor ) break; } if( i<nSrc ){ p->nThis++; }else if( pExpr->iTable<p->iSrcInner ){ /* In a well-formed parse tree (no name resolution errors), ** TK_COLUMN nodes with smaller Expr.iTable values are in an ** outer context. Those are the only ones to count as "other" */ p->nOther++; } } return WRC_Continue; |
︙ | ︙ | |||
5697 5698 5699 5700 5701 5702 5703 | */ int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ Walker w; struct SrcCount cnt; assert( pExpr->op==TK_AGG_FUNCTION ); memset(&w, 0, sizeof(w)); w.xExprCallback = exprSrcCount; | | > | 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 | */ int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ Walker w; struct SrcCount cnt; assert( pExpr->op==TK_AGG_FUNCTION ); memset(&w, 0, sizeof(w)); w.xExprCallback = exprSrcCount; w.xSelectCallback = selectSrcCount; w.u.pSrcCount = &cnt; cnt.pSrc = pSrcList; cnt.iSrcInner = (pSrcList&&pSrcList->nSrc)?pSrcList->a[0].iCursor:0x7FFFFFFF; cnt.nThis = 0; cnt.nOther = 0; sqlite3WalkExprList(&w, pExpr->x.pList); #ifndef SQLITE_OMIT_WINDOWFUNC if( ExprHasProperty(pExpr, EP_WinFunc) ){ sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter); } |
︙ | ︙ |
Changes to test/aggnested.test.
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # This file implements tests for processing aggregate queries with # subqueries in which the subqueries hold the aggregate functions # or in which the subqueries are themselves aggregate queries # set testdir [file dirname $argv0] source $testdir/tester.tcl do_test aggnested-1.1 { db eval { CREATE TABLE t1(a1 INTEGER); INSERT INTO t1 VALUES(1), (2), (3); CREATE TABLE t2(b1 INTEGER); INSERT INTO t2 VALUES(4), (5); | > | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # This file implements tests for processing aggregate queries with # subqueries in which the subqueries hold the aggregate functions # or in which the subqueries are themselves aggregate queries # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix aggnested do_test aggnested-1.1 { db eval { CREATE TABLE t1(a1 INTEGER); INSERT INTO t1 VALUES(1), (2), (3); CREATE TABLE t2(b1 INTEGER); INSERT INTO t2 VALUES(4), (5); |
︙ | ︙ | |||
254 255 256 257 258 259 260 261 | CREATE TABLE ty(y INT); INSERT INTO ty VALUES(91),(92),(93); SELECT min((SELECT count(y) FROM ty)) FROM tx; } {3} do_execsql_test aggnested-4.4 { SELECT max((SELECT a FROM (SELECT count(*) AS a FROM ty) AS s)) FROM tx; } {3} | > | > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | CREATE TABLE ty(y INT); INSERT INTO ty VALUES(91),(92),(93); SELECT min((SELECT count(y) FROM ty)) FROM tx; } {3} do_execsql_test aggnested-4.4 { SELECT max((SELECT a FROM (SELECT count(*) AS a FROM ty) AS s)) FROM tx; } {3} #-------------------------------------------------------------------------- # reset_db do_execsql_test 5.0 { CREATE TABLE x1(a, b); INSERT INTO x1 VALUES(1, 2); CREATE TABLE x2(x); INSERT INTO x2 VALUES(NULL), (NULL), (NULL); } # At one point, aggregate "total()" in the query below was being processed # as part of the outer SELECT, not as part of the sub-select with no FROM # clause. do_execsql_test 5.1 { SELECT ( SELECT total( (SELECT b FROM x1) ) ) FROM x2; } {2.0 2.0 2.0} do_execsql_test 5.2 { SELECT ( SELECT total( (SELECT 2 FROM x1) ) ) FROM x2; } {2.0 2.0 2.0} do_execsql_test 5.3 { CREATE TABLE t1(a); CREATE TABLE t2(b); } do_execsql_test 5.4 { SELECT( SELECT max(b) LIMIT ( SELECT total( (SELECT a FROM t1) ) ) ) FROM t2; } {{}} do_execsql_test 5.5 { CREATE TABLE a(b); WITH c AS(SELECT a) SELECT(SELECT(SELECT group_concat(b, b) LIMIT(SELECT 0.100000 * AVG(DISTINCT(SELECT 0 FROM a ORDER BY b, b, b)))) FROM a GROUP BY b, b, b) FROM a EXCEPT SELECT b FROM a ORDER BY b, b, b; } finish_test |
Changes to test/window9.test.
︙ | ︙ | |||
251 252 253 254 255 256 257 | SELECT count() OVER() FROM (SELECT 0) ORDER BY 1 ; } do_catchsql_test 8.3 { SELECT min( max((SELECT x FROM v1)) ) OVER() | | | | 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | SELECT count() OVER() FROM (SELECT 0) ORDER BY 1 ; } do_catchsql_test 8.3 { SELECT min( max((SELECT x FROM v1)) ) OVER() } {0 0} do_execsql_test 8.4 { SELECT( SELECT x UNION SELECT sum( avg((SELECT x FROM v1)) ) OVER() ) FROM v1; } {0.0 0.0} finish_test |