Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
select.c9064 linesDownload Raw Back to src
1/*2** 2001 September 153**4** The author disclaims copyright to this source code.  In place of5** a legal notice, here is a blessing:6**7**    May you do good and not evil.8**    May you find forgiveness for yourself and forgive others.9**    May you share freely, never taking more than you give.10**11*************************************************************************12** This file contains C code routines that are called by the parser13** to handle SELECT statements in SQLite.14*/15#include "sqliteInt.h"16 17/*18** An instance of the following object is used to record information about19** how to process the DISTINCT keyword, to simplify passing that information20** into the selectInnerLoop() routine.21*/22typedef struct DistinctCtx DistinctCtx;23struct DistinctCtx {24  u8 isTnct;      /* 0: Not distinct. 1: DISTICT  2: DISTINCT and ORDER BY */25  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */26  int tabTnct;    /* Ephemeral table used for DISTINCT processing */27  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */28};29 30/*31** An instance of the following object is used to record information about32** the ORDER BY (or GROUP BY) clause of query is being coded.33**34** The aDefer[] array is used by the sorter-references optimization. For35** example, assuming there is no index that can be used for the ORDER BY,36** for the query:37**38**     SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;39**40** it may be more efficient to add just the "a" values to the sorter, and41** retrieve the associated "bigblob" values directly from table t1 as the42** 10 smallest "a" values are extracted from the sorter.43**44** When the sorter-reference optimization is used, there is one entry in the45** aDefer[] array for each database table that may be read as values are46** extracted from the sorter.47*/48typedef struct SortCtx SortCtx;49struct SortCtx {50  ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */51  int nOBSat;           /* Number of ORDER BY terms satisfied by indices */52  int iECursor;         /* Cursor number for the sorter */53  int regReturn;        /* Register holding block-output return address */54  int labelBkOut;       /* Start label for the block-output subroutine */55  int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */56  int labelDone;        /* Jump here when done, ex: LIMIT reached */57  int labelOBLopt;      /* Jump here when sorter is full */58  u8 sortFlags;         /* Zero or more SORTFLAG_* bits */59#ifdef SQLITE_ENABLE_SORTER_REFERENCES60  u8 nDefer;            /* Number of valid entries in aDefer[] */61  struct DeferredCsr {62    Table *pTab;        /* Table definition */63    int iCsr;           /* Cursor number for table */64    int nKey;           /* Number of PK columns for table pTab (>=1) */65  } aDefer[4];66#endif67  struct RowLoadInfo *pDeferredRowLoad;  /* Deferred row loading info or NULL */68#ifdef SQLITE_ENABLE_STMT_SCANSTATUS69  int addrPush;         /* First instruction to push data into sorter */70  int addrPushEnd;      /* Last instruction that pushes data into sorter */71#endif72};73#define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */74 75/*76** Delete all the content of a Select structure.  Deallocate the structure77** itself depending on the value of bFree78**79** If bFree==1, call sqlite3DbFree() on the p object.80** If bFree==0, Leave the first Select object unfreed81*/82static void clearSelect(sqlite3 *db, Select *p, int bFree){83  assert( db!=0 );84  while( p ){85    Select *pPrior = p->pPrior;86    sqlite3ExprListDelete(db, p->pEList);87    sqlite3SrcListDelete(db, p->pSrc);88    sqlite3ExprDelete(db, p->pWhere);89    sqlite3ExprListDelete(db, p->pGroupBy);90    sqlite3ExprDelete(db, p->pHaving);91    sqlite3ExprListDelete(db, p->pOrderBy);92    sqlite3ExprDelete(db, p->pLimit);93    if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);94#ifndef SQLITE_OMIT_WINDOWFUNC95    if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){96      sqlite3WindowListDelete(db, p->pWinDefn);97    }98    while( p->pWin ){99      assert( p->pWin->ppThis==&p->pWin );100      sqlite3WindowUnlinkFromSelect(p->pWin);101    }102#endif103    if( bFree ) sqlite3DbNNFreeNN(db, p);104    p = pPrior;105    bFree = 1;106  }107}108 109/*110** Initialize a SelectDest structure.111*/112void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){113  pDest->eDest = (u8)eDest;114  pDest->iSDParm = iParm;115  pDest->iSDParm2 = 0;116  pDest->zAffSdst = 0;117  pDest->iSdst = 0;118  pDest->nSdst = 0;119}120 121 122/*123** Allocate a new Select structure and return a pointer to that124** structure.125*/126Select *sqlite3SelectNew(127  Parse *pParse,        /* Parsing context */128  ExprList *pEList,     /* which columns to include in the result */129  SrcList *pSrc,        /* the FROM clause -- which tables to scan */130  Expr *pWhere,         /* the WHERE clause */131  ExprList *pGroupBy,   /* the GROUP BY clause */132  Expr *pHaving,        /* the HAVING clause */133  ExprList *pOrderBy,   /* the ORDER BY clause */134  u32 selFlags,         /* Flag parameters, such as SF_Distinct */135  Expr *pLimit          /* LIMIT value.  NULL means not used */136){137  Select *pNew, *pAllocated;138  Select standin;139  pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );140  if( pNew==0 ){141    assert( pParse->db->mallocFailed );142    pNew = &standin;143  }144  if( pEList==0 ){145    pEList = sqlite3ExprListAppend(pParse, 0,146                                   sqlite3Expr(pParse->db,TK_ASTERISK,0));147  }148  pNew->pEList = pEList;149  pNew->op = TK_SELECT;150  pNew->selFlags = selFlags;151  pNew->iLimit = 0;152  pNew->iOffset = 0;153  pNew->selId = ++pParse->nSelect;154  pNew->addrOpenEphm[0] = -1;155  pNew->addrOpenEphm[1] = -1;156  pNew->nSelectRow = 0;157  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);158  pNew->pSrc = pSrc;159  pNew->pWhere = pWhere;160  pNew->pGroupBy = pGroupBy;161  pNew->pHaving = pHaving;162  pNew->pOrderBy = pOrderBy;163  pNew->pPrior = 0;164  pNew->pNext = 0;165  pNew->pLimit = pLimit;166  pNew->pWith = 0;167#ifndef SQLITE_OMIT_WINDOWFUNC168  pNew->pWin = 0;169  pNew->pWinDefn = 0;170#endif171  if( pParse->db->mallocFailed ) {172    clearSelect(pParse->db, pNew, pNew!=&standin);173    pAllocated = 0;174  }else{175    assert( pNew->pSrc!=0 || pParse->nErr>0 );176  }177  return pAllocated;178}179 180 181/*182** Delete the given Select structure and all of its substructures.183*/184void sqlite3SelectDelete(sqlite3 *db, Select *p){185  if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);186}187void sqlite3SelectDeleteGeneric(sqlite3 *db, void *p){188  if( ALWAYS(p) ) clearSelect(db, (Select*)p, 1);189}190 191/*192** Return a pointer to the right-most SELECT statement in a compound.193*/194static Select *findRightmost(Select *p){195  while( p->pNext ) p = p->pNext;196  return p;197}198 199/*200** Given 1 to 3 identifiers preceding the JOIN keyword, determine the201** type of join.  Return an integer constant that expresses that type202** in terms of the following bit values:203**204**     JT_INNER205**     JT_CROSS206**     JT_OUTER207**     JT_NATURAL208**     JT_LEFT209**     JT_RIGHT210**211** A full outer join is the combination of JT_LEFT and JT_RIGHT.212**213** If an illegal or unsupported join type is seen, then still return214** a join type, but put an error in the pParse structure.215**216** These are the valid join types:217**218**219**      pA       pB       pC               Return Value220**     -------  -----    -----             ------------221**     CROSS      -        -                 JT_CROSS222**     INNER      -        -                 JT_INNER223**     LEFT       -        -                 JT_LEFT|JT_OUTER224**     LEFT     OUTER      -                 JT_LEFT|JT_OUTER225**     RIGHT      -        -                 JT_RIGHT|JT_OUTER226**     RIGHT    OUTER      -                 JT_RIGHT|JT_OUTER227**     FULL       -        -                 JT_LEFT|JT_RIGHT|JT_OUTER228**     FULL     OUTER      -                 JT_LEFT|JT_RIGHT|JT_OUTER229**     NATURAL  INNER      -                 JT_NATURAL|JT_INNER230**     NATURAL  LEFT       -                 JT_NATURAL|JT_LEFT|JT_OUTER231**     NATURAL  LEFT     OUTER               JT_NATURAL|JT_LEFT|JT_OUTER232**     NATURAL  RIGHT      -                 JT_NATURAL|JT_RIGHT|JT_OUTER233**     NATURAL  RIGHT    OUTER               JT_NATURAL|JT_RIGHT|JT_OUTER234**     NATURAL  FULL       -                 JT_NATURAL|JT_LEFT|JT_RIGHT235**     NATURAL  FULL     OUTER               JT_NATRUAL|JT_LEFT|JT_RIGHT236**237** To preserve historical compatibly, SQLite also accepts a variety238** of other non-standard and in many cases nonsensical join types.239** This routine makes as much sense at it can from the nonsense join240** type and returns a result.  Examples of accepted nonsense join types241** include but are not limited to:242**243**          INNER CROSS JOIN        ->   same as JOIN244**          NATURAL CROSS JOIN      ->   same as NATURAL JOIN245**          OUTER LEFT JOIN         ->   same as LEFT JOIN246**          LEFT NATURAL JOIN       ->   same as NATURAL LEFT JOIN247**          LEFT RIGHT JOIN         ->   same as FULL JOIN248**          RIGHT OUTER FULL JOIN   ->   same as FULL JOIN249**          CROSS CROSS CROSS JOIN  ->   same as JOIN250**251** The only restrictions on the join type name are:252**253**    *   "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",254**        or "FULL".255**256**    *   "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,257**        or "FULL".258**259**    *   If "OUTER" is present then there must also be one of260**        "LEFT", "RIGHT", or "FULL"261*/262int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){263  int jointype = 0;264  Token *apAll[3];265  Token *p;266                             /*   0123456789 123456789 123456789 123 */267  static const char zKeyText[] = "naturaleftouterightfullinnercross";268  static const struct {269    u8 i;        /* Beginning of keyword text in zKeyText[] */270    u8 nChar;    /* Length of the keyword in characters */271    u8 code;     /* Join type mask */272  } aKeyword[] = {273    /* (0) natural */ { 0,  7, JT_NATURAL                },274    /* (1) left    */ { 6,  4, JT_LEFT|JT_OUTER          },275    /* (2) outer   */ { 10, 5, JT_OUTER                  },276    /* (3) right   */ { 14, 5, JT_RIGHT|JT_OUTER         },277    /* (4) full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },278    /* (5) inner   */ { 23, 5, JT_INNER                  },279    /* (6) cross   */ { 28, 5, JT_INNER|JT_CROSS         },280  };281  int i, j;282  apAll[0] = pA;283  apAll[1] = pB;284  apAll[2] = pC;285  for(i=0; i<3 && apAll[i]; i++){286    p = apAll[i];287    for(j=0; j<ArraySize(aKeyword); j++){288      if( p->n==aKeyword[j].nChar289          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){290        jointype |= aKeyword[j].code;291        break;292      }293    }294    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );295    if( j>=ArraySize(aKeyword) ){296      jointype |= JT_ERROR;297      break;298    }299  }300  if(301     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||302     (jointype & JT_ERROR)!=0 ||303     (jointype & (JT_OUTER|JT_LEFT|JT_RIGHT))==JT_OUTER304  ){305    const char *zSp1 = " ";306    const char *zSp2 = " ";307    if( pB==0 ){ zSp1++; }308    if( pC==0 ){ zSp2++; }309    sqlite3ErrorMsg(pParse, "unknown join type: "310       "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);311    jointype = JT_INNER;312  }313  return jointype;314}315 316/*317** Return the index of a column in a table.  Return -1 if the column318** is not contained in the table.319*/320int sqlite3ColumnIndex(Table *pTab, const char *zCol){321  int i;322  u8 h;323  const Column *aCol;324  int nCol;325 326  h = sqlite3StrIHash(zCol);327  aCol = pTab->aCol;328  nCol = pTab->nCol;329 330  /* See if the aHx gives us a lucky match */331  i = pTab->aHx[h % sizeof(pTab->aHx)];332  assert( i<nCol );333  if( aCol[i].hName==h334   && sqlite3StrICmp(aCol[i].zCnName, zCol)==0335  ){336    return i;337  }338 339  /* No lucky match from the hash table.  Do a full search. */340  i = 0;341  while( 1 /*exit-by-break*/ ){342    if( aCol[i].hName==h343     && sqlite3StrICmp(aCol[i].zCnName, zCol)==0344    ){345      return i;346    }347    i++;348    if( i>=nCol ) break;349  }350  return -1;351}352 353/*354** Mark a subquery result column as having been used.355*/356void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){357  assert( pItem!=0 );358  assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) );359  if( pItem->fg.isNestedFrom ){360    ExprList *pResults;361    assert( pItem->fg.isSubquery );362    assert( pItem->u4.pSubq!=0 );363    assert( pItem->u4.pSubq->pSelect!=0 );364    pResults = pItem->u4.pSubq->pSelect->pEList;365    assert( pResults!=0 );366    assert( iCol>=0 && iCol<pResults->nExpr );367    pResults->a[iCol].fg.bUsed = 1;368  }369}370 371/*372** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a373** table that has a column named zCol.  The search is left-to-right.374** The first match found is returned.375**376** When found, set *piTab and *piCol to the table index and column index377** of the matching column and return TRUE.378**379** If not found, return FALSE.380*/381static int tableAndColumnIndex(382  SrcList *pSrc,       /* Array of tables to search */383  int iStart,          /* First member of pSrc->a[] to check */384  int iEnd,            /* Last member of pSrc->a[] to check */385  const char *zCol,    /* Name of the column we are looking for */386  int *piTab,          /* Write index of pSrc->a[] here */387  int *piCol,          /* Write index of pSrc->a[*piTab].pSTab->aCol[] here */388  int bIgnoreHidden    /* Ignore hidden columns */389){390  int i;               /* For looping over tables in pSrc */391  int iCol;            /* Index of column matching zCol */392 393  assert( iEnd<pSrc->nSrc );394  assert( iStart>=0 );395  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */396 397  for(i=iStart; i<=iEnd; i++){398    iCol = sqlite3ColumnIndex(pSrc->a[i].pSTab, zCol);399    if( iCol>=0400     && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pSTab->aCol[iCol])==0)401    ){402      if( piTab ){403        sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);404        *piTab = i;405        *piCol = iCol;406      }407      return 1;408    }409  }410  return 0;411}412 413/*414** Set the EP_OuterON property on all terms of the given expression.415** And set the Expr.w.iJoin to iTable for every term in the416** expression.417**418** The EP_OuterON property is used on terms of an expression to tell419** the OUTER JOIN processing logic that this term is part of the420** join restriction specified in the ON or USING clause and not a part421** of the more general WHERE clause.  These terms are moved over to the422** WHERE clause during join processing but we need to remember that they423** originated in the ON or USING clause.424**425** The Expr.w.iJoin tells the WHERE clause processing that the426** expression depends on table w.iJoin even if that table is not427** explicitly mentioned in the expression.  That information is needed428** for cases like this:429**430**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5431**432** The where clause needs to defer the handling of the t1.x=5433** term until after the t2 loop of the join.  In that way, a434** NULL t2 row will be inserted whenever t1.x!=5.  If we do not435** defer the handling of t1.x=5, it will be processed immediately436** after the t1 loop and rows with t1.x!=5 will never appear in437** the output, which is incorrect.438*/439void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){440  assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON );441  while( p ){442    ExprSetProperty(p, joinFlag);443    assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );444    ExprSetVVAProperty(p, EP_NoReduce);445    p->w.iJoin = iTable;446    if( ExprUseXList(p) ){447      if( p->x.pList ){448        int i;449        for(i=0; i<p->x.pList->nExpr; i++){450          sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);451        }452      }453    }454    sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);455    p = p->pRight;456  }457}458 459/* Undo the work of sqlite3SetJoinExpr().  This is used when a LEFT JOIN460** is simplified into an ordinary JOIN, and when an ON expression is461** "pushed down" into the WHERE clause of a subquery.462**463** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into464** an ordinary term that omits the EP_OuterON mark.  Or if iTable<0, then465** just clear every EP_OuterON and EP_InnerON mark from the expression tree.466**467** If nullable is true, that means that Expr p might evaluate to NULL even468** if it is a reference to a NOT NULL column.  This can happen, for example,469** if the table that p references is on the left side of a RIGHT JOIN.470** If nullable is true, then take care to not remove the EP_CanBeNull bit.471** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c472*/473static void unsetJoinExpr(Expr *p, int iTable, int nullable){474  while( p ){475    if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){476      ExprClearProperty(p, EP_OuterON|EP_InnerON);477      if( iTable>=0 ) ExprSetProperty(p, EP_InnerON);478    }479    if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){480      ExprClearProperty(p, EP_CanBeNull);481    }482    if( p->op==TK_FUNCTION ){483      assert( ExprUseXList(p) );484      assert( p->pLeft==0 );485      if( p->x.pList ){486        int i;487        for(i=0; i<p->x.pList->nExpr; i++){488          unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);489        }490      }491    }492    unsetJoinExpr(p->pLeft, iTable, nullable);493    p = p->pRight;494  }495}496 497/*498** This routine processes the join information for a SELECT statement.499**500**   *  A NATURAL join is converted into a USING join.  After that, we501**      do not need to be concerned with NATURAL joins and we only have502**      think about USING joins.503**504**   *  ON and USING clauses result in extra terms being added to the505**      WHERE clause to enforce the specified constraints.  The extra506**      WHERE clause terms will be tagged with EP_OuterON or507**      EP_InnerON so that we know that they originated in ON/USING.508**509** The terms of a FROM clause are contained in the Select.pSrc structure.510** The left most table is the first entry in Select.pSrc.  The right-most511** table is the last entry.  The join operator is held in the entry to512** the right.  Thus entry 1 contains the join operator for the join between513** entries 0 and 1.  Any ON or USING clauses associated with the join are514** also attached to the right entry.515**516** This routine returns the number of errors encountered.517*/518static int sqlite3ProcessJoin(Parse *pParse, Select *p){519  SrcList *pSrc;                  /* All tables in the FROM clause */520  int i, j;                       /* Loop counters */521  SrcItem *pLeft;                 /* Left table being joined */522  SrcItem *pRight;                /* Right table being joined */523 524  pSrc = p->pSrc;525  pLeft = &pSrc->a[0];526  pRight = &pLeft[1];527  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){528    Table *pRightTab = pRight->pSTab;529    u32 joinType;530 531    if( NEVER(pLeft->pSTab==0 || pRightTab==0) ) continue;532    joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;533 534    /* If this is a NATURAL join, synthesize an appropriate USING clause535    ** to specify which columns should be joined.536    */537    if( pRight->fg.jointype & JT_NATURAL ){538      IdList *pUsing = 0;539      if( pRight->fg.isUsing || pRight->u3.pOn ){540        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "541           "an ON or USING clause", 0);542        return 1;543      }544      for(j=0; j<pRightTab->nCol; j++){545        char *zName;   /* Name of column in the right table */546 547        if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;548        zName = pRightTab->aCol[j].zCnName;549        if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){550          pUsing = sqlite3IdListAppend(pParse, pUsing, 0);551          if( pUsing ){552            assert( pUsing->nId>0 );553            assert( pUsing->a[pUsing->nId-1].zName==0 );554            pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);555          }556        }557      }558      if( pUsing ){559        pRight->fg.isUsing = 1;560        pRight->fg.isSynthUsing = 1;561        pRight->u3.pUsing = pUsing;562      }563      if( pParse->nErr ) return 1;564    }565 566    /* Create extra terms on the WHERE clause for each column named567    ** in the USING clause.  Example: If the two tables to be joined are568    ** A and B and the USING clause names X, Y, and Z, then add this569    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z570    ** Report an error if any column mentioned in the USING clause is571    ** not contained in both tables to be joined.572    */573    if( pRight->fg.isUsing ){574      IdList *pList = pRight->u3.pUsing;575      sqlite3 *db = pParse->db;576      assert( pList!=0 );577      for(j=0; j<pList->nId; j++){578        char *zName;     /* Name of the term in the USING clause */579        int iLeft;       /* Table on the left with matching column name */580        int iLeftCol;    /* Column number of matching column on the left */581        int iRightCol;   /* Column number of matching column on the right */582        Expr *pE1;       /* Reference to the column on the LEFT of the join */583        Expr *pE2;       /* Reference to the column on the RIGHT of the join */584        Expr *pEq;       /* Equality constraint.  pE1 == pE2 */585 586        zName = pList->a[j].zName;587        iRightCol = sqlite3ColumnIndex(pRightTab, zName);588        if( iRightCol<0589         || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol,590                                pRight->fg.isSynthUsing)==0591        ){592          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "593            "not present in both tables", zName);594          return 1;595        }596        pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);597        sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);598        if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 && pParse->nErr==0 ){599          /* This branch runs if the query contains one or more RIGHT or FULL600          ** JOINs.  If only a single table on the left side of this join601          ** contains the zName column, then this branch is a no-op.602          ** But if there are two or more tables on the left side603          ** of the join, construct a coalesce() function that gathers all604          ** such tables.  Raise an error if more than one of those references605          ** to zName is not also within a prior USING clause.606          **607          ** We really ought to raise an error if there are two or more608          ** non-USING references to zName on the left of an INNER or LEFT609          ** JOIN.  But older versions of SQLite do not do that, so we avoid610          ** adding a new error so as to not break legacy applications.611          */612          ExprList *pFuncArgs = 0;   /* Arguments to the coalesce() */613          static const Token tkCoalesce = { "coalesce", 8 };614          assert( pE1!=0 );615          ExprSetProperty(pE1, EP_CanBeNull);616          while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol,617                                     pRight->fg.isSynthUsing)!=0 ){618            if( pSrc->a[iLeft].fg.isUsing==0619             || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0620            ){621              sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()",622                              zName);623              break;624            }625            pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);626            pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);627            sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);628          }629          if( pFuncArgs ){630            pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);631            pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0);632            if( pE1 ){633              pE1->affExpr = SQLITE_AFF_DEFER;634            }635          }636        }else if( (pSrc->a[i+1].fg.jointype & JT_LEFT)!=0 && pParse->nErr==0 ){637          assert( pE1!=0 );638          ExprSetProperty(pE1, EP_CanBeNull);639        }640        pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol);641        sqlite3SrcItemColumnUsed(pRight, iRightCol);642        pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);643        assert( pE2!=0 || pEq==0 );644        if( pEq ){645          ExprSetProperty(pEq, joinType);646          assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );647          ExprSetVVAProperty(pEq, EP_NoReduce);648          pEq->w.iJoin = pE2->iTable;649        }650        p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);651      }652    }653 654    /* Add the ON clause to the end of the WHERE clause, connected by655    ** an AND operator.656    */657    else if( pRight->u3.pOn ){658      sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);659      p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);660      pRight->u3.pOn = 0;661      pRight->fg.isOn = 1;662      p->selFlags |= SF_OnToWhere;663    }664  }665  return 0;666}667 668/*669** An instance of this object holds information (beyond pParse and pSelect)670** needed to load the next result row that is to be added to the sorter.671*/672typedef struct RowLoadInfo RowLoadInfo;673struct RowLoadInfo {674  int regResult;               /* Store results in array of registers here */675  u8 ecelFlags;                /* Flag argument to ExprCodeExprList() */676#ifdef SQLITE_ENABLE_SORTER_REFERENCES677  ExprList *pExtra;            /* Extra columns needed by sorter refs */678  int regExtraResult;          /* Where to load the extra columns */679#endif680};681 682/*683** This routine does the work of loading query data into an array of684** registers so that it can be added to the sorter.685*/686static void innerLoopLoadRow(687  Parse *pParse,             /* Statement under construction */688  Select *pSelect,           /* The query being coded */689  RowLoadInfo *pInfo         /* Info needed to complete the row load */690){691  sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,692                          0, pInfo->ecelFlags);693#ifdef SQLITE_ENABLE_SORTER_REFERENCES694  if( pInfo->pExtra ){695    sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);696    sqlite3ExprListDelete(pParse->db, pInfo->pExtra);697  }698#endif699}700 701/*702** Code the OP_MakeRecord instruction that generates the entry to be703** added into the sorter.704**705** Return the register in which the result is stored.706*/707static int makeSorterRecord(708  Parse *pParse,709  SortCtx *pSort,710  Select *pSelect,711  int regBase,712  int nBase713){714  int nOBSat = pSort->nOBSat;715  Vdbe *v = pParse->pVdbe;716  int regOut = ++pParse->nMem;717  if( pSort->pDeferredRowLoad ){718    innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);719  }720  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);721  return regOut;722}723 724/*725** Generate code that will push the record in registers regData726** through regData+nData-1 onto the sorter.727*/728static void pushOntoSorter(729  Parse *pParse,         /* Parser context */730  SortCtx *pSort,        /* Information about the ORDER BY clause */731  Select *pSelect,       /* The whole SELECT statement */732  int regData,           /* First register holding data to be sorted */733  int regOrigData,       /* First register holding data before packing */734  int nData,             /* Number of elements in the regData data array */735  int nPrefixReg         /* No. of reg prior to regData available for use */736){737  Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */738  int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);739  int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */740  int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */741  int regBase;                                     /* Regs for sorter record */742  int regRecord = 0;                               /* Assembled sorter record */743  int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */744  int op;                            /* Opcode to add sorter record to sorter */745  int iLimit;                        /* LIMIT counter */746  int iSkip = 0;                     /* End of the sorter insert loop */747 748  assert( bSeq==0 || bSeq==1 );749 750  /* Three cases:751  **   (1) The data to be sorted has already been packed into a Record752  **       by a prior OP_MakeRecord.  In this case nData==1 and regData753  **       will be completely unrelated to regOrigData.754  **   (2) All output columns are included in the sort record.  In that755  **       case regData==regOrigData.756  **   (3) Some output columns are omitted from the sort record due to757  **       the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the758  **       SQLITE_ECEL_OMITREF optimization, or due to the759  **       SortCtx.pDeferredRowLoad optimization.  In any of these cases760  **       regOrigData is 0 to prevent this routine from trying to copy761  **       values that might not yet exist.762  */763  assert( nData==1 || regData==regOrigData || regOrigData==0 );764 765#ifdef SQLITE_ENABLE_STMT_SCANSTATUS766  pSort->addrPush = sqlite3VdbeCurrentAddr(v);767#endif768 769  if( nPrefixReg ){770    assert( nPrefixReg==nExpr+bSeq );771    regBase = regData - nPrefixReg;772  }else{773    regBase = pParse->nMem + 1;774    pParse->nMem += nBase;775  }776  assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );777  iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;778  pSort->labelDone = sqlite3VdbeMakeLabel(pParse);779  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,780                          SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));781  if( bSeq ){782    sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);783  }784  if( nPrefixReg==0 && nData>0 ){785    sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);786  }787  if( nOBSat>0 ){788    int regPrevKey;   /* The first nOBSat columns of the previous row */789    int addrFirst;    /* Address of the OP_IfNot opcode */790    int addrJmp;      /* Address of the OP_Jump opcode */791    VdbeOp *pOp;      /* Opcode that opens the sorter */792    int nKey;         /* Number of sorting key columns, including OP_Sequence */793    KeyInfo *pKI;     /* Original KeyInfo on the sorter table */794 795    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);796    regPrevKey = pParse->nMem+1;797    pParse->nMem += pSort->nOBSat;798    nKey = nExpr - pSort->nOBSat + bSeq;799    if( bSeq ){800      addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);801    }else{802      addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);803    }804    VdbeCoverage(v);805    sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);806    pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);807    if( pParse->db->mallocFailed ) return;808    pOp->p2 = nKey + nData;809    pKI = pOp->p4.pKeyInfo;810    memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */811    sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);812    testcase( pKI->nAllField > pKI->nKeyField+2 );813    pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,814                                           pKI->nAllField-pKI->nKeyField-1);815    pOp = 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */816    addrJmp = sqlite3VdbeCurrentAddr(v);817    sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);818    pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);819    pSort->regReturn = ++pParse->nMem;820    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);821    sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);822    if( iLimit ){823      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);824      VdbeCoverage(v);825    }826    sqlite3VdbeJumpHere(v, addrFirst);827    sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);828    sqlite3VdbeJumpHere(v, addrJmp);829  }830  if( iLimit ){831    /* At this point the values for the new sorter entry are stored832    ** in an array of registers. They need to be composed into a record833    ** and inserted into the sorter if either (a) there are currently834    ** less than LIMIT+OFFSET items or (b) the new record is smaller than835    ** the largest record currently in the sorter. If (b) is true and there836    ** are already LIMIT+OFFSET items in the sorter, delete the largest837    ** entry before inserting the new one. This way there are never more838    ** than LIMIT+OFFSET items in the sorter.839    **840    ** If the new record does not need to be inserted into the sorter,841    ** jump to the next iteration of the loop. If the pSort->labelOBLopt842    ** value is not zero, then it is a label of where to jump.  Otherwise,843    ** just bypass the row insert logic.  See the header comment on the844    ** sqlite3WhereOrderByLimitOptLabel() function for additional info.845    */846    int iCsr = pSort->iECursor;847    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);848    VdbeCoverage(v);849    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);850    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,851                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);852    VdbeCoverage(v);853    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);854  }855  if( regRecord==0 ){856    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);857  }858  if( pSort->sortFlags & SORTFLAG_UseSorter ){859    op = OP_SorterInsert;860  }else{861    op = OP_IdxInsert;862  }863  sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,864                       regBase+nOBSat, nBase-nOBSat);865  if( iSkip ){866    sqlite3VdbeChangeP2(v, iSkip,867         pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));868  }869#ifdef SQLITE_ENABLE_STMT_SCANSTATUS870  pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;871#endif872}873 874/*875** Add code to implement the OFFSET876*/877static void codeOffset(878  Vdbe *v,          /* Generate code into this VM */879  int iOffset,      /* Register holding the offset counter */880  int iContinue     /* Jump here to skip the current record */881){882  if( iOffset>0 ){883    sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);884    VdbeComment((v, "OFFSET"));885  }886}887 888/*889** Add code that will check to make sure the array of registers starting at890** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and891** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies892** are available. Which is used depends on the value of parameter eTnctType,893** as follows:894**895**   WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:896**     Build an ephemeral table that contains all entries seen before and897**     skip entries which have been seen before.898**899**     Parameter iTab is the cursor number of an ephemeral table that must900**     be opened before the VM code generated by this routine is executed.901**     The ephemeral cursor table is queried for a record identical to the902**     record formed by the current array of registers. If one is found,903**     jump to VM address addrRepeat. Otherwise, insert a new record into904**     the ephemeral cursor and proceed.905**906**     The returned value in this case is a copy of parameter iTab.907**908**   WHERE_DISTINCT_ORDERED:909**     In this case rows are being delivered sorted order. The ephemeral910**     table is not required. Instead, the current set of values911**     is compared against previous row. If they match, the new row912**     is not distinct and control jumps to VM address addrRepeat. Otherwise,913**     the VM program proceeds with processing the new row.914**915**     The returned value in this case is the register number of the first916**     in an array of registers used to store the previous result row so that917**     it can be compared to the next. The caller must ensure that this918**     register is initialized to NULL.  (The fixDistinctOpenEph() routine919**     will take care of this initialization.)920**921**   WHERE_DISTINCT_UNIQUE:922**     In this case it has already been determined that the rows are distinct.923**     No special action is required. The return value is zero.924**925** Parameter pEList is the list of expressions used to generated the926** contents of each row. It is used by this routine to determine (a)927** how many elements there are in the array of registers and (b) the928** collation sequences that should be used for the comparisons if929** eTnctType is WHERE_DISTINCT_ORDERED.930*/931static int codeDistinct(932  Parse *pParse,     /* Parsing and code generating context */933  int eTnctType,     /* WHERE_DISTINCT_* value */934  int iTab,          /* A sorting index used to test for distinctness */935  int addrRepeat,    /* Jump to here if not distinct */936  ExprList *pEList,  /* Expression for each element */937  int regElem        /* First element */938){939  int iRet = 0;940  int nResultCol = pEList->nExpr;941  Vdbe *v = pParse->pVdbe;942 943  switch( eTnctType ){944    case WHERE_DISTINCT_ORDERED: {945      int i;946      int iJump;              /* Jump destination */947      int regPrev;            /* Previous row content */948 949      /* Allocate space for the previous row */950      iRet = regPrev = pParse->nMem+1;951      pParse->nMem += nResultCol;952 953      iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;954      for(i=0; i<nResultCol; i++){955        CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);956        if( i<nResultCol-1 ){957          sqlite3VdbeAddOp3(v, OP_Ne, regElem+i, iJump, regPrev+i);958          VdbeCoverage(v);959        }else{960          sqlite3VdbeAddOp3(v, OP_Eq, regElem+i, addrRepeat, regPrev+i);961          VdbeCoverage(v);962         }963        sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);964        sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);965      }966      assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );967      sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);968      break;969    }970 971    case WHERE_DISTINCT_UNIQUE: {972      /* nothing to do */973      break;974    }975 976    default: {977      int r1 = sqlite3GetTempReg(pParse);978      sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, regElem, nResultCol);979      VdbeCoverage(v);980      sqlite3VdbeAddOp3(v, OP_MakeRecord, regElem, nResultCol, r1);981      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, regElem, nResultCol);982      sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);983      sqlite3ReleaseTempReg(pParse, r1);984      iRet = iTab;985      break;986    }987  }988 989  return iRet;990}991 992/*993** This routine runs after codeDistinct().  It makes necessary994** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()995** routine made use of.  This processing must be done separately since996** sometimes codeDistinct is called before the OP_OpenEphemeral is actually997** laid down.998**999** WHERE_DISTINCT_NOOP:1000** WHERE_DISTINCT_UNORDERED:1001**1002**     No adjustments necessary.  This function is a no-op.1003**1004** WHERE_DISTINCT_UNIQUE:1005**1006**     The ephemeral table is not needed.  So change the1007**     OP_OpenEphemeral opcode into an OP_Noop.1008**1009** WHERE_DISTINCT_ORDERED:1010**1011**     The ephemeral table is not needed.  But we do need register1012**     iVal to be initialized to NULL.  So change the OP_OpenEphemeral1013**     into an OP_Null on the iVal register.1014*/1015static void fixDistinctOpenEph(1016  Parse *pParse,     /* Parsing and code generating context */1017  int eTnctType,     /* WHERE_DISTINCT_* value */1018  int iVal,          /* Value returned by codeDistinct() */1019  int iOpenEphAddr   /* Address of OP_OpenEphemeral instruction for iTab */1020){1021  if( pParse->nErr==01022   && (eTnctType==WHERE_DISTINCT_UNIQUE || eTnctType==WHERE_DISTINCT_ORDERED)1023  ){1024    Vdbe *v = pParse->pVdbe;1025    sqlite3VdbeChangeToNoop(v, iOpenEphAddr);1026    if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){1027      sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1);1028    }1029    if( eTnctType==WHERE_DISTINCT_ORDERED ){1030      /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared1031      ** bit on the first register of the previous value.  This will cause the1032      ** OP_Ne added in codeDistinct() to always fail on the first iteration of1033      ** the loop even if the first row is all NULLs.  */1034      VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr);1035      pOp->opcode = OP_Null;1036      pOp->p1 = 1;1037      pOp->p2 = iVal;1038    }1039  }1040}1041 1042#ifdef SQLITE_ENABLE_SORTER_REFERENCES1043/*1044** This function is called as part of inner-loop generation for a SELECT1045** statement with an ORDER BY that is not optimized by an index. It1046** determines the expressions, if any, that the sorter-reference1047** optimization should be used for. The sorter-reference optimization1048** is used for SELECT queries like:1049**1050**   SELECT a, bigblob FROM t1 ORDER BY a LIMIT 101051**1052** If the optimization is used for expression "bigblob", then instead of1053** storing values read from that column in the sorter records, the PK of1054** the row from table t1 is stored instead. Then, as records are extracted from1055** the sorter to return to the user, the required value of bigblob is1056** retrieved directly from table t1. If the values are very large, this1057** can be more efficient than storing them directly in the sorter records.1058**1059** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList1060** for which the sorter-reference optimization should be enabled.1061** Additionally, the pSort->aDefer[] array is populated with entries1062** for all cursors required to evaluate all selected expressions. Finally.1063** output variable (*ppExtra) is set to an expression list containing1064** expressions for all extra PK values that should be stored in the1065** sorter records.1066*/1067static void selectExprDefer(1068  Parse *pParse,                  /* Leave any error here */1069  SortCtx *pSort,                 /* Sorter context */1070  ExprList *pEList,               /* Expressions destined for sorter */1071  ExprList **ppExtra              /* Expressions to append to sorter record */1072){1073  int i;1074  int nDefer = 0;1075  ExprList *pExtra = 0;1076  for(i=0; i<pEList->nExpr; i++){1077    struct ExprList_item *pItem = &pEList->a[i];1078    if( pItem->u.x.iOrderByCol==0 ){1079      Expr *pExpr = pItem->pExpr;1080      Table *pTab;1081      if( pExpr->op==TK_COLUMN1082       && pExpr->iColumn>=01083       && ALWAYS( ExprUseYTab(pExpr) )1084       && (pTab = pExpr->y.pTab)!=01085       && IsOrdinaryTable(pTab)1086       && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=01087      ){1088        int j;1089        for(j=0; j<nDefer; j++){1090          if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;1091        }1092        if( j==nDefer ){1093          if( nDefer==ArraySize(pSort->aDefer) ){1094            continue;1095          }else{1096            int nKey = 1;1097            int k;1098            Index *pPk = 0;1099            if( !HasRowid(pTab) ){1100              pPk = sqlite3PrimaryKeyIndex(pTab);1101              nKey = pPk->nKeyCol;1102            }1103            for(k=0; k<nKey; k++){1104              Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);1105              if( pNew ){1106                pNew->iTable = pExpr->iTable;1107                assert( ExprUseYTab(pNew) );1108                pNew->y.pTab = pExpr->y.pTab;1109                pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;1110                pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);1111              }1112            }1113            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;1114            pSort->aDefer[nDefer].iCsr = pExpr->iTable;1115            pSort->aDefer[nDefer].nKey = nKey;1116            nDefer++;1117          }1118        }1119        pItem->fg.bSorterRef = 1;1120      }1121    }1122  }1123  pSort->nDefer = (u8)nDefer;1124  *ppExtra = pExtra;1125}1126#endif1127 1128/*1129** This routine generates the code for the inside of the inner loop1130** of a SELECT.1131**1132** If srcTab is negative, then the p->pEList expressions1133** are evaluated in order to get the data for this row.  If srcTab is1134** zero or more, then data is pulled from srcTab and p->pEList is used only1135** to get the number of columns and the collation sequence for each column.1136*/1137static void selectInnerLoop(1138  Parse *pParse,          /* The parser context */1139  Select *p,              /* The complete select statement being coded */1140  int srcTab,             /* Pull data from this table if non-negative */1141  SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */1142  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */1143  SelectDest *pDest,      /* How to dispose of the results */1144  int iContinue,          /* Jump here to continue with next row */1145  int iBreak              /* Jump here to break out of the inner loop */1146){1147  Vdbe *v = pParse->pVdbe;1148  int i;1149  int hasDistinct;            /* True if the DISTINCT keyword is present */1150  int eDest = pDest->eDest;   /* How to dispose of results */1151  int iParm = pDest->iSDParm; /* First argument to disposal method */1152  int nResultCol;             /* Number of result columns */1153  int nPrefixReg = 0;         /* Number of extra registers before regResult */1154  RowLoadInfo sRowLoadInfo;   /* Info for deferred row loading */1155 1156  /* Usually, regResult is the first cell in an array of memory cells1157  ** containing the current result row. In this case regOrig is set to the1158  ** same value. However, if the results are being sent to the sorter, the1159  ** values for any expressions that are also part of the sort-key are omitted1160  ** from this array. In this case regOrig is set to zero.  */1161  int regResult;              /* Start of memory holding current results */1162  int regOrig;                /* Start of memory holding full result (or 0) */1163 1164  assert( v );1165  assert( p->pEList!=0 );1166  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;1167  if( pSort && pSort->pOrderBy==0 ) pSort = 0;1168  if( pSort==0 && !hasDistinct ){1169    assert( iContinue!=0 );1170    codeOffset(v, p->iOffset, iContinue);1171  }1172 1173  /* Pull the requested columns.1174  */1175  nResultCol = p->pEList->nExpr;1176 1177  if( pDest->iSdst==0 ){1178    if( pSort ){1179      nPrefixReg = pSort->pOrderBy->nExpr;1180      if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;1181      pParse->nMem += nPrefixReg;1182    }1183    pDest->iSdst = pParse->nMem+1;1184    pParse->nMem += nResultCol;1185  }else if( pDest->iSdst+nResultCol > pParse->nMem ){1186    /* This is an error condition that can result, for example, when a SELECT1187    ** on the right-hand side of an INSERT contains more result columns than1188    ** there are columns in the table on the left.  The error will be caught1189    ** and reported later.  But we need to make sure enough memory is allocated1190    ** to avoid other spurious errors in the meantime. */1191    pParse->nMem += nResultCol;1192  }1193  pDest->nSdst = nResultCol;1194  regOrig = regResult = pDest->iSdst;1195  if( srcTab>=0 ){1196    for(i=0; i<nResultCol; i++){1197      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);1198      VdbeComment((v, "%s", p->pEList->a[i].zEName));1199    }1200  }else if( eDest!=SRT_Exists ){

Showing the first 1,200 of 9064 lines. Download the file for the rest.