diff options
author | dan <Dan Kennedy> | 2021-02-26 20:39:08 +0000 |
---|---|---|
committer | dan <Dan Kennedy> | 2021-02-26 20:39:08 +0000 |
commit | eca6d60bfa384fbb65ae1261d55c0ba35b557f90 (patch) | |
tree | 276114bb703d38da69e90129f4a64cf9ad321c70 /src/resolve.c | |
parent | 4b17455ab2f38d51ab0dcd6f01dd8d99c8229e8c (diff) | |
parent | 8ddf686267c58ee0e519cfd9024f177e606be0ab (diff) | |
download | sqlite-eca6d60bfa384fbb65ae1261d55c0ba35b557f90.tar.gz sqlite-eca6d60bfa384fbb65ae1261d55c0ba35b557f90.zip |
Attempt to optimize "x IS NULL" and "x IS NOT NULL" expressions when x is a column with a NOT NULL constraint.
FossilOrigin-Name: de9c86c9e4cdb34f4b7d65f160d1e589fb969bbf64c66d2b24c502d1ee424dbb
Diffstat (limited to 'src/resolve.c')
-rw-r--r-- | src/resolve.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/resolve.c b/src/resolve.c index 689f19703..203253c6e 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -785,6 +785,44 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ break; } + /* An "<expr> IS NOT NULL" or "<expr> IS NULL". After resolving the + ** LHS, check if there is a NOT NULL constraint in the schema that + ** means the value of the expression can be determined immediately. + ** If that is the case, replace the current expression node with + ** a TK_TRUEFALSE node. + ** + ** If the node is replaced with a TK_TRUEFALSE node, then also restore + ** the NameContext ref-counts to the state they where in before the + ** LHS expression was resolved. This prevents the current select + ** from being erroneously marked as correlated in some cases. + */ + case TK_NOTNULL: + case TK_ISNULL: { + int anRef[8]; + NameContext *p; + int i; + for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){ + anRef[i] = p->nRef; + } + sqlite3WalkExpr(pWalker, pExpr->pLeft); + if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) ){ + if( pExpr->op==TK_NOTNULL ){ + pExpr->u.zToken = "true"; + ExprSetProperty(pExpr, EP_IsTrue); + }else{ + pExpr->u.zToken = "false"; + ExprSetProperty(pExpr, EP_IsFalse); + } + pExpr->op = TK_TRUEFALSE; + for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){ + p->nRef = anRef[i]; + } + sqlite3ExprDelete(pParse->db, pExpr->pLeft); + pExpr->pLeft = 0; + } + return WRC_Prune; + } + /* A column name: ID ** Or table name and column name: ID.ID ** Or a database, table and column: ID.ID.ID |