aboutsummaryrefslogtreecommitdiff
path: root/src/resolve.c
diff options
context:
space:
mode:
authordan <Dan Kennedy>2021-02-26 20:14:32 +0000
committerdan <Dan Kennedy>2021-02-26 20:14:32 +0000
commit8ddf686267c58ee0e519cfd9024f177e606be0ab (patch)
tree2972477484a91b982f5b2d4e005d4199550ee208 /src/resolve.c
parent4b17455ab2f38d51ab0dcd6f01dd8d99c8229e8c (diff)
downloadsqlite-8ddf686267c58ee0e519cfd9024f177e606be0ab.tar.gz
sqlite-8ddf686267c58ee0e519cfd9024f177e606be0ab.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: 5ecd842555009ce27ee6390325ac5c2504143474b12b730933f0833b3dad788a
Diffstat (limited to 'src/resolve.c')
-rw-r--r--src/resolve.c38
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