xref: /sqlite-3.40.0/src/treeview.c (revision dedd51ae)
1 /*
2 ** 2015-06-08
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** This file contains C code to implement the TreeView debugging routines.
14 ** These routines print a parse tree to standard output for debugging and
15 ** analysis.
16 **
17 ** The interfaces in this file is only available when compiling
18 ** with SQLITE_DEBUG.
19 */
20 #include "sqliteInt.h"
21 #ifdef SQLITE_DEBUG
22 
23 /*
24 ** Add a new subitem to the tree.  The moreToFollow flag indicates that this
25 ** is not the last item in the tree.
26 */
27 static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
28   if( p==0 ){
29     p = sqlite3_malloc64( sizeof(*p) );
30     if( p==0 ) return 0;
31     memset(p, 0, sizeof(*p));
32   }else{
33     p->iLevel++;
34   }
35   assert( moreToFollow==0 || moreToFollow==1 );
36   if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
37   return p;
38 }
39 
40 /*
41 ** Finished with one layer of the tree
42 */
43 static void sqlite3TreeViewPop(TreeView *p){
44   if( p==0 ) return;
45   p->iLevel--;
46   if( p->iLevel<0 ) sqlite3_free(p);
47 }
48 
49 /*
50 ** Generate a single line of output for the tree, with a prefix that contains
51 ** all the appropriate tree lines
52 */
53 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
54   va_list ap;
55   int i;
56   StrAccum acc;
57   char zBuf[500];
58   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
59   if( p ){
60     for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
61       sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
62     }
63     sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
64   }
65   if( zFormat!=0 ){
66     va_start(ap, zFormat);
67     sqlite3_str_vappendf(&acc, zFormat, ap);
68     va_end(ap);
69     assert( acc.nChar>0 || acc.accError );
70     sqlite3_str_append(&acc, "\n", 1);
71   }
72   sqlite3StrAccumFinish(&acc);
73   fprintf(stdout,"%s", zBuf);
74   fflush(stdout);
75 }
76 
77 /*
78 ** Shorthand for starting a new tree item that consists of a single label
79 */
80 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
81   p = sqlite3TreeViewPush(p, moreFollows);
82   sqlite3TreeViewLine(p, "%s", zLabel);
83 }
84 
85 /*
86 ** Generate a human-readable description of a WITH clause.
87 */
88 void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
89   int i;
90   if( pWith==0 ) return;
91   if( pWith->nCte==0 ) return;
92   if( pWith->pOuter ){
93     sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
94   }else{
95     sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
96   }
97   if( pWith->nCte>0 ){
98     pView = sqlite3TreeViewPush(pView, 1);
99     for(i=0; i<pWith->nCte; i++){
100       StrAccum x;
101       char zLine[1000];
102       const struct Cte *pCte = &pWith->a[i];
103       sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
104       sqlite3_str_appendf(&x, "%s", pCte->zName);
105       if( pCte->pCols && pCte->pCols->nExpr>0 ){
106         char cSep = '(';
107         int j;
108         for(j=0; j<pCte->pCols->nExpr; j++){
109           sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
110           cSep = ',';
111         }
112         sqlite3_str_appendf(&x, ")");
113       }
114       if( pCte->pUse ){
115         sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
116                  pCte->pUse->nUse);
117       }
118       sqlite3StrAccumFinish(&x);
119       sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
120       sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
121       sqlite3TreeViewPop(pView);
122     }
123     sqlite3TreeViewPop(pView);
124   }
125 }
126 
127 /*
128 ** Generate a human-readable description of a SrcList object.
129 */
130 void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
131   int i;
132   for(i=0; i<pSrc->nSrc; i++){
133     const SrcItem *pItem = &pSrc->a[i];
134     StrAccum x;
135     char zLine[100];
136     sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
137     x.printfFlags |= SQLITE_PRINTF_INTERNAL;
138     sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
139     if( pItem->pTab ){
140       sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
141            pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
142     }
143     if( pItem->fg.jointype & JT_LEFT ){
144       sqlite3_str_appendf(&x, " LEFT-JOIN");
145     }else if( pItem->fg.jointype & JT_CROSS ){
146       sqlite3_str_appendf(&x, " CROSS-JOIN");
147     }
148     if( pItem->fg.fromDDL ){
149       sqlite3_str_appendf(&x, " DDL");
150     }
151     if( pItem->fg.isCte ){
152       sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
153     }
154     sqlite3StrAccumFinish(&x);
155     sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
156     if( pItem->pSelect ){
157       sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
158     }
159     if( pItem->fg.isTabFunc ){
160       sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
161     }
162     sqlite3TreeViewPop(pView);
163   }
164 }
165 
166 /*
167 ** Generate a human-readable description of a Select object.
168 */
169 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
170   int n = 0;
171   int cnt = 0;
172   if( p==0 ){
173     sqlite3TreeViewLine(pView, "nil-SELECT");
174     return;
175   }
176   pView = sqlite3TreeViewPush(pView, moreToFollow);
177   if( p->pWith ){
178     sqlite3TreeViewWith(pView, p->pWith, 1);
179     cnt = 1;
180     sqlite3TreeViewPush(pView, 1);
181   }
182   do{
183     if( p->selFlags & SF_WhereBegin ){
184       sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
185     }else{
186       sqlite3TreeViewLine(pView,
187         "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
188         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
189         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
190         p->selId, p, p->selFlags,
191         (int)p->nSelectRow
192       );
193     }
194     if( cnt++ ) sqlite3TreeViewPop(pView);
195     if( p->pPrior ){
196       n = 1000;
197     }else{
198       n = 0;
199       if( p->pSrc && p->pSrc->nSrc ) n++;
200       if( p->pWhere ) n++;
201       if( p->pGroupBy ) n++;
202       if( p->pHaving ) n++;
203       if( p->pOrderBy ) n++;
204       if( p->pLimit ) n++;
205 #ifndef SQLITE_OMIT_WINDOWFUNC
206       if( p->pWin ) n++;
207       if( p->pWinDefn ) n++;
208 #endif
209     }
210     if( p->pEList ){
211       sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
212     }
213     n--;
214 #ifndef SQLITE_OMIT_WINDOWFUNC
215     if( p->pWin ){
216       Window *pX;
217       pView = sqlite3TreeViewPush(pView, (n--)>0);
218       sqlite3TreeViewLine(pView, "window-functions");
219       for(pX=p->pWin; pX; pX=pX->pNextWin){
220         sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
221       }
222       sqlite3TreeViewPop(pView);
223     }
224 #endif
225     if( p->pSrc && p->pSrc->nSrc ){
226       pView = sqlite3TreeViewPush(pView, (n--)>0);
227       sqlite3TreeViewLine(pView, "FROM");
228       sqlite3TreeViewSrcList(pView, p->pSrc);
229       sqlite3TreeViewPop(pView);
230     }
231     if( p->pWhere ){
232       sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
233       sqlite3TreeViewExpr(pView, p->pWhere, 0);
234       sqlite3TreeViewPop(pView);
235     }
236     if( p->pGroupBy ){
237       sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
238     }
239     if( p->pHaving ){
240       sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
241       sqlite3TreeViewExpr(pView, p->pHaving, 0);
242       sqlite3TreeViewPop(pView);
243     }
244 #ifndef SQLITE_OMIT_WINDOWFUNC
245     if( p->pWinDefn ){
246       Window *pX;
247       sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
248       for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
249         sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
250       }
251       sqlite3TreeViewPop(pView);
252     }
253 #endif
254     if( p->pOrderBy ){
255       sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
256     }
257     if( p->pLimit ){
258       sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
259       sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
260       if( p->pLimit->pRight ){
261         sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
262         sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
263         sqlite3TreeViewPop(pView);
264       }
265       sqlite3TreeViewPop(pView);
266     }
267     if( p->pPrior ){
268       const char *zOp = "UNION";
269       switch( p->op ){
270         case TK_ALL:         zOp = "UNION ALL";  break;
271         case TK_INTERSECT:   zOp = "INTERSECT";  break;
272         case TK_EXCEPT:      zOp = "EXCEPT";     break;
273       }
274       sqlite3TreeViewItem(pView, zOp, 1);
275     }
276     p = p->pPrior;
277   }while( p!=0 );
278   sqlite3TreeViewPop(pView);
279 }
280 
281 #ifndef SQLITE_OMIT_WINDOWFUNC
282 /*
283 ** Generate a description of starting or stopping bounds
284 */
285 void sqlite3TreeViewBound(
286   TreeView *pView,        /* View context */
287   u8 eBound,              /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
288   Expr *pExpr,            /* Value for PRECEDING or FOLLOWING */
289   u8 moreToFollow         /* True if more to follow */
290 ){
291   switch( eBound ){
292     case TK_UNBOUNDED: {
293       sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
294       sqlite3TreeViewPop(pView);
295       break;
296     }
297     case TK_CURRENT: {
298       sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
299       sqlite3TreeViewPop(pView);
300       break;
301     }
302     case TK_PRECEDING: {
303       sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
304       sqlite3TreeViewExpr(pView, pExpr, 0);
305       sqlite3TreeViewPop(pView);
306       break;
307     }
308     case TK_FOLLOWING: {
309       sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
310       sqlite3TreeViewExpr(pView, pExpr, 0);
311       sqlite3TreeViewPop(pView);
312       break;
313     }
314   }
315 }
316 #endif /* SQLITE_OMIT_WINDOWFUNC */
317 
318 #ifndef SQLITE_OMIT_WINDOWFUNC
319 /*
320 ** Generate a human-readable explanation for a Window object
321 */
322 void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
323   int nElement = 0;
324   if( pWin->pFilter ){
325     sqlite3TreeViewItem(pView, "FILTER", 1);
326     sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
327     sqlite3TreeViewPop(pView);
328   }
329   pView = sqlite3TreeViewPush(pView, more);
330   if( pWin->zName ){
331     sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
332   }else{
333     sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
334   }
335   if( pWin->zBase )    nElement++;
336   if( pWin->pOrderBy ) nElement++;
337   if( pWin->eFrmType ) nElement++;
338   if( pWin->eExclude ) nElement++;
339   if( pWin->zBase ){
340     sqlite3TreeViewPush(pView, (--nElement)>0);
341     sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
342     sqlite3TreeViewPop(pView);
343   }
344   if( pWin->pPartition ){
345     sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
346   }
347   if( pWin->pOrderBy ){
348     sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
349   }
350   if( pWin->eFrmType ){
351     char zBuf[30];
352     const char *zFrmType = "ROWS";
353     if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
354     if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
355     sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
356         pWin->bImplicitFrame ? " (implied)" : "");
357     sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
358     sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
359     sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
360     sqlite3TreeViewPop(pView);
361   }
362   if( pWin->eExclude ){
363     char zBuf[30];
364     const char *zExclude;
365     switch( pWin->eExclude ){
366       case TK_NO:      zExclude = "NO OTHERS";   break;
367       case TK_CURRENT: zExclude = "CURRENT ROW"; break;
368       case TK_GROUP:   zExclude = "GROUP";       break;
369       case TK_TIES:    zExclude = "TIES";        break;
370       default:
371         sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
372         zExclude = zBuf;
373         break;
374     }
375     sqlite3TreeViewPush(pView, 0);
376     sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
377     sqlite3TreeViewPop(pView);
378   }
379   sqlite3TreeViewPop(pView);
380 }
381 #endif /* SQLITE_OMIT_WINDOWFUNC */
382 
383 #ifndef SQLITE_OMIT_WINDOWFUNC
384 /*
385 ** Generate a human-readable explanation for a Window Function object
386 */
387 void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
388   pView = sqlite3TreeViewPush(pView, more);
389   sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
390                        pWin->pFunc->zName, pWin->pFunc->nArg);
391   sqlite3TreeViewWindow(pView, pWin, 0);
392   sqlite3TreeViewPop(pView);
393 }
394 #endif /* SQLITE_OMIT_WINDOWFUNC */
395 
396 /*
397 ** Generate a human-readable explanation of an expression tree.
398 */
399 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
400   const char *zBinOp = 0;   /* Binary operator */
401   const char *zUniOp = 0;   /* Unary operator */
402   char zFlgs[200];
403   pView = sqlite3TreeViewPush(pView, moreToFollow);
404   if( pExpr==0 ){
405     sqlite3TreeViewLine(pView, "nil");
406     sqlite3TreeViewPop(pView);
407     return;
408   }
409   if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
410     StrAccum x;
411     sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
412     sqlite3_str_appendf(&x, " fg.af=%x.%c",
413       pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
414     if( ExprHasProperty(pExpr, EP_FromJoin) ){
415       sqlite3_str_appendf(&x, " iRJT=%d", pExpr->w.iRightJoinTable);
416     }
417     if( ExprHasProperty(pExpr, EP_FromDDL) ){
418       sqlite3_str_appendf(&x, " DDL");
419     }
420     if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
421       sqlite3_str_appendf(&x, " IMMUTABLE");
422     }
423     sqlite3StrAccumFinish(&x);
424   }else{
425     zFlgs[0] = 0;
426   }
427   switch( pExpr->op ){
428     case TK_AGG_COLUMN: {
429       sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
430             pExpr->iTable, pExpr->iColumn, zFlgs);
431       break;
432     }
433     case TK_COLUMN: {
434       if( pExpr->iTable<0 ){
435         /* This only happens when coding check constraints */
436         char zOp2[16];
437         if( pExpr->op2 ){
438           sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
439         }else{
440           zOp2[0] = 0;
441         }
442         sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
443                                     pExpr->iColumn, zFlgs, zOp2);
444       }else{
445         assert( ExprUseYTab(pExpr) );
446         sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
447                         pExpr->iTable, pExpr->iColumn,
448                         pExpr->y.pTab, zFlgs);
449       }
450       if( ExprHasProperty(pExpr, EP_FixedCol) ){
451         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
452       }
453       break;
454     }
455     case TK_INTEGER: {
456       if( pExpr->flags & EP_IntValue ){
457         sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
458       }else{
459         sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
460       }
461       break;
462     }
463 #ifndef SQLITE_OMIT_FLOATING_POINT
464     case TK_FLOAT: {
465       assert( !ExprHasProperty(pExpr, EP_IntValue) );
466       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
467       break;
468     }
469 #endif
470     case TK_STRING: {
471       assert( !ExprHasProperty(pExpr, EP_IntValue) );
472       sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
473       break;
474     }
475     case TK_NULL: {
476       sqlite3TreeViewLine(pView,"NULL");
477       break;
478     }
479     case TK_TRUEFALSE: {
480       sqlite3TreeViewLine(pView,"%s%s",
481          sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
482       break;
483     }
484 #ifndef SQLITE_OMIT_BLOB_LITERAL
485     case TK_BLOB: {
486       assert( !ExprHasProperty(pExpr, EP_IntValue) );
487       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
488       break;
489     }
490 #endif
491     case TK_VARIABLE: {
492       assert( !ExprHasProperty(pExpr, EP_IntValue) );
493       sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
494                           pExpr->u.zToken, pExpr->iColumn);
495       break;
496     }
497     case TK_REGISTER: {
498       sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
499       break;
500     }
501     case TK_ID: {
502       assert( !ExprHasProperty(pExpr, EP_IntValue) );
503       sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
504       break;
505     }
506 #ifndef SQLITE_OMIT_CAST
507     case TK_CAST: {
508       /* Expressions of the form:   CAST(pLeft AS token) */
509       assert( !ExprHasProperty(pExpr, EP_IntValue) );
510       sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
511       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
512       break;
513     }
514 #endif /* SQLITE_OMIT_CAST */
515     case TK_LT:      zBinOp = "LT";     break;
516     case TK_LE:      zBinOp = "LE";     break;
517     case TK_GT:      zBinOp = "GT";     break;
518     case TK_GE:      zBinOp = "GE";     break;
519     case TK_NE:      zBinOp = "NE";     break;
520     case TK_EQ:      zBinOp = "EQ";     break;
521     case TK_IS:      zBinOp = "IS";     break;
522     case TK_ISNOT:   zBinOp = "ISNOT";  break;
523     case TK_AND:     zBinOp = "AND";    break;
524     case TK_OR:      zBinOp = "OR";     break;
525     case TK_PLUS:    zBinOp = "ADD";    break;
526     case TK_STAR:    zBinOp = "MUL";    break;
527     case TK_MINUS:   zBinOp = "SUB";    break;
528     case TK_REM:     zBinOp = "REM";    break;
529     case TK_BITAND:  zBinOp = "BITAND"; break;
530     case TK_BITOR:   zBinOp = "BITOR";  break;
531     case TK_SLASH:   zBinOp = "DIV";    break;
532     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
533     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
534     case TK_CONCAT:  zBinOp = "CONCAT"; break;
535     case TK_DOT:     zBinOp = "DOT";    break;
536     case TK_LIMIT:   zBinOp = "LIMIT";  break;
537 
538     case TK_UMINUS:  zUniOp = "UMINUS"; break;
539     case TK_UPLUS:   zUniOp = "UPLUS";  break;
540     case TK_BITNOT:  zUniOp = "BITNOT"; break;
541     case TK_NOT:     zUniOp = "NOT";    break;
542     case TK_ISNULL:  zUniOp = "ISNULL"; break;
543     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
544 
545     case TK_TRUTH: {
546       int x;
547       const char *azOp[] = {
548          "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
549       };
550       assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
551       assert( pExpr->pRight );
552       assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
553       x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
554       zUniOp = azOp[x];
555       break;
556     }
557 
558     case TK_SPAN: {
559       assert( !ExprHasProperty(pExpr, EP_IntValue) );
560       sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
561       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
562       break;
563     }
564 
565     case TK_COLLATE: {
566       /* COLLATE operators without the EP_Collate flag are intended to
567       ** emulate collation associated with a table column.  These show
568       ** up in the treeview output as "SOFT-COLLATE".  Explicit COLLATE
569       ** operators that appear in the original SQL always have the
570       ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
571       assert( !ExprHasProperty(pExpr, EP_IntValue) );
572       sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
573         !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
574         pExpr->u.zToken, zFlgs);
575       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
576       break;
577     }
578 
579     case TK_AGG_FUNCTION:
580     case TK_FUNCTION: {
581       ExprList *pFarg;       /* List of function arguments */
582       Window *pWin;
583       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
584         pFarg = 0;
585         pWin = 0;
586       }else{
587         assert( ExprUseXList(pExpr) );
588         pFarg = pExpr->x.pList;
589 #ifndef SQLITE_OMIT_WINDOWFUNC
590         pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
591 #else
592         pWin = 0;
593 #endif
594       }
595       assert( !ExprHasProperty(pExpr, EP_IntValue) );
596       if( pExpr->op==TK_AGG_FUNCTION ){
597         sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
598                              pExpr->op2, pExpr->u.zToken, zFlgs,
599                              pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
600                              pExpr->iAgg, pExpr->pAggInfo);
601       }else if( pExpr->op2!=0 ){
602         const char *zOp2;
603         char zBuf[8];
604         sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
605         zOp2 = zBuf;
606         if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
607         if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
608         if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
609         if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
610         sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
611                             pExpr->u.zToken, zFlgs, zOp2);
612       }else{
613         sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
614       }
615       if( pFarg ){
616         sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
617       }
618 #ifndef SQLITE_OMIT_WINDOWFUNC
619       if( pWin ){
620         sqlite3TreeViewWindow(pView, pWin, 0);
621       }
622 #endif
623       break;
624     }
625 #ifndef SQLITE_OMIT_SUBQUERY
626     case TK_EXISTS: {
627       assert( ExprUseXSelect(pExpr) );
628       sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
629       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
630       break;
631     }
632     case TK_SELECT: {
633       assert( ExprUseXSelect(pExpr) );
634       sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
635       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
636       break;
637     }
638     case TK_IN: {
639       sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
640       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
641       if( ExprUseXSelect(pExpr) ){
642         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
643       }else{
644         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
645       }
646       break;
647     }
648 #endif /* SQLITE_OMIT_SUBQUERY */
649 
650     /*
651     **    x BETWEEN y AND z
652     **
653     ** This is equivalent to
654     **
655     **    x>=y AND x<=z
656     **
657     ** X is stored in pExpr->pLeft.
658     ** Y is stored in pExpr->pList->a[0].pExpr.
659     ** Z is stored in pExpr->pList->a[1].pExpr.
660     */
661     case TK_BETWEEN: {
662       const Expr *pX, *pY, *pZ;
663       pX = pExpr->pLeft;
664       assert( ExprUseXList(pExpr) );
665       assert( pExpr->x.pList->nExpr==2 );
666       pY = pExpr->x.pList->a[0].pExpr;
667       pZ = pExpr->x.pList->a[1].pExpr;
668       sqlite3TreeViewLine(pView, "BETWEEN");
669       sqlite3TreeViewExpr(pView, pX, 1);
670       sqlite3TreeViewExpr(pView, pY, 1);
671       sqlite3TreeViewExpr(pView, pZ, 0);
672       break;
673     }
674     case TK_TRIGGER: {
675       /* If the opcode is TK_TRIGGER, then the expression is a reference
676       ** to a column in the new.* or old.* pseudo-tables available to
677       ** trigger programs. In this case Expr.iTable is set to 1 for the
678       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
679       ** is set to the column of the pseudo-table to read, or to -1 to
680       ** read the rowid field.
681       */
682       sqlite3TreeViewLine(pView, "%s(%d)",
683           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
684       break;
685     }
686     case TK_CASE: {
687       sqlite3TreeViewLine(pView, "CASE");
688       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
689       assert( ExprUseXList(pExpr) );
690       sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
691       break;
692     }
693 #ifndef SQLITE_OMIT_TRIGGER
694     case TK_RAISE: {
695       const char *zType = "unk";
696       switch( pExpr->affExpr ){
697         case OE_Rollback:   zType = "rollback";  break;
698         case OE_Abort:      zType = "abort";     break;
699         case OE_Fail:       zType = "fail";      break;
700         case OE_Ignore:     zType = "ignore";    break;
701       }
702       assert( !ExprHasProperty(pExpr, EP_IntValue) );
703       sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
704       break;
705     }
706 #endif
707     case TK_MATCH: {
708       sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
709                           pExpr->iTable, pExpr->iColumn, zFlgs);
710       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
711       break;
712     }
713     case TK_VECTOR: {
714       char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
715       assert( ExprUseXList(pExpr) );
716       sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
717       sqlite3_free(z);
718       break;
719     }
720     case TK_SELECT_COLUMN: {
721       sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
722               pExpr->iColumn, pExpr->iTable-1,
723               pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
724       assert( ExprUseXSelect(pExpr->pLeft) );
725       sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
726       break;
727     }
728     case TK_IF_NULL_ROW: {
729       sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
730       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
731       break;
732     }
733     case TK_ERROR: {
734       Expr tmp;
735       sqlite3TreeViewLine(pView, "ERROR");
736       tmp = *pExpr;
737       tmp.op = pExpr->op2;
738       sqlite3TreeViewExpr(pView, &tmp, 0);
739       break;
740     }
741     case TK_ROW: {
742       if( pExpr->iColumn<=0 ){
743         sqlite3TreeViewLine(pView, "First FROM table rowid");
744       }else{
745         sqlite3TreeViewLine(pView, "First FROM table column %d",
746             pExpr->iColumn-1);
747       }
748       break;
749     }
750     default: {
751       sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
752       break;
753     }
754   }
755   if( zBinOp ){
756     sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
757     sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
758     sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
759   }else if( zUniOp ){
760     sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
761    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
762   }
763   sqlite3TreeViewPop(pView);
764 }
765 
766 
767 /*
768 ** Generate a human-readable explanation of an expression list.
769 */
770 void sqlite3TreeViewBareExprList(
771   TreeView *pView,
772   const ExprList *pList,
773   const char *zLabel
774 ){
775   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
776   if( pList==0 ){
777     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
778   }else{
779     int i;
780     sqlite3TreeViewLine(pView, "%s", zLabel);
781     for(i=0; i<pList->nExpr; i++){
782       int j = pList->a[i].u.x.iOrderByCol;
783       char *zName = pList->a[i].zEName;
784       int moreToFollow = i<pList->nExpr - 1;
785       if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
786       if( j || zName ){
787         sqlite3TreeViewPush(pView, moreToFollow);
788         moreToFollow = 0;
789         sqlite3TreeViewLine(pView, 0);
790         if( zName ){
791           fprintf(stdout, "AS %s ", zName);
792         }
793         if( j ){
794           fprintf(stdout, "iOrderByCol=%d", j);
795         }
796         fprintf(stdout, "\n");
797         fflush(stdout);
798       }
799       sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
800       if( j || zName ){
801         sqlite3TreeViewPop(pView);
802       }
803     }
804   }
805 }
806 void sqlite3TreeViewExprList(
807   TreeView *pView,
808   const ExprList *pList,
809   u8 moreToFollow,
810   const char *zLabel
811 ){
812   pView = sqlite3TreeViewPush(pView, moreToFollow);
813   sqlite3TreeViewBareExprList(pView, pList, zLabel);
814   sqlite3TreeViewPop(pView);
815 }
816 
817 #endif /* SQLITE_DEBUG */
818