2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2001-09-16 04:13:26 +04:00
|
|
|
** 2001 September 15
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
** This module contains C code that generates VDBE code used to process
|
2008-05-30 18:58:37 +04:00
|
|
|
** the WHERE clause of SQL statements. This module is responsible for
|
2004-12-18 21:40:26 +03:00
|
|
|
** generating the code that loops through a table looking for applicable
|
|
|
|
** rows. Indices are selected and used to speed the search when doing
|
|
|
|
** so is applicable. Because this module is responsible for selecting
|
|
|
|
** indices, you might also think of this module as the "query optimizer".
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2008-06-26 22:04:03 +04:00
|
|
|
** $Id: where.c,v 1.311 2008/06/26 18:04:03 danielk1977 Exp $
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
|
|
|
|
2005-07-16 17:33:20 +04:00
|
|
|
/*
|
|
|
|
** The number of bits in a Bitmask. "BMS" means "BitMask Size".
|
|
|
|
*/
|
2005-07-21 22:23:20 +04:00
|
|
|
#define BMS (sizeof(Bitmask)*8)
|
2005-07-16 17:33:20 +04:00
|
|
|
|
2005-07-24 02:59:55 +04:00
|
|
|
/*
|
|
|
|
** Trace output macros
|
|
|
|
*/
|
|
|
|
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
|
2008-03-04 20:45:01 +03:00
|
|
|
int sqlite3WhereTrace = 0;
|
|
|
|
# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
|
2005-07-24 02:59:55 +04:00
|
|
|
#else
|
2007-03-27 02:05:01 +04:00
|
|
|
# define WHERETRACE(X)
|
2005-07-24 02:59:55 +04:00
|
|
|
#endif
|
|
|
|
|
2005-07-19 21:38:22 +04:00
|
|
|
/* Forward reference
|
|
|
|
*/
|
|
|
|
typedef struct WhereClause WhereClause;
|
2007-02-06 16:26:32 +03:00
|
|
|
typedef struct ExprMaskSet ExprMaskSet;
|
2005-07-16 17:33:20 +04:00
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** The query generator uses an array of instances of this structure to
|
|
|
|
** help it analyze the subexpressions of the WHERE clause. Each WHERE
|
|
|
|
** clause subexpression is separated from the others by an AND operator.
|
2004-12-18 21:40:26 +03:00
|
|
|
**
|
2005-07-19 21:38:22 +04:00
|
|
|
** All WhereTerms are collected into a single WhereClause structure.
|
|
|
|
** The following identity holds:
|
|
|
|
**
|
|
|
|
** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
|
|
|
|
**
|
|
|
|
** When a term is of the form:
|
2004-12-18 21:40:26 +03:00
|
|
|
**
|
2005-07-19 21:38:22 +04:00
|
|
|
** X <op> <expr>
|
2004-12-18 21:40:26 +03:00
|
|
|
**
|
2005-07-19 21:38:22 +04:00
|
|
|
** where X is a column name and <op> is one of certain operators,
|
|
|
|
** then WhereTerm.leftCursor and WhereTerm.leftColumn record the
|
2005-07-24 02:59:55 +04:00
|
|
|
** cursor number and column number for X. WhereTerm.operator records
|
|
|
|
** the <op> using a bitmask encoding defined by WO_xxx below. The
|
|
|
|
** use of a bitmask encoding for the operator allows us to search
|
|
|
|
** quickly for terms that match any of several different operators.
|
2005-07-19 21:38:22 +04:00
|
|
|
**
|
|
|
|
** prereqRight and prereqAll record sets of cursor numbers,
|
2004-12-18 21:40:26 +03:00
|
|
|
** but they do so indirectly. A single ExprMaskSet structure translates
|
|
|
|
** cursor number into bits and the translated bit is stored in the prereq
|
|
|
|
** fields. The translation is used in order to maximize the number of
|
|
|
|
** bits that will fit in a Bitmask. The VDBE cursor numbers might be
|
|
|
|
** spread out over the non-negative integers. For example, the cursor
|
|
|
|
** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The ExprMaskSet
|
|
|
|
** translates these sparse cursor numbers into consecutive integers
|
|
|
|
** beginning with 0 in order to make the best possible use of the available
|
|
|
|
** bits in the Bitmask. So, in the example above, the cursor numbers
|
|
|
|
** would be mapped into integers 0 through 7.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-07-16 17:33:20 +04:00
|
|
|
typedef struct WhereTerm WhereTerm;
|
|
|
|
struct WhereTerm {
|
2005-07-19 21:38:22 +04:00
|
|
|
Expr *pExpr; /* Pointer to the subexpression */
|
2005-08-02 21:48:22 +04:00
|
|
|
i16 iParent; /* Disable pWC->a[iParent] when this term disabled */
|
2005-07-19 21:38:22 +04:00
|
|
|
i16 leftCursor; /* Cursor number of X in "X <op> <expr>" */
|
|
|
|
i16 leftColumn; /* Column number of X in "X <op> <expr>" */
|
2006-01-23 16:22:09 +03:00
|
|
|
u16 eOperator; /* A WO_xx value describing <op> */
|
2005-07-29 19:10:17 +04:00
|
|
|
u8 flags; /* Bit flags. See below */
|
2005-08-02 21:48:22 +04:00
|
|
|
u8 nChild; /* Number of children that must disable us */
|
2005-07-19 21:38:22 +04:00
|
|
|
WhereClause *pWC; /* The clause this term is part of */
|
|
|
|
Bitmask prereqRight; /* Bitmask of tables used by pRight */
|
2004-12-18 21:40:26 +03:00
|
|
|
Bitmask prereqAll; /* Bitmask of tables referenced by p */
|
2000-05-29 18:26:00 +04:00
|
|
|
};
|
|
|
|
|
2005-07-16 17:33:20 +04:00
|
|
|
/*
|
|
|
|
** Allowed values of WhereTerm.flags
|
|
|
|
*/
|
2005-07-29 19:10:17 +04:00
|
|
|
#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(pExpr) */
|
|
|
|
#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
|
|
|
|
#define TERM_CODED 0x04 /* This term is already coded */
|
2005-08-02 21:48:22 +04:00
|
|
|
#define TERM_COPIED 0x08 /* Has a child */
|
2005-07-29 19:10:17 +04:00
|
|
|
#define TERM_OR_OK 0x10 /* Used during OR-clause processing */
|
2005-07-16 17:33:20 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure holds all information about a
|
|
|
|
** WHERE clause. Mostly this is a container for one or more WhereTerms.
|
|
|
|
*/
|
|
|
|
struct WhereClause {
|
2005-07-21 07:14:59 +04:00
|
|
|
Parse *pParse; /* The parser context */
|
2007-02-06 16:26:32 +03:00
|
|
|
ExprMaskSet *pMaskSet; /* Mapping of table indices to bitmasks */
|
2005-07-16 17:33:20 +04:00
|
|
|
int nTerm; /* Number of terms */
|
|
|
|
int nSlot; /* Number of entries in a[] */
|
2005-07-24 02:59:55 +04:00
|
|
|
WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
|
|
|
|
WhereTerm aStatic[10]; /* Initial static space for a[] */
|
2005-07-22 04:31:39 +04:00
|
|
|
};
|
|
|
|
|
2003-05-02 18:32:12 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following structure keeps track of a mapping
|
2005-07-16 17:33:20 +04:00
|
|
|
** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
|
2004-12-18 21:40:26 +03:00
|
|
|
**
|
|
|
|
** The VDBE cursor numbers are small integers contained in
|
|
|
|
** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
|
|
|
|
** clause, the cursor numbers might not begin with 0 and they might
|
|
|
|
** contain gaps in the numbering sequence. But we want to make maximum
|
|
|
|
** use of the bits in our bitmasks. This structure provides a mapping
|
|
|
|
** from the sparse cursor numbers into consecutive integers beginning
|
|
|
|
** with 0.
|
|
|
|
**
|
|
|
|
** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
|
|
|
|
** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
|
|
|
|
**
|
|
|
|
** For example, if the WHERE clause expression used these VDBE
|
|
|
|
** cursors: 4, 5, 8, 29, 57, 73. Then the ExprMaskSet structure
|
|
|
|
** would map those cursor numbers into bits 0 through 5.
|
|
|
|
**
|
|
|
|
** Note that the mapping is not necessarily ordered. In the example
|
|
|
|
** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
|
|
|
|
** 57->5, 73->4. Or one of 719 other combinations might be used. It
|
|
|
|
** does not really matter. What is important is that sparse cursor
|
|
|
|
** numbers all get mapped into bit numbers that begin with 0 and contain
|
|
|
|
** no gaps.
|
2003-05-02 18:32:12 +04:00
|
|
|
*/
|
|
|
|
struct ExprMaskSet {
|
2005-01-20 02:24:50 +03:00
|
|
|
int n; /* Number of assigned cursor values */
|
|
|
|
int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */
|
2003-05-02 18:32:12 +04:00
|
|
|
};
|
|
|
|
|
2005-07-16 17:33:20 +04:00
|
|
|
|
2005-07-24 02:59:55 +04:00
|
|
|
/*
|
|
|
|
** Bitmasks for the operators that indices are able to exploit. An
|
|
|
|
** OR-ed combination of these values can be used when searching for
|
|
|
|
** terms in the where clause.
|
|
|
|
*/
|
|
|
|
#define WO_IN 1
|
2005-07-29 00:51:19 +04:00
|
|
|
#define WO_EQ 2
|
2005-07-24 02:59:55 +04:00
|
|
|
#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
|
|
|
|
#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
|
|
|
|
#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
|
|
|
|
#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
|
2006-06-13 21:38:59 +04:00
|
|
|
#define WO_MATCH 64
|
2006-10-28 04:28:09 +04:00
|
|
|
#define WO_ISNULL 128
|
2005-07-24 02:59:55 +04:00
|
|
|
|
|
|
|
/*
|
2007-01-25 19:56:06 +03:00
|
|
|
** Value for flags returned by bestIndex().
|
|
|
|
**
|
|
|
|
** The least significant byte is reserved as a mask for WO_ values above.
|
|
|
|
** The WhereLevel.flags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
|
|
|
|
** But if the table is the right table of a left join, WhereLevel.flags
|
|
|
|
** is set to WO_IN|WO_EQ. The WhereLevel.flags field can then be used as
|
|
|
|
** the "op" parameter to findTerm when we are resolving equality constraints.
|
|
|
|
** ISNULL constraints will then not be used on the right table of a left
|
|
|
|
** join. Tickets #2177 and #2189.
|
2005-07-24 02:59:55 +04:00
|
|
|
*/
|
2007-01-25 19:56:06 +03:00
|
|
|
#define WHERE_ROWID_EQ 0x000100 /* rowid=EXPR or rowid IN (...) */
|
|
|
|
#define WHERE_ROWID_RANGE 0x000200 /* rowid<EXPR and/or rowid>EXPR */
|
|
|
|
#define WHERE_COLUMN_EQ 0x001000 /* x=EXPR or x IN (...) */
|
|
|
|
#define WHERE_COLUMN_RANGE 0x002000 /* x<EXPR and/or x>EXPR */
|
|
|
|
#define WHERE_COLUMN_IN 0x004000 /* x IN (...) */
|
|
|
|
#define WHERE_TOP_LIMIT 0x010000 /* x<EXPR or x<=EXPR constraint */
|
|
|
|
#define WHERE_BTM_LIMIT 0x020000 /* x>EXPR or x>=EXPR constraint */
|
|
|
|
#define WHERE_IDX_ONLY 0x080000 /* Use index only - omit table */
|
|
|
|
#define WHERE_ORDERBY 0x100000 /* Output will appear in correct order */
|
|
|
|
#define WHERE_REVERSE 0x200000 /* Scan in reverse order */
|
|
|
|
#define WHERE_UNIQUE 0x400000 /* Selects no more than one row */
|
|
|
|
#define WHERE_VIRTUALTABLE 0x800000 /* Use virtual-table processing */
|
2005-07-24 02:59:55 +04:00
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2005-07-16 17:33:20 +04:00
|
|
|
** Initialize a preallocated WhereClause structure.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2007-02-06 16:26:32 +03:00
|
|
|
static void whereClauseInit(
|
|
|
|
WhereClause *pWC, /* The WhereClause to be initialized */
|
|
|
|
Parse *pParse, /* The parsing context */
|
|
|
|
ExprMaskSet *pMaskSet /* Mapping from table indices to bitmasks */
|
|
|
|
){
|
2005-07-21 07:14:59 +04:00
|
|
|
pWC->pParse = pParse;
|
2007-02-06 16:26:32 +03:00
|
|
|
pWC->pMaskSet = pMaskSet;
|
2005-07-16 17:33:20 +04:00
|
|
|
pWC->nTerm = 0;
|
2007-04-20 16:22:01 +04:00
|
|
|
pWC->nSlot = ArraySize(pWC->aStatic);
|
2005-07-16 17:33:20 +04:00
|
|
|
pWC->a = pWC->aStatic;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Deallocate a WhereClause structure. The WhereClause structure
|
|
|
|
** itself is not freed. This routine is the inverse of whereClauseInit().
|
|
|
|
*/
|
|
|
|
static void whereClauseClear(WhereClause *pWC){
|
|
|
|
int i;
|
|
|
|
WhereTerm *a;
|
|
|
|
for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
|
|
|
|
if( a->flags & TERM_DYNAMIC ){
|
2005-07-19 21:38:22 +04:00
|
|
|
sqlite3ExprDelete(a->pExpr);
|
2005-07-16 17:33:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( pWC->a!=pWC->aStatic ){
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(pWC->a);
|
2005-07-16 17:33:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Add a new entries to the WhereClause structure. Increase the allocated
|
|
|
|
** space as necessary.
|
2005-08-24 07:52:18 +04:00
|
|
|
**
|
2007-03-31 05:34:44 +04:00
|
|
|
** If the flags argument includes TERM_DYNAMIC, then responsibility
|
|
|
|
** for freeing the expression p is assumed by the WhereClause object.
|
|
|
|
**
|
2005-08-24 07:52:18 +04:00
|
|
|
** WARNING: This routine might reallocate the space used to store
|
2008-05-30 18:58:37 +04:00
|
|
|
** WhereTerms. All pointers to WhereTerms should be invalidated after
|
2005-08-24 07:52:18 +04:00
|
|
|
** calling this routine. Such pointers may be reinitialized by referencing
|
|
|
|
** the pWC->a[] array.
|
2005-07-16 17:33:20 +04:00
|
|
|
*/
|
2005-08-24 07:52:18 +04:00
|
|
|
static int whereClauseInsert(WhereClause *pWC, Expr *p, int flags){
|
2005-07-16 17:33:20 +04:00
|
|
|
WhereTerm *pTerm;
|
2005-08-24 07:52:18 +04:00
|
|
|
int idx;
|
2005-07-16 17:33:20 +04:00
|
|
|
if( pWC->nTerm>=pWC->nSlot ){
|
|
|
|
WhereTerm *pOld = pWC->a;
|
2008-06-15 06:51:47 +04:00
|
|
|
pWC->a = sqlite3Malloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
|
2007-03-31 05:34:44 +04:00
|
|
|
if( pWC->a==0 ){
|
2007-08-16 08:30:38 +04:00
|
|
|
pWC->pParse->db->mallocFailed = 1;
|
2007-03-31 05:34:44 +04:00
|
|
|
if( flags & TERM_DYNAMIC ){
|
|
|
|
sqlite3ExprDelete(p);
|
|
|
|
}
|
2007-11-26 16:36:00 +03:00
|
|
|
pWC->a = pOld;
|
2007-03-31 05:34:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2005-07-16 17:33:20 +04:00
|
|
|
memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
|
|
|
|
if( pOld!=pWC->aStatic ){
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(pOld);
|
2005-07-16 17:33:20 +04:00
|
|
|
}
|
|
|
|
pWC->nSlot *= 2;
|
|
|
|
}
|
2005-08-24 07:52:18 +04:00
|
|
|
pTerm = &pWC->a[idx = pWC->nTerm];
|
2005-07-19 21:38:22 +04:00
|
|
|
pWC->nTerm++;
|
|
|
|
pTerm->pExpr = p;
|
2005-07-16 17:33:20 +04:00
|
|
|
pTerm->flags = flags;
|
2005-07-19 21:38:22 +04:00
|
|
|
pTerm->pWC = pWC;
|
2005-08-02 21:48:22 +04:00
|
|
|
pTerm->iParent = -1;
|
2005-08-24 07:52:18 +04:00
|
|
|
return idx;
|
2005-07-16 17:33:20 +04:00
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/*
|
2004-12-18 21:40:26 +03:00
|
|
|
** This routine identifies subexpressions in the WHERE clause where
|
2005-09-20 12:47:20 +04:00
|
|
|
** each subexpression is separated by the AND operator or some other
|
2005-07-29 19:10:17 +04:00
|
|
|
** operator specified in the op parameter. The WhereClause structure
|
|
|
|
** is filled with pointers to subexpressions. For example:
|
2004-12-18 21:40:26 +03:00
|
|
|
**
|
|
|
|
** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
|
|
|
|
** \________/ \_______________/ \________________/
|
|
|
|
** slot[0] slot[1] slot[2]
|
|
|
|
**
|
|
|
|
** The original WHERE clause in pExpr is unaltered. All this routine
|
2005-07-24 02:59:55 +04:00
|
|
|
** does is make slot[] entries point to substructure within pExpr.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2005-07-24 02:59:55 +04:00
|
|
|
** In the previous sentence and in the diagram, "slot[]" refers to
|
|
|
|
** the WhereClause.a[] array. This array grows as needed to contain
|
|
|
|
** all terms of the WHERE clause.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-07-29 19:10:17 +04:00
|
|
|
static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
|
2005-07-16 17:33:20 +04:00
|
|
|
if( pExpr==0 ) return;
|
2005-07-29 19:10:17 +04:00
|
|
|
if( pExpr->op!=op ){
|
2005-07-16 17:33:20 +04:00
|
|
|
whereClauseInsert(pWC, pExpr, 0);
|
2000-05-29 18:26:00 +04:00
|
|
|
}else{
|
2005-07-29 19:10:17 +04:00
|
|
|
whereSplit(pWC, pExpr->pLeft, op);
|
|
|
|
whereSplit(pWC, pExpr->pRight, op);
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-02 18:32:12 +04:00
|
|
|
/*
|
|
|
|
** Initialize an expression mask set
|
|
|
|
*/
|
|
|
|
#define initMaskSet(P) memset(P, 0, sizeof(*P))
|
|
|
|
|
|
|
|
/*
|
2005-01-20 02:24:50 +03:00
|
|
|
** Return the bitmask for the given cursor number. Return 0 if
|
|
|
|
** iCursor is not in the set.
|
2003-05-02 18:32:12 +04:00
|
|
|
*/
|
2004-12-18 21:40:26 +03:00
|
|
|
static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){
|
2003-05-02 18:32:12 +04:00
|
|
|
int i;
|
|
|
|
for(i=0; i<pMaskSet->n; i++){
|
2004-12-18 21:40:26 +03:00
|
|
|
if( pMaskSet->ix[i]==iCursor ){
|
|
|
|
return ((Bitmask)1)<<i;
|
|
|
|
}
|
2003-05-02 18:32:12 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-20 02:24:50 +03:00
|
|
|
/*
|
|
|
|
** Create a new mask for cursor iCursor.
|
2005-07-19 21:38:22 +04:00
|
|
|
**
|
|
|
|
** There is one cursor per table in the FROM clause. The number of
|
|
|
|
** tables in the FROM clause is limited by a test early in the
|
2005-09-20 12:47:20 +04:00
|
|
|
** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
|
2005-07-19 21:38:22 +04:00
|
|
|
** array will never overflow.
|
2005-01-20 02:24:50 +03:00
|
|
|
*/
|
|
|
|
static void createMask(ExprMaskSet *pMaskSet, int iCursor){
|
2007-04-20 16:22:01 +04:00
|
|
|
assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
|
2005-07-19 21:38:22 +04:00
|
|
|
pMaskSet->ix[pMaskSet->n++] = iCursor;
|
2005-01-20 02:24:50 +03:00
|
|
|
}
|
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
|
|
|
** This routine walks (recursively) an expression tree and generates
|
|
|
|
** a bitmask indicating which tables are used in that expression
|
2003-05-02 18:32:12 +04:00
|
|
|
** tree.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
** In order for this routine to work, the calling function must have
|
2005-01-18 01:08:19 +03:00
|
|
|
** previously invoked sqlite3ExprResolveNames() on the expression. See
|
2000-05-29 18:26:00 +04:00
|
|
|
** the header comment on that routine for additional information.
|
2005-01-18 01:08:19 +03:00
|
|
|
** The sqlite3ExprResolveNames() routines looks for column names and
|
2003-05-02 18:32:12 +04:00
|
|
|
** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
|
2005-07-24 02:59:55 +04:00
|
|
|
** the VDBE cursor number of the table. This routine just has to
|
|
|
|
** translate the cursor numbers into bitmask values and OR all
|
|
|
|
** the bitmasks together.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-09-17 17:07:13 +04:00
|
|
|
static Bitmask exprListTableUsage(ExprMaskSet*, ExprList*);
|
|
|
|
static Bitmask exprSelectTableUsage(ExprMaskSet*, Select*);
|
2004-12-18 21:40:26 +03:00
|
|
|
static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
|
|
|
|
Bitmask mask = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
if( p==0 ) return 0;
|
2000-06-21 17:59:10 +04:00
|
|
|
if( p->op==TK_COLUMN ){
|
2004-07-19 06:12:14 +04:00
|
|
|
mask = getMask(pMaskSet, p->iTable);
|
|
|
|
return mask;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2005-01-29 11:32:43 +03:00
|
|
|
mask = exprTableUsage(pMaskSet, p->pRight);
|
|
|
|
mask |= exprTableUsage(pMaskSet, p->pLeft);
|
|
|
|
mask |= exprListTableUsage(pMaskSet, p->pList);
|
2005-09-17 17:07:13 +04:00
|
|
|
mask |= exprSelectTableUsage(pMaskSet, p->pSelect);
|
2005-01-29 11:32:43 +03:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
static Bitmask exprListTableUsage(ExprMaskSet *pMaskSet, ExprList *pList){
|
|
|
|
int i;
|
|
|
|
Bitmask mask = 0;
|
|
|
|
if( pList ){
|
|
|
|
for(i=0; i<pList->nExpr; i++){
|
|
|
|
mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
|
2002-04-02 05:58:57 +04:00
|
|
|
}
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
return mask;
|
|
|
|
}
|
2005-09-17 17:07:13 +04:00
|
|
|
static Bitmask exprSelectTableUsage(ExprMaskSet *pMaskSet, Select *pS){
|
2007-09-12 19:41:01 +04:00
|
|
|
Bitmask mask = 0;
|
|
|
|
while( pS ){
|
|
|
|
mask |= exprListTableUsage(pMaskSet, pS->pEList);
|
2005-09-17 17:07:13 +04:00
|
|
|
mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
|
|
|
|
mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
|
|
|
|
mask |= exprTableUsage(pMaskSet, pS->pWhere);
|
|
|
|
mask |= exprTableUsage(pMaskSet, pS->pHaving);
|
2007-09-12 19:41:01 +04:00
|
|
|
pS = pS->pPrior;
|
2005-09-17 17:07:13 +04:00
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2001-11-08 03:45:21 +03:00
|
|
|
/*
|
|
|
|
** Return TRUE if the given operator is one of the operators that is
|
2004-12-18 21:40:26 +03:00
|
|
|
** allowed for an indexable WHERE clause term. The allowed operators are
|
2002-06-15 00:58:45 +04:00
|
|
|
** "=", "<", ">", "<=", ">=", and "IN".
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
|
|
|
static int allowedOp(int op){
|
2005-07-21 07:14:59 +04:00
|
|
|
assert( TK_GT>TK_EQ && TK_GT<TK_GE );
|
|
|
|
assert( TK_LT>TK_EQ && TK_LT<TK_GE );
|
|
|
|
assert( TK_LE>TK_EQ && TK_LE<TK_GE );
|
|
|
|
assert( TK_GE==TK_EQ+4 );
|
2006-10-28 04:28:09 +04:00
|
|
|
return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
|
|
|
|
2004-07-20 22:23:14 +04:00
|
|
|
/*
|
2004-12-18 21:40:26 +03:00
|
|
|
** Swap two objects of type T.
|
2004-07-20 22:23:14 +04:00
|
|
|
*/
|
|
|
|
#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
|
|
|
|
|
|
|
|
/*
|
2008-05-30 18:58:37 +04:00
|
|
|
** Commute a comparison operator. Expressions of the form "X op Y"
|
2005-07-19 21:38:22 +04:00
|
|
|
** are converted into "Y op X".
|
2007-07-30 18:40:48 +04:00
|
|
|
**
|
|
|
|
** If a collation sequence is associated with either the left or right
|
|
|
|
** side of the comparison, it remains associated with the same side after
|
|
|
|
** the commutation. So "Y collate NOCASE op X" becomes
|
|
|
|
** "X collate NOCASE op Y". This is because any collation sequence on
|
|
|
|
** the left hand side of a comparison overrides any collation sequence
|
|
|
|
** attached to the right. For the same reason the EP_ExpCollate flag
|
|
|
|
** is not commuted.
|
2004-07-20 22:23:14 +04:00
|
|
|
*/
|
2005-07-19 21:38:22 +04:00
|
|
|
static void exprCommute(Expr *pExpr){
|
2007-07-30 18:40:48 +04:00
|
|
|
u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
|
|
|
|
u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
|
2005-07-21 07:14:59 +04:00
|
|
|
assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
|
2005-07-19 21:38:22 +04:00
|
|
|
SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
|
2007-07-30 18:40:48 +04:00
|
|
|
pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
|
|
|
|
pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
|
2005-07-19 21:38:22 +04:00
|
|
|
SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
|
|
|
|
if( pExpr->op>=TK_GT ){
|
|
|
|
assert( TK_LT==TK_GT+2 );
|
|
|
|
assert( TK_GE==TK_LE+2 );
|
|
|
|
assert( TK_GT>TK_EQ );
|
|
|
|
assert( TK_GT<TK_LE );
|
|
|
|
assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
|
|
|
|
pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
|
2004-07-20 22:23:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-21 07:14:59 +04:00
|
|
|
/*
|
|
|
|
** Translate from TK_xx operator to WO_xx bitmask.
|
|
|
|
*/
|
|
|
|
static int operatorMask(int op){
|
2005-07-24 02:59:55 +04:00
|
|
|
int c;
|
2005-07-21 07:14:59 +04:00
|
|
|
assert( allowedOp(op) );
|
|
|
|
if( op==TK_IN ){
|
2005-07-24 02:59:55 +04:00
|
|
|
c = WO_IN;
|
2006-10-28 04:28:09 +04:00
|
|
|
}else if( op==TK_ISNULL ){
|
|
|
|
c = WO_ISNULL;
|
2005-07-21 07:14:59 +04:00
|
|
|
}else{
|
2005-07-24 02:59:55 +04:00
|
|
|
c = WO_EQ<<(op-TK_EQ);
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2006-10-28 04:28:09 +04:00
|
|
|
assert( op!=TK_ISNULL || c==WO_ISNULL );
|
2005-07-24 02:59:55 +04:00
|
|
|
assert( op!=TK_IN || c==WO_IN );
|
|
|
|
assert( op!=TK_EQ || c==WO_EQ );
|
|
|
|
assert( op!=TK_LT || c==WO_LT );
|
|
|
|
assert( op!=TK_LE || c==WO_LE );
|
|
|
|
assert( op!=TK_GT || c==WO_GT );
|
|
|
|
assert( op!=TK_GE || c==WO_GE );
|
|
|
|
return c;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
|
|
|
|
** where X is a reference to the iColumn of table iCur and <op> is one of
|
|
|
|
** the WO_xx operator codes specified by the op parameter.
|
|
|
|
** Return a pointer to the term. Return 0 if not found.
|
|
|
|
*/
|
|
|
|
static WhereTerm *findTerm(
|
|
|
|
WhereClause *pWC, /* The WHERE clause to be searched */
|
|
|
|
int iCur, /* Cursor number of LHS */
|
|
|
|
int iColumn, /* Column number of LHS */
|
|
|
|
Bitmask notReady, /* RHS must not overlap with this mask */
|
2005-07-24 02:59:55 +04:00
|
|
|
u16 op, /* Mask of WO_xx values describing operator */
|
2005-07-21 07:14:59 +04:00
|
|
|
Index *pIdx /* Must be compatible with this index, if not NULL */
|
|
|
|
){
|
|
|
|
WhereTerm *pTerm;
|
|
|
|
int k;
|
|
|
|
for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
|
|
|
|
if( pTerm->leftCursor==iCur
|
|
|
|
&& (pTerm->prereqRight & notReady)==0
|
|
|
|
&& pTerm->leftColumn==iColumn
|
2006-01-23 16:22:09 +03:00
|
|
|
&& (pTerm->eOperator & op)!=0
|
2005-07-21 07:14:59 +04:00
|
|
|
){
|
2006-10-28 04:28:09 +04:00
|
|
|
if( iCur>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
|
2005-07-21 07:14:59 +04:00
|
|
|
Expr *pX = pTerm->pExpr;
|
|
|
|
CollSeq *pColl;
|
|
|
|
char idxaff;
|
2006-01-24 15:09:17 +03:00
|
|
|
int j;
|
2005-07-21 07:14:59 +04:00
|
|
|
Parse *pParse = pWC->pParse;
|
|
|
|
|
|
|
|
idxaff = pIdx->pTable->aCol[iColumn].affinity;
|
|
|
|
if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
|
2007-05-29 16:11:29 +04:00
|
|
|
|
|
|
|
/* Figure out the collation sequence required from an index for
|
|
|
|
** it to be useful for optimising expression pX. Store this
|
|
|
|
** value in variable pColl.
|
|
|
|
*/
|
|
|
|
assert(pX->pLeft);
|
|
|
|
pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
|
2005-07-21 07:14:59 +04:00
|
|
|
if( !pColl ){
|
2007-05-29 16:11:29 +04:00
|
|
|
pColl = pParse->db->pDfltColl;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2007-05-29 16:11:29 +04:00
|
|
|
|
2006-01-24 15:09:17 +03:00
|
|
|
for(j=0; j<pIdx->nColumn && pIdx->aiColumn[j]!=iColumn; j++){}
|
|
|
|
assert( j<pIdx->nColumn );
|
|
|
|
if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
return pTerm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-29 19:10:17 +04:00
|
|
|
/* Forward reference */
|
2007-02-06 16:26:32 +03:00
|
|
|
static void exprAnalyze(SrcList*, WhereClause*, int);
|
2005-07-29 19:10:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Call exprAnalyze on all terms in a WHERE clause.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
static void exprAnalyzeAll(
|
|
|
|
SrcList *pTabList, /* the FROM clause */
|
|
|
|
WhereClause *pWC /* the WHERE clause to be analyzed */
|
|
|
|
){
|
|
|
|
int i;
|
2005-08-24 07:52:18 +04:00
|
|
|
for(i=pWC->nTerm-1; i>=0; i--){
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyze(pTabList, pWC, i);
|
2005-07-29 19:10:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-13 02:56:09 +04:00
|
|
|
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
|
|
|
|
/*
|
|
|
|
** Check to see if the given expression is a LIKE or GLOB operator that
|
|
|
|
** can be optimized using inequality constraints. Return TRUE if it is
|
|
|
|
** so and false if not.
|
|
|
|
**
|
|
|
|
** In order for the operator to be optimizible, the RHS must be a string
|
|
|
|
** literal that does not begin with a wildcard.
|
|
|
|
*/
|
|
|
|
static int isLikeOrGlob(
|
2005-08-14 05:20:37 +04:00
|
|
|
sqlite3 *db, /* The database */
|
2005-08-13 02:56:09 +04:00
|
|
|
Expr *pExpr, /* Test this expression */
|
|
|
|
int *pnPattern, /* Number of non-wildcard prefix characters */
|
2008-02-24 00:55:39 +03:00
|
|
|
int *pisComplete, /* True if the only wildcard is % in the last character */
|
|
|
|
int *pnoCase /* True if uppercase is equivalent to lowercase */
|
2005-08-13 02:56:09 +04:00
|
|
|
){
|
|
|
|
const char *z;
|
|
|
|
Expr *pRight, *pLeft;
|
2005-08-14 05:20:37 +04:00
|
|
|
ExprList *pList;
|
2005-08-13 02:56:09 +04:00
|
|
|
int c, cnt;
|
2005-08-14 05:20:37 +04:00
|
|
|
char wc[3];
|
2005-08-28 21:00:23 +04:00
|
|
|
CollSeq *pColl;
|
|
|
|
|
2008-02-24 00:55:39 +03:00
|
|
|
if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
|
2005-08-13 02:56:09 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-02-24 00:55:39 +03:00
|
|
|
#ifdef SQLITE_EBCDIC
|
|
|
|
if( *pnoCase ) return 0;
|
|
|
|
#endif
|
2005-08-14 05:20:37 +04:00
|
|
|
pList = pExpr->pList;
|
|
|
|
pRight = pList->a[0].pExpr;
|
2008-03-31 22:19:54 +04:00
|
|
|
if( pRight->op!=TK_STRING
|
|
|
|
&& (pRight->op!=TK_REGISTER || pRight->iColumn!=TK_STRING) ){
|
2005-08-13 02:56:09 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-14 05:20:37 +04:00
|
|
|
pLeft = pList->a[1].pExpr;
|
2005-08-13 02:56:09 +04:00
|
|
|
if( pLeft->op!=TK_COLUMN ){
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-28 21:00:23 +04:00
|
|
|
pColl = pLeft->pColl;
|
2008-01-23 15:52:40 +03:00
|
|
|
assert( pColl!=0 || pLeft->iColumn==-1 );
|
2005-08-28 21:00:23 +04:00
|
|
|
if( pColl==0 ){
|
2008-01-23 15:52:40 +03:00
|
|
|
/* No collation is defined for the ROWID. Use the default. */
|
2005-08-28 21:00:23 +04:00
|
|
|
pColl = db->pDfltColl;
|
|
|
|
}
|
2008-02-24 00:55:39 +03:00
|
|
|
if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
|
|
|
|
(pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
|
2005-08-28 21:00:23 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3DequoteExpr(db, pRight);
|
2005-12-07 09:27:43 +03:00
|
|
|
z = (char *)pRight->token.z;
|
2007-11-26 16:36:00 +03:00
|
|
|
cnt = 0;
|
|
|
|
if( z ){
|
|
|
|
while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ cnt++; }
|
|
|
|
}
|
2005-08-13 02:56:09 +04:00
|
|
|
if( cnt==0 || 255==(u8)z[cnt] ){
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-14 05:20:37 +04:00
|
|
|
*pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
|
2005-08-13 02:56:09 +04:00
|
|
|
*pnPattern = cnt;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
|
|
|
|
|
2006-06-27 17:20:21 +04:00
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-13 21:38:59 +04:00
|
|
|
/*
|
|
|
|
** Check to see if the given expression is of the form
|
|
|
|
**
|
|
|
|
** column MATCH expr
|
|
|
|
**
|
|
|
|
** If it is then return TRUE. If not, return FALSE.
|
|
|
|
*/
|
|
|
|
static int isMatchOfColumn(
|
|
|
|
Expr *pExpr /* Test this expression */
|
|
|
|
){
|
|
|
|
ExprList *pList;
|
|
|
|
|
|
|
|
if( pExpr->op!=TK_FUNCTION ){
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-27 17:20:21 +04:00
|
|
|
if( pExpr->token.n!=5 ||
|
|
|
|
sqlite3StrNICmp((const char*)pExpr->token.z,"match",5)!=0 ){
|
2006-06-13 21:38:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pList = pExpr->pList;
|
|
|
|
if( pList->nExpr!=2 ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( pList->a[1].pExpr->op != TK_COLUMN ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2006-06-27 17:20:21 +04:00
|
|
|
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
2006-06-13 21:38:59 +04:00
|
|
|
|
2005-11-26 17:08:07 +03:00
|
|
|
/*
|
|
|
|
** If the pBase expression originated in the ON or USING clause of
|
|
|
|
** a join, then transfer the appropriate markings over to derived.
|
|
|
|
*/
|
|
|
|
static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
|
|
|
|
pDerived->flags |= pBase->flags & EP_FromJoin;
|
|
|
|
pDerived->iRightJoinTable = pBase->iRightJoinTable;
|
|
|
|
}
|
|
|
|
|
2007-02-24 02:13:33 +03:00
|
|
|
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
|
|
|
|
/*
|
|
|
|
** Return TRUE if the given term of an OR clause can be converted
|
|
|
|
** into an IN clause. The iCursor and iColumn define the left-hand
|
|
|
|
** side of the IN clause.
|
|
|
|
**
|
|
|
|
** The context is that we have multiple OR-connected equality terms
|
|
|
|
** like this:
|
|
|
|
**
|
|
|
|
** a=<expr1> OR a=<expr2> OR b=<expr3> OR ...
|
|
|
|
**
|
|
|
|
** The pOrTerm input to this routine corresponds to a single term of
|
2008-05-30 18:58:37 +04:00
|
|
|
** this OR clause. In order for the term to be a candidate for
|
2007-02-24 02:13:33 +03:00
|
|
|
** conversion to an IN operator, the following must be true:
|
|
|
|
**
|
|
|
|
** * The left-hand side of the term must be the column which
|
|
|
|
** is identified by iCursor and iColumn.
|
|
|
|
**
|
|
|
|
** * If the right-hand side is also a column, then the affinities
|
|
|
|
** of both right and left sides must be such that no type
|
|
|
|
** conversions are required on the right. (Ticket #2249)
|
|
|
|
**
|
|
|
|
** If both of these conditions are true, then return true. Otherwise
|
|
|
|
** return false.
|
|
|
|
*/
|
|
|
|
static int orTermIsOptCandidate(WhereTerm *pOrTerm, int iCursor, int iColumn){
|
|
|
|
int affLeft, affRight;
|
|
|
|
assert( pOrTerm->eOperator==WO_EQ );
|
|
|
|
if( pOrTerm->leftCursor!=iCursor ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( pOrTerm->leftColumn!=iColumn ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
|
|
|
|
if( affRight==0 ){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
|
|
|
|
if( affRight!=affLeft ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return true if the given term of an OR clause can be ignored during
|
|
|
|
** a check to make sure all OR terms are candidates for optimization.
|
|
|
|
** In other words, return true if a call to the orTermIsOptCandidate()
|
|
|
|
** above returned false but it is not necessary to disqualify the
|
|
|
|
** optimization.
|
|
|
|
**
|
|
|
|
** Suppose the original OR phrase was this:
|
|
|
|
**
|
|
|
|
** a=4 OR a=11 OR a=b
|
|
|
|
**
|
|
|
|
** During analysis, the third term gets flipped around and duplicate
|
|
|
|
** so that we are left with this:
|
|
|
|
**
|
|
|
|
** a=4 OR a=11 OR a=b OR b=a
|
|
|
|
**
|
|
|
|
** Since the last two terms are duplicates, only one of them
|
|
|
|
** has to qualify in order for the whole phrase to qualify. When
|
|
|
|
** this routine is called, we know that pOrTerm did not qualify.
|
|
|
|
** This routine merely checks to see if pOrTerm has a duplicate that
|
|
|
|
** might qualify. If there is a duplicate that has not yet been
|
|
|
|
** disqualified, then return true. If there are no duplicates, or
|
2008-05-30 18:58:37 +04:00
|
|
|
** the duplicate has also been disqualified, return false.
|
2007-02-24 02:13:33 +03:00
|
|
|
*/
|
|
|
|
static int orTermHasOkDuplicate(WhereClause *pOr, WhereTerm *pOrTerm){
|
|
|
|
if( pOrTerm->flags & TERM_COPIED ){
|
|
|
|
/* This is the original term. The duplicate is to the left had
|
|
|
|
** has not yet been analyzed and thus has not yet been disqualified. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if( (pOrTerm->flags & TERM_VIRTUAL)!=0
|
|
|
|
&& (pOr->a[pOrTerm->iParent].flags & TERM_OR_OK)!=0 ){
|
|
|
|
/* This is a duplicate term. The original qualified so this one
|
|
|
|
** does not have to. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* This is either a singleton term or else it is a duplicate for
|
|
|
|
** which the original did not qualify. Either way we are done for. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
|
2005-11-26 17:08:07 +03:00
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2005-07-16 17:33:20 +04:00
|
|
|
** The input to this routine is an WhereTerm structure with only the
|
2005-07-24 02:59:55 +04:00
|
|
|
** "pExpr" field filled in. The job of this routine is to analyze the
|
2005-07-16 17:33:20 +04:00
|
|
|
** subexpression and populate all the other fields of the WhereTerm
|
2000-05-29 18:26:00 +04:00
|
|
|
** structure.
|
2005-07-24 02:59:55 +04:00
|
|
|
**
|
|
|
|
** If the expression is of the form "<expr> <op> X" it gets commuted
|
|
|
|
** to the standard form of "X <op> <expr>". If the expression is of
|
|
|
|
** the form "X <op> Y" where both X and Y are columns, then the original
|
|
|
|
** expression is unchanged and a new virtual expression of the form
|
2005-09-20 12:47:20 +04:00
|
|
|
** "Y <op> X" is added to the WHERE clause and analyzed separately.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-07-19 21:38:22 +04:00
|
|
|
static void exprAnalyze(
|
|
|
|
SrcList *pSrc, /* the FROM clause */
|
2005-08-24 07:52:18 +04:00
|
|
|
WhereClause *pWC, /* the WHERE clause */
|
|
|
|
int idxTerm /* Index of the term to be analyzed */
|
2005-07-19 21:38:22 +04:00
|
|
|
){
|
2007-11-26 16:36:00 +03:00
|
|
|
WhereTerm *pTerm;
|
|
|
|
ExprMaskSet *pMaskSet;
|
|
|
|
Expr *pExpr;
|
2005-07-19 21:38:22 +04:00
|
|
|
Bitmask prereqLeft;
|
|
|
|
Bitmask prereqAll;
|
2008-04-17 23:14:02 +04:00
|
|
|
Bitmask extraRight = 0;
|
2005-08-13 02:56:09 +04:00
|
|
|
int nPattern;
|
|
|
|
int isComplete;
|
2008-02-24 00:55:39 +03:00
|
|
|
int noCase;
|
2006-10-28 04:28:09 +04:00
|
|
|
int op;
|
2007-08-16 14:09:01 +04:00
|
|
|
Parse *pParse = pWC->pParse;
|
|
|
|
sqlite3 *db = pParse->db;
|
2005-07-19 21:38:22 +04:00
|
|
|
|
2007-11-26 16:36:00 +03:00
|
|
|
if( db->mallocFailed ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pTerm = &pWC->a[idxTerm];
|
|
|
|
pMaskSet = pWC->pMaskSet;
|
|
|
|
pExpr = pTerm->pExpr;
|
2005-07-19 21:38:22 +04:00
|
|
|
prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
|
2006-10-28 04:28:09 +04:00
|
|
|
op = pExpr->op;
|
|
|
|
if( op==TK_IN ){
|
2005-09-17 17:07:13 +04:00
|
|
|
assert( pExpr->pRight==0 );
|
|
|
|
pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->pList)
|
|
|
|
| exprSelectTableUsage(pMaskSet, pExpr->pSelect);
|
2006-10-28 04:28:09 +04:00
|
|
|
}else if( op==TK_ISNULL ){
|
|
|
|
pTerm->prereqRight = 0;
|
2005-09-17 17:07:13 +04:00
|
|
|
}else{
|
|
|
|
pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
|
|
|
|
}
|
2005-09-20 01:05:48 +04:00
|
|
|
prereqAll = exprTableUsage(pMaskSet, pExpr);
|
|
|
|
if( ExprHasProperty(pExpr, EP_FromJoin) ){
|
2008-03-26 17:56:34 +03:00
|
|
|
Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
|
|
|
|
prereqAll |= x;
|
2008-04-17 23:14:02 +04:00
|
|
|
extraRight = x-1; /* ON clause terms may not be used with an index
|
|
|
|
** on left table of a LEFT JOIN. Ticket #3015 */
|
2005-09-20 01:05:48 +04:00
|
|
|
}
|
|
|
|
pTerm->prereqAll = prereqAll;
|
2005-07-19 21:38:22 +04:00
|
|
|
pTerm->leftCursor = -1;
|
2005-08-02 21:48:22 +04:00
|
|
|
pTerm->iParent = -1;
|
2006-01-23 16:22:09 +03:00
|
|
|
pTerm->eOperator = 0;
|
2006-10-28 04:28:09 +04:00
|
|
|
if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
|
2005-07-19 21:38:22 +04:00
|
|
|
Expr *pLeft = pExpr->pLeft;
|
|
|
|
Expr *pRight = pExpr->pRight;
|
|
|
|
if( pLeft->op==TK_COLUMN ){
|
|
|
|
pTerm->leftCursor = pLeft->iTable;
|
|
|
|
pTerm->leftColumn = pLeft->iColumn;
|
2006-10-28 04:28:09 +04:00
|
|
|
pTerm->eOperator = operatorMask(op);
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2005-07-19 21:38:22 +04:00
|
|
|
if( pRight && pRight->op==TK_COLUMN ){
|
|
|
|
WhereTerm *pNew;
|
|
|
|
Expr *pDup;
|
|
|
|
if( pTerm->leftCursor>=0 ){
|
2005-08-24 07:52:18 +04:00
|
|
|
int idxNew;
|
2007-08-16 08:30:38 +04:00
|
|
|
pDup = sqlite3ExprDup(db, pExpr);
|
|
|
|
if( db->mallocFailed ){
|
2007-03-31 05:34:44 +04:00
|
|
|
sqlite3ExprDelete(pDup);
|
2006-10-19 03:26:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2005-08-24 07:52:18 +04:00
|
|
|
idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
|
|
|
|
if( idxNew==0 ) return;
|
|
|
|
pNew = &pWC->a[idxNew];
|
|
|
|
pNew->iParent = idxTerm;
|
|
|
|
pTerm = &pWC->a[idxTerm];
|
2005-08-02 21:48:22 +04:00
|
|
|
pTerm->nChild = 1;
|
|
|
|
pTerm->flags |= TERM_COPIED;
|
2005-07-19 21:38:22 +04:00
|
|
|
}else{
|
|
|
|
pDup = pExpr;
|
|
|
|
pNew = pTerm;
|
2004-07-20 22:23:14 +04:00
|
|
|
}
|
2005-07-19 21:38:22 +04:00
|
|
|
exprCommute(pDup);
|
|
|
|
pLeft = pDup->pLeft;
|
|
|
|
pNew->leftCursor = pLeft->iTable;
|
|
|
|
pNew->leftColumn = pLeft->iColumn;
|
|
|
|
pNew->prereqRight = prereqLeft;
|
|
|
|
pNew->prereqAll = prereqAll;
|
2006-01-23 16:22:09 +03:00
|
|
|
pNew->eOperator = operatorMask(pDup->op);
|
2004-07-20 22:23:14 +04:00
|
|
|
}
|
2005-07-19 21:38:22 +04:00
|
|
|
}
|
2005-07-29 03:12:08 +04:00
|
|
|
|
2005-08-13 02:56:09 +04:00
|
|
|
#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
|
2005-07-29 03:12:08 +04:00
|
|
|
/* If a term is the BETWEEN operator, create two new virtual terms
|
|
|
|
** that define the range that the BETWEEN implements.
|
|
|
|
*/
|
|
|
|
else if( pExpr->op==TK_BETWEEN ){
|
|
|
|
ExprList *pList = pExpr->pList;
|
|
|
|
int i;
|
|
|
|
static const u8 ops[] = {TK_GE, TK_LE};
|
|
|
|
assert( pList!=0 );
|
|
|
|
assert( pList->nExpr==2 );
|
|
|
|
for(i=0; i<2; i++){
|
|
|
|
Expr *pNewExpr;
|
2005-08-24 07:52:18 +04:00
|
|
|
int idxNew;
|
2007-08-29 16:31:25 +04:00
|
|
|
pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft),
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3ExprDup(db, pList->a[i].pExpr), 0);
|
2005-08-24 07:52:18 +04:00
|
|
|
idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyze(pSrc, pWC, idxNew);
|
2005-08-24 07:52:18 +04:00
|
|
|
pTerm = &pWC->a[idxTerm];
|
|
|
|
pWC->a[idxNew].iParent = idxTerm;
|
2005-07-29 03:12:08 +04:00
|
|
|
}
|
2005-08-02 21:48:22 +04:00
|
|
|
pTerm->nChild = 2;
|
2005-07-29 03:12:08 +04:00
|
|
|
}
|
2005-08-13 02:56:09 +04:00
|
|
|
#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
|
2005-07-29 03:12:08 +04:00
|
|
|
|
2006-01-14 11:02:28 +03:00
|
|
|
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
|
2005-07-29 19:10:17 +04:00
|
|
|
/* Attempt to convert OR-connected terms into an IN operator so that
|
2005-09-17 17:07:13 +04:00
|
|
|
** they can make use of indices. Example:
|
|
|
|
**
|
|
|
|
** x = expr1 OR expr2 = x OR x = expr3
|
|
|
|
**
|
|
|
|
** is converted into
|
|
|
|
**
|
|
|
|
** x IN (expr1,expr2,expr3)
|
2006-01-14 11:02:28 +03:00
|
|
|
**
|
|
|
|
** This optimization must be omitted if OMIT_SUBQUERY is defined because
|
|
|
|
** the compiler for the the IN operator is part of sub-queries.
|
2005-07-29 19:10:17 +04:00
|
|
|
*/
|
|
|
|
else if( pExpr->op==TK_OR ){
|
|
|
|
int ok;
|
|
|
|
int i, j;
|
|
|
|
int iColumn, iCursor;
|
|
|
|
WhereClause sOr;
|
|
|
|
WhereTerm *pOrTerm;
|
|
|
|
|
|
|
|
assert( (pTerm->flags & TERM_DYNAMIC)==0 );
|
2007-02-06 16:26:32 +03:00
|
|
|
whereClauseInit(&sOr, pWC->pParse, pMaskSet);
|
2005-07-29 19:10:17 +04:00
|
|
|
whereSplit(&sOr, pExpr, TK_OR);
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyzeAll(pSrc, &sOr);
|
2007-02-24 02:13:33 +03:00
|
|
|
assert( sOr.nTerm>=2 );
|
2005-07-29 19:10:17 +04:00
|
|
|
j = 0;
|
2008-03-17 20:08:33 +03:00
|
|
|
if( db->mallocFailed ) goto or_not_possible;
|
2005-07-29 19:10:17 +04:00
|
|
|
do{
|
2007-02-24 02:13:33 +03:00
|
|
|
assert( j<sOr.nTerm );
|
2005-07-29 19:10:17 +04:00
|
|
|
iColumn = sOr.a[j].leftColumn;
|
|
|
|
iCursor = sOr.a[j].leftCursor;
|
|
|
|
ok = iCursor>=0;
|
|
|
|
for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
|
2006-01-23 16:22:09 +03:00
|
|
|
if( pOrTerm->eOperator!=WO_EQ ){
|
2005-07-29 19:10:17 +04:00
|
|
|
goto or_not_possible;
|
|
|
|
}
|
2007-02-24 02:13:33 +03:00
|
|
|
if( orTermIsOptCandidate(pOrTerm, iCursor, iColumn) ){
|
2005-07-29 19:10:17 +04:00
|
|
|
pOrTerm->flags |= TERM_OR_OK;
|
2007-02-24 02:13:33 +03:00
|
|
|
}else if( orTermHasOkDuplicate(&sOr, pOrTerm) ){
|
2005-07-29 19:10:17 +04:00
|
|
|
pOrTerm->flags &= ~TERM_OR_OK;
|
|
|
|
}else{
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-02-24 02:13:33 +03:00
|
|
|
}while( !ok && (sOr.a[j++].flags & TERM_COPIED)!=0 && j<2 );
|
2005-07-29 19:10:17 +04:00
|
|
|
if( ok ){
|
|
|
|
ExprList *pList = 0;
|
|
|
|
Expr *pNew, *pDup;
|
2006-11-06 18:10:05 +03:00
|
|
|
Expr *pLeft = 0;
|
2005-07-29 19:10:17 +04:00
|
|
|
for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
|
|
|
|
if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue;
|
2007-08-16 08:30:38 +04:00
|
|
|
pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight);
|
|
|
|
pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
|
2006-11-06 18:10:05 +03:00
|
|
|
pLeft = pOrTerm->pExpr->pLeft;
|
2005-07-29 19:10:17 +04:00
|
|
|
}
|
2006-11-06 18:10:05 +03:00
|
|
|
assert( pLeft!=0 );
|
2007-08-16 08:30:38 +04:00
|
|
|
pDup = sqlite3ExprDup(db, pLeft);
|
2007-08-29 16:31:25 +04:00
|
|
|
pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
|
2005-09-16 06:38:09 +04:00
|
|
|
if( pNew ){
|
2005-11-26 17:24:40 +03:00
|
|
|
int idxNew;
|
2005-11-26 17:08:07 +03:00
|
|
|
transferJoinMarkings(pNew, pExpr);
|
2005-09-16 06:38:09 +04:00
|
|
|
pNew->pList = pList;
|
2005-11-26 17:24:40 +03:00
|
|
|
idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyze(pSrc, pWC, idxNew);
|
2005-11-26 17:24:40 +03:00
|
|
|
pTerm = &pWC->a[idxTerm];
|
|
|
|
pWC->a[idxNew].iParent = idxTerm;
|
|
|
|
pTerm->nChild = 1;
|
2005-09-16 06:38:09 +04:00
|
|
|
}else{
|
|
|
|
sqlite3ExprListDelete(pList);
|
|
|
|
}
|
2005-07-29 19:10:17 +04:00
|
|
|
}
|
|
|
|
or_not_possible:
|
|
|
|
whereClauseClear(&sOr);
|
|
|
|
}
|
2005-08-13 02:56:09 +04:00
|
|
|
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
|
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
|
|
|
|
/* Add constraints to reduce the search space on a LIKE or GLOB
|
|
|
|
** operator.
|
2008-02-24 00:55:39 +03:00
|
|
|
**
|
|
|
|
** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
|
|
|
|
**
|
|
|
|
** x>='abc' AND x<'abd' AND x LIKE 'abc%'
|
|
|
|
**
|
|
|
|
** The last character of the prefix "abc" is incremented to form the
|
2008-05-28 22:01:44 +04:00
|
|
|
** termination condition "abd".
|
2005-08-13 02:56:09 +04:00
|
|
|
*/
|
2008-02-24 00:55:39 +03:00
|
|
|
if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete, &noCase) ){
|
2005-08-13 02:56:09 +04:00
|
|
|
Expr *pLeft, *pRight;
|
|
|
|
Expr *pStr1, *pStr2;
|
|
|
|
Expr *pNewExpr1, *pNewExpr2;
|
2005-08-24 07:52:18 +04:00
|
|
|
int idxNew1, idxNew2;
|
|
|
|
|
2005-08-13 02:56:09 +04:00
|
|
|
pLeft = pExpr->pList->a[1].pExpr;
|
|
|
|
pRight = pExpr->pList->a[0].pExpr;
|
2007-08-16 08:30:38 +04:00
|
|
|
pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
|
2005-08-13 02:56:09 +04:00
|
|
|
if( pStr1 ){
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
|
2005-08-13 02:56:09 +04:00
|
|
|
pStr1->token.n = nPattern;
|
2007-06-11 16:56:15 +04:00
|
|
|
pStr1->flags = EP_Dequoted;
|
2005-08-13 02:56:09 +04:00
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
pStr2 = sqlite3ExprDup(db, pStr1);
|
2007-11-26 16:36:00 +03:00
|
|
|
if( !db->mallocFailed ){
|
2008-02-24 00:55:39 +03:00
|
|
|
u8 c, *pC;
|
2005-08-13 02:56:09 +04:00
|
|
|
assert( pStr2->token.dyn );
|
2008-02-24 00:55:39 +03:00
|
|
|
pC = (u8*)&pStr2->token.z[nPattern-1];
|
|
|
|
c = *pC;
|
2008-05-26 22:33:40 +04:00
|
|
|
if( noCase ){
|
|
|
|
if( c=='@' ) isComplete = 0;
|
|
|
|
c = sqlite3UpperToLower[c];
|
|
|
|
}
|
2008-02-24 00:55:39 +03:00
|
|
|
*pC = c + 1;
|
2005-08-13 02:56:09 +04:00
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0);
|
2005-08-24 07:52:18 +04:00
|
|
|
idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyze(pSrc, pWC, idxNew1);
|
2007-08-16 08:30:38 +04:00
|
|
|
pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0);
|
2005-08-24 07:52:18 +04:00
|
|
|
idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyze(pSrc, pWC, idxNew2);
|
2005-08-24 07:52:18 +04:00
|
|
|
pTerm = &pWC->a[idxTerm];
|
2005-08-13 02:56:09 +04:00
|
|
|
if( isComplete ){
|
2005-08-24 07:52:18 +04:00
|
|
|
pWC->a[idxNew1].iParent = idxTerm;
|
|
|
|
pWC->a[idxNew2].iParent = idxTerm;
|
2005-08-13 02:56:09 +04:00
|
|
|
pTerm->nChild = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
|
2006-06-13 21:38:59 +04:00
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
|
|
|
/* Add a WO_MATCH auxiliary term to the constraint set if the
|
|
|
|
** current expression is of the form: column MATCH expr.
|
|
|
|
** This information is used by the xBestIndex methods of
|
|
|
|
** virtual tables. The native query optimizer does not attempt
|
|
|
|
** to do anything with MATCH functions.
|
|
|
|
*/
|
|
|
|
if( isMatchOfColumn(pExpr) ){
|
|
|
|
int idxNew;
|
|
|
|
Expr *pRight, *pLeft;
|
|
|
|
WhereTerm *pNewTerm;
|
|
|
|
Bitmask prereqColumn, prereqExpr;
|
|
|
|
|
|
|
|
pRight = pExpr->pList->a[0].pExpr;
|
|
|
|
pLeft = pExpr->pList->a[1].pExpr;
|
|
|
|
prereqExpr = exprTableUsage(pMaskSet, pRight);
|
|
|
|
prereqColumn = exprTableUsage(pMaskSet, pLeft);
|
|
|
|
if( (prereqExpr & prereqColumn)==0 ){
|
2006-06-15 02:07:10 +04:00
|
|
|
Expr *pNewExpr;
|
2007-08-29 16:31:25 +04:00
|
|
|
pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0);
|
2006-06-15 02:07:10 +04:00
|
|
|
idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
|
2006-06-13 21:38:59 +04:00
|
|
|
pNewTerm = &pWC->a[idxNew];
|
|
|
|
pNewTerm->prereqRight = prereqExpr;
|
|
|
|
pNewTerm->leftCursor = pLeft->iTable;
|
|
|
|
pNewTerm->leftColumn = pLeft->iColumn;
|
|
|
|
pNewTerm->eOperator = WO_MATCH;
|
|
|
|
pNewTerm->iParent = idxTerm;
|
2006-06-27 06:36:58 +04:00
|
|
|
pTerm = &pWC->a[idxTerm];
|
2006-06-13 21:38:59 +04:00
|
|
|
pTerm->nChild = 1;
|
|
|
|
pTerm->flags |= TERM_COPIED;
|
|
|
|
pNewTerm->prereqAll = pTerm->prereqAll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
2008-04-17 23:14:02 +04:00
|
|
|
|
|
|
|
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
|
|
|
|
** an index for tables to the left of the join.
|
|
|
|
*/
|
|
|
|
pTerm->prereqRight |= extraRight;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
2007-02-06 16:26:32 +03:00
|
|
|
/*
|
|
|
|
** Return TRUE if any of the expressions in pList->a[iFirst...] contain
|
|
|
|
** a reference to any table other than the iBase table.
|
|
|
|
*/
|
|
|
|
static int referencesOtherTables(
|
|
|
|
ExprList *pList, /* Search expressions in ths list */
|
|
|
|
ExprMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
|
|
|
|
int iFirst, /* Be searching with the iFirst-th expression */
|
|
|
|
int iBase /* Ignore references to this table */
|
|
|
|
){
|
|
|
|
Bitmask allowed = ~getMask(pMaskSet, iBase);
|
|
|
|
while( iFirst<pList->nExpr ){
|
|
|
|
if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-19 21:38:22 +04:00
|
|
|
|
2004-12-18 21:40:26 +03:00
|
|
|
/*
|
|
|
|
** This routine decides if pIdx can be used to satisfy the ORDER BY
|
|
|
|
** clause. If it can, it returns 1. If pIdx cannot satisfy the
|
|
|
|
** ORDER BY clause, this routine returns 0.
|
|
|
|
**
|
|
|
|
** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
|
|
|
|
** left-most table in the FROM clause of that same SELECT statement and
|
|
|
|
** the table has a cursor number of "base". pIdx is an index on pTab.
|
|
|
|
**
|
|
|
|
** nEqCol is the number of columns of pIdx that are used as equality
|
|
|
|
** constraints. Any of these columns may be missing from the ORDER BY
|
|
|
|
** clause and the match can still be a success.
|
|
|
|
**
|
|
|
|
** All terms of the ORDER BY that match against the index must be either
|
|
|
|
** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
|
|
|
|
** index do not need to satisfy this constraint.) The *pbRev value is
|
|
|
|
** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
|
|
|
|
** the ORDER BY clause is all ASC.
|
|
|
|
*/
|
|
|
|
static int isSortingIndex(
|
|
|
|
Parse *pParse, /* Parsing context */
|
2007-02-06 16:26:32 +03:00
|
|
|
ExprMaskSet *pMaskSet, /* Mapping from table indices to bitmaps */
|
2004-12-18 21:40:26 +03:00
|
|
|
Index *pIdx, /* The index we are testing */
|
2006-02-24 05:53:49 +03:00
|
|
|
int base, /* Cursor number for the table to be sorted */
|
2004-12-18 21:40:26 +03:00
|
|
|
ExprList *pOrderBy, /* The ORDER BY clause */
|
|
|
|
int nEqCol, /* Number of index columns with == constraints */
|
|
|
|
int *pbRev /* Set to 1 if ORDER BY is DESC */
|
|
|
|
){
|
2005-08-29 20:40:52 +04:00
|
|
|
int i, j; /* Loop counters */
|
2005-12-21 06:16:42 +03:00
|
|
|
int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
|
2005-08-29 20:40:52 +04:00
|
|
|
int nTerm; /* Number of ORDER BY terms */
|
|
|
|
struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
|
2004-12-18 21:40:26 +03:00
|
|
|
sqlite3 *db = pParse->db;
|
|
|
|
|
|
|
|
assert( pOrderBy!=0 );
|
|
|
|
nTerm = pOrderBy->nExpr;
|
|
|
|
assert( nTerm>0 );
|
|
|
|
|
|
|
|
/* Match terms of the ORDER BY clause against columns of
|
|
|
|
** the index.
|
2006-12-20 06:24:19 +03:00
|
|
|
**
|
|
|
|
** Note that indices have pIdx->nColumn regular columns plus
|
|
|
|
** one additional column containing the rowid. The rowid column
|
|
|
|
** of the index is also allowed to match against the ORDER BY
|
|
|
|
** clause.
|
2004-12-18 21:40:26 +03:00
|
|
|
*/
|
2006-12-20 06:24:19 +03:00
|
|
|
for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
|
2004-12-18 21:40:26 +03:00
|
|
|
Expr *pExpr; /* The expression of the ORDER BY pTerm */
|
|
|
|
CollSeq *pColl; /* The collating sequence of pExpr */
|
2005-12-21 06:16:42 +03:00
|
|
|
int termSortOrder; /* Sort order for this term */
|
2006-12-20 06:24:19 +03:00
|
|
|
int iColumn; /* The i-th column of the index. -1 for rowid */
|
|
|
|
int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
|
|
|
|
const char *zColl; /* Name of the collating sequence for i-th index term */
|
2004-12-18 21:40:26 +03:00
|
|
|
|
|
|
|
pExpr = pTerm->pExpr;
|
|
|
|
if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
|
|
|
|
/* Can not use an index sort on anything that is not a column in the
|
|
|
|
** left-most table of the FROM clause */
|
2007-02-06 16:26:32 +03:00
|
|
|
break;
|
2004-12-18 21:40:26 +03:00
|
|
|
}
|
|
|
|
pColl = sqlite3ExprCollSeq(pParse, pExpr);
|
2006-12-20 06:24:19 +03:00
|
|
|
if( !pColl ){
|
|
|
|
pColl = db->pDfltColl;
|
|
|
|
}
|
|
|
|
if( i<pIdx->nColumn ){
|
|
|
|
iColumn = pIdx->aiColumn[i];
|
|
|
|
if( iColumn==pIdx->pTable->iPKey ){
|
|
|
|
iColumn = -1;
|
|
|
|
}
|
|
|
|
iSortOrder = pIdx->aSortOrder[i];
|
|
|
|
zColl = pIdx->azColl[i];
|
|
|
|
}else{
|
|
|
|
iColumn = -1;
|
|
|
|
iSortOrder = 0;
|
|
|
|
zColl = pColl->zName;
|
|
|
|
}
|
|
|
|
if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
|
2004-12-19 03:11:35 +03:00
|
|
|
/* Term j of the ORDER BY clause does not match column i of the index */
|
|
|
|
if( i<nEqCol ){
|
2004-12-18 21:40:26 +03:00
|
|
|
/* If an index column that is constrained by == fails to match an
|
|
|
|
** ORDER BY term, that is OK. Just ignore that column of the index
|
|
|
|
*/
|
|
|
|
continue;
|
2008-06-25 06:47:57 +04:00
|
|
|
}else if( i==pIdx->nColumn ){
|
|
|
|
/* Index column i is the rowid. All other terms match. */
|
|
|
|
break;
|
2004-12-18 21:40:26 +03:00
|
|
|
}else{
|
|
|
|
/* If an index column fails to match and is not constrained by ==
|
|
|
|
** then the index cannot satisfy the ORDER BY constraint.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2006-01-10 20:58:23 +03:00
|
|
|
assert( pIdx->aSortOrder!=0 );
|
2005-12-21 06:16:42 +03:00
|
|
|
assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
|
2006-12-20 06:24:19 +03:00
|
|
|
assert( iSortOrder==0 || iSortOrder==1 );
|
|
|
|
termSortOrder = iSortOrder ^ pTerm->sortOrder;
|
2004-12-18 21:40:26 +03:00
|
|
|
if( i>nEqCol ){
|
2005-12-21 06:16:42 +03:00
|
|
|
if( termSortOrder!=sortOrder ){
|
2004-12-18 21:40:26 +03:00
|
|
|
/* Indices can only be used if all ORDER BY terms past the
|
|
|
|
** equality constraints are all either DESC or ASC. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}else{
|
2005-12-21 06:16:42 +03:00
|
|
|
sortOrder = termSortOrder;
|
2004-12-18 21:40:26 +03:00
|
|
|
}
|
|
|
|
j++;
|
|
|
|
pTerm++;
|
2007-02-06 16:26:32 +03:00
|
|
|
if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
|
2006-12-20 06:24:19 +03:00
|
|
|
/* If the indexed column is the primary key and everything matches
|
2007-02-06 16:26:32 +03:00
|
|
|
** so far and none of the ORDER BY terms to the right reference other
|
|
|
|
** tables in the join, then we are assured that the index can be used
|
|
|
|
** to sort because the primary key is unique and so none of the other
|
|
|
|
** columns will make any difference
|
2006-12-20 06:24:19 +03:00
|
|
|
*/
|
|
|
|
j = nTerm;
|
|
|
|
}
|
2004-12-18 21:40:26 +03:00
|
|
|
}
|
|
|
|
|
2006-12-20 06:24:19 +03:00
|
|
|
*pbRev = sortOrder!=0;
|
2005-08-13 20:13:04 +04:00
|
|
|
if( j>=nTerm ){
|
2006-12-20 06:24:19 +03:00
|
|
|
/* All terms of the ORDER BY clause are covered by this index so
|
|
|
|
** this index can be used for sorting. */
|
|
|
|
return 1;
|
|
|
|
}
|
2007-02-06 16:26:32 +03:00
|
|
|
if( pIdx->onError!=OE_None && i==pIdx->nColumn
|
|
|
|
&& !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
|
2006-12-20 06:24:19 +03:00
|
|
|
/* All terms of this index match some prefix of the ORDER BY clause
|
2007-02-06 16:26:32 +03:00
|
|
|
** and the index is UNIQUE and no terms on the tail of the ORDER BY
|
|
|
|
** clause reference other tables in a join. If this is all true then
|
|
|
|
** the order by clause is superfluous. */
|
2004-12-18 21:40:26 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-22 22:12:19 +03:00
|
|
|
/*
|
|
|
|
** Check table to see if the ORDER BY clause in pOrderBy can be satisfied
|
|
|
|
** by sorting in order of ROWID. Return true if so and set *pbRev to be
|
|
|
|
** true for reverse ROWID and false for forward ROWID order.
|
|
|
|
*/
|
|
|
|
static int sortableByRowid(
|
|
|
|
int base, /* Cursor number for table to be sorted */
|
|
|
|
ExprList *pOrderBy, /* The ORDER BY clause */
|
2007-02-06 16:26:32 +03:00
|
|
|
ExprMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
|
2004-11-22 22:12:19 +03:00
|
|
|
int *pbRev /* Set to 1 if ORDER BY is DESC */
|
|
|
|
){
|
|
|
|
Expr *p;
|
|
|
|
|
|
|
|
assert( pOrderBy!=0 );
|
|
|
|
assert( pOrderBy->nExpr>0 );
|
|
|
|
p = pOrderBy->a[0].pExpr;
|
2007-02-06 16:26:32 +03:00
|
|
|
if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1
|
|
|
|
&& !referencesOtherTables(pOrderBy, pMaskSet, 1, base) ){
|
2004-11-22 22:12:19 +03:00
|
|
|
*pbRev = pOrderBy->a[0].sortOrder;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-28 00:41:43 +04:00
|
|
|
/*
|
2005-09-20 12:47:20 +04:00
|
|
|
** Prepare a crude estimate of the logarithm of the input value.
|
2005-07-28 00:41:43 +04:00
|
|
|
** The results need not be exact. This is only used for estimating
|
2008-05-30 18:58:37 +04:00
|
|
|
** the total cost of performing operations with O(logN) or O(NlogN)
|
2005-07-28 00:41:43 +04:00
|
|
|
** complexity. Because N is just a guess, it is no great tragedy if
|
|
|
|
** logN is a little off.
|
|
|
|
*/
|
|
|
|
static double estLog(double N){
|
2005-10-13 06:09:49 +04:00
|
|
|
double logN = 1;
|
|
|
|
double x = 10;
|
2005-07-28 00:41:43 +04:00
|
|
|
while( N>x ){
|
2005-10-13 06:09:49 +04:00
|
|
|
logN += 1;
|
2005-07-28 00:41:43 +04:00
|
|
|
x *= 10;
|
|
|
|
}
|
|
|
|
return logN;
|
|
|
|
}
|
|
|
|
|
2006-06-27 05:54:26 +04:00
|
|
|
/*
|
|
|
|
** Two routines for printing the content of an sqlite3_index_info
|
|
|
|
** structure. Used for testing and debugging only. If neither
|
|
|
|
** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
|
|
|
|
** are no-ops.
|
|
|
|
*/
|
2007-04-06 05:04:39 +04:00
|
|
|
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
|
2006-06-27 05:54:26 +04:00
|
|
|
static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
|
|
|
|
int i;
|
2008-03-04 20:45:01 +03:00
|
|
|
if( !sqlite3WhereTrace ) return;
|
2006-06-27 05:54:26 +04:00
|
|
|
for(i=0; i<p->nConstraint; i++){
|
|
|
|
sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
|
|
|
|
i,
|
|
|
|
p->aConstraint[i].iColumn,
|
|
|
|
p->aConstraint[i].iTermOffset,
|
|
|
|
p->aConstraint[i].op,
|
|
|
|
p->aConstraint[i].usable);
|
|
|
|
}
|
|
|
|
for(i=0; i<p->nOrderBy; i++){
|
|
|
|
sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
|
|
|
|
i,
|
|
|
|
p->aOrderBy[i].iColumn,
|
|
|
|
p->aOrderBy[i].desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
|
|
|
|
int i;
|
2008-03-04 20:45:01 +03:00
|
|
|
if( !sqlite3WhereTrace ) return;
|
2006-06-27 05:54:26 +04:00
|
|
|
for(i=0; i<p->nConstraint; i++){
|
|
|
|
sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
|
|
|
|
i,
|
|
|
|
p->aConstraintUsage[i].argvIndex,
|
|
|
|
p->aConstraintUsage[i].omit);
|
|
|
|
}
|
|
|
|
sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
|
|
|
|
sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
|
|
|
|
sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
|
|
|
|
sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define TRACE_IDX_INPUTS(A)
|
|
|
|
#define TRACE_IDX_OUTPUTS(A)
|
|
|
|
#endif
|
|
|
|
|
2006-06-13 01:59:13 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
|
|
|
/*
|
2006-06-13 21:38:59 +04:00
|
|
|
** Compute the best index for a virtual table.
|
|
|
|
**
|
|
|
|
** The best index is computed by the xBestIndex method of the virtual
|
|
|
|
** table module. This routine is really just a wrapper that sets up
|
|
|
|
** the sqlite3_index_info structure that is used to communicate with
|
|
|
|
** xBestIndex.
|
|
|
|
**
|
|
|
|
** In a join, this routine might be called multiple times for the
|
|
|
|
** same virtual table. The sqlite3_index_info structure is created
|
|
|
|
** and initialized on the first invocation and reused on all subsequent
|
|
|
|
** invocations. The sqlite3_index_info structure is also used when
|
|
|
|
** code is generated to access the virtual table. The whereInfoDelete()
|
|
|
|
** routine takes care of freeing the sqlite3_index_info structure after
|
|
|
|
** everybody has finished with it.
|
2006-06-13 01:59:13 +04:00
|
|
|
*/
|
|
|
|
static double bestVirtualIndex(
|
|
|
|
Parse *pParse, /* The parsing context */
|
|
|
|
WhereClause *pWC, /* The WHERE clause */
|
|
|
|
struct SrcList_item *pSrc, /* The FROM clause term to search */
|
|
|
|
Bitmask notReady, /* Mask of cursors that are not available */
|
|
|
|
ExprList *pOrderBy, /* The order by clause */
|
|
|
|
int orderByUsable, /* True if we can potential sort */
|
|
|
|
sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
|
|
|
|
){
|
|
|
|
Table *pTab = pSrc->pTab;
|
|
|
|
sqlite3_index_info *pIdxInfo;
|
|
|
|
struct sqlite3_index_constraint *pIdxCons;
|
|
|
|
struct sqlite3_index_orderby *pIdxOrderBy;
|
|
|
|
struct sqlite3_index_constraint_usage *pUsage;
|
|
|
|
WhereTerm *pTerm;
|
|
|
|
int i, j;
|
|
|
|
int nOrderBy;
|
2006-06-19 16:02:58 +04:00
|
|
|
int rc;
|
2006-06-13 01:59:13 +04:00
|
|
|
|
|
|
|
/* If the sqlite3_index_info structure has not been previously
|
|
|
|
** allocated and initialized for this virtual table, then allocate
|
|
|
|
** and initialize it now
|
|
|
|
*/
|
|
|
|
pIdxInfo = *ppIdxInfo;
|
|
|
|
if( pIdxInfo==0 ){
|
|
|
|
WhereTerm *pTerm;
|
|
|
|
int nTerm;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("Recomputing index info for %s...\n", pTab->zName));
|
2006-06-13 01:59:13 +04:00
|
|
|
|
|
|
|
/* Count the number of possible WHERE clause constraints referring
|
|
|
|
** to this virtual table */
|
|
|
|
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
|
|
|
|
if( pTerm->leftCursor != pSrc->iCursor ) continue;
|
2008-04-19 18:40:43 +04:00
|
|
|
if( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
|
|
|
|
testcase( pTerm->eOperator==WO_IN );
|
|
|
|
testcase( pTerm->eOperator==WO_ISNULL );
|
|
|
|
if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
|
2006-06-13 01:59:13 +04:00
|
|
|
nTerm++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the ORDER BY clause contains only columns in the current
|
|
|
|
** virtual table then allocate space for the aOrderBy part of
|
|
|
|
** the sqlite3_index_info structure.
|
|
|
|
*/
|
|
|
|
nOrderBy = 0;
|
|
|
|
if( pOrderBy ){
|
|
|
|
for(i=0; i<pOrderBy->nExpr; i++){
|
|
|
|
Expr *pExpr = pOrderBy->a[i].pExpr;
|
|
|
|
if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
|
|
|
|
}
|
|
|
|
if( i==pOrderBy->nExpr ){
|
|
|
|
nOrderBy = pOrderBy->nExpr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the sqlite3_index_info structure
|
|
|
|
*/
|
2007-08-29 18:06:22 +04:00
|
|
|
pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
|
2006-06-13 01:59:13 +04:00
|
|
|
+ (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
|
|
|
|
+ sizeof(*pIdxOrderBy)*nOrderBy );
|
|
|
|
if( pIdxInfo==0 ){
|
|
|
|
sqlite3ErrorMsg(pParse, "out of memory");
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
*ppIdxInfo = pIdxInfo;
|
|
|
|
|
|
|
|
/* Initialize the structure. The sqlite3_index_info structure contains
|
|
|
|
** many fields that are declared "const" to prevent xBestIndex from
|
|
|
|
** changing them. We have to do some funky casting in order to
|
|
|
|
** initialize those fields.
|
|
|
|
*/
|
|
|
|
pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
|
|
|
|
pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
|
|
|
|
pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
|
|
|
|
*(int*)&pIdxInfo->nConstraint = nTerm;
|
|
|
|
*(int*)&pIdxInfo->nOrderBy = nOrderBy;
|
|
|
|
*(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
|
|
|
|
*(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
|
|
|
|
*(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
|
|
|
|
pUsage;
|
|
|
|
|
|
|
|
for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
|
|
|
|
if( pTerm->leftCursor != pSrc->iCursor ) continue;
|
2008-04-19 18:40:43 +04:00
|
|
|
if( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
|
|
|
|
testcase( pTerm->eOperator==WO_IN );
|
|
|
|
testcase( pTerm->eOperator==WO_ISNULL );
|
|
|
|
if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
|
2006-06-13 01:59:13 +04:00
|
|
|
pIdxCons[j].iColumn = pTerm->leftColumn;
|
|
|
|
pIdxCons[j].iTermOffset = i;
|
|
|
|
pIdxCons[j].op = pTerm->eOperator;
|
2006-06-13 21:38:59 +04:00
|
|
|
/* The direct assignment in the previous line is possible only because
|
|
|
|
** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
|
|
|
|
** following asserts verify this fact. */
|
2006-06-13 01:59:13 +04:00
|
|
|
assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
|
|
|
|
assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
|
|
|
|
assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
|
|
|
|
assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
|
|
|
|
assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
|
2006-06-13 21:38:59 +04:00
|
|
|
assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
|
|
|
|
assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
|
2006-06-13 01:59:13 +04:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
for(i=0; i<nOrderBy; i++){
|
|
|
|
Expr *pExpr = pOrderBy->a[i].pExpr;
|
|
|
|
pIdxOrderBy[i].iColumn = pExpr->iColumn;
|
|
|
|
pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:38:59 +04:00
|
|
|
/* At this point, the sqlite3_index_info structure that pIdxInfo points
|
|
|
|
** to will have been initialized, either during the current invocation or
|
|
|
|
** during some prior invocation. Now we just have to customize the
|
|
|
|
** details of pIdxInfo for the current invocation and pass it to
|
|
|
|
** xBestIndex.
|
|
|
|
*/
|
|
|
|
|
2007-03-30 13:13:13 +04:00
|
|
|
/* The module name must be defined. Also, by this point there must
|
|
|
|
** be a pointer to an sqlite3_vtab structure. Otherwise
|
|
|
|
** sqlite3ViewGetColumnNames() would have picked up the error.
|
|
|
|
*/
|
2006-06-13 01:59:13 +04:00
|
|
|
assert( pTab->azModuleArg && pTab->azModuleArg[0] );
|
2007-03-30 13:13:13 +04:00
|
|
|
assert( pTab->pVtab );
|
|
|
|
#if 0
|
2006-06-13 01:59:13 +04:00
|
|
|
if( pTab->pVtab==0 ){
|
|
|
|
sqlite3ErrorMsg(pParse, "undefined module %s for table %s",
|
|
|
|
pTab->azModuleArg[0], pTab->zName);
|
|
|
|
return 0.0;
|
|
|
|
}
|
2007-03-30 13:13:13 +04:00
|
|
|
#endif
|
2006-06-13 01:59:13 +04:00
|
|
|
|
|
|
|
/* Set the aConstraint[].usable fields and initialize all
|
2006-06-13 21:38:59 +04:00
|
|
|
** output variables to zero.
|
|
|
|
**
|
|
|
|
** aConstraint[].usable is true for constraints where the right-hand
|
|
|
|
** side contains only references to tables to the left of the current
|
|
|
|
** table. In other words, if the constraint is of the form:
|
|
|
|
**
|
|
|
|
** column = expr
|
|
|
|
**
|
|
|
|
** and we are evaluating a join, then the constraint on column is
|
|
|
|
** only valid if all tables referenced in expr occur to the left
|
|
|
|
** of the table containing column.
|
|
|
|
**
|
|
|
|
** The aConstraints[] array contains entries for all constraints
|
|
|
|
** on the current table. That way we only have to compute it once
|
|
|
|
** even though we might try to pick the best index multiple times.
|
|
|
|
** For each attempt at picking an index, the order of tables in the
|
|
|
|
** join might be different so we have to recompute the usable flag
|
|
|
|
** each time.
|
2006-06-13 01:59:13 +04:00
|
|
|
*/
|
|
|
|
pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
|
|
|
|
pUsage = pIdxInfo->aConstraintUsage;
|
|
|
|
for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
|
|
|
|
j = pIdxCons->iTermOffset;
|
|
|
|
pTerm = &pWC->a[j];
|
|
|
|
pIdxCons->usable = (pTerm->prereqRight & notReady)==0;
|
|
|
|
}
|
|
|
|
memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
|
2006-06-14 03:51:34 +04:00
|
|
|
if( pIdxInfo->needToFreeIdxStr ){
|
|
|
|
sqlite3_free(pIdxInfo->idxStr);
|
|
|
|
}
|
|
|
|
pIdxInfo->idxStr = 0;
|
|
|
|
pIdxInfo->idxNum = 0;
|
|
|
|
pIdxInfo->needToFreeIdxStr = 0;
|
2006-06-13 01:59:13 +04:00
|
|
|
pIdxInfo->orderByConsumed = 0;
|
2006-06-20 17:07:27 +04:00
|
|
|
pIdxInfo->estimatedCost = SQLITE_BIG_DBL / 2.0;
|
2006-06-13 01:59:13 +04:00
|
|
|
nOrderBy = pIdxInfo->nOrderBy;
|
|
|
|
if( pIdxInfo->nOrderBy && !orderByUsable ){
|
2006-06-13 05:04:52 +04:00
|
|
|
*(int*)&pIdxInfo->nOrderBy = 0;
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
2006-06-19 16:02:58 +04:00
|
|
|
|
2008-01-23 06:03:05 +03:00
|
|
|
(void)sqlite3SafetyOff(pParse->db);
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("xBestIndex for %s\n", pTab->zName));
|
2006-06-27 05:54:26 +04:00
|
|
|
TRACE_IDX_INPUTS(pIdxInfo);
|
2006-06-23 12:05:19 +04:00
|
|
|
rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
|
2006-06-27 05:54:26 +04:00
|
|
|
TRACE_IDX_OUTPUTS(pIdxInfo);
|
2008-03-17 12:36:44 +03:00
|
|
|
(void)sqlite3SafetyOn(pParse->db);
|
|
|
|
|
|
|
|
for(i=0; i<pIdxInfo->nConstraint; i++){
|
|
|
|
if( !pIdxInfo->aConstraint[i].usable && pUsage[i].argvIndex>0 ){
|
|
|
|
sqlite3ErrorMsg(pParse,
|
|
|
|
"table %s: xBestIndex returned an invalid plan", pTab->zName);
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-19 16:02:58 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
2006-06-23 12:05:19 +04:00
|
|
|
if( rc==SQLITE_NOMEM ){
|
2007-08-16 08:30:38 +04:00
|
|
|
pParse->db->mallocFailed = 1;
|
2006-06-23 12:05:19 +04:00
|
|
|
}else {
|
|
|
|
sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
|
|
|
|
}
|
2006-06-19 16:02:58 +04:00
|
|
|
}
|
2006-06-13 05:04:52 +04:00
|
|
|
*(int*)&pIdxInfo->nOrderBy = nOrderBy;
|
2007-03-02 11:12:22 +03:00
|
|
|
|
2006-06-13 01:59:13 +04:00
|
|
|
return pIdxInfo->estimatedCost;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
|
|
|
|
2005-07-21 07:14:59 +04:00
|
|
|
/*
|
2005-07-24 02:59:55 +04:00
|
|
|
** Find the best index for accessing a particular table. Return a pointer
|
|
|
|
** to the index, flags that describe how the index should be used, the
|
2005-07-29 00:51:19 +04:00
|
|
|
** number of equality constraints, and the "cost" for this index.
|
2005-07-24 02:59:55 +04:00
|
|
|
**
|
|
|
|
** The lowest cost index wins. The cost is an estimate of the amount of
|
|
|
|
** CPU and disk I/O need to process the request using the selected index.
|
|
|
|
** Factors that influence cost include:
|
|
|
|
**
|
|
|
|
** * The estimated number of rows that will be retrieved. (The
|
|
|
|
** fewer the better.)
|
|
|
|
**
|
|
|
|
** * Whether or not sorting must occur.
|
|
|
|
**
|
|
|
|
** * Whether or not there must be separate lookups in the
|
|
|
|
** index and in the main table.
|
|
|
|
**
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
|
|
|
static double bestIndex(
|
|
|
|
Parse *pParse, /* The parsing context */
|
|
|
|
WhereClause *pWC, /* The WHERE clause */
|
|
|
|
struct SrcList_item *pSrc, /* The FROM clause term to search */
|
|
|
|
Bitmask notReady, /* Mask of cursors that are not available */
|
|
|
|
ExprList *pOrderBy, /* The order by clause */
|
|
|
|
Index **ppIndex, /* Make *ppIndex point to the best index */
|
2005-07-24 02:59:55 +04:00
|
|
|
int *pFlags, /* Put flags describing this choice in *pFlags */
|
|
|
|
int *pnEq /* Put the number of == or IN constraints here */
|
2005-07-21 07:14:59 +04:00
|
|
|
){
|
|
|
|
WhereTerm *pTerm;
|
2005-07-24 02:59:55 +04:00
|
|
|
Index *bestIdx = 0; /* Index that gives the lowest cost */
|
2005-10-13 06:09:49 +04:00
|
|
|
double lowestCost; /* The cost of using bestIdx */
|
2005-07-24 02:59:55 +04:00
|
|
|
int bestFlags = 0; /* Flags associated with bestIdx */
|
|
|
|
int bestNEq = 0; /* Best value for nEq */
|
|
|
|
int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
|
|
|
|
Index *pProbe; /* An index we are evaluating */
|
|
|
|
int rev; /* True to scan in reverse order */
|
|
|
|
int flags; /* Flags associated with pProbe */
|
|
|
|
int nEq; /* Number of == or IN constraints */
|
2007-01-19 04:06:01 +03:00
|
|
|
int eqTermMask; /* Mask of valid equality operators */
|
2005-07-24 02:59:55 +04:00
|
|
|
double cost; /* Cost of using pProbe */
|
|
|
|
|
2008-05-28 22:01:44 +04:00
|
|
|
WHERETRACE(("bestIndex: tbl=%s notReady=%llx\n", pSrc->pTab->zName, notReady));
|
2005-10-13 06:09:49 +04:00
|
|
|
lowestCost = SQLITE_BIG_DBL;
|
2006-03-29 03:55:57 +04:00
|
|
|
pProbe = pSrc->pTab->pIndex;
|
|
|
|
|
|
|
|
/* If the table has no indices and there are no terms in the where
|
|
|
|
** clause that refer to the ROWID, then we will never be able to do
|
|
|
|
** anything other than a full table scan on this table. We might as
|
|
|
|
** well put it first in the join order. That way, perhaps it can be
|
|
|
|
** referenced by other tables in the join.
|
|
|
|
*/
|
|
|
|
if( pProbe==0 &&
|
|
|
|
findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IN|WO_LT|WO_LE|WO_GT|WO_GE,0)==0 &&
|
2007-02-06 16:26:32 +03:00
|
|
|
(pOrderBy==0 || !sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev)) ){
|
2006-03-29 03:55:57 +04:00
|
|
|
*pFlags = 0;
|
|
|
|
*ppIndex = 0;
|
|
|
|
*pnEq = 0;
|
|
|
|
return 0.0;
|
|
|
|
}
|
2005-07-24 02:59:55 +04:00
|
|
|
|
|
|
|
/* Check for a rowid=EXPR or rowid IN (...) constraints
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
|
|
|
pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
|
|
|
|
if( pTerm ){
|
2005-07-29 00:51:19 +04:00
|
|
|
Expr *pExpr;
|
2005-07-21 07:14:59 +04:00
|
|
|
*ppIndex = 0;
|
2005-07-24 02:59:55 +04:00
|
|
|
bestFlags = WHERE_ROWID_EQ;
|
2006-01-23 16:22:09 +03:00
|
|
|
if( pTerm->eOperator & WO_EQ ){
|
2005-07-28 00:41:43 +04:00
|
|
|
/* Rowid== is always the best pick. Look no further. Because only
|
|
|
|
** a single row is generated, output is always in sorted order */
|
2005-07-29 23:43:58 +04:00
|
|
|
*pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
|
2005-07-24 02:59:55 +04:00
|
|
|
*pnEq = 1;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... best is rowid\n"));
|
2005-07-24 02:59:55 +04:00
|
|
|
return 0.0;
|
2005-07-29 00:51:19 +04:00
|
|
|
}else if( (pExpr = pTerm->pExpr)->pList!=0 ){
|
2005-07-28 00:41:43 +04:00
|
|
|
/* Rowid IN (LIST): cost is NlogN where N is the number of list
|
|
|
|
** elements. */
|
2005-07-29 00:51:19 +04:00
|
|
|
lowestCost = pExpr->pList->nExpr;
|
2005-07-28 00:41:43 +04:00
|
|
|
lowestCost *= estLog(lowestCost);
|
2005-07-21 07:14:59 +04:00
|
|
|
}else{
|
2005-07-28 00:41:43 +04:00
|
|
|
/* Rowid IN (SELECT): cost is NlogN where N is the number of rows
|
|
|
|
** in the result of the inner select. We have no way to estimate
|
|
|
|
** that value so make a wild guess. */
|
2005-10-13 06:09:49 +04:00
|
|
|
lowestCost = 200;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... rowid IN cost: %.9g\n", lowestCost));
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
|
2005-07-28 00:41:43 +04:00
|
|
|
/* Estimate the cost of a table scan. If we do not know how many
|
|
|
|
** entries are in the table, use 1 million as a guess.
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
2005-10-13 06:09:49 +04:00
|
|
|
cost = pProbe ? pProbe->aiRowEst[0] : 1000000;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... table scan base cost: %.9g\n", cost));
|
2005-07-28 00:41:43 +04:00
|
|
|
flags = WHERE_ROWID_RANGE;
|
|
|
|
|
|
|
|
/* Check for constraints on a range of rowids in a table scan.
|
|
|
|
*/
|
2005-07-21 07:14:59 +04:00
|
|
|
pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0);
|
|
|
|
if( pTerm ){
|
2005-07-24 02:59:55 +04:00
|
|
|
if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){
|
|
|
|
flags |= WHERE_TOP_LIMIT;
|
2005-10-13 06:09:49 +04:00
|
|
|
cost /= 3; /* Guess that rowid<EXPR eliminates two-thirds or rows */
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2005-07-24 02:59:55 +04:00
|
|
|
if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){
|
|
|
|
flags |= WHERE_BTM_LIMIT;
|
2005-10-13 06:09:49 +04:00
|
|
|
cost /= 3; /* Guess that rowid>EXPR eliminates two-thirds of rows */
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... rowid range reduces cost to %.9g\n", cost));
|
2005-07-24 02:59:55 +04:00
|
|
|
}else{
|
|
|
|
flags = 0;
|
|
|
|
}
|
2005-07-28 00:41:43 +04:00
|
|
|
|
|
|
|
/* If the table scan does not satisfy the ORDER BY clause, increase
|
|
|
|
** the cost by NlogN to cover the expense of sorting. */
|
|
|
|
if( pOrderBy ){
|
2007-02-06 16:26:32 +03:00
|
|
|
if( sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev) ){
|
2005-07-28 00:41:43 +04:00
|
|
|
flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE;
|
|
|
|
if( rev ){
|
|
|
|
flags |= WHERE_REVERSE;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
cost += cost*estLog(cost);
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... sorting increases cost to %.9g\n", cost));
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
|
|
|
if( cost<lowestCost ){
|
|
|
|
lowestCost = cost;
|
2005-07-21 07:14:59 +04:00
|
|
|
bestFlags = flags;
|
|
|
|
}
|
|
|
|
|
2007-01-19 04:06:01 +03:00
|
|
|
/* If the pSrc table is the right table of a LEFT JOIN then we may not
|
|
|
|
** use an index to satisfy IS NULL constraints on that table. This is
|
|
|
|
** because columns might end up being NULL if the table does not match -
|
|
|
|
** a circumstance which the index cannot help us discover. Ticket #2177.
|
|
|
|
*/
|
|
|
|
if( (pSrc->jointype & JT_LEFT)!=0 ){
|
|
|
|
eqTermMask = WO_EQ|WO_IN;
|
|
|
|
}else{
|
|
|
|
eqTermMask = WO_EQ|WO_IN|WO_ISNULL;
|
|
|
|
}
|
|
|
|
|
2005-07-21 07:14:59 +04:00
|
|
|
/* Look at each index.
|
|
|
|
*/
|
2005-07-24 02:59:55 +04:00
|
|
|
for(; pProbe; pProbe=pProbe->pNext){
|
|
|
|
int i; /* Loop counter */
|
2005-10-13 06:09:49 +04:00
|
|
|
double inMultiplier = 1;
|
2005-07-24 02:59:55 +04:00
|
|
|
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("... index %s:\n", pProbe->zName));
|
2005-07-21 07:14:59 +04:00
|
|
|
|
|
|
|
/* Count the number of columns in the index that are satisfied
|
|
|
|
** by x=EXPR constraints or x IN (...) constraints.
|
|
|
|
*/
|
2005-07-24 02:59:55 +04:00
|
|
|
flags = 0;
|
2005-07-21 07:14:59 +04:00
|
|
|
for(i=0; i<pProbe->nColumn; i++){
|
|
|
|
int j = pProbe->aiColumn[i];
|
2007-01-19 04:06:01 +03:00
|
|
|
pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pProbe);
|
2005-07-21 07:14:59 +04:00
|
|
|
if( pTerm==0 ) break;
|
2005-07-24 02:59:55 +04:00
|
|
|
flags |= WHERE_COLUMN_EQ;
|
2006-01-23 16:22:09 +03:00
|
|
|
if( pTerm->eOperator & WO_IN ){
|
2005-07-29 00:51:19 +04:00
|
|
|
Expr *pExpr = pTerm->pExpr;
|
2005-07-24 02:59:55 +04:00
|
|
|
flags |= WHERE_COLUMN_IN;
|
2005-07-29 00:51:19 +04:00
|
|
|
if( pExpr->pSelect!=0 ){
|
2006-05-11 17:26:25 +04:00
|
|
|
inMultiplier *= 25;
|
2005-07-29 00:51:19 +04:00
|
|
|
}else if( pExpr->pList!=0 ){
|
2005-10-13 06:09:49 +04:00
|
|
|
inMultiplier *= pExpr->pList->nExpr + 1;
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
}
|
2005-07-28 00:41:43 +04:00
|
|
|
cost = pProbe->aiRowEst[i] * inMultiplier * estLog(inMultiplier);
|
2005-07-24 02:59:55 +04:00
|
|
|
nEq = i;
|
2005-07-29 23:43:58 +04:00
|
|
|
if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
|
|
|
|
&& nEq==pProbe->nColumn ){
|
|
|
|
flags |= WHERE_UNIQUE;
|
|
|
|
}
|
2007-09-13 21:54:40 +04:00
|
|
|
WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n",nEq,inMultiplier,cost));
|
2005-07-22 04:31:39 +04:00
|
|
|
|
2005-07-21 07:14:59 +04:00
|
|
|
/* Look for range constraints
|
|
|
|
*/
|
2005-07-24 02:59:55 +04:00
|
|
|
if( nEq<pProbe->nColumn ){
|
2005-07-21 07:14:59 +04:00
|
|
|
int j = pProbe->aiColumn[nEq];
|
|
|
|
pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe);
|
|
|
|
if( pTerm ){
|
2005-07-29 00:51:19 +04:00
|
|
|
flags |= WHERE_COLUMN_RANGE;
|
2005-07-24 02:59:55 +04:00
|
|
|
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pProbe) ){
|
2005-07-21 07:14:59 +04:00
|
|
|
flags |= WHERE_TOP_LIMIT;
|
2005-10-13 06:09:49 +04:00
|
|
|
cost /= 3;
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
|
|
|
if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){
|
2005-07-21 07:14:59 +04:00
|
|
|
flags |= WHERE_BTM_LIMIT;
|
2005-10-13 06:09:49 +04:00
|
|
|
cost /= 3;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("...... range reduces cost to %.9g\n", cost));
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-28 00:41:43 +04:00
|
|
|
/* Add the additional cost of sorting if that is a factor.
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
2005-07-28 00:41:43 +04:00
|
|
|
if( pOrderBy ){
|
|
|
|
if( (flags & WHERE_COLUMN_IN)==0 &&
|
2007-02-06 16:26:32 +03:00
|
|
|
isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev) ){
|
2005-07-28 00:41:43 +04:00
|
|
|
if( flags==0 ){
|
|
|
|
flags = WHERE_COLUMN_RANGE;
|
|
|
|
}
|
|
|
|
flags |= WHERE_ORDERBY;
|
|
|
|
if( rev ){
|
|
|
|
flags |= WHERE_REVERSE;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
cost += cost*estLog(cost);
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check to see if we can get away with using just the index without
|
2005-07-24 02:59:55 +04:00
|
|
|
** ever reading the table. If that is the case, then halve the
|
|
|
|
** cost of this index.
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
2005-07-24 02:59:55 +04:00
|
|
|
if( flags && pSrc->colUsed < (((Bitmask)1)<<(BMS-1)) ){
|
2005-07-21 07:14:59 +04:00
|
|
|
Bitmask m = pSrc->colUsed;
|
|
|
|
int j;
|
|
|
|
for(j=0; j<pProbe->nColumn; j++){
|
|
|
|
int x = pProbe->aiColumn[j];
|
|
|
|
if( x<BMS-1 ){
|
|
|
|
m &= ~(((Bitmask)1)<<x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( m==0 ){
|
|
|
|
flags |= WHERE_IDX_ONLY;
|
2005-10-13 06:09:49 +04:00
|
|
|
cost /= 2;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("...... idx-only reduces cost to %.9g\n", cost));
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-24 02:59:55 +04:00
|
|
|
/* If this index has achieved the lowest cost so far, then use it.
|
2005-07-21 07:14:59 +04:00
|
|
|
*/
|
2007-09-13 21:54:40 +04:00
|
|
|
if( flags && cost < lowestCost ){
|
2005-07-21 07:14:59 +04:00
|
|
|
bestIdx = pProbe;
|
2005-07-24 02:59:55 +04:00
|
|
|
lowestCost = cost;
|
2005-07-21 07:14:59 +04:00
|
|
|
bestFlags = flags;
|
2005-07-24 02:59:55 +04:00
|
|
|
bestNEq = nEq;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Report the best result
|
|
|
|
*/
|
|
|
|
*ppIndex = bestIdx;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
|
2005-07-24 02:59:55 +04:00
|
|
|
bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq));
|
2007-01-25 19:56:06 +03:00
|
|
|
*pFlags = bestFlags | eqTermMask;
|
2005-07-24 02:59:55 +04:00
|
|
|
*pnEq = bestNEq;
|
|
|
|
return lowestCost;
|
2005-07-21 07:14:59 +04:00
|
|
|
}
|
|
|
|
|
2004-11-22 22:12:19 +03:00
|
|
|
|
2004-07-19 23:14:01 +04:00
|
|
|
/*
|
|
|
|
** Disable a term in the WHERE clause. Except, do not disable the term
|
|
|
|
** if it controls a LEFT OUTER JOIN and it did not originate in the ON
|
|
|
|
** or USING clause of that join.
|
|
|
|
**
|
|
|
|
** Consider the term t2.z='ok' in the following queries:
|
|
|
|
**
|
|
|
|
** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
|
|
|
|
** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
|
|
|
|
** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
|
|
|
|
**
|
2004-12-14 06:34:34 +03:00
|
|
|
** The t2.z='ok' is disabled in the in (2) because it originates
|
2004-07-19 23:14:01 +04:00
|
|
|
** in the ON clause. The term is disabled in (3) because it is not part
|
|
|
|
** of a LEFT OUTER JOIN. In (1), the term is not disabled.
|
|
|
|
**
|
|
|
|
** Disabling a term causes that term to not be tested in the inner loop
|
2005-09-20 12:47:20 +04:00
|
|
|
** of the join. Disabling is an optimization. When terms are satisfied
|
|
|
|
** by indices, we disable them to prevent redundant tests in the inner
|
|
|
|
** loop. We would get the correct results if nothing were ever disabled,
|
|
|
|
** but joins might run a little slower. The trick is to disable as much
|
|
|
|
** as we can without disabling too much. If we disabled in (1), we'd get
|
|
|
|
** the wrong answer. See ticket #813.
|
2004-07-19 23:14:01 +04:00
|
|
|
*/
|
2005-07-19 21:38:22 +04:00
|
|
|
static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
|
|
|
|
if( pTerm
|
|
|
|
&& (pTerm->flags & TERM_CODED)==0
|
|
|
|
&& (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
|
|
|
|
){
|
|
|
|
pTerm->flags |= TERM_CODED;
|
2005-08-02 21:48:22 +04:00
|
|
|
if( pTerm->iParent>=0 ){
|
|
|
|
WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
|
|
|
|
if( (--pOther->nChild)==0 ){
|
2005-07-29 03:12:08 +04:00
|
|
|
disableTerm(pLevel, pOther);
|
|
|
|
}
|
2005-07-19 21:38:22 +04:00
|
|
|
}
|
2004-07-19 23:14:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-25 17:12:14 +04:00
|
|
|
/*
|
2008-04-18 14:25:24 +04:00
|
|
|
** Apply the affinities associated with the first n columns of index
|
|
|
|
** pIdx to the values in the n registers starting at base.
|
2004-09-25 17:12:14 +04:00
|
|
|
*/
|
2008-04-18 14:25:24 +04:00
|
|
|
static void codeApplyAffinity(Parse *pParse, int base, int n, Index *pIdx){
|
|
|
|
if( n>0 ){
|
|
|
|
Vdbe *v = pParse->pVdbe;
|
|
|
|
assert( v!=0 );
|
|
|
|
sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
|
|
|
|
sqlite3IndexAffinityStr(v, pIdx);
|
|
|
|
sqlite3ExprCacheAffinityChange(pParse, base, n);
|
|
|
|
}
|
2004-09-25 17:12:14 +04:00
|
|
|
}
|
|
|
|
|
2005-07-20 02:22:12 +04:00
|
|
|
|
2004-09-25 17:12:14 +04:00
|
|
|
/*
|
2005-07-24 02:59:55 +04:00
|
|
|
** Generate code for a single equality term of the WHERE clause. An equality
|
|
|
|
** term can be either X=expr or X IN (...). pTerm is the term to be
|
|
|
|
** coded.
|
|
|
|
**
|
2008-01-17 05:36:28 +03:00
|
|
|
** The current value for the constraint is left in register iReg.
|
2005-07-24 02:59:55 +04:00
|
|
|
**
|
|
|
|
** For a constraint of the form X=expr, the expression is evaluated and its
|
|
|
|
** result is left on the stack. For constraints of the form X IN (...)
|
|
|
|
** this routine sets up a loop that will iterate over all values of X.
|
2004-09-25 17:12:14 +04:00
|
|
|
*/
|
2008-03-31 22:19:54 +04:00
|
|
|
static int codeEqualityTerm(
|
2004-09-25 17:12:14 +04:00
|
|
|
Parse *pParse, /* The parsing context */
|
2005-07-22 04:31:39 +04:00
|
|
|
WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
|
2008-01-17 05:36:28 +03:00
|
|
|
WhereLevel *pLevel, /* When level of the FROM clause we are working on */
|
2008-03-31 22:19:54 +04:00
|
|
|
int iTarget /* Attempt to leave results in this register */
|
2004-09-25 17:12:14 +04:00
|
|
|
){
|
2005-07-19 21:38:22 +04:00
|
|
|
Expr *pX = pTerm->pExpr;
|
2006-10-28 04:28:09 +04:00
|
|
|
Vdbe *v = pParse->pVdbe;
|
2008-03-31 22:19:54 +04:00
|
|
|
int iReg; /* Register holding results */
|
2008-01-17 05:36:28 +03:00
|
|
|
|
2008-03-31 22:19:54 +04:00
|
|
|
if( iTarget<=0 ){
|
|
|
|
iReg = iTarget = sqlite3GetTempReg(pParse);
|
|
|
|
}
|
2006-10-28 04:28:09 +04:00
|
|
|
if( pX->op==TK_EQ ){
|
2008-03-31 22:19:54 +04:00
|
|
|
iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
|
2006-10-28 04:28:09 +04:00
|
|
|
}else if( pX->op==TK_ISNULL ){
|
2008-03-31 22:19:54 +04:00
|
|
|
iReg = iTarget;
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
|
2005-01-29 11:32:43 +03:00
|
|
|
#ifndef SQLITE_OMIT_SUBQUERY
|
2004-09-25 17:12:14 +04:00
|
|
|
}else{
|
2007-11-29 20:05:18 +03:00
|
|
|
int eType;
|
2005-01-29 11:32:43 +03:00
|
|
|
int iTab;
|
2007-03-28 18:30:06 +04:00
|
|
|
struct InLoop *pIn;
|
2005-01-29 11:32:43 +03:00
|
|
|
|
2006-10-28 04:28:09 +04:00
|
|
|
assert( pX->op==TK_IN );
|
2008-03-31 22:19:54 +04:00
|
|
|
iReg = iTarget;
|
2008-06-26 22:04:03 +04:00
|
|
|
eType = sqlite3FindInIndex(pParse, pX, 0);
|
2005-01-29 11:32:43 +03:00
|
|
|
iTab = pX->iTable;
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
|
2008-01-02 03:34:36 +03:00
|
|
|
VdbeComment((v, "%.*s", pX->span.n, pX->span.z));
|
2007-03-28 18:30:06 +04:00
|
|
|
if( pLevel->nIn==0 ){
|
|
|
|
pLevel->nxt = sqlite3VdbeMakeLabel(v);
|
|
|
|
}
|
2005-07-22 04:31:39 +04:00
|
|
|
pLevel->nIn++;
|
2007-08-16 08:30:38 +04:00
|
|
|
pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop,
|
2007-03-28 18:30:06 +04:00
|
|
|
sizeof(pLevel->aInLoop[0])*pLevel->nIn);
|
|
|
|
pIn = pLevel->aInLoop;
|
|
|
|
if( pIn ){
|
|
|
|
pIn += pLevel->nIn - 1;
|
|
|
|
pIn->iCur = iTab;
|
2008-01-17 05:36:28 +03:00
|
|
|
if( eType==IN_INDEX_ROWID ){
|
|
|
|
pIn->topAddr = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
|
|
|
|
}else{
|
|
|
|
pIn->topAddr = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
|
|
|
|
}
|
|
|
|
sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
|
2005-07-29 00:51:19 +04:00
|
|
|
}else{
|
|
|
|
pLevel->nIn = 0;
|
2005-07-22 04:31:39 +04:00
|
|
|
}
|
2005-01-29 11:32:43 +03:00
|
|
|
#endif
|
2004-09-25 17:12:14 +04:00
|
|
|
}
|
2005-07-19 21:38:22 +04:00
|
|
|
disableTerm(pLevel, pTerm);
|
2008-03-31 22:19:54 +04:00
|
|
|
return iReg;
|
2004-09-25 17:12:14 +04:00
|
|
|
}
|
|
|
|
|
2005-07-24 02:59:55 +04:00
|
|
|
/*
|
|
|
|
** Generate code that will evaluate all == and IN constraints for an
|
|
|
|
** index. The values for all constraints are left on the stack.
|
|
|
|
**
|
|
|
|
** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
|
|
|
|
** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
|
|
|
|
** The index has as many as three equality constraints, but in this
|
|
|
|
** example, the third "c" value is an inequality. So only two
|
|
|
|
** constraints are coded. This routine will generate code to evaluate
|
|
|
|
** a==5 and b IN (1,2,3). The current values for a and b will be left
|
|
|
|
** on the stack - a is the deepest and b the shallowest.
|
|
|
|
**
|
|
|
|
** In the example above nEq==2. But this subroutine works for any value
|
|
|
|
** of nEq including 0. If nEq==0, this routine is nearly a no-op.
|
|
|
|
** The only thing it does is allocate the pLevel->iMem memory cell.
|
|
|
|
**
|
|
|
|
** This routine always allocates at least one memory cell and puts
|
|
|
|
** the address of that memory cell in pLevel->iMem. The code that
|
|
|
|
** calls this routine will use pLevel->iMem to store the termination
|
|
|
|
** key value of the loop. If one or more IN operators appear, then
|
|
|
|
** this routine allocates an additional nEq memory cells for internal
|
|
|
|
** use.
|
|
|
|
*/
|
2008-01-17 05:36:28 +03:00
|
|
|
static int codeAllEqualityTerms(
|
2005-07-24 02:59:55 +04:00
|
|
|
Parse *pParse, /* Parsing context */
|
|
|
|
WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
|
|
|
|
WhereClause *pWC, /* The WHERE clause */
|
2008-01-17 05:36:28 +03:00
|
|
|
Bitmask notReady, /* Which parts of FROM have not yet been coded */
|
|
|
|
int nExtraReg /* Number of extra registers to allocate */
|
2005-07-24 02:59:55 +04:00
|
|
|
){
|
|
|
|
int nEq = pLevel->nEq; /* The number of == or IN constraints to code */
|
|
|
|
Vdbe *v = pParse->pVdbe; /* The virtual machine under construction */
|
|
|
|
Index *pIdx = pLevel->pIdx; /* The index being used for this loop */
|
|
|
|
int iCur = pLevel->iTabCur; /* The cursor of the table */
|
|
|
|
WhereTerm *pTerm; /* A single constraint term */
|
|
|
|
int j; /* Loop counter */
|
2008-01-17 05:36:28 +03:00
|
|
|
int regBase; /* Base register */
|
2005-07-24 02:59:55 +04:00
|
|
|
|
|
|
|
/* Figure out how many memory cells we will need then allocate them.
|
|
|
|
** We always need at least one used to store the loop terminator
|
|
|
|
** value. If there are IN operators we'll need one for each == or
|
|
|
|
** IN constraint.
|
|
|
|
*/
|
2008-01-17 05:36:28 +03:00
|
|
|
pLevel->iMem = pParse->nMem + 1;
|
|
|
|
regBase = pParse->nMem + 2;
|
|
|
|
pParse->nMem += pLevel->nEq + 2 + nExtraReg;
|
2005-07-24 02:59:55 +04:00
|
|
|
|
|
|
|
/* Evaluate the equality constraints
|
|
|
|
*/
|
2007-01-19 04:06:01 +03:00
|
|
|
assert( pIdx->nColumn>=nEq );
|
|
|
|
for(j=0; j<nEq; j++){
|
2008-03-31 22:19:54 +04:00
|
|
|
int r1;
|
2005-07-24 02:59:55 +04:00
|
|
|
int k = pIdx->aiColumn[j];
|
2007-01-25 19:56:06 +03:00
|
|
|
pTerm = findTerm(pWC, iCur, k, notReady, pLevel->flags, pIdx);
|
2005-07-24 02:59:55 +04:00
|
|
|
if( pTerm==0 ) break;
|
|
|
|
assert( (pTerm->flags & TERM_CODED)==0 );
|
2008-03-31 22:19:54 +04:00
|
|
|
r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
|
|
|
|
if( r1!=regBase+j ){
|
|
|
|
sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
|
|
|
|
}
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pTerm->eOperator & WO_ISNULL );
|
|
|
|
testcase( pTerm->eOperator & WO_IN );
|
2007-03-28 18:30:06 +04:00
|
|
|
if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->brk);
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
return regBase;
|
2005-07-24 02:59:55 +04:00
|
|
|
}
|
|
|
|
|
2005-09-19 17:15:23 +04:00
|
|
|
#if defined(SQLITE_TEST)
|
2005-07-15 17:05:21 +04:00
|
|
|
/*
|
|
|
|
** The following variable holds a text description of query plan generated
|
|
|
|
** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
|
|
|
|
** overwrites the previous. This information is used for testing and
|
|
|
|
** analysis only.
|
|
|
|
*/
|
|
|
|
char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
|
|
|
|
static int nQPlan = 0; /* Next free slow in _query_plan[] */
|
|
|
|
|
|
|
|
#endif /* SQLITE_TEST */
|
|
|
|
|
|
|
|
|
2006-06-13 01:59:13 +04:00
|
|
|
/*
|
|
|
|
** Free a WhereInfo structure
|
|
|
|
*/
|
|
|
|
static void whereInfoFree(WhereInfo *pWInfo){
|
|
|
|
if( pWInfo ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pWInfo->nLevel; i++){
|
2006-06-14 03:51:34 +04:00
|
|
|
sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
|
|
|
|
if( pInfo ){
|
2008-01-23 15:52:40 +03:00
|
|
|
assert( pInfo->needToFreeIdxStr==0 );
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(pInfo);
|
2006-06-13 19:00:54 +04:00
|
|
|
}
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(pWInfo);
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-25 17:12:14 +04:00
|
|
|
|
2002-06-19 18:27:05 +04:00
|
|
|
/*
|
|
|
|
** Generate the beginning of the loop used for WHERE clause processing.
|
2005-01-03 04:27:18 +03:00
|
|
|
** The return value is a pointer to an opaque structure that contains
|
2000-05-29 18:26:00 +04:00
|
|
|
** information needed to terminate the loop. Later, the calling routine
|
2004-05-08 12:23:19 +04:00
|
|
|
** should invoke sqlite3WhereEnd() with the return value of this function
|
2000-05-29 18:26:00 +04:00
|
|
|
** in order to complete the WHERE clause processing.
|
|
|
|
**
|
|
|
|
** If an error occurs, this routine returns NULL.
|
2002-06-15 00:58:45 +04:00
|
|
|
**
|
|
|
|
** The basic idea is to do a nested loop, one loop for each table in
|
|
|
|
** the FROM clause of a select. (INSERT and UPDATE statements are the
|
|
|
|
** same as a SELECT with only a single table in the FROM clause.) For
|
|
|
|
** example, if the SQL is this:
|
|
|
|
**
|
|
|
|
** SELECT * FROM t1, t2, t3 WHERE ...;
|
|
|
|
**
|
|
|
|
** Then the code generated is conceptually like the following:
|
|
|
|
**
|
|
|
|
** foreach row1 in t1 do \ Code generated
|
2004-05-08 12:23:19 +04:00
|
|
|
** foreach row2 in t2 do |-- by sqlite3WhereBegin()
|
2002-06-15 00:58:45 +04:00
|
|
|
** foreach row3 in t3 do /
|
|
|
|
** ...
|
|
|
|
** end \ Code generated
|
2004-05-08 12:23:19 +04:00
|
|
|
** end |-- by sqlite3WhereEnd()
|
2002-06-15 00:58:45 +04:00
|
|
|
** end /
|
|
|
|
**
|
2005-07-21 22:23:20 +04:00
|
|
|
** Note that the loops might not be nested in the order in which they
|
|
|
|
** appear in the FROM clause if a different order is better able to make
|
2005-07-24 02:59:55 +04:00
|
|
|
** use of indices. Note also that when the IN operator appears in
|
|
|
|
** the WHERE clause, it might result in additional nested loops for
|
|
|
|
** scanning through all values on the right-hand side of the IN.
|
2005-07-21 22:23:20 +04:00
|
|
|
**
|
2002-06-15 00:58:45 +04:00
|
|
|
** There are Btree cursors associated with each table. t1 uses cursor
|
2003-05-02 18:32:12 +04:00
|
|
|
** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
|
|
|
|
** And so forth. This routine generates code to open those VDBE cursors
|
2004-05-08 12:23:19 +04:00
|
|
|
** and sqlite3WhereEnd() generates the code to close them.
|
2002-06-15 00:58:45 +04:00
|
|
|
**
|
2004-12-25 04:03:13 +03:00
|
|
|
** The code that sqlite3WhereBegin() generates leaves the cursors named
|
|
|
|
** in pTabList pointing at their appropriate entries. The [...] code
|
2005-06-13 01:35:51 +04:00
|
|
|
** can use OP_Column and OP_Rowid opcodes on these cursors to extract
|
2004-12-25 04:03:13 +03:00
|
|
|
** data from the various tables of the loop.
|
|
|
|
**
|
2002-06-15 00:58:45 +04:00
|
|
|
** If the WHERE clause is empty, the foreach loops must each scan their
|
|
|
|
** entire tables. Thus a three-way join is an O(N^3) operation. But if
|
|
|
|
** the tables have indices and there are terms in the WHERE clause that
|
|
|
|
** refer to those indices, a complete table scan can be avoided and the
|
|
|
|
** code will run much faster. Most of the work of this routine is checking
|
|
|
|
** to see if there are indices that can be used to speed up the loop.
|
|
|
|
**
|
|
|
|
** Terms of the WHERE clause are also used to limit which rows actually
|
|
|
|
** make it to the "..." in the middle of the loop. After each "foreach",
|
|
|
|
** terms of the WHERE clause that use only terms in that loop and outer
|
|
|
|
** loops are evaluated and if false a jump is made around all subsequent
|
|
|
|
** inner loops (or around the "..." if the test occurs within the inner-
|
|
|
|
** most loop)
|
|
|
|
**
|
|
|
|
** OUTER JOINS
|
|
|
|
**
|
|
|
|
** An outer join of tables t1 and t2 is conceptally coded as follows:
|
|
|
|
**
|
|
|
|
** foreach row1 in t1 do
|
|
|
|
** flag = 0
|
|
|
|
** foreach row2 in t2 do
|
|
|
|
** start:
|
|
|
|
** ...
|
|
|
|
** flag = 1
|
|
|
|
** end
|
2002-06-19 18:27:05 +04:00
|
|
|
** if flag==0 then
|
|
|
|
** move the row2 cursor to a null row
|
|
|
|
** goto start
|
|
|
|
** fi
|
2002-06-15 00:58:45 +04:00
|
|
|
** end
|
|
|
|
**
|
2002-06-19 18:27:05 +04:00
|
|
|
** ORDER BY CLAUSE PROCESSING
|
|
|
|
**
|
|
|
|
** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
|
|
|
|
** if there is one. If there is no ORDER BY clause or if this routine
|
|
|
|
** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
|
|
|
|
**
|
|
|
|
** If an index can be used so that the natural output order of the table
|
|
|
|
** scan is correct for the ORDER BY clause, then that index is used and
|
|
|
|
** *ppOrderBy is set to NULL. This is an optimization that prevents an
|
|
|
|
** unnecessary sort of the result set if an index appropriate for the
|
|
|
|
** ORDER BY clause already exists.
|
|
|
|
**
|
|
|
|
** If the where clause loops cannot be arranged to provide the correct
|
|
|
|
** output order, then the *ppOrderBy is unchanged.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
WhereInfo *sqlite3WhereBegin(
|
2004-11-16 18:50:19 +03:00
|
|
|
Parse *pParse, /* The parser context */
|
|
|
|
SrcList *pTabList, /* A list of all tables to be scanned */
|
|
|
|
Expr *pWhere, /* The WHERE clause */
|
2008-01-05 20:39:29 +03:00
|
|
|
ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
|
2008-04-10 17:33:18 +04:00
|
|
|
u8 wflags /* One of the WHERE_* flags defined in sqliteInt.h */
|
2000-05-29 18:26:00 +04:00
|
|
|
){
|
|
|
|
int i; /* Loop counter */
|
|
|
|
WhereInfo *pWInfo; /* Will become the return value of this function */
|
|
|
|
Vdbe *v = pParse->pVdbe; /* The virtual database engine */
|
2003-07-16 04:54:31 +04:00
|
|
|
int brk, cont = 0; /* Addresses used during code generation */
|
2005-07-21 07:14:59 +04:00
|
|
|
Bitmask notReady; /* Cursors that are not yet positioned */
|
2005-07-16 17:33:20 +04:00
|
|
|
WhereTerm *pTerm; /* A single term in the WHERE clause */
|
|
|
|
ExprMaskSet maskSet; /* The expression mask set */
|
|
|
|
WhereClause wc; /* The WHERE clause is divided into these terms */
|
2004-12-19 03:11:35 +03:00
|
|
|
struct SrcList_item *pTabItem; /* A single entry from pTabList */
|
|
|
|
WhereLevel *pLevel; /* A single level in the pWInfo list */
|
2005-07-21 22:23:20 +04:00
|
|
|
int iFrom; /* First unused FROM clause element */
|
2005-07-29 23:43:58 +04:00
|
|
|
int andFlags; /* AND-ed combination of all wc.a[].flags */
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3 *db; /* Database connection */
|
2008-01-05 20:39:29 +03:00
|
|
|
ExprList *pOrderBy = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* The number of tables in the FROM clause is limited by the number of
|
2005-01-20 02:24:50 +03:00
|
|
|
** bits in a Bitmask
|
|
|
|
*/
|
2005-07-21 22:23:20 +04:00
|
|
|
if( pTabList->nSrc>BMS ){
|
|
|
|
sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
|
2005-01-20 02:24:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-05 20:39:29 +03:00
|
|
|
if( ppOrderBy ){
|
|
|
|
pOrderBy = *ppOrderBy;
|
|
|
|
}
|
|
|
|
|
2002-06-28 05:02:38 +04:00
|
|
|
/* Split the WHERE clause into separate subexpressions where each
|
2005-07-21 22:23:20 +04:00
|
|
|
** subexpression is separated by an AND operator.
|
2002-06-28 05:02:38 +04:00
|
|
|
*/
|
2003-05-02 18:32:12 +04:00
|
|
|
initMaskSet(&maskSet);
|
2007-02-06 16:26:32 +03:00
|
|
|
whereClauseInit(&wc, pParse, &maskSet);
|
2008-03-31 22:19:54 +04:00
|
|
|
sqlite3ExprCodeConstants(pParse, pWhere);
|
2005-07-29 19:10:17 +04:00
|
|
|
whereSplit(&wc, pWhere, TK_AND);
|
2005-01-20 02:24:50 +03:00
|
|
|
|
2000-05-29 18:26:00 +04:00
|
|
|
/* Allocate and initialize the WhereInfo structure that will become the
|
|
|
|
** return value.
|
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
db = pParse->db;
|
|
|
|
pWInfo = sqlite3DbMallocZero(db,
|
|
|
|
sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
|
|
|
|
if( db->mallocFailed ){
|
2005-07-22 04:31:39 +04:00
|
|
|
goto whereBeginNoMem;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2006-06-19 08:49:34 +04:00
|
|
|
pWInfo->nLevel = pTabList->nSrc;
|
2000-05-29 18:26:00 +04:00
|
|
|
pWInfo->pParse = pParse;
|
|
|
|
pWInfo->pTabList = pTabList;
|
2004-05-08 12:23:19 +04:00
|
|
|
pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
|
2002-04-30 23:20:28 +04:00
|
|
|
|
|
|
|
/* Special case: a WHERE clause that is constant. Evaluate the
|
|
|
|
** expression and either jump over all of the code or fall thru.
|
|
|
|
*/
|
2007-06-08 04:20:47 +04:00
|
|
|
if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
|
2008-01-09 02:54:25 +03:00
|
|
|
sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
|
2002-06-15 02:38:41 +04:00
|
|
|
pWhere = 0;
|
2002-04-30 23:20:28 +04:00
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2008-03-26 17:56:34 +03:00
|
|
|
/* Assign a bit from the bitmask to every term in the FROM clause.
|
|
|
|
**
|
|
|
|
** When assigning bitmask values to FROM clause cursors, it must be
|
|
|
|
** the case that if X is the bitmask for the N-th FROM clause term then
|
|
|
|
** the bitmask for all FROM clause terms to the left of the N-th term
|
|
|
|
** is (X-1). An expression from the ON clause of a LEFT JOIN can use
|
|
|
|
** its Expr.iRightJoinTable value to find the bitmask of the right table
|
|
|
|
** of the join. Subtracting one from the right table bitmask gives a
|
|
|
|
** bitmask for all tables to the left of the join. Knowing the bitmask
|
|
|
|
** for all tables to the left of a left join is important. Ticket #3015.
|
|
|
|
*/
|
|
|
|
for(i=0; i<pTabList->nSrc; i++){
|
|
|
|
createMask(&maskSet, pTabList->a[i].iCursor);
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
|
|
|
Bitmask toTheLeft = 0;
|
|
|
|
for(i=0; i<pTabList->nSrc; i++){
|
|
|
|
Bitmask m = getMask(&maskSet, pTabList->a[i].iCursor);
|
|
|
|
assert( (m-1)==toTheLeft );
|
|
|
|
toTheLeft |= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Analyze all of the subexpressions. Note that exprAnalyze() might
|
|
|
|
** add new virtual terms onto the end of the WHERE clause. We do not
|
|
|
|
** want to analyze these virtual terms, so start analyzing at the end
|
2005-09-20 12:47:20 +04:00
|
|
|
** and work forward so that the added virtual terms are never processed.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2007-02-06 16:26:32 +03:00
|
|
|
exprAnalyzeAll(pTabList, &wc);
|
2007-08-16 08:30:38 +04:00
|
|
|
if( db->mallocFailed ){
|
2005-08-19 23:14:12 +04:00
|
|
|
goto whereBeginNoMem;
|
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Chose the best index to use for each table in the FROM clause.
|
|
|
|
**
|
2005-07-24 02:59:55 +04:00
|
|
|
** This loop fills in the following fields:
|
|
|
|
**
|
|
|
|
** pWInfo->a[].pIdx The index to use for this level of the loop.
|
|
|
|
** pWInfo->a[].flags WHERE_xxx flags associated with pIdx
|
|
|
|
** pWInfo->a[].nEq The number of == and IN constraints
|
|
|
|
** pWInfo->a[].iFrom When term of the FROM clause is being coded
|
|
|
|
** pWInfo->a[].iTabCur The VDBE cursor for the database table
|
|
|
|
** pWInfo->a[].iIdxCur The VDBE cursor for the index
|
|
|
|
**
|
|
|
|
** This loop also figures out the nesting order of tables in the FROM
|
|
|
|
** clause.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-07-21 07:14:59 +04:00
|
|
|
notReady = ~(Bitmask)0;
|
2004-12-19 03:11:35 +03:00
|
|
|
pTabItem = pTabList->a;
|
|
|
|
pLevel = pWInfo->a;
|
2005-07-29 23:43:58 +04:00
|
|
|
andFlags = ~0;
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("*** Optimizer Start ***\n"));
|
2005-07-21 22:23:20 +04:00
|
|
|
for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
|
|
|
|
Index *pIdx; /* Index for FROM table at pTabItem */
|
|
|
|
int flags; /* Flags asssociated with pIdx */
|
2005-07-24 02:59:55 +04:00
|
|
|
int nEq; /* Number of == or IN constraints */
|
|
|
|
double cost; /* The cost for pIdx */
|
2005-07-21 22:23:20 +04:00
|
|
|
int j; /* For looping over FROM tables */
|
|
|
|
Index *pBest = 0; /* The best index seen so far */
|
|
|
|
int bestFlags = 0; /* Flags associated with pBest */
|
2005-07-24 02:59:55 +04:00
|
|
|
int bestNEq = 0; /* nEq associated with pBest */
|
2005-10-13 06:09:49 +04:00
|
|
|
double lowestCost; /* Cost of the pBest */
|
2006-01-20 21:10:57 +03:00
|
|
|
int bestJ = 0; /* The value of j */
|
2005-07-21 22:23:20 +04:00
|
|
|
Bitmask m; /* Bitmask value for j or bestJ */
|
2006-02-01 05:45:02 +03:00
|
|
|
int once = 0; /* True when first table is seen */
|
2006-06-27 05:54:26 +04:00
|
|
|
sqlite3_index_info *pIndex; /* Current virtual index */
|
2005-07-21 22:23:20 +04:00
|
|
|
|
2005-10-13 06:09:49 +04:00
|
|
|
lowestCost = SQLITE_BIG_DBL;
|
2005-07-21 22:23:20 +04:00
|
|
|
for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
|
2006-06-06 15:45:54 +04:00
|
|
|
int doNotReorder; /* True if this table should not be reordered */
|
|
|
|
|
2006-12-16 19:25:15 +03:00
|
|
|
doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
|
2006-06-06 15:45:54 +04:00
|
|
|
if( once && doNotReorder ) break;
|
2005-07-21 22:23:20 +04:00
|
|
|
m = getMask(&maskSet, pTabItem->iCursor);
|
|
|
|
if( (m & notReady)==0 ){
|
|
|
|
if( j==iFrom ) iFrom++;
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-13 01:59:13 +04:00
|
|
|
assert( pTabItem->pTab );
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-14 23:00:20 +04:00
|
|
|
if( IsVirtual(pTabItem->pTab) ){
|
2006-06-27 05:54:26 +04:00
|
|
|
sqlite3_index_info **ppIdxInfo = &pWInfo->a[j].pIdxInfo;
|
2006-06-13 01:59:13 +04:00
|
|
|
cost = bestVirtualIndex(pParse, &wc, pTabItem, notReady,
|
|
|
|
ppOrderBy ? *ppOrderBy : 0, i==0,
|
2006-06-27 05:54:26 +04:00
|
|
|
ppIdxInfo);
|
2006-06-13 01:59:13 +04:00
|
|
|
flags = WHERE_VIRTUALTABLE;
|
2006-06-27 05:54:26 +04:00
|
|
|
pIndex = *ppIdxInfo;
|
2006-06-20 17:07:27 +04:00
|
|
|
if( pIndex && pIndex->orderByConsumed ){
|
2006-06-15 02:07:10 +04:00
|
|
|
flags = WHERE_VIRTUALTABLE | WHERE_ORDERBY;
|
|
|
|
}
|
2006-06-13 01:59:13 +04:00
|
|
|
pIdx = 0;
|
|
|
|
nEq = 0;
|
2007-03-02 11:12:22 +03:00
|
|
|
if( (SQLITE_BIG_DBL/2.0)<cost ){
|
|
|
|
/* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
|
|
|
|
** inital value of lowestCost in this loop. If it is, then
|
|
|
|
** the (cost<lowestCost) test below will never be true and
|
|
|
|
** pLevel->pBestIdx never set.
|
|
|
|
*/
|
|
|
|
cost = (SQLITE_BIG_DBL/2.0);
|
|
|
|
}
|
2006-06-13 01:59:13 +04:00
|
|
|
}else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
cost = bestIndex(pParse, &wc, pTabItem, notReady,
|
|
|
|
(i==0 && ppOrderBy) ? *ppOrderBy : 0,
|
|
|
|
&pIdx, &flags, &nEq);
|
2006-06-27 06:33:40 +04:00
|
|
|
pIndex = 0;
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
2005-07-24 02:59:55 +04:00
|
|
|
if( cost<lowestCost ){
|
2006-02-01 05:45:02 +03:00
|
|
|
once = 1;
|
2005-07-24 02:59:55 +04:00
|
|
|
lowestCost = cost;
|
2005-07-21 22:23:20 +04:00
|
|
|
pBest = pIdx;
|
|
|
|
bestFlags = flags;
|
2005-07-24 02:59:55 +04:00
|
|
|
bestNEq = nEq;
|
2005-07-21 22:23:20 +04:00
|
|
|
bestJ = j;
|
2006-06-27 05:54:26 +04:00
|
|
|
pLevel->pBestIdx = pIndex;
|
2005-07-21 22:23:20 +04:00
|
|
|
}
|
2006-06-06 15:45:54 +04:00
|
|
|
if( doNotReorder ) break;
|
2005-07-21 22:23:20 +04:00
|
|
|
}
|
2008-06-12 04:07:29 +04:00
|
|
|
WHERETRACE(("*** Optimizer selects table %d for loop %d\n", bestJ,
|
2005-09-10 19:28:09 +04:00
|
|
|
pLevel-pWInfo->a));
|
2005-07-29 23:43:58 +04:00
|
|
|
if( (bestFlags & WHERE_ORDERBY)!=0 ){
|
2005-07-21 07:14:59 +04:00
|
|
|
*ppOrderBy = 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2005-07-29 23:43:58 +04:00
|
|
|
andFlags &= bestFlags;
|
2005-07-21 22:23:20 +04:00
|
|
|
pLevel->flags = bestFlags;
|
2005-07-21 07:14:59 +04:00
|
|
|
pLevel->pIdx = pBest;
|
2005-07-24 02:59:55 +04:00
|
|
|
pLevel->nEq = bestNEq;
|
2005-07-22 04:31:39 +04:00
|
|
|
pLevel->aInLoop = 0;
|
|
|
|
pLevel->nIn = 0;
|
2005-07-21 07:14:59 +04:00
|
|
|
if( pBest ){
|
2004-12-19 03:11:35 +03:00
|
|
|
pLevel->iIdxCur = pParse->nTab++;
|
2005-07-21 07:14:59 +04:00
|
|
|
}else{
|
|
|
|
pLevel->iIdxCur = -1;
|
2001-11-07 19:48:26 +03:00
|
|
|
}
|
2005-07-21 22:23:20 +04:00
|
|
|
notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor);
|
|
|
|
pLevel->iFrom = bestJ;
|
2002-06-19 18:27:05 +04:00
|
|
|
}
|
2007-03-27 02:05:01 +04:00
|
|
|
WHERETRACE(("*** Optimizer Finished ***\n"));
|
2002-06-19 18:27:05 +04:00
|
|
|
|
2005-07-29 23:43:58 +04:00
|
|
|
/* If the total query only selects a single row, then the ORDER BY
|
|
|
|
** clause is irrelevant.
|
|
|
|
*/
|
|
|
|
if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
|
|
|
|
*ppOrderBy = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-10 17:33:18 +04:00
|
|
|
/* If the caller is an UPDATE or DELETE statement that is requesting
|
|
|
|
** to use a one-pass algorithm, determine if this is appropriate.
|
|
|
|
** The one-pass algorithm only works if the WHERE clause constraints
|
|
|
|
** the statement to update a single row.
|
|
|
|
*/
|
|
|
|
assert( (wflags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
|
|
|
|
if( (wflags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
|
|
|
|
pWInfo->okOnePass = 1;
|
|
|
|
pWInfo->a[0].flags &= ~WHERE_IDX_ONLY;
|
|
|
|
}
|
|
|
|
|
2004-12-19 03:11:35 +03:00
|
|
|
/* Open all tables in the pTabList and any indices selected for
|
|
|
|
** searching those tables.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2004-07-19 21:25:24 +04:00
|
|
|
sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
|
2005-07-21 22:23:20 +04:00
|
|
|
for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
|
2006-01-05 14:34:32 +03:00
|
|
|
Table *pTab; /* Table to open */
|
|
|
|
Index *pIx; /* Index used to access pTab (if any) */
|
|
|
|
int iDb; /* Index of database containing table/index */
|
2004-12-19 03:11:35 +03:00
|
|
|
int iIdxCur = pLevel->iIdxCur;
|
2001-10-08 17:22:32 +04:00
|
|
|
|
2005-09-10 20:46:12 +04:00
|
|
|
#ifndef SQLITE_OMIT_EXPLAIN
|
|
|
|
if( pParse->explain==2 ){
|
|
|
|
char *zMsg;
|
|
|
|
struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
|
2005-09-10 20:46:12 +04:00
|
|
|
if( pItem->zAlias ){
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "%z AS %s", zMsg, pItem->zAlias);
|
2005-09-10 20:46:12 +04:00
|
|
|
}
|
|
|
|
if( (pIx = pLevel->pIdx)!=0 ){
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "%z WITH INDEX %s", zMsg, pIx->zName);
|
2005-11-21 15:46:27 +03:00
|
|
|
}else if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "%z USING PRIMARY KEY", zMsg);
|
2005-09-10 20:46:12 +04:00
|
|
|
}
|
2006-06-13 01:59:13 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-27 05:54:26 +04:00
|
|
|
else if( pLevel->pBestIdx ){
|
|
|
|
sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "%z VIRTUAL TABLE INDEX %d:%s", zMsg,
|
2006-06-27 05:54:26 +04:00
|
|
|
pBestIdx->idxNum, pBestIdx->idxStr);
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
|
|
|
#endif
|
2006-04-21 13:38:36 +04:00
|
|
|
if( pLevel->flags & WHERE_ORDERBY ){
|
2007-08-16 14:09:01 +04:00
|
|
|
zMsg = sqlite3MPrintf(db, "%z ORDER BY", zMsg);
|
2006-04-21 13:38:36 +04:00
|
|
|
}
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC);
|
2005-09-10 20:46:12 +04:00
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_EXPLAIN */
|
2005-07-21 22:23:20 +04:00
|
|
|
pTabItem = &pTabList->a[pLevel->iFrom];
|
2004-12-19 03:11:35 +03:00
|
|
|
pTab = pTabItem->pTab;
|
2006-01-05 14:34:32 +03:00
|
|
|
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
|
2006-06-12 03:41:55 +04:00
|
|
|
if( pTab->isEphem || pTab->pSelect ) continue;
|
2006-06-13 01:59:13 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-27 05:54:26 +04:00
|
|
|
if( pLevel->pBestIdx ){
|
2006-06-20 17:07:27 +04:00
|
|
|
int iCur = pTabItem->iCursor;
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0,
|
|
|
|
(const char*)pTab->pVtab, P4_VTAB);
|
2006-06-13 01:59:13 +04:00
|
|
|
}else
|
|
|
|
#endif
|
2005-07-21 07:14:59 +04:00
|
|
|
if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
|
2008-04-10 17:33:18 +04:00
|
|
|
int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
|
|
|
|
sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
|
|
|
|
if( !pWInfo->okOnePass && pTab->nCol<(sizeof(Bitmask)*8) ){
|
2006-01-13 18:58:43 +03:00
|
|
|
Bitmask b = pTabItem->colUsed;
|
|
|
|
int n = 0;
|
2006-02-24 05:53:49 +03:00
|
|
|
for(; b; b=b>>1, n++){}
|
2008-03-25 12:47:35 +03:00
|
|
|
sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n);
|
2006-01-13 18:58:43 +03:00
|
|
|
assert( n<=pTab->nCol );
|
|
|
|
}
|
2006-01-07 16:21:04 +03:00
|
|
|
}else{
|
|
|
|
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
|
2004-12-19 03:11:35 +03:00
|
|
|
}
|
|
|
|
pLevel->iTabCur = pTabItem->iCursor;
|
|
|
|
if( (pIx = pLevel->pIdx)!=0 ){
|
2006-01-10 20:58:23 +03:00
|
|
|
KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
|
2006-01-05 14:34:32 +03:00
|
|
|
assert( pIx->pSchema==pTab->pSchema );
|
2008-03-25 12:47:35 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIx->nColumn+1);
|
2008-01-03 10:54:23 +03:00
|
|
|
sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
|
2008-01-03 03:01:23 +03:00
|
|
|
(char*)pKey, P4_KEYINFO_HANDOFF);
|
2008-01-03 10:54:23 +03:00
|
|
|
VdbeComment((v, "%s", pIx->zName));
|
2004-12-19 03:11:35 +03:00
|
|
|
}
|
2006-01-05 14:34:32 +03:00
|
|
|
sqlite3CodeVerifySchema(pParse, iDb);
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2004-12-19 03:11:35 +03:00
|
|
|
pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Generate the code to do the search. Each iteration of the for
|
|
|
|
** loop below generates code for a single nested loop of the VM
|
|
|
|
** program.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2005-07-21 07:14:59 +04:00
|
|
|
notReady = ~(Bitmask)0;
|
2005-07-21 22:23:20 +04:00
|
|
|
for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
|
2005-07-21 07:14:59 +04:00
|
|
|
int j;
|
2004-12-19 03:11:35 +03:00
|
|
|
int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */
|
|
|
|
Index *pIdx; /* The index we will be using */
|
2007-03-28 18:30:06 +04:00
|
|
|
int nxt; /* Where to jump to continue with the next IN case */
|
2004-12-19 03:11:35 +03:00
|
|
|
int iIdxCur; /* The VDBE cursor for the index */
|
|
|
|
int omitTable; /* True if we use the index only */
|
2005-07-21 22:23:20 +04:00
|
|
|
int bRev; /* True if we need to scan in reverse order */
|
2004-12-19 03:11:35 +03:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
pTabItem = &pTabList->a[pLevel->iFrom];
|
|
|
|
iCur = pTabItem->iCursor;
|
2004-12-19 03:11:35 +03:00
|
|
|
pIdx = pLevel->pIdx;
|
|
|
|
iIdxCur = pLevel->iIdxCur;
|
2005-07-21 22:23:20 +04:00
|
|
|
bRev = (pLevel->flags & WHERE_REVERSE)!=0;
|
|
|
|
omitTable = (pLevel->flags & WHERE_IDX_ONLY)!=0;
|
2004-12-19 03:11:35 +03:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Create labels for the "break" and "continue" instructions
|
|
|
|
** for the current loop. Jump to brk to break out of a loop.
|
|
|
|
** Jump to cont to go immediately to the next iteration of the
|
|
|
|
** loop.
|
2007-03-28 18:30:06 +04:00
|
|
|
**
|
|
|
|
** When there is an IN operator, we also have a "nxt" label that
|
|
|
|
** means to continue with the next IN value combination. When
|
|
|
|
** there are no IN operators in the constraints, the "nxt" label
|
|
|
|
** is the same as "brk".
|
2004-12-19 03:11:35 +03:00
|
|
|
*/
|
2007-03-28 18:30:06 +04:00
|
|
|
brk = pLevel->brk = pLevel->nxt = sqlite3VdbeMakeLabel(v);
|
2005-07-21 22:23:20 +04:00
|
|
|
cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2002-05-25 00:31:36 +04:00
|
|
|
/* If this is the right table of a LEFT OUTER JOIN, allocate and
|
2002-12-03 05:22:52 +03:00
|
|
|
** initialize a memory cell that records if this table matches any
|
2002-06-15 00:58:45 +04:00
|
|
|
** row of the left table of the join.
|
2002-05-25 00:31:36 +04:00
|
|
|
*/
|
2006-12-16 19:25:15 +03:00
|
|
|
if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
|
2008-01-03 21:03:08 +03:00
|
|
|
pLevel->iLeftJoin = ++pParse->nMem;
|
2008-01-05 01:01:03 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
|
2008-01-02 03:34:36 +03:00
|
|
|
VdbeComment((v, "init LEFT JOIN no-match flag"));
|
2002-05-25 00:31:36 +04:00
|
|
|
}
|
|
|
|
|
2006-06-13 01:59:13 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-27 05:54:26 +04:00
|
|
|
if( pLevel->pBestIdx ){
|
2006-06-13 21:38:59 +04:00
|
|
|
/* Case 0: The table is a virtual-table. Use the VFilter and VNext
|
|
|
|
** to access the data.
|
|
|
|
*/
|
2006-06-27 06:33:40 +04:00
|
|
|
int j;
|
2008-01-03 21:39:41 +03:00
|
|
|
int iReg; /* P3 Value for OP_VFilter */
|
2006-06-27 05:54:26 +04:00
|
|
|
sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
|
|
|
|
int nConstraint = pBestIdx->nConstraint;
|
2006-06-14 03:51:34 +04:00
|
|
|
struct sqlite3_index_constraint_usage *aUsage =
|
2006-06-27 05:54:26 +04:00
|
|
|
pBestIdx->aConstraintUsage;
|
2006-06-14 03:51:34 +04:00
|
|
|
const struct sqlite3_index_constraint *aConstraint =
|
2006-06-27 05:54:26 +04:00
|
|
|
pBestIdx->aConstraint;
|
2006-06-14 03:51:34 +04:00
|
|
|
|
2008-01-17 05:36:28 +03:00
|
|
|
iReg = sqlite3GetTempRange(pParse, nConstraint+2);
|
2008-05-16 19:40:40 +04:00
|
|
|
pParse->disableColCache++;
|
2006-06-27 06:33:40 +04:00
|
|
|
for(j=1; j<=nConstraint; j++){
|
|
|
|
int k;
|
|
|
|
for(k=0; k<nConstraint; k++){
|
|
|
|
if( aUsage[k].argvIndex==j ){
|
2006-06-27 16:16:56 +04:00
|
|
|
int iTerm = aConstraint[k].iTermOffset;
|
2008-05-29 09:23:41 +04:00
|
|
|
assert( pParse->disableColCache );
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ExprCode(pParse, wc.a[iTerm].pExpr->pRight, iReg+j+1);
|
2006-06-13 01:59:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-27 06:33:40 +04:00
|
|
|
if( k==nConstraint ) break;
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
2008-05-29 09:23:41 +04:00
|
|
|
assert( pParse->disableColCache );
|
2008-05-16 19:40:40 +04:00
|
|
|
pParse->disableColCache--;
|
2008-01-05 01:01:03 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Integer, pBestIdx->idxNum, iReg);
|
|
|
|
sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
|
2008-01-03 21:39:41 +03:00
|
|
|
sqlite3VdbeAddOp4(v, OP_VFilter, iCur, brk, iReg, pBestIdx->idxStr,
|
2008-01-03 03:01:23 +03:00
|
|
|
pBestIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
|
2006-06-27 05:54:26 +04:00
|
|
|
pBestIdx->needToFreeIdxStr = 0;
|
2008-05-29 09:23:41 +04:00
|
|
|
for(j=0; j<nConstraint; j++){
|
2006-06-27 06:33:40 +04:00
|
|
|
if( aUsage[j].omit ){
|
|
|
|
int iTerm = aConstraint[j].iTermOffset;
|
|
|
|
disableTerm(pLevel, &wc.a[iTerm]);
|
2006-06-13 01:59:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pLevel->op = OP_VNext;
|
2006-06-13 05:04:52 +04:00
|
|
|
pLevel->p1 = iCur;
|
|
|
|
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
|
2006-06-13 01:59:13 +04:00
|
|
|
}else
|
|
|
|
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
|
|
|
|
2005-07-21 07:14:59 +04:00
|
|
|
if( pLevel->flags & WHERE_ROWID_EQ ){
|
2001-12-22 17:49:24 +03:00
|
|
|
/* Case 1: We can directly reference a single row using an
|
2002-06-15 00:58:45 +04:00
|
|
|
** equality comparison against the ROWID field. Or
|
|
|
|
** we reference multiple rows using a "rowid IN (...)"
|
|
|
|
** construct.
|
2001-04-04 15:48:57 +04:00
|
|
|
*/
|
2008-01-17 05:36:28 +03:00
|
|
|
int r1;
|
2005-07-21 07:14:59 +04:00
|
|
|
pTerm = findTerm(&wc, iCur, -1, notReady, WO_EQ|WO_IN, 0);
|
|
|
|
assert( pTerm!=0 );
|
2005-07-19 21:38:22 +04:00
|
|
|
assert( pTerm->pExpr!=0 );
|
|
|
|
assert( pTerm->leftCursor==iCur );
|
2004-12-19 03:11:35 +03:00
|
|
|
assert( omitTable==0 );
|
2008-03-31 22:19:54 +04:00
|
|
|
r1 = codeEqualityTerm(pParse, pTerm, pLevel, 0);
|
2007-03-28 18:30:06 +04:00
|
|
|
nxt = pLevel->nxt;
|
2008-03-31 22:19:54 +04:00
|
|
|
sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, nxt);
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp3(v, OP_NotExists, iCur, nxt, r1);
|
2005-01-03 21:13:18 +03:00
|
|
|
VdbeComment((v, "pk"));
|
2001-11-07 19:48:26 +03:00
|
|
|
pLevel->op = OP_Noop;
|
2005-07-21 07:14:59 +04:00
|
|
|
}else if( pLevel->flags & WHERE_ROWID_RANGE ){
|
2005-07-24 02:59:55 +04:00
|
|
|
/* Case 2: We have an inequality comparison against the ROWID field.
|
2001-12-22 17:49:24 +03:00
|
|
|
*/
|
|
|
|
int testOp = OP_Noop;
|
|
|
|
int start;
|
2005-07-21 07:14:59 +04:00
|
|
|
WhereTerm *pStart, *pEnd;
|
2001-12-22 17:49:24 +03:00
|
|
|
|
2004-12-19 03:11:35 +03:00
|
|
|
assert( omitTable==0 );
|
2005-07-29 00:51:19 +04:00
|
|
|
pStart = findTerm(&wc, iCur, -1, notReady, WO_GT|WO_GE, 0);
|
|
|
|
pEnd = findTerm(&wc, iCur, -1, notReady, WO_LT|WO_LE, 0);
|
2004-11-22 22:12:19 +03:00
|
|
|
if( bRev ){
|
2005-07-21 07:14:59 +04:00
|
|
|
pTerm = pStart;
|
|
|
|
pStart = pEnd;
|
|
|
|
pEnd = pTerm;
|
2004-11-22 22:12:19 +03:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
if( pStart ){
|
2004-09-25 17:12:14 +04:00
|
|
|
Expr *pX;
|
2008-01-17 05:36:28 +03:00
|
|
|
int r1, regFree1;
|
2005-07-21 07:14:59 +04:00
|
|
|
pX = pStart->pExpr;
|
2004-09-25 17:12:14 +04:00
|
|
|
assert( pX!=0 );
|
2005-07-21 07:14:59 +04:00
|
|
|
assert( pStart->leftCursor==iCur );
|
2008-01-17 05:36:28 +03:00
|
|
|
r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, ®Free1);
|
|
|
|
sqlite3VdbeAddOp3(v, OP_ForceInt, r1, brk,
|
2008-01-05 08:38:21 +03:00
|
|
|
pX->op==TK_LE || pX->op==TK_GT);
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp3(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk, r1);
|
2005-01-03 21:13:18 +03:00
|
|
|
VdbeComment((v, "pk"));
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ReleaseTempReg(pParse, regFree1);
|
2005-07-21 07:14:59 +04:00
|
|
|
disableTerm(pLevel, pStart);
|
2001-12-22 17:49:24 +03:00
|
|
|
}else{
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, brk);
|
2001-12-22 17:49:24 +03:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
if( pEnd ){
|
2004-09-25 17:12:14 +04:00
|
|
|
Expr *pX;
|
2005-07-21 07:14:59 +04:00
|
|
|
pX = pEnd->pExpr;
|
2004-09-25 17:12:14 +04:00
|
|
|
assert( pX!=0 );
|
2005-07-21 07:14:59 +04:00
|
|
|
assert( pEnd->leftCursor==iCur );
|
2008-01-03 21:03:08 +03:00
|
|
|
pLevel->iMem = ++pParse->nMem;
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ExprCode(pParse, pX->pRight, pLevel->iMem);
|
2004-09-25 17:12:14 +04:00
|
|
|
if( pX->op==TK_LT || pX->op==TK_GT ){
|
2004-11-22 22:12:19 +03:00
|
|
|
testOp = bRev ? OP_Le : OP_Ge;
|
2001-12-22 17:49:24 +03:00
|
|
|
}else{
|
2004-11-22 22:12:19 +03:00
|
|
|
testOp = bRev ? OP_Lt : OP_Gt;
|
2001-12-22 17:49:24 +03:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
disableTerm(pLevel, pEnd);
|
2001-12-22 17:49:24 +03:00
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
start = sqlite3VdbeCurrentAddr(v);
|
2004-11-22 22:12:19 +03:00
|
|
|
pLevel->op = bRev ? OP_Prev : OP_Next;
|
2003-05-02 18:32:12 +04:00
|
|
|
pLevel->p1 = iCur;
|
2001-12-22 17:49:24 +03:00
|
|
|
pLevel->p2 = start;
|
|
|
|
if( testOp!=OP_Noop ){
|
2008-01-17 05:36:28 +03:00
|
|
|
int r1 = sqlite3GetTempReg(pParse);
|
|
|
|
sqlite3VdbeAddOp2(v, OP_Rowid, iCur, r1);
|
|
|
|
/* sqlite3VdbeAddOp2(v, OP_SCopy, pLevel->iMem, 0); */
|
|
|
|
sqlite3VdbeAddOp3(v, testOp, pLevel->iMem, brk, r1);
|
2008-01-09 02:54:25 +03:00
|
|
|
sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ReleaseTempReg(pParse, r1);
|
2001-12-22 17:49:24 +03:00
|
|
|
}
|
2008-04-18 14:25:24 +04:00
|
|
|
}else if( pLevel->flags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
|
|
|
|
/* Case 3: A scan using an index.
|
|
|
|
**
|
2008-04-19 18:40:43 +04:00
|
|
|
** The WHERE clause may contain zero or more equality
|
2008-04-18 14:25:24 +04:00
|
|
|
** terms ("==" or "IN" operators) that refer to the N
|
|
|
|
** left-most columns of the index. It may also contain
|
|
|
|
** inequality constraints (>, <, >= or <=) on the indexed
|
|
|
|
** column that immediately follows the N equalities. Only
|
|
|
|
** the right-most column can be an inequality - the rest must
|
|
|
|
** use the "==" and "IN" operators. For example, if the
|
|
|
|
** index is on (x,y,z), then the following clauses are all
|
|
|
|
** optimized:
|
|
|
|
**
|
|
|
|
** x=5
|
|
|
|
** x=5 AND y=10
|
|
|
|
** x=5 AND y<10
|
|
|
|
** x=5 AND y>5 AND y<10
|
|
|
|
** x=5 AND y=5 AND z<=10
|
|
|
|
**
|
2008-04-19 18:40:43 +04:00
|
|
|
** The z<10 term of the following cannot be used, only
|
|
|
|
** the x=5 term:
|
2008-04-18 14:25:24 +04:00
|
|
|
**
|
|
|
|
** x=5 AND z<10
|
2002-06-19 18:27:05 +04:00
|
|
|
**
|
2008-04-19 18:40:43 +04:00
|
|
|
** N may be zero if there are inequality constraints.
|
|
|
|
** If there are no inequality constraints, then N is at
|
|
|
|
** least one.
|
|
|
|
**
|
2002-06-19 18:27:05 +04:00
|
|
|
** This case is also used when there are no WHERE clause
|
|
|
|
** constraints but an index is selected anyway, in order
|
|
|
|
** to force the output order to conform to an ORDER BY.
|
2008-04-18 14:25:24 +04:00
|
|
|
*/
|
2008-04-18 13:01:15 +04:00
|
|
|
int aStartOp[] = {
|
|
|
|
0,
|
|
|
|
0,
|
2008-04-18 14:25:24 +04:00
|
|
|
OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
|
|
|
|
OP_Last, /* 3: (!start_constraints && startEq && bRev) */
|
2008-04-18 13:01:15 +04:00
|
|
|
OP_MoveGt, /* 4: (start_constraints && !startEq && !bRev) */
|
2008-04-18 14:25:24 +04:00
|
|
|
OP_MoveLt, /* 5: (start_constraints && !startEq && bRev) */
|
2008-04-18 13:01:15 +04:00
|
|
|
OP_MoveGe, /* 6: (start_constraints && startEq && !bRev) */
|
2008-04-18 14:25:24 +04:00
|
|
|
OP_MoveLe /* 7: (start_constraints && startEq && bRev) */
|
2008-04-18 13:01:15 +04:00
|
|
|
};
|
|
|
|
int aEndOp[] = {
|
2008-04-18 14:25:24 +04:00
|
|
|
OP_Noop, /* 0: (!end_constraints) */
|
2008-04-18 13:01:15 +04:00
|
|
|
OP_IdxGE, /* 1: (end_constraints && !bRev) */
|
|
|
|
OP_IdxLT /* 2: (end_constraints && bRev) */
|
|
|
|
};
|
2005-07-24 02:59:55 +04:00
|
|
|
int nEq = pLevel->nEq;
|
2008-04-18 13:01:15 +04:00
|
|
|
int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
|
|
|
|
int regBase; /* Base register holding constraint values */
|
|
|
|
int r1; /* Temp register */
|
|
|
|
WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
|
|
|
|
WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
|
|
|
|
int startEq; /* True if range start uses ==, >= or <= */
|
|
|
|
int endEq; /* True if range end uses ==, >= or <= */
|
|
|
|
int start_constraints; /* Start of range is constrained */
|
|
|
|
int k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
|
2008-04-19 18:40:43 +04:00
|
|
|
int nConstraint; /* Number of constraint terms */
|
2008-04-18 13:01:15 +04:00
|
|
|
int op;
|
2001-11-08 03:45:21 +03:00
|
|
|
|
2005-07-24 02:59:55 +04:00
|
|
|
/* Generate code to evaluate all constraint terms using == or IN
|
2008-04-18 13:01:15 +04:00
|
|
|
** and store the values of those terms in an array of registers
|
|
|
|
** starting at regBase.
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
2008-01-17 05:36:28 +03:00
|
|
|
regBase = codeAllEqualityTerms(pParse, pLevel, &wc, notReady, 2);
|
2008-04-18 13:01:15 +04:00
|
|
|
nxt = pLevel->nxt;
|
2005-12-21 06:16:42 +03:00
|
|
|
|
2008-01-05 20:39:29 +03:00
|
|
|
/* If this loop satisfies a sort order (pOrderBy) request that
|
|
|
|
** was passed to this function to implement a "SELECT min(x) ..."
|
|
|
|
** query, then the caller will only allow the loop to run for
|
|
|
|
** a single iteration. This means that the first row returned
|
|
|
|
** should not have a NULL value stored in 'x'. If column 'x' is
|
|
|
|
** the first one after the nEq equality constraints in the index,
|
|
|
|
** this requires some special handling.
|
|
|
|
*/
|
2008-04-10 17:33:18 +04:00
|
|
|
if( (wflags&WHERE_ORDERBY_MIN)!=0
|
2008-01-05 20:39:29 +03:00
|
|
|
&& (pLevel->flags&WHERE_ORDERBY)
|
|
|
|
&& (pIdx->nColumn>nEq)
|
|
|
|
&& (pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq])
|
|
|
|
){
|
|
|
|
isMinQuery = 1;
|
|
|
|
}
|
|
|
|
|
2008-04-18 14:25:24 +04:00
|
|
|
/* Find any inequality constraint terms for the start and end
|
2008-04-18 13:01:15 +04:00
|
|
|
** of the range.
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
2008-04-18 13:01:15 +04:00
|
|
|
if( pLevel->flags & WHERE_TOP_LIMIT ){
|
|
|
|
pRangeEnd = findTerm(&wc, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
if( pLevel->flags & WHERE_BTM_LIMIT ){
|
|
|
|
pRangeStart = findTerm(&wc, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
|
|
|
|
/* If we are doing a reverse order scan on an ascending index, or
|
|
|
|
** a forward order scan on a descending index, interchange the
|
|
|
|
** start and end terms (pRangeStart and pRangeEnd).
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
2008-04-19 18:40:43 +04:00
|
|
|
if( bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
|
2008-04-18 13:01:15 +04:00
|
|
|
SWAP(WhereTerm *, pRangeEnd, pRangeStart);
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
|
|
|
|
testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
|
|
|
|
testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
|
|
|
|
testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
|
|
|
|
startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
|
|
|
|
endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
|
|
|
|
start_constraints = pRangeStart || nEq>0;
|
2008-04-18 13:01:15 +04:00
|
|
|
|
|
|
|
/* Seek the index cursor to the start of the range. */
|
2008-04-19 18:40:43 +04:00
|
|
|
nConstraint = nEq;
|
2008-04-18 13:01:15 +04:00
|
|
|
if( pRangeStart ){
|
|
|
|
int dcc = pParse->disableColCache;
|
|
|
|
if( pRangeEnd ){
|
2008-05-29 09:23:41 +04:00
|
|
|
pParse->disableColCache++;
|
2002-12-04 23:01:06 +03:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
sqlite3ExprCode(pParse, pRangeStart->pExpr->pRight, regBase+nEq);
|
|
|
|
pParse->disableColCache = dcc;
|
|
|
|
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt);
|
2008-04-19 18:40:43 +04:00
|
|
|
nConstraint++;
|
2008-04-18 13:01:15 +04:00
|
|
|
}else if( isMinQuery ){
|
|
|
|
sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
|
2008-04-19 18:40:43 +04:00
|
|
|
nConstraint++;
|
2008-04-18 13:01:15 +04:00
|
|
|
startEq = 0;
|
|
|
|
start_constraints = 1;
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-04-19 18:40:43 +04:00
|
|
|
codeApplyAffinity(pParse, regBase, nConstraint, pIdx);
|
2008-04-18 13:01:15 +04:00
|
|
|
op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
|
2008-04-19 18:40:43 +04:00
|
|
|
assert( op!=0 );
|
|
|
|
testcase( op==OP_Rewind );
|
|
|
|
testcase( op==OP_Last );
|
|
|
|
testcase( op==OP_MoveGt );
|
|
|
|
testcase( op==OP_MoveGe );
|
|
|
|
testcase( op==OP_MoveLe );
|
|
|
|
testcase( op==OP_MoveLt );
|
|
|
|
sqlite3VdbeAddOp4(v, op, iIdxCur, nxt, regBase,
|
|
|
|
(char*)nConstraint, P4_INT32);
|
2001-11-08 03:45:21 +03:00
|
|
|
|
2008-04-18 13:01:15 +04:00
|
|
|
/* Load the value for the inequality constraint at the end of the
|
|
|
|
** range (if any).
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
2008-04-19 18:40:43 +04:00
|
|
|
nConstraint = nEq;
|
2008-04-18 13:01:15 +04:00
|
|
|
if( pRangeEnd ){
|
|
|
|
sqlite3ExprCode(pParse, pRangeEnd->pExpr->pRight, regBase+nEq);
|
|
|
|
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt);
|
2008-04-18 14:25:24 +04:00
|
|
|
codeApplyAffinity(pParse, regBase, nEq+1, pIdx);
|
2008-04-19 18:40:43 +04:00
|
|
|
nConstraint++;
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
|
|
|
|
/* Top of the loop body */
|
|
|
|
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
|
|
|
|
|
|
|
|
/* Check if the index cursor is past the end of the range. */
|
2008-04-19 18:40:43 +04:00
|
|
|
op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
|
|
|
|
testcase( op==OP_Noop );
|
|
|
|
testcase( op==OP_IdxGE );
|
|
|
|
testcase( op==OP_IdxLT );
|
|
|
|
sqlite3VdbeAddOp4(v, op, iIdxCur, nxt, regBase,
|
|
|
|
(char*)nConstraint, P4_INT32);
|
2008-04-18 13:01:15 +04:00
|
|
|
sqlite3VdbeChangeP5(v, endEq!=bRev);
|
|
|
|
|
2008-04-19 18:40:43 +04:00
|
|
|
/* If there are inequality constraints, check that the value
|
|
|
|
** of the table column that the inequality contrains is not NULL.
|
2008-04-18 13:01:15 +04:00
|
|
|
** If it is, jump to the next iteration of the loop.
|
|
|
|
*/
|
2008-01-17 05:36:28 +03:00
|
|
|
r1 = sqlite3GetTempReg(pParse);
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pLevel->flags & WHERE_BTM_LIMIT );
|
|
|
|
testcase( pLevel->flags & WHERE_TOP_LIMIT );
|
2008-04-18 13:01:15 +04:00
|
|
|
if( pLevel->flags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT) ){
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
|
|
|
|
sqlite3VdbeAddOp2(v, OP_IsNull, r1, cont);
|
2006-10-28 04:28:09 +04:00
|
|
|
}
|
2008-04-18 13:01:15 +04:00
|
|
|
|
|
|
|
/* Seek the table cursor, if required */
|
2004-12-25 04:03:13 +03:00
|
|
|
if( !omitTable ){
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, r1);
|
2008-01-19 23:11:25 +03:00
|
|
|
sqlite3VdbeAddOp3(v, OP_MoveGe, iCur, 0, r1); /* Deferred seek */
|
2001-11-08 03:45:21 +03:00
|
|
|
}
|
2008-01-17 05:36:28 +03:00
|
|
|
sqlite3ReleaseTempReg(pParse, r1);
|
2001-11-08 03:45:21 +03:00
|
|
|
|
2008-04-18 13:01:15 +04:00
|
|
|
/* Record the instruction used to terminate the loop. Disable
|
|
|
|
** WHERE clause terms made redundant by the index range scan.
|
2001-11-08 03:45:21 +03:00
|
|
|
*/
|
2005-07-21 07:14:59 +04:00
|
|
|
pLevel->op = bRev ? OP_Prev : OP_Next;
|
2004-12-19 03:11:35 +03:00
|
|
|
pLevel->p1 = iIdxCur;
|
2008-04-18 13:01:15 +04:00
|
|
|
disableTerm(pLevel, pRangeStart);
|
|
|
|
disableTerm(pLevel, pRangeEnd);
|
2005-07-21 07:14:59 +04:00
|
|
|
}else{
|
2008-04-19 18:40:43 +04:00
|
|
|
/* Case 4: There is no usable index. We must do a complete
|
2005-07-21 07:14:59 +04:00
|
|
|
** scan of the entire table.
|
|
|
|
*/
|
|
|
|
assert( omitTable==0 );
|
2005-07-29 00:51:19 +04:00
|
|
|
assert( bRev==0 );
|
|
|
|
pLevel->op = OP_Next;
|
2005-07-21 07:14:59 +04:00
|
|
|
pLevel->p1 = iCur;
|
2008-01-03 03:01:23 +03:00
|
|
|
pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, OP_Rewind, iCur, brk);
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2005-07-21 07:14:59 +04:00
|
|
|
notReady &= ~getMask(&maskSet, iCur);
|
2000-05-29 18:26:00 +04:00
|
|
|
|
|
|
|
/* Insert code to test every subexpression that can be completely
|
|
|
|
** computed using the current set of tables.
|
|
|
|
*/
|
2005-07-19 21:38:22 +04:00
|
|
|
for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){
|
|
|
|
Expr *pE;
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pTerm->flags & TERM_VIRTUAL );
|
|
|
|
testcase( pTerm->flags & TERM_CODED );
|
2005-07-19 21:38:22 +04:00
|
|
|
if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
|
2005-07-21 07:14:59 +04:00
|
|
|
if( (pTerm->prereqAll & notReady)!=0 ) continue;
|
2005-07-19 21:38:22 +04:00
|
|
|
pE = pTerm->pExpr;
|
|
|
|
assert( pE!=0 );
|
2005-07-08 18:14:22 +04:00
|
|
|
if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
|
2002-10-27 22:35:33 +03:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-09 02:54:25 +03:00
|
|
|
sqlite3ExprIfFalse(pParse, pE, cont, SQLITE_JUMPIFNULL);
|
2005-07-19 21:38:22 +04:00
|
|
|
pTerm->flags |= TERM_CODED;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2002-05-25 00:31:36 +04:00
|
|
|
|
|
|
|
/* For a LEFT OUTER JOIN, generate code that will record the fact that
|
|
|
|
** at least one row of the right table has matched the left table.
|
|
|
|
*/
|
|
|
|
if( pLevel->iLeftJoin ){
|
2004-05-08 12:23:19 +04:00
|
|
|
pLevel->top = sqlite3VdbeCurrentAddr(v);
|
2008-01-05 01:01:03 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
|
2008-01-02 03:34:36 +03:00
|
|
|
VdbeComment((v, "record LEFT JOIN hit"));
|
2008-04-01 03:48:03 +04:00
|
|
|
sqlite3ExprClearColumnCache(pParse, pLevel->iTabCur);
|
|
|
|
sqlite3ExprClearColumnCache(pParse, pLevel->iIdxCur);
|
2005-07-16 17:33:20 +04:00
|
|
|
for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pTerm->flags & TERM_VIRTUAL );
|
|
|
|
testcase( pTerm->flags & TERM_CODED );
|
2005-07-19 21:38:22 +04:00
|
|
|
if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
|
2005-07-21 07:14:59 +04:00
|
|
|
if( (pTerm->prereqAll & notReady)!=0 ) continue;
|
2005-07-19 21:38:22 +04:00
|
|
|
assert( pTerm->pExpr );
|
2008-01-09 02:54:25 +03:00
|
|
|
sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, SQLITE_JUMPIFNULL);
|
2005-07-19 21:38:22 +04:00
|
|
|
pTerm->flags |= TERM_CODED;
|
2002-06-25 02:01:57 +04:00
|
|
|
}
|
2002-05-25 00:31:36 +04:00
|
|
|
}
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
2005-07-21 07:48:20 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_TEST /* For testing and debugging use only */
|
|
|
|
/* Record in the query plan information about the current table
|
|
|
|
** and the index used to access it (if any). If the table itself
|
|
|
|
** is not used, its name is just '{}'. If no index is used
|
|
|
|
** the index is listed as "{}". If the primary key is used the
|
|
|
|
** index name is '*'.
|
|
|
|
*/
|
|
|
|
for(i=0; i<pTabList->nSrc; i++){
|
|
|
|
char *z;
|
|
|
|
int n;
|
|
|
|
pLevel = &pWInfo->a[i];
|
2005-07-21 22:23:20 +04:00
|
|
|
pTabItem = &pTabList->a[pLevel->iFrom];
|
2005-07-21 07:48:20 +04:00
|
|
|
z = pTabItem->zAlias;
|
|
|
|
if( z==0 ) z = pTabItem->pTab->zName;
|
|
|
|
n = strlen(z);
|
|
|
|
if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
|
|
|
|
if( pLevel->flags & WHERE_IDX_ONLY ){
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
|
2005-07-21 07:48:20 +04:00
|
|
|
nQPlan += 2;
|
|
|
|
}else{
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(&sqlite3_query_plan[nQPlan], z, n);
|
2005-07-21 07:48:20 +04:00
|
|
|
nQPlan += n;
|
|
|
|
}
|
|
|
|
sqlite3_query_plan[nQPlan++] = ' ';
|
|
|
|
}
|
2008-04-19 18:40:43 +04:00
|
|
|
testcase( pLevel->flags & WHERE_ROWID_EQ );
|
|
|
|
testcase( pLevel->flags & WHERE_ROWID_RANGE );
|
2005-07-21 07:48:20 +04:00
|
|
|
if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
|
2005-07-21 07:48:20 +04:00
|
|
|
nQPlan += 2;
|
|
|
|
}else if( pLevel->pIdx==0 ){
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
|
2005-07-21 07:48:20 +04:00
|
|
|
nQPlan += 3;
|
|
|
|
}else{
|
|
|
|
n = strlen(pLevel->pIdx->zName);
|
|
|
|
if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
|
2007-05-04 17:15:55 +04:00
|
|
|
memcpy(&sqlite3_query_plan[nQPlan], pLevel->pIdx->zName, n);
|
2005-07-21 07:48:20 +04:00
|
|
|
nQPlan += n;
|
|
|
|
sqlite3_query_plan[nQPlan++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
|
|
|
|
sqlite3_query_plan[--nQPlan] = 0;
|
|
|
|
}
|
|
|
|
sqlite3_query_plan[nQPlan] = 0;
|
|
|
|
nQPlan = 0;
|
|
|
|
#endif /* SQLITE_TEST // Testing and debugging use only */
|
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Record the continuation address in the WhereInfo structure. Then
|
|
|
|
** clean up and return.
|
|
|
|
*/
|
2000-05-29 18:26:00 +04:00
|
|
|
pWInfo->iContinue = cont;
|
2005-07-16 17:33:20 +04:00
|
|
|
whereClauseClear(&wc);
|
2000-05-29 18:26:00 +04:00
|
|
|
return pWInfo;
|
2005-07-22 04:31:39 +04:00
|
|
|
|
|
|
|
/* Jump here if malloc fails */
|
|
|
|
whereBeginNoMem:
|
|
|
|
whereClauseClear(&wc);
|
2006-06-13 01:59:13 +04:00
|
|
|
whereInfoFree(pWInfo);
|
2005-07-22 04:31:39 +04:00
|
|
|
return 0;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-15 00:58:45 +04:00
|
|
|
** Generate the end of the WHERE loop. See comments on
|
2004-05-08 12:23:19 +04:00
|
|
|
** sqlite3WhereBegin() for additional information.
|
2000-05-29 18:26:00 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
void sqlite3WhereEnd(WhereInfo *pWInfo){
|
2000-05-29 18:26:00 +04:00
|
|
|
Vdbe *v = pWInfo->pParse->pVdbe;
|
2000-06-05 22:54:46 +04:00
|
|
|
int i;
|
2001-11-07 19:48:26 +03:00
|
|
|
WhereLevel *pLevel;
|
2002-05-24 06:04:32 +04:00
|
|
|
SrcList *pTabList = pWInfo->pTabList;
|
2000-06-05 22:54:46 +04:00
|
|
|
|
2004-12-19 03:11:35 +03:00
|
|
|
/* Generate loop termination code.
|
|
|
|
*/
|
2008-04-01 03:48:03 +04:00
|
|
|
sqlite3ExprClearColumnCache(pWInfo->pParse, -1);
|
2002-05-24 06:04:32 +04:00
|
|
|
for(i=pTabList->nSrc-1; i>=0; i--){
|
2001-11-07 19:48:26 +03:00
|
|
|
pLevel = &pWInfo->a[i];
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3VdbeResolveLabel(v, pLevel->cont);
|
2001-11-07 19:48:26 +03:00
|
|
|
if( pLevel->op!=OP_Noop ){
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
|
2001-11-07 19:48:26 +03:00
|
|
|
}
|
2005-07-22 04:31:39 +04:00
|
|
|
if( pLevel->nIn ){
|
2007-03-28 18:30:06 +04:00
|
|
|
struct InLoop *pIn;
|
2005-07-22 04:31:39 +04:00
|
|
|
int j;
|
2007-03-28 18:30:06 +04:00
|
|
|
sqlite3VdbeResolveLabel(v, pLevel->nxt);
|
|
|
|
for(j=pLevel->nIn, pIn=&pLevel->aInLoop[j-1]; j>0; j--, pIn--){
|
|
|
|
sqlite3VdbeJumpHere(v, pIn->topAddr+1);
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->topAddr);
|
2007-03-28 18:30:06 +04:00
|
|
|
sqlite3VdbeJumpHere(v, pIn->topAddr-1);
|
2005-07-22 04:31:39 +04:00
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(pLevel->aInLoop);
|
2002-06-09 03:25:08 +04:00
|
|
|
}
|
2007-03-28 18:30:06 +04:00
|
|
|
sqlite3VdbeResolveLabel(v, pLevel->brk);
|
2002-05-25 00:31:36 +04:00
|
|
|
if( pLevel->iLeftJoin ){
|
|
|
|
int addr;
|
2008-01-09 05:15:38 +03:00
|
|
|
addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
|
|
|
|
sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
|
2004-12-19 03:11:35 +03:00
|
|
|
if( pLevel->iIdxCur>=0 ){
|
2008-01-09 05:15:38 +03:00
|
|
|
sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
|
2002-08-13 17:15:49 +04:00
|
|
|
}
|
2008-01-03 03:01:23 +03:00
|
|
|
sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->top);
|
2005-09-20 21:42:23 +04:00
|
|
|
sqlite3VdbeJumpHere(v, addr);
|
2002-05-25 00:31:36 +04:00
|
|
|
}
|
2001-11-07 19:48:26 +03:00
|
|
|
}
|
2004-12-19 03:11:35 +03:00
|
|
|
|
|
|
|
/* The "break" point is here, just past the end of the outer loop.
|
|
|
|
** Set it.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
|
2004-12-19 03:11:35 +03:00
|
|
|
|
2005-07-21 22:23:20 +04:00
|
|
|
/* Close all of the cursors that were opened by sqlite3WhereBegin.
|
2004-12-19 03:11:35 +03:00
|
|
|
*/
|
2005-07-21 22:23:20 +04:00
|
|
|
for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
|
|
|
|
struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
|
2004-12-19 03:11:35 +03:00
|
|
|
Table *pTab = pTabItem->pTab;
|
2003-04-24 05:45:04 +04:00
|
|
|
assert( pTab!=0 );
|
2006-06-12 03:41:55 +04:00
|
|
|
if( pTab->isEphem || pTab->pSelect ) continue;
|
2008-04-10 17:33:18 +04:00
|
|
|
if( !pWInfo->okOnePass && (pLevel->flags & WHERE_IDX_ONLY)==0 ){
|
2008-01-09 05:15:38 +03:00
|
|
|
sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
|
2004-12-19 03:11:35 +03:00
|
|
|
}
|
2001-11-07 19:48:26 +03:00
|
|
|
if( pLevel->pIdx!=0 ){
|
2008-01-09 05:15:38 +03:00
|
|
|
sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
|
2004-12-19 03:11:35 +03:00
|
|
|
}
|
|
|
|
|
2007-11-29 20:43:27 +03:00
|
|
|
/* If this scan uses an index, make code substitutions to read data
|
|
|
|
** from the index in preference to the table. Sometimes, this means
|
|
|
|
** the table need never be read from. This is a performance boost,
|
|
|
|
** as the vdbe level waits until the table is read before actually
|
|
|
|
** seeking the table cursor to the record corresponding to the current
|
|
|
|
** position in the index.
|
2004-12-19 03:11:35 +03:00
|
|
|
**
|
|
|
|
** Calls to the code generator in between sqlite3WhereBegin and
|
|
|
|
** sqlite3WhereEnd will have created code that references the table
|
|
|
|
** directly. This loop scans all that code looking for opcodes
|
|
|
|
** that reference the table and converts them into opcodes that
|
|
|
|
** reference the index.
|
|
|
|
*/
|
2007-11-29 20:43:27 +03:00
|
|
|
if( pLevel->pIdx ){
|
2006-01-24 15:09:17 +03:00
|
|
|
int k, j, last;
|
2004-12-19 03:11:35 +03:00
|
|
|
VdbeOp *pOp;
|
|
|
|
Index *pIdx = pLevel->pIdx;
|
2007-11-29 20:43:27 +03:00
|
|
|
int useIndexOnly = pLevel->flags & WHERE_IDX_ONLY;
|
2004-12-19 03:11:35 +03:00
|
|
|
|
|
|
|
assert( pIdx!=0 );
|
|
|
|
pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
|
|
|
|
last = sqlite3VdbeCurrentAddr(v);
|
2006-01-24 15:09:17 +03:00
|
|
|
for(k=pWInfo->iTop; k<last; k++, pOp++){
|
2004-12-19 03:11:35 +03:00
|
|
|
if( pOp->p1!=pLevel->iTabCur ) continue;
|
|
|
|
if( pOp->opcode==OP_Column ){
|
|
|
|
for(j=0; j<pIdx->nColumn; j++){
|
|
|
|
if( pOp->p2==pIdx->aiColumn[j] ){
|
|
|
|
pOp->p2 = j;
|
2007-11-29 20:43:27 +03:00
|
|
|
pOp->p1 = pLevel->iIdxCur;
|
2004-12-19 03:11:35 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-29 20:43:27 +03:00
|
|
|
assert(!useIndexOnly || j<pIdx->nColumn);
|
2005-06-13 01:35:51 +04:00
|
|
|
}else if( pOp->opcode==OP_Rowid ){
|
2004-12-19 03:11:35 +03:00
|
|
|
pOp->p1 = pLevel->iIdxCur;
|
2005-06-13 01:35:51 +04:00
|
|
|
pOp->opcode = OP_IdxRowid;
|
2007-11-29 20:43:27 +03:00
|
|
|
}else if( pOp->opcode==OP_NullRow && useIndexOnly ){
|
2005-01-30 12:17:58 +03:00
|
|
|
pOp->opcode = OP_Noop;
|
2004-12-19 03:11:35 +03:00
|
|
|
}
|
|
|
|
}
|
2000-06-05 22:54:46 +04:00
|
|
|
}
|
|
|
|
}
|
2004-12-19 03:11:35 +03:00
|
|
|
|
|
|
|
/* Final cleanup
|
|
|
|
*/
|
2006-06-13 01:59:13 +04:00
|
|
|
whereInfoFree(pWInfo);
|
2000-05-29 18:26:00 +04:00
|
|
|
return;
|
|
|
|
}
|