extensions/sqlite/sqlite-source/select.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2001 September 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains C code routines that are called by the parser
ds@1952
    13
** to handle SELECT statements in SQLite.
ds@1952
    14
**
ds@1952
    15
** $Id$
ds@1952
    16
*/
ds@1952
    17
#include "sqliteInt.h"
ds@1952
    18
ds@1952
    19
ds@1952
    20
/*
ds@1952
    21
** Delete all the content of a Select structure but do not deallocate
ds@1952
    22
** the select structure itself.
ds@1952
    23
*/
ds@1952
    24
static void clearSelect(Select *p){
ds@1952
    25
  sqlite3ExprListDelete(p->pEList);
ds@1952
    26
  sqlite3SrcListDelete(p->pSrc);
ds@1952
    27
  sqlite3ExprDelete(p->pWhere);
ds@1952
    28
  sqlite3ExprListDelete(p->pGroupBy);
ds@1952
    29
  sqlite3ExprDelete(p->pHaving);
ds@1952
    30
  sqlite3ExprListDelete(p->pOrderBy);
ds@1952
    31
  sqlite3SelectDelete(p->pPrior);
ds@1952
    32
  sqlite3ExprDelete(p->pLimit);
ds@1952
    33
  sqlite3ExprDelete(p->pOffset);
ds@1952
    34
}
ds@1952
    35
ds@1952
    36
ds@1952
    37
/*
ds@1952
    38
** Allocate a new Select structure and return a pointer to that
ds@1952
    39
** structure.
ds@1952
    40
*/
ds@1952
    41
Select *sqlite3SelectNew(
ds@1952
    42
  Parse *pParse,        /* Parsing context */
ds@1952
    43
  ExprList *pEList,     /* which columns to include in the result */
ds@1952
    44
  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
ds@1952
    45
  Expr *pWhere,         /* the WHERE clause */
ds@1952
    46
  ExprList *pGroupBy,   /* the GROUP BY clause */
ds@1952
    47
  Expr *pHaving,        /* the HAVING clause */
ds@1952
    48
  ExprList *pOrderBy,   /* the ORDER BY clause */
ds@1952
    49
  int isDistinct,       /* true if the DISTINCT keyword is present */
ds@1952
    50
  Expr *pLimit,         /* LIMIT value.  NULL means not used */
ds@1952
    51
  Expr *pOffset         /* OFFSET value.  NULL means no offset */
ds@1952
    52
){
ds@1952
    53
  Select *pNew;
ds@1952
    54
  Select standin;
ds@1952
    55
  sqlite3 *db = pParse->db;
ds@1952
    56
  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
ds@1952
    57
  assert( !pOffset || pLimit );   /* Can't have OFFSET without LIMIT. */
ds@1952
    58
  if( pNew==0 ){
ds@1952
    59
    pNew = &standin;
ds@1952
    60
    memset(pNew, 0, sizeof(*pNew));
ds@1952
    61
  }
ds@1952
    62
  if( pEList==0 ){
ds@1952
    63
    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0);
ds@1952
    64
  }
ds@1952
    65
  pNew->pEList = pEList;
ds@1952
    66
  pNew->pSrc = pSrc;
ds@1952
    67
  pNew->pWhere = pWhere;
ds@1952
    68
  pNew->pGroupBy = pGroupBy;
ds@1952
    69
  pNew->pHaving = pHaving;
ds@1952
    70
  pNew->pOrderBy = pOrderBy;
ds@1952
    71
  pNew->isDistinct = isDistinct;
ds@1952
    72
  pNew->op = TK_SELECT;
ds@1952
    73
  assert( pOffset==0 || pLimit!=0 );
ds@1952
    74
  pNew->pLimit = pLimit;
ds@1952
    75
  pNew->pOffset = pOffset;
ds@1952
    76
  pNew->iLimit = -1;
ds@1952
    77
  pNew->iOffset = -1;
ds@1952
    78
  pNew->addrOpenEphm[0] = -1;
ds@1952
    79
  pNew->addrOpenEphm[1] = -1;
ds@1952
    80
  pNew->addrOpenEphm[2] = -1;
ds@1952
    81
  if( pNew==&standin) {
ds@1952
    82
    clearSelect(pNew);
ds@1952
    83
    pNew = 0;
ds@1952
    84
  }
ds@1952
    85
  return pNew;
ds@1952
    86
}
ds@1952
    87
ds@1952
    88
/*
ds@1952
    89
** Delete the given Select structure and all of its substructures.
ds@1952
    90
*/
ds@1952
    91
void sqlite3SelectDelete(Select *p){
ds@1952
    92
  if( p ){
ds@1952
    93
    clearSelect(p);
ds@1952
    94
    sqlite3_free(p);
ds@1952
    95
  }
ds@1952
    96
}
ds@1952
    97
ds@1952
    98
/*
ds@1952
    99
** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
ds@1952
   100
** type of join.  Return an integer constant that expresses that type
ds@1952
   101
** in terms of the following bit values:
ds@1952
   102
**
ds@1952
   103
**     JT_INNER
ds@1952
   104
**     JT_CROSS
ds@1952
   105
**     JT_OUTER
ds@1952
   106
**     JT_NATURAL
ds@1952
   107
**     JT_LEFT
ds@1952
   108
**     JT_RIGHT
ds@1952
   109
**
ds@1952
   110
** A full outer join is the combination of JT_LEFT and JT_RIGHT.
ds@1952
   111
**
ds@1952
   112
** If an illegal or unsupported join type is seen, then still return
ds@1952
   113
** a join type, but put an error in the pParse structure.
ds@1952
   114
*/
ds@1952
   115
int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
ds@1952
   116
  int jointype = 0;
ds@1952
   117
  Token *apAll[3];
ds@1952
   118
  Token *p;
ds@1952
   119
  static const struct {
ds@1952
   120
    const char zKeyword[8];
ds@1952
   121
    u8 nChar;
ds@1952
   122
    u8 code;
ds@1952
   123
  } keywords[] = {
ds@1952
   124
    { "natural", 7, JT_NATURAL },
ds@1952
   125
    { "left",    4, JT_LEFT|JT_OUTER },
ds@1952
   126
    { "right",   5, JT_RIGHT|JT_OUTER },
ds@1952
   127
    { "full",    4, JT_LEFT|JT_RIGHT|JT_OUTER },
ds@1952
   128
    { "outer",   5, JT_OUTER },
ds@1952
   129
    { "inner",   5, JT_INNER },
ds@1952
   130
    { "cross",   5, JT_INNER|JT_CROSS },
ds@1952
   131
  };
ds@1952
   132
  int i, j;
ds@1952
   133
  apAll[0] = pA;
ds@1952
   134
  apAll[1] = pB;
ds@1952
   135
  apAll[2] = pC;
ds@1952
   136
  for(i=0; i<3 && apAll[i]; i++){
ds@1952
   137
    p = apAll[i];
ds@1952
   138
    for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
ds@1952
   139
      if( p->n==keywords[j].nChar 
ds@1952
   140
          && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
ds@1952
   141
        jointype |= keywords[j].code;
ds@1952
   142
        break;
ds@1952
   143
      }
ds@1952
   144
    }
ds@1952
   145
    if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
ds@1952
   146
      jointype |= JT_ERROR;
ds@1952
   147
      break;
ds@1952
   148
    }
ds@1952
   149
  }
ds@1952
   150
  if(
ds@1952
   151
     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
ds@1952
   152
     (jointype & JT_ERROR)!=0
ds@1952
   153
  ){
ds@1952
   154
    const char *zSp1 = " ";
ds@1952
   155
    const char *zSp2 = " ";
ds@1952
   156
    if( pB==0 ){ zSp1++; }
ds@1952
   157
    if( pC==0 ){ zSp2++; }
ds@1952
   158
    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
ds@1952
   159
       "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
ds@1952
   160
    jointype = JT_INNER;
ds@1952
   161
  }else if( jointype & JT_RIGHT ){
ds@1952
   162
    sqlite3ErrorMsg(pParse, 
ds@1952
   163
      "RIGHT and FULL OUTER JOINs are not currently supported");
ds@1952
   164
    jointype = JT_INNER;
ds@1952
   165
  }
ds@1952
   166
  return jointype;
ds@1952
   167
}
ds@1952
   168
ds@1952
   169
/*
ds@1952
   170
** Return the index of a column in a table.  Return -1 if the column
ds@1952
   171
** is not contained in the table.
ds@1952
   172
*/
ds@1952
   173
static int columnIndex(Table *pTab, const char *zCol){
ds@1952
   174
  int i;
ds@1952
   175
  for(i=0; i<pTab->nCol; i++){
ds@1952
   176
    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
ds@1952
   177
  }
ds@1952
   178
  return -1;
ds@1952
   179
}
ds@1952
   180
ds@1952
   181
/*
ds@1952
   182
** Set the value of a token to a '\000'-terminated string.
ds@1952
   183
*/
ds@1952
   184
static void setToken(Token *p, const char *z){
ds@1952
   185
  p->z = (u8*)z;
ds@1952
   186
  p->n = z ? strlen(z) : 0;
ds@1952
   187
  p->dyn = 0;
ds@1952
   188
}
ds@1952
   189
ds@1952
   190
/*
ds@1952
   191
** Set the token to the double-quoted and escaped version of the string pointed
ds@1952
   192
** to by z. For example;
ds@1952
   193
**
ds@1952
   194
**    {a"bc}  ->  {"a""bc"}
ds@1952
   195
*/
ds@1952
   196
static void setQuotedToken(Parse *pParse, Token *p, const char *z){
ds@1952
   197
  p->z = (u8 *)sqlite3MPrintf(0, "\"%w\"", z);
ds@1952
   198
  p->dyn = 1;
ds@1952
   199
  if( p->z ){
ds@1952
   200
    p->n = strlen((char *)p->z);
ds@1952
   201
  }else{
ds@1952
   202
    pParse->db->mallocFailed = 1;
ds@1952
   203
  }
ds@1952
   204
}
ds@1952
   205
ds@1952
   206
/*
ds@1952
   207
** Create an expression node for an identifier with the name of zName
ds@1952
   208
*/
ds@1952
   209
Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
ds@1952
   210
  Token dummy;
ds@1952
   211
  setToken(&dummy, zName);
ds@1952
   212
  return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy);
ds@1952
   213
}
ds@1952
   214
ds@1952
   215
ds@1952
   216
/*
ds@1952
   217
** Add a term to the WHERE expression in *ppExpr that requires the
ds@1952
   218
** zCol column to be equal in the two tables pTab1 and pTab2.
ds@1952
   219
*/
ds@1952
   220
static void addWhereTerm(
ds@1952
   221
  Parse *pParse,           /* Parsing context */
ds@1952
   222
  const char *zCol,        /* Name of the column */
ds@1952
   223
  const Table *pTab1,      /* First table */
ds@1952
   224
  const char *zAlias1,     /* Alias for first table.  May be NULL */
ds@1952
   225
  const Table *pTab2,      /* Second table */
ds@1952
   226
  const char *zAlias2,     /* Alias for second table.  May be NULL */
ds@1952
   227
  int iRightJoinTable,     /* VDBE cursor for the right table */
ds@1952
   228
  Expr **ppExpr            /* Add the equality term to this expression */
ds@1952
   229
){
ds@1952
   230
  Expr *pE1a, *pE1b, *pE1c;
ds@1952
   231
  Expr *pE2a, *pE2b, *pE2c;
ds@1952
   232
  Expr *pE;
ds@1952
   233
ds@1952
   234
  pE1a = sqlite3CreateIdExpr(pParse, zCol);
ds@1952
   235
  pE2a = sqlite3CreateIdExpr(pParse, zCol);
ds@1952
   236
  if( zAlias1==0 ){
ds@1952
   237
    zAlias1 = pTab1->zName;
ds@1952
   238
  }
ds@1952
   239
  pE1b = sqlite3CreateIdExpr(pParse, zAlias1);
ds@1952
   240
  if( zAlias2==0 ){
ds@1952
   241
    zAlias2 = pTab2->zName;
ds@1952
   242
  }
ds@1952
   243
  pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
ds@1952
   244
  pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
ds@1952
   245
  pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
ds@1952
   246
  pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
ds@1952
   247
  if( pE ){
ds@1952
   248
    ExprSetProperty(pE, EP_FromJoin);
ds@1952
   249
    pE->iRightJoinTable = iRightJoinTable;
ds@1952
   250
  }
ds@1952
   251
  pE = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
ds@1952
   252
  if( pE ){
ds@1952
   253
    *ppExpr = pE;
ds@1952
   254
  }
ds@1952
   255
}
ds@1952
   256
ds@1952
   257
/*
ds@1952
   258
** Set the EP_FromJoin property on all terms of the given expression.
ds@1952
   259
** And set the Expr.iRightJoinTable to iTable for every term in the
ds@1952
   260
** expression.
ds@1952
   261
**
ds@1952
   262
** The EP_FromJoin property is used on terms of an expression to tell
ds@1952
   263
** the LEFT OUTER JOIN processing logic that this term is part of the
ds@1952
   264
** join restriction specified in the ON or USING clause and not a part
ds@1952
   265
** of the more general WHERE clause.  These terms are moved over to the
ds@1952
   266
** WHERE clause during join processing but we need to remember that they
ds@1952
   267
** originated in the ON or USING clause.
ds@1952
   268
**
ds@1952
   269
** The Expr.iRightJoinTable tells the WHERE clause processing that the
ds@1952
   270
** expression depends on table iRightJoinTable even if that table is not
ds@1952
   271
** explicitly mentioned in the expression.  That information is needed
ds@1952
   272
** for cases like this:
ds@1952
   273
**
ds@1952
   274
**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
ds@1952
   275
**
ds@1952
   276
** The where clause needs to defer the handling of the t1.x=5
ds@1952
   277
** term until after the t2 loop of the join.  In that way, a
ds@1952
   278
** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
ds@1952
   279
** defer the handling of t1.x=5, it will be processed immediately
ds@1952
   280
** after the t1 loop and rows with t1.x!=5 will never appear in
ds@1952
   281
** the output, which is incorrect.
ds@1952
   282
*/
ds@1952
   283
static void setJoinExpr(Expr *p, int iTable){
ds@1952
   284
  while( p ){
ds@1952
   285
    ExprSetProperty(p, EP_FromJoin);
ds@1952
   286
    p->iRightJoinTable = iTable;
ds@1952
   287
    setJoinExpr(p->pLeft, iTable);
ds@1952
   288
    p = p->pRight;
ds@1952
   289
  } 
ds@1952
   290
}
ds@1952
   291
ds@1952
   292
/*
ds@1952
   293
** This routine processes the join information for a SELECT statement.
ds@1952
   294
** ON and USING clauses are converted into extra terms of the WHERE clause.
ds@1952
   295
** NATURAL joins also create extra WHERE clause terms.
ds@1952
   296
**
ds@1952
   297
** The terms of a FROM clause are contained in the Select.pSrc structure.
ds@1952
   298
** The left most table is the first entry in Select.pSrc.  The right-most
ds@1952
   299
** table is the last entry.  The join operator is held in the entry to
ds@1952
   300
** the left.  Thus entry 0 contains the join operator for the join between
ds@1952
   301
** entries 0 and 1.  Any ON or USING clauses associated with the join are
ds@1952
   302
** also attached to the left entry.
ds@1952
   303
**
ds@1952
   304
** This routine returns the number of errors encountered.
ds@1952
   305
*/
ds@1952
   306
static int sqliteProcessJoin(Parse *pParse, Select *p){
ds@1952
   307
  SrcList *pSrc;                  /* All tables in the FROM clause */
ds@1952
   308
  int i, j;                       /* Loop counters */
ds@1952
   309
  struct SrcList_item *pLeft;     /* Left table being joined */
ds@1952
   310
  struct SrcList_item *pRight;    /* Right table being joined */
ds@1952
   311
ds@1952
   312
  pSrc = p->pSrc;
ds@1952
   313
  pLeft = &pSrc->a[0];
ds@1952
   314
  pRight = &pLeft[1];
ds@1952
   315
  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
ds@1952
   316
    Table *pLeftTab = pLeft->pTab;
ds@1952
   317
    Table *pRightTab = pRight->pTab;
ds@1952
   318
ds@1952
   319
    if( pLeftTab==0 || pRightTab==0 ) continue;
ds@1952
   320
ds@1952
   321
    /* When the NATURAL keyword is present, add WHERE clause terms for
ds@1952
   322
    ** every column that the two tables have in common.
ds@1952
   323
    */
ds@1952
   324
    if( pRight->jointype & JT_NATURAL ){
ds@1952
   325
      if( pRight->pOn || pRight->pUsing ){
ds@1952
   326
        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
ds@1952
   327
           "an ON or USING clause", 0);
ds@1952
   328
        return 1;
ds@1952
   329
      }
ds@1952
   330
      for(j=0; j<pLeftTab->nCol; j++){
ds@1952
   331
        char *zName = pLeftTab->aCol[j].zName;
ds@1952
   332
        if( columnIndex(pRightTab, zName)>=0 ){
ds@1952
   333
          addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias, 
ds@1952
   334
                              pRightTab, pRight->zAlias,
ds@1952
   335
                              pRight->iCursor, &p->pWhere);
ds@1952
   336
          
ds@1952
   337
        }
ds@1952
   338
      }
ds@1952
   339
    }
ds@1952
   340
ds@1952
   341
    /* Disallow both ON and USING clauses in the same join
ds@1952
   342
    */
ds@1952
   343
    if( pRight->pOn && pRight->pUsing ){
ds@1952
   344
      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
ds@1952
   345
        "clauses in the same join");
ds@1952
   346
      return 1;
ds@1952
   347
    }
ds@1952
   348
ds@1952
   349
    /* Add the ON clause to the end of the WHERE clause, connected by
ds@1952
   350
    ** an AND operator.
ds@1952
   351
    */
ds@1952
   352
    if( pRight->pOn ){
ds@1952
   353
      setJoinExpr(pRight->pOn, pRight->iCursor);
ds@1952
   354
      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
ds@1952
   355
      pRight->pOn = 0;
ds@1952
   356
    }
ds@1952
   357
ds@1952
   358
    /* Create extra terms on the WHERE clause for each column named
ds@1952
   359
    ** in the USING clause.  Example: If the two tables to be joined are 
ds@1952
   360
    ** A and B and the USING clause names X, Y, and Z, then add this
ds@1952
   361
    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
ds@1952
   362
    ** Report an error if any column mentioned in the USING clause is
ds@1952
   363
    ** not contained in both tables to be joined.
ds@1952
   364
    */
ds@1952
   365
    if( pRight->pUsing ){
ds@1952
   366
      IdList *pList = pRight->pUsing;
ds@1952
   367
      for(j=0; j<pList->nId; j++){
ds@1952
   368
        char *zName = pList->a[j].zName;
ds@1952
   369
        if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){
ds@1952
   370
          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
ds@1952
   371
            "not present in both tables", zName);
ds@1952
   372
          return 1;
ds@1952
   373
        }
ds@1952
   374
        addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias, 
ds@1952
   375
                            pRightTab, pRight->zAlias,
ds@1952
   376
                            pRight->iCursor, &p->pWhere);
ds@1952
   377
      }
ds@1952
   378
    }
ds@1952
   379
  }
ds@1952
   380
  return 0;
ds@1952
   381
}
ds@1952
   382
ds@1952
   383
/*
ds@1952
   384
** Insert code into "v" that will push the record on the top of the
ds@1952
   385
** stack into the sorter.
ds@1952
   386
*/
ds@1952
   387
static void pushOntoSorter(
ds@1952
   388
  Parse *pParse,         /* Parser context */
ds@1952
   389
  ExprList *pOrderBy,    /* The ORDER BY clause */
ds@1952
   390
  Select *pSelect        /* The whole SELECT statement */
ds@1952
   391
){
ds@1952
   392
  Vdbe *v = pParse->pVdbe;
ds@1952
   393
  sqlite3ExprCodeExprList(pParse, pOrderBy);
ds@1952
   394
  sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iECursor, 0);
ds@1952
   395
  sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0);
ds@1952
   396
  sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0);
ds@1952
   397
  sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iECursor, 0);
ds@1952
   398
  if( pSelect->iLimit>=0 ){
ds@1952
   399
    int addr1, addr2;
ds@1952
   400
    addr1 = sqlite3VdbeAddOp(v, OP_IfMemZero, pSelect->iLimit+1, 0);
ds@1952
   401
    sqlite3VdbeAddOp(v, OP_MemIncr, -1, pSelect->iLimit+1);
ds@1952
   402
    addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
ds@1952
   403
    sqlite3VdbeJumpHere(v, addr1);
ds@1952
   404
    sqlite3VdbeAddOp(v, OP_Last, pOrderBy->iECursor, 0);
ds@1952
   405
    sqlite3VdbeAddOp(v, OP_Delete, pOrderBy->iECursor, 0);
ds@1952
   406
    sqlite3VdbeJumpHere(v, addr2);
ds@1952
   407
    pSelect->iLimit = -1;
ds@1952
   408
  }
ds@1952
   409
}
ds@1952
   410
ds@1952
   411
/*
ds@1952
   412
** Add code to implement the OFFSET
ds@1952
   413
*/
ds@1952
   414
static void codeOffset(
ds@1952
   415
  Vdbe *v,          /* Generate code into this VM */
ds@1952
   416
  Select *p,        /* The SELECT statement being coded */
ds@1952
   417
  int iContinue,    /* Jump here to skip the current record */
ds@1952
   418
  int nPop          /* Number of times to pop stack when jumping */
ds@1952
   419
){
ds@1952
   420
  if( p->iOffset>=0 && iContinue!=0 ){
ds@1952
   421
    int addr;
ds@1952
   422
    sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iOffset);
ds@1952
   423
    addr = sqlite3VdbeAddOp(v, OP_IfMemNeg, p->iOffset, 0);
ds@1952
   424
    if( nPop>0 ){
ds@1952
   425
      sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
ds@1952
   426
    }
ds@1952
   427
    sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
ds@1952
   428
    VdbeComment((v, "# skip OFFSET records"));
ds@1952
   429
    sqlite3VdbeJumpHere(v, addr);
ds@1952
   430
  }
ds@1952
   431
}
ds@1952
   432
ds@1952
   433
/*
ds@1952
   434
** Add code that will check to make sure the top N elements of the
ds@1952
   435
** stack are distinct.  iTab is a sorting index that holds previously
ds@1952
   436
** seen combinations of the N values.  A new entry is made in iTab
ds@1952
   437
** if the current N values are new.
ds@1952
   438
**
ds@1952
   439
** A jump to addrRepeat is made and the N+1 values are popped from the
ds@1952
   440
** stack if the top N elements are not distinct.
ds@1952
   441
*/
ds@1952
   442
static void codeDistinct(
ds@1952
   443
  Vdbe *v,           /* Generate code into this VM */
ds@1952
   444
  int iTab,          /* A sorting index used to test for distinctness */
ds@1952
   445
  int addrRepeat,    /* Jump to here if not distinct */
ds@1952
   446
  int N              /* The top N elements of the stack must be distinct */
ds@1952
   447
){
ds@1952
   448
  sqlite3VdbeAddOp(v, OP_MakeRecord, -N, 0);
ds@1952
   449
  sqlite3VdbeAddOp(v, OP_Distinct, iTab, sqlite3VdbeCurrentAddr(v)+3);
ds@1952
   450
  sqlite3VdbeAddOp(v, OP_Pop, N+1, 0);
ds@1952
   451
  sqlite3VdbeAddOp(v, OP_Goto, 0, addrRepeat);
ds@1952
   452
  VdbeComment((v, "# skip indistinct records"));
ds@1952
   453
  sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0);
ds@1952
   454
}
ds@1952
   455
ds@1952
   456
/*
ds@1952
   457
** Generate an error message when a SELECT is used within a subexpression
ds@1952
   458
** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
ds@1952
   459
** column.  We do this in a subroutine because the error occurs in multiple
ds@1952
   460
** places.
ds@1952
   461
*/
ds@1952
   462
static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){
ds@1952
   463
  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
ds@1952
   464
    sqlite3ErrorMsg(pParse, "only a single result allowed for "
ds@1952
   465
       "a SELECT that is part of an expression");
ds@1952
   466
    return 1;
ds@1952
   467
  }else{
ds@1952
   468
    return 0;
ds@1952
   469
  }
ds@1952
   470
}
ds@1952
   471
ds@1952
   472
/*
ds@1952
   473
** This routine generates the code for the inside of the inner loop
ds@1952
   474
** of a SELECT.
ds@1952
   475
**
ds@1952
   476
** If srcTab and nColumn are both zero, then the pEList expressions
ds@1952
   477
** are evaluated in order to get the data for this row.  If nColumn>0
ds@1952
   478
** then data is pulled from srcTab and pEList is used only to get the
ds@1952
   479
** datatypes for each column.
ds@1952
   480
*/
ds@1952
   481
static int selectInnerLoop(
ds@1952
   482
  Parse *pParse,          /* The parser context */
ds@1952
   483
  Select *p,              /* The complete select statement being coded */
ds@1952
   484
  ExprList *pEList,       /* List of values being extracted */
ds@1952
   485
  int srcTab,             /* Pull data from this table */
ds@1952
   486
  int nColumn,            /* Number of columns in the source table */
ds@1952
   487
  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
ds@1952
   488
  int distinct,           /* If >=0, make sure results are distinct */
ds@1952
   489
  int eDest,              /* How to dispose of the results */
ds@1952
   490
  int iParm,              /* An argument to the disposal method */
ds@1952
   491
  int iContinue,          /* Jump here to continue with next row */
ds@1952
   492
  int iBreak,             /* Jump here to break out of the inner loop */
ds@1952
   493
  char *aff               /* affinity string if eDest is SRT_Union */
ds@1952
   494
){
ds@1952
   495
  Vdbe *v = pParse->pVdbe;
ds@1952
   496
  int i;
ds@1952
   497
  int hasDistinct;        /* True if the DISTINCT keyword is present */
ds@1952
   498
ds@1952
   499
  if( v==0 ) return 0;
ds@1952
   500
  assert( pEList!=0 );
ds@1952
   501
ds@1952
   502
  /* If there was a LIMIT clause on the SELECT statement, then do the check
ds@1952
   503
  ** to see if this row should be output.
ds@1952
   504
  */
ds@1952
   505
  hasDistinct = distinct>=0 && pEList->nExpr>0;
ds@1952
   506
  if( pOrderBy==0 && !hasDistinct ){
ds@1952
   507
    codeOffset(v, p, iContinue, 0);
ds@1952
   508
  }
ds@1952
   509
ds@1952
   510
  /* Pull the requested columns.
ds@1952
   511
  */
ds@1952
   512
  if( nColumn>0 ){
ds@1952
   513
    for(i=0; i<nColumn; i++){
ds@1952
   514
      sqlite3VdbeAddOp(v, OP_Column, srcTab, i);
ds@1952
   515
    }
ds@1952
   516
  }else{
ds@1952
   517
    nColumn = pEList->nExpr;
ds@1952
   518
    sqlite3ExprCodeExprList(pParse, pEList);
ds@1952
   519
  }
ds@1952
   520
ds@1952
   521
  /* If the DISTINCT keyword was present on the SELECT statement
ds@1952
   522
  ** and this row has been seen before, then do not make this row
ds@1952
   523
  ** part of the result.
ds@1952
   524
  */
ds@1952
   525
  if( hasDistinct ){
ds@1952
   526
    assert( pEList!=0 );
ds@1952
   527
    assert( pEList->nExpr==nColumn );
ds@1952
   528
    codeDistinct(v, distinct, iContinue, nColumn);
ds@1952
   529
    if( pOrderBy==0 ){
ds@1952
   530
      codeOffset(v, p, iContinue, nColumn);
ds@1952
   531
    }
ds@1952
   532
  }
ds@1952
   533
ds@1952
   534
  if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
ds@1952
   535
    return 0;
ds@1952
   536
  }
ds@1952
   537
ds@1952
   538
  switch( eDest ){
ds@1952
   539
    /* In this mode, write each query result to the key of the temporary
ds@1952
   540
    ** table iParm.
ds@1952
   541
    */
ds@1952
   542
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
   543
    case SRT_Union: {
ds@1952
   544
      sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
ds@1952
   545
      if( aff ){
ds@1952
   546
        sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
ds@1952
   547
      }
ds@1952
   548
      sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0);
ds@1952
   549
      break;
ds@1952
   550
    }
ds@1952
   551
ds@1952
   552
    /* Construct a record from the query result, but instead of
ds@1952
   553
    ** saving that record, use it as a key to delete elements from
ds@1952
   554
    ** the temporary table iParm.
ds@1952
   555
    */
ds@1952
   556
    case SRT_Except: {
ds@1952
   557
      int addr;
ds@1952
   558
      addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
ds@1952
   559
      sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
ds@1952
   560
      sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3);
ds@1952
   561
      sqlite3VdbeAddOp(v, OP_Delete, iParm, 0);
ds@1952
   562
      break;
ds@1952
   563
    }
ds@1952
   564
#endif
ds@1952
   565
ds@1952
   566
    /* Store the result as data using a unique key.
ds@1952
   567
    */
ds@1952
   568
    case SRT_Table:
ds@1952
   569
    case SRT_EphemTab: {
ds@1952
   570
      sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
ds@1952
   571
      if( pOrderBy ){
ds@1952
   572
        pushOntoSorter(pParse, pOrderBy, p);
ds@1952
   573
      }else{
ds@1952
   574
        sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
ds@1952
   575
        sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
ds@1952
   576
        sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
ds@1952
   577
      }
ds@1952
   578
      break;
ds@1952
   579
    }
ds@1952
   580
ds@1952
   581
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
   582
    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
ds@1952
   583
    ** then there should be a single item on the stack.  Write this
ds@1952
   584
    ** item into the set table with bogus data.
ds@1952
   585
    */
ds@1952
   586
    case SRT_Set: {
ds@1952
   587
      int addr1 = sqlite3VdbeCurrentAddr(v);
ds@1952
   588
      int addr2;
ds@1952
   589
ds@1952
   590
      assert( nColumn==1 );
ds@1952
   591
      sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
ds@1952
   592
      sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
ds@1952
   593
      addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
ds@1952
   594
      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr,(iParm>>16)&0xff);
ds@1952
   595
      if( pOrderBy ){
ds@1952
   596
        /* At first glance you would think we could optimize out the
ds@1952
   597
        ** ORDER BY in this case since the order of entries in the set
ds@1952
   598
        ** does not matter.  But there might be a LIMIT clause, in which
ds@1952
   599
        ** case the order does matter */
ds@1952
   600
        pushOntoSorter(pParse, pOrderBy, p);
ds@1952
   601
      }else{
ds@1952
   602
        sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
ds@1952
   603
        sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
ds@1952
   604
      }
ds@1952
   605
      sqlite3VdbeJumpHere(v, addr2);
ds@1952
   606
      break;
ds@1952
   607
    }
ds@1952
   608
ds@1952
   609
    /* If any row exist in the result set, record that fact and abort.
ds@1952
   610
    */
ds@1952
   611
    case SRT_Exists: {
ds@1952
   612
      sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm);
ds@1952
   613
      sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
ds@1952
   614
      /* The LIMIT clause will terminate the loop for us */
ds@1952
   615
      break;
ds@1952
   616
    }
ds@1952
   617
ds@1952
   618
    /* If this is a scalar select that is part of an expression, then
ds@1952
   619
    ** store the results in the appropriate memory cell and break out
ds@1952
   620
    ** of the scan loop.
ds@1952
   621
    */
ds@1952
   622
    case SRT_Mem: {
ds@1952
   623
      assert( nColumn==1 );
ds@1952
   624
      if( pOrderBy ){
ds@1952
   625
        pushOntoSorter(pParse, pOrderBy, p);
ds@1952
   626
      }else{
ds@1952
   627
        sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
ds@1952
   628
        /* The LIMIT clause will jump out of the loop for us */
ds@1952
   629
      }
ds@1952
   630
      break;
ds@1952
   631
    }
ds@1952
   632
#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
ds@1952
   633
ds@1952
   634
    /* Send the data to the callback function or to a subroutine.  In the
ds@1952
   635
    ** case of a subroutine, the subroutine itself is responsible for
ds@1952
   636
    ** popping the data from the stack.
ds@1952
   637
    */
ds@1952
   638
    case SRT_Subroutine:
ds@1952
   639
    case SRT_Callback: {
ds@1952
   640
      if( pOrderBy ){
ds@1952
   641
        sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
ds@1952
   642
        pushOntoSorter(pParse, pOrderBy, p);
ds@1952
   643
      }else if( eDest==SRT_Subroutine ){
ds@1952
   644
        sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
ds@1952
   645
      }else{
ds@1952
   646
        sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
ds@1952
   647
      }
ds@1952
   648
      break;
ds@1952
   649
    }
ds@1952
   650
ds@1952
   651
#if !defined(SQLITE_OMIT_TRIGGER)
ds@1952
   652
    /* Discard the results.  This is used for SELECT statements inside
ds@1952
   653
    ** the body of a TRIGGER.  The purpose of such selects is to call
ds@1952
   654
    ** user-defined functions that have side effects.  We do not care
ds@1952
   655
    ** about the actual results of the select.
ds@1952
   656
    */
ds@1952
   657
    default: {
ds@1952
   658
      assert( eDest==SRT_Discard );
ds@1952
   659
      sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
ds@1952
   660
      break;
ds@1952
   661
    }
ds@1952
   662
#endif
ds@1952
   663
  }
ds@1952
   664
ds@1952
   665
  /* Jump to the end of the loop if the LIMIT is reached.
ds@1952
   666
  */
ds@1952
   667
  if( p->iLimit>=0 && pOrderBy==0 ){
ds@1952
   668
    sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
ds@1952
   669
    sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, iBreak);
ds@1952
   670
  }
ds@1952
   671
  return 0;
ds@1952
   672
}
ds@1952
   673
ds@1952
   674
/*
ds@1952
   675
** Given an expression list, generate a KeyInfo structure that records
ds@1952
   676
** the collating sequence for each expression in that expression list.
ds@1952
   677
**
ds@1952
   678
** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
ds@1952
   679
** KeyInfo structure is appropriate for initializing a virtual index to
ds@1952
   680
** implement that clause.  If the ExprList is the result set of a SELECT
ds@1952
   681
** then the KeyInfo structure is appropriate for initializing a virtual
ds@1952
   682
** index to implement a DISTINCT test.
ds@1952
   683
**
ds@1952
   684
** Space to hold the KeyInfo structure is obtain from malloc.  The calling
ds@1952
   685
** function is responsible for seeing that this structure is eventually
ds@1952
   686
** freed.  Add the KeyInfo structure to the P3 field of an opcode using
ds@1952
   687
** P3_KEYINFO_HANDOFF is the usual way of dealing with this.
ds@1952
   688
*/
ds@1952
   689
static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
ds@1952
   690
  sqlite3 *db = pParse->db;
ds@1952
   691
  int nExpr;
ds@1952
   692
  KeyInfo *pInfo;
ds@1952
   693
  struct ExprList_item *pItem;
ds@1952
   694
  int i;
ds@1952
   695
ds@1952
   696
  nExpr = pList->nExpr;
ds@1952
   697
  pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
ds@1952
   698
  if( pInfo ){
ds@1952
   699
    pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
ds@1952
   700
    pInfo->nField = nExpr;
ds@1952
   701
    pInfo->enc = ENC(db);
ds@1952
   702
    for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
ds@1952
   703
      CollSeq *pColl;
ds@1952
   704
      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
ds@1952
   705
      if( !pColl ){
ds@1952
   706
        pColl = db->pDfltColl;
ds@1952
   707
      }
ds@1952
   708
      pInfo->aColl[i] = pColl;
ds@1952
   709
      pInfo->aSortOrder[i] = pItem->sortOrder;
ds@1952
   710
    }
ds@1952
   711
  }
ds@1952
   712
  return pInfo;
ds@1952
   713
}
ds@1952
   714
ds@1952
   715
ds@1952
   716
/*
ds@1952
   717
** If the inner loop was generated using a non-null pOrderBy argument,
ds@1952
   718
** then the results were placed in a sorter.  After the loop is terminated
ds@1952
   719
** we need to run the sorter and output the results.  The following
ds@1952
   720
** routine generates the code needed to do that.
ds@1952
   721
*/
ds@1952
   722
static void generateSortTail(
ds@1952
   723
  Parse *pParse,   /* Parsing context */
ds@1952
   724
  Select *p,       /* The SELECT statement */
ds@1952
   725
  Vdbe *v,         /* Generate code into this VDBE */
ds@1952
   726
  int nColumn,     /* Number of columns of data */
ds@1952
   727
  int eDest,       /* Write the sorted results here */
ds@1952
   728
  int iParm        /* Optional parameter associated with eDest */
ds@1952
   729
){
ds@1952
   730
  int brk = sqlite3VdbeMakeLabel(v);
ds@1952
   731
  int cont = sqlite3VdbeMakeLabel(v);
ds@1952
   732
  int addr;
ds@1952
   733
  int iTab;
ds@1952
   734
  int pseudoTab = 0;
ds@1952
   735
  ExprList *pOrderBy = p->pOrderBy;
ds@1952
   736
ds@1952
   737
  iTab = pOrderBy->iECursor;
ds@1952
   738
  if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
ds@1952
   739
    pseudoTab = pParse->nTab++;
ds@1952
   740
    sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0);
ds@1952
   741
    sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn);
ds@1952
   742
  }
ds@1952
   743
  addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk);
ds@1952
   744
  codeOffset(v, p, cont, 0);
ds@1952
   745
  if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
ds@1952
   746
    sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
ds@1952
   747
  }
ds@1952
   748
  sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1);
ds@1952
   749
  switch( eDest ){
ds@1952
   750
    case SRT_Table:
ds@1952
   751
    case SRT_EphemTab: {
ds@1952
   752
      sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0);
ds@1952
   753
      sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
ds@1952
   754
      sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND);
ds@1952
   755
      break;
ds@1952
   756
    }
ds@1952
   757
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
   758
    case SRT_Set: {
ds@1952
   759
      assert( nColumn==1 );
ds@1952
   760
      sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
ds@1952
   761
      sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
ds@1952
   762
      sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
ds@1952
   763
      sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1);
ds@1952
   764
      sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0);
ds@1952
   765
      break;
ds@1952
   766
    }
ds@1952
   767
    case SRT_Mem: {
ds@1952
   768
      assert( nColumn==1 );
ds@1952
   769
      sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
ds@1952
   770
      /* The LIMIT clause will terminate the loop for us */
ds@1952
   771
      break;
ds@1952
   772
    }
ds@1952
   773
#endif
ds@1952
   774
    case SRT_Callback:
ds@1952
   775
    case SRT_Subroutine: {
ds@1952
   776
      int i;
ds@1952
   777
      sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0);
ds@1952
   778
      for(i=0; i<nColumn; i++){
ds@1952
   779
        sqlite3VdbeAddOp(v, OP_Column, pseudoTab, i);
ds@1952
   780
      }
ds@1952
   781
      if( eDest==SRT_Callback ){
ds@1952
   782
        sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
ds@1952
   783
      }else{
ds@1952
   784
        sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
ds@1952
   785
      }
ds@1952
   786
      break;
ds@1952
   787
    }
ds@1952
   788
    default: {
ds@1952
   789
      /* Do nothing */
ds@1952
   790
      break;
ds@1952
   791
    }
ds@1952
   792
  }
ds@1952
   793
ds@1952
   794
  /* Jump to the end of the loop when the LIMIT is reached
ds@1952
   795
  */
ds@1952
   796
  if( p->iLimit>=0 ){
ds@1952
   797
    sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit);
ds@1952
   798
    sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, brk);
ds@1952
   799
  }
ds@1952
   800
ds@1952
   801
  /* The bottom of the loop
ds@1952
   802
  */
ds@1952
   803
  sqlite3VdbeResolveLabel(v, cont);
ds@1952
   804
  sqlite3VdbeAddOp(v, OP_Next, iTab, addr);
ds@1952
   805
  sqlite3VdbeResolveLabel(v, brk);
ds@1952
   806
  if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
ds@1952
   807
    sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0);
ds@1952
   808
  }
ds@1952
   809
ds@1952
   810
}
ds@1952
   811
ds@1952
   812
/*
ds@1952
   813
** Return a pointer to a string containing the 'declaration type' of the
ds@1952
   814
** expression pExpr. The string may be treated as static by the caller.
ds@1952
   815
**
ds@1952
   816
** The declaration type is the exact datatype definition extracted from the
ds@1952
   817
** original CREATE TABLE statement if the expression is a column. The
ds@1952
   818
** declaration type for a ROWID field is INTEGER. Exactly when an expression
ds@1952
   819
** is considered a column can be complex in the presence of subqueries. The
ds@1952
   820
** result-set expression in all of the following SELECT statements is 
ds@1952
   821
** considered a column by this function.
ds@1952
   822
**
ds@1952
   823
**   SELECT col FROM tbl;
ds@1952
   824
**   SELECT (SELECT col FROM tbl;
ds@1952
   825
**   SELECT (SELECT col FROM tbl);
ds@1952
   826
**   SELECT abc FROM (SELECT col AS abc FROM tbl);
ds@1952
   827
** 
ds@1952
   828
** The declaration type for any expression other than a column is NULL.
ds@1952
   829
*/
ds@1952
   830
static const char *columnType(
ds@1952
   831
  NameContext *pNC, 
ds@1952
   832
  Expr *pExpr,
ds@1952
   833
  const char **pzOriginDb,
ds@1952
   834
  const char **pzOriginTab,
ds@1952
   835
  const char **pzOriginCol
ds@1952
   836
){
ds@1952
   837
  char const *zType = 0;
ds@1952
   838
  char const *zOriginDb = 0;
ds@1952
   839
  char const *zOriginTab = 0;
ds@1952
   840
  char const *zOriginCol = 0;
ds@1952
   841
  int j;
ds@1952
   842
  if( pExpr==0 || pNC->pSrcList==0 ) return 0;
ds@1952
   843
ds@1952
   844
  switch( pExpr->op ){
ds@1952
   845
    case TK_AGG_COLUMN:
ds@1952
   846
    case TK_COLUMN: {
ds@1952
   847
      /* The expression is a column. Locate the table the column is being
ds@1952
   848
      ** extracted from in NameContext.pSrcList. This table may be real
ds@1952
   849
      ** database table or a subquery.
ds@1952
   850
      */
ds@1952
   851
      Table *pTab = 0;            /* Table structure column is extracted from */
ds@1952
   852
      Select *pS = 0;             /* Select the column is extracted from */
ds@1952
   853
      int iCol = pExpr->iColumn;  /* Index of column in pTab */
ds@1952
   854
      while( pNC && !pTab ){
ds@1952
   855
        SrcList *pTabList = pNC->pSrcList;
ds@1952
   856
        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
ds@1952
   857
        if( j<pTabList->nSrc ){
ds@1952
   858
          pTab = pTabList->a[j].pTab;
ds@1952
   859
          pS = pTabList->a[j].pSelect;
ds@1952
   860
        }else{
ds@1952
   861
          pNC = pNC->pNext;
ds@1952
   862
        }
ds@1952
   863
      }
ds@1952
   864
ds@1952
   865
      if( pTab==0 ){
ds@1952
   866
        /* FIX ME:
ds@1952
   867
        ** This can occurs if you have something like "SELECT new.x;" inside
ds@1952
   868
        ** a trigger.  In other words, if you reference the special "new"
ds@1952
   869
        ** table in the result set of a select.  We do not have a good way
ds@1952
   870
        ** to find the actual table type, so call it "TEXT".  This is really
ds@1952
   871
        ** something of a bug, but I do not know how to fix it.
ds@1952
   872
        **
ds@1952
   873
        ** This code does not produce the correct answer - it just prevents
ds@1952
   874
        ** a segfault.  See ticket #1229.
ds@1952
   875
        */
ds@1952
   876
        zType = "TEXT";
ds@1952
   877
        break;
ds@1952
   878
      }
ds@1952
   879
ds@1952
   880
      assert( pTab );
ds@1952
   881
      if( pS ){
ds@1952
   882
        /* The "table" is actually a sub-select or a view in the FROM clause
ds@1952
   883
        ** of the SELECT statement. Return the declaration type and origin
ds@1952
   884
        ** data for the result-set column of the sub-select.
ds@1952
   885
        */
ds@1952
   886
        if( iCol>=0 && iCol<pS->pEList->nExpr ){
ds@1952
   887
          /* If iCol is less than zero, then the expression requests the
ds@1952
   888
          ** rowid of the sub-select or view. This expression is legal (see 
ds@1952
   889
          ** test case misc2.2.2) - it always evaluates to NULL.
ds@1952
   890
          */
ds@1952
   891
          NameContext sNC;
ds@1952
   892
          Expr *p = pS->pEList->a[iCol].pExpr;
ds@1952
   893
          sNC.pSrcList = pS->pSrc;
ds@1952
   894
          sNC.pNext = 0;
ds@1952
   895
          sNC.pParse = pNC->pParse;
ds@1952
   896
          zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
ds@1952
   897
        }
ds@1952
   898
      }else if( pTab->pSchema ){
ds@1952
   899
        /* A real table */
ds@1952
   900
        assert( !pS );
ds@1952
   901
        if( iCol<0 ) iCol = pTab->iPKey;
ds@1952
   902
        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
ds@1952
   903
        if( iCol<0 ){
ds@1952
   904
          zType = "INTEGER";
ds@1952
   905
          zOriginCol = "rowid";
ds@1952
   906
        }else{
ds@1952
   907
          zType = pTab->aCol[iCol].zType;
ds@1952
   908
          zOriginCol = pTab->aCol[iCol].zName;
ds@1952
   909
        }
ds@1952
   910
        zOriginTab = pTab->zName;
ds@1952
   911
        if( pNC->pParse ){
ds@1952
   912
          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
ds@1952
   913
          zOriginDb = pNC->pParse->db->aDb[iDb].zName;
ds@1952
   914
        }
ds@1952
   915
      }
ds@1952
   916
      break;
ds@1952
   917
    }
ds@1952
   918
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
   919
    case TK_SELECT: {
ds@1952
   920
      /* The expression is a sub-select. Return the declaration type and
ds@1952
   921
      ** origin info for the single column in the result set of the SELECT
ds@1952
   922
      ** statement.
ds@1952
   923
      */
ds@1952
   924
      NameContext sNC;
ds@1952
   925
      Select *pS = pExpr->pSelect;
ds@1952
   926
      Expr *p = pS->pEList->a[0].pExpr;
ds@1952
   927
      sNC.pSrcList = pS->pSrc;
ds@1952
   928
      sNC.pNext = pNC;
ds@1952
   929
      sNC.pParse = pNC->pParse;
ds@1952
   930
      zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
ds@1952
   931
      break;
ds@1952
   932
    }
ds@1952
   933
#endif
ds@1952
   934
  }
ds@1952
   935
  
ds@1952
   936
  if( pzOriginDb ){
ds@1952
   937
    assert( pzOriginTab && pzOriginCol );
ds@1952
   938
    *pzOriginDb = zOriginDb;
ds@1952
   939
    *pzOriginTab = zOriginTab;
ds@1952
   940
    *pzOriginCol = zOriginCol;
ds@1952
   941
  }
ds@1952
   942
  return zType;
ds@1952
   943
}
ds@1952
   944
ds@1952
   945
/*
ds@1952
   946
** Generate code that will tell the VDBE the declaration types of columns
ds@1952
   947
** in the result set.
ds@1952
   948
*/
ds@1952
   949
static void generateColumnTypes(
ds@1952
   950
  Parse *pParse,      /* Parser context */
ds@1952
   951
  SrcList *pTabList,  /* List of tables */
ds@1952
   952
  ExprList *pEList    /* Expressions defining the result set */
ds@1952
   953
){
ds@1952
   954
  Vdbe *v = pParse->pVdbe;
ds@1952
   955
  int i;
ds@1952
   956
  NameContext sNC;
ds@1952
   957
  sNC.pSrcList = pTabList;
ds@1952
   958
  sNC.pParse = pParse;
ds@1952
   959
  for(i=0; i<pEList->nExpr; i++){
ds@1952
   960
    Expr *p = pEList->a[i].pExpr;
ds@1952
   961
    const char *zOrigDb = 0;
ds@1952
   962
    const char *zOrigTab = 0;
ds@1952
   963
    const char *zOrigCol = 0;
ds@1952
   964
    const char *zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
ds@1952
   965
ds@1952
   966
    /* The vdbe must make it's own copy of the column-type and other 
ds@1952
   967
    ** column specific strings, in case the schema is reset before this
ds@1952
   968
    ** virtual machine is deleted.
ds@1952
   969
    */
ds@1952
   970
    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT);
ds@1952
   971
    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT);
ds@1952
   972
    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT);
ds@1952
   973
    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT);
ds@1952
   974
  }
ds@1952
   975
}
ds@1952
   976
ds@1952
   977
/*
ds@1952
   978
** Generate code that will tell the VDBE the names of columns
ds@1952
   979
** in the result set.  This information is used to provide the
ds@1952
   980
** azCol[] values in the callback.
ds@1952
   981
*/
ds@1952
   982
static void generateColumnNames(
ds@1952
   983
  Parse *pParse,      /* Parser context */
ds@1952
   984
  SrcList *pTabList,  /* List of tables */
ds@1952
   985
  ExprList *pEList    /* Expressions defining the result set */
ds@1952
   986
){
ds@1952
   987
  Vdbe *v = pParse->pVdbe;
ds@1952
   988
  int i, j;
ds@1952
   989
  sqlite3 *db = pParse->db;
ds@1952
   990
  int fullNames, shortNames;
ds@1952
   991
ds@1952
   992
#ifndef SQLITE_OMIT_EXPLAIN
ds@1952
   993
  /* If this is an EXPLAIN, skip this step */
ds@1952
   994
  if( pParse->explain ){
ds@1952
   995
    return;
ds@1952
   996
  }
ds@1952
   997
#endif
ds@1952
   998
ds@1952
   999
  assert( v!=0 );
ds@1952
  1000
  if( pParse->colNamesSet || v==0 || db->mallocFailed ) return;
ds@1952
  1001
  pParse->colNamesSet = 1;
ds@1952
  1002
  fullNames = (db->flags & SQLITE_FullColNames)!=0;
ds@1952
  1003
  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
ds@1952
  1004
  sqlite3VdbeSetNumCols(v, pEList->nExpr);
ds@1952
  1005
  for(i=0; i<pEList->nExpr; i++){
ds@1952
  1006
    Expr *p;
ds@1952
  1007
    p = pEList->a[i].pExpr;
ds@1952
  1008
    if( p==0 ) continue;
ds@1952
  1009
    if( pEList->a[i].zName ){
ds@1952
  1010
      char *zName = pEList->a[i].zName;
ds@1952
  1011
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, strlen(zName));
ds@1952
  1012
      continue;
ds@1952
  1013
    }
ds@1952
  1014
    if( p->op==TK_COLUMN && pTabList ){
ds@1952
  1015
      Table *pTab;
ds@1952
  1016
      char *zCol;
ds@1952
  1017
      int iCol = p->iColumn;
ds@1952
  1018
      for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
ds@1952
  1019
      assert( j<pTabList->nSrc );
ds@1952
  1020
      pTab = pTabList->a[j].pTab;
ds@1952
  1021
      if( iCol<0 ) iCol = pTab->iPKey;
ds@1952
  1022
      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
ds@1952
  1023
      if( iCol<0 ){
ds@1952
  1024
        zCol = "rowid";
ds@1952
  1025
      }else{
ds@1952
  1026
        zCol = pTab->aCol[iCol].zName;
ds@1952
  1027
      }
ds@1952
  1028
      if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
ds@1952
  1029
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
ds@1952
  1030
      }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
ds@1952
  1031
        char *zName = 0;
ds@1952
  1032
        char *zTab;
ds@1952
  1033
 
ds@1952
  1034
        zTab = pTabList->a[j].zAlias;
ds@1952
  1035
        if( fullNames || zTab==0 ) zTab = pTab->zName;
ds@1952
  1036
        sqlite3SetString(&zName, zTab, ".", zCol, (char*)0);
ds@1952
  1037
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC);
ds@1952
  1038
      }else{
ds@1952
  1039
        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol));
ds@1952
  1040
      }
ds@1952
  1041
    }else if( p->span.z && p->span.z[0] ){
ds@1952
  1042
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n);
ds@1952
  1043
      /* sqlite3VdbeCompressSpace(v, addr); */
ds@1952
  1044
    }else{
ds@1952
  1045
      char zName[30];
ds@1952
  1046
      assert( p->op!=TK_COLUMN || pTabList==0 );
ds@1952
  1047
      sqlite3_snprintf(sizeof(zName), zName, "column%d", i+1);
ds@1952
  1048
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0);
ds@1952
  1049
    }
ds@1952
  1050
  }
ds@1952
  1051
  generateColumnTypes(pParse, pTabList, pEList);
ds@1952
  1052
}
ds@1952
  1053
ds@1952
  1054
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
  1055
/*
ds@1952
  1056
** Name of the connection operator, used for error messages.
ds@1952
  1057
*/
ds@1952
  1058
static const char *selectOpName(int id){
ds@1952
  1059
  char *z;
ds@1952
  1060
  switch( id ){
ds@1952
  1061
    case TK_ALL:       z = "UNION ALL";   break;
ds@1952
  1062
    case TK_INTERSECT: z = "INTERSECT";   break;
ds@1952
  1063
    case TK_EXCEPT:    z = "EXCEPT";      break;
ds@1952
  1064
    default:           z = "UNION";       break;
ds@1952
  1065
  }
ds@1952
  1066
  return z;
ds@1952
  1067
}
ds@1952
  1068
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
ds@1952
  1069
ds@1952
  1070
/*
ds@1952
  1071
** Forward declaration
ds@1952
  1072
*/
ds@1952
  1073
static int prepSelectStmt(Parse*, Select*);
ds@1952
  1074
ds@1952
  1075
/*
ds@1952
  1076
** Given a SELECT statement, generate a Table structure that describes
ds@1952
  1077
** the result set of that SELECT.
ds@1952
  1078
*/
ds@1952
  1079
Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
ds@1952
  1080
  Table *pTab;
ds@1952
  1081
  int i, j;
ds@1952
  1082
  ExprList *pEList;
ds@1952
  1083
  Column *aCol, *pCol;
ds@1952
  1084
  sqlite3 *db = pParse->db;
ds@1952
  1085
ds@1952
  1086
  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
ds@1952
  1087
  if( prepSelectStmt(pParse, pSelect) ){
ds@1952
  1088
    return 0;
ds@1952
  1089
  }
ds@1952
  1090
  if( sqlite3SelectResolve(pParse, pSelect, 0) ){
ds@1952
  1091
    return 0;
ds@1952
  1092
  }
ds@1952
  1093
  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
ds@1952
  1094
  if( pTab==0 ){
ds@1952
  1095
    return 0;
ds@1952
  1096
  }
ds@1952
  1097
  pTab->nRef = 1;
ds@1952
  1098
  pTab->zName = zTabName ? sqlite3DbStrDup(db, zTabName) : 0;
ds@1952
  1099
  pEList = pSelect->pEList;
ds@1952
  1100
  pTab->nCol = pEList->nExpr;
ds@1952
  1101
  assert( pTab->nCol>0 );
ds@1952
  1102
  pTab->aCol = aCol = sqlite3DbMallocZero(db, sizeof(pTab->aCol[0])*pTab->nCol);
ds@1952
  1103
  for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
ds@1952
  1104
    Expr *p, *pR;
ds@1952
  1105
    char *zType;
ds@1952
  1106
    char *zName;
ds@1952
  1107
    int nName;
ds@1952
  1108
    CollSeq *pColl;
ds@1952
  1109
    int cnt;
ds@1952
  1110
    NameContext sNC;
ds@1952
  1111
    
ds@1952
  1112
    /* Get an appropriate name for the column
ds@1952
  1113
    */
ds@1952
  1114
    p = pEList->a[i].pExpr;
ds@1952
  1115
    assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
ds@1952
  1116
    if( (zName = pEList->a[i].zName)!=0 ){
ds@1952
  1117
      /* If the column contains an "AS <name>" phrase, use <name> as the name */
ds@1952
  1118
      zName = sqlite3DbStrDup(db, zName);
ds@1952
  1119
    }else if( p->op==TK_DOT 
ds@1952
  1120
              && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
ds@1952
  1121
      /* For columns of the from A.B use B as the name */
ds@1952
  1122
      zName = sqlite3MPrintf(db, "%T", &pR->token);
ds@1952
  1123
    }else if( p->span.z && p->span.z[0] ){
ds@1952
  1124
      /* Use the original text of the column expression as its name */
ds@1952
  1125
      zName = sqlite3MPrintf(db, "%T", &p->span);
ds@1952
  1126
    }else{
ds@1952
  1127
      /* If all else fails, make up a name */
ds@1952
  1128
      zName = sqlite3MPrintf(db, "column%d", i+1);
ds@1952
  1129
    }
ds@1952
  1130
    if( !zName || db->mallocFailed ){
ds@1952
  1131
      db->mallocFailed = 1;
ds@1952
  1132
      sqlite3_free(zName);
ds@1952
  1133
      sqlite3DeleteTable(pTab);
ds@1952
  1134
      return 0;
ds@1952
  1135
    }
ds@1952
  1136
    sqlite3Dequote(zName);
ds@1952
  1137
ds@1952
  1138
    /* Make sure the column name is unique.  If the name is not unique,
ds@1952
  1139
    ** append a integer to the name so that it becomes unique.
ds@1952
  1140
    */
ds@1952
  1141
    nName = strlen(zName);
ds@1952
  1142
    for(j=cnt=0; j<i; j++){
ds@1952
  1143
      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
ds@1952
  1144
        zName[nName] = 0;
ds@1952
  1145
        zName = sqlite3MPrintf(db, "%z:%d", zName, ++cnt);
ds@1952
  1146
        j = -1;
ds@1952
  1147
        if( zName==0 ) break;
ds@1952
  1148
      }
ds@1952
  1149
    }
ds@1952
  1150
    pCol->zName = zName;
ds@1952
  1151
ds@1952
  1152
    /* Get the typename, type affinity, and collating sequence for the
ds@1952
  1153
    ** column.
ds@1952
  1154
    */
ds@1952
  1155
    memset(&sNC, 0, sizeof(sNC));
ds@1952
  1156
    sNC.pSrcList = pSelect->pSrc;
ds@1952
  1157
    zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
ds@1952
  1158
    pCol->zType = zType;
ds@1952
  1159
    pCol->affinity = sqlite3ExprAffinity(p);
ds@1952
  1160
    pColl = sqlite3ExprCollSeq(pParse, p);
ds@1952
  1161
    if( pColl ){
ds@1952
  1162
      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
ds@1952
  1163
    }
ds@1952
  1164
  }
ds@1952
  1165
  pTab->iPKey = -1;
ds@1952
  1166
  return pTab;
ds@1952
  1167
}
ds@1952
  1168
ds@1952
  1169
/*
ds@1952
  1170
** Prepare a SELECT statement for processing by doing the following
ds@1952
  1171
** things:
ds@1952
  1172
**
ds@1952
  1173
**    (1)  Make sure VDBE cursor numbers have been assigned to every
ds@1952
  1174
**         element of the FROM clause.
ds@1952
  1175
**
ds@1952
  1176
**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
ds@1952
  1177
**         defines FROM clause.  When views appear in the FROM clause,
ds@1952
  1178
**         fill pTabList->a[].pSelect with a copy of the SELECT statement
ds@1952
  1179
**         that implements the view.  A copy is made of the view's SELECT
ds@1952
  1180
**         statement so that we can freely modify or delete that statement
ds@1952
  1181
**         without worrying about messing up the presistent representation
ds@1952
  1182
**         of the view.
ds@1952
  1183
**
ds@1952
  1184
**    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
ds@1952
  1185
**         on joins and the ON and USING clause of joins.
ds@1952
  1186
**
ds@1952
  1187
**    (4)  Scan the list of columns in the result set (pEList) looking
ds@1952
  1188
**         for instances of the "*" operator or the TABLE.* operator.
ds@1952
  1189
**         If found, expand each "*" to be every column in every table
ds@1952
  1190
**         and TABLE.* to be every column in TABLE.
ds@1952
  1191
**
ds@1952
  1192
** Return 0 on success.  If there are problems, leave an error message
ds@1952
  1193
** in pParse and return non-zero.
ds@1952
  1194
*/
ds@1952
  1195
static int prepSelectStmt(Parse *pParse, Select *p){
ds@1952
  1196
  int i, j, k, rc;
ds@1952
  1197
  SrcList *pTabList;
ds@1952
  1198
  ExprList *pEList;
ds@1952
  1199
  struct SrcList_item *pFrom;
ds@1952
  1200
  sqlite3 *db = pParse->db;
ds@1952
  1201
ds@1952
  1202
  if( p==0 || p->pSrc==0 || db->mallocFailed ){
ds@1952
  1203
    return 1;
ds@1952
  1204
  }
ds@1952
  1205
  pTabList = p->pSrc;
ds@1952
  1206
  pEList = p->pEList;
ds@1952
  1207
ds@1952
  1208
  /* Make sure cursor numbers have been assigned to all entries in
ds@1952
  1209
  ** the FROM clause of the SELECT statement.
ds@1952
  1210
  */
ds@1952
  1211
  sqlite3SrcListAssignCursors(pParse, p->pSrc);
ds@1952
  1212
ds@1952
  1213
  /* Look up every table named in the FROM clause of the select.  If
ds@1952
  1214
  ** an entry of the FROM clause is a subquery instead of a table or view,
ds@1952
  1215
  ** then create a transient table structure to describe the subquery.
ds@1952
  1216
  */
ds@1952
  1217
  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
ds@1952
  1218
    Table *pTab;
ds@1952
  1219
    if( pFrom->pTab!=0 ){
ds@1952
  1220
      /* This statement has already been prepared.  There is no need
ds@1952
  1221
      ** to go further. */
ds@1952
  1222
      assert( i==0 );
ds@1952
  1223
      return 0;
ds@1952
  1224
    }
ds@1952
  1225
    if( pFrom->zName==0 ){
ds@1952
  1226
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
  1227
      /* A sub-query in the FROM clause of a SELECT */
ds@1952
  1228
      assert( pFrom->pSelect!=0 );
ds@1952
  1229
      if( pFrom->zAlias==0 ){
ds@1952
  1230
        pFrom->zAlias =
ds@1952
  1231
          sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pFrom->pSelect);
ds@1952
  1232
      }
ds@1952
  1233
      assert( pFrom->pTab==0 );
ds@1952
  1234
      pFrom->pTab = pTab = 
ds@1952
  1235
        sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect);
ds@1952
  1236
      if( pTab==0 ){
ds@1952
  1237
        return 1;
ds@1952
  1238
      }
ds@1952
  1239
      /* The isEphem flag indicates that the Table structure has been
ds@1952
  1240
      ** dynamically allocated and may be freed at any time.  In other words,
ds@1952
  1241
      ** pTab is not pointing to a persistent table structure that defines
ds@1952
  1242
      ** part of the schema. */
ds@1952
  1243
      pTab->isEphem = 1;
ds@1952
  1244
#endif
ds@1952
  1245
    }else{
ds@1952
  1246
      /* An ordinary table or view name in the FROM clause */
ds@1952
  1247
      assert( pFrom->pTab==0 );
ds@1952
  1248
      pFrom->pTab = pTab = 
ds@1952
  1249
        sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase);
ds@1952
  1250
      if( pTab==0 ){
ds@1952
  1251
        return 1;
ds@1952
  1252
      }
ds@1952
  1253
      pTab->nRef++;
ds@1952
  1254
#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
ds@1952
  1255
      if( pTab->pSelect || IsVirtual(pTab) ){
ds@1952
  1256
        /* We reach here if the named table is a really a view */
ds@1952
  1257
        if( sqlite3ViewGetColumnNames(pParse, pTab) ){
ds@1952
  1258
          return 1;
ds@1952
  1259
        }
ds@1952
  1260
        /* If pFrom->pSelect!=0 it means we are dealing with a
ds@1952
  1261
        ** view within a view.  The SELECT structure has already been
ds@1952
  1262
        ** copied by the outer view so we can skip the copy step here
ds@1952
  1263
        ** in the inner view.
ds@1952
  1264
        */
ds@1952
  1265
        if( pFrom->pSelect==0 ){
ds@1952
  1266
          pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect);
ds@1952
  1267
        }
ds@1952
  1268
      }
ds@1952
  1269
#endif
ds@1952
  1270
    }
ds@1952
  1271
  }
ds@1952
  1272
ds@1952
  1273
  /* Process NATURAL keywords, and ON and USING clauses of joins.
ds@1952
  1274
  */
ds@1952
  1275
  if( sqliteProcessJoin(pParse, p) ) return 1;
ds@1952
  1276
ds@1952
  1277
  /* For every "*" that occurs in the column list, insert the names of
ds@1952
  1278
  ** all columns in all tables.  And for every TABLE.* insert the names
ds@1952
  1279
  ** of all columns in TABLE.  The parser inserted a special expression
ds@1952
  1280
  ** with the TK_ALL operator for each "*" that it found in the column list.
ds@1952
  1281
  ** The following code just has to locate the TK_ALL expressions and expand
ds@1952
  1282
  ** each one to the list of all columns in all tables.
ds@1952
  1283
  **
ds@1952
  1284
  ** The first loop just checks to see if there are any "*" operators
ds@1952
  1285
  ** that need expanding.
ds@1952
  1286
  */
ds@1952
  1287
  for(k=0; k<pEList->nExpr; k++){
ds@1952
  1288
    Expr *pE = pEList->a[k].pExpr;
ds@1952
  1289
    if( pE->op==TK_ALL ) break;
ds@1952
  1290
    if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
ds@1952
  1291
         && pE->pLeft && pE->pLeft->op==TK_ID ) break;
ds@1952
  1292
  }
ds@1952
  1293
  rc = 0;
ds@1952
  1294
  if( k<pEList->nExpr ){
ds@1952
  1295
    /*
ds@1952
  1296
    ** If we get here it means the result set contains one or more "*"
ds@1952
  1297
    ** operators that need to be expanded.  Loop through each expression
ds@1952
  1298
    ** in the result set and expand them one by one.
ds@1952
  1299
    */
ds@1952
  1300
    struct ExprList_item *a = pEList->a;
ds@1952
  1301
    ExprList *pNew = 0;
ds@1952
  1302
    int flags = pParse->db->flags;
ds@1952
  1303
    int longNames = (flags & SQLITE_FullColNames)!=0 &&
ds@1952
  1304
                      (flags & SQLITE_ShortColNames)==0;
ds@1952
  1305
ds@1952
  1306
    for(k=0; k<pEList->nExpr; k++){
ds@1952
  1307
      Expr *pE = a[k].pExpr;
ds@1952
  1308
      if( pE->op!=TK_ALL &&
ds@1952
  1309
           (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
ds@1952
  1310
        /* This particular expression does not need to be expanded.
ds@1952
  1311
        */
ds@1952
  1312
        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
ds@1952
  1313
        if( pNew ){
ds@1952
  1314
          pNew->a[pNew->nExpr-1].zName = a[k].zName;
ds@1952
  1315
        }else{
ds@1952
  1316
          rc = 1;
ds@1952
  1317
        }
ds@1952
  1318
        a[k].pExpr = 0;
ds@1952
  1319
        a[k].zName = 0;
ds@1952
  1320
      }else{
ds@1952
  1321
        /* This expression is a "*" or a "TABLE.*" and needs to be
ds@1952
  1322
        ** expanded. */
ds@1952
  1323
        int tableSeen = 0;      /* Set to 1 when TABLE matches */
ds@1952
  1324
        char *zTName;            /* text of name of TABLE */
ds@1952
  1325
        if( pE->op==TK_DOT && pE->pLeft ){
ds@1952
  1326
          zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
ds@1952
  1327
        }else{
ds@1952
  1328
          zTName = 0;
ds@1952
  1329
        }
ds@1952
  1330
        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
ds@1952
  1331
          Table *pTab = pFrom->pTab;
ds@1952
  1332
          char *zTabName = pFrom->zAlias;
ds@1952
  1333
          if( zTabName==0 || zTabName[0]==0 ){ 
ds@1952
  1334
            zTabName = pTab->zName;
ds@1952
  1335
          }
ds@1952
  1336
          if( zTName && (zTabName==0 || zTabName[0]==0 || 
ds@1952
  1337
                 sqlite3StrICmp(zTName, zTabName)!=0) ){
ds@1952
  1338
            continue;
ds@1952
  1339
          }
ds@1952
  1340
          tableSeen = 1;
ds@1952
  1341
          for(j=0; j<pTab->nCol; j++){
ds@1952
  1342
            Expr *pExpr, *pRight;
ds@1952
  1343
            char *zName = pTab->aCol[j].zName;
ds@1952
  1344
ds@1952
  1345
            /* If a column is marked as 'hidden' (currently only possible
ds@1952
  1346
            ** for virtual tables), do not include it in the expanded
ds@1952
  1347
            ** result-set list.
ds@1952
  1348
            */
ds@1952
  1349
            if( IsHiddenColumn(&pTab->aCol[j]) ){
ds@1952
  1350
              assert(IsVirtual(pTab));
ds@1952
  1351
              continue;
ds@1952
  1352
            }
ds@1952
  1353
ds@1952
  1354
            if( i>0 ){
ds@1952
  1355
              struct SrcList_item *pLeft = &pTabList->a[i-1];
ds@1952
  1356
              if( (pLeft[1].jointype & JT_NATURAL)!=0 &&
ds@1952
  1357
                        columnIndex(pLeft->pTab, zName)>=0 ){
ds@1952
  1358
                /* In a NATURAL join, omit the join columns from the 
ds@1952
  1359
                ** table on the right */
ds@1952
  1360
                continue;
ds@1952
  1361
              }
ds@1952
  1362
              if( sqlite3IdListIndex(pLeft[1].pUsing, zName)>=0 ){
ds@1952
  1363
                /* In a join with a USING clause, omit columns in the
ds@1952
  1364
                ** using clause from the table on the right. */
ds@1952
  1365
                continue;
ds@1952
  1366
              }
ds@1952
  1367
            }
ds@1952
  1368
            pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
ds@1952
  1369
            if( pRight==0 ) break;
ds@1952
  1370
            setQuotedToken(pParse, &pRight->token, zName);
ds@1952
  1371
            if( zTabName && (longNames || pTabList->nSrc>1) ){
ds@1952
  1372
              Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
ds@1952
  1373
              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
ds@1952
  1374
              if( pExpr==0 ) break;
ds@1952
  1375
              setQuotedToken(pParse, &pLeft->token, zTabName);
ds@1952
  1376
              setToken(&pExpr->span, 
ds@1952
  1377
                  sqlite3MPrintf(db, "%s.%s", zTabName, zName));
ds@1952
  1378
              pExpr->span.dyn = 1;
ds@1952
  1379
              pExpr->token.z = 0;
ds@1952
  1380
              pExpr->token.n = 0;
ds@1952
  1381
              pExpr->token.dyn = 0;
ds@1952
  1382
            }else{
ds@1952
  1383
              pExpr = pRight;
ds@1952
  1384
              pExpr->span = pExpr->token;
ds@1952
  1385
              pExpr->span.dyn = 0;
ds@1952
  1386
            }
ds@1952
  1387
            if( longNames ){
ds@1952
  1388
              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span);
ds@1952
  1389
            }else{
ds@1952
  1390
              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token);
ds@1952
  1391
            }
ds@1952
  1392
          }
ds@1952
  1393
        }
ds@1952
  1394
        if( !tableSeen ){
ds@1952
  1395
          if( zTName ){
ds@1952
  1396
            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
ds@1952
  1397
          }else{
ds@1952
  1398
            sqlite3ErrorMsg(pParse, "no tables specified");
ds@1952
  1399
          }
ds@1952
  1400
          rc = 1;
ds@1952
  1401
        }
ds@1952
  1402
        sqlite3_free(zTName);
ds@1952
  1403
      }
ds@1952
  1404
    }
ds@1952
  1405
    sqlite3ExprListDelete(pEList);
ds@1952
  1406
    p->pEList = pNew;
ds@1952
  1407
  }
ds@1952
  1408
  if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){
ds@1952
  1409
    sqlite3ErrorMsg(pParse, "too many columns in result set");
ds@1952
  1410
    rc = SQLITE_ERROR;
ds@1952
  1411
  }
ds@1952
  1412
  if( db->mallocFailed ){
ds@1952
  1413
    rc = SQLITE_NOMEM;
ds@1952
  1414
  }
ds@1952
  1415
  return rc;
ds@1952
  1416
}
ds@1952
  1417
ds@1952
  1418
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
  1419
/*
ds@1952
  1420
** This routine associates entries in an ORDER BY expression list with
ds@1952
  1421
** columns in a result.  For each ORDER BY expression, the opcode of
ds@1952
  1422
** the top-level node is changed to TK_COLUMN and the iColumn value of
ds@1952
  1423
** the top-level node is filled in with column number and the iTable
ds@1952
  1424
** value of the top-level node is filled with iTable parameter.
ds@1952
  1425
**
ds@1952
  1426
** If there are prior SELECT clauses, they are processed first.  A match
ds@1952
  1427
** in an earlier SELECT takes precedence over a later SELECT.
ds@1952
  1428
**
ds@1952
  1429
** Any entry that does not match is flagged as an error.  The number
ds@1952
  1430
** of errors is returned.
ds@1952
  1431
*/
ds@1952
  1432
static int matchOrderbyToColumn(
ds@1952
  1433
  Parse *pParse,          /* A place to leave error messages */
ds@1952
  1434
  Select *pSelect,        /* Match to result columns of this SELECT */
ds@1952
  1435
  ExprList *pOrderBy,     /* The ORDER BY values to match against columns */
ds@1952
  1436
  int iTable,             /* Insert this value in iTable */
ds@1952
  1437
  int mustComplete        /* If TRUE all ORDER BYs must match */
ds@1952
  1438
){
ds@1952
  1439
  int nErr = 0;
ds@1952
  1440
  int i, j;
ds@1952
  1441
  ExprList *pEList;
ds@1952
  1442
  sqlite3 *db = pParse->db;
ds@1952
  1443
ds@1952
  1444
  if( pSelect==0 || pOrderBy==0 ) return 1;
ds@1952
  1445
  if( mustComplete ){
ds@1952
  1446
    for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
ds@1952
  1447
  }
ds@1952
  1448
  if( prepSelectStmt(pParse, pSelect) ){
ds@1952
  1449
    return 1;
ds@1952
  1450
  }
ds@1952
  1451
  if( pSelect->pPrior ){
ds@1952
  1452
    if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
ds@1952
  1453
      return 1;
ds@1952
  1454
    }
ds@1952
  1455
  }
ds@1952
  1456
  pEList = pSelect->pEList;
ds@1952
  1457
  for(i=0; i<pOrderBy->nExpr; i++){
ds@1952
  1458
    struct ExprList_item *pItem;
ds@1952
  1459
    Expr *pE = pOrderBy->a[i].pExpr;
ds@1952
  1460
    int iCol = -1;
ds@1952
  1461
    char *zLabel;
ds@1952
  1462
ds@1952
  1463
    if( pOrderBy->a[i].done ) continue;
ds@1952
  1464
    if( sqlite3ExprIsInteger(pE, &iCol) ){
ds@1952
  1465
      if( iCol<=0 || iCol>pEList->nExpr ){
ds@1952
  1466
        sqlite3ErrorMsg(pParse,
ds@1952
  1467
          "ORDER BY position %d should be between 1 and %d",
ds@1952
  1468
          iCol, pEList->nExpr);
ds@1952
  1469
        nErr++;
ds@1952
  1470
        break;
ds@1952
  1471
      }
ds@1952
  1472
      if( !mustComplete ) continue;
ds@1952
  1473
      iCol--;
ds@1952
  1474
    }
ds@1952
  1475
    if( iCol<0 && (zLabel = sqlite3NameFromToken(db, &pE->token))!=0 ){
ds@1952
  1476
      for(j=0, pItem=pEList->a; j<pEList->nExpr; j++, pItem++){
ds@1952
  1477
        char *zName;
ds@1952
  1478
        int isMatch;
ds@1952
  1479
        if( pItem->zName ){
ds@1952
  1480
          zName = sqlite3DbStrDup(db, pItem->zName);
ds@1952
  1481
        }else{
ds@1952
  1482
          zName = sqlite3NameFromToken(db, &pItem->pExpr->token);
ds@1952
  1483
        }
ds@1952
  1484
        isMatch = zName && sqlite3StrICmp(zName, zLabel)==0;
ds@1952
  1485
        sqlite3_free(zName);
ds@1952
  1486
        if( isMatch ){
ds@1952
  1487
          iCol = j;
ds@1952
  1488
          break;
ds@1952
  1489
        }
ds@1952
  1490
      }
ds@1952
  1491
      sqlite3_free(zLabel);
ds@1952
  1492
    }
ds@1952
  1493
    if( iCol>=0 ){
ds@1952
  1494
      pE->op = TK_COLUMN;
ds@1952
  1495
      pE->iColumn = iCol;
ds@1952
  1496
      pE->iTable = iTable;
ds@1952
  1497
      pE->iAgg = -1;
ds@1952
  1498
      pOrderBy->a[i].done = 1;
ds@1952
  1499
    }else if( mustComplete ){
ds@1952
  1500
      sqlite3ErrorMsg(pParse,
ds@1952
  1501
        "ORDER BY term number %d does not match any result column", i+1);
ds@1952
  1502
      nErr++;
ds@1952
  1503
      break;
ds@1952
  1504
    }
ds@1952
  1505
  }
ds@1952
  1506
  return nErr;  
ds@1952
  1507
}
ds@1952
  1508
#endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */
ds@1952
  1509
ds@1952
  1510
/*
ds@1952
  1511
** Get a VDBE for the given parser context.  Create a new one if necessary.
ds@1952
  1512
** If an error occurs, return NULL and leave a message in pParse.
ds@1952
  1513
*/
ds@1952
  1514
Vdbe *sqlite3GetVdbe(Parse *pParse){
ds@1952
  1515
  Vdbe *v = pParse->pVdbe;
ds@1952
  1516
  if( v==0 ){
ds@1952
  1517
    v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
ds@1952
  1518
  }
ds@1952
  1519
  return v;
ds@1952
  1520
}
ds@1952
  1521
ds@1952
  1522
ds@1952
  1523
/*
ds@1952
  1524
** Compute the iLimit and iOffset fields of the SELECT based on the
ds@1952
  1525
** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
ds@1952
  1526
** that appear in the original SQL statement after the LIMIT and OFFSET
ds@1952
  1527
** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
ds@1952
  1528
** are the integer memory register numbers for counters used to compute 
ds@1952
  1529
** the limit and offset.  If there is no limit and/or offset, then 
ds@1952
  1530
** iLimit and iOffset are negative.
ds@1952
  1531
**
ds@1952
  1532
** This routine changes the values of iLimit and iOffset only if
ds@1952
  1533
** a limit or offset is defined by pLimit and pOffset.  iLimit and
ds@1952
  1534
** iOffset should have been preset to appropriate default values
ds@1952
  1535
** (usually but not always -1) prior to calling this routine.
ds@1952
  1536
** Only if pLimit!=0 or pOffset!=0 do the limit registers get
ds@1952
  1537
** redefined.  The UNION ALL operator uses this property to force
ds@1952
  1538
** the reuse of the same limit and offset registers across multiple
ds@1952
  1539
** SELECT statements.
ds@1952
  1540
*/
ds@1952
  1541
static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
ds@1952
  1542
  Vdbe *v = 0;
ds@1952
  1543
  int iLimit = 0;
ds@1952
  1544
  int iOffset;
ds@1952
  1545
  int addr1, addr2;
ds@1952
  1546
ds@1952
  1547
  /* 
ds@1952
  1548
  ** "LIMIT -1" always shows all rows.  There is some
ds@1952
  1549
  ** contraversy about what the correct behavior should be.
ds@1952
  1550
  ** The current implementation interprets "LIMIT 0" to mean
ds@1952
  1551
  ** no rows.
ds@1952
  1552
  */
ds@1952
  1553
  if( p->pLimit ){
ds@1952
  1554
    p->iLimit = iLimit = pParse->nMem;
ds@1952
  1555
    pParse->nMem += 2;
ds@1952
  1556
    v = sqlite3GetVdbe(pParse);
ds@1952
  1557
    if( v==0 ) return;
ds@1952
  1558
    sqlite3ExprCode(pParse, p->pLimit);
ds@1952
  1559
    sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
ds@1952
  1560
    sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1);
ds@1952
  1561
    VdbeComment((v, "# LIMIT counter"));
ds@1952
  1562
    sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak);
ds@1952
  1563
    sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0);
ds@1952
  1564
  }
ds@1952
  1565
  if( p->pOffset ){
ds@1952
  1566
    p->iOffset = iOffset = pParse->nMem++;
ds@1952
  1567
    v = sqlite3GetVdbe(pParse);
ds@1952
  1568
    if( v==0 ) return;
ds@1952
  1569
    sqlite3ExprCode(pParse, p->pOffset);
ds@1952
  1570
    sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
ds@1952
  1571
    sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0);
ds@1952
  1572
    VdbeComment((v, "# OFFSET counter"));
ds@1952
  1573
    addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0);
ds@1952
  1574
    sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
ds@1952
  1575
    sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
ds@1952
  1576
    sqlite3VdbeJumpHere(v, addr1);
ds@1952
  1577
    if( p->pLimit ){
ds@1952
  1578
      sqlite3VdbeAddOp(v, OP_Add, 0, 0);
ds@1952
  1579
    }
ds@1952
  1580
  }
ds@1952
  1581
  if( p->pLimit ){
ds@1952
  1582
    addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0);
ds@1952
  1583
    sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
ds@1952
  1584
    sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1);
ds@1952
  1585
    addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
ds@1952
  1586
    sqlite3VdbeJumpHere(v, addr1);
ds@1952
  1587
    sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1);
ds@1952
  1588
    VdbeComment((v, "# LIMIT+OFFSET"));
ds@1952
  1589
    sqlite3VdbeJumpHere(v, addr2);
ds@1952
  1590
  }
ds@1952
  1591
}
ds@1952
  1592
ds@1952
  1593
/*
ds@1952
  1594
** Allocate a virtual index to use for sorting.
ds@1952
  1595
*/
ds@1952
  1596
static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){
ds@1952
  1597
  if( pOrderBy ){
ds@1952
  1598
    int addr;
ds@1952
  1599
    assert( pOrderBy->iECursor==0 );
ds@1952
  1600
    pOrderBy->iECursor = pParse->nTab++;
ds@1952
  1601
    addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral,
ds@1952
  1602
                            pOrderBy->iECursor, pOrderBy->nExpr+1);
ds@1952
  1603
    assert( p->addrOpenEphm[2] == -1 );
ds@1952
  1604
    p->addrOpenEphm[2] = addr;
ds@1952
  1605
  }
ds@1952
  1606
}
ds@1952
  1607
ds@1952
  1608
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
  1609
/*
ds@1952
  1610
** Return the appropriate collating sequence for the iCol-th column of
ds@1952
  1611
** the result set for the compound-select statement "p".  Return NULL if
ds@1952
  1612
** the column has no default collating sequence.
ds@1952
  1613
**
ds@1952
  1614
** The collating sequence for the compound select is taken from the
ds@1952
  1615
** left-most term of the select that has a collating sequence.
ds@1952
  1616
*/
ds@1952
  1617
static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
ds@1952
  1618
  CollSeq *pRet;
ds@1952
  1619
  if( p->pPrior ){
ds@1952
  1620
    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
ds@1952
  1621
  }else{
ds@1952
  1622
    pRet = 0;
ds@1952
  1623
  }
ds@1952
  1624
  if( pRet==0 ){
ds@1952
  1625
    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
ds@1952
  1626
  }
ds@1952
  1627
  return pRet;
ds@1952
  1628
}
ds@1952
  1629
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
ds@1952
  1630
ds@1952
  1631
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
  1632
/*
ds@1952
  1633
** This routine is called to process a query that is really the union
ds@1952
  1634
** or intersection of two or more separate queries.
ds@1952
  1635
**
ds@1952
  1636
** "p" points to the right-most of the two queries.  the query on the
ds@1952
  1637
** left is p->pPrior.  The left query could also be a compound query
ds@1952
  1638
** in which case this routine will be called recursively. 
ds@1952
  1639
**
ds@1952
  1640
** The results of the total query are to be written into a destination
ds@1952
  1641
** of type eDest with parameter iParm.
ds@1952
  1642
**
ds@1952
  1643
** Example 1:  Consider a three-way compound SQL statement.
ds@1952
  1644
**
ds@1952
  1645
**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
ds@1952
  1646
**
ds@1952
  1647
** This statement is parsed up as follows:
ds@1952
  1648
**
ds@1952
  1649
**     SELECT c FROM t3
ds@1952
  1650
**      |
ds@1952
  1651
**      `----->  SELECT b FROM t2
ds@1952
  1652
**                |
ds@1952
  1653
**                `------>  SELECT a FROM t1
ds@1952
  1654
**
ds@1952
  1655
** The arrows in the diagram above represent the Select.pPrior pointer.
ds@1952
  1656
** So if this routine is called with p equal to the t3 query, then
ds@1952
  1657
** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
ds@1952
  1658
**
ds@1952
  1659
** Notice that because of the way SQLite parses compound SELECTs, the
ds@1952
  1660
** individual selects always group from left to right.
ds@1952
  1661
*/
ds@1952
  1662
static int multiSelect(
ds@1952
  1663
  Parse *pParse,        /* Parsing context */
ds@1952
  1664
  Select *p,            /* The right-most of SELECTs to be coded */
ds@1952
  1665
  int eDest,            /* \___  Store query results as specified */
ds@1952
  1666
  int iParm,            /* /     by these two parameters.         */
ds@1952
  1667
  char *aff             /* If eDest is SRT_Union, the affinity string */
ds@1952
  1668
){
ds@1952
  1669
  int rc = SQLITE_OK;   /* Success code from a subroutine */
ds@1952
  1670
  Select *pPrior;       /* Another SELECT immediately to our left */
ds@1952
  1671
  Vdbe *v;              /* Generate code to this VDBE */
ds@1952
  1672
  int nCol;             /* Number of columns in the result set */
ds@1952
  1673
  ExprList *pOrderBy;   /* The ORDER BY clause on p */
ds@1952
  1674
  int aSetP2[2];        /* Set P2 value of these op to number of columns */
ds@1952
  1675
  int nSetP2 = 0;       /* Number of slots in aSetP2[] used */
ds@1952
  1676
ds@1952
  1677
  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
ds@1952
  1678
  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
ds@1952
  1679
  */
ds@1952
  1680
  if( p==0 || p->pPrior==0 ){
ds@1952
  1681
    rc = 1;
ds@1952
  1682
    goto multi_select_end;
ds@1952
  1683
  }
ds@1952
  1684
  pPrior = p->pPrior;
ds@1952
  1685
  assert( pPrior->pRightmost!=pPrior );
ds@1952
  1686
  assert( pPrior->pRightmost==p->pRightmost );
ds@1952
  1687
  if( pPrior->pOrderBy ){
ds@1952
  1688
    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
ds@1952
  1689
      selectOpName(p->op));
ds@1952
  1690
    rc = 1;
ds@1952
  1691
    goto multi_select_end;
ds@1952
  1692
  }
ds@1952
  1693
  if( pPrior->pLimit ){
ds@1952
  1694
    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
ds@1952
  1695
      selectOpName(p->op));
ds@1952
  1696
    rc = 1;
ds@1952
  1697
    goto multi_select_end;
ds@1952
  1698
  }
ds@1952
  1699
ds@1952
  1700
  /* Make sure we have a valid query engine.  If not, create a new one.
ds@1952
  1701
  */
ds@1952
  1702
  v = sqlite3GetVdbe(pParse);
ds@1952
  1703
  if( v==0 ){
ds@1952
  1704
    rc = 1;
ds@1952
  1705
    goto multi_select_end;
ds@1952
  1706
  }
ds@1952
  1707
ds@1952
  1708
  /* Create the destination temporary table if necessary
ds@1952
  1709
  */
ds@1952
  1710
  if( eDest==SRT_EphemTab ){
ds@1952
  1711
    assert( p->pEList );
ds@1952
  1712
    assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
ds@1952
  1713
    aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 0);
ds@1952
  1714
    eDest = SRT_Table;
ds@1952
  1715
  }
ds@1952
  1716
ds@1952
  1717
  /* Generate code for the left and right SELECT statements.
ds@1952
  1718
  */
ds@1952
  1719
  pOrderBy = p->pOrderBy;
ds@1952
  1720
  switch( p->op ){
ds@1952
  1721
    case TK_ALL: {
ds@1952
  1722
      if( pOrderBy==0 ){
ds@1952
  1723
        int addr = 0;
ds@1952
  1724
        assert( !pPrior->pLimit );
ds@1952
  1725
        pPrior->pLimit = p->pLimit;
ds@1952
  1726
        pPrior->pOffset = p->pOffset;
ds@1952
  1727
        rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);
ds@1952
  1728
        p->pLimit = 0;
ds@1952
  1729
        p->pOffset = 0;
ds@1952
  1730
        if( rc ){
ds@1952
  1731
          goto multi_select_end;
ds@1952
  1732
        }
ds@1952
  1733
        p->pPrior = 0;
ds@1952
  1734
        p->iLimit = pPrior->iLimit;
ds@1952
  1735
        p->iOffset = pPrior->iOffset;
ds@1952
  1736
        if( p->iLimit>=0 ){
ds@1952
  1737
          addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0);
ds@1952
  1738
          VdbeComment((v, "# Jump ahead if LIMIT reached"));
ds@1952
  1739
        }
ds@1952
  1740
        rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);
ds@1952
  1741
        p->pPrior = pPrior;
ds@1952
  1742
        if( rc ){
ds@1952
  1743
          goto multi_select_end;
ds@1952
  1744
        }
ds@1952
  1745
        if( addr ){
ds@1952
  1746
          sqlite3VdbeJumpHere(v, addr);
ds@1952
  1747
        }
ds@1952
  1748
        break;
ds@1952
  1749
      }
ds@1952
  1750
      /* For UNION ALL ... ORDER BY fall through to the next case */
ds@1952
  1751
    }
ds@1952
  1752
    case TK_EXCEPT:
ds@1952
  1753
    case TK_UNION: {
ds@1952
  1754
      int unionTab;    /* Cursor number of the temporary table holding result */
ds@1952
  1755
      int op = 0;      /* One of the SRT_ operations to apply to self */
ds@1952
  1756
      int priorOp;     /* The SRT_ operation to apply to prior selects */
ds@1952
  1757
      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
ds@1952
  1758
      int addr;
ds@1952
  1759
ds@1952
  1760
      priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
ds@1952
  1761
      if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){
ds@1952
  1762
        /* We can reuse a temporary table generated by a SELECT to our
ds@1952
  1763
        ** right.
ds@1952
  1764
        */
ds@1952
  1765
        unionTab = iParm;
ds@1952
  1766
      }else{
ds@1952
  1767
        /* We will need to create our own temporary table to hold the
ds@1952
  1768
        ** intermediate results.
ds@1952
  1769
        */
ds@1952
  1770
        unionTab = pParse->nTab++;
ds@1952
  1771
        if( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){
ds@1952
  1772
          rc = 1;
ds@1952
  1773
          goto multi_select_end;
ds@1952
  1774
        }
ds@1952
  1775
        addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0);
ds@1952
  1776
        if( priorOp==SRT_Table ){
ds@1952
  1777
          assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );
ds@1952
  1778
          aSetP2[nSetP2++] = addr;
ds@1952
  1779
        }else{
ds@1952
  1780
          assert( p->addrOpenEphm[0] == -1 );
ds@1952
  1781
          p->addrOpenEphm[0] = addr;
ds@1952
  1782
          p->pRightmost->usesEphm = 1;
ds@1952
  1783
        }
ds@1952
  1784
        createSortingIndex(pParse, p, pOrderBy);
ds@1952
  1785
        assert( p->pEList );
ds@1952
  1786
      }
ds@1952
  1787
ds@1952
  1788
      /* Code the SELECT statements to our left
ds@1952
  1789
      */
ds@1952
  1790
      assert( !pPrior->pOrderBy );
ds@1952
  1791
      rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff);
ds@1952
  1792
      if( rc ){
ds@1952
  1793
        goto multi_select_end;
ds@1952
  1794
      }
ds@1952
  1795
ds@1952
  1796
      /* Code the current SELECT statement
ds@1952
  1797
      */
ds@1952
  1798
      switch( p->op ){
ds@1952
  1799
         case TK_EXCEPT:  op = SRT_Except;   break;
ds@1952
  1800
         case TK_UNION:   op = SRT_Union;    break;
ds@1952
  1801
         case TK_ALL:     op = SRT_Table;    break;
ds@1952
  1802
      }
ds@1952
  1803
      p->pPrior = 0;
ds@1952
  1804
      p->pOrderBy = 0;
ds@1952
  1805
      p->disallowOrderBy = pOrderBy!=0;
ds@1952
  1806
      pLimit = p->pLimit;
ds@1952
  1807
      p->pLimit = 0;
ds@1952
  1808
      pOffset = p->pOffset;
ds@1952
  1809
      p->pOffset = 0;
ds@1952
  1810
      rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);
ds@1952
  1811
      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
ds@1952
  1812
      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
ds@1952
  1813
      sqlite3ExprListDelete(p->pOrderBy);
ds@1952
  1814
      p->pPrior = pPrior;
ds@1952
  1815
      p->pOrderBy = pOrderBy;
ds@1952
  1816
      sqlite3ExprDelete(p->pLimit);
ds@1952
  1817
      p->pLimit = pLimit;
ds@1952
  1818
      p->pOffset = pOffset;
ds@1952
  1819
      p->iLimit = -1;
ds@1952
  1820
      p->iOffset = -1;
ds@1952
  1821
      if( rc ){
ds@1952
  1822
        goto multi_select_end;
ds@1952
  1823
      }
ds@1952
  1824
ds@1952
  1825
ds@1952
  1826
      /* Convert the data in the temporary table into whatever form
ds@1952
  1827
      ** it is that we currently need.
ds@1952
  1828
      */      
ds@1952
  1829
      if( eDest!=priorOp || unionTab!=iParm ){
ds@1952
  1830
        int iCont, iBreak, iStart;
ds@1952
  1831
        assert( p->pEList );
ds@1952
  1832
        if( eDest==SRT_Callback ){
ds@1952
  1833
          Select *pFirst = p;
ds@1952
  1834
          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
ds@1952
  1835
          generateColumnNames(pParse, 0, pFirst->pEList);
ds@1952
  1836
        }
ds@1952
  1837
        iBreak = sqlite3VdbeMakeLabel(v);
ds@1952
  1838
        iCont = sqlite3VdbeMakeLabel(v);
ds@1952
  1839
        computeLimitRegisters(pParse, p, iBreak);
ds@1952
  1840
        sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);
ds@1952
  1841
        iStart = sqlite3VdbeCurrentAddr(v);
ds@1952
  1842
        rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
ds@1952
  1843
                             pOrderBy, -1, eDest, iParm, 
ds@1952
  1844
                             iCont, iBreak, 0);
ds@1952
  1845
        if( rc ){
ds@1952
  1846
          rc = 1;
ds@1952
  1847
          goto multi_select_end;
ds@1952
  1848
        }
ds@1952
  1849
        sqlite3VdbeResolveLabel(v, iCont);
ds@1952
  1850
        sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);
ds@1952
  1851
        sqlite3VdbeResolveLabel(v, iBreak);
ds@1952
  1852
        sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);
ds@1952
  1853
      }
ds@1952
  1854
      break;
ds@1952
  1855
    }
ds@1952
  1856
    case TK_INTERSECT: {
ds@1952
  1857
      int tab1, tab2;
ds@1952
  1858
      int iCont, iBreak, iStart;
ds@1952
  1859
      Expr *pLimit, *pOffset;
ds@1952
  1860
      int addr;
ds@1952
  1861
ds@1952
  1862
      /* INTERSECT is different from the others since it requires
ds@1952
  1863
      ** two temporary tables.  Hence it has its own case.  Begin
ds@1952
  1864
      ** by allocating the tables we will need.
ds@1952
  1865
      */
ds@1952
  1866
      tab1 = pParse->nTab++;
ds@1952
  1867
      tab2 = pParse->nTab++;
ds@1952
  1868
      if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){
ds@1952
  1869
        rc = 1;
ds@1952
  1870
        goto multi_select_end;
ds@1952
  1871
      }
ds@1952
  1872
      createSortingIndex(pParse, p, pOrderBy);
ds@1952
  1873
ds@1952
  1874
      addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0);
ds@1952
  1875
      assert( p->addrOpenEphm[0] == -1 );
ds@1952
  1876
      p->addrOpenEphm[0] = addr;
ds@1952
  1877
      p->pRightmost->usesEphm = 1;
ds@1952
  1878
      assert( p->pEList );
ds@1952
  1879
ds@1952
  1880
      /* Code the SELECTs to our left into temporary table "tab1".
ds@1952
  1881
      */
ds@1952
  1882
      rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff);
ds@1952
  1883
      if( rc ){
ds@1952
  1884
        goto multi_select_end;
ds@1952
  1885
      }
ds@1952
  1886
ds@1952
  1887
      /* Code the current SELECT into temporary table "tab2"
ds@1952
  1888
      */
ds@1952
  1889
      addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0);
ds@1952
  1890
      assert( p->addrOpenEphm[1] == -1 );
ds@1952
  1891
      p->addrOpenEphm[1] = addr;
ds@1952
  1892
      p->pPrior = 0;
ds@1952
  1893
      pLimit = p->pLimit;
ds@1952
  1894
      p->pLimit = 0;
ds@1952
  1895
      pOffset = p->pOffset;
ds@1952
  1896
      p->pOffset = 0;
ds@1952
  1897
      rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff);
ds@1952
  1898
      p->pPrior = pPrior;
ds@1952
  1899
      sqlite3ExprDelete(p->pLimit);
ds@1952
  1900
      p->pLimit = pLimit;
ds@1952
  1901
      p->pOffset = pOffset;
ds@1952
  1902
      if( rc ){
ds@1952
  1903
        goto multi_select_end;
ds@1952
  1904
      }
ds@1952
  1905
ds@1952
  1906
      /* Generate code to take the intersection of the two temporary
ds@1952
  1907
      ** tables.
ds@1952
  1908
      */
ds@1952
  1909
      assert( p->pEList );
ds@1952
  1910
      if( eDest==SRT_Callback ){
ds@1952
  1911
        Select *pFirst = p;
ds@1952
  1912
        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
ds@1952
  1913
        generateColumnNames(pParse, 0, pFirst->pEList);
ds@1952
  1914
      }
ds@1952
  1915
      iBreak = sqlite3VdbeMakeLabel(v);
ds@1952
  1916
      iCont = sqlite3VdbeMakeLabel(v);
ds@1952
  1917
      computeLimitRegisters(pParse, p, iBreak);
ds@1952
  1918
      sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);
ds@1952
  1919
      iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0);
ds@1952
  1920
      sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);
ds@1952
  1921
      rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
ds@1952
  1922
                             pOrderBy, -1, eDest, iParm, 
ds@1952
  1923
                             iCont, iBreak, 0);
ds@1952
  1924
      if( rc ){
ds@1952
  1925
        rc = 1;
ds@1952
  1926
        goto multi_select_end;
ds@1952
  1927
      }
ds@1952
  1928
      sqlite3VdbeResolveLabel(v, iCont);
ds@1952
  1929
      sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);
ds@1952
  1930
      sqlite3VdbeResolveLabel(v, iBreak);
ds@1952
  1931
      sqlite3VdbeAddOp(v, OP_Close, tab2, 0);
ds@1952
  1932
      sqlite3VdbeAddOp(v, OP_Close, tab1, 0);
ds@1952
  1933
      break;
ds@1952
  1934
    }
ds@1952
  1935
  }
ds@1952
  1936
ds@1952
  1937
  /* Make sure all SELECTs in the statement have the same number of elements
ds@1952
  1938
  ** in their result sets.
ds@1952
  1939
  */
ds@1952
  1940
  assert( p->pEList && pPrior->pEList );
ds@1952
  1941
  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
ds@1952
  1942
    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
ds@1952
  1943
      " do not have the same number of result columns", selectOpName(p->op));
ds@1952
  1944
    rc = 1;
ds@1952
  1945
    goto multi_select_end;
ds@1952
  1946
  }
ds@1952
  1947
ds@1952
  1948
  /* Set the number of columns in temporary tables
ds@1952
  1949
  */
ds@1952
  1950
  nCol = p->pEList->nExpr;
ds@1952
  1951
  while( nSetP2 ){
ds@1952
  1952
    sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol);
ds@1952
  1953
  }
ds@1952
  1954
ds@1952
  1955
  /* Compute collating sequences used by either the ORDER BY clause or
ds@1952
  1956
  ** by any temporary tables needed to implement the compound select.
ds@1952
  1957
  ** Attach the KeyInfo structure to all temporary tables.  Invoke the
ds@1952
  1958
  ** ORDER BY processing if there is an ORDER BY clause.
ds@1952
  1959
  **
ds@1952
  1960
  ** This section is run by the right-most SELECT statement only.
ds@1952
  1961
  ** SELECT statements to the left always skip this part.  The right-most
ds@1952
  1962
  ** SELECT might also skip this part if it has no ORDER BY clause and
ds@1952
  1963
  ** no temp tables are required.
ds@1952
  1964
  */
ds@1952
  1965
  if( pOrderBy || p->usesEphm ){
ds@1952
  1966
    int i;                        /* Loop counter */
ds@1952
  1967
    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
ds@1952
  1968
    Select *pLoop;                /* For looping through SELECT statements */
ds@1952
  1969
    int nKeyCol;                  /* Number of entries in pKeyInfo->aCol[] */
ds@1952
  1970
    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
ds@1952
  1971
    CollSeq **aCopy;              /* A copy of pKeyInfo->aColl[] */
ds@1952
  1972
ds@1952
  1973
    assert( p->pRightmost==p );
ds@1952
  1974
    nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0);
ds@1952
  1975
    pKeyInfo = sqlite3DbMallocZero(pParse->db,
ds@1952
  1976
                       sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));
ds@1952
  1977
    if( !pKeyInfo ){
ds@1952
  1978
      rc = SQLITE_NOMEM;
ds@1952
  1979
      goto multi_select_end;
ds@1952
  1980
    }
ds@1952
  1981
ds@1952
  1982
    pKeyInfo->enc = ENC(pParse->db);
ds@1952
  1983
    pKeyInfo->nField = nCol;
ds@1952
  1984
ds@1952
  1985
    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
ds@1952
  1986
      *apColl = multiSelectCollSeq(pParse, p, i);
ds@1952
  1987
      if( 0==*apColl ){
ds@1952
  1988
        *apColl = pParse->db->pDfltColl;
ds@1952
  1989
      }
ds@1952
  1990
    }
ds@1952
  1991
ds@1952
  1992
    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
ds@1952
  1993
      for(i=0; i<2; i++){
ds@1952
  1994
        int addr = pLoop->addrOpenEphm[i];
ds@1952
  1995
        if( addr<0 ){
ds@1952
  1996
          /* If [0] is unused then [1] is also unused.  So we can
ds@1952
  1997
          ** always safely abort as soon as the first unused slot is found */
ds@1952
  1998
          assert( pLoop->addrOpenEphm[1]<0 );
ds@1952
  1999
          break;
ds@1952
  2000
        }
ds@1952
  2001
        sqlite3VdbeChangeP2(v, addr, nCol);
ds@1952
  2002
        sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO);
ds@1952
  2003
        pLoop->addrOpenEphm[i] = -1;
ds@1952
  2004
      }
ds@1952
  2005
    }
ds@1952
  2006
ds@1952
  2007
    if( pOrderBy ){
ds@1952
  2008
      struct ExprList_item *pOTerm = pOrderBy->a;
ds@1952
  2009
      int nOrderByExpr = pOrderBy->nExpr;
ds@1952
  2010
      int addr;
ds@1952
  2011
      u8 *pSortOrder;
ds@1952
  2012
ds@1952
  2013
      /* Reuse the same pKeyInfo for the ORDER BY as was used above for
ds@1952
  2014
      ** the compound select statements.  Except we have to change out the
ds@1952
  2015
      ** pKeyInfo->aColl[] values.  Some of the aColl[] values will be
ds@1952
  2016
      ** reused when constructing the pKeyInfo for the ORDER BY, so make
ds@1952
  2017
      ** a copy.  Sufficient space to hold both the nCol entries for
ds@1952
  2018
      ** the compound select and the nOrderbyExpr entries for the ORDER BY
ds@1952
  2019
      ** was allocated above.  But we need to move the compound select
ds@1952
  2020
      ** entries out of the way before constructing the ORDER BY entries.
ds@1952
  2021
      ** Move the compound select entries into aCopy[] where they can be
ds@1952
  2022
      ** accessed and reused when constructing the ORDER BY entries.
ds@1952
  2023
      ** Because nCol might be greater than or less than nOrderByExpr
ds@1952
  2024
      ** we have to use memmove() when doing the copy.
ds@1952
  2025
      */
ds@1952
  2026
      aCopy = &pKeyInfo->aColl[nOrderByExpr];
ds@1952
  2027
      pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol];
ds@1952
  2028
      memmove(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*));
ds@1952
  2029
ds@1952
  2030
      apColl = pKeyInfo->aColl;
ds@1952
  2031
      for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){
ds@1952
  2032
        Expr *pExpr = pOTerm->pExpr;
ds@1952
  2033
        if( (pExpr->flags & EP_ExpCollate) ){
ds@1952
  2034
          assert( pExpr->pColl!=0 );
ds@1952
  2035
          *apColl = pExpr->pColl;
ds@1952
  2036
        }else{
ds@1952
  2037
          *apColl = aCopy[pExpr->iColumn];
ds@1952
  2038
        }
ds@1952
  2039
        *pSortOrder = pOTerm->sortOrder;
ds@1952
  2040
      }
ds@1952
  2041
      assert( p->pRightmost==p );
ds@1952
  2042
      assert( p->addrOpenEphm[2]>=0 );
ds@1952
  2043
      addr = p->addrOpenEphm[2];
ds@1952
  2044
      sqlite3VdbeChangeP2(v, addr, p->pOrderBy->nExpr+2);
ds@1952
  2045
      pKeyInfo->nField = nOrderByExpr;
ds@1952
  2046
      sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
ds@1952
  2047
      pKeyInfo = 0;
ds@1952
  2048
      generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
ds@1952
  2049
    }
ds@1952
  2050
ds@1952
  2051
    sqlite3_free(pKeyInfo);
ds@1952
  2052
  }
ds@1952
  2053
ds@1952
  2054
multi_select_end:
ds@1952
  2055
  return rc;
ds@1952
  2056
}
ds@1952
  2057
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
ds@1952
  2058
ds@1952
  2059
#ifndef SQLITE_OMIT_VIEW
ds@1952
  2060
/* Forward Declarations */
ds@1952
  2061
static void substExprList(sqlite3*, ExprList*, int, ExprList*);
ds@1952
  2062
static void substSelect(sqlite3*, Select *, int, ExprList *);
ds@1952
  2063
ds@1952
  2064
/*
ds@1952
  2065
** Scan through the expression pExpr.  Replace every reference to
ds@1952
  2066
** a column in table number iTable with a copy of the iColumn-th
ds@1952
  2067
** entry in pEList.  (But leave references to the ROWID column 
ds@1952
  2068
** unchanged.)
ds@1952
  2069
**
ds@1952
  2070
** This routine is part of the flattening procedure.  A subquery
ds@1952
  2071
** whose result set is defined by pEList appears as entry in the
ds@1952
  2072
** FROM clause of a SELECT such that the VDBE cursor assigned to that
ds@1952
  2073
** FORM clause entry is iTable.  This routine make the necessary 
ds@1952
  2074
** changes to pExpr so that it refers directly to the source table
ds@1952
  2075
** of the subquery rather the result set of the subquery.
ds@1952
  2076
*/
ds@1952
  2077
static void substExpr(
ds@1952
  2078
  sqlite3 *db,        /* Report malloc errors to this connection */
ds@1952
  2079
  Expr *pExpr,        /* Expr in which substitution occurs */
ds@1952
  2080
  int iTable,         /* Table to be substituted */
ds@1952
  2081
  ExprList *pEList    /* Substitute expressions */
ds@1952
  2082
){
ds@1952
  2083
  if( pExpr==0 ) return;
ds@1952
  2084
  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
ds@1952
  2085
    if( pExpr->iColumn<0 ){
ds@1952
  2086
      pExpr->op = TK_NULL;
ds@1952
  2087
    }else{
ds@1952
  2088
      Expr *pNew;
ds@1952
  2089
      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
ds@1952
  2090
      assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
ds@1952
  2091
      pNew = pEList->a[pExpr->iColumn].pExpr;
ds@1952
  2092
      assert( pNew!=0 );
ds@1952
  2093
      pExpr->op = pNew->op;
ds@1952
  2094
      assert( pExpr->pLeft==0 );
ds@1952
  2095
      pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft);
ds@1952
  2096
      assert( pExpr->pRight==0 );
ds@1952
  2097
      pExpr->pRight = sqlite3ExprDup(db, pNew->pRight);
ds@1952
  2098
      assert( pExpr->pList==0 );
ds@1952
  2099
      pExpr->pList = sqlite3ExprListDup(db, pNew->pList);
ds@1952
  2100
      pExpr->iTable = pNew->iTable;
ds@1952
  2101
      pExpr->pTab = pNew->pTab;
ds@1952
  2102
      pExpr->iColumn = pNew->iColumn;
ds@1952
  2103
      pExpr->iAgg = pNew->iAgg;
ds@1952
  2104
      sqlite3TokenCopy(db, &pExpr->token, &pNew->token);
ds@1952
  2105
      sqlite3TokenCopy(db, &pExpr->span, &pNew->span);
ds@1952
  2106
      pExpr->pSelect = sqlite3SelectDup(db, pNew->pSelect);
ds@1952
  2107
      pExpr->flags = pNew->flags;
ds@1952
  2108
    }
ds@1952
  2109
  }else{
ds@1952
  2110
    substExpr(db, pExpr->pLeft, iTable, pEList);
ds@1952
  2111
    substExpr(db, pExpr->pRight, iTable, pEList);
ds@1952
  2112
    substSelect(db, pExpr->pSelect, iTable, pEList);
ds@1952
  2113
    substExprList(db, pExpr->pList, iTable, pEList);
ds@1952
  2114
  }
ds@1952
  2115
}
ds@1952
  2116
static void substExprList(
ds@1952
  2117
  sqlite3 *db,         /* Report malloc errors here */
ds@1952
  2118
  ExprList *pList,     /* List to scan and in which to make substitutes */
ds@1952
  2119
  int iTable,          /* Table to be substituted */
ds@1952
  2120
  ExprList *pEList     /* Substitute values */
ds@1952
  2121
){
ds@1952
  2122
  int i;
ds@1952
  2123
  if( pList==0 ) return;
ds@1952
  2124
  for(i=0; i<pList->nExpr; i++){
ds@1952
  2125
    substExpr(db, pList->a[i].pExpr, iTable, pEList);
ds@1952
  2126
  }
ds@1952
  2127
}
ds@1952
  2128
static void substSelect(
ds@1952
  2129
  sqlite3 *db,         /* Report malloc errors here */
ds@1952
  2130
  Select *p,           /* SELECT statement in which to make substitutions */
ds@1952
  2131
  int iTable,          /* Table to be replaced */
ds@1952
  2132
  ExprList *pEList     /* Substitute values */
ds@1952
  2133
){
ds@1952
  2134
  if( !p ) return;
ds@1952
  2135
  substExprList(db, p->pEList, iTable, pEList);
ds@1952
  2136
  substExprList(db, p->pGroupBy, iTable, pEList);
ds@1952
  2137
  substExprList(db, p->pOrderBy, iTable, pEList);
ds@1952
  2138
  substExpr(db, p->pHaving, iTable, pEList);
ds@1952
  2139
  substExpr(db, p->pWhere, iTable, pEList);
ds@1952
  2140
  substSelect(db, p->pPrior, iTable, pEList);
ds@1952
  2141
}
ds@1952
  2142
#endif /* !defined(SQLITE_OMIT_VIEW) */
ds@1952
  2143
ds@1952
  2144
#ifndef SQLITE_OMIT_VIEW
ds@1952
  2145
/*
ds@1952
  2146
** This routine attempts to flatten subqueries in order to speed
ds@1952
  2147
** execution.  It returns 1 if it makes changes and 0 if no flattening
ds@1952
  2148
** occurs.
ds@1952
  2149
**
ds@1952
  2150
** To understand the concept of flattening, consider the following
ds@1952
  2151
** query:
ds@1952
  2152
**
ds@1952
  2153
**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
ds@1952
  2154
**
ds@1952
  2155
** The default way of implementing this query is to execute the
ds@1952
  2156
** subquery first and store the results in a temporary table, then
ds@1952
  2157
** run the outer query on that temporary table.  This requires two
ds@1952
  2158
** passes over the data.  Furthermore, because the temporary table
ds@1952
  2159
** has no indices, the WHERE clause on the outer query cannot be
ds@1952
  2160
** optimized.
ds@1952
  2161
**
ds@1952
  2162
** This routine attempts to rewrite queries such as the above into
ds@1952
  2163
** a single flat select, like this:
ds@1952
  2164
**
ds@1952
  2165
**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
ds@1952
  2166
**
ds@1952
  2167
** The code generated for this simpification gives the same result
ds@1952
  2168
** but only has to scan the data once.  And because indices might 
ds@1952
  2169
** exist on the table t1, a complete scan of the data might be
ds@1952
  2170
** avoided.
ds@1952
  2171
**
ds@1952
  2172
** Flattening is only attempted if all of the following are true:
ds@1952
  2173
**
ds@1952
  2174
**   (1)  The subquery and the outer query do not both use aggregates.
ds@1952
  2175
**
ds@1952
  2176
**   (2)  The subquery is not an aggregate or the outer query is not a join.
ds@1952
  2177
**
ds@1952
  2178
**   (3)  The subquery is not the right operand of a left outer join, or
ds@1952
  2179
**        the subquery is not itself a join.  (Ticket #306)
ds@1952
  2180
**
ds@1952
  2181
**   (4)  The subquery is not DISTINCT or the outer query is not a join.
ds@1952
  2182
**
ds@1952
  2183
**   (5)  The subquery is not DISTINCT or the outer query does not use
ds@1952
  2184
**        aggregates.
ds@1952
  2185
**
ds@1952
  2186
**   (6)  The subquery does not use aggregates or the outer query is not
ds@1952
  2187
**        DISTINCT.
ds@1952
  2188
**
ds@1952
  2189
**   (7)  The subquery has a FROM clause.
ds@1952
  2190
**
ds@1952
  2191
**   (8)  The subquery does not use LIMIT or the outer query is not a join.
ds@1952
  2192
**
ds@1952
  2193
**   (9)  The subquery does not use LIMIT or the outer query does not use
ds@1952
  2194
**        aggregates.
ds@1952
  2195
**
ds@1952
  2196
**  (10)  The subquery does not use aggregates or the outer query does not
ds@1952
  2197
**        use LIMIT.
ds@1952
  2198
**
ds@1952
  2199
**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
ds@1952
  2200
**
ds@1952
  2201
**  (12)  The subquery is not the right term of a LEFT OUTER JOIN or the
ds@1952
  2202
**        subquery has no WHERE clause.  (added by ticket #350)
ds@1952
  2203
**
ds@1952
  2204
**  (13)  The subquery and outer query do not both use LIMIT
ds@1952
  2205
**
ds@1952
  2206
**  (14)  The subquery does not use OFFSET
ds@1952
  2207
**
ds@1952
  2208
**  (15)  The outer query is not part of a compound select or the
ds@1952
  2209
**        subquery does not have both an ORDER BY and a LIMIT clause.
ds@1952
  2210
**        (See ticket #2339)
ds@1952
  2211
**
ds@1952
  2212
** In this routine, the "p" parameter is a pointer to the outer query.
ds@1952
  2213
** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
ds@1952
  2214
** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
ds@1952
  2215
**
ds@1952
  2216
** If flattening is not attempted, this routine is a no-op and returns 0.
ds@1952
  2217
** If flattening is attempted this routine returns 1.
ds@1952
  2218
**
ds@1952
  2219
** All of the expression analysis must occur on both the outer query and
ds@1952
  2220
** the subquery before this routine runs.
ds@1952
  2221
*/
ds@1952
  2222
static int flattenSubquery(
ds@1952
  2223
  sqlite3 *db,         /* Database connection */
ds@1952
  2224
  Select *p,           /* The parent or outer SELECT statement */
ds@1952
  2225
  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
ds@1952
  2226
  int isAgg,           /* True if outer SELECT uses aggregate functions */
ds@1952
  2227
  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
ds@1952
  2228
){
ds@1952
  2229
  Select *pSub;       /* The inner query or "subquery" */
ds@1952
  2230
  SrcList *pSrc;      /* The FROM clause of the outer query */
ds@1952
  2231
  SrcList *pSubSrc;   /* The FROM clause of the subquery */
ds@1952
  2232
  ExprList *pList;    /* The result set of the outer query */
ds@1952
  2233
  int iParent;        /* VDBE cursor number of the pSub result set temp table */
ds@1952
  2234
  int i;              /* Loop counter */
ds@1952
  2235
  Expr *pWhere;                    /* The WHERE clause */
ds@1952
  2236
  struct SrcList_item *pSubitem;   /* The subquery */
ds@1952
  2237
ds@1952
  2238
  /* Check to see if flattening is permitted.  Return 0 if not.
ds@1952
  2239
  */
ds@1952
  2240
  if( p==0 ) return 0;
ds@1952
  2241
  pSrc = p->pSrc;
ds@1952
  2242
  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
ds@1952
  2243
  pSubitem = &pSrc->a[iFrom];
ds@1952
  2244
  pSub = pSubitem->pSelect;
ds@1952
  2245
  assert( pSub!=0 );
ds@1952
  2246
  if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
ds@1952
  2247
  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
ds@1952
  2248
  pSubSrc = pSub->pSrc;
ds@1952
  2249
  assert( pSubSrc );
ds@1952
  2250
  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
ds@1952
  2251
  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
ds@1952
  2252
  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
ds@1952
  2253
  ** became arbitrary expressions, we were forced to add restrictions (13)
ds@1952
  2254
  ** and (14). */
ds@1952
  2255
  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
ds@1952
  2256
  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
ds@1952
  2257
  if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
ds@1952
  2258
    return 0;                                            /* Restriction (15) */
ds@1952
  2259
  }
ds@1952
  2260
  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
ds@1952
  2261
  if( (pSub->isDistinct || pSub->pLimit) 
ds@1952
  2262
         && (pSrc->nSrc>1 || isAgg) ){          /* Restrictions (4)(5)(8)(9) */
ds@1952
  2263
     return 0;       
ds@1952
  2264
  }
ds@1952
  2265
  if( p->isDistinct && subqueryIsAgg ) return 0;         /* Restriction (6)  */
ds@1952
  2266
  if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){
ds@1952
  2267
     return 0;                                           /* Restriction (11) */
ds@1952
  2268
  }
ds@1952
  2269
ds@1952
  2270
  /* Restriction 3:  If the subquery is a join, make sure the subquery is 
ds@1952
  2271
  ** not used as the right operand of an outer join.  Examples of why this
ds@1952
  2272
  ** is not allowed:
ds@1952
  2273
  **
ds@1952
  2274
  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
ds@1952
  2275
  **
ds@1952
  2276
  ** If we flatten the above, we would get
ds@1952
  2277
  **
ds@1952
  2278
  **         (t1 LEFT OUTER JOIN t2) JOIN t3
ds@1952
  2279
  **
ds@1952
  2280
  ** which is not at all the same thing.
ds@1952
  2281
  */
ds@1952
  2282
  if( pSubSrc->nSrc>1 && (pSubitem->jointype & JT_OUTER)!=0 ){
ds@1952
  2283
    return 0;
ds@1952
  2284
  }
ds@1952
  2285
ds@1952
  2286
  /* Restriction 12:  If the subquery is the right operand of a left outer
ds@1952
  2287
  ** join, make sure the subquery has no WHERE clause.
ds@1952
  2288
  ** An examples of why this is not allowed:
ds@1952
  2289
  **
ds@1952
  2290
  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
ds@1952
  2291
  **
ds@1952
  2292
  ** If we flatten the above, we would get
ds@1952
  2293
  **
ds@1952
  2294
  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
ds@1952
  2295
  **
ds@1952
  2296
  ** But the t2.x>0 test will always fail on a NULL row of t2, which
ds@1952
  2297
  ** effectively converts the OUTER JOIN into an INNER JOIN.
ds@1952
  2298
  */
ds@1952
  2299
  if( (pSubitem->jointype & JT_OUTER)!=0 && pSub->pWhere!=0 ){
ds@1952
  2300
    return 0;
ds@1952
  2301
  }
ds@1952
  2302
ds@1952
  2303
  /* If we reach this point, it means flattening is permitted for the
ds@1952
  2304
  ** iFrom-th entry of the FROM clause in the outer query.
ds@1952
  2305
  */
ds@1952
  2306
ds@1952
  2307
  /* Move all of the FROM elements of the subquery into the
ds@1952
  2308
  ** the FROM clause of the outer query.  Before doing this, remember
ds@1952
  2309
  ** the cursor number for the original outer query FROM element in
ds@1952
  2310
  ** iParent.  The iParent cursor will never be used.  Subsequent code
ds@1952
  2311
  ** will scan expressions looking for iParent references and replace
ds@1952
  2312
  ** those references with expressions that resolve to the subquery FROM
ds@1952
  2313
  ** elements we are now copying in.
ds@1952
  2314
  */
ds@1952
  2315
  iParent = pSubitem->iCursor;
ds@1952
  2316
  {
ds@1952
  2317
    int nSubSrc = pSubSrc->nSrc;
ds@1952
  2318
    int jointype = pSubitem->jointype;
ds@1952
  2319
ds@1952
  2320
    sqlite3DeleteTable(pSubitem->pTab);
ds@1952
  2321
    sqlite3_free(pSubitem->zDatabase);
ds@1952
  2322
    sqlite3_free(pSubitem->zName);
ds@1952
  2323
    sqlite3_free(pSubitem->zAlias);
ds@1952
  2324
    if( nSubSrc>1 ){
ds@1952
  2325
      int extra = nSubSrc - 1;
ds@1952
  2326
      for(i=1; i<nSubSrc; i++){
ds@1952
  2327
        pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
ds@1952
  2328
      }
ds@1952
  2329
      p->pSrc = pSrc;
ds@1952
  2330
      for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
ds@1952
  2331
        pSrc->a[i] = pSrc->a[i-extra];
ds@1952
  2332
      }
ds@1952
  2333
    }
ds@1952
  2334
    for(i=0; i<nSubSrc; i++){
ds@1952
  2335
      pSrc->a[i+iFrom] = pSubSrc->a[i];
ds@1952
  2336
      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
ds@1952
  2337
    }
ds@1952
  2338
    pSrc->a[iFrom].jointype = jointype;
ds@1952
  2339
  }
ds@1952
  2340
ds@1952
  2341
  /* Now begin substituting subquery result set expressions for 
ds@1952
  2342
  ** references to the iParent in the outer query.
ds@1952
  2343
  ** 
ds@1952
  2344
  ** Example:
ds@1952
  2345
  **
ds@1952
  2346
  **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
ds@1952
  2347
  **   \                     \_____________ subquery __________/          /
ds@1952
  2348
  **    \_____________________ outer query ______________________________/
ds@1952
  2349
  **
ds@1952
  2350
  ** We look at every expression in the outer query and every place we see
ds@1952
  2351
  ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
ds@1952
  2352
  */
ds@1952
  2353
  pList = p->pEList;
ds@1952
  2354
  for(i=0; i<pList->nExpr; i++){
ds@1952
  2355
    Expr *pExpr;
ds@1952
  2356
    if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
ds@1952
  2357
      pList->a[i].zName = 
ds@1952
  2358
             sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n);
ds@1952
  2359
    }
ds@1952
  2360
  }
ds@1952
  2361
  substExprList(db, p->pEList, iParent, pSub->pEList);
ds@1952
  2362
  if( isAgg ){
ds@1952
  2363
    substExprList(db, p->pGroupBy, iParent, pSub->pEList);
ds@1952
  2364
    substExpr(db, p->pHaving, iParent, pSub->pEList);
ds@1952
  2365
  }
ds@1952
  2366
  if( pSub->pOrderBy ){
ds@1952
  2367
    assert( p->pOrderBy==0 );
ds@1952
  2368
    p->pOrderBy = pSub->pOrderBy;
ds@1952
  2369
    pSub->pOrderBy = 0;
ds@1952
  2370
  }else if( p->pOrderBy ){
ds@1952
  2371
    substExprList(db, p->pOrderBy, iParent, pSub->pEList);
ds@1952
  2372
  }
ds@1952
  2373
  if( pSub->pWhere ){
ds@1952
  2374
    pWhere = sqlite3ExprDup(db, pSub->pWhere);
ds@1952
  2375
  }else{
ds@1952
  2376
    pWhere = 0;
ds@1952
  2377
  }
ds@1952
  2378
  if( subqueryIsAgg ){
ds@1952
  2379
    assert( p->pHaving==0 );
ds@1952
  2380
    p->pHaving = p->pWhere;
ds@1952
  2381
    p->pWhere = pWhere;
ds@1952
  2382
    substExpr(db, p->pHaving, iParent, pSub->pEList);
ds@1952
  2383
    p->pHaving = sqlite3ExprAnd(db, p->pHaving, 
ds@1952
  2384
                                sqlite3ExprDup(db, pSub->pHaving));
ds@1952
  2385
    assert( p->pGroupBy==0 );
ds@1952
  2386
    p->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy);
ds@1952
  2387
  }else{
ds@1952
  2388
    substExpr(db, p->pWhere, iParent, pSub->pEList);
ds@1952
  2389
    p->pWhere = sqlite3ExprAnd(db, p->pWhere, pWhere);
ds@1952
  2390
  }
ds@1952
  2391
ds@1952
  2392
  /* The flattened query is distinct if either the inner or the
ds@1952
  2393
  ** outer query is distinct. 
ds@1952
  2394
  */
ds@1952
  2395
  p->isDistinct = p->isDistinct || pSub->isDistinct;
ds@1952
  2396
ds@1952
  2397
  /*
ds@1952
  2398
  ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
ds@1952
  2399
  **
ds@1952
  2400
  ** One is tempted to try to add a and b to combine the limits.  But this
ds@1952
  2401
  ** does not work if either limit is negative.
ds@1952
  2402
  */
ds@1952
  2403
  if( pSub->pLimit ){
ds@1952
  2404
    p->pLimit = pSub->pLimit;
ds@1952
  2405
    pSub->pLimit = 0;
ds@1952
  2406
  }
ds@1952
  2407
ds@1952
  2408
  /* Finially, delete what is left of the subquery and return
ds@1952
  2409
  ** success.
ds@1952
  2410
  */
ds@1952
  2411
  sqlite3SelectDelete(pSub);
ds@1952
  2412
  return 1;
ds@1952
  2413
}
ds@1952
  2414
#endif /* SQLITE_OMIT_VIEW */
ds@1952
  2415
ds@1952
  2416
/*
ds@1952
  2417
** Analyze the SELECT statement passed in as an argument to see if it
ds@1952
  2418
** is a simple min() or max() query.  If it is and this query can be
ds@1952
  2419
** satisfied using a single seek to the beginning or end of an index,
ds@1952
  2420
** then generate the code for this SELECT and return 1.  If this is not a 
ds@1952
  2421
** simple min() or max() query, then return 0;
ds@1952
  2422
**
ds@1952
  2423
** A simply min() or max() query looks like this:
ds@1952
  2424
**
ds@1952
  2425
**    SELECT min(a) FROM table;
ds@1952
  2426
**    SELECT max(a) FROM table;
ds@1952
  2427
**
ds@1952
  2428
** The query may have only a single table in its FROM argument.  There
ds@1952
  2429
** can be no GROUP BY or HAVING or WHERE clauses.  The result set must
ds@1952
  2430
** be the min() or max() of a single column of the table.  The column
ds@1952
  2431
** in the min() or max() function must be indexed.
ds@1952
  2432
**
ds@1952
  2433
** The parameters to this routine are the same as for sqlite3Select().
ds@1952
  2434
** See the header comment on that routine for additional information.
ds@1952
  2435
*/
ds@1952
  2436
static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
ds@1952
  2437
  Expr *pExpr;
ds@1952
  2438
  int iCol;
ds@1952
  2439
  Table *pTab;
ds@1952
  2440
  Index *pIdx;
ds@1952
  2441
  int base;
ds@1952
  2442
  Vdbe *v;
ds@1952
  2443
  int seekOp;
ds@1952
  2444
  ExprList *pEList, *pList, eList;
ds@1952
  2445
  struct ExprList_item eListItem;
ds@1952
  2446
  SrcList *pSrc;
ds@1952
  2447
  int brk;
ds@1952
  2448
  int iDb;
ds@1952
  2449
ds@1952
  2450
  /* Check to see if this query is a simple min() or max() query.  Return
ds@1952
  2451
  ** zero if it is  not.
ds@1952
  2452
  */
ds@1952
  2453
  if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
ds@1952
  2454
  pSrc = p->pSrc;
ds@1952
  2455
  if( pSrc->nSrc!=1 ) return 0;
ds@1952
  2456
  pEList = p->pEList;
ds@1952
  2457
  if( pEList->nExpr!=1 ) return 0;
ds@1952
  2458
  pExpr = pEList->a[0].pExpr;
ds@1952
  2459
  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
ds@1952
  2460
  pList = pExpr->pList;
ds@1952
  2461
  if( pList==0 || pList->nExpr!=1 ) return 0;
ds@1952
  2462
  if( pExpr->token.n!=3 ) return 0;
ds@1952
  2463
  if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
ds@1952
  2464
    seekOp = OP_Rewind;
ds@1952
  2465
  }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
ds@1952
  2466
    seekOp = OP_Last;
ds@1952
  2467
  }else{
ds@1952
  2468
    return 0;
ds@1952
  2469
  }
ds@1952
  2470
  pExpr = pList->a[0].pExpr;
ds@1952
  2471
  if( pExpr->op!=TK_COLUMN ) return 0;
ds@1952
  2472
  iCol = pExpr->iColumn;
ds@1952
  2473
  pTab = pSrc->a[0].pTab;
ds@1952
  2474
ds@1952
  2475
  /* This optimization cannot be used with virtual tables. */
ds@1952
  2476
  if( IsVirtual(pTab) ) return 0;
ds@1952
  2477
ds@1952
  2478
  /* If we get to here, it means the query is of the correct form.
ds@1952
  2479
  ** Check to make sure we have an index and make pIdx point to the
ds@1952
  2480
  ** appropriate index.  If the min() or max() is on an INTEGER PRIMARY
ds@1952
  2481
  ** key column, no index is necessary so set pIdx to NULL.  If no
ds@1952
  2482
  ** usable index is found, return 0.
ds@1952
  2483
  */
ds@1952
  2484
  if( iCol<0 ){
ds@1952
  2485
    pIdx = 0;
ds@1952
  2486
  }else{
ds@1952
  2487
    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
ds@1952
  2488
    if( pColl==0 ) return 0;
ds@1952
  2489
    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
ds@1952
  2490
      assert( pIdx->nColumn>=1 );
ds@1952
  2491
      if( pIdx->aiColumn[0]==iCol && 
ds@1952
  2492
          0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){
ds@1952
  2493
        break;
ds@1952
  2494
      }
ds@1952
  2495
    }
ds@1952
  2496
    if( pIdx==0 ) return 0;
ds@1952
  2497
  }
ds@1952
  2498
ds@1952
  2499
  /* Identify column types if we will be using the callback.  This
ds@1952
  2500
  ** step is skipped if the output is going to a table or a memory cell.
ds@1952
  2501
  ** The column names have already been generated in the calling function.
ds@1952
  2502
  */
ds@1952
  2503
  v = sqlite3GetVdbe(pParse);
ds@1952
  2504
  if( v==0 ) return 0;
ds@1952
  2505
ds@1952
  2506
  /* If the output is destined for a temporary table, open that table.
ds@1952
  2507
  */
ds@1952
  2508
  if( eDest==SRT_EphemTab ){
ds@1952
  2509
    sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 1);
ds@1952
  2510
  }
ds@1952
  2511
ds@1952
  2512
  /* Generating code to find the min or the max.  Basically all we have
ds@1952
  2513
  ** to do is find the first or the last entry in the chosen index.  If
ds@1952
  2514
  ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
ds@1952
  2515
  ** or last entry in the main table.
ds@1952
  2516
  */
ds@1952
  2517
  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
ds@1952
  2518
  assert( iDb>=0 || pTab->isEphem );
ds@1952
  2519
  sqlite3CodeVerifySchema(pParse, iDb);
ds@1952
  2520
  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
ds@1952
  2521
  base = pSrc->a[0].iCursor;
ds@1952
  2522
  brk = sqlite3VdbeMakeLabel(v);
ds@1952
  2523
  computeLimitRegisters(pParse, p, brk);
ds@1952
  2524
  if( pSrc->a[0].pSelect==0 ){
ds@1952
  2525
    sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead);
ds@1952
  2526
  }
ds@1952
  2527
  if( pIdx==0 ){
ds@1952
  2528
    sqlite3VdbeAddOp(v, seekOp, base, 0);
ds@1952
  2529
  }else{
ds@1952
  2530
    /* Even though the cursor used to open the index here is closed
ds@1952
  2531
    ** as soon as a single value has been read from it, allocate it
ds@1952
  2532
    ** using (pParse->nTab++) to prevent the cursor id from being 
ds@1952
  2533
    ** reused. This is important for statements of the form 
ds@1952
  2534
    ** "INSERT INTO x SELECT max() FROM x".
ds@1952
  2535
    */
ds@1952
  2536
    int iIdx;
ds@1952
  2537
    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
ds@1952
  2538
    iIdx = pParse->nTab++;
ds@1952
  2539
    assert( pIdx->pSchema==pTab->pSchema );
ds@1952
  2540
    sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
ds@1952
  2541
    sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 
ds@1952
  2542
        (char*)pKey, P3_KEYINFO_HANDOFF);
ds@1952
  2543
    if( seekOp==OP_Rewind ){
ds@1952
  2544
      sqlite3VdbeAddOp(v, OP_Null, 0, 0);
ds@1952
  2545
      sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
ds@1952
  2546
      seekOp = OP_MoveGt;
ds@1952
  2547
    }
ds@1952
  2548
    if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){
ds@1952
  2549
      /* Ticket #2514: invert the seek operator if we are using
ds@1952
  2550
      ** a descending index. */
ds@1952
  2551
      if( seekOp==OP_Last ){
ds@1952
  2552
        seekOp = OP_Rewind;
ds@1952
  2553
      }else{
ds@1952
  2554
        assert( seekOp==OP_MoveGt );
ds@1952
  2555
        seekOp = OP_MoveLt;
ds@1952
  2556
      }
ds@1952
  2557
    }
ds@1952
  2558
    sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
ds@1952
  2559
    sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0);
ds@1952
  2560
    sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
ds@1952
  2561
    sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
ds@1952
  2562
  }
ds@1952
  2563
  eList.nExpr = 1;
ds@1952
  2564
  memset(&eListItem, 0, sizeof(eListItem));
ds@1952
  2565
  eList.a = &eListItem;
ds@1952
  2566
  eList.a[0].pExpr = pExpr;
ds@1952
  2567
  selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0);
ds@1952
  2568
  sqlite3VdbeResolveLabel(v, brk);
ds@1952
  2569
  sqlite3VdbeAddOp(v, OP_Close, base, 0);
ds@1952
  2570
  
ds@1952
  2571
  return 1;
ds@1952
  2572
}
ds@1952
  2573
ds@1952
  2574
/*
ds@1952
  2575
** Analyze and ORDER BY or GROUP BY clause in a SELECT statement.  Return
ds@1952
  2576
** the number of errors seen.
ds@1952
  2577
**
ds@1952
  2578
** An ORDER BY or GROUP BY is a list of expressions.  If any expression
ds@1952
  2579
** is an integer constant, then that expression is replaced by the
ds@1952
  2580
** corresponding entry in the result set.
ds@1952
  2581
*/
ds@1952
  2582
static int processOrderGroupBy(
ds@1952
  2583
  NameContext *pNC,     /* Name context of the SELECT statement. */
ds@1952
  2584
  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
ds@1952
  2585
  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
ds@1952
  2586
){
ds@1952
  2587
  int i;
ds@1952
  2588
  ExprList *pEList = pNC->pEList;     /* The result set of the SELECT */
ds@1952
  2589
  Parse *pParse = pNC->pParse;     /* The result set of the SELECT */
ds@1952
  2590
  assert( pEList );
ds@1952
  2591
ds@1952
  2592
  if( pOrderBy==0 ) return 0;
ds@1952
  2593
  if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){
ds@1952
  2594
    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
ds@1952
  2595
    return 1;
ds@1952
  2596
  }
ds@1952
  2597
  for(i=0; i<pOrderBy->nExpr; i++){
ds@1952
  2598
    int iCol;
ds@1952
  2599
    Expr *pE = pOrderBy->a[i].pExpr;
ds@1952
  2600
    if( sqlite3ExprIsInteger(pE, &iCol) ){
ds@1952
  2601
      if( iCol>0 && iCol<=pEList->nExpr ){
ds@1952
  2602
        CollSeq *pColl = pE->pColl;
ds@1952
  2603
        int flags = pE->flags & EP_ExpCollate;
ds@1952
  2604
        sqlite3ExprDelete(pE);
ds@1952
  2605
        pE = sqlite3ExprDup(pParse->db, pEList->a[iCol-1].pExpr);
ds@1952
  2606
        pOrderBy->a[i].pExpr = pE;
ds@1952
  2607
        if( pColl && flags ){
ds@1952
  2608
          pE->pColl = pColl;
ds@1952
  2609
          pE->flags |= flags;
ds@1952
  2610
        }
ds@1952
  2611
      }else{
ds@1952
  2612
        sqlite3ErrorMsg(pParse, 
ds@1952
  2613
           "%s BY column number %d out of range - should be "
ds@1952
  2614
           "between 1 and %d", zType, iCol, pEList->nExpr);
ds@1952
  2615
        return 1;
ds@1952
  2616
      }
ds@1952
  2617
    }
ds@1952
  2618
    if( sqlite3ExprResolveNames(pNC, pE) ){
ds@1952
  2619
      return 1;
ds@1952
  2620
    }
ds@1952
  2621
  }
ds@1952
  2622
  return 0;
ds@1952
  2623
}
ds@1952
  2624
ds@1952
  2625
/*
ds@1952
  2626
** This routine resolves any names used in the result set of the
ds@1952
  2627
** supplied SELECT statement. If the SELECT statement being resolved
ds@1952
  2628
** is a sub-select, then pOuterNC is a pointer to the NameContext 
ds@1952
  2629
** of the parent SELECT.
ds@1952
  2630
*/
ds@1952
  2631
int sqlite3SelectResolve(
ds@1952
  2632
  Parse *pParse,         /* The parser context */
ds@1952
  2633
  Select *p,             /* The SELECT statement being coded. */
ds@1952
  2634
  NameContext *pOuterNC  /* The outer name context. May be NULL. */
ds@1952
  2635
){
ds@1952
  2636
  ExprList *pEList;          /* Result set. */
ds@1952
  2637
  int i;                     /* For-loop variable used in multiple places */
ds@1952
  2638
  NameContext sNC;           /* Local name-context */
ds@1952
  2639
  ExprList *pGroupBy;        /* The group by clause */
ds@1952
  2640
ds@1952
  2641
  /* If this routine has run before, return immediately. */
ds@1952
  2642
  if( p->isResolved ){
ds@1952
  2643
    assert( !pOuterNC );
ds@1952
  2644
    return SQLITE_OK;
ds@1952
  2645
  }
ds@1952
  2646
  p->isResolved = 1;
ds@1952
  2647
ds@1952
  2648
  /* If there have already been errors, do nothing. */
ds@1952
  2649
  if( pParse->nErr>0 ){
ds@1952
  2650
    return SQLITE_ERROR;
ds@1952
  2651
  }
ds@1952
  2652
ds@1952
  2653
  /* Prepare the select statement. This call will allocate all cursors
ds@1952
  2654
  ** required to handle the tables and subqueries in the FROM clause.
ds@1952
  2655
  */
ds@1952
  2656
  if( prepSelectStmt(pParse, p) ){
ds@1952
  2657
    return SQLITE_ERROR;
ds@1952
  2658
  }
ds@1952
  2659
ds@1952
  2660
  /* Resolve the expressions in the LIMIT and OFFSET clauses. These
ds@1952
  2661
  ** are not allowed to refer to any names, so pass an empty NameContext.
ds@1952
  2662
  */
ds@1952
  2663
  memset(&sNC, 0, sizeof(sNC));
ds@1952
  2664
  sNC.pParse = pParse;
ds@1952
  2665
  if( sqlite3ExprResolveNames(&sNC, p->pLimit) ||
ds@1952
  2666
      sqlite3ExprResolveNames(&sNC, p->pOffset) ){
ds@1952
  2667
    return SQLITE_ERROR;
ds@1952
  2668
  }
ds@1952
  2669
ds@1952
  2670
  /* Set up the local name-context to pass to ExprResolveNames() to
ds@1952
  2671
  ** resolve the expression-list.
ds@1952
  2672
  */
ds@1952
  2673
  sNC.allowAgg = 1;
ds@1952
  2674
  sNC.pSrcList = p->pSrc;
ds@1952
  2675
  sNC.pNext = pOuterNC;
ds@1952
  2676
ds@1952
  2677
  /* Resolve names in the result set. */
ds@1952
  2678
  pEList = p->pEList;
ds@1952
  2679
  if( !pEList ) return SQLITE_ERROR;
ds@1952
  2680
  for(i=0; i<pEList->nExpr; i++){
ds@1952
  2681
    Expr *pX = pEList->a[i].pExpr;
ds@1952
  2682
    if( sqlite3ExprResolveNames(&sNC, pX) ){
ds@1952
  2683
      return SQLITE_ERROR;
ds@1952
  2684
    }
ds@1952
  2685
  }
ds@1952
  2686
ds@1952
  2687
  /* If there are no aggregate functions in the result-set, and no GROUP BY 
ds@1952
  2688
  ** expression, do not allow aggregates in any of the other expressions.
ds@1952
  2689
  */
ds@1952
  2690
  assert( !p->isAgg );
ds@1952
  2691
  pGroupBy = p->pGroupBy;
ds@1952
  2692
  if( pGroupBy || sNC.hasAgg ){
ds@1952
  2693
    p->isAgg = 1;
ds@1952
  2694
  }else{
ds@1952
  2695
    sNC.allowAgg = 0;
ds@1952
  2696
  }
ds@1952
  2697
ds@1952
  2698
  /* If a HAVING clause is present, then there must be a GROUP BY clause.
ds@1952
  2699
  */
ds@1952
  2700
  if( p->pHaving && !pGroupBy ){
ds@1952
  2701
    sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
ds@1952
  2702
    return SQLITE_ERROR;
ds@1952
  2703
  }
ds@1952
  2704
ds@1952
  2705
  /* Add the expression list to the name-context before parsing the
ds@1952
  2706
  ** other expressions in the SELECT statement. This is so that
ds@1952
  2707
  ** expressions in the WHERE clause (etc.) can refer to expressions by
ds@1952
  2708
  ** aliases in the result set.
ds@1952
  2709
  **
ds@1952
  2710
  ** Minor point: If this is the case, then the expression will be
ds@1952
  2711
  ** re-evaluated for each reference to it.
ds@1952
  2712
  */
ds@1952
  2713
  sNC.pEList = p->pEList;
ds@1952
  2714
  if( sqlite3ExprResolveNames(&sNC, p->pWhere) ||
ds@1952
  2715
     sqlite3ExprResolveNames(&sNC, p->pHaving) ){
ds@1952
  2716
    return SQLITE_ERROR;
ds@1952
  2717
  }
ds@1952
  2718
  if( p->pPrior==0 ){
ds@1952
  2719
    if( processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") ||
ds@1952
  2720
        processOrderGroupBy(&sNC, pGroupBy, "GROUP") ){
ds@1952
  2721
      return SQLITE_ERROR;
ds@1952
  2722
    }
ds@1952
  2723
  }
ds@1952
  2724
ds@1952
  2725
  if( pParse->db->mallocFailed ){
ds@1952
  2726
    return SQLITE_NOMEM;
ds@1952
  2727
  }
ds@1952
  2728
ds@1952
  2729
  /* Make sure the GROUP BY clause does not contain aggregate functions.
ds@1952
  2730
  */
ds@1952
  2731
  if( pGroupBy ){
ds@1952
  2732
    struct ExprList_item *pItem;
ds@1952
  2733
  
ds@1952
  2734
    for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
ds@1952
  2735
      if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
ds@1952
  2736
        sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
ds@1952
  2737
            "the GROUP BY clause");
ds@1952
  2738
        return SQLITE_ERROR;
ds@1952
  2739
      }
ds@1952
  2740
    }
ds@1952
  2741
  }
ds@1952
  2742
ds@1952
  2743
  /* If this is one SELECT of a compound, be sure to resolve names
ds@1952
  2744
  ** in the other SELECTs.
ds@1952
  2745
  */
ds@1952
  2746
  if( p->pPrior ){
ds@1952
  2747
    return sqlite3SelectResolve(pParse, p->pPrior, pOuterNC);
ds@1952
  2748
  }else{
ds@1952
  2749
    return SQLITE_OK;
ds@1952
  2750
  }
ds@1952
  2751
}
ds@1952
  2752
ds@1952
  2753
/*
ds@1952
  2754
** Reset the aggregate accumulator.
ds@1952
  2755
**
ds@1952
  2756
** The aggregate accumulator is a set of memory cells that hold
ds@1952
  2757
** intermediate results while calculating an aggregate.  This
ds@1952
  2758
** routine simply stores NULLs in all of those memory cells.
ds@1952
  2759
*/
ds@1952
  2760
static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
ds@1952
  2761
  Vdbe *v = pParse->pVdbe;
ds@1952
  2762
  int i;
ds@1952
  2763
  struct AggInfo_func *pFunc;
ds@1952
  2764
  if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
ds@1952
  2765
    return;
ds@1952
  2766
  }
ds@1952
  2767
  for(i=0; i<pAggInfo->nColumn; i++){
ds@1952
  2768
    sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0);
ds@1952
  2769
  }
ds@1952
  2770
  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
ds@1952
  2771
    sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0);
ds@1952
  2772
    if( pFunc->iDistinct>=0 ){
ds@1952
  2773
      Expr *pE = pFunc->pExpr;
ds@1952
  2774
      if( pE->pList==0 || pE->pList->nExpr!=1 ){
ds@1952
  2775
        sqlite3ErrorMsg(pParse, "DISTINCT in aggregate must be followed "
ds@1952
  2776
           "by an expression");
ds@1952
  2777
        pFunc->iDistinct = -1;
ds@1952
  2778
      }else{
ds@1952
  2779
        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList);
ds@1952
  2780
        sqlite3VdbeOp3(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 
ds@1952
  2781
                          (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
ds@1952
  2782
      }
ds@1952
  2783
    }
ds@1952
  2784
  }
ds@1952
  2785
}
ds@1952
  2786
ds@1952
  2787
/*
ds@1952
  2788
** Invoke the OP_AggFinalize opcode for every aggregate function
ds@1952
  2789
** in the AggInfo structure.
ds@1952
  2790
*/
ds@1952
  2791
static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
ds@1952
  2792
  Vdbe *v = pParse->pVdbe;
ds@1952
  2793
  int i;
ds@1952
  2794
  struct AggInfo_func *pF;
ds@1952
  2795
  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
ds@1952
  2796
    ExprList *pList = pF->pExpr->pList;
ds@1952
  2797
    sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0,
ds@1952
  2798
                      (void*)pF->pFunc, P3_FUNCDEF);
ds@1952
  2799
  }
ds@1952
  2800
}
ds@1952
  2801
ds@1952
  2802
/*
ds@1952
  2803
** Update the accumulator memory cells for an aggregate based on
ds@1952
  2804
** the current cursor position.
ds@1952
  2805
*/
ds@1952
  2806
static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
ds@1952
  2807
  Vdbe *v = pParse->pVdbe;
ds@1952
  2808
  int i;
ds@1952
  2809
  struct AggInfo_func *pF;
ds@1952
  2810
  struct AggInfo_col *pC;
ds@1952
  2811
ds@1952
  2812
  pAggInfo->directMode = 1;
ds@1952
  2813
  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
ds@1952
  2814
    int nArg;
ds@1952
  2815
    int addrNext = 0;
ds@1952
  2816
    ExprList *pList = pF->pExpr->pList;
ds@1952
  2817
    if( pList ){
ds@1952
  2818
      nArg = pList->nExpr;
ds@1952
  2819
      sqlite3ExprCodeExprList(pParse, pList);
ds@1952
  2820
    }else{
ds@1952
  2821
      nArg = 0;
ds@1952
  2822
    }
ds@1952
  2823
    if( pF->iDistinct>=0 ){
ds@1952
  2824
      addrNext = sqlite3VdbeMakeLabel(v);
ds@1952
  2825
      assert( nArg==1 );
ds@1952
  2826
      codeDistinct(v, pF->iDistinct, addrNext, 1);
ds@1952
  2827
    }
ds@1952
  2828
    if( pF->pFunc->needCollSeq ){
ds@1952
  2829
      CollSeq *pColl = 0;
ds@1952
  2830
      struct ExprList_item *pItem;
ds@1952
  2831
      int j;
ds@1952
  2832
      assert( pList!=0 );  /* pList!=0 if pF->pFunc->needCollSeq is true */
ds@1952
  2833
      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
ds@1952
  2834
        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
ds@1952
  2835
      }
ds@1952
  2836
      if( !pColl ){
ds@1952
  2837
        pColl = pParse->db->pDfltColl;
ds@1952
  2838
      }
ds@1952
  2839
      sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
ds@1952
  2840
    }
ds@1952
  2841
    sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF);
ds@1952
  2842
    if( addrNext ){
ds@1952
  2843
      sqlite3VdbeResolveLabel(v, addrNext);
ds@1952
  2844
    }
ds@1952
  2845
  }
ds@1952
  2846
  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
ds@1952
  2847
    sqlite3ExprCode(pParse, pC->pExpr);
ds@1952
  2848
    sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1);
ds@1952
  2849
  }
ds@1952
  2850
  pAggInfo->directMode = 0;
ds@1952
  2851
}
ds@1952
  2852
ds@1952
  2853
ds@1952
  2854
/*
ds@1952
  2855
** Generate code for the given SELECT statement.
ds@1952
  2856
**
ds@1952
  2857
** The results are distributed in various ways depending on the
ds@1952
  2858
** value of eDest and iParm.
ds@1952
  2859
**
ds@1952
  2860
**     eDest Value       Result
ds@1952
  2861
**     ------------    -------------------------------------------
ds@1952
  2862
**     SRT_Callback    Invoke the callback for each row of the result.
ds@1952
  2863
**
ds@1952
  2864
**     SRT_Mem         Store first result in memory cell iParm
ds@1952
  2865
**
ds@1952
  2866
**     SRT_Set         Store results as keys of table iParm.
ds@1952
  2867
**
ds@1952
  2868
**     SRT_Union       Store results as a key in a temporary table iParm
ds@1952
  2869
**
ds@1952
  2870
**     SRT_Except      Remove results from the temporary table iParm.
ds@1952
  2871
**
ds@1952
  2872
**     SRT_Table       Store results in temporary table iParm
ds@1952
  2873
**
ds@1952
  2874
** The table above is incomplete.  Additional eDist value have be added
ds@1952
  2875
** since this comment was written.  See the selectInnerLoop() function for
ds@1952
  2876
** a complete listing of the allowed values of eDest and their meanings.
ds@1952
  2877
**
ds@1952
  2878
** This routine returns the number of errors.  If any errors are
ds@1952
  2879
** encountered, then an appropriate error message is left in
ds@1952
  2880
** pParse->zErrMsg.
ds@1952
  2881
**
ds@1952
  2882
** This routine does NOT free the Select structure passed in.  The
ds@1952
  2883
** calling function needs to do that.
ds@1952
  2884
**
ds@1952
  2885
** The pParent, parentTab, and *pParentAgg fields are filled in if this
ds@1952
  2886
** SELECT is a subquery.  This routine may try to combine this SELECT
ds@1952
  2887
** with its parent to form a single flat query.  In so doing, it might
ds@1952
  2888
** change the parent query from a non-aggregate to an aggregate query.
ds@1952
  2889
** For that reason, the pParentAgg flag is passed as a pointer, so it
ds@1952
  2890
** can be changed.
ds@1952
  2891
**
ds@1952
  2892
** Example 1:   The meaning of the pParent parameter.
ds@1952
  2893
**
ds@1952
  2894
**    SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
ds@1952
  2895
**    \                      \_______ subquery _______/        /
ds@1952
  2896
**     \                                                      /
ds@1952
  2897
**      \____________________ outer query ___________________/
ds@1952
  2898
**
ds@1952
  2899
** This routine is called for the outer query first.   For that call,
ds@1952
  2900
** pParent will be NULL.  During the processing of the outer query, this 
ds@1952
  2901
** routine is called recursively to handle the subquery.  For the recursive
ds@1952
  2902
** call, pParent will point to the outer query.  Because the subquery is
ds@1952
  2903
** the second element in a three-way join, the parentTab parameter will
ds@1952
  2904
** be 1 (the 2nd value of a 0-indexed array.)
ds@1952
  2905
*/
ds@1952
  2906
int sqlite3Select(
ds@1952
  2907
  Parse *pParse,         /* The parser context */
ds@1952
  2908
  Select *p,             /* The SELECT statement being coded. */
ds@1952
  2909
  int eDest,             /* How to dispose of the results */
ds@1952
  2910
  int iParm,             /* A parameter used by the eDest disposal method */
ds@1952
  2911
  Select *pParent,       /* Another SELECT for which this is a sub-query */
ds@1952
  2912
  int parentTab,         /* Index in pParent->pSrc of this query */
ds@1952
  2913
  int *pParentAgg,       /* True if pParent uses aggregate functions */
ds@1952
  2914
  char *aff              /* If eDest is SRT_Union, the affinity string */
ds@1952
  2915
){
ds@1952
  2916
  int i, j;              /* Loop counters */
ds@1952
  2917
  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
ds@1952
  2918
  Vdbe *v;               /* The virtual machine under construction */
ds@1952
  2919
  int isAgg;             /* True for select lists like "count(*)" */
ds@1952
  2920
  ExprList *pEList;      /* List of columns to extract. */
ds@1952
  2921
  SrcList *pTabList;     /* List of tables to select from */
ds@1952
  2922
  Expr *pWhere;          /* The WHERE clause.  May be NULL */
ds@1952
  2923
  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
ds@1952
  2924
  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
ds@1952
  2925
  Expr *pHaving;         /* The HAVING clause.  May be NULL */
ds@1952
  2926
  int isDistinct;        /* True if the DISTINCT keyword is present */
ds@1952
  2927
  int distinct;          /* Table to use for the distinct set */
ds@1952
  2928
  int rc = 1;            /* Value to return from this function */
ds@1952
  2929
  int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
ds@1952
  2930
  AggInfo sAggInfo;      /* Information used by aggregate queries */
ds@1952
  2931
  int iEnd;              /* Address of the end of the query */
ds@1952
  2932
  sqlite3 *db;           /* The database connection */
ds@1952
  2933
ds@1952
  2934
  db = pParse->db;
ds@1952
  2935
  if( p==0 || db->mallocFailed || pParse->nErr ){
ds@1952
  2936
    return 1;
ds@1952
  2937
  }
ds@1952
  2938
  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
ds@1952
  2939
  memset(&sAggInfo, 0, sizeof(sAggInfo));
ds@1952
  2940
ds@1952
  2941
#ifndef SQLITE_OMIT_COMPOUND_SELECT
ds@1952
  2942
  /* If there is are a sequence of queries, do the earlier ones first.
ds@1952
  2943
  */
ds@1952
  2944
  if( p->pPrior ){
ds@1952
  2945
    if( p->pRightmost==0 ){
ds@1952
  2946
      Select *pLoop;
ds@1952
  2947
      int cnt = 0;
ds@1952
  2948
      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
ds@1952
  2949
        pLoop->pRightmost = p;
ds@1952
  2950
      }
ds@1952
  2951
      if( SQLITE_MAX_COMPOUND_SELECT>0 && cnt>SQLITE_MAX_COMPOUND_SELECT ){
ds@1952
  2952
        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
ds@1952
  2953
        return 1;
ds@1952
  2954
      }
ds@1952
  2955
    }
ds@1952
  2956
    return multiSelect(pParse, p, eDest, iParm, aff);
ds@1952
  2957
  }
ds@1952
  2958
#endif
ds@1952
  2959
ds@1952
  2960
  pOrderBy = p->pOrderBy;
ds@1952
  2961
  if( IgnorableOrderby(eDest) ){
ds@1952
  2962
    p->pOrderBy = 0;
ds@1952
  2963
  }
ds@1952
  2964
  if( sqlite3SelectResolve(pParse, p, 0) ){
ds@1952
  2965
    goto select_end;
ds@1952
  2966
  }
ds@1952
  2967
  p->pOrderBy = pOrderBy;
ds@1952
  2968
ds@1952
  2969
  /* Make local copies of the parameters for this query.
ds@1952
  2970
  */
ds@1952
  2971
  pTabList = p->pSrc;
ds@1952
  2972
  pWhere = p->pWhere;
ds@1952
  2973
  pGroupBy = p->pGroupBy;
ds@1952
  2974
  pHaving = p->pHaving;
ds@1952
  2975
  isAgg = p->isAgg;
ds@1952
  2976
  isDistinct = p->isDistinct;
ds@1952
  2977
  pEList = p->pEList;
ds@1952
  2978
  if( pEList==0 ) goto select_end;
ds@1952
  2979
ds@1952
  2980
  /* 
ds@1952
  2981
  ** Do not even attempt to generate any code if we have already seen
ds@1952
  2982
  ** errors before this routine starts.
ds@1952
  2983
  */
ds@1952
  2984
  if( pParse->nErr>0 ) goto select_end;
ds@1952
  2985
ds@1952
  2986
  /* If writing to memory or generating a set
ds@1952
  2987
  ** only a single column may be output.
ds@1952
  2988
  */
ds@1952
  2989
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
  2990
  if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){
ds@1952
  2991
    goto select_end;
ds@1952
  2992
  }
ds@1952
  2993
#endif
ds@1952
  2994
ds@1952
  2995
  /* ORDER BY is ignored for some destinations.
ds@1952
  2996
  */
ds@1952
  2997
  if( IgnorableOrderby(eDest) ){
ds@1952
  2998
    pOrderBy = 0;
ds@1952
  2999
  }
ds@1952
  3000
ds@1952
  3001
  /* Begin generating code.
ds@1952
  3002
  */
ds@1952
  3003
  v = sqlite3GetVdbe(pParse);
ds@1952
  3004
  if( v==0 ) goto select_end;
ds@1952
  3005
ds@1952
  3006
  /* Generate code for all sub-queries in the FROM clause
ds@1952
  3007
  */
ds@1952
  3008
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
ds@1952
  3009
  for(i=0; i<pTabList->nSrc; i++){
ds@1952
  3010
    const char *zSavedAuthContext = 0;
ds@1952
  3011
    int needRestoreContext;
ds@1952
  3012
    struct SrcList_item *pItem = &pTabList->a[i];
ds@1952
  3013
ds@1952
  3014
    if( pItem->pSelect==0 || pItem->isPopulated ) continue;
ds@1952
  3015
    if( pItem->zName!=0 ){
ds@1952
  3016
      zSavedAuthContext = pParse->zAuthContext;
ds@1952
  3017
      pParse->zAuthContext = pItem->zName;
ds@1952
  3018
      needRestoreContext = 1;
ds@1952
  3019
    }else{
ds@1952
  3020
      needRestoreContext = 0;
ds@1952
  3021
    }
ds@1952
  3022
#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
ds@1952
  3023
    /* Increment Parse.nHeight by the height of the largest expression
ds@1952
  3024
    ** tree refered to by this, the parent select. The child select
ds@1952
  3025
    ** may contain expression trees of at most
ds@1952
  3026
    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
ds@1952
  3027
    ** more conservative than necessary, but much easier than enforcing
ds@1952
  3028
    ** an exact limit.
ds@1952
  3029
    */
ds@1952
  3030
    pParse->nHeight += sqlite3SelectExprHeight(p);
ds@1952
  3031
#endif
ds@1952
  3032
    sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab, 
ds@1952
  3033
                 pItem->iCursor, p, i, &isAgg, 0);
ds@1952
  3034
#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
ds@1952
  3035
    pParse->nHeight -= sqlite3SelectExprHeight(p);
ds@1952
  3036
#endif
ds@1952
  3037
    if( needRestoreContext ){
ds@1952
  3038
      pParse->zAuthContext = zSavedAuthContext;
ds@1952
  3039
    }
ds@1952
  3040
    pTabList = p->pSrc;
ds@1952
  3041
    pWhere = p->pWhere;
ds@1952
  3042
    if( !IgnorableOrderby(eDest) ){
ds@1952
  3043
      pOrderBy = p->pOrderBy;
ds@1952
  3044
    }
ds@1952
  3045
    pGroupBy = p->pGroupBy;
ds@1952
  3046
    pHaving = p->pHaving;
ds@1952
  3047
    isDistinct = p->isDistinct;
ds@1952
  3048
  }
ds@1952
  3049
#endif
ds@1952
  3050
ds@1952
  3051
  /* Check for the special case of a min() or max() function by itself
ds@1952
  3052
  ** in the result set.
ds@1952
  3053
  */
ds@1952
  3054
  if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
ds@1952
  3055
    rc = 0;
ds@1952
  3056
    goto select_end;
ds@1952
  3057
  }
ds@1952
  3058
ds@1952
  3059
  /* Check to see if this is a subquery that can be "flattened" into its parent.
ds@1952
  3060
  ** If flattening is a possiblity, do so and return immediately.  
ds@1952
  3061
  */
ds@1952
  3062
#ifndef SQLITE_OMIT_VIEW
ds@1952
  3063
  if( pParent && pParentAgg &&
ds@1952
  3064
      flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){
ds@1952
  3065
    if( isAgg ) *pParentAgg = 1;
ds@1952
  3066
    goto select_end;
ds@1952
  3067
  }
ds@1952
  3068
#endif
ds@1952
  3069
ds@1952
  3070
  /* If there is an ORDER BY clause, then this sorting
ds@1952
  3071
  ** index might end up being unused if the data can be 
ds@1952
  3072
  ** extracted in pre-sorted order.  If that is the case, then the
ds@1952
  3073
  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
ds@1952
  3074
  ** we figure out that the sorting index is not needed.  The addrSortIndex
ds@1952
  3075
  ** variable is used to facilitate that change.
ds@1952
  3076
  */
ds@1952
  3077
  if( pOrderBy ){
ds@1952
  3078
    KeyInfo *pKeyInfo;
ds@1952
  3079
    if( pParse->nErr ){
ds@1952
  3080
      goto select_end;
ds@1952
  3081
    }
ds@1952
  3082
    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
ds@1952
  3083
    pOrderBy->iECursor = pParse->nTab++;
ds@1952
  3084
    p->addrOpenEphm[2] = addrSortIndex =
ds@1952
  3085
      sqlite3VdbeOp3(v, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+2,                     (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
ds@1952
  3086
  }else{
ds@1952
  3087
    addrSortIndex = -1;
ds@1952
  3088
  }
ds@1952
  3089
ds@1952
  3090
  /* If the output is destined for a temporary table, open that table.
ds@1952
  3091
  */
ds@1952
  3092
  if( eDest==SRT_EphemTab ){
ds@1952
  3093
    sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, pEList->nExpr);
ds@1952
  3094
  }
ds@1952
  3095
ds@1952
  3096
  /* Set the limiter.
ds@1952
  3097
  */
ds@1952
  3098
  iEnd = sqlite3VdbeMakeLabel(v);
ds@1952
  3099
  computeLimitRegisters(pParse, p, iEnd);
ds@1952
  3100
ds@1952
  3101
  /* Open a virtual index to use for the distinct set.
ds@1952
  3102
  */
ds@1952
  3103
  if( isDistinct ){
ds@1952
  3104
    KeyInfo *pKeyInfo;
ds@1952
  3105
    distinct = pParse->nTab++;
ds@1952
  3106
    pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
ds@1952
  3107
    sqlite3VdbeOp3(v, OP_OpenEphemeral, distinct, 0, 
ds@1952
  3108
                        (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
ds@1952
  3109
  }else{
ds@1952
  3110
    distinct = -1;
ds@1952
  3111
  }
ds@1952
  3112
ds@1952
  3113
  /* Aggregate and non-aggregate queries are handled differently */
ds@1952
  3114
  if( !isAgg && pGroupBy==0 ){
ds@1952
  3115
    /* This case is for non-aggregate queries
ds@1952
  3116
    ** Begin the database scan
ds@1952
  3117
    */
ds@1952
  3118
    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy);
ds@1952
  3119
    if( pWInfo==0 ) goto select_end;
ds@1952
  3120
ds@1952
  3121
    /* If sorting index that was created by a prior OP_OpenEphemeral 
ds@1952
  3122
    ** instruction ended up not being needed, then change the OP_OpenEphemeral
ds@1952
  3123
    ** into an OP_Noop.
ds@1952
  3124
    */
ds@1952
  3125
    if( addrSortIndex>=0 && pOrderBy==0 ){
ds@1952
  3126
      sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
ds@1952
  3127
      p->addrOpenEphm[2] = -1;
ds@1952
  3128
    }
ds@1952
  3129
ds@1952
  3130
    /* Use the standard inner loop
ds@1952
  3131
    */
ds@1952
  3132
    if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
ds@1952
  3133
                    iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){
ds@1952
  3134
       goto select_end;
ds@1952
  3135
    }
ds@1952
  3136
ds@1952
  3137
    /* End the database scan loop.
ds@1952
  3138
    */
ds@1952
  3139
    sqlite3WhereEnd(pWInfo);
ds@1952
  3140
  }else{
ds@1952
  3141
    /* This is the processing for aggregate queries */
ds@1952
  3142
    NameContext sNC;    /* Name context for processing aggregate information */
ds@1952
  3143
    int iAMem;          /* First Mem address for storing current GROUP BY */
ds@1952
  3144
    int iBMem;          /* First Mem address for previous GROUP BY */
ds@1952
  3145
    int iUseFlag;       /* Mem address holding flag indicating that at least
ds@1952
  3146
                        ** one row of the input to the aggregator has been
ds@1952
  3147
                        ** processed */
ds@1952
  3148
    int iAbortFlag;     /* Mem address which causes query abort if positive */
ds@1952
  3149
    int groupBySort;    /* Rows come from source in GROUP BY order */
ds@1952
  3150
ds@1952
  3151
ds@1952
  3152
    /* The following variables hold addresses or labels for parts of the
ds@1952
  3153
    ** virtual machine program we are putting together */
ds@1952
  3154
    int addrOutputRow;      /* Start of subroutine that outputs a result row */
ds@1952
  3155
    int addrSetAbort;       /* Set the abort flag and return */
ds@1952
  3156
    int addrInitializeLoop; /* Start of code that initializes the input loop */
ds@1952
  3157
    int addrTopOfLoop;      /* Top of the input loop */
ds@1952
  3158
    int addrGroupByChange;  /* Code that runs when any GROUP BY term changes */
ds@1952
  3159
    int addrProcessRow;     /* Code to process a single input row */
ds@1952
  3160
    int addrEnd;            /* End of all processing */
ds@1952
  3161
    int addrSortingIdx;     /* The OP_OpenEphemeral for the sorting index */
ds@1952
  3162
    int addrReset;          /* Subroutine for resetting the accumulator */
ds@1952
  3163
ds@1952
  3164
    addrEnd = sqlite3VdbeMakeLabel(v);
ds@1952
  3165
ds@1952
  3166
    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
ds@1952
  3167
    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
ds@1952
  3168
    ** SELECT statement.
ds@1952
  3169
    */
ds@1952
  3170
    memset(&sNC, 0, sizeof(sNC));
ds@1952
  3171
    sNC.pParse = pParse;
ds@1952
  3172
    sNC.pSrcList = pTabList;
ds@1952
  3173
    sNC.pAggInfo = &sAggInfo;
ds@1952
  3174
    sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
ds@1952
  3175
    sAggInfo.pGroupBy = pGroupBy;
ds@1952
  3176
    if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){
ds@1952
  3177
      goto select_end;
ds@1952
  3178
    }
ds@1952
  3179
    if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){
ds@1952
  3180
      goto select_end;
ds@1952
  3181
    }
ds@1952
  3182
    if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){
ds@1952
  3183
      goto select_end;
ds@1952
  3184
    }
ds@1952
  3185
    sAggInfo.nAccumulator = sAggInfo.nColumn;
ds@1952
  3186
    for(i=0; i<sAggInfo.nFunc; i++){
ds@1952
  3187
      if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
ds@1952
  3188
        goto select_end;
ds@1952
  3189
      }
ds@1952
  3190
    }
ds@1952
  3191
    if( db->mallocFailed ) goto select_end;
ds@1952
  3192
ds@1952
  3193
    /* Processing for aggregates with GROUP BY is very different and
ds@1952
  3194
    ** much more complex tha aggregates without a GROUP BY.
ds@1952
  3195
    */
ds@1952
  3196
    if( pGroupBy ){
ds@1952
  3197
      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
ds@1952
  3198
ds@1952
  3199
      /* Create labels that we will be needing
ds@1952
  3200
      */
ds@1952
  3201
     
ds@1952
  3202
      addrInitializeLoop = sqlite3VdbeMakeLabel(v);
ds@1952
  3203
      addrGroupByChange = sqlite3VdbeMakeLabel(v);
ds@1952
  3204
      addrProcessRow = sqlite3VdbeMakeLabel(v);
ds@1952
  3205
ds@1952
  3206
      /* If there is a GROUP BY clause we might need a sorting index to
ds@1952
  3207
      ** implement it.  Allocate that sorting index now.  If it turns out
ds@1952
  3208
      ** that we do not need it after all, the OpenEphemeral instruction
ds@1952
  3209
      ** will be converted into a Noop.  
ds@1952
  3210
      */
ds@1952
  3211
      sAggInfo.sortingIdx = pParse->nTab++;
ds@1952
  3212
      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
ds@1952
  3213
      addrSortingIdx =
ds@1952
  3214
          sqlite3VdbeOp3(v, OP_OpenEphemeral, sAggInfo.sortingIdx,
ds@1952
  3215
                         sAggInfo.nSortingColumn,
ds@1952
  3216
                         (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
ds@1952
  3217
ds@1952
  3218
      /* Initialize memory locations used by GROUP BY aggregate processing
ds@1952
  3219
      */
ds@1952
  3220
      iUseFlag = pParse->nMem++;
ds@1952
  3221
      iAbortFlag = pParse->nMem++;
ds@1952
  3222
      iAMem = pParse->nMem;
ds@1952
  3223
      pParse->nMem += pGroupBy->nExpr;
ds@1952
  3224
      iBMem = pParse->nMem;
ds@1952
  3225
      pParse->nMem += pGroupBy->nExpr;
ds@1952
  3226
      sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag);
ds@1952
  3227
      VdbeComment((v, "# clear abort flag"));
ds@1952
  3228
      sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag);
ds@1952
  3229
      VdbeComment((v, "# indicate accumulator empty"));
ds@1952
  3230
      sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop);
ds@1952
  3231
ds@1952
  3232
      /* Generate a subroutine that outputs a single row of the result
ds@1952
  3233
      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
ds@1952
  3234
      ** is less than or equal to zero, the subroutine is a no-op.  If
ds@1952
  3235
      ** the processing calls for the query to abort, this subroutine
ds@1952
  3236
      ** increments the iAbortFlag memory location before returning in
ds@1952
  3237
      ** order to signal the caller to abort.
ds@1952
  3238
      */
ds@1952
  3239
      addrSetAbort = sqlite3VdbeCurrentAddr(v);
ds@1952
  3240
      sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag);
ds@1952
  3241
      VdbeComment((v, "# set abort flag"));
ds@1952
  3242
      sqlite3VdbeAddOp(v, OP_Return, 0, 0);
ds@1952
  3243
      addrOutputRow = sqlite3VdbeCurrentAddr(v);
ds@1952
  3244
      sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2);
ds@1952
  3245
      VdbeComment((v, "# Groupby result generator entry point"));
ds@1952
  3246
      sqlite3VdbeAddOp(v, OP_Return, 0, 0);
ds@1952
  3247
      finalizeAggFunctions(pParse, &sAggInfo);
ds@1952
  3248
      if( pHaving ){
ds@1952
  3249
        sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1);
ds@1952
  3250
      }
ds@1952
  3251
      rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
ds@1952
  3252
                           distinct, eDest, iParm, 
ds@1952
  3253
                           addrOutputRow+1, addrSetAbort, aff);
ds@1952
  3254
      if( rc ){
ds@1952
  3255
        goto select_end;
ds@1952
  3256
      }
ds@1952
  3257
      sqlite3VdbeAddOp(v, OP_Return, 0, 0);
ds@1952
  3258
      VdbeComment((v, "# end groupby result generator"));
ds@1952
  3259
ds@1952
  3260
      /* Generate a subroutine that will reset the group-by accumulator
ds@1952
  3261
      */
ds@1952
  3262
      addrReset = sqlite3VdbeCurrentAddr(v);
ds@1952
  3263
      resetAccumulator(pParse, &sAggInfo);
ds@1952
  3264
      sqlite3VdbeAddOp(v, OP_Return, 0, 0);
ds@1952
  3265
ds@1952
  3266
      /* Begin a loop that will extract all source rows in GROUP BY order.
ds@1952
  3267
      ** This might involve two separate loops with an OP_Sort in between, or
ds@1952
  3268
      ** it might be a single loop that uses an index to extract information
ds@1952
  3269
      ** in the right order to begin with.
ds@1952
  3270
      */
ds@1952
  3271
      sqlite3VdbeResolveLabel(v, addrInitializeLoop);
ds@1952
  3272
      sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
ds@1952
  3273
      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy);
ds@1952
  3274
      if( pWInfo==0 ) goto select_end;
ds@1952
  3275
      if( pGroupBy==0 ){
ds@1952
  3276
        /* The optimizer is able to deliver rows in group by order so
ds@1952
  3277
        ** we do not have to sort.  The OP_OpenEphemeral table will be
ds@1952
  3278
        ** cancelled later because we still need to use the pKeyInfo
ds@1952
  3279
        */
ds@1952
  3280
        pGroupBy = p->pGroupBy;
ds@1952
  3281
        groupBySort = 0;
ds@1952
  3282
      }else{
ds@1952
  3283
        /* Rows are coming out in undetermined order.  We have to push
ds@1952
  3284
        ** each row into a sorting index, terminate the first loop,
ds@1952
  3285
        ** then loop over the sorting index in order to get the output
ds@1952
  3286
        ** in sorted order
ds@1952
  3287
        */
ds@1952
  3288
        groupBySort = 1;
ds@1952
  3289
        sqlite3ExprCodeExprList(pParse, pGroupBy);
ds@1952
  3290
        sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0);
ds@1952
  3291
        j = pGroupBy->nExpr+1;
ds@1952
  3292
        for(i=0; i<sAggInfo.nColumn; i++){
ds@1952
  3293
          struct AggInfo_col *pCol = &sAggInfo.aCol[i];
ds@1952
  3294
          if( pCol->iSorterColumn<j ) continue;
ds@1952
  3295
          sqlite3ExprCodeGetColumn(v, pCol->pTab, pCol->iColumn, pCol->iTable);
ds@1952
  3296
          j++;
ds@1952
  3297
        }
ds@1952
  3298
        sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0);
ds@1952
  3299
        sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0);
ds@1952
  3300
        sqlite3WhereEnd(pWInfo);
ds@1952
  3301
        sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
ds@1952
  3302
        VdbeComment((v, "# GROUP BY sort"));
ds@1952
  3303
        sAggInfo.useSortingIdx = 1;
ds@1952
  3304
      }
ds@1952
  3305
ds@1952
  3306
      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
ds@1952
  3307
      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
ds@1952
  3308
      ** Then compare the current GROUP BY terms against the GROUP BY terms
ds@1952
  3309
      ** from the previous row currently stored in a0, a1, a2...
ds@1952
  3310
      */
ds@1952
  3311
      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
ds@1952
  3312
      for(j=0; j<pGroupBy->nExpr; j++){
ds@1952
  3313
        if( groupBySort ){
ds@1952
  3314
          sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j);
ds@1952
  3315
        }else{
ds@1952
  3316
          sAggInfo.directMode = 1;
ds@1952
  3317
          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr);
ds@1952
  3318
        }
ds@1952
  3319
        sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, j<pGroupBy->nExpr-1);
ds@1952
  3320
      }
ds@1952
  3321
      for(j=pGroupBy->nExpr-1; j>=0; j--){
ds@1952
  3322
        if( j<pGroupBy->nExpr-1 ){
ds@1952
  3323
          sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0);
ds@1952
  3324
        }
ds@1952
  3325
        sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0);
ds@1952
  3326
        if( j==0 ){
ds@1952
  3327
          sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow);
ds@1952
  3328
        }else{
ds@1952
  3329
          sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange);
ds@1952
  3330
        }
ds@1952
  3331
        sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ);
ds@1952
  3332
      }
ds@1952
  3333
ds@1952
  3334
      /* Generate code that runs whenever the GROUP BY changes.
ds@1952
  3335
      ** Change in the GROUP BY are detected by the previous code
ds@1952
  3336
      ** block.  If there were no changes, this block is skipped.
ds@1952
  3337
      **
ds@1952
  3338
      ** This code copies current group by terms in b0,b1,b2,...
ds@1952
  3339
      ** over to a0,a1,a2.  It then calls the output subroutine
ds@1952
  3340
      ** and resets the aggregate accumulator registers in preparation
ds@1952
  3341
      ** for the next GROUP BY batch.
ds@1952
  3342
      */
ds@1952
  3343
      sqlite3VdbeResolveLabel(v, addrGroupByChange);
ds@1952
  3344
      for(j=0; j<pGroupBy->nExpr; j++){
ds@1952
  3345
        sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j);
ds@1952
  3346
      }
ds@1952
  3347
      sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
ds@1952
  3348
      VdbeComment((v, "# output one row"));
ds@1952
  3349
      sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd);
ds@1952
  3350
      VdbeComment((v, "# check abort flag"));
ds@1952
  3351
      sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset);
ds@1952
  3352
      VdbeComment((v, "# reset accumulator"));
ds@1952
  3353
ds@1952
  3354
      /* Update the aggregate accumulators based on the content of
ds@1952
  3355
      ** the current row
ds@1952
  3356
      */
ds@1952
  3357
      sqlite3VdbeResolveLabel(v, addrProcessRow);
ds@1952
  3358
      updateAccumulator(pParse, &sAggInfo);
ds@1952
  3359
      sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag);
ds@1952
  3360
      VdbeComment((v, "# indicate data in accumulator"));
ds@1952
  3361
ds@1952
  3362
      /* End of the loop
ds@1952
  3363
      */
ds@1952
  3364
      if( groupBySort ){
ds@1952
  3365
        sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
ds@1952
  3366
      }else{
ds@1952
  3367
        sqlite3WhereEnd(pWInfo);
ds@1952
  3368
        sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
ds@1952
  3369
      }
ds@1952
  3370
ds@1952
  3371
      /* Output the final row of result
ds@1952
  3372
      */
ds@1952
  3373
      sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow);
ds@1952
  3374
      VdbeComment((v, "# output final row"));
ds@1952
  3375
      
ds@1952
  3376
    } /* endif pGroupBy */
ds@1952
  3377
    else {
ds@1952
  3378
      /* This case runs if the aggregate has no GROUP BY clause.  The
ds@1952
  3379
      ** processing is much simpler since there is only a single row
ds@1952
  3380
      ** of output.
ds@1952
  3381
      */
ds@1952
  3382
      resetAccumulator(pParse, &sAggInfo);
ds@1952
  3383
      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
ds@1952
  3384
      if( pWInfo==0 ) goto select_end;
ds@1952
  3385
      updateAccumulator(pParse, &sAggInfo);
ds@1952
  3386
      sqlite3WhereEnd(pWInfo);
ds@1952
  3387
      finalizeAggFunctions(pParse, &sAggInfo);
ds@1952
  3388
      pOrderBy = 0;
ds@1952
  3389
      if( pHaving ){
ds@1952
  3390
        sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1);
ds@1952
  3391
      }
ds@1952
  3392
      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
ds@1952
  3393
                      eDest, iParm, addrEnd, addrEnd, aff);
ds@1952
  3394
    }
ds@1952
  3395
    sqlite3VdbeResolveLabel(v, addrEnd);
ds@1952
  3396
    
ds@1952
  3397
  } /* endif aggregate query */
ds@1952
  3398
ds@1952
  3399
  /* If there is an ORDER BY clause, then we need to sort the results
ds@1952
  3400
  ** and send them to the callback one by one.
ds@1952
  3401
  */
ds@1952
  3402
  if( pOrderBy ){
ds@1952
  3403
    generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
ds@1952
  3404
  }
ds@1952
  3405
ds@1952
  3406
#ifndef SQLITE_OMIT_SUBQUERY
ds@1952
  3407
  /* If this was a subquery, we have now converted the subquery into a
ds@1952
  3408
  ** temporary table.  So set the SrcList_item.isPopulated flag to prevent
ds@1952
  3409
  ** this subquery from being evaluated again and to force the use of
ds@1952
  3410
  ** the temporary table.
ds@1952
  3411
  */
ds@1952
  3412
  if( pParent ){
ds@1952
  3413
    assert( pParent->pSrc->nSrc>parentTab );
ds@1952
  3414
    assert( pParent->pSrc->a[parentTab].pSelect==p );
ds@1952
  3415
    pParent->pSrc->a[parentTab].isPopulated = 1;
ds@1952
  3416
  }
ds@1952
  3417
#endif
ds@1952
  3418
ds@1952
  3419
  /* Jump here to skip this query
ds@1952
  3420
  */
ds@1952
  3421
  sqlite3VdbeResolveLabel(v, iEnd);
ds@1952
  3422
ds@1952
  3423
  /* The SELECT was successfully coded.   Set the return code to 0
ds@1952
  3424
  ** to indicate no errors.
ds@1952
  3425
  */
ds@1952
  3426
  rc = 0;
ds@1952
  3427
ds@1952
  3428
  /* Control jumps to here if an error is encountered above, or upon
ds@1952
  3429
  ** successful coding of the SELECT.
ds@1952
  3430
  */
ds@1952
  3431
select_end:
ds@1952
  3432
ds@1952
  3433
  /* Identify column names if we will be using them in a callback.  This
ds@1952
  3434
  ** step is skipped if the output is going to some other destination.
ds@1952
  3435
  */
ds@1952
  3436
  if( rc==SQLITE_OK && eDest==SRT_Callback ){
ds@1952
  3437
    generateColumnNames(pParse, pTabList, pEList);
ds@1952
  3438
  }
ds@1952
  3439
ds@1952
  3440
  sqlite3_free(sAggInfo.aCol);
ds@1952
  3441
  sqlite3_free(sAggInfo.aFunc);
ds@1952
  3442
  return rc;
ds@1952
  3443
}
ds@1952
  3444
ds@1952
  3445
#if defined(SQLITE_DEBUG)
ds@1952
  3446
/*
ds@1952
  3447
*******************************************************************************
ds@1952
  3448
** The following code is used for testing and debugging only.  The code
ds@1952
  3449
** that follows does not appear in normal builds.
ds@1952
  3450
**
ds@1952
  3451
** These routines are used to print out the content of all or part of a 
ds@1952
  3452
** parse structures such as Select or Expr.  Such printouts are useful
ds@1952
  3453
** for helping to understand what is happening inside the code generator
ds@1952
  3454
** during the execution of complex SELECT statements.
ds@1952
  3455
**
ds@1952
  3456
** These routine are not called anywhere from within the normal
ds@1952
  3457
** code base.  Then are intended to be called from within the debugger
ds@1952
  3458
** or from temporary "printf" statements inserted for debugging.
ds@1952
  3459
*/
ds@1952
  3460
void sqlite3PrintExpr(Expr *p){
ds@1952
  3461
  if( p->token.z && p->token.n>0 ){
ds@1952
  3462
    sqlite3DebugPrintf("(%.*s", p->token.n, p->token.z);
ds@1952
  3463
  }else{
ds@1952
  3464
    sqlite3DebugPrintf("(%d", p->op);
ds@1952
  3465
  }
ds@1952
  3466
  if( p->pLeft ){
ds@1952
  3467
    sqlite3DebugPrintf(" ");
ds@1952
  3468
    sqlite3PrintExpr(p->pLeft);
ds@1952
  3469
  }
ds@1952
  3470
  if( p->pRight ){
ds@1952
  3471
    sqlite3DebugPrintf(" ");
ds@1952
  3472
    sqlite3PrintExpr(p->pRight);
ds@1952
  3473
  }
ds@1952
  3474
  sqlite3DebugPrintf(")");
ds@1952
  3475
}
ds@1952
  3476
void sqlite3PrintExprList(ExprList *pList){
ds@1952
  3477
  int i;
ds@1952
  3478
  for(i=0; i<pList->nExpr; i++){
ds@1952
  3479
    sqlite3PrintExpr(pList->a[i].pExpr);
ds@1952
  3480
    if( i<pList->nExpr-1 ){
ds@1952
  3481
      sqlite3DebugPrintf(", ");
ds@1952
  3482
    }
ds@1952
  3483
  }
ds@1952
  3484
}
ds@1952
  3485
void sqlite3PrintSelect(Select *p, int indent){
ds@1952
  3486
  sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
ds@1952
  3487
  sqlite3PrintExprList(p->pEList);
ds@1952
  3488
  sqlite3DebugPrintf("\n");
ds@1952
  3489
  if( p->pSrc ){
ds@1952
  3490
    char *zPrefix;
ds@1952
  3491
    int i;
ds@1952
  3492
    zPrefix = "FROM";
ds@1952
  3493
    for(i=0; i<p->pSrc->nSrc; i++){
ds@1952
  3494
      struct SrcList_item *pItem = &p->pSrc->a[i];
ds@1952
  3495
      sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
ds@1952
  3496
      zPrefix = "";
ds@1952
  3497
      if( pItem->pSelect ){
ds@1952
  3498
        sqlite3DebugPrintf("(\n");
ds@1952
  3499
        sqlite3PrintSelect(pItem->pSelect, indent+10);
ds@1952
  3500
        sqlite3DebugPrintf("%*s)", indent+8, "");
ds@1952
  3501
      }else if( pItem->zName ){
ds@1952
  3502
        sqlite3DebugPrintf("%s", pItem->zName);
ds@1952
  3503
      }
ds@1952
  3504
      if( pItem->pTab ){
ds@1952
  3505
        sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
ds@1952
  3506
      }
ds@1952
  3507
      if( pItem->zAlias ){
ds@1952
  3508
        sqlite3DebugPrintf(" AS %s", pItem->zAlias);
ds@1952
  3509
      }
ds@1952
  3510
      if( i<p->pSrc->nSrc-1 ){
ds@1952
  3511
        sqlite3DebugPrintf(",");
ds@1952
  3512
      }
ds@1952
  3513
      sqlite3DebugPrintf("\n");
ds@1952
  3514
    }
ds@1952
  3515
  }
ds@1952
  3516
  if( p->pWhere ){
ds@1952
  3517
    sqlite3DebugPrintf("%*s WHERE ", indent, "");
ds@1952
  3518
    sqlite3PrintExpr(p->pWhere);
ds@1952
  3519
    sqlite3DebugPrintf("\n");
ds@1952
  3520
  }
ds@1952
  3521
  if( p->pGroupBy ){
ds@1952
  3522
    sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
ds@1952
  3523
    sqlite3PrintExprList(p->pGroupBy);
ds@1952
  3524
    sqlite3DebugPrintf("\n");
ds@1952
  3525
  }
ds@1952
  3526
  if( p->pHaving ){
ds@1952
  3527
    sqlite3DebugPrintf("%*s HAVING ", indent, "");
ds@1952
  3528
    sqlite3PrintExpr(p->pHaving);
ds@1952
  3529
    sqlite3DebugPrintf("\n");
ds@1952
  3530
  }
ds@1952
  3531
  if( p->pOrderBy ){
ds@1952
  3532
    sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
ds@1952
  3533
    sqlite3PrintExprList(p->pOrderBy);
ds@1952
  3534
    sqlite3DebugPrintf("\n");
ds@1952
  3535
  }
ds@1952
  3536
}
ds@1952
  3537
/* End of the structure debug printing code
ds@1952
  3538
*****************************************************************************/
ds@1952
  3539
#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */