/ Check-in [247f3899]
Login

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

Overview
Comment:Change the sqlite3OpenTable() utility to open the PRIMARY KEY index when reading a WITHOUT ROWID table.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | omit-rowid
Files: files | file ages | folders
SHA1: 247f389930aededaa54ecb792748aa8d0ad6d57b
User & Date: drh 2013-10-23 23:37:02
Context
2013-10-24
00:18
Correctly handle queries that use secondary indices of WITHOUT ROWID tables. check-in: d8bc8595 user: drh tags: omit-rowid
2013-10-23
23:37
Change the sqlite3OpenTable() utility to open the PRIMARY KEY index when reading a WITHOUT ROWID table. check-in: 247f3899 user: drh tags: omit-rowid
22:23
Construct secondary indices on WITHOUT ROWID tables. check-in: 2c028ddc user: drh tags: omit-rowid
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

  2627   2627   
  2628   2628     /* Open the sorter cursor if we are to use one. */
  2629   2629     iSorter = pParse->nTab++;
  2630   2630     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
  2631   2631   
  2632   2632     /* Open the table. Loop through all rows of the table, inserting index
  2633   2633     ** records into the sorter. */
  2634         -  if( HasRowid(pTab) ){
  2635         -    sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2636         -  }else{
  2637         -    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  2638         -    assert( pPk!=0 );
  2639         -    assert( pPk->tnum=pTab->tnum );
  2640         -    sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pPk->tnum, iDb,
  2641         -                     (char*)sqlite3IndexKeyinfo(pParse, pPk),
  2642         -                     P4_KEYINFO_HANDOFF);
  2643         -  }
         2634  +  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2644   2635     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2645   2636     regRecord = sqlite3GetTempReg(pParse);
  2646   2637   
  2647   2638     sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1, &iPartIdxLabel);
  2648   2639     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
  2649   2640     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
  2650   2641     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);

Changes to src/insert.c.

    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17     17   /*
    18         -** Generate code that will open a table for reading.
           18  +** Generate code that will open table pTab for reading or writing
           19  +** on cursor iCur.
           20  +**
           21  +** Always acquire a table lock.  Always do the open for rowid tables.
           22  +** For WITHOUT ROWID tables, only do read opens, and then open the
           23  +** PRIMARY KEY index, not the main table, since the main table doesn't
           24  +** exist.
    19     25   */
    20     26   void sqlite3OpenTable(
    21     27     Parse *p,       /* Generate code into this VDBE */
    22     28     int iCur,       /* The cursor number of the table */
    23     29     int iDb,        /* The database index in sqlite3.aDb[] */
    24     30     Table *pTab,    /* The table to be opened */
    25     31     int opcode      /* OP_OpenRead or OP_OpenWrite */
................................................................................
    26     32   ){
    27     33     Vdbe *v;
    28     34     assert( !IsVirtual(pTab) );
    29     35     v = sqlite3GetVdbe(p);
    30     36     assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
    31     37     sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
    32     38     if( HasRowid(pTab) ){
    33         -    sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
    34         -    sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
           39  +    sqlite3VdbeAddOp4(v, opcode, iCur, pTab->tnum, iDb,
           40  +                      SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
           41  +    VdbeComment((v, "%s", pTab->zName));
           42  +  }else if( opcode==OP_OpenRead ){
           43  +    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
           44  +    assert( pPk!=0 );
           45  +    assert( pPk->tnum=pTab->tnum );
           46  +    sqlite3VdbeAddOp4(v, opcode, iCur, pPk->tnum, iDb,
           47  +                      (char*)sqlite3IndexKeyinfo(p, pPk), P4_KEYINFO_HANDOFF);
    35     48       VdbeComment((v, "%s", pTab->zName));
    36     49     }
    37     50   }
    38     51   
    39     52   /*
    40     53   ** Return a pointer to the column affinity string associated with index
    41     54   ** pIdx. A column affinity string has one character for each column in 

Changes to src/where.c.

  5951   5951   #endif
  5952   5952       if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
  5953   5953            && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
  5954   5954         int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
  5955   5955         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
  5956   5956         testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
  5957   5957         testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
  5958         -      if( !pWInfo->okOnePass && pTab->nCol<BMS ){
         5958  +      if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
  5959   5959           Bitmask b = pTabItem->colUsed;
  5960   5960           int n = 0;
  5961   5961           for(; b; b=b>>1, n++){}
  5962   5962           sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
  5963   5963                               SQLITE_INT_TO_PTR(n), P4_INT32);
  5964   5964           assert( n<=pTab->nCol );
  5965   5965         }