You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlite.patch001 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. diff -Npur sqlite-version-3.32.2/src/expr.c sqlite-version-3.32.2-patched/src/expr.c
  2. --- sqlite-version-3.32.2/src/expr.c 2020-06-04 08:58:43.000000000 -0400
  3. +++ sqlite-version-3.32.2-patched/src/expr.c 2021-04-29 04:06:04.544208700 -0400
  4. @@ -3813,6 +3813,7 @@ expr_code_doover:
  5. AggInfo *pAggInfo = pExpr->pAggInfo;
  6. struct AggInfo_col *pCol;
  7. assert( pAggInfo!=0 );
  8. + assert( AggInfoValid(pAggInfo) );
  9. assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
  10. pCol = &pAggInfo->aCol[pExpr->iAgg];
  11. if( !pAggInfo->directMode ){
  12. @@ -4121,6 +4122,7 @@ expr_code_doover:
  13. assert( !ExprHasProperty(pExpr, EP_IntValue) );
  14. sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
  15. }else{
  16. + assert( AggInfoValid(pInfo) );
  17. return pInfo->aFunc[pExpr->iAgg].iMem;
  18. }
  19. break;
  20. @@ -5658,13 +5660,7 @@ struct SrcCount {
  21. ** Count the number of references to columns.
  22. */
  23. static int exprSrcCount(Walker *pWalker, Expr *pExpr){
  24. - /* There was once a NEVER() on the second term on the grounds that
  25. - ** sqlite3FunctionUsesThisSrc() was always called before
  26. - ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
  27. - ** been converted into TK_AGG_COLUMN. But this is no longer true due
  28. - ** to window functions - sqlite3WindowRewrite() may now indirectly call
  29. - ** FunctionUsesThisSrc() when creating a new sub-select. */
  30. - if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
  31. + if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
  32. int i;
  33. struct SrcCount *p = pWalker->u.pSrcCount;
  34. SrcList *pSrc = p->pSrc;
  35. diff -Npur sqlite-version-3.32.2/src/global.c sqlite-version-3.32.2-patched/src/global.c
  36. --- sqlite-version-3.32.2/src/global.c 2020-06-04 08:58:43.000000000 -0400
  37. +++ sqlite-version-3.32.2-patched/src/global.c 2021-04-29 04:06:04.544208700 -0400
  38. @@ -300,6 +300,11 @@ sqlite3_uint64 sqlite3NProfileCnt = 0;
  39. int sqlite3PendingByte = 0x40000000;
  40. #endif
  41. +/*
  42. +** Flags for select tracing and the ".selecttrace" macro of the CLI
  43. +*/
  44. +/**/ u32 sqlite3SelectTrace = 0;
  45. +
  46. #include "opcodes.h"
  47. /*
  48. ** Properties of opcodes. The OPFLG_INITIALIZER macro is
  49. diff -Npur sqlite-version-3.32.2/src/resolve.c sqlite-version-3.32.2-patched/src/resolve.c
  50. --- sqlite-version-3.32.2/src/resolve.c 2020-06-04 08:58:43.000000000 -0400
  51. +++ sqlite-version-3.32.2-patched/src/resolve.c 2021-04-29 04:06:04.545208700 -0400
  52. @@ -1715,6 +1715,14 @@ static int resolveSelectStep(Walker *pWa
  53. return WRC_Abort;
  54. }
  55. }
  56. + }else if( p->pWin && ALWAYS( (p->selFlags & SF_WinRewrite)==0 ) ){
  57. + sqlite3WindowRewrite(pParse, p);
  58. +#if SELECTTRACE_ENABLED
  59. + if( (sqlite3SelectTrace & 0x108)!=0 ){
  60. + SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  61. + sqlite3TreeViewSelect(0, p, 0);
  62. + }
  63. +#endif
  64. }
  65. #endif
  66. diff -Npur sqlite-version-3.32.2/src/select.c sqlite-version-3.32.2-patched/src/select.c
  67. --- sqlite-version-3.32.2/src/select.c 2020-06-04 08:58:43.000000000 -0400
  68. +++ sqlite-version-3.32.2-patched/src/select.c 2021-04-29 04:07:21.458212191 -0400
  69. @@ -15,20 +15,6 @@
  70. #include "sqliteInt.h"
  71. /*
  72. -** Trace output macros
  73. -*/
  74. -#if SELECTTRACE_ENABLED
  75. -/***/ int sqlite3SelectTrace = 0;
  76. -# define SELECTTRACE(K,P,S,X) \
  77. - if(sqlite3SelectTrace&(K)) \
  78. - sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  79. - sqlite3DebugPrintf X
  80. -#else
  81. -# define SELECTTRACE(K,P,S,X)
  82. -#endif
  83. -
  84. -
  85. -/*
  86. ** An instance of the following object is used to record information about
  87. ** how to process the DISTINCT keyword, to simplify passing that information
  88. ** into the selectInnerLoop() routine.
  89. @@ -4426,11 +4412,14 @@ static int pushDownWhereTerms(
  90. ){
  91. Expr *pNew;
  92. int nChng = 0;
  93. + Select *pSel;
  94. if( pWhere==0 ) return 0;
  95. if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
  96. #ifndef SQLITE_OMIT_WINDOWFUNC
  97. - if( pSubq->pWin ) return 0; /* restriction (6) */
  98. + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
  99. + if( pSel->pWin ) return 0; /* restriction (6) */
  100. + }
  101. #endif
  102. #ifdef SQLITE_DEBUG
  103. @@ -5553,7 +5542,9 @@ static void explainSimpleCount(
  104. static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
  105. if( pExpr->op!=TK_AND ){
  106. Select *pS = pWalker->u.pSelect;
  107. - if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
  108. + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
  109. + && ExprAlwaysFalse(pExpr)==0
  110. + ){
  111. sqlite3 *db = pWalker->pParse->db;
  112. Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
  113. if( pNew ){
  114. @@ -5766,6 +5757,9 @@ int sqlite3Select(
  115. }
  116. if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  117. memset(&sAggInfo, 0, sizeof(sAggInfo));
  118. +#ifdef SQLITE_DEBUG
  119. + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
  120. +#endif
  121. #if SELECTTRACE_ENABLED
  122. SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
  123. if( sqlite3SelectTrace & 0x100 ){
  124. @@ -5804,19 +5798,6 @@ int sqlite3Select(
  125. generateColumnNames(pParse, p);
  126. }
  127. -#ifndef SQLITE_OMIT_WINDOWFUNC
  128. - rc = sqlite3WindowRewrite(pParse, p);
  129. - if( rc ){
  130. - assert( db->mallocFailed || pParse->nErr>0 );
  131. - goto select_end;
  132. - }
  133. -#if SELECTTRACE_ENABLED
  134. - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
  135. - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  136. - sqlite3TreeViewSelect(0, p, 0);
  137. - }
  138. -#endif
  139. -#endif /* SQLITE_OMIT_WINDOWFUNC */
  140. pTabList = p->pSrc;
  141. isAgg = (p->selFlags & SF_Aggregate)!=0;
  142. memset(&sSort, 0, sizeof(sSort));
  143. @@ -6144,7 +6125,7 @@ int sqlite3Select(
  144. if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
  145. && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  146. #ifndef SQLITE_OMIT_WINDOWFUNC
  147. - && p->pWin==0
  148. + && ALWAYS(p->pWin==0)
  149. #endif
  150. ){
  151. p->selFlags &= ~SF_Distinct;
  152. @@ -6791,6 +6772,14 @@ int sqlite3Select(
  153. select_end:
  154. sqlite3ExprListDelete(db, pMinMaxOrderBy);
  155. sqlite3DbFree(db, sAggInfo.aCol);
  156. +#ifdef SQLITE_DEBUG
  157. + for(i=0; i<sAggInfo.nFunc; i++){
  158. + assert( sAggInfo.aFunc[i].pExpr!=0 );
  159. + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
  160. + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
  161. + }
  162. + sAggInfo.iAggMagic = 0;
  163. +#endif
  164. sqlite3DbFree(db, sAggInfo.aFunc);
  165. #if SELECTTRACE_ENABLED
  166. SELECTTRACE(0x1,pParse,p,("end processing\n"));
  167. diff -Npur sqlite-version-3.32.2/src/sqliteInt.h sqlite-version-3.32.2-patched/src/sqliteInt.h
  168. --- sqlite-version-3.32.2/src/sqliteInt.h 2020-06-04 08:58:43.000000000 -0400
  169. +++ sqlite-version-3.32.2-patched/src/sqliteInt.h 2021-04-29 04:06:04.547208700 -0400
  170. @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst;
  171. */
  172. #if defined(SQLITE_ENABLE_SELECTTRACE)
  173. # define SELECTTRACE_ENABLED 1
  174. +# define SELECTTRACE(K,P,S,X) \
  175. + if(sqlite3SelectTrace&(K)) \
  176. + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  177. + sqlite3DebugPrintf X
  178. #else
  179. +# define SELECTTRACE(K,P,S,X)
  180. # define SELECTTRACE_ENABLED 0
  181. #endif
  182. @@ -2523,9 +2528,24 @@ struct AggInfo {
  183. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  184. } *aFunc;
  185. int nFunc; /* Number of entries in aFunc[] */
  186. +#ifdef SQLITE_DEBUG
  187. + u32 iAggMagic; /* Sanity checking constant */
  188. +#endif
  189. };
  190. /*
  191. +** Allowed values for AggInfo.iAggMagic
  192. +*/
  193. +#define SQLITE_AGGMAGIC_VALID 0x05cadade
  194. +
  195. +/*
  196. +** True if the AggInfo object is valid. Used inside of assert() only.
  197. +*/
  198. +#ifdef SQLITE_DEBUG
  199. +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
  200. +#endif
  201. +
  202. +/*
  203. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  204. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  205. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  206. @@ -4546,10 +4566,11 @@ extern const unsigned char sqlite3UpperT
  207. extern const unsigned char sqlite3CtypeMap[];
  208. extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  209. extern FuncDefHash sqlite3BuiltinFunctions;
  210. +extern u32 sqlite3SelectTrace;
  211. #ifndef SQLITE_OMIT_WSD
  212. extern int sqlite3PendingByte;
  213. #endif
  214. -#endif
  215. +#endif /* !defined(SQLITE_AMALGAMATION) */
  216. #ifdef VDBE_PROFILE
  217. extern sqlite3_uint64 sqlite3NProfileCnt;
  218. #endif
  219. diff -Npur sqlite-version-3.32.2/src/test1.c sqlite-version-3.32.2-patched/src/test1.c
  220. --- sqlite-version-3.32.2/src/test1.c 2020-06-04 08:58:43.000000000 -0400
  221. +++ sqlite-version-3.32.2-patched/src/test1.c 2021-04-29 04:06:04.548208700 -0400
  222. @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp)
  223. #endif
  224. #endif
  225. #if defined(SQLITE_ENABLE_SELECTTRACE)
  226. - extern int sqlite3SelectTrace;
  227. + extern u32 sqlite3SelectTrace;
  228. #endif
  229. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  230. diff -Npur sqlite-version-3.32.2/src/window.c sqlite-version-3.32.2-patched/src/window.c
  231. --- sqlite-version-3.32.2/src/window.c 2020-06-04 08:58:43.000000000 -0400
  232. +++ sqlite-version-3.32.2-patched/src/window.c 2021-04-29 04:06:04.548208700 -0400
  233. @@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept
  234. */
  235. int sqlite3WindowRewrite(Parse *pParse, Select *p){
  236. int rc = SQLITE_OK;
  237. - if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
  238. + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){
  239. Vdbe *v = sqlite3GetVdbe(pParse);
  240. sqlite3 *db = pParse->db;
  241. Select *pSub = 0; /* The subquery */
  242. diff -Npur sqlite-version-3.32.2/test/having.test sqlite-version-3.32.2-patched/test/having.test
  243. --- sqlite-version-3.32.2/test/having.test 2020-06-04 08:58:43.000000000 -0400
  244. +++ sqlite-version-3.32.2-patched/test/having.test 2021-04-29 04:08:11.785214475 -0400
  245. @@ -154,5 +154,24 @@ do_execsql_test 4.3 {
  246. SELECT a, sum(b) FROM t3 WHERE nondeter(a) GROUP BY a
  247. } {1 4 2 2}
  248. +#-------------------------------------------------------------------------
  249. +reset_db
  250. +do_execsql_test 5.0 {
  251. + CREATE TABLE t1(a, b);
  252. + CREATE TABLE t2(x, y);
  253. + INSERT INTO t1 VALUES('a', 'b');
  254. +}
  255. +
  256. +# The WHERE clause (a=2), uses an aggregate column from the outer query.
  257. +# If the HAVING term (0) is moved into the WHERE clause in this case,
  258. +# SQLite would at one point optimize (a=2 AND 0) to simply (0). Which
  259. +# is logically correct, but happened to cause problems in aggregate
  260. +# processing for the outer query. This test case verifies that those
  261. +# problems are no longer present.
  262. +do_execsql_test 5.1 {
  263. + SELECT min(b), (
  264. + SELECT x FROM t2 WHERE a=2 GROUP BY y HAVING 0
  265. + ) FROM t1;
  266. +} {b {}}
  267. finish_test
  268. diff -Npur sqlite-version-3.32.2/test/window1.test sqlite-version-3.32.2-patched/test/window1.test
  269. --- sqlite-version-3.32.2/test/window1.test 2020-06-04 08:58:43.000000000 -0400
  270. +++ sqlite-version-3.32.2-patched/test/window1.test 2021-04-29 04:06:04.549208700 -0400
  271. @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 {
  272. WHERE a.c);
  273. } {4 4 4 4}
  274. +#-------------------------------------------------------------------------
  275. +reset_db
  276. +do_execsql_test 54.1 {
  277. + CREATE TABLE t1(a VARCHAR(20), b FLOAT);
  278. + INSERT INTO t1 VALUES('1',10.0);
  279. +}
  280. +
  281. +do_execsql_test 54.2 {
  282. + SELECT * FROM (
  283. + SELECT sum(b) OVER() AS c FROM t1
  284. + UNION
  285. + SELECT b AS c FROM t1
  286. + ) WHERE c>10;
  287. +}
  288. +
  289. +do_execsql_test 54.3 {
  290. + INSERT INTO t1 VALUES('2',5.0);
  291. + INSERT INTO t1 VALUES('3',15.0);
  292. +}
  293. +
  294. +do_execsql_test 54.4 {
  295. + SELECT * FROM (
  296. + SELECT sum(b) OVER() AS c FROM t1
  297. + UNION
  298. + SELECT b AS c FROM t1
  299. + ) WHERE c>10;
  300. +} {15.0 30.0}
  301. +
  302. +# 2020-06-05 ticket c8d3b9f0a750a529
  303. +reset_db
  304. +do_execsql_test 55.1 {
  305. + CREATE TABLE a(b);
  306. + SELECT
  307. + (SELECT b FROM a
  308. + GROUP BY b
  309. + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
  310. + )
  311. + FROM a
  312. + UNION
  313. + SELECT 99
  314. + ORDER BY 1;
  315. +} {99}
  316. finish_test