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