From: drh <> Date: Sat, 28 Jun 2025 17:59:15 +0000 (+0000) Subject: Improve the bytecode generated for comparisons so that if one operand is X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=311d73efc27a72478cdb6672ff2685306c2dea3e;p=thirdparty%2Fsqlite.git Improve the bytecode generated for comparisons so that if one operand is a subquery and the other operand evaluates to NULL, the subquery operand is not even computed. This fixes 5 of the 12 slow queries described in [forum:/forumpost/52651713ac|forum post 52651713ac]. FossilOrigin-Name: f147bc04776ac0056412f69dfc518016c0d5b4e9d964664e3d88f595fb29dbe0 --- diff --git a/manifest b/manifest index 361c6e5174..fcdf5f0344 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Raise\san\serror\sright\saway\sif\sthe\snumber\sof\saggregate\sterms\sin\sa\squery\nexceeds\sthe\smaximum\snumber\sof\scolumns. -D 2025-06-27T19:02:21.291 +C Improve\sthe\sbytecode\sgenerated\sfor\scomparisons\sso\sthat\sif\sone\soperand\sis\na\ssubquery\sand\sthe\sother\soperand\sevaluates\sto\sNULL,\sthe\ssubquery\soperand\nis\snot\seven\scomputed.\s\sThis\sfixes\s5\sof\sthe\s12\sslow\squeries\sdescribed\nin\s[forum:/forumpost/52651713ac|forum\spost\s52651713ac]. +D 2025-06-28T17:59:15.236 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -736,7 +736,7 @@ F src/date.c 9db4d604e699a73e10b8e85a44db074a1f04c0591a77e2abfd77703f50dce1e9 F src/dbpage.c b3e218f8ed74fcbb7fa805df8ca669a3718d397617b3d8a8aac3307dc315c4d6 F src/dbstat.c 73362c0df0f40ad5523a6f5501224959d0976757b511299bf892313e79d14f5c F src/delete.c 03a77ba20e54f0f42ebd8eddf15411ed6bdb06a2c472ac4b6b336521bf7cea42 -F src/expr.c dbb55f616dc6e5ad3507bb62ef609a48aaf1f909e40d6878d57d7626213933c5 +F src/expr.c b46621069e36a2fbc966215691e4775134e2cc6c7a6e9e54b01cbb70b51d57a5 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 928ed2517e8732113d2b9821aa37af639688d752f4ea9ac6e0e393d713eeb76f F src/func.c de47a8295503aa130baae5e6d9868ecf4f7c4dbffa65d83ad1f70bdbac0ee2d6 @@ -2208,8 +2208,11 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P c9ddd15b0197e6e5c5a74581d94cf986523514ffdb28c66ba18de9a22aec97e9 -R e1a26ca0e980fdc17e9e03398afd0539 +P 5508b56fd24016c13981ec280ecdd833007c9d8dd595edb295b984c2b487b5c8 +R c5ec83310afc6f17c7231575eaf1e606 +T *branch * optimize-null-values +T *sym-optimize-null-values * +T -sym-trunk * U drh -Z 007c4fc453decb57a623a0c9c1fba3c7 +Z 4eeee81d77d56831be900c22e39d8eec # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index cb6314c3f1..19505125a7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5508b56fd24016c13981ec280ecdd833007c9d8dd595edb295b984c2b487b5c8 +f147bc04776ac0056412f69dfc518016c0d5b4e9d964664e3d88f595fb29dbe0 diff --git a/src/expr.c b/src/expr.c index 6dcf8c3ac3..b4461cf5c6 100644 --- a/src/expr.c +++ b/src/expr.c @@ -6088,10 +6088,48 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ case TK_GE: case TK_NE: case TK_EQ: { + int r2Done = 0; + int addrBypass = 0; if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; testcase( jumpIfNull==0 ); + /* + ** If the left operand is a (possibly expensive) subquery and the + ** right operand is not and the right operation might be NULL and + ** the operator is not IS or IS NOT, then compute the right operand + ** first and skip the computation of the left operand if the right + ** operand is NULL. + */ + if( jumpIfNull!=SQLITE_NULLEQ + && ExprHasProperty(pExpr->pLeft, EP_Subquery) + && !ExprHasProperty(pExpr->pRight, EP_Subquery) + && sqlite3ExprCanBeNull(pExpr->pRight) + ){ + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + addrBypass = sqlite3VdbeAddOp2(v, OP_IsNull, r2, dest); + VdbeCoverageIf(v, jumpIfNull==SQLITE_JUMPIFNULL); + VdbeCoverageIf(v, jumpIfNull!=SQLITE_JUMPIFNULL); + if( jumpIfNull==SQLITE_JUMPIFNULL ) addrBypass = 0; + r2Done = 1; + } r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + if( !r2Done ){ + /* + ** If the right operand is a subquery and the left operand is not + ** and the left operand might be NULL and the comparison operator + ** is not IS or IS NOT, then check the left operand to see if it is + ** NULL and skip the computation of the right operand if it is. + */ + if( jumpIfNull!=SQLITE_NULLEQ + && ExprHasProperty(pExpr->pRight, EP_Subquery) + && sqlite3ExprCanBeNull(pExpr->pLeft) + ){ + addrBypass = sqlite3VdbeAddOp2(v, OP_IsNull, r1, dest); + VdbeCoverageIf(v, jumpIfNull==SQLITE_JUMPIFNULL); + VdbeCoverageIf(v, jumpIfNull!=SQLITE_JUMPIFNULL); + if( jumpIfNull==SQLITE_JUMPIFNULL ) addrBypass = 0; + } + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + } codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)); assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); @@ -6106,6 +6144,7 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); testcase( regFree1==0 ); testcase( regFree2==0 ); + if( addrBypass ) sqlite3VdbeJumpHere(v, addrBypass); break; } case TK_ISNULL: