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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. diff -Npur sqlite-version-3.32.2-new/src/expr.c sqlite-version-3.32.2/src/expr.c
  2. --- sqlite-version-3.32.2-new/src/expr.c 2020-06-04 08:58:43.000000000 -0400
  3. +++ sqlite-version-3.32.2/src/expr.c 2021-08-04 11:57:45.029230992 -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-new/src/global.c sqlite-version-3.32.2/src/global.c
  36. --- sqlite-version-3.32.2-new/src/global.c 2020-06-04 08:58:43.000000000 -0400
  37. +++ sqlite-version-3.32.2/src/global.c 2021-08-04 11:57:45.033230992 -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-new/src/resolve.c sqlite-version-3.32.2/src/resolve.c
  50. --- sqlite-version-3.32.2-new/src/resolve.c 2020-06-04 08:58:43.000000000 -0400
  51. +++ sqlite-version-3.32.2/src/resolve.c 2021-08-04 11:57:45.033230992 -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-new/src/select.c sqlite-version-3.32.2/src/select.c
  67. --- sqlite-version-3.32.2-new/src/select.c 2020-06-04 08:58:43.000000000 -0400
  68. +++ sqlite-version-3.32.2/src/select.c 2021-08-04 12:27:34.737267443 -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. @@ -2717,9 +2703,7 @@ static int multiSelect(
  90. selectOpName(p->op)));
  91. rc = sqlite3Select(pParse, p, &uniondest);
  92. testcase( rc!=SQLITE_OK );
  93. - /* Query flattening in sqlite3Select() might refill p->pOrderBy.
  94. - ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
  95. - sqlite3ExprListDelete(db, p->pOrderBy);
  96. + assert( p->pOrderBy==0 );
  97. pDelete = p->pPrior;
  98. p->pPrior = pPrior;
  99. p->pOrderBy = 0;
  100. @@ -4105,7 +4089,7 @@ static int flattenSubquery(
  101. ** We look at every expression in the outer query and every place we see
  102. ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
  103. */
  104. - if( pSub->pOrderBy ){
  105. + if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
  106. /* At this point, any non-zero iOrderByCol values indicate that the
  107. ** ORDER BY column expression is identical to the iOrderByCol'th
  108. ** expression returned by SELECT statement pSub. Since these values
  109. @@ -4426,11 +4410,14 @@ static int pushDownWhereTerms(
  110. ){
  111. Expr *pNew;
  112. int nChng = 0;
  113. + Select *pSel;
  114. if( pWhere==0 ) return 0;
  115. if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
  116. #ifndef SQLITE_OMIT_WINDOWFUNC
  117. - if( pSubq->pWin ) return 0; /* restriction (6) */
  118. + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
  119. + if( pSel->pWin ) return 0; /* restriction (6) */
  120. + }
  121. #endif
  122. #ifdef SQLITE_DEBUG
  123. @@ -5553,7 +5540,9 @@ static void explainSimpleCount(
  124. static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
  125. if( pExpr->op!=TK_AND ){
  126. Select *pS = pWalker->u.pSelect;
  127. - if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
  128. + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
  129. + && ExprAlwaysFalse(pExpr)==0
  130. + ){
  131. sqlite3 *db = pWalker->pParse->db;
  132. Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
  133. if( pNew ){
  134. @@ -5766,6 +5755,9 @@ int sqlite3Select(
  135. }
  136. if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  137. memset(&sAggInfo, 0, sizeof(sAggInfo));
  138. +#ifdef SQLITE_DEBUG
  139. + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
  140. +#endif
  141. #if SELECTTRACE_ENABLED
  142. SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
  143. if( sqlite3SelectTrace & 0x100 ){
  144. @@ -5787,6 +5779,7 @@ int sqlite3Select(
  145. sqlite3ExprListDelete(db, p->pOrderBy);
  146. p->pOrderBy = 0;
  147. p->selFlags &= ~SF_Distinct;
  148. + p->selFlags |= SF_NoopOrderBy;
  149. }
  150. sqlite3SelectPrep(pParse, p, 0);
  151. if( pParse->nErr || db->mallocFailed ){
  152. @@ -5804,19 +5797,6 @@ int sqlite3Select(
  153. generateColumnNames(pParse, p);
  154. }
  155. -#ifndef SQLITE_OMIT_WINDOWFUNC
  156. - rc = sqlite3WindowRewrite(pParse, p);
  157. - if( rc ){
  158. - assert( db->mallocFailed || pParse->nErr>0 );
  159. - goto select_end;
  160. - }
  161. -#if SELECTTRACE_ENABLED
  162. - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
  163. - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  164. - sqlite3TreeViewSelect(0, p, 0);
  165. - }
  166. -#endif
  167. -#endif /* SQLITE_OMIT_WINDOWFUNC */
  168. pTabList = p->pSrc;
  169. isAgg = (p->selFlags & SF_Aggregate)!=0;
  170. memset(&sSort, 0, sizeof(sSort));
  171. @@ -6144,7 +6124,7 @@ int sqlite3Select(
  172. if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
  173. && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  174. #ifndef SQLITE_OMIT_WINDOWFUNC
  175. - && p->pWin==0
  176. + && ALWAYS(p->pWin==0)
  177. #endif
  178. ){
  179. p->selFlags &= ~SF_Distinct;
  180. @@ -6791,6 +6771,14 @@ int sqlite3Select(
  181. select_end:
  182. sqlite3ExprListDelete(db, pMinMaxOrderBy);
  183. sqlite3DbFree(db, sAggInfo.aCol);
  184. +#ifdef SQLITE_DEBUG
  185. + for(i=0; i<sAggInfo.nFunc; i++){
  186. + assert( sAggInfo.aFunc[i].pExpr!=0 );
  187. + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
  188. + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
  189. + }
  190. + sAggInfo.iAggMagic = 0;
  191. +#endif
  192. sqlite3DbFree(db, sAggInfo.aFunc);
  193. #if SELECTTRACE_ENABLED
  194. SELECTTRACE(0x1,pParse,p,("end processing\n"));
  195. diff -Npur sqlite-version-3.32.2-new/src/sqliteInt.h sqlite-version-3.32.2/src/sqliteInt.h
  196. --- sqlite-version-3.32.2-new/src/sqliteInt.h 2020-06-04 08:58:43.000000000 -0400
  197. +++ sqlite-version-3.32.2/src/sqliteInt.h 2021-08-04 12:28:22.825268422 -0400
  198. @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst;
  199. */
  200. #if defined(SQLITE_ENABLE_SELECTTRACE)
  201. # define SELECTTRACE_ENABLED 1
  202. +# define SELECTTRACE(K,P,S,X) \
  203. + if(sqlite3SelectTrace&(K)) \
  204. + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  205. + sqlite3DebugPrintf X
  206. #else
  207. +# define SELECTTRACE(K,P,S,X)
  208. # define SELECTTRACE_ENABLED 0
  209. #endif
  210. @@ -2523,9 +2528,24 @@ struct AggInfo {
  211. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  212. } *aFunc;
  213. int nFunc; /* Number of entries in aFunc[] */
  214. +#ifdef SQLITE_DEBUG
  215. + u32 iAggMagic; /* Sanity checking constant */
  216. +#endif
  217. };
  218. /*
  219. +** Allowed values for AggInfo.iAggMagic
  220. +*/
  221. +#define SQLITE_AGGMAGIC_VALID 0x05cadade
  222. +
  223. +/*
  224. +** True if the AggInfo object is valid. Used inside of assert() only.
  225. +*/
  226. +#ifdef SQLITE_DEBUG
  227. +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
  228. +#endif
  229. +
  230. +/*
  231. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  232. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  233. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  234. @@ -3105,6 +3125,7 @@ struct Select {
  235. #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
  236. #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
  237. #define SF_View 0x0200000 /* SELECT statement is a view */
  238. +#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
  239. /*
  240. ** The results of a SELECT can be distributed in several ways, as defined
  241. @@ -4546,10 +4567,11 @@ extern const unsigned char sqlite3UpperT
  242. extern const unsigned char sqlite3CtypeMap[];
  243. extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  244. extern FuncDefHash sqlite3BuiltinFunctions;
  245. +extern u32 sqlite3SelectTrace;
  246. #ifndef SQLITE_OMIT_WSD
  247. extern int sqlite3PendingByte;
  248. #endif
  249. -#endif
  250. +#endif /* !defined(SQLITE_AMALGAMATION) */
  251. #ifdef VDBE_PROFILE
  252. extern sqlite3_uint64 sqlite3NProfileCnt;
  253. #endif
  254. diff -Npur sqlite-version-3.32.2-new/src/test1.c sqlite-version-3.32.2/src/test1.c
  255. --- sqlite-version-3.32.2-new/src/test1.c 2020-06-04 08:58:43.000000000 -0400
  256. +++ sqlite-version-3.32.2/src/test1.c 2021-08-04 11:57:45.037230992 -0400
  257. @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp)
  258. #endif
  259. #endif
  260. #if defined(SQLITE_ENABLE_SELECTTRACE)
  261. - extern int sqlite3SelectTrace;
  262. + extern u32 sqlite3SelectTrace;
  263. #endif
  264. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  265. diff -Npur sqlite-version-3.32.2-new/src/window.c sqlite-version-3.32.2/src/window.c
  266. --- sqlite-version-3.32.2-new/src/window.c 2020-06-04 08:58:43.000000000 -0400
  267. +++ sqlite-version-3.32.2/src/window.c 2021-08-04 11:57:45.041230992 -0400
  268. @@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept
  269. */
  270. int sqlite3WindowRewrite(Parse *pParse, Select *p){
  271. int rc = SQLITE_OK;
  272. - if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
  273. + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){
  274. Vdbe *v = sqlite3GetVdbe(pParse);
  275. sqlite3 *db = pParse->db;
  276. Select *pSub = 0; /* The subquery */
  277. diff -Npur sqlite-version-3.32.2-new/test/having.test sqlite-version-3.32.2/test/having.test
  278. --- sqlite-version-3.32.2-new/test/having.test 2020-06-04 08:58:43.000000000 -0400
  279. +++ sqlite-version-3.32.2/test/having.test 2021-08-04 11:57:45.041230992 -0400
  280. @@ -154,5 +154,24 @@ do_execsql_test 4.3 {
  281. SELECT a, sum(b) FROM t3 WHERE nondeter(a) GROUP BY a
  282. } {1 4 2 2}
  283. +#-------------------------------------------------------------------------
  284. +reset_db
  285. +do_execsql_test 5.0 {
  286. + CREATE TABLE t1(a, b);
  287. + CREATE TABLE t2(x, y);
  288. + INSERT INTO t1 VALUES('a', 'b');
  289. +}
  290. +
  291. +# The WHERE clause (a=2), uses an aggregate column from the outer query.
  292. +# If the HAVING term (0) is moved into the WHERE clause in this case,
  293. +# SQLite would at one point optimize (a=2 AND 0) to simply (0). Which
  294. +# is logically correct, but happened to cause problems in aggregate
  295. +# processing for the outer query. This test case verifies that those
  296. +# problems are no longer present.
  297. +do_execsql_test 5.1 {
  298. + SELECT min(b), (
  299. + SELECT x FROM t2 WHERE a=2 GROUP BY y HAVING 0
  300. + ) FROM t1;
  301. +} {b {}}
  302. finish_test
  303. diff -Npur sqlite-version-3.32.2-new/test/selectA.test sqlite-version-3.32.2/test/selectA.test
  304. --- sqlite-version-3.32.2-new/test/selectA.test 2020-06-04 08:58:43.000000000 -0400
  305. +++ sqlite-version-3.32.2/test/selectA.test 2021-08-04 12:29:43.021270055 -0400
  306. @@ -1446,5 +1446,26 @@ do_execsql_test 6.1 {
  307. SELECT * FROM (SELECT a FROM t1 UNION SELECT b FROM t2) WHERE a=a;
  308. } {12345}
  309. +# 2020-06-15 ticket 8f157e8010b22af0
  310. +#
  311. +reset_db
  312. +do_execsql_test 7.1 {
  313. + CREATE TABLE t1(c1); INSERT INTO t1 VALUES(12),(123),(1234),(NULL),('abc');
  314. + CREATE TABLE t2(c2); INSERT INTO t2 VALUES(44),(55),(123);
  315. + CREATE TABLE t3(c3,c4); INSERT INTO t3 VALUES(66,1),(123,2),(77,3);
  316. + CREATE VIEW t4 AS SELECT c3 FROM t3;
  317. + CREATE VIEW t5 AS SELECT c3 FROM t3 ORDER BY c4;
  318. +}
  319. +do_execsql_test 7.2 {
  320. + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t4) AND c1=123;
  321. +} {123 123}
  322. +do_execsql_test 7.3 {
  323. + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t5) AND c1=123;
  324. +} {123 123}
  325. +do_execsql_test 7.4 {
  326. + CREATE TABLE a(b);
  327. + CREATE VIEW c(d) AS SELECT b FROM a ORDER BY b;
  328. + SELECT sum(d) OVER( PARTITION BY(SELECT 0 FROM c JOIN a WHERE b =(SELECT b INTERSECT SELECT d FROM c) AND b = 123)) FROM c;
  329. +} {}
  330. finish_test
  331. diff -Npur sqlite-version-3.32.2-new/test/window1.test sqlite-version-3.32.2/test/window1.test
  332. --- sqlite-version-3.32.2-new/test/window1.test 2020-06-04 08:58:43.000000000 -0400
  333. +++ sqlite-version-3.32.2/test/window1.test 2021-08-04 11:57:45.041230992 -0400
  334. @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 {
  335. WHERE a.c);
  336. } {4 4 4 4}
  337. +#-------------------------------------------------------------------------
  338. +reset_db
  339. +do_execsql_test 54.1 {
  340. + CREATE TABLE t1(a VARCHAR(20), b FLOAT);
  341. + INSERT INTO t1 VALUES('1',10.0);
  342. +}
  343. +
  344. +do_execsql_test 54.2 {
  345. + SELECT * FROM (
  346. + SELECT sum(b) OVER() AS c FROM t1
  347. + UNION
  348. + SELECT b AS c FROM t1
  349. + ) WHERE c>10;
  350. +}
  351. +
  352. +do_execsql_test 54.3 {
  353. + INSERT INTO t1 VALUES('2',5.0);
  354. + INSERT INTO t1 VALUES('3',15.0);
  355. +}
  356. +
  357. +do_execsql_test 54.4 {
  358. + SELECT * FROM (
  359. + SELECT sum(b) OVER() AS c FROM t1
  360. + UNION
  361. + SELECT b AS c FROM t1
  362. + ) WHERE c>10;
  363. +} {15.0 30.0}
  364. +
  365. +# 2020-06-05 ticket c8d3b9f0a750a529
  366. +reset_db
  367. +do_execsql_test 55.1 {
  368. + CREATE TABLE a(b);
  369. + SELECT
  370. + (SELECT b FROM a
  371. + GROUP BY b
  372. + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
  373. + )
  374. + FROM a
  375. + UNION
  376. + SELECT 99
  377. + ORDER BY 1;
  378. +} {99}
  379. finish_test