xref: /sqlite-3.40.0/src/treeview.c (revision 9c42626e)
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       sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
62     }
63     sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
64   }
65   va_start(ap, zFormat);
66   sqlite3VXPrintf(&acc, zFormat, ap);
67   va_end(ap);
68   assert( acc.nChar>0 );
69   if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
70   sqlite3StrAccumFinish(&acc);
71   fprintf(stdout,"%s", zBuf);
72   fflush(stdout);
73 }
74 
75 /*
76 ** Shorthand for starting a new tree item that consists of a single label
77 */
78 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
79   p = sqlite3TreeViewPush(p, moreFollows);
80   sqlite3TreeViewLine(p, "%s", zLabel);
81 }
82 
83 /*
84 ** Generate a human-readable description of a WITH clause.
85 */
86 void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
87   int i;
88   if( pWith==0 ) return;
89   if( pWith->nCte==0 ) return;
90   if( pWith->pOuter ){
91     sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
92   }else{
93     sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
94   }
95   if( pWith->nCte>0 ){
96     pView = sqlite3TreeViewPush(pView, 1);
97     for(i=0; i<pWith->nCte; i++){
98       StrAccum x;
99       char zLine[1000];
100       const struct Cte *pCte = &pWith->a[i];
101       sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
102       sqlite3XPrintf(&x, "%s", pCte->zName);
103       if( pCte->pCols && pCte->pCols->nExpr>0 ){
104         char cSep = '(';
105         int j;
106         for(j=0; j<pCte->pCols->nExpr; j++){
107           sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
108           cSep = ',';
109         }
110         sqlite3XPrintf(&x, ")");
111       }
112       sqlite3XPrintf(&x, " AS");
113       sqlite3StrAccumFinish(&x);
114       sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
115       sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
116       sqlite3TreeViewPop(pView);
117     }
118     sqlite3TreeViewPop(pView);
119   }
120 }
121 
122 
123 /*
124 ** Generate a human-readable description of a Select object.
125 */
126 void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
127   int n = 0;
128   int cnt = 0;
129   if( p==0 ){
130     sqlite3TreeViewLine(pView, "nil-SELECT");
131     return;
132   }
133   pView = sqlite3TreeViewPush(pView, moreToFollow);
134   if( p->pWith ){
135     sqlite3TreeViewWith(pView, p->pWith, 1);
136     cnt = 1;
137     sqlite3TreeViewPush(pView, 1);
138   }
139   do{
140     sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
141       ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
142       ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
143       (int)p->nSelectRow
144     );
145     if( cnt++ ) sqlite3TreeViewPop(pView);
146     if( p->pPrior ){
147       n = 1000;
148     }else{
149       n = 0;
150       if( p->pSrc && p->pSrc->nSrc ) n++;
151       if( p->pWhere ) n++;
152       if( p->pGroupBy ) n++;
153       if( p->pHaving ) n++;
154       if( p->pOrderBy ) n++;
155       if( p->pLimit ) n++;
156     }
157     sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
158     if( p->pSrc && p->pSrc->nSrc ){
159       int i;
160       pView = sqlite3TreeViewPush(pView, (n--)>0);
161       sqlite3TreeViewLine(pView, "FROM");
162       for(i=0; i<p->pSrc->nSrc; i++){
163         struct SrcList_item *pItem = &p->pSrc->a[i];
164         StrAccum x;
165         char zLine[100];
166         sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
167         sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
168         if( pItem->zDatabase ){
169           sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
170         }else if( pItem->zName ){
171           sqlite3XPrintf(&x, " %s", pItem->zName);
172         }
173         if( pItem->pTab ){
174           sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
175         }
176         if( pItem->zAlias ){
177           sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
178         }
179         if( pItem->fg.jointype & JT_LEFT ){
180           sqlite3XPrintf(&x, " LEFT-JOIN");
181         }
182         sqlite3StrAccumFinish(&x);
183         sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
184         if( pItem->pSelect ){
185           sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
186         }
187         if( pItem->fg.isTabFunc ){
188           sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
189         }
190         sqlite3TreeViewPop(pView);
191       }
192       sqlite3TreeViewPop(pView);
193     }
194     if( p->pWhere ){
195       sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
196       sqlite3TreeViewExpr(pView, p->pWhere, 0);
197       sqlite3TreeViewPop(pView);
198     }
199     if( p->pGroupBy ){
200       sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
201     }
202     if( p->pHaving ){
203       sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
204       sqlite3TreeViewExpr(pView, p->pHaving, 0);
205       sqlite3TreeViewPop(pView);
206     }
207     if( p->pOrderBy ){
208       sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
209     }
210     if( p->pLimit ){
211       sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
212       sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
213       if( p->pLimit->pRight ){
214         sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
215         sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
216         sqlite3TreeViewPop(pView);
217       }
218       sqlite3TreeViewPop(pView);
219     }
220     if( p->pPrior ){
221       const char *zOp = "UNION";
222       switch( p->op ){
223         case TK_ALL:         zOp = "UNION ALL";  break;
224         case TK_INTERSECT:   zOp = "INTERSECT";  break;
225         case TK_EXCEPT:      zOp = "EXCEPT";     break;
226       }
227       sqlite3TreeViewItem(pView, zOp, 1);
228     }
229     p = p->pPrior;
230   }while( p!=0 );
231   sqlite3TreeViewPop(pView);
232 }
233 
234 /*
235 ** Generate a human-readable explanation of an expression tree.
236 */
237 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
238   const char *zBinOp = 0;   /* Binary operator */
239   const char *zUniOp = 0;   /* Unary operator */
240   char zFlgs[60];
241   pView = sqlite3TreeViewPush(pView, moreToFollow);
242   if( pExpr==0 ){
243     sqlite3TreeViewLine(pView, "nil");
244     sqlite3TreeViewPop(pView);
245     return;
246   }
247   if( pExpr->flags ){
248     if( ExprHasProperty(pExpr, EP_FromJoin) ){
249       sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x iRJT=%d",
250                        pExpr->flags, pExpr->iRightJoinTable);
251     }else{
252       sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
253     }
254   }else{
255     zFlgs[0] = 0;
256   }
257   switch( pExpr->op ){
258     case TK_AGG_COLUMN: {
259       sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
260             pExpr->iTable, pExpr->iColumn, zFlgs);
261       break;
262     }
263     case TK_COLUMN: {
264       if( pExpr->iTable<0 ){
265         /* This only happens when coding check constraints */
266         sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
267       }else{
268         sqlite3TreeViewLine(pView, "{%d:%d}%s",
269                              pExpr->iTable, pExpr->iColumn, zFlgs);
270       }
271       break;
272     }
273     case TK_INTEGER: {
274       if( pExpr->flags & EP_IntValue ){
275         sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
276       }else{
277         sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
278       }
279       break;
280     }
281 #ifndef SQLITE_OMIT_FLOATING_POINT
282     case TK_FLOAT: {
283       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
284       break;
285     }
286 #endif
287     case TK_STRING: {
288       sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
289       break;
290     }
291     case TK_NULL: {
292       sqlite3TreeViewLine(pView,"NULL");
293       break;
294     }
295     case TK_TRUEFALSE: {
296       sqlite3TreeViewLine(pView,
297          sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
298       break;
299     }
300 #ifndef SQLITE_OMIT_BLOB_LITERAL
301     case TK_BLOB: {
302       sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
303       break;
304     }
305 #endif
306     case TK_VARIABLE: {
307       sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
308                           pExpr->u.zToken, pExpr->iColumn);
309       break;
310     }
311     case TK_REGISTER: {
312       sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
313       break;
314     }
315     case TK_ID: {
316       sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
317       break;
318     }
319 #ifndef SQLITE_OMIT_CAST
320     case TK_CAST: {
321       /* Expressions of the form:   CAST(pLeft AS token) */
322       sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
323       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
324       break;
325     }
326 #endif /* SQLITE_OMIT_CAST */
327     case TK_LT:      zBinOp = "LT";     break;
328     case TK_LE:      zBinOp = "LE";     break;
329     case TK_GT:      zBinOp = "GT";     break;
330     case TK_GE:      zBinOp = "GE";     break;
331     case TK_NE:      zBinOp = "NE";     break;
332     case TK_EQ:      zBinOp = "EQ";     break;
333     case TK_IS:      zBinOp = "IS";     break;
334     case TK_ISNOT:   zBinOp = "ISNOT";  break;
335     case TK_AND:     zBinOp = "AND";    break;
336     case TK_OR:      zBinOp = "OR";     break;
337     case TK_PLUS:    zBinOp = "ADD";    break;
338     case TK_STAR:    zBinOp = "MUL";    break;
339     case TK_MINUS:   zBinOp = "SUB";    break;
340     case TK_REM:     zBinOp = "REM";    break;
341     case TK_BITAND:  zBinOp = "BITAND"; break;
342     case TK_BITOR:   zBinOp = "BITOR";  break;
343     case TK_SLASH:   zBinOp = "DIV";    break;
344     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
345     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
346     case TK_CONCAT:  zBinOp = "CONCAT"; break;
347     case TK_DOT:     zBinOp = "DOT";    break;
348 
349     case TK_UMINUS:  zUniOp = "UMINUS"; break;
350     case TK_UPLUS:   zUniOp = "UPLUS";  break;
351     case TK_BITNOT:  zUniOp = "BITNOT"; break;
352     case TK_NOT:     zUniOp = "NOT";    break;
353     case TK_ISNULL:  zUniOp = "ISNULL"; break;
354     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
355 
356     case TK_TRUTH: {
357       int x;
358       const char *azOp[] = {
359          "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
360       };
361       assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
362       assert( pExpr->pRight );
363       assert( pExpr->pRight->op==TK_TRUEFALSE );
364       x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
365       zUniOp = azOp[x];
366       break;
367     }
368 
369     case TK_SPAN: {
370       sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
371       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
372       break;
373     }
374 
375     case TK_COLLATE: {
376       sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
377       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
378       break;
379     }
380 
381     case TK_AGG_FUNCTION:
382     case TK_FUNCTION: {
383       ExprList *pFarg;       /* List of function arguments */
384       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
385         pFarg = 0;
386       }else{
387         pFarg = pExpr->x.pList;
388       }
389       if( pExpr->op==TK_AGG_FUNCTION ){
390         sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
391                              pExpr->op2, pExpr->u.zToken);
392       }else{
393         sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
394       }
395       if( pFarg ){
396         sqlite3TreeViewExprList(pView, pFarg, 0, 0);
397       }
398       break;
399     }
400 #ifndef SQLITE_OMIT_SUBQUERY
401     case TK_EXISTS: {
402       sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
403       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
404       break;
405     }
406     case TK_SELECT: {
407       sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
408       sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
409       break;
410     }
411     case TK_IN: {
412       sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
413       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
414       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
415         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
416       }else{
417         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
418       }
419       break;
420     }
421 #endif /* SQLITE_OMIT_SUBQUERY */
422 
423     /*
424     **    x BETWEEN y AND z
425     **
426     ** This is equivalent to
427     **
428     **    x>=y AND x<=z
429     **
430     ** X is stored in pExpr->pLeft.
431     ** Y is stored in pExpr->pList->a[0].pExpr.
432     ** Z is stored in pExpr->pList->a[1].pExpr.
433     */
434     case TK_BETWEEN: {
435       Expr *pX = pExpr->pLeft;
436       Expr *pY = pExpr->x.pList->a[0].pExpr;
437       Expr *pZ = pExpr->x.pList->a[1].pExpr;
438       sqlite3TreeViewLine(pView, "BETWEEN");
439       sqlite3TreeViewExpr(pView, pX, 1);
440       sqlite3TreeViewExpr(pView, pY, 1);
441       sqlite3TreeViewExpr(pView, pZ, 0);
442       break;
443     }
444     case TK_TRIGGER: {
445       /* If the opcode is TK_TRIGGER, then the expression is a reference
446       ** to a column in the new.* or old.* pseudo-tables available to
447       ** trigger programs. In this case Expr.iTable is set to 1 for the
448       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
449       ** is set to the column of the pseudo-table to read, or to -1 to
450       ** read the rowid field.
451       */
452       sqlite3TreeViewLine(pView, "%s(%d)",
453           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
454       break;
455     }
456     case TK_CASE: {
457       sqlite3TreeViewLine(pView, "CASE");
458       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
459       sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
460       break;
461     }
462 #ifndef SQLITE_OMIT_TRIGGER
463     case TK_RAISE: {
464       const char *zType = "unk";
465       switch( pExpr->affinity ){
466         case OE_Rollback:   zType = "rollback";  break;
467         case OE_Abort:      zType = "abort";     break;
468         case OE_Fail:       zType = "fail";      break;
469         case OE_Ignore:     zType = "ignore";    break;
470       }
471       sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
472       break;
473     }
474 #endif
475     case TK_MATCH: {
476       sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
477                           pExpr->iTable, pExpr->iColumn, zFlgs);
478       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
479       break;
480     }
481     case TK_VECTOR: {
482       sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
483       break;
484     }
485     case TK_SELECT_COLUMN: {
486       sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
487       sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
488       break;
489     }
490     case TK_IF_NULL_ROW: {
491       sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
492       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
493       break;
494     }
495     default: {
496       sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
497       break;
498     }
499   }
500   if( zBinOp ){
501     sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
502     sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
503     sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
504   }else if( zUniOp ){
505     sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
506     sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
507   }
508   sqlite3TreeViewPop(pView);
509 }
510 
511 
512 /*
513 ** Generate a human-readable explanation of an expression list.
514 */
515 void sqlite3TreeViewBareExprList(
516   TreeView *pView,
517   const ExprList *pList,
518   const char *zLabel
519 ){
520   if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
521   if( pList==0 ){
522     sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
523   }else{
524     int i;
525     sqlite3TreeViewLine(pView, "%s", zLabel);
526     for(i=0; i<pList->nExpr; i++){
527       int j = pList->a[i].u.x.iOrderByCol;
528       char *zName = pList->a[i].zName;
529       if( j || zName ){
530         sqlite3TreeViewPush(pView, 0);
531       }
532       if( zName ){
533         sqlite3TreeViewLine(pView, "AS %s", zName);
534       }
535       if( j ){
536         sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
537       }
538       sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
539       if( j || zName ){
540         sqlite3TreeViewPop(pView);
541       }
542     }
543   }
544 }
545 void sqlite3TreeViewExprList(
546   TreeView *pView,
547   const ExprList *pList,
548   u8 moreToFollow,
549   const char *zLabel
550 ){
551   pView = sqlite3TreeViewPush(pView, moreToFollow);
552   sqlite3TreeViewBareExprList(pView, pList, zLabel);
553   sqlite3TreeViewPop(pView);
554 }
555 
556 #endif /* SQLITE_DEBUG */
557