#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** 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.
**
*************************************************************************
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.114.2.3 2004/07/22 17:10:10 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
/*
** Construct a new expression node and return a pointer to it. Memory
** for this node is obtained from sqliteMalloc(). The calling function
** is responsible for making sure the node eventually gets freed.
*/
if( pNew==0 ){
/* When malloc fails, we leak memory from pLeft and pRight */
return 0;
}
if( pToken ){
}else{
}else{
}
}
return pNew;
}
/*
** Set the Expr.span field of the given expression to span all
** text between the two given tokens.
*/
/* Note: pExpr might be NULL due to a prior malloc failure */
}else{
}
}
}
/*
** Construct a new expression node for a function with multiple
** arguments.
*/
if( pNew==0 ){
/* sqliteExprListDelete(pList); // Leak pList when malloc fails */
return 0;
}
if( pToken ){
}else{
}
return pNew;
}
/*
** Recursively delete an expression tree.
*/
if( p==0 ) return;
sqliteExprDelete(p->pLeft);
sqliteExprDelete(p->pRight);
sqliteFree(p);
}
/*
** The following group of routines make deep copies of expressions,
** expression lists, ID lists, and select statements. The copies can
** be deleted (by being passed to their respective ...Delete() routines)
** without effecting the originals.
**
** The expression list, ID, and source lists return by sqliteExprListDup(),
** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
** by subsequent calls to sqlite*ListAppend() routines.
**
** Any tables that the SrcList might point to are not duplicated.
*/
if( p==0 ) return 0;
pNew = sqliteMallocRaw( sizeof(*p) );
if( pNew==0 ) return 0;
if( p->token.z!=0 ){
}else{
}
return pNew;
}
if( pFrom->z ){
}else{
pTo->z = 0;
}
}
int i;
if( p==0 ) return 0;
if( pNew==0 ) return 0;
if( pItem==0 ){
return 0;
}
/* Always make a copy of the span for top-level expressions in the
** expression list. The logic in SELECT processing that determines
** the names of columns in the result set needs this information */
}
}
return pNew;
}
int i;
int nByte;
if( p==0 ) return 0;
if( pNew==0 ) return 0;
for(i=0; i<p->nSrc; i++){
}
return pNew;
}
int i;
if( p==0 ) return 0;
if( pNew==0 ) return 0;
if( pNew->a==0 ) return 0;
for(i=0; i<p->nId; i++){
}
return pNew;
}
if( p==0 ) return 0;
pNew = sqliteMallocRaw( sizeof(*p) );
if( pNew==0 ) return 0;
return pNew;
}
/*
** Add a new element to the end of an expression list. If pList is
** initially NULL, then create a new expression list.
*/
if( pList==0 ){
if( pList==0 ){
/* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
return 0;
}
}
if( pList->a==0 ){
/* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
return pList;
}
}
if( pName ){
}
}
return pList;
}
/*
** Delete an entire expression list.
*/
int i;
if( pList==0 ) return;
}
sqliteFree(pList->a);
}
/*
** Walk an expression tree. Return 1 if the expression is constant
** and 0 if it involves variables.
**
** For the purposes of this function, a double-quoted string (ex: "abc")
** is considered a variable but a single-quoted string (ex: 'abc') is
** a constant.
*/
switch( p->op ){
case TK_ID:
case TK_COLUMN:
case TK_DOT:
case TK_FUNCTION:
return 0;
case TK_NULL:
case TK_STRING:
case TK_INTEGER:
case TK_FLOAT:
case TK_VARIABLE:
return 1;
default: {
if( p->pList ){
int i;
}
}
}
}
return 0;
}
/*
** If the given expression codes a constant integer that is small enough
** to fit in a 32-bit integer, return 1 and put the value of the integer
** in *pValue. If the expression is not an integer or if it is too big
** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
*/
switch( p->op ){
case TK_INTEGER: {
if( sqliteFitsIn32Bits(p->token.z) ){
return 1;
}
break;
}
case TK_STRING: {
const char *z = p->token.z;
int n = p->token.n;
if( n>0 && z[0]=='-' ){ z++; n--; }
while( n>0 && *z && isdigit(*z) ){ z++; n--; }
if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
return 1;
}
break;
}
case TK_UPLUS: {
}
case TK_UMINUS: {
int v;
if( sqliteExprIsInteger(p->pLeft, &v) ){
*pValue = -v;
return 1;
}
break;
}
default: break;
}
return 0;
}
/*
** Return TRUE if the given string is a row-id column name.
*/
int sqliteIsRowid(const char *z){
return 0;
}
/*
** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
** that name in the set of source tables in pSrcList and make the pExpr
** expression node refer back to that source column. The following changes
** are made to pExpr:
**
** pExpr->iDb Set the index in db->aDb[] of the database holding
** the table.
** pExpr->iTable Set to the cursor number for the table obtained
** from pSrcList.
** pExpr->iColumn Set to the column number within the table.
** pExpr->dataType Set to the appropriate data type for the column.
** pExpr->op Set to TK_COLUMN.
** pExpr->pLeft Any expression this points to is deleted
** pExpr->pRight Any expression this points to is deleted.
**
** The pDbToken is the name of the database (the "X"). This value may be
** NULL meaning that name is of the form Y.Z or Z. Any available database
** can be used. The pTableToken is the name of the table (the "Y"). This
** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
** means that the form of the name is Z and that columns from any table
** can be used.
**
** If the name cannot be resolved unambiguously, leave an error message
** in pParse and return non-zero. Return zero on success.
*/
static int lookupName(
){
int i, j; /* Loop counters */
}else{
zDb = 0;
}
if( pTableToken && pTableToken->z ){
}else{
zTab = 0;
}
if( sqlite_malloc_failed ){
return 1; /* Leak memory (zDb and zTab) if malloc fails */
}
if( pTab==0 ) continue;
if( zTab ){
}else{
continue;
}
}
}
if( 0==(cntTab++) ){
}
cnt++;
/* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
break;
}
}
}
/* If we have not already resolved the name, then maybe
** it is a new.* or old.* trigger argument reference
*/
}
if( pTab ){
int j;
cntTab++;
cnt++;
break;
}
}
}
}
/*
** Perhaps the name is a reference to the ROWID
*/
cnt = 1;
}
/*
** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
** might refer to an result-set alias. This happens, for example, when
** we are resolving names in the WHERE clause of the following command:
**
** SELECT a+b AS x FROM table WHERE x<10;
**
** In cases like this, replace pExpr with a copy of the expression that
** forms the result set entry ("a+b" in the example) and return immediately.
** Note that the expression in the result set should have already been
** resolved by the time the WHERE clause is resolved.
*/
return 0;
}
}
}
/*
** If X and Y are NULL (in other words if only the column name Z is
** supplied) and the value of Z is enclosed in double-quotes, then
** Z is a string literal if it doesn't match any column names. In that
** case, we need to return right away and not make any changes to
** pExpr.
*/
return 0;
}
/*
** cnt==0 means there was not match. cnt>1 means there were two or
** more matches. Either way, we have an error.
*/
if( cnt!=1 ){
char *z = 0;
char *zErr;
if( zDb ){
}else if( zTab ){
}else{
z = sqliteStrDup(zCol);
}
sqliteFree(z);
}
/* Clean up and return
*/
return cnt!=1;
}
/*
** This routine walks an expression tree and resolves references to
** table columns. Nodes of the form ID.ID or ID resolve into an
** index to the table in the table list and a column offset. The
** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
** value is changed to the index of the referenced table in pTabList
** plus the "base" value. The base value will ultimately become the
** VDBE cursor number for a cursor that is pointing into the referenced
** table. The Expr.iColumn value is changed to the index of the column
** of the referenced table. The Expr.iColumn value for the special
** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
** alias for ROWID.
**
** We also check for instances of the IN operator. IN comes in two
** forms:
**
** expr IN (exprlist)
** and
** expr IN (SELECT ...)
**
** The first form is handled by creating a set holding the list
** of allowed values. The second form causes the SELECT to generate
** a temporary table.
**
** This routine also looks for scalar SELECTs that are part of an expression.
** If it finds any, it generates code to write the value of that select
** into a memory cell.
**
** Unknown columns or tables provoke an error. The function returns
** the number of errors seen and leaves an error message on pParse->zErrMsg.
*/
int sqliteExprResolveIds(
){
int i;
}
/* Double-quoted strings (ex: "abc") are used as identifiers if
** possible. Otherwise they remain as strings. Single-quoted
** strings (ex: 'abc') are always string literals.
*/
case TK_STRING: {
/* Fall thru into the TK_ID case if this is a double-quoted string */
}
/* A lone identifier is the name of a columnd.
*/
case TK_ID: {
return 1;
}
break;
}
/* A table name and column name: ID.ID
** Or a database, table and column: ID.ID.ID
*/
case TK_DOT: {
pDb = 0;
}else{
}
return 1;
}
break;
}
case TK_IN: {
if( v==0 ) return 1;
return 1;
}
/* Case 1: expr IN (SELECT ...)
**
** Generate code to write the results of the select into a temporary
** table. The cursor number of the temporary table has already
** been put in iTable by sqliteExprResolveInSelect().
*/
/* Case 2: expr IN (exprlist)
**
** Create a set to put the exprlist values in. The Set id is stored
** in iTable.
*/
int i, iSet;
if( !sqliteExprIsConstant(pE2) ){
"right-hand side of IN operator must be constant");
return 1;
}
return 1;
}
}
case TK_FLOAT:
case TK_INTEGER:
case TK_STRING: {
int addr;
sqliteVdbeDequoteP3(v, addr);
break;
}
default: {
break;
}
}
}
}
break;
}
case TK_SELECT: {
/* This has to be a scalar SELECT. Generate code to put the
** value of this select in a memory cell and record the number
** of the memory cell in iColumn.
*/
return 1;
}
break;
}
/* For all else, just recursively walk the tree */
default: {
return 1;
}
return 1;
}
int i;
return 1;
}
}
}
}
}
return 0;
}
/*
** pExpr is a node that defines a function of some kind. It might
** be a syntactic function like "count(x)" or it might be a function
** that implements an operator, like "a LIKE b".
**
** This routine makes *pzName point to the name of the function and
** *pnName hold the number of characters in the function name.
*/
case TK_FUNCTION: {
break;
}
case TK_LIKE: {
*pzName = "like";
*pnName = 4;
break;
}
case TK_GLOB: {
*pzName = "glob";
*pnName = 4;
break;
}
default: {
*pzName = "can't happen";
*pnName = 12;
break;
}
}
}
/*
** Error check the functions in an expression. Make sure all
** function names are recognized and all functions have the correct
** number of arguments. Leave an error message in pParse->zErrMsg
** if anything is amiss. Return the number of errors.
**
** if pIsAgg is not null and this expression is an aggregate function
** (like count(*) or max(value)) then write a 1 into *pIsAgg.
*/
int nErr = 0;
if( pExpr==0 ) return 0;
case TK_GLOB:
case TK_LIKE:
case TK_FUNCTION: {
int i;
if( pDef==0 ){
if( pDef==0 ){
no_such_func = 1;
}else{
wrong_num_args = 1;
}
}else{
}
nErr++;
is_agg = 0;
}else if( no_such_func ){
nErr++;
}else if( wrong_num_args ){
nErr++;
}
if( is_agg ){
}
for(i=0; nErr==0 && i<n; i++){
}
if( pDef==0 ){
/* Already reported an error */
}else{
}
for(i=0; i<n; i++){
break;
}
}
}else{
}
}
default: {
}
}
int i;
for(i=0; nErr==0 && i<n; i++){
}
}
break;
}
}
return nErr;
}
/*
** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
** given expression should sort as numeric values or as text.
**
** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
** both been called on the expression before it is passed to this routine.
*/
if( p==0 ) return SQLITE_SO_NUM;
while( p ) switch( p->op ){
case TK_PLUS:
case TK_MINUS:
case TK_STAR:
case TK_SLASH:
case TK_AND:
case TK_OR:
case TK_ISNULL:
case TK_NOTNULL:
case TK_NOT:
case TK_UMINUS:
case TK_UPLUS:
case TK_BITAND:
case TK_BITOR:
case TK_BITNOT:
case TK_LSHIFT:
case TK_RSHIFT:
case TK_REM:
case TK_INTEGER:
case TK_FLOAT:
case TK_IN:
case TK_BETWEEN:
case TK_GLOB:
case TK_LIKE:
return SQLITE_SO_NUM;
case TK_STRING:
case TK_NULL:
case TK_CONCAT:
case TK_VARIABLE:
return SQLITE_SO_TEXT;
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ:
return SQLITE_SO_NUM;
}
p = p->pRight;
break;
case TK_AS:
p = p->pLeft;
break;
case TK_COLUMN:
case TK_FUNCTION:
case TK_AGG_FUNCTION:
return p->dataType;
case TK_SELECT:
break;
case TK_CASE: {
return SQLITE_SO_NUM;
}
if( p->pList ){
int i;
return SQLITE_SO_NUM;
}
}
}
return SQLITE_SO_TEXT;
}
default:
break;
}
return SQLITE_SO_NUM;
}
/*
** Generate code into the current Vdbe to evaluate the given
** expression and leave the result on the top of stack.
*/
int op;
if( v==0 || pExpr==0 ) return;
default: break;
}
case TK_COLUMN: {
}else{
}
break;
}
case TK_STRING:
case TK_FLOAT:
case TK_INTEGER: {
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0);
}
sqliteVdbeDequoteP3(v, -1);
break;
}
case TK_NULL: {
sqliteVdbeAddOp(v, OP_String, 0, 0);
break;
}
case TK_VARIABLE: {
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
}
/* Fall through into the next case */
}
case TK_AND:
case TK_OR:
case TK_PLUS:
case TK_STAR:
case TK_MINUS:
case TK_REM:
case TK_BITAND:
case TK_BITOR:
case TK_SLASH: {
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_LSHIFT:
case TK_RSHIFT: {
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_CONCAT: {
break;
}
case TK_UMINUS: {
char *z = sqliteMalloc( p->n + 2 );
sprintf(z, "-%.*s", p->n, p->z);
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0);
}
sqliteFree(z);
break;
}
/* Fall through into TK_NOT */
}
case TK_BITNOT:
case TK_NOT: {
sqliteVdbeAddOp(v, op, 0, 0);
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
int dest;
break;
}
case TK_AGG_FUNCTION: {
break;
}
case TK_GLOB:
case TK_LIKE:
case TK_FUNCTION: {
int nId;
const char *zId;
break;
}
case TK_SELECT: {
break;
}
case TK_IN: {
int addr;
addr = sqliteVdbeCurrentAddr(v);
sqliteVdbeAddOp(v, OP_String, 0, 0);
}else{
}
break;
}
case TK_BETWEEN: {
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
sqliteVdbeAddOp(v, OP_Ge, 0, 0);
sqliteVdbeAddOp(v, OP_Le, 0, 0);
sqliteVdbeAddOp(v, OP_And, 0, 0);
break;
}
case TK_UPLUS:
case TK_AS: {
break;
}
case TK_CASE: {
int expr_end_label;
int jumpInst;
int addr;
int nExpr;
int i;
}
for(i=0; i<nExpr; i=i+2){
}else{
}
addr = sqliteVdbeCurrentAddr(v);
}
}
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0);
}
break;
}
case TK_RAISE: {
"RAISE() may only be used within a trigger-program");
return;
}
sqliteVdbeDequoteP3(v, -1);
} else {
"(IGNORE jump)", 0);
}
}
break;
}
}
/*
** Generate code that pushes the value of every element of the given
** expression list onto the stack. If the includeTypes flag is true,
** then also push a string that is the datatype of each element onto
** the stack after the value.
**
** Return the number of elements pushed onto the stack.
*/
int includeTypes /* TRUE to put datatypes on the stack too */
){
int i, n;
Vdbe *v;
if( pList==0 ) return 0;
v = sqliteGetVdbe(pParse);
if( includeTypes ){
sqliteVdbeOp3(v, OP_String, 0, 0,
}
}
return includeTypes ? n*2 : n;
}
/*
** Generate code for a boolean expression such that a jump is made
** to the label "dest" if the expression is true but execution
** continues straight thru if the expression is false.
**
** If the expression evaluates to NULL (neither true nor false), then
** take the jump if the jumpIfNull flag is true.
*/
int op = 0;
if( v==0 || pExpr==0 ) return;
default: break;
}
case TK_AND: {
sqliteVdbeResolveLabel(v, d2);
break;
}
case TK_OR: {
break;
}
case TK_NOT: {
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
}
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
break;
}
case TK_IN: {
int addr;
addr = sqliteVdbeCurrentAddr(v);
}else{
}
break;
}
case TK_BETWEEN: {
int addr;
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
break;
}
default: {
break;
}
}
}
/*
** Generate code for a boolean expression such that a jump is made
** to the label "dest" if the expression is false but execution
** continues straight thru if the expression is true.
**
** If the expression evaluates to NULL (neither true nor false) then
** jump if jumpIfNull is true or fall through if jumpIfNull is false.
*/
int op = 0;
if( v==0 || pExpr==0 ) return;
default: break;
}
case TK_AND: {
break;
}
case TK_OR: {
sqliteVdbeResolveLabel(v, d2);
break;
}
case TK_NOT: {
break;
}
case TK_LT:
case TK_LE:
case TK_GT:
case TK_GE:
case TK_NE:
case TK_EQ: {
/* Convert numeric comparison opcodes into text comparison opcodes.
** This step depends on the fact that the text comparision opcodes are
** always 6 greater than their corresponding numeric comparison
** opcodes.
*/
op += 6;
}
break;
}
case TK_ISNULL:
case TK_NOTNULL: {
break;
}
case TK_IN: {
int addr;
addr = sqliteVdbeCurrentAddr(v);
}else{
}
break;
}
case TK_BETWEEN: {
int addr;
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
addr = sqliteVdbeCurrentAddr(v);
break;
}
default: {
break;
}
}
}
/*
** Do a deep comparison of two expression trees. Return TRUE (non-zero)
** if they are identical and return FALSE if they differ in any way.
*/
int i;
if( pA==0 ){
return pB==0;
}else if( pB==0 ){
return 0;
}
return 0;
}
}
return 0;
}
}
return 1;
}
/*
** Add a new element to the pParse->aAgg[] array and return its index.
*/
if( aAgg==0 ){
return -1;
}
}
}
/*
** Analyze the given expression looking for aggregate functions and
** for variables that need to be added to the pParse->aAgg[] array.
** Make additional entries to the pParse->aAgg[] array as necessary.
**
** This routine should only be called after the expression has been
** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
**
** If errors are seen, leave an error message in zErrMsg and return
** the number of errors.
*/
int i;
int nErr = 0;
if( pExpr==0 ) return 0;
case TK_COLUMN: {
break;
}
}
i = appendAggInfo(pParse);
if( i<0 ) return 1;
}
break;
}
case TK_AGG_FUNCTION: {
break;
}
}
i = appendAggInfo(pParse);
if( i<0 ) return 1;
}
break;
}
default: {
}
}
int i;
for(i=0; nErr==0 && i<n; i++){
}
}
break;
}
}
return nErr;
}
/*
** Locate a user function given a name and a number of arguments.
** Return a pointer to the FuncDef structure that defines that
** function, or return NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed. When createFlag is true
** and the nArg parameter is -1, then only a function that accepts
** any number of arguments will be returned.
**
** If createFlag is false and nArg is -1, then the first valid
** function found is returned. A function is valid if either xFunc
** or xStep is non-zero.
*/
const char *zName, /* Name of the function. Not null-terminated */
int nName, /* Number of characters in the name */
int nArg, /* Number of arguments. -1 means any number */
int createFlag /* Create new entry if true and does not otherwise exist */
){
if( p && !createFlag && nArg<0 ){
return p;
}
pMaybe = 0;
p = p->pNext;
}
return 0;
}
if( p==0 && pMaybe ){
assert( createFlag==0 );
return pMaybe;
}
if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
}
return p;
}