From d564bdb0507aabce5d64905734d5d7e050a2c86d Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 5 Oct 2024 18:10:02 +0000 Subject: [PATCH] Allow expressions with subtypes to be read from indexes unless they are being used as direct or indirect parameters to SQLITE_SUBTYPE functions. FossilOrigin-Name: aa440e78e9004c7ca3e03beaf264f54d0070ad7298a3c96ca097d8b35c872e5f --- manifest | 21 +++++++++------------ manifest.uuid | 2 +- src/expr.c | 8 +------- src/resolve.c | 18 ++++++++++++++++++ src/sqliteInt.h | 2 +- test/indexexpr3.test | 25 +++++++++++++++++++------ 6 files changed, 49 insertions(+), 27 deletions(-) diff --git a/manifest b/manifest index 37b5b5e282..4f91484dbd 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Experimental\schange\sto\sallow\sexpressions\swith\ssubtypes\sto\sbe\sread\sfrom\sindexes\sin\ssituations\swhere\sthey\sare\snot\sused\sas\sfunction\sparameters. -D 2024-10-05T17:37:19.432 +C Allow\sexpressions\swith\ssubtypes\sto\sbe\sread\sfrom\sindexes\sunless\sthey\sare\sbeing\sused\sas\sdirect\sor\sindirect\sparameters\sto\sSQLITE_SUBTYPE\sfunctions. +D 2024-10-05T18:10:02.045 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -719,7 +719,7 @@ F src/date.c 89ce1ff20512a7fa5070ba6e7dd5c171148ca7d580955795bf97c79c2456144a F src/dbpage.c db1be8adaf1f839ad733c08baeac5c22aa912f7b535865c0c061382602081360 F src/dbstat.c 73362c0df0f40ad5523a6f5501224959d0976757b511299bf892313e79d14f5c F src/delete.c 03a77ba20e54f0f42ebd8eddf15411ed6bdb06a2c472ac4b6b336521bf7cea42 -F src/expr.c 9441dc9f9f4dd3a172270d3dbf476d709aa82ca9bab73a5f3bb878654afe4424 +F src/expr.c 0aafe1b0d3893e9f568f30efa2e7b96a6e6bcc072e481ae68c5abe3f01d81367 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 928ed2517e8732113d2b9821aa37af639688d752f4ea9ac6e0e393d713eeb76f F src/func.c df400a1d3f4625997d4dd8a81951c303e066277c29b861d37e03cd152d7858dd @@ -767,14 +767,14 @@ F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7 F src/prepare.c 3ba0ad907b7773ed642f66cea8a2c9c8edc18841aa1050b6218dbb3479e86225 F src/printf.c 6a87534ebfb9e5346011191b1f3a7ebc457f5938c7e4feeea478ecf53f6a41b2 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c -F src/resolve.c 9750a281f7ba073b4e6da2be1a6c4071f5d841a7746c5fb3f70d6d793b6675ea +F src/resolve.c c8a5372b97b2a2e972a280676f06ddb5b74e885d3b1f5ce383f839907b57ef68 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 F src/select.c 4b14337a2742f0c0beeba490e9a05507e9b4b12184b9cd12773501d08d48e3fe F src/shell.c.in 981efe98f98a983c1d0193d18528eb2d765207c0c82b67b610be60f17995a43e F src/sqlite.h.in 1def838497ad53c81486649ce79821925d1ac20a9843af317a344d507efe116e F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h 3f046c04ea3595d6bfda99b781926b17e672fd6d27da2ba6d8d8fc39981dcb54 -F src/sqliteInt.h 40552bd22f4bc3330f2a73633d12f12863c5aa4e409af75f8ebfaeb794b11e4f +F src/sqliteInt.h ad02397dc4d22b77f9a331412d46e4c1e49459dd386fba8373fa148998e1e7d0 F src/sqliteLimit.h 6878ab64bdeb8c24a1d762d45635e34b96da21132179023338c93f820eee6728 F src/status.c cb11f8589a6912af2da3bb1ec509a94dd8ef27df4d4c1a97e0bcf2309ece972b F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 @@ -1324,7 +1324,7 @@ F test/indexA.test 11d84f6995e6e5b9d8315953fb1b6d29772ee7c7803ee9112715e7e4dd3e4 F test/indexedby.test f21eca4f7a6ffe14c8500a7ad6cd53166666c99e5ccd311842a28bc94a195fe0 F test/indexexpr1.test 24fa85a12da384dd1d56f7b24e593c51a8a54b4c5e2e8bbb9e5fdf1099427faf F test/indexexpr2.test 1c382e81ef996d8ae8b834a74f2a9013dddf59214c32201d7c8a656d739f999a -F test/indexexpr3.test 7689d11a60dca09879f490db5fd26b9171a57e31d97282d9963e944b1536f676 +F test/indexexpr3.test 9d893bf440937ebcc1e59c7c9c1505c40c918346a3ddde76a69078f3c733c45d F test/indexfault.test 98d78a8ff1f5335628b62f886a1cb7c7dac1ef6d48fa39c51ec871c87dce9811 F test/init.test 15c823093fdabbf7b531fe22cf037134d09587a7 F test/insert.test 4e3f0de67aac3c5be1f4aaedbcea11638f1b5cdc9a3115be14d19aa9db7623c6 @@ -2216,11 +2216,8 @@ F vsixtest/vsixtest.tcl 6195aba1f12a5e10efc2b8c0009532167be5e301abe5b31385638080 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 2f7eab381e16760952d1c90a9119d2a217933f0136442d8f6eeb6d95e366ca4f -R 05b36287276c4340f7e35170606a310b -T *branch * indexed-subtype-expr -T *sym-indexed-subtype-expr * -T -sym-trunk * +P ac63f98ad85a4dd1e49cc64b41f0ca0044153972c15d71c669f4bc3ec590e268 +R d7f4847c66a2bc32b5fcb862258f0d6b U dan -Z 943b26137ccac8e38cc1774f405e6dc0 +Z f387b8ba9011d1385bb9a2c14c76e4d1 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index a0670d9728..2574038fb7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ac63f98ad85a4dd1e49cc64b41f0ca0044153972c15d71c669f4bc3ec590e268 +aa440e78e9004c7ca3e03beaf264f54d0070ad7298a3c96ca097d8b35c872e5f diff --git a/src/expr.c b/src/expr.c index 83b1ff56cd..506b92c828 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1164,7 +1164,6 @@ Expr *sqlite3ExprFunction( ){ Expr *pNew; sqlite3 *db = pParse->db; - int ii; assert( pToken ); pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); if( pNew==0 ){ @@ -1179,11 +1178,6 @@ Expr *sqlite3ExprFunction( ){ sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken); } - if( pList && pParse->nErr==0 ){ - for(ii=0; iinExpr; ii++){ - ExprSetProperty(pList->a[ii].pExpr, EP_FuncArg); - } - } pNew->x.pList = pList; ExprSetProperty(pNew, EP_HasFunc); assert( ExprUseXList(pNew) ); @@ -4650,7 +4644,7 @@ static SQLITE_NOINLINE int sqlite3IndexedExprLookup( ** value taken from an expression index if they are themselves an ** argument to another scalar function or aggregate. ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */ - if( ExprHasProperty(pExpr, EP_FuncArg) + if( ExprHasProperty(pExpr, EP_SubtArg) && sqlite3ExprCanReturnSubtype(pParse, pExpr) ){ continue; diff --git a/src/resolve.c b/src/resolve.c index 8e8da66910..d6a5144af8 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -1183,6 +1183,24 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ } } #endif + + /* If the function may call sqlite3_value_subtype(), then set the + ** EP_SubtArg flag on all of its argument expressions. This prevents + ** where.c from replacing the expression with a value read from an + ** index on the same expression, which will not have the correct + ** subtype. Also set the flag if the function expression itself is + ** an EP_SubtArg expression. In this case subtypes are required as + ** the function may return a value with a subtype back to its + ** caller using sqlite3_result_value(). */ + if( (pDef->funcFlags & SQLITE_SUBTYPE) + || ExprHasProperty(pExpr, EP_SubtArg) + ){ + int ii; + for(ii=0; iia[ii].pExpr, EP_SubtArg); + } + } + if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){ /* For the purposes of the EP_ConstFunc flag, date and time ** functions and other functions that change slowly are considered diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 8288786700..f88b9e67c3 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -3104,7 +3104,7 @@ struct Expr { #define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */ #define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */ #define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */ -#define EP_FuncArg 0x80000000 /* Is a function() argument */ +#define EP_SubtArg 0x80000000 /* Is argument to SQLITE_SUBTYPE function */ /* The EP_Propagate mask is a set of properties that automatically propagate ** upwards into parent nodes. diff --git a/test/indexexpr3.test b/test/indexexpr3.test index e3cfcd116a..21e1d329ad 100644 --- a/test/indexexpr3.test +++ b/test/indexexpr3.test @@ -28,15 +28,15 @@ do_execsql_test 1.0 { } proc do_hasfunction_test {tn sql res} { - set bFunction 0 + set nFunction 0 db eval "EXPLAIN $sql" x { if {$x(opcode)=="Function"} { - set bFunction 1 + incr nFunction } } do_execsql_test $tn " - SELECT $bFunction; + SELECT $nFunction; $sql " $res } @@ -54,17 +54,30 @@ do_hasfunction_test 1.2 { } do_hasfunction_test 1.3 { - SELECT coalesce(json_extract(j, '$.x'), 0) FROM t1 WHERE a=2 + SELECT coalesce(json_extract(j, '$.x'), 'five') FROM t1 WHERE a=2 } { - 1 two + 0 two } -do_hasfunction_test 1.3 { +do_hasfunction_test 1.4 { SELECT json_extract(j, '$.x') || '.two' FROM t1 WHERE a=2 } { 0 two.two } +do_hasfunction_test 1.5 { + SELECT json_insert( '{}', '$.y', json_extract(j, '$.x') ) FROM t1 WHERE a=2 +} { + 2 {{"y":"two"}} +} + +do_hasfunction_test 1.6 { + SELECT json_insert( '{}', '$.y', coalesce( json_extract(j, '$.x'), 'five' ) ) + FROM t1 WHERE a=2 +} { + 2 {{"y":"two"}} +} + finish_test