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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 20:58:43.000000000 +0800
  3. +++ sqlite-version-3.32.2-patched/src/expr.c 2020-06-15 16:03:29.343573250 +0800
  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 20:58:43.000000000 +0800
  37. +++ sqlite-version-3.32.2-patched/src/global.c 2020-06-15 16:03:29.343573250 +0800
  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 20:58:43.000000000 +0800
  51. +++ sqlite-version-3.32.2-patched/src/resolve.c 2020-06-15 16:03:29.343573250 +0800
  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 20:58:43.000000000 +0800
  68. +++ sqlite-version-3.32.2-patched/src/select.c 2020-06-15 16:03:29.343573250 +0800
  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. @@ -5766,6 +5755,9 @@ int sqlite3Select(
  104. }
  105. if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  106. memset(&sAggInfo, 0, sizeof(sAggInfo));
  107. +#ifdef SQLITE_DEBUG
  108. + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
  109. +#endif
  110. #if SELECTTRACE_ENABLED
  111. SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
  112. if( sqlite3SelectTrace & 0x100 ){
  113. @@ -5804,19 +5796,6 @@ int sqlite3Select(
  114. generateColumnNames(pParse, p);
  115. }
  116. -#ifndef SQLITE_OMIT_WINDOWFUNC
  117. - rc = sqlite3WindowRewrite(pParse, p);
  118. - if( rc ){
  119. - assert( db->mallocFailed || pParse->nErr>0 );
  120. - goto select_end;
  121. - }
  122. -#if SELECTTRACE_ENABLED
  123. - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
  124. - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  125. - sqlite3TreeViewSelect(0, p, 0);
  126. - }
  127. -#endif
  128. -#endif /* SQLITE_OMIT_WINDOWFUNC */
  129. pTabList = p->pSrc;
  130. isAgg = (p->selFlags & SF_Aggregate)!=0;
  131. memset(&sSort, 0, sizeof(sSort));
  132. @@ -6144,7 +6123,7 @@ int sqlite3Select(
  133. if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
  134. && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  135. #ifndef SQLITE_OMIT_WINDOWFUNC
  136. - && p->pWin==0
  137. + && ALWAYS(p->pWin==0)
  138. #endif
  139. ){
  140. p->selFlags &= ~SF_Distinct;
  141. @@ -6791,6 +6770,14 @@ int sqlite3Select(
  142. select_end:
  143. sqlite3ExprListDelete(db, pMinMaxOrderBy);
  144. sqlite3DbFree(db, sAggInfo.aCol);
  145. +#ifdef SQLITE_DEBUG
  146. + for(i=0; i<sAggInfo.nFunc; i++){
  147. + assert( sAggInfo.aFunc[i].pExpr!=0 );
  148. + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
  149. + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
  150. + }
  151. + sAggInfo.iAggMagic = 0;
  152. +#endif
  153. sqlite3DbFree(db, sAggInfo.aFunc);
  154. #if SELECTTRACE_ENABLED
  155. SELECTTRACE(0x1,pParse,p,("end processing\n"));
  156. diff -Npur sqlite-version-3.32.2/src/sqliteInt.h sqlite-version-3.32.2-patched/src/sqliteInt.h
  157. --- sqlite-version-3.32.2/src/sqliteInt.h 2020-06-04 20:58:43.000000000 +0800
  158. +++ sqlite-version-3.32.2-patched/src/sqliteInt.h 2020-06-15 16:03:29.347573247 +0800
  159. @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst;
  160. */
  161. #if defined(SQLITE_ENABLE_SELECTTRACE)
  162. # define SELECTTRACE_ENABLED 1
  163. +# define SELECTTRACE(K,P,S,X) \
  164. + if(sqlite3SelectTrace&(K)) \
  165. + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  166. + sqlite3DebugPrintf X
  167. #else
  168. +# define SELECTTRACE(K,P,S,X)
  169. # define SELECTTRACE_ENABLED 0
  170. #endif
  171. @@ -2523,9 +2528,24 @@ struct AggInfo {
  172. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  173. } *aFunc;
  174. int nFunc; /* Number of entries in aFunc[] */
  175. +#ifdef SQLITE_DEBUG
  176. + u32 iAggMagic; /* Sanity checking constant */
  177. +#endif
  178. };
  179. /*
  180. +** Allowed values for AggInfo.iAggMagic
  181. +*/
  182. +#define SQLITE_AGGMAGIC_VALID 0x05cadade
  183. +
  184. +/*
  185. +** True if the AggInfo object is valid. Used inside of assert() only.
  186. +*/
  187. +#ifdef SQLITE_DEBUG
  188. +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
  189. +#endif
  190. +
  191. +/*
  192. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  193. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  194. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  195. @@ -4546,10 +4566,11 @@ extern const unsigned char sqlite3UpperT
  196. extern const unsigned char sqlite3CtypeMap[];
  197. extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  198. extern FuncDefHash sqlite3BuiltinFunctions;
  199. +extern u32 sqlite3SelectTrace;
  200. #ifndef SQLITE_OMIT_WSD
  201. extern int sqlite3PendingByte;
  202. #endif
  203. -#endif
  204. +#endif /* !defined(SQLITE_AMALGAMATION) */
  205. #ifdef VDBE_PROFILE
  206. extern sqlite3_uint64 sqlite3NProfileCnt;
  207. #endif
  208. diff -Npur sqlite-version-3.32.2/src/test1.c sqlite-version-3.32.2-patched/src/test1.c
  209. --- sqlite-version-3.32.2/src/test1.c 2020-06-04 20:58:43.000000000 +0800
  210. +++ sqlite-version-3.32.2-patched/src/test1.c 2020-06-15 16:03:29.347573247 +0800
  211. @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp)
  212. #endif
  213. #endif
  214. #if defined(SQLITE_ENABLE_SELECTTRACE)
  215. - extern int sqlite3SelectTrace;
  216. + extern u32 sqlite3SelectTrace;
  217. #endif
  218. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  219. diff -Npur sqlite-version-3.32.2/src/window.c sqlite-version-3.32.2-patched/src/window.c
  220. --- sqlite-version-3.32.2/src/window.c 2020-06-04 20:58:43.000000000 +0800
  221. +++ sqlite-version-3.32.2-patched/src/window.c 2020-06-15 16:03:29.347573247 +0800
  222. @@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept
  223. */
  224. int sqlite3WindowRewrite(Parse *pParse, Select *p){
  225. int rc = SQLITE_OK;
  226. - if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
  227. + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){
  228. Vdbe *v = sqlite3GetVdbe(pParse);
  229. sqlite3 *db = pParse->db;
  230. Select *pSub = 0; /* The subquery */
  231. diff -Npur sqlite-version-3.32.2/test/window1.test sqlite-version-3.32.2-patched/test/window1.test
  232. --- sqlite-version-3.32.2/test/window1.test 2020-06-04 20:58:43.000000000 +0800
  233. +++ sqlite-version-3.32.2-patched/test/window1.test 2020-06-15 16:03:29.347573247 +0800
  234. @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 {
  235. WHERE a.c);
  236. } {4 4 4 4}
  237. +#-------------------------------------------------------------------------
  238. +reset_db
  239. +do_execsql_test 54.1 {
  240. + CREATE TABLE t1(a VARCHAR(20), b FLOAT);
  241. + INSERT INTO t1 VALUES('1',10.0);
  242. +}
  243. +
  244. +do_execsql_test 54.2 {
  245. + SELECT * FROM (
  246. + SELECT sum(b) OVER() AS c FROM t1
  247. + UNION
  248. + SELECT b AS c FROM t1
  249. + ) WHERE c>10;
  250. +}
  251. +
  252. +do_execsql_test 54.3 {
  253. + INSERT INTO t1 VALUES('2',5.0);
  254. + INSERT INTO t1 VALUES('3',15.0);
  255. +}
  256. +
  257. +do_execsql_test 54.4 {
  258. + SELECT * FROM (
  259. + SELECT sum(b) OVER() AS c FROM t1
  260. + UNION
  261. + SELECT b AS c FROM t1
  262. + ) WHERE c>10;
  263. +} {15.0 30.0}
  264. +
  265. +# 2020-06-05 ticket c8d3b9f0a750a529
  266. +reset_db
  267. +do_execsql_test 55.1 {
  268. + CREATE TABLE a(b);
  269. + SELECT
  270. + (SELECT b FROM a
  271. + GROUP BY b
  272. + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
  273. + )
  274. + FROM a
  275. + UNION
  276. + SELECT 99
  277. + ORDER BY 1;
  278. +} {99}
  279. finish_test