/ Check-in [a92aee55]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix a problem causing incorrect code to be generated for IN constraints like "a IN (1, 2, 3)" where column "a" is a rowid column with an extra UNIQUE index created on it. Ticket [0eab1ac759].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a92aee5520cfaf85ae896365a7e42bdd981f828d
User & Date: dan 2016-09-16 16:30:57
Context
2016-09-16
18:53
Remove the #ifdef SQLITE_DEBUG from around the testcase_glob() routine in the command-line shell. check-in: 9885dac4 user: drh tags: trunk
17:50
Merge recent changes from trunk. check-in: e3d9efa2 user: drh tags: apple-osx
16:30
Fix a problem causing incorrect code to be generated for IN constraints like "a IN (1, 2, 3)" where column "a" is a rowid column with an extra UNIQUE index created on it. Ticket [0eab1ac759]. check-in: a92aee55 user: dan tags: trunk
15:42
Replace a faulty assert() with a testcase() to assure the condition is tested. Ticket [0eab1ac7591f]. check-in: a49bc0a8 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/wherecode.c.

521
522
523
524
525
526
527
528
529

530
531
532
533
534
535
536
537
538
539
540
541
542
       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
    pIn = pLevel->u.in.aInLoop;
    if( pIn ){
      int iMap = 0;               /* Index in aiMap[] */
      pIn += i;
      for(i=iEq;i<pLoop->nLTerm; i++){
        int iOut = iReg;
        if( pLoop->aLTerm[i]->pExpr==pX ){

          if( eType==IN_INDEX_ROWID ){
            testcase( nEq>1 );  /* Happens with a UNIQUE index on ROWID */
            pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
          }else{
            int iCol = aiMap ? aiMap[iMap++] : 0;
            iOut = iReg + i - iEq;
            pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
          }
          sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
          if( i==iEq ){
            pIn->iCur = iTab;
            pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
          }else{







<

>


|


<







521
522
523
524
525
526
527

528
529
530
531
532
533
534

535
536
537
538
539
540
541
       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
    pIn = pLevel->u.in.aInLoop;
    if( pIn ){
      int iMap = 0;               /* Index in aiMap[] */
      pIn += i;
      for(i=iEq;i<pLoop->nLTerm; i++){

        if( pLoop->aLTerm[i]->pExpr==pX ){
          int iOut = iReg + i - iEq;
          if( eType==IN_INDEX_ROWID ){
            testcase( nEq>1 );  /* Happens with a UNIQUE index on ROWID */
            pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
          }else{
            int iCol = aiMap ? aiMap[iMap++] : 0;

            pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
          }
          sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
          if( i==iEq ){
            pIn->iCur = iTab;
            pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
          }else{

Changes to test/in5.test.

210
211
212
213
214
215
216
217



218



219
220














221
  CREATE INDEX y2c ON y2(c);
  SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2);
} {1 3}
do_execsql_test 7.3.2 {
  SELECT a FROM y1 WHERE b IN (SELECT a FROM y2);
} {two}

finish_test























finish_test







<
>
>
>
|
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>

210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  CREATE INDEX y2c ON y2(c);
  SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2);
} {1 3}
do_execsql_test 7.3.2 {
  SELECT a FROM y1 WHERE b IN (SELECT a FROM y2);
} {two}


#-------------------------------------------------------------------------
# Tests to confirm that indexes on the rowid column do not confuse
# the query planner. See ticket [0eab1ac7591f511d].
#
do_execsql_test 8.0 {
  CREATE TABLE n1(a INTEGER PRIMARY KEY, b VARCHAR(500));
  CREATE UNIQUE INDEX n1a ON n1(a);
}

do_execsql_test 8.1 {
  SELECT count(*) FROM n1 WHERE a IN (1, 2, 3)
} 0
do_execsql_test 8.2 {
  SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1)
} 0
do_execsql_test 8.3 {
  INSERT INTO n1 VALUES(1, NULL), (2, NULL), (3, NULL);
  SELECT count(*) FROM n1 WHERE a IN (1, 2, 3)
} 3
do_execsql_test 8.4 {
  SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1)
} 3

finish_test