From: dan Date: Wed, 13 Mar 2024 20:04:11 +0000 (+0000) Subject: Change the way parse.y handles multi-row VALUES clauses to save a few cycles. X-Git-Tag: version-3.46.0~123^2~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=56be6f6c084070026fe720668f644f82fd628f8a;p=thirdparty%2Fsqlite.git Change the way parse.y handles multi-row VALUES clauses to save a few cycles. FossilOrigin-Name: 88d5bc91a52675d2a83748b79ad50de0e9732b2afd23107627bdc604f744d275 --- diff --git a/manifest b/manifest index 1f5591b8ed..86fcabaf5e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Remove\sunreachable\scode\sfrom\sthis\sbranch. -D 2024-03-13T18:41:05.450 +C Change\sthe\sway\sparse.y\shandles\smulti-row\sVALUES\sclauses\sto\ssave\sa\sfew\scycles. +D 2024-03-13T20:04:11.997 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -709,7 +709,7 @@ F src/hash.c 9ee4269fb1d6632a6fecfb9479c93a1f29271bddbbaf215dd60420bcb80c7220 F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 -F src/insert.c 05dd5ea1b8da8a81e1132140fd4dd6d4d9e777ee282799c53cce4f3e9d7dd0ca +F src/insert.c 6323a2da33b5af954f914f33e06af30cce354bbd2553261a4b64e578339e498f F src/json.c e2e40760d6689134c3e2ece38c6a496b34ff5e2661a8f238444a119af666fdce F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 @@ -739,7 +739,7 @@ F src/os_win.c 6ff43bac175bd9ed79e7c0f96840b139f2f51d01689a638fd05128becf94908a F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c ff60e98138d2499082ac6230f01ac508aba545315debccfca2fd6042f5f10fcd F src/pager.h 4b1140d691860de0be1347474c51fee07d5420bd7f802d38cbab8ea4ab9f538a -F src/parse.y 3d022ba4c68bf8c7c4c40b3e364d7a8226c4089fc9ff7a3a95f969f8f1bec959 +F src/parse.y 2ff84e5e346974100b7ed37f01abfd148012e1ce65a6b5a72d5025258077121e F src/pcache.c 040b165f30622a21b7a9a77c6f2e4877a32fb7f22d4c7f0d2a6fa6833a156a75 F src/pcache.h 1497ce1b823cf00094bb0cf3bac37b345937e6f910890c626b16512316d3abf5 F src/pcache1.c 602acb23c471bb8d557a6f0083cc2be641d6cafcafa19e481eba7ef4c9ca0f00 @@ -1921,7 +1921,7 @@ F test/vacuum4.test 7ea76b769fffeb41f925303b04cbcf5a5bbeabe55e4c60ae754ff24eeeb7 F test/vacuum5.test 263b144d537e92ad8e9ca8a73cc6e1583f41cfd0dda9432b87f7806174a2f48c F test/vacuum6.test b137b04bf3392d3f5c3b8fda0ce85a6775a70ca112f6559f74ff52dc9ce042fd F test/vacuummem.test 4b30f5b95a9ff86e9d5c20741e50a898b2dc10b0962a3211571eb165357003fb -F test/values.test 3f0fe0e8859306e9a5cfc800e327278762510c8d5b075d8ddce40f2d13d1405a +F test/values.test 1f0a7f8468d7f087bc4add7a95545afb15e22d5e629a18fbea73b9d76f3ef8bd F test/valuesfault.test 2ef23ed965e3bd08e268cdc38a0d11653390ddbbe1e8e2e98d16f55edd30f6e8 F test/varint.test bbce22cda8fc4d135bcc2b589574be8410614e62 F test/veryquick.test 57ab846bacf7b90cf4e9a672721ea5c5b669b661 @@ -2179,8 +2179,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 2085c7f12a3916ec883c31795e29f2e2b6641c30ecf748cce9bff7b13b061d1f -R 77c42a89dcd308559626c377320e4df2 +P 657c7b4f9df53e041a33cfad00ee2f507ac09c97a2ae61164bd189d660d167da +R c74bcaf2a43ef6c41ceff5cdf5beeb0e U dan -Z 54b19091c4c166a485c83de0a7190e37 +Z dc10b4e059ebbb23528a1aebd608a6c4 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 0dc187c408..e561c09c27 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -657c7b4f9df53e041a33cfad00ee2f507ac09c97a2ae61164bd189d660d167da \ No newline at end of file +88d5bc91a52675d2a83748b79ad50de0e9732b2afd23107627bdc604f744d275 \ No newline at end of file diff --git a/src/insert.c b/src/insert.c index 49ec5e0dc9..9302c10417 100644 --- a/src/insert.c +++ b/src/insert.c @@ -577,6 +577,11 @@ void sqlite3AutoincrementEnd(Parse *pParse){ # define autoIncStep(A,B,C) #endif /* SQLITE_OMIT_AUTOINCREMENT */ +/* +** If argument pVal is a Select object returned by an sqlite3MultiValues() +** that was able to use the co-routine optimization, finish coding the +** co-routine. +*/ void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){ if( pVal && pVal->pSrc->nSrc>0 ){ SrcItem *pItem = &pVal->pSrc->a[0]; @@ -585,7 +590,11 @@ void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){ } } -static int multiValueIsConstant(ExprList *pRow){ +/* +** Return true if all expressions in the expression-list passed as the +** only argument are constant. +*/ +static int exprListIsConstant(ExprList *pRow){ int ii; for(ii=0; iinExpr; ii++){ if( 0==sqlite3ExprIsConstant(pRow->a[ii].pExpr) ) return 0; @@ -593,9 +602,13 @@ static int multiValueIsConstant(ExprList *pRow){ return 1; } -static int multiValueIsConstantNoAff(ExprList *pRow){ +/* +** Return true if all expressions in the expression-list passed as the +** only argument are both constant and have no affinity. +*/ +static int exprListIsNoAffinity(ExprList *pRow){ int ii; - if( multiValueIsConstant(pRow)==0 ) return 0; + if( exprListIsConstant(pRow)==0 ) return 0; for(ii=0; iinExpr; ii++){ assert( pRow->a[ii].pExpr->affExpr==0 ); if( 0!=sqlite3ExprAffinity(pRow->a[ii].pExpr) ) return 0; @@ -606,17 +619,64 @@ static int multiValueIsConstantNoAff(ExprList *pRow){ /* ** This function is called by the parser for the second and subsequent -** rows of a multi-row VALUES clause. +** rows of a multi-row VALUES clause. Argument pLeft is the part of +** the VALUES clause already parsed, argument pRow is the vector of values +** for the new row. The Select object returned represents the complete +** VALUES clause, including the new row. +** +** There are two ways in which this may be achieved - by incremental +** coding of a co-routine (the "co-routine" method) or by returning a +** Select object equivalent to the following (the "UNION ALL" method): +** +** "pLeft UNION ALL SELECT pRow" +** +** If the VALUES clause contains a lot of rows, this compound Select +** object may consume a lot of memory. +** +** When the co-routine method is used, each row that will be returned +** by the VALUES clause is coded into part of a co-routine as it is +** passed to this function. The returned Select object is equivalent to: +** +** SELECT * FROM ( +** Select object to read co-routine +** ) +** +** The co-routine method is used in most cases. Exceptions are: +** +** a) If the current statement has a WITH clause. This is to avoid +** statements like: +** +** WITH cte AS ( VALUES('x'), ('y') ... ) +** SELECT * FROM cte AS a, cte AS b; +** +** This will not work, as the co-routine uses a hard-coded register +** for its OP_Yield instructions, and so it is not possible for two +** cursors to iterate through it concurrently. +** +** b) The schema is currently being parsed (i.e. the VALUES clause is part +** of a schema item like a VIEW or TRIGGER). In this case there is no VM +** being generated when parsing is taking place, and so generating +** a co-routine is not possible. +** +** c) There are non-constant expressions in the VALUES clause (e.g. +** the VALUES clause is part of a correlated sub-query). +** +** d) One or more of the values in the first row of the VALUES clause +** has an affinity (i.e. is a CAST expression). This causes problems +** because the complex rules SQLite uses (see function +** sqlite3SubqueryColumnTypes() in select.c) to determine the effective +** affinity of such a column for all rows require access to all values in +** the column simultaneously. */ Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){ - if( pLeft->pPrior - || pParse->bHasWith - || pParse->db->init.busy - || multiValueIsConstant(pRow)==0 - || (pLeft->pSrc->nSrc==0 && multiValueIsConstantNoAff(pLeft->pEList)==0) + if( pLeft->pPrior /* co-routine precluded by prior row */ + || pParse->bHasWith /* condition (a) above */ + || pParse->db->init.busy /* condition (b) above */ + || exprListIsConstant(pRow)==0 /* condition (c) above */ + || (pLeft->pSrc->nSrc==0 && exprListIsNoAffinity(pLeft->pEList)==0) /* (d) */ ){ - /* This row of the VALUES clause cannot be coded immediately. */ + /* The co-routine method cannot be used. Fall back to UNION ALL. */ Select *pSelect = 0; int f = SF_Values | SF_MultiValue; if( pLeft->pSrc->nSrc ){ @@ -634,14 +694,14 @@ Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){ pLeft = pSelect; } }else{ - SrcItem *p = 0; + SrcItem *p = 0; /* SrcItem that reads from co-routine */ if( pLeft->pSrc->nSrc==0 ){ - /* Co-routine has not yet been started. */ + /* Co-routine has not yet been started and the special Select object + ** that accesses the co-routine has not yet been created. This block + ** does both those things. */ Vdbe *v = sqlite3GetVdbe(pParse); - Select *pRet; - - pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0); + Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0); if( pRet ){ SelectDest dest; pRet->pSrc->nSrc = 1; diff --git a/src/parse.y b/src/parse.y index 515d512f2c..36566d28aa 100644 --- a/src/parse.y +++ b/src/parse.y @@ -562,13 +562,10 @@ cmd ::= select(X). { } %ifndef SQLITE_OMIT_CTE -select(A) ::= withkw wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} -select(A) ::= withkw RECURSIVE wqlist(W) selectnowith(X). +select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} +select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} -withkw ::= WITH. { - pParse->bHasWith = 1; -} %endif /* SQLITE_OMIT_CTE */ select(A) ::= selectnowith(A). { Select *p = A; @@ -626,16 +623,26 @@ oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) %endif -oneselect(A) ::= values(A). { - sqlite3MultiValuesEnd(pParse, A); -} - +// Single row VALUES clause. +// %type values {Select*} +oneselect(A) ::= values(A). %destructor values {sqlite3SelectDelete(pParse->db, $$);} values(A) ::= VALUES LP nexprlist(X) RP. { A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0); } -values(A) ::= values(A) COMMA LP nexprlist(Y) RP. { + +// Multiple row VALUES clause. +// +%type mvalues {Select*} +oneselect(A) ::= mvalues(A). { + sqlite3MultiValuesEnd(pParse, A); +} +%destructor mvalues {sqlite3SelectDelete(pParse->db, $$);} +mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. { + A = sqlite3MultiValues(pParse, A, Y); +} +mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. { A = sqlite3MultiValues(pParse, A, Y); } @@ -1751,16 +1758,17 @@ anylist ::= anylist ANY. with ::= . %ifndef SQLITE_OMIT_CTE -with ::= withkw wqlist(W). { sqlite3WithPush(pParse, W, 1); } -with ::= withkw RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } +with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } +with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } %type wqas {u8} wqas(A) ::= AS. {A = M10d_Any;} wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} -wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { +wqitem(A) ::= withnm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ } +withnm(A) ::= nm(A). {pParse->bHasWith = 1;} wqlist(A) ::= wqitem(X). { A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ } diff --git a/test/values.test b/test/values.test index 627987b43b..7c260260d0 100644 --- a/test/values.test +++ b/test/values.test @@ -267,5 +267,18 @@ do_execsql_test 6.2 { VALUES(CAST(44 AS REAL)),(55); } {44.0 55} +#------------------------------------------------------------------------ +do_execsql_test 7.1 { + WITH x1(a, b) AS ( + VALUES(1, 2), ('a', 'b') + ) + SELECT * FROM x1 one, x1 two +} { + 1 2 1 2 + 1 2 a b + a b 1 2 + a b a b +} + finish_test