/ Check-in [6affb1c8]
Login

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

Overview
Comment:Always use the IsVirtual() macro to determine if a Table object is a virtual table. Slightly smaller and faster code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6affb1c89d87288cad87dde5a533832cdf06b8aa
User & Date: drh 2017-02-16 14:48:08
Context
2017-02-16
15:06
Remove two redundant initializations from the virtual table logic. check-in: 6bd82b95 user: drh tags: trunk
14:48
Always use the IsVirtual() macro to determine if a Table object is a virtual table. Slightly smaller and faster code. check-in: 6affb1c8 user: drh tags: trunk
2017-02-15
22:36
Query planner optimization to detect empty tables in a join early and bail out without doing excess work. check-in: 58797e9b user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/insert.c.

  1975   1975       ** error if pSelect reads from a CTE named "xxx".  */
  1976   1976       return 0;
  1977   1977     }
  1978   1978     if( sqlite3TriggerList(pParse, pDest) ){
  1979   1979       return 0;   /* tab1 must not have triggers */
  1980   1980     }
  1981   1981   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1982         -  if( pDest->tabFlags & TF_Virtual ){
         1982  +  if( IsVirtual(pDest) ){
  1983   1983       return 0;   /* tab1 must not be a virtual table */
  1984   1984     }
  1985   1985   #endif
  1986   1986     if( onError==OE_Default ){
  1987   1987       if( pDest->iPKey>=0 ) onError = pDest->keyConf;
  1988   1988       if( onError==OE_Default ) onError = OE_Abort;
  1989   1989     }
................................................................................
  2037   2037     if( pSrc==pDest ){
  2038   2038       return 0;   /* tab1 and tab2 may not be the same table */
  2039   2039     }
  2040   2040     if( HasRowid(pDest)!=HasRowid(pSrc) ){
  2041   2041       return 0;   /* source and destination must both be WITHOUT ROWID or not */
  2042   2042     }
  2043   2043   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2044         -  if( pSrc->tabFlags & TF_Virtual ){
         2044  +  if( IsVirtual(pSrc) ){
  2045   2045       return 0;   /* tab2 must not be a virtual table */
  2046   2046     }
  2047   2047   #endif
  2048   2048     if( pSrc->pSelect ){
  2049   2049       return 0;   /* tab2 may not be a view */
  2050   2050     }
  2051   2051     if( pDest->nCol!=pSrc->nCol ){

Changes to src/sqliteInt.h.

  1879   1879   ** the TF_OOOHidden attribute would apply in this case.  Such tables require
  1880   1880   ** special handling during INSERT processing.
  1881   1881   */
  1882   1882   #define TF_Readonly        0x01    /* Read-only system table */
  1883   1883   #define TF_Ephemeral       0x02    /* An ephemeral table */
  1884   1884   #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
  1885   1885   #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
  1886         -#define TF_Virtual         0x10    /* Is a virtual table */
         1886  +/* available for reuse:    0x10    */
  1887   1887   #define TF_WithoutRowid    0x20    /* No rowid.  PRIMARY KEY is the key */
  1888   1888   #define TF_NoVisibleRowid  0x40    /* No user-visible "rowid" column */
  1889   1889   #define TF_OOOHidden       0x80    /* Out-of-Order hidden columns */
  1890   1890   
  1891   1891   
  1892   1892   /*
  1893   1893   ** Test to see whether or not a table is a virtual table.  This is
  1894   1894   ** done as a macro so that it will be optimized out when virtual
  1895   1895   ** table support is omitted from the build.
  1896   1896   */
  1897   1897   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1898         -#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
         1898  +#  define IsVirtual(X)      ((X)->nModuleArg)
  1899   1899   #else
  1900   1900   #  define IsVirtual(X)      0
  1901   1901   #endif
  1902   1902   
  1903   1903   /*
  1904   1904   ** Macros to determine if a column is hidden.  IsOrdinaryHiddenColumn()
  1905   1905   ** only works for non-virtual tables (ordinary tables and views) and is

Changes to src/vtab.c.

   335    335     if( pTable==0 ) return;
   336    336     assert( 0==pTable->pIndex );
   337    337   
   338    338     db = pParse->db;
   339    339     iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
   340    340     assert( iDb>=0 );
   341    341   
   342         -  pTable->tabFlags |= TF_Virtual;
   343    342     pTable->nModuleArg = 0;
   344    343     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
   345    344     addModuleArgument(db, pTable, 0);
   346    345     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
   347    346     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
   348    347          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
   349    348     );
................................................................................
   624    623   int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
   625    624     sqlite3 *db = pParse->db;
   626    625     const char *zMod;
   627    626     Module *pMod;
   628    627     int rc;
   629    628   
   630    629     assert( pTab );
   631         -  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
          630  +  if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
   632    631       return SQLITE_OK;
   633    632     }
   634    633   
   635    634     /* Locate the required virtual table module */
   636    635     zMod = pTab->azModuleArg[0];
   637    636     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
   638    637   
................................................................................
   694    693   int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
   695    694     int rc = SQLITE_OK;
   696    695     Table *pTab;
   697    696     Module *pMod;
   698    697     const char *zMod;
   699    698   
   700    699     pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
   701         -  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
          700  +  assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
   702    701   
   703    702     /* Locate the required virtual table module */
   704    703     zMod = pTab->azModuleArg[0];
   705    704     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
   706    705   
   707    706     /* If the module has been registered and includes a Create method, 
   708    707     ** invoke it now. If the module has not been registered, return an 
................................................................................
   748    747     pCtx = db->pVtabCtx;
   749    748     if( !pCtx || pCtx->bDeclared ){
   750    749       sqlite3Error(db, SQLITE_MISUSE);
   751    750       sqlite3_mutex_leave(db->mutex);
   752    751       return SQLITE_MISUSE_BKPT;
   753    752     }
   754    753     pTab = pCtx->pTab;
   755         -  assert( (pTab->tabFlags & TF_Virtual)!=0 );
          754  +  assert( IsVirtual(pTab) );
   756    755   
   757    756     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   758    757     if( pParse==0 ){
   759    758       rc = SQLITE_NOMEM_BKPT;
   760    759     }else{
   761    760       pParse->declareVtab = 1;
   762    761       pParse->db = db;
   763    762       pParse->nQueryLoop = 1;
   764    763     
   765    764       if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
   766    765        && pParse->pNewTable
   767    766        && !db->mallocFailed
   768    767        && !pParse->pNewTable->pSelect
   769         -     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
          768  +     && !IsVirtual(pParse->pNewTable)
   770    769       ){
   771    770         if( !pTab->aCol ){
   772    771           Table *pNew = pParse->pNewTable;
   773    772           Index *pIdx;
   774    773           pTab->aCol = pNew->aCol;
   775    774           pTab->nCol = pNew->nCol;
   776    775           pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
................................................................................
  1051   1050   
  1052   1051   
  1053   1052     /* Check to see the left operand is a column in a virtual table */
  1054   1053     if( NEVER(pExpr==0) ) return pDef;
  1055   1054     if( pExpr->op!=TK_COLUMN ) return pDef;
  1056   1055     pTab = pExpr->pTab;
  1057   1056     if( NEVER(pTab==0) ) return pDef;
  1058         -  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
         1057  +  if( !IsVirtual(pTab) ) return pDef;
  1059   1058     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
  1060   1059     assert( pVtab!=0 );
  1061   1060     assert( pVtab->pModule!=0 );
  1062   1061     pMod = (sqlite3_module *)pVtab->pModule;
  1063   1062     if( pMod->xFindFunction==0 ) return pDef;
  1064   1063    
  1065   1064     /* Call the xFindFunction method on the virtual table implementation
................................................................................
  1146   1145     if( pTab->zName==0 ){
  1147   1146       sqlite3DbFree(db, pTab);
  1148   1147       return 0;
  1149   1148     }
  1150   1149     pMod->pEpoTab = pTab;
  1151   1150     pTab->nTabRef = 1;
  1152   1151     pTab->pSchema = db->aDb[0].pSchema;
  1153         -  pTab->tabFlags |= TF_Virtual;
  1154   1152     pTab->nModuleArg = 0;
  1155   1153     pTab->iPKey = -1;
  1156   1154     addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
  1157   1155     addModuleArgument(db, pTab, 0);
  1158   1156     addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
  1159   1157     rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
  1160   1158     if( rc ){
................................................................................
  1218   1216     va_start(ap, op);
  1219   1217     switch( op ){
  1220   1218       case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
  1221   1219         VtabCtx *p = db->pVtabCtx;
  1222   1220         if( !p ){
  1223   1221           rc = SQLITE_MISUSE_BKPT;
  1224   1222         }else{
  1225         -        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
         1223  +        assert( p->pTab==0 || IsVirtual(p->pTab) );
  1226   1224           p->pVTable->bConstraint = (u8)va_arg(ap, int);
  1227   1225         }
  1228   1226         break;
  1229   1227       }
  1230   1228       default:
  1231   1229         rc = SQLITE_MISUSE_BKPT;
  1232   1230         break;