Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -307,10 +307,21 @@ k = 0; pScan->iEquiv++; } return 0; } + +/* +** This is whereScanInit() for the case of an index on an expression. +** It is factored out into a separate tail-recursion subroutine so that +** the normal whereScanInit() routine, which is a high-runner, does not +** need to push registers onto the stack as part of its prologue. +*/ +static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){ + pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr); + return whereScanNext(pScan); +} /* ** Initialize a WHERE clause scanner object. Return a pointer to the ** first match. Return NULL if there are no matches. ** @@ -340,31 +351,33 @@ pScan->pOrigWC = pWC; pScan->pWC = pWC; pScan->pIdxExpr = 0; pScan->idxaff = 0; pScan->zCollName = 0; + pScan->opMask = opMask; + pScan->k = 0; + pScan->aiCur[0] = iCur; + pScan->nEquiv = 1; + pScan->iEquiv = 1; if( pIdx ){ int j = iColumn; iColumn = pIdx->aiColumn[j]; if( iColumn==XN_EXPR ){ pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; pScan->zCollName = pIdx->azColl[j]; + pScan->aiColumn[0] = XN_EXPR; + return whereScanInitIndexExpr(pScan); }else if( iColumn==pIdx->pTable->iPKey ){ iColumn = XN_ROWID; }else if( iColumn>=0 ){ pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; pScan->zCollName = pIdx->azColl[j]; } }else if( iColumn==XN_EXPR ){ return 0; } - pScan->opMask = opMask; - pScan->k = 0; - pScan->aiCur[0] = iCur; pScan->aiColumn[0] = iColumn; - pScan->nEquiv = 1; - pScan->iEquiv = 1; return whereScanNext(pScan); } /* ** Search for a term in the WHERE clause that is of the form "X " Index: test/indexexpr2.test ================================================================== --- test/indexexpr2.test +++ test/indexexpr2.test @@ -247,8 +247,36 @@ } do_execsql_test 5.4 { SELECT * FROM t5 WHERE abs(a)=2 or abs(b)=9; } {2 4 3 9} +#------------------------------------------------------------------------- +do_execsql_test 6.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b); + INSERT INTO x1 VALUES + (1, 123), (2, '123'), (3, '123abc'), (4, 123.0), (5, 1234); +} + +do_execsql_test 6.1.1 { + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {1 123 2 123 3 123abc 4 123.0} +do_execsql_test 6.1.2 { + CREATE INDEX x1i ON x1( CAST(b AS INTEGER) ); + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {1 123 2 123 3 123abc 4 123.0} +do_eqp_test 6.1.3 { + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {SEARCH TABLE x1 USING INDEX x1i (=?)} + +do_execsql_test 6.2.1 { + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {1 123 2 123} +do_execsql_test 6.2.2 { + CREATE INDEX x1i2 ON x1( CAST(b AS TEXT) ); + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {1 123 2 123} +do_eqp_test 6.2.3 { + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {SEARCH TABLE x1 USING INDEX x1i2 (=?)} finish_test