/ Check-in [f678ed34]
Login

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

Overview
Comment:Changes to select.c to facilitate full coverage testing. (CVS 6658)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f678ed34faab88f4e126e553119773386c7c84fa
User & Date: drh 2009-05-19 19:04:58
References
2010-02-24
14:03 Ticket [d2f02d37] Assertion fault on UPDATE involving two virtual tables. status still Open with 3 other changes artifact: 150a3df0 user: drh
Context
2009-05-20
02:40
Updates to the extension loading logic to support full coverage testing. (CVS 6659) check-in: 929cfbc6 user: drh tags: trunk
2009-05-19
19:04
Changes to select.c to facilitate full coverage testing. (CVS 6658) check-in: f678ed34 user: drh tags: trunk
14:21
Improvements to the SQLITE_INT_TO_PTR macro to reduce the number of warnings. For some platforms it might be necessary to compile with the -DHAVE_STDINT_H flag. Ticket #3860. (CVS 6657) check-in: 1b0ee9d1 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.517 2009/05/18 15:46:08 drh Exp $
           15  +** $Id: select.c,v 1.518 2009/05/19 19:04:58 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Delete all the content of a Select structure but do not deallocate
    22     22   ** the select structure itself.
................................................................................
  2917   2917   */
  2918   2918   static u8 minMaxQuery(Select *p){
  2919   2919     Expr *pExpr;
  2920   2920     ExprList *pEList = p->pEList;
  2921   2921   
  2922   2922     if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
  2923   2923     pExpr = pEList->a[0].pExpr;
  2924         -  if( ExprHasProperty(pExpr, EP_xIsSelect) ) return 0;
         2924  +  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
         2925  +  if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
  2925   2926     pEList = pExpr->x.pList;
  2926         -  if( pExpr->op!=TK_AGG_FUNCTION || pEList==0 || pEList->nExpr!=1 ) return 0;
         2927  +  if( pEList==0 || pEList->nExpr!=1 ) return 0;
  2927   2928     if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
  2928   2929     if( pExpr->token.n!=3 ) return WHERE_ORDERBY_NORMAL;
  2929   2930     if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
  2930   2931       return WHERE_ORDERBY_MIN;
  2931   2932     }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
  2932   2933       return WHERE_ORDERBY_MAX;
  2933   2934     }
................................................................................
  3024   3025     ExprList *pEList;
  3025   3026     struct SrcList_item *pFrom;
  3026   3027     sqlite3 *db = pParse->db;
  3027   3028   
  3028   3029     if( db->mallocFailed  ){
  3029   3030       return WRC_Abort;
  3030   3031     }
  3031         -  if( p->pSrc==0 || (p->selFlags & SF_Expanded)!=0 ){
         3032  +  if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
  3032   3033       return WRC_Prune;
  3033   3034     }
  3034   3035     p->selFlags |= SF_Expanded;
  3035   3036     pTabList = p->pSrc;
  3036   3037     pEList = p->pEList;
  3037   3038   
  3038   3039     /* Make sure cursor numbers have been assigned to all entries in
................................................................................
  3076   3077           sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
  3077   3078         if( pTab==0 ) return WRC_Abort;
  3078   3079         pTab->nRef++;
  3079   3080   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  3080   3081         if( pTab->pSelect || IsVirtual(pTab) ){
  3081   3082           /* We reach here if the named table is a really a view */
  3082   3083           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  3083         -
  3084         -        /* If pFrom->pSelect!=0 it means we are dealing with a
  3085         -        ** view within a view.  The SELECT structure has already been
  3086         -        ** copied by the outer view so we can skip the copy step here
  3087         -        ** in the inner view.
  3088         -        */
  3089         -        if( pFrom->pSelect==0 ){
  3090         -          pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
  3091         -          sqlite3WalkSelect(pWalker, pFrom->pSelect);
  3092         -        }
         3084  +        assert( pFrom->pSelect==0 );
         3085  +        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
         3086  +        sqlite3WalkSelect(pWalker, pFrom->pSelect);
  3093   3087         }
  3094   3088   #endif
  3095   3089       }
  3096   3090   
  3097   3091       /* Locate the index named by the INDEXED BY clause, if any. */
  3098   3092       if( sqlite3IndexedByLookup(pParse, pFrom) ){
  3099   3093         return WRC_Abort;
................................................................................
  3115   3109     **
  3116   3110     ** The first loop just checks to see if there are any "*" operators
  3117   3111     ** that need expanding.
  3118   3112     */
  3119   3113     for(k=0; k<pEList->nExpr; k++){
  3120   3114       Expr *pE = pEList->a[k].pExpr;
  3121   3115       if( pE->op==TK_ALL ) break;
  3122         -    if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
  3123         -         && pE->pLeft && pE->pLeft->op==TK_ID ) break;
         3116  +    assert( pE->op!=TK_DOT || pE->pRight!=0 );
         3117  +    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
         3118  +    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
  3124   3119     }
  3125   3120     if( k<pEList->nExpr ){
  3126   3121       /*
  3127   3122       ** If we get here it means the result set contains one or more "*"
  3128   3123       ** operators that need to be expanded.  Loop through each expression
  3129   3124       ** in the result set and expand them one by one.
  3130   3125       */
................................................................................
  3132   3127       ExprList *pNew = 0;
  3133   3128       int flags = pParse->db->flags;
  3134   3129       int longNames = (flags & SQLITE_FullColNames)!=0
  3135   3130                         && (flags & SQLITE_ShortColNames)==0;
  3136   3131   
  3137   3132       for(k=0; k<pEList->nExpr; k++){
  3138   3133         Expr *pE = a[k].pExpr;
  3139         -      if( pE->op!=TK_ALL &&
  3140         -           (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
         3134  +      assert( pE->op!=TK_DOT || pE->pRight!=0 );
         3135  +      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
  3141   3136           /* This particular expression does not need to be expanded.
  3142   3137           */
  3143   3138           pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
  3144   3139           if( pNew ){
  3145   3140             pNew->a[pNew->nExpr-1].zName = a[k].zName;
  3146   3141           }
  3147   3142           a[k].pExpr = 0;
  3148   3143           a[k].zName = 0;
  3149   3144         }else{
  3150   3145           /* This expression is a "*" or a "TABLE.*" and needs to be
  3151   3146           ** expanded. */
  3152   3147           int tableSeen = 0;      /* Set to 1 when TABLE matches */
  3153   3148           char *zTName;            /* text of name of TABLE */
  3154         -        if( pE->op==TK_DOT && pE->pLeft ){
         3149  +        if( pE->op==TK_DOT ){
         3150  +          assert( pE->pLeft!=0 );
  3155   3151             zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
  3156   3152           }else{
  3157   3153             zTName = 0;
  3158   3154           }
  3159   3155           for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3160   3156             Table *pTab = pFrom->pTab;
  3161   3157             char *zTabName = pFrom->zAlias;
  3162         -          if( zTabName==0 || zTabName[0]==0 ){ 
         3158  +          if( zTabName==0 ){
  3163   3159               zTabName = pTab->zName;
  3164   3160             }
  3165   3161             if( db->mallocFailed ) break;
  3166   3162             if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
  3167   3163               continue;
  3168   3164             }
  3169   3165             tableSeen = 1;
................................................................................
  3294   3290   static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
  3295   3291     Parse *pParse;
  3296   3292     int i;
  3297   3293     SrcList *pTabList;
  3298   3294     struct SrcList_item *pFrom;
  3299   3295   
  3300   3296     assert( p->selFlags & SF_Resolved );
  3301         -  if( (p->selFlags & SF_HasTypeInfo)==0 ){
  3302         -    p->selFlags |= SF_HasTypeInfo;
  3303         -    pParse = pWalker->pParse;
  3304         -    pTabList = p->pSrc;
  3305         -    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3306         -      Table *pTab = pFrom->pTab;
  3307         -      if( pTab && (pTab->tabFlags & TF_Ephemeral)!=0 ){
  3308         -        /* A sub-query in the FROM clause of a SELECT */
  3309         -        Select *pSel = pFrom->pSelect;
  3310         -        assert( pSel );
  3311         -        while( pSel->pPrior ) pSel = pSel->pPrior;
  3312         -        selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
  3313         -      }
         3297  +  assert( (p->selFlags & SF_HasTypeInfo)==0 );
         3298  +  p->selFlags |= SF_HasTypeInfo;
         3299  +  pParse = pWalker->pParse;
         3300  +  pTabList = p->pSrc;
         3301  +  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
         3302  +    Table *pTab = pFrom->pTab;
         3303  +    if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
         3304  +      /* A sub-query in the FROM clause of a SELECT */
         3305  +      Select *pSel = pFrom->pSelect;
         3306  +      assert( pSel );
         3307  +      while( pSel->pPrior ) pSel = pSel->pPrior;
         3308  +      selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
  3314   3309       }
  3315   3310     }
  3316   3311     return WRC_Continue;
  3317   3312   }
  3318   3313   #endif
  3319   3314   
  3320   3315   
................................................................................
  3350   3345   */
  3351   3346   void sqlite3SelectPrep(
  3352   3347     Parse *pParse,         /* The parser context */
  3353   3348     Select *p,             /* The SELECT statement being coded. */
  3354   3349     NameContext *pOuterNC  /* Name context for container */
  3355   3350   ){
  3356   3351     sqlite3 *db;
  3357         -  if( p==0 ) return;
         3352  +  if( NEVER(p==0) ) return;
  3358   3353     db = pParse->db;
  3359   3354     if( p->selFlags & SF_HasTypeInfo ) return;
  3360         -  if( pParse->nErr || db->mallocFailed ) return;
  3361   3355     sqlite3SelectExpand(pParse, p);
  3362   3356     if( pParse->nErr || db->mallocFailed ) return;
  3363   3357     sqlite3ResolveSelectNames(pParse, p, pOuterNC);
  3364   3358     if( pParse->nErr || db->mallocFailed ) return;
  3365   3359     sqlite3SelectAddTypeInfo(pParse, p);
  3366   3360   }
  3367   3361   
................................................................................
  3574   3568     pOrderBy = p->pOrderBy;
  3575   3569     pTabList = p->pSrc;
  3576   3570     pEList = p->pEList;
  3577   3571     if( pParse->nErr || db->mallocFailed ){
  3578   3572       goto select_end;
  3579   3573     }
  3580   3574     isAgg = (p->selFlags & SF_Aggregate)!=0;
  3581         -  if( pEList==0 ) goto select_end;
  3582         -
  3583         -  /* 
  3584         -  ** Do not even attempt to generate any code if we have already seen
  3585         -  ** errors before this routine starts.
  3586         -  */
  3587         -  if( pParse->nErr>0 ) goto select_end;
         3575  +  assert( pEList!=0 );
  3588   3576   
  3589   3577     /* Begin generating code.
  3590   3578     */
  3591   3579     v = sqlite3GetVdbe(pParse);
  3592   3580     if( v==0 ) goto select_end;
  3593   3581   
  3594   3582     /* Generate code for all sub-queries in the FROM clause
................................................................................
  3621   3609         i = -1;
  3622   3610       }else{
  3623   3611         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
  3624   3612         assert( pItem->isPopulated==0 );
  3625   3613         sqlite3Select(pParse, pSub, &dest);
  3626   3614         pItem->isPopulated = 1;
  3627   3615       }
  3628         -    if( pParse->nErr || db->mallocFailed ){
         3616  +    if( /*pParse->nErr ||*/ db->mallocFailed ){
  3629   3617         goto select_end;
  3630   3618       }
  3631   3619       pParse->nHeight -= sqlite3SelectExprHeight(p);
  3632   3620       pTabList = p->pSrc;
  3633   3621       if( !IgnorableOrderby(pDest) ){
  3634   3622         pOrderBy = p->pOrderBy;
  3635   3623       }
................................................................................
  3672   3660       goto select_end;
  3673   3661     }
  3674   3662   #endif
  3675   3663   
  3676   3664     /* If possible, rewrite the query to use GROUP BY instead of DISTINCT.
  3677   3665     ** GROUP BY might use an index, DISTINCT never does.
  3678   3666     */
  3679         -  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct && !p->pGroupBy ){
         3667  +  assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 );
         3668  +  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){
  3680   3669       p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  3681   3670       pGroupBy = p->pGroupBy;
  3682   3671       p->selFlags &= ~SF_Distinct;
  3683   3672       isDistinct = 0;
  3684   3673     }
  3685   3674   
  3686   3675     /* If there is an ORDER BY clause, then this sorting
................................................................................
  4005   3994   
  4006   3995         /* Generate a subroutine that will reset the group-by accumulator
  4007   3996         */
  4008   3997         sqlite3VdbeResolveLabel(v, addrReset);
  4009   3998         resetAccumulator(pParse, &sAggInfo);
  4010   3999         sqlite3VdbeAddOp1(v, OP_Return, regReset);
  4011   4000        
  4012         -    } /* endif pGroupBy */
         4001  +    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
  4013   4002       else {
  4014   4003         ExprList *pDel = 0;
  4015   4004   #ifndef SQLITE_OMIT_BTREECOUNT
  4016   4005         Table *pTab;
  4017   4006         if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
  4018   4007           /* If isSimpleCount() returns a pointer to a Table structure, then
  4019   4008           ** the SQL statement is of the form: