}   if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ rc = 0; goto select_end;
int sqliteSelect(  Parse *pParse,         /* The parser context */  Select *p,             /* The SELECT statement being coded. */  int eDest,             /* How to dispose of the results */  int iParm,             /* A parameter used by the eDest disposal method */  Select *pParent,       /* Another SELECT for which this is a sub-query */  int parentTab,         /* Index in pParent->pSrc of this query */  int *pParentAgg        /* True if pParent uses aggregate functions */){  int i;  WhereInfo *pWInfo;  Vdbe *v;  int isAgg = 0;         /* True for select lists like "count(*)" */  ExprList *pEList;      /* List of columns to extract. */  SrcList *pTabList;     /* List of tables to select from */  Expr *pWhere;          /* The WHERE clause.  May be NULL */  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */  Expr *pHaving;         /* The HAVING clause.  May be NULL */  int isDistinct;        /* True if the DISTINCT keyword is present */  int distinct;          /* Table to use for the distinct set */  int rc = 1;            /* Value to return from this function */  if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;  if( sqliteAuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;  /* If there is are a sequence of queries, do the earlier ones first.  */  if( p->pPrior ){    return multiSelect(pParse, p, eDest, iParm);  }  /* Make local copies of the parameters for this query.  */  pTabList = p->pSrc;  pWhere = p->pWhere;  pOrderBy = p->pOrderBy;  pGroupBy = p->pGroupBy;  pHaving = p->pHaving;  isDistinct = p->isDistinct;  /* Allocate VDBE cursors for each table in the FROM clause  */  sqliteSrcListAssignCursors(pParse, pTabList);  /*   ** Do not even attempt to generate any code if we have already seen  ** errors before this routine starts.  */  if( pParse->nErr>0 ) goto select_end;  /* Expand any "*" terms in the result set.  (For example the "*" in  ** "SELECT * FROM t1")  The fillInColumnlist() routine also does some  ** other housekeeping - see the header comment for details.  */  if( fillInColumnList(pParse, p) ){    goto select_end;  }  pWhere = p->pWhere;  pEList = p->pEList;  if( pEList==0 ) goto select_end;  /* If writing to memory or generating a set  ** only a single column may be output.  */  if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){    sqliteErrorMsg(pParse, "only a single result allowed for "       "a SELECT that is part of an expression");    goto select_end;  }  /* ORDER BY is ignored for some destinations.  */  switch( eDest ){    case SRT_Union:    case SRT_Except:    case SRT_Discard:      pOrderBy = 0;      break;    default:      break;  }  /* At this point, we should have allocated all the cursors that we  ** need to handle subquerys and temporary tables.    **  ** Resolve the column names and do a semantics check on all the expressions.  */  for(i=0; i<pEList->nExpr; i++){    if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){      goto select_end;    }    if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){      goto select_end;    }  }  if( pWhere ){    if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){      goto select_end;    }    if( sqliteExprCheck(pParse, pWhere, 0, 0) ){      goto select_end;    }  }  if( pHaving ){    if( pGroupBy==0 ){      sqliteErrorMsg(pParse, "a GROUP BY clause is required before HAVING");      goto select_end;    }    if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){      goto select_end;    }    if( sqliteExprCheck(pParse, pHaving, 1, &isAgg) ){      goto select_end;    }  }  if( pOrderBy ){    for(i=0; i<pOrderBy->nExpr; i++){      int iCol;      Expr *pE = pOrderBy->a[i].pExpr;      if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){        sqliteExprDelete(pE);        pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);      }      if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){        goto select_end;      }      if( sqliteExprCheck(pParse, pE, isAgg, 0) ){        goto select_end;      }      if( sqliteExprIsConstant(pE) ){        if( sqliteExprIsInteger(pE, &iCol)==0 ){          sqliteErrorMsg(pParse,             "ORDER BY terms must not be non-integer constants");          goto select_end;        }else if( iCol<=0 || iCol>pEList->nExpr ){          sqliteErrorMsg(pParse,              "ORDER BY column number %d out of range - should be "             "between 1 and %d", iCol, pEList->nExpr);          goto select_end;        }      }    }  }  if( pGroupBy ){    for(i=0; i<pGroupBy->nExpr; i++){      int iCol;      Expr *pE = pGroupBy->a[i].pExpr;      if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){        sqliteExprDelete(pE);        pE = pGroupBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);      }      if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){        goto select_end;      }      if( sqliteExprCheck(pParse, pE, isAgg, 0) ){        goto select_end;      }      if( sqliteExprIsConstant(pE) ){        if( sqliteExprIsInteger(pE, &iCol)==0 ){          sqliteErrorMsg(pParse,            "GROUP BY terms must not be non-integer constants");          goto select_end;        }else if( iCol<=0 || iCol>pEList->nExpr ){          sqliteErrorMsg(pParse,             "GROUP BY column number %d out of range - should be "             "between 1 and %d", iCol, pEList->nExpr);          goto select_end;        }      }    }  }  /* Begin generating code.  */  v = sqliteGetVdbe(pParse);  if( v==0 ) goto select_end;  /* Identify column names if we will be using them in a callback.  This  ** step is skipped if the output is going to some other destination.  */  if( eDest==SRT_Callback ){    generateColumnNames(pParse, pTabList, pEList);  }  /* Check for the special case of a min() or max() function by itself  ** in the result set.  */  if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){    rc = 0;    goto select_end;  }  /* Generate code for all sub-queries in the FROM clause  */  for(i=0; i<pTabList->nSrc; i++){    const char *zSavedAuthContext;    int needRestoreContext;    if( pTabList->a[i].pSelect==0 ) continue;    if( pTabList->a[i].zName!=0 ){      zSavedAuthContext = pParse->zAuthContext;      pParse->zAuthContext = pTabList->a[i].zName;      needRestoreContext = 1;    }else{      needRestoreContext = 0;    }    sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable,                  pTabList->a[i].iCursor, p, i, &isAgg);    if( needRestoreContext ){      pParse->zAuthContext = zSavedAuthContext;    }    pTabList = p->pSrc;    pWhere = p->pWhere;    if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){      pOrderBy = p->pOrderBy;    }    pGroupBy = p->pGroupBy;    pHaving = p->pHaving;    isDistinct = p->isDistinct;  }  /* Check to see if this is a subquery that can be "flattened" into its parent.  ** If flattening is a possiblity, do so and return immediately.    */  if( pParent && pParentAgg &&      flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){    if( isAgg ) *pParentAgg = 1;    return rc;  }  /* Set the limiter.  */  computeLimitRegisters(pParse, p);  /* Identify column types if we will be using a callback.  This  ** step is skipped if the output is going to a destination other  ** than a callback.  **  ** We have to do this separately from the creation of column names  ** above because if the pTabList contains views then they will not  ** have been resolved and we will not know the column types until  ** now.  */  if( eDest==SRT_Callback ){    generateColumnTypes(pParse, pTabList, pEList);  }  /* If the output is destined for a temporary table, open that table.  */  if( eDest==SRT_TempTable ){    sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);  }  /* Do an analysis of aggregate expressions.  */  sqliteAggregateInfoReset(pParse);  if( isAgg || pGroupBy ){    assert( pParse->nAgg==0 );    isAgg = 1;    for(i=0; i<pEList->nExpr; i++){      if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){        goto select_end;      }    }    if( pGroupBy ){      for(i=0; i<pGroupBy->nExpr; i++){        if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){          goto select_end;        }      }    }    if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){      goto select_end;    }    if( pOrderBy ){      for(i=0; i<pOrderBy->nExpr; i++){        if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){          goto select_end;        }      }    }  }  /* Reset the aggregator  */  if( isAgg ){    sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);    for(i=0; i<pParse->nAgg; i++){      FuncDef *pFunc;      if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){        sqliteVdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_POINTER);      }    }    if( pGroupBy==0 ){      sqliteVdbeAddOp(v, OP_String, 0, 0);      sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);    }  }  /* Initialize the memory cell to NULL  */  if( eDest==SRT_Mem ){    sqliteVdbeAddOp(v, OP_String, 0, 0);    sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);  }  /* Open a temporary table to use for the distinct set.  */  if( isDistinct ){    distinct = pParse->nTab++;    sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);  }else{    distinct = -1;  }  /* Begin the database scan  */  pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0,                             pGroupBy ? 0 : &pOrderBy);  if( pWInfo==0 ) goto select_end;  /* Use the standard inner loop if we are not dealing with  ** aggregates  */  if( !isAgg ){    if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,                    iParm, pWInfo->iContinue, pWInfo->iBreak) ){       goto select_end;    }  }  /* If we are dealing with aggregates, then do the special aggregate  ** processing.    */  else{    AggExpr *pAgg;    if( pGroupBy ){      int lbl1;      for(i=0; i<pGroupBy->nExpr; i++){        sqliteExprCode(pParse, pGroupBy->a[i].pExpr);      }      sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);      if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);      lbl1 = sqliteVdbeMakeLabel(v);      sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);      for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){        if( pAgg->isAgg ) continue;        sqliteExprCode(pParse, pAgg->pExpr);        sqliteVdbeAddOp(v, OP_AggSet, 0, i);      }      sqliteVdbeResolveLabel(v, lbl1);    }    for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){      Expr *pE;      int nExpr;      FuncDef *pDef;      if( !pAgg->isAgg ) continue;      assert( pAgg->pFunc!=0 );      assert( pAgg->pFunc->xStep!=0 );      pDef = pAgg->pFunc;      pE = pAgg->pExpr;      assert( pE!=0 );      assert( pE->op==TK_AGG_FUNCTION );      nExpr = sqliteExprCodeExprList(pParse, pE->pList, pDef->includeTypes);      sqliteVdbeAddOp(v, OP_Integer, i, 0);      sqliteVdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER);    }  }  /* End the database scan loop.  */  sqliteWhereEnd(pWInfo);  /* If we are processing aggregates, we need to set up a second loop  ** over all of the aggregate values and process them.  */  if( isAgg ){    int endagg = sqliteVdbeMakeLabel(v);    int startagg;    startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);    pParse->useAgg = 1;    if( pHaving ){      sqliteExprIfFalse(pParse, pHaving, startagg, 1);    }    if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,                    iParm, startagg, endagg) ){      goto select_end;    }    sqliteVdbeAddOp(v, OP_Goto, 0, startagg);    sqliteVdbeResolveLabel(v, endagg);    sqliteVdbeAddOp(v, OP_Noop, 0, 0);    pParse->useAgg = 0;  }  /* If there is an ORDER BY clause, then we need to sort the results  ** and send them to the callback one by one.  */  if( pOrderBy ){    generateSortTail(p, v, pEList->nExpr, eDest, iParm);  }  /* If this was a subquery, we have now converted the subquery into a  ** temporary table.  So delete the subquery structure from the parent  ** to prevent this subquery from being evaluated again and to force the  ** the use of the temporary table.  */  if( pParent ){    assert( pParent->pSrc->nSrc>parentTab );    assert( pParent->pSrc->a[parentTab].pSelect==p );    sqliteSelectDelete(p);    pParent->pSrc->a[parentTab].pSelect = 0;  }  /* The SELECT was successfully coded.   Set the return code to 0  ** to indicate no errors.  */  rc = 0;  /* Control jumps to here if an error is encountered above, or upon  ** successful coding of the SELECT.  */select_end:  sqliteAggregateInfoReset(pParse);  return rc;}