2021-05-14 09:07:09 +00:00
|
|
|
/*
|
|
|
|
** 2008 August 16
|
|
|
|
**
|
|
|
|
** 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 walking the parser tree for
|
|
|
|
** an SQL statement.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
#include "libc/mem/mem.h"
|
|
|
|
#include "libc/str/str.h"
|
2021-05-14 17:18:28 +00:00
|
|
|
#include "third_party/sqlite3/sqliteInt.inc"
|
2021-05-14 09:07:09 +00:00
|
|
|
|
|
|
|
#if !defined(SQLITE_OMIT_WINDOWFUNC)
|
|
|
|
/*
|
|
|
|
** Walk all expressions linked into the list of Window objects passed
|
|
|
|
** as the second argument.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly) {
|
2021-05-14 09:07:09 +00:00
|
|
|
Window *pWin;
|
2021-05-04 14:39:59 +00:00
|
|
|
for (pWin = pList; pWin; pWin = pWin->pNextWin) {
|
2021-05-14 09:07:09 +00:00
|
|
|
int rc;
|
|
|
|
rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (rc) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (rc) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (rc) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
|
|
|
|
/* The next two are purely for calls to sqlite3RenameExprUnmap()
|
|
|
|
** within sqlite3WindowOffsetExpr(). Because of constraints imposed
|
|
|
|
** by sqlite3WindowOffsetExpr(), they can never fail. The results do
|
|
|
|
** not matter anyhow. */
|
|
|
|
rc = sqlite3WalkExpr(pWalker, pWin->pStart);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (NEVER(rc)) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (NEVER(rc)) return WRC_Abort;
|
|
|
|
if (bOneOnly) break;
|
2021-05-14 09:07:09 +00:00
|
|
|
}
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Walk an expression tree. Invoke the callback once for each node
|
|
|
|
** of the expression, while descending. (In other words, the callback
|
|
|
|
** is invoked before visiting children.)
|
|
|
|
**
|
|
|
|
** The return value from the callback should be one of the WRC_*
|
|
|
|
** constants to specify how to proceed with the walk.
|
|
|
|
**
|
|
|
|
** WRC_Continue Continue descending down the tree.
|
|
|
|
**
|
|
|
|
** WRC_Prune Do not descend into child nodes, but allow
|
|
|
|
** the walk to continue with sibling nodes.
|
|
|
|
**
|
|
|
|
** WRC_Abort Do no more callbacks. Unwind the stack and
|
|
|
|
** return from the top-level walk call.
|
|
|
|
**
|
|
|
|
** The return value from this routine is WRC_Abort to abandon the tree walk
|
|
|
|
** and WRC_Continue to continue.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr) {
|
2021-05-14 09:07:09 +00:00
|
|
|
int rc;
|
2021-05-04 14:39:59 +00:00
|
|
|
testcase(ExprHasProperty(pExpr, EP_TokenOnly));
|
|
|
|
testcase(ExprHasProperty(pExpr, EP_Reduced));
|
|
|
|
while (1) {
|
2021-05-14 09:07:09 +00:00
|
|
|
rc = pWalker->xExprCallback(pWalker, pExpr);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (rc) return rc & WRC_Abort;
|
|
|
|
if (!ExprHasProperty(pExpr, (EP_TokenOnly | EP_Leaf))) {
|
|
|
|
assert(pExpr->x.pList == 0 || pExpr->pRight == 0);
|
|
|
|
if (pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft)) return WRC_Abort;
|
|
|
|
if (pExpr->pRight) {
|
|
|
|
assert(!ExprHasProperty(pExpr, EP_WinFunc));
|
2021-05-14 09:07:09 +00:00
|
|
|
pExpr = pExpr->pRight;
|
|
|
|
continue;
|
2021-05-04 14:39:59 +00:00
|
|
|
} else if (ExprHasProperty(pExpr, EP_xIsSelect)) {
|
|
|
|
assert(!ExprHasProperty(pExpr, EP_WinFunc));
|
|
|
|
if (sqlite3WalkSelect(pWalker, pExpr->x.pSelect)) return WRC_Abort;
|
|
|
|
} else {
|
|
|
|
if (pExpr->x.pList) {
|
|
|
|
if (sqlite3WalkExprList(pWalker, pExpr->x.pList)) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
}
|
|
|
|
#ifndef SQLITE_OMIT_WINDOWFUNC
|
2021-05-04 14:39:59 +00:00
|
|
|
if (ExprHasProperty(pExpr, EP_WinFunc)) {
|
|
|
|
if (walkWindowList(pWalker, pExpr->y.pWin, 1)) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr) {
|
|
|
|
return pExpr ? walkExpr(pWalker, pExpr) : WRC_Continue;
|
2021-05-14 09:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Call sqlite3WalkExpr() for every expression in list p or until
|
|
|
|
** an abort request is seen.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkExprList(Walker *pWalker, ExprList *p) {
|
2021-05-14 09:07:09 +00:00
|
|
|
int i;
|
|
|
|
struct ExprList_item *pItem;
|
2021-05-04 14:39:59 +00:00
|
|
|
if (p) {
|
|
|
|
for (i = p->nExpr, pItem = p->a; i > 0; i--, pItem++) {
|
|
|
|
if (sqlite3WalkExpr(pWalker, pItem->pExpr)) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Walk all expressions associated with SELECT statement p. Do
|
|
|
|
** not invoke the SELECT callback on p, but do (of course) invoke
|
|
|
|
** any expr callbacks and SELECT callbacks that come from subqueries.
|
|
|
|
** Return WRC_Abort or WRC_Continue.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkSelectExpr(Walker *pWalker, Select *p) {
|
|
|
|
if (sqlite3WalkExprList(pWalker, p->pEList)) return WRC_Abort;
|
|
|
|
if (sqlite3WalkExpr(pWalker, p->pWhere)) return WRC_Abort;
|
|
|
|
if (sqlite3WalkExprList(pWalker, p->pGroupBy)) return WRC_Abort;
|
|
|
|
if (sqlite3WalkExpr(pWalker, p->pHaving)) return WRC_Abort;
|
|
|
|
if (sqlite3WalkExprList(pWalker, p->pOrderBy)) return WRC_Abort;
|
|
|
|
if (sqlite3WalkExpr(pWalker, p->pLimit)) return WRC_Abort;
|
2021-05-14 09:07:09 +00:00
|
|
|
#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
|
|
|
|
{
|
|
|
|
Parse *pParse = pWalker->pParse;
|
2021-05-04 14:39:59 +00:00
|
|
|
if (pParse && IN_RENAME_OBJECT) {
|
2021-05-14 09:07:09 +00:00
|
|
|
/* The following may return WRC_Abort if there are unresolvable
|
|
|
|
** symbols (e.g. a table that does not exist) in a window definition. */
|
|
|
|
int rc = walkWindowList(pWalker, p->pWinDefn, 0);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Walk the parse trees associated with all subqueries in the
|
|
|
|
** FROM clause of SELECT statement p. Do not invoke the select
|
|
|
|
** callback on p, but do invoke it on each FROM clause subquery
|
2021-05-04 14:39:59 +00:00
|
|
|
** and on any subqueries further down in the tree. Return
|
2021-05-14 09:07:09 +00:00
|
|
|
** WRC_Abort or WRC_Continue;
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkSelectFrom(Walker *pWalker, Select *p) {
|
2021-05-14 09:07:09 +00:00
|
|
|
SrcList *pSrc;
|
|
|
|
int i;
|
|
|
|
SrcItem *pItem;
|
|
|
|
|
|
|
|
pSrc = p->pSrc;
|
2021-05-04 14:39:59 +00:00
|
|
|
if (pSrc) {
|
|
|
|
for (i = pSrc->nSrc, pItem = pSrc->a; i > 0; i--, pItem++) {
|
|
|
|
if (pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect)) {
|
2021-05-14 09:07:09 +00:00
|
|
|
return WRC_Abort;
|
|
|
|
}
|
2021-05-04 14:39:59 +00:00
|
|
|
if (pItem->fg.isTabFunc &&
|
|
|
|
sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)) {
|
2021-05-14 09:07:09 +00:00
|
|
|
return WRC_Abort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WRC_Continue;
|
2021-05-04 14:39:59 +00:00
|
|
|
}
|
2021-05-14 09:07:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Call sqlite3WalkExpr() for every expression in Select statement p.
|
|
|
|
** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
|
2021-05-04 14:39:59 +00:00
|
|
|
** on the compound select chain, p->pPrior.
|
2021-05-14 09:07:09 +00:00
|
|
|
**
|
|
|
|
** If it is not NULL, the xSelectCallback() callback is invoked before
|
|
|
|
** the walk of the expressions and FROM clause. The xSelectCallback2()
|
|
|
|
** method is invoked following the walk of the expressions and FROM clause,
|
|
|
|
** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
|
|
|
|
** and if the expressions and FROM clause both return WRC_Continue;
|
|
|
|
**
|
|
|
|
** Return WRC_Continue under normal conditions. Return WRC_Abort if
|
|
|
|
** there is an abort request.
|
|
|
|
**
|
|
|
|
** If the Walker does not have an xSelectCallback() then this routine
|
|
|
|
** is a no-op returning WRC_Continue.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkSelect(Walker *pWalker, Select *p) {
|
2021-05-14 09:07:09 +00:00
|
|
|
int rc;
|
2021-05-04 14:39:59 +00:00
|
|
|
if (p == 0) return WRC_Continue;
|
|
|
|
if (pWalker->xSelectCallback == 0) return WRC_Continue;
|
|
|
|
do {
|
2021-05-14 09:07:09 +00:00
|
|
|
rc = pWalker->xSelectCallback(pWalker, p);
|
2021-05-04 14:39:59 +00:00
|
|
|
if (rc) return rc & WRC_Abort;
|
|
|
|
if (sqlite3WalkSelectExpr(pWalker, p) ||
|
|
|
|
sqlite3WalkSelectFrom(pWalker, p)) {
|
2021-05-14 09:07:09 +00:00
|
|
|
return WRC_Abort;
|
|
|
|
}
|
2021-05-04 14:39:59 +00:00
|
|
|
if (pWalker->xSelectCallback2) {
|
2021-05-14 09:07:09 +00:00
|
|
|
pWalker->xSelectCallback2(pWalker, p);
|
|
|
|
}
|
|
|
|
p = p->pPrior;
|
2021-05-04 14:39:59 +00:00
|
|
|
} while (p != 0);
|
2021-05-14 09:07:09 +00:00
|
|
|
return WRC_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase the walkerDepth when entering a subquery, and
|
|
|
|
** descrease when leaving the subquery.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect) {
|
2021-05-14 09:07:09 +00:00
|
|
|
UNUSED_PARAMETER(pSelect);
|
|
|
|
pWalker->walkerDepth++;
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
2021-05-04 14:39:59 +00:00
|
|
|
void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect) {
|
2021-05-14 09:07:09 +00:00
|
|
|
UNUSED_PARAMETER(pSelect);
|
|
|
|
pWalker->walkerDepth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** No-op routine for the parse-tree walker.
|
|
|
|
**
|
|
|
|
** When this routine is the Walker.xExprCallback then expression trees
|
|
|
|
** are walked without any actions being taken at each node. Presumably,
|
2021-05-04 14:39:59 +00:00
|
|
|
** when this routine is used for Walker.xExprCallback then
|
|
|
|
** Walker.xSelectCallback is set to do something useful for every
|
2021-05-14 09:07:09 +00:00
|
|
|
** subquery in the parser tree.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2) {
|
2021-05-14 09:07:09 +00:00
|
|
|
UNUSED_PARAMETER2(NotUsed, NotUsed2);
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** No-op routine for the parse-tree walker for SELECT statements.
|
|
|
|
** subquery in the parser tree.
|
|
|
|
*/
|
2021-05-04 14:39:59 +00:00
|
|
|
int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2) {
|
2021-05-14 09:07:09 +00:00
|
|
|
UNUSED_PARAMETER2(NotUsed, NotUsed2);
|
|
|
|
return WRC_Continue;
|
|
|
|
}
|