/ Check-in [c0b61f70]
Login

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

Overview
Comment:Improved ".selecttrace" output.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | select-trace
Files: files | file ages | folders
SHA1: c0b61f7092a7fd2c5f51db26ce7a7a5c75c227fe
User & Date: drh 2014-09-21 00:27:26
Context
2014-09-21
17:51
Add the "showauth" extension in ext/misc. check-in: 28d52c1c user: drh tags: select-trace
00:27
Improved ".selecttrace" output. check-in: c0b61f70 user: drh tags: select-trace
2014-09-20
20:38
Candidate fix for [d11a6e908f]. check-in: 89398880 user: dan tags: select-trace
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

  1065   1065     pNew->iLimit = 0;
  1066   1066     pNew->iOffset = 0;
  1067   1067     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1068   1068     pNew->addrOpenEphm[0] = -1;
  1069   1069     pNew->addrOpenEphm[1] = -1;
  1070   1070     pNew->nSelectRow = p->nSelectRow;
  1071   1071     pNew->pWith = withDup(db, p->pWith);
  1072         -#if SELECTTRACE_ENABLED
  1073         -  memcpy(pNew->zSelLabel, p->zSelLabel, sizeof(p->zSelLabel));
  1074         -#endif
         1072  +  sqlite3SelectSetName(pNew, p->zSelName);
  1075   1073     return pNew;
  1076   1074   }
  1077   1075   #else
  1078   1076   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1079   1077     assert( p==0 );
  1080   1078     return 0;
  1081   1079   }

Changes to src/parse.y.

   459    459   multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
   460    460   multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
   461    461   %endif SQLITE_OMIT_COMPOUND_SELECT
   462    462   oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
   463    463                    groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
   464    464     A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
   465    465   #if SELECTTRACE_ENABLED
   466         -  /* Populate the Select.zSelLabel[] string that is used to help with
          466  +  /* Populate the Select.zSelName[] string that is used to help with
   467    467     ** query planner debugging, to differentiate between multiple Select
   468    468     ** objects in a complex query.
   469    469     **
   470    470     ** If the SELECT keyword is immediately followed by a C-style comment
   471    471     ** then extract the first few alphanumeric characters from within that
   472         -  ** comment to be the zSelLabel value.  Otherwise, the label is #N where
          472  +  ** comment to be the zSelName value.  Otherwise, the label is #N where
   473    473     ** is an integer that is incremented with each SELECT statement seen.
   474    474     */
   475    475     if( A!=0 ){
   476    476       const char *z = S.z+6;
   477    477       int i;
   478         -    sqlite3_snprintf(sizeof(A->zSelLabel), A->zSelLabel, "#%d",
          478  +    sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
   479    479                        ++pParse->nSelect);
   480    480       while( z[0]==' ' ) z++;
   481    481       if( z[0]=='/' && z[1]=='*' ){
   482    482         z += 2;
   483    483         while( z[0]==' ' ) z++;
   484    484         for(i=0; sqlite3Isalnum(z[i]); i++){}
   485         -      sqlite3_snprintf(sizeof(A->zSelLabel), A->zSelLabel, "%.*s", i, z);
          485  +      sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
   486    486       }
   487    487     }
   488    488   #endif /* SELECTRACE_ENABLED */
   489    489   }
   490    490   oneselect(A) ::= values(X).    {A = X;}
   491    491   
   492    492   %type values {Select*}

Changes to src/select.c.

    15     15   #include "sqliteInt.h"
    16     16   
    17     17   /*
    18     18   ** Trace output macros
    19     19   */
    20     20   #if SELECTTRACE_ENABLED
    21     21   /***/ int sqlite3SelectTrace = 0;
    22         -# define SELECTTRACE(K,X)  if(sqlite3SelectTrace&(K)) sqlite3DebugPrintf X
           22  +# define SELECTTRACE(K,P,S,X)  \
           23  +  if(sqlite3SelectTrace&(K))   \
           24  +    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
           25  +    sqlite3DebugPrintf X
    23     26   #else
    24         -# define SELECTTRACE(K,X)
           27  +# define SELECTTRACE(K,P,S,X)
    25     28   #endif
    26     29   
    27     30   
    28     31   /*
    29     32   ** An instance of the following object is used to record information about
    30     33   ** how to process the DISTINCT keyword, to simplify passing that information
    31     34   ** into the selectInnerLoop() routine.
................................................................................
   132    135       pNew = 0;
   133    136     }else{
   134    137       assert( pNew->pSrc!=0 || pParse->nErr>0 );
   135    138     }
   136    139     assert( pNew!=&standin );
   137    140     return pNew;
   138    141   }
          142  +
          143  +#if SELECTTRACE_ENABLED
          144  +/*
          145  +** Set the name of a Select object
          146  +*/
          147  +void sqlite3SelectSetName(Select *p, const char *zName){
          148  +  if( p && zName ){
          149  +    sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
          150  +  }
          151  +}
          152  +#endif
          153  +
   139    154   
   140    155   /*
   141    156   ** Delete the given Select structure and all of its substructures.
   142    157   */
   143    158   void sqlite3SelectDelete(sqlite3 *db, Select *p){
   144    159     if( p ){
   145    160       clearSelect(db, p);
................................................................................
  3362   3377         for(ii=0; ii<p->pOrderBy->nExpr; ii++){
  3363   3378           if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
  3364   3379         }
  3365   3380       }
  3366   3381     }
  3367   3382   
  3368   3383     /***** If we reach this point, flattening is permitted. *****/
  3369         -  SELECTTRACE(1, ("flatten %s (term %d) into %s\n",
  3370         -                   pSub->zSelLabel, iFrom, p->zSelLabel));
         3384  +  SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
         3385  +                   pSub->zSelName, pSub, iFrom));
  3371   3386   
  3372   3387     /* Authorize the subquery */
  3373   3388     pParse->zAuthContext = pSubitem->zName;
  3374   3389     TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
  3375   3390     testcase( i==SQLITE_DENY );
  3376   3391     pParse->zAuthContext = zSavedAuthContext;
  3377   3392   
................................................................................
  3416   3431       Select *pPrior = p->pPrior;
  3417   3432       p->pOrderBy = 0;
  3418   3433       p->pSrc = 0;
  3419   3434       p->pPrior = 0;
  3420   3435       p->pLimit = 0;
  3421   3436       p->pOffset = 0;
  3422   3437       pNew = sqlite3SelectDup(db, p, 0);
         3438  +    sqlite3SelectSetName(pNew, pSub->zSelName);
  3423   3439       p->pOffset = pOffset;
  3424   3440       p->pLimit = pLimit;
  3425   3441       p->pOrderBy = pOrderBy;
  3426   3442       p->pSrc = pSrc;
  3427   3443       p->op = TK_ALL;
  3428   3444       if( pNew==0 ){
  3429   3445         p->pPrior = pPrior;
  3430   3446       }else{
  3431   3447         pNew->pPrior = pPrior;
  3432   3448         if( pPrior ) pPrior->pNext = pNew;
  3433   3449         pNew->pNext = p;
  3434   3450         p->pPrior = pNew;
         3451  +      SELECTTRACE(2,pParse,p,
         3452  +         ("compound-subquery flattener creates %s.%p as peer\n",
         3453  +         pNew->zSelName, pNew));
  3435   3454       }
  3436   3455       if( db->mallocFailed ) return 1;
  3437   3456     }
  3438   3457   
  3439   3458     /* Begin flattening the iFrom-th entry of the FROM clause 
  3440   3459     ** in the outer query.
  3441   3460     */
................................................................................
  4089   4108         pTab->nRef++;
  4090   4109   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  4091   4110         if( pTab->pSelect || IsVirtual(pTab) ){
  4092   4111           /* We reach here if the named table is a really a view */
  4093   4112           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  4094   4113           assert( pFrom->pSelect==0 );
  4095   4114           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
         4115  +        sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
  4096   4116           sqlite3WalkSelect(pWalker, pFrom->pSelect);
  4097   4117         }
  4098   4118   #endif
  4099   4119       }
  4100   4120   
  4101   4121       /* Locate the index named by the INDEXED BY clause, if any. */
  4102   4122       if( sqlite3IndexedByLookup(pParse, pFrom) ){
................................................................................
  4623   4643   
  4624   4644     db = pParse->db;
  4625   4645     if( p==0 || db->mallocFailed || pParse->nErr ){
  4626   4646       return 1;
  4627   4647     }
  4628   4648     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  4629   4649     memset(&sAggInfo, 0, sizeof(sAggInfo));
  4630         -  SELECTTRACE(1, ("begin processing %s\n", p->zSelLabel));
         4650  +#if SELECTTRACE_ENABLED
         4651  +  pParse->nSelectIndent++;
         4652  +  SELECTTRACE(1,pParse,p, ("begin processing\n"));
         4653  +#endif
  4631   4654   
  4632   4655     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
  4633   4656     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
  4634   4657     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
  4635   4658     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
  4636   4659     if( IgnorableOrderby(pDest) ){
  4637   4660       assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
................................................................................
  4780   4803   
  4781   4804   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  4782   4805     /* If there is are a sequence of queries, do the earlier ones first.
  4783   4806     */
  4784   4807     if( p->pPrior ){
  4785   4808       rc = multiSelect(pParse, p, pDest);
  4786   4809       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
         4810  +#if SELECTTRACE_ENABLED
         4811  +    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
         4812  +    pParse->nSelectIndent--;
         4813  +#endif
  4787   4814       return rc;
  4788   4815     }
  4789   4816   #endif
  4790   4817   
  4791   4818     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  4792   4819     ** if the select-list is the same as the ORDER BY list, then this query
  4793   4820     ** can be rewritten as a GROUP BY. In other words, this:
................................................................................
  5379   5406     */
  5380   5407     if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
  5381   5408       generateColumnNames(pParse, pTabList, pEList);
  5382   5409     }
  5383   5410   
  5384   5411     sqlite3DbFree(db, sAggInfo.aCol);
  5385   5412     sqlite3DbFree(db, sAggInfo.aFunc);
  5386         -  SELECTTRACE(1, ("end processing %s\n", p->zSelLabel));
         5413  +#if SELECTTRACE_ENABLED
         5414  +  SELECTTRACE(1,pParse,p,("end processing\n"));
         5415  +  pParse->nSelectIndent--;
         5416  +#endif
  5387   5417     return rc;
  5388   5418   }
  5389   5419   
  5390   5420   #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  5391   5421   /*
  5392   5422   ** Generate a human-readable description of a the Select object.
  5393   5423   */

Changes to src/sqliteInt.h.

  2307   2307   */
  2308   2308   struct Select {
  2309   2309     ExprList *pEList;      /* The fields of the result */
  2310   2310     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2311   2311     u16 selFlags;          /* Various SF_* values */
  2312   2312     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  2313   2313   #if SELECTTRACE_ENABLED
  2314         -  char zSelLabel[12];    /* Text in comment following SELECT keyword */
         2314  +  char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
  2315   2315   #endif
  2316   2316     int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  2317   2317     u64 nSelectRow;        /* Estimated number of result rows */
  2318   2318     SrcList *pSrc;         /* The FROM clause */
  2319   2319     Expr *pWhere;          /* The WHERE clause */
  2320   2320     ExprList *pGroupBy;    /* The GROUP BY clause */
  2321   2321     Expr *pHaving;         /* The HAVING clause */
................................................................................
  2569   2569     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  2570   2570     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  2571   2571     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  2572   2572     int regRoot;         /* Register holding root page number for new objects */
  2573   2573     int nMaxArg;         /* Max args passed to user function by sub-program */
  2574   2574   #if SELECTTRACE_ENABLED
  2575   2575     int nSelect;         /* Number of SELECT statements seen */
         2576  +  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
  2576   2577   #endif
  2577   2578   #ifndef SQLITE_OMIT_SHARED_CACHE
  2578   2579     int nTableLock;        /* Number of locks in aTableLock */
  2579   2580     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  2580   2581   #endif
  2581   2582     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
  2582   2583   
................................................................................
  3304   3305   void sqlite3UniqueConstraint(Parse*, int, Index*);
  3305   3306   void sqlite3RowidConstraint(Parse*, int, Table*);
  3306   3307   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  3307   3308   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  3308   3309   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  3309   3310   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  3310   3311   Select *sqlite3SelectDup(sqlite3*,Select*,int);
         3312  +#if SELECTTRACE_ENABLED
         3313  +void sqlite3SelectSetName(Select*,const char*);
         3314  +#else
         3315  +# define sqlite3SelectSetName(A,B)
         3316  +#endif
  3311   3317   void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  3312   3318   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
  3313   3319   void sqlite3RegisterBuiltinFunctions(sqlite3*);
  3314   3320   void sqlite3RegisterDateTimeFunctions(void);
  3315   3321   void sqlite3RegisterGlobalFunctions(void);
  3316   3322   int sqlite3SafetyCheckOk(sqlite3*);
  3317   3323   int sqlite3SafetyCheckSickOrOk(sqlite3*);