mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-08 14:02:16 +03:00
The NOT NULL strength reduction optimization from [de9c86c9e4cdb34f] should
be applied to the WHERE clause only. Otherwise, the operand of the IS NULL or IS NOT NULL operator might be a reference to a bare column of an aggregate table, and we can't tell if it is NULL or not based only on its NOT NULL attribute. [forum:/forumpost/440f2a2f17|Forum post 440f2a2f17]. FossilOrigin-Name: 51704feae224eff601db5607f8651da11b3c2ed8a58ffe5b6ee8260cab50695b
This commit is contained in:
@@ -971,6 +971,19 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
||||
** resolved. This prevents "column" from being counted as having been
|
||||
** referenced, which might prevent a SELECT from being erroneously
|
||||
** marked as correlated.
|
||||
**
|
||||
** 2024-03-28: Beware of aggregates. A bare column of aggregated table
|
||||
** can still evaluate to NULL even though it is marked as NOT NULL.
|
||||
** Example:
|
||||
**
|
||||
** CREATE TABLE t1(a INT NOT NULL);
|
||||
** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1;
|
||||
**
|
||||
** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized
|
||||
** here because at the time this case is hit, we do not yet know whether
|
||||
** or not t1 is being aggregated. We have to assume the worst and omit
|
||||
** the optimization. The only time it is safe to apply this optimization
|
||||
** is within the WHERE clause.
|
||||
*/
|
||||
case TK_NOTNULL:
|
||||
case TK_ISNULL: {
|
||||
@@ -981,19 +994,36 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
|
||||
anRef[i] = p->nRef;
|
||||
}
|
||||
sqlite3WalkExpr(pWalker, pExpr->pLeft);
|
||||
if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
|
||||
testcase( ExprHasProperty(pExpr, EP_OuterON) );
|
||||
assert( !ExprHasProperty(pExpr, EP_IntValue) );
|
||||
pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
|
||||
pExpr->flags |= EP_IntValue;
|
||||
pExpr->op = TK_INTEGER;
|
||||
|
||||
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;
|
||||
if( IN_RENAME_OBJECT ) return WRC_Prune;
|
||||
if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
|
||||
/* The expression can be NULL. So the optimization does not apply */
|
||||
return WRC_Prune;
|
||||
}
|
||||
|
||||
for(i=0, p=pNC; p; p=p->pNext, i++){
|
||||
if( (p->ncFlags & NC_Where)==0 ){
|
||||
return WRC_Prune; /* Not in a WHERE clause. Unsafe to optimize. */
|
||||
}
|
||||
}
|
||||
testcase( ExprHasProperty(pExpr, EP_OuterON) );
|
||||
assert( !ExprHasProperty(pExpr, EP_IntValue) );
|
||||
#if TREETRACE_ENABLED
|
||||
if( sqlite3TreeTrace & 0x80000 ){
|
||||
sqlite3DebugPrintf(
|
||||
"NOT NULL strength reduction converts the following to %d:\n",
|
||||
pExpr->op==TK_NOTNULL
|
||||
);
|
||||
sqlite3ShowExpr(pExpr);
|
||||
}
|
||||
#endif /* TREETRACE_ENABLED */
|
||||
pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
|
||||
pExpr->flags |= EP_IntValue;
|
||||
pExpr->op = TK_INTEGER;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1891,7 +1921,9 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
|
||||
}
|
||||
if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
|
||||
}
|
||||
sNC.ncFlags |= NC_Where;
|
||||
if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
|
||||
sNC.ncFlags &= ~NC_Where;
|
||||
|
||||
/* Resolve names in table-valued-function arguments */
|
||||
for(i=0; i<p->pSrc->nSrc; i++){
|
||||
|
@@ -1125,6 +1125,7 @@ extern u32 sqlite3TreeTrace;
|
||||
** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
|
||||
** 0x00020000 Transform DISTINCT into GROUP BY
|
||||
** 0x00040000 SELECT tree dump after all code has been generated
|
||||
** 0x00080000 NOT NULL strength reduction
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -3453,6 +3454,7 @@ struct NameContext {
|
||||
#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */
|
||||
#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */
|
||||
#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
|
||||
#define NC_Where 0x100000 /* Processing WHERE clause of a SELECT */
|
||||
#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user