blob: 907159c06dfae4a8a159559cac7c18b214e38908 [file] [log] [blame]
drh38b41492015-06-08 15:08:15 +00001/*
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*/
27static 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*/
43static 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*/
53static 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);
drh5f4a6862016-01-30 12:50:25 +000066 sqlite3VXPrintf(&acc, zFormat, ap);
drh38b41492015-06-08 15:08:15 +000067 va_end(ap);
68 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
69 sqlite3StrAccumFinish(&acc);
70 fprintf(stdout,"%s", zBuf);
71 fflush(stdout);
72}
73
74/*
75** Shorthand for starting a new tree item that consists of a single label
76*/
77static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
78 p = sqlite3TreeViewPush(p, moreFollows);
79 sqlite3TreeViewLine(p, "%s", zLabel);
80}
81
drh2476a6f2015-11-07 15:19:59 +000082/*
83** Generate a human-readable description of a WITH clause.
84*/
85void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
86 int i;
87 if( pWith==0 ) return;
88 if( pWith->nCte==0 ) return;
89 if( pWith->pOuter ){
90 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
91 }else{
92 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
93 }
94 if( pWith->nCte>0 ){
95 pView = sqlite3TreeViewPush(pView, 1);
96 for(i=0; i<pWith->nCte; i++){
97 StrAccum x;
98 char zLine[1000];
99 const struct Cte *pCte = &pWith->a[i];
100 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
drh5f4a6862016-01-30 12:50:25 +0000101 sqlite3XPrintf(&x, "%s", pCte->zName);
drh2476a6f2015-11-07 15:19:59 +0000102 if( pCte->pCols && pCte->pCols->nExpr>0 ){
103 char cSep = '(';
104 int j;
105 for(j=0; j<pCte->pCols->nExpr; j++){
drh5f4a6862016-01-30 12:50:25 +0000106 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
drh2476a6f2015-11-07 15:19:59 +0000107 cSep = ',';
108 }
drh5f4a6862016-01-30 12:50:25 +0000109 sqlite3XPrintf(&x, ")");
drh2476a6f2015-11-07 15:19:59 +0000110 }
drh5f4a6862016-01-30 12:50:25 +0000111 sqlite3XPrintf(&x, " AS");
drh2476a6f2015-11-07 15:19:59 +0000112 sqlite3StrAccumFinish(&x);
113 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
114 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
115 sqlite3TreeViewPop(pView);
116 }
117 sqlite3TreeViewPop(pView);
118 }
119}
120
drh38b41492015-06-08 15:08:15 +0000121
122/*
123** Generate a human-readable description of a the Select object.
124*/
125void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
126 int n = 0;
drh1c4505d2015-08-26 11:34:31 +0000127 int cnt = 0;
drh38b41492015-06-08 15:08:15 +0000128 pView = sqlite3TreeViewPush(pView, moreToFollow);
drh2476a6f2015-11-07 15:19:59 +0000129 if( p->pWith ){
130 sqlite3TreeViewWith(pView, p->pWith, 1);
131 cnt = 1;
132 sqlite3TreeViewPush(pView, 1);
133 }
drh1c4505d2015-08-26 11:34:31 +0000134 do{
drhc3489bb2016-02-25 16:04:59 +0000135 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
drh1c4505d2015-08-26 11:34:31 +0000136 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
drhc3489bb2016-02-25 16:04:59 +0000137 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
138 (int)p->nSelectRow
drh1c4505d2015-08-26 11:34:31 +0000139 );
140 if( cnt++ ) sqlite3TreeViewPop(pView);
141 if( p->pPrior ){
142 n = 1000;
143 }else{
144 n = 0;
145 if( p->pSrc && p->pSrc->nSrc ) n++;
146 if( p->pWhere ) n++;
147 if( p->pGroupBy ) n++;
148 if( p->pHaving ) n++;
149 if( p->pOrderBy ) n++;
150 if( p->pLimit ) n++;
151 if( p->pOffset ) n++;
152 }
153 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
154 if( p->pSrc && p->pSrc->nSrc ){
155 int i;
156 pView = sqlite3TreeViewPush(pView, (n--)>0);
157 sqlite3TreeViewLine(pView, "FROM");
158 for(i=0; i<p->pSrc->nSrc; i++){
159 struct SrcList_item *pItem = &p->pSrc->a[i];
160 StrAccum x;
161 char zLine[100];
162 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
drh5f4a6862016-01-30 12:50:25 +0000163 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
drh1c4505d2015-08-26 11:34:31 +0000164 if( pItem->zDatabase ){
drh5f4a6862016-01-30 12:50:25 +0000165 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
drh1c4505d2015-08-26 11:34:31 +0000166 }else if( pItem->zName ){
drh5f4a6862016-01-30 12:50:25 +0000167 sqlite3XPrintf(&x, " %s", pItem->zName);
drh1c4505d2015-08-26 11:34:31 +0000168 }
169 if( pItem->pTab ){
drh5f4a6862016-01-30 12:50:25 +0000170 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
drh1c4505d2015-08-26 11:34:31 +0000171 }
172 if( pItem->zAlias ){
drh5f4a6862016-01-30 12:50:25 +0000173 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
drh1c4505d2015-08-26 11:34:31 +0000174 }
175 if( pItem->fg.jointype & JT_LEFT ){
drh5f4a6862016-01-30 12:50:25 +0000176 sqlite3XPrintf(&x, " LEFT-JOIN");
drh1c4505d2015-08-26 11:34:31 +0000177 }
178 sqlite3StrAccumFinish(&x);
179 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
180 if( pItem->pSelect ){
181 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
182 }
183 if( pItem->fg.isTabFunc ){
184 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
185 }
186 sqlite3TreeViewPop(pView);
drh01d230c2015-08-19 17:11:37 +0000187 }
drh38b41492015-06-08 15:08:15 +0000188 sqlite3TreeViewPop(pView);
189 }
drh1c4505d2015-08-26 11:34:31 +0000190 if( p->pWhere ){
191 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
192 sqlite3TreeViewExpr(pView, p->pWhere, 0);
193 sqlite3TreeViewPop(pView);
drh38b41492015-06-08 15:08:15 +0000194 }
drh1c4505d2015-08-26 11:34:31 +0000195 if( p->pGroupBy ){
196 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
197 }
198 if( p->pHaving ){
199 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
200 sqlite3TreeViewExpr(pView, p->pHaving, 0);
201 sqlite3TreeViewPop(pView);
202 }
203 if( p->pOrderBy ){
204 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
205 }
206 if( p->pLimit ){
207 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
208 sqlite3TreeViewExpr(pView, p->pLimit, 0);
209 sqlite3TreeViewPop(pView);
210 }
211 if( p->pOffset ){
212 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
213 sqlite3TreeViewExpr(pView, p->pOffset, 0);
214 sqlite3TreeViewPop(pView);
215 }
216 if( p->pPrior ){
217 const char *zOp = "UNION";
218 switch( p->op ){
219 case TK_ALL: zOp = "UNION ALL"; break;
220 case TK_INTERSECT: zOp = "INTERSECT"; break;
221 case TK_EXCEPT: zOp = "EXCEPT"; break;
222 }
223 sqlite3TreeViewItem(pView, zOp, 1);
224 }
225 p = p->pPrior;
226 }while( p!=0 );
drh38b41492015-06-08 15:08:15 +0000227 sqlite3TreeViewPop(pView);
228}
229
230/*
231** Generate a human-readable explanation of an expression tree.
232*/
233void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
234 const char *zBinOp = 0; /* Binary operator */
235 const char *zUniOp = 0; /* Unary operator */
drhb3d903e2015-06-18 14:09:13 +0000236 char zFlgs[30];
drh38b41492015-06-08 15:08:15 +0000237 pView = sqlite3TreeViewPush(pView, moreToFollow);
238 if( pExpr==0 ){
239 sqlite3TreeViewLine(pView, "nil");
240 sqlite3TreeViewPop(pView);
241 return;
242 }
drhb3d903e2015-06-18 14:09:13 +0000243 if( pExpr->flags ){
244 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
245 }else{
246 zFlgs[0] = 0;
247 }
drh38b41492015-06-08 15:08:15 +0000248 switch( pExpr->op ){
249 case TK_AGG_COLUMN: {
drhb3d903e2015-06-18 14:09:13 +0000250 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
251 pExpr->iTable, pExpr->iColumn, zFlgs);
drh38b41492015-06-08 15:08:15 +0000252 break;
253 }
254 case TK_COLUMN: {
255 if( pExpr->iTable<0 ){
256 /* This only happens when coding check constraints */
drhb3d903e2015-06-18 14:09:13 +0000257 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
drh38b41492015-06-08 15:08:15 +0000258 }else{
drhb3d903e2015-06-18 14:09:13 +0000259 sqlite3TreeViewLine(pView, "{%d:%d}%s",
260 pExpr->iTable, pExpr->iColumn, zFlgs);
drh38b41492015-06-08 15:08:15 +0000261 }
262 break;
263 }
264 case TK_INTEGER: {
265 if( pExpr->flags & EP_IntValue ){
266 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
267 }else{
268 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
269 }
270 break;
271 }
272#ifndef SQLITE_OMIT_FLOATING_POINT
273 case TK_FLOAT: {
274 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
275 break;
276 }
277#endif
278 case TK_STRING: {
279 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
280 break;
281 }
282 case TK_NULL: {
283 sqlite3TreeViewLine(pView,"NULL");
284 break;
285 }
286#ifndef SQLITE_OMIT_BLOB_LITERAL
287 case TK_BLOB: {
288 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
289 break;
290 }
291#endif
292 case TK_VARIABLE: {
293 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
294 pExpr->u.zToken, pExpr->iColumn);
295 break;
296 }
297 case TK_REGISTER: {
298 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
299 break;
300 }
drh38b41492015-06-08 15:08:15 +0000301 case TK_ID: {
302 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
303 break;
304 }
305#ifndef SQLITE_OMIT_CAST
306 case TK_CAST: {
307 /* Expressions of the form: CAST(pLeft AS token) */
308 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
309 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
310 break;
311 }
312#endif /* SQLITE_OMIT_CAST */
313 case TK_LT: zBinOp = "LT"; break;
314 case TK_LE: zBinOp = "LE"; break;
315 case TK_GT: zBinOp = "GT"; break;
316 case TK_GE: zBinOp = "GE"; break;
317 case TK_NE: zBinOp = "NE"; break;
318 case TK_EQ: zBinOp = "EQ"; break;
319 case TK_IS: zBinOp = "IS"; break;
320 case TK_ISNOT: zBinOp = "ISNOT"; break;
321 case TK_AND: zBinOp = "AND"; break;
322 case TK_OR: zBinOp = "OR"; break;
323 case TK_PLUS: zBinOp = "ADD"; break;
324 case TK_STAR: zBinOp = "MUL"; break;
325 case TK_MINUS: zBinOp = "SUB"; break;
326 case TK_REM: zBinOp = "REM"; break;
327 case TK_BITAND: zBinOp = "BITAND"; break;
328 case TK_BITOR: zBinOp = "BITOR"; break;
329 case TK_SLASH: zBinOp = "DIV"; break;
330 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
331 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
332 case TK_CONCAT: zBinOp = "CONCAT"; break;
333 case TK_DOT: zBinOp = "DOT"; break;
334
335 case TK_UMINUS: zUniOp = "UMINUS"; break;
336 case TK_UPLUS: zUniOp = "UPLUS"; break;
337 case TK_BITNOT: zUniOp = "BITNOT"; break;
338 case TK_NOT: zUniOp = "NOT"; break;
339 case TK_ISNULL: zUniOp = "ISNULL"; break;
340 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
341
drh94fa9c42016-02-27 21:16:04 +0000342 case TK_SPAN: {
343 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
344 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
345 break;
346 }
347
drh38b41492015-06-08 15:08:15 +0000348 case TK_COLLATE: {
349 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
350 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
351 break;
352 }
353
354 case TK_AGG_FUNCTION:
355 case TK_FUNCTION: {
356 ExprList *pFarg; /* List of function arguments */
357 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
358 pFarg = 0;
359 }else{
360 pFarg = pExpr->x.pList;
361 }
362 if( pExpr->op==TK_AGG_FUNCTION ){
363 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
364 pExpr->op2, pExpr->u.zToken);
365 }else{
366 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
367 }
368 if( pFarg ){
369 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
370 }
371 break;
372 }
373#ifndef SQLITE_OMIT_SUBQUERY
374 case TK_EXISTS: {
375 sqlite3TreeViewLine(pView, "EXISTS-expr");
376 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
377 break;
378 }
379 case TK_SELECT: {
380 sqlite3TreeViewLine(pView, "SELECT-expr");
381 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
382 break;
383 }
384 case TK_IN: {
385 sqlite3TreeViewLine(pView, "IN");
386 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
387 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
388 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
389 }else{
390 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
391 }
392 break;
393 }
394#endif /* SQLITE_OMIT_SUBQUERY */
395
396 /*
397 ** x BETWEEN y AND z
398 **
399 ** This is equivalent to
400 **
401 ** x>=y AND x<=z
402 **
403 ** X is stored in pExpr->pLeft.
404 ** Y is stored in pExpr->pList->a[0].pExpr.
405 ** Z is stored in pExpr->pList->a[1].pExpr.
406 */
407 case TK_BETWEEN: {
408 Expr *pX = pExpr->pLeft;
409 Expr *pY = pExpr->x.pList->a[0].pExpr;
410 Expr *pZ = pExpr->x.pList->a[1].pExpr;
411 sqlite3TreeViewLine(pView, "BETWEEN");
412 sqlite3TreeViewExpr(pView, pX, 1);
413 sqlite3TreeViewExpr(pView, pY, 1);
414 sqlite3TreeViewExpr(pView, pZ, 0);
415 break;
416 }
417 case TK_TRIGGER: {
418 /* If the opcode is TK_TRIGGER, then the expression is a reference
419 ** to a column in the new.* or old.* pseudo-tables available to
420 ** trigger programs. In this case Expr.iTable is set to 1 for the
421 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
422 ** is set to the column of the pseudo-table to read, or to -1 to
423 ** read the rowid field.
424 */
425 sqlite3TreeViewLine(pView, "%s(%d)",
426 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
427 break;
428 }
429 case TK_CASE: {
430 sqlite3TreeViewLine(pView, "CASE");
431 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
432 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
433 break;
434 }
435#ifndef SQLITE_OMIT_TRIGGER
436 case TK_RAISE: {
437 const char *zType = "unk";
438 switch( pExpr->affinity ){
439 case OE_Rollback: zType = "rollback"; break;
440 case OE_Abort: zType = "abort"; break;
441 case OE_Fail: zType = "fail"; break;
442 case OE_Ignore: zType = "ignore"; break;
443 }
444 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
445 break;
446 }
447#endif
448 default: {
449 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
450 break;
451 }
452 }
453 if( zBinOp ){
drhb3d903e2015-06-18 14:09:13 +0000454 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
drh38b41492015-06-08 15:08:15 +0000455 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
456 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
457 }else if( zUniOp ){
drhb3d903e2015-06-18 14:09:13 +0000458 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
drh38b41492015-06-08 15:08:15 +0000459 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
460 }
461 sqlite3TreeViewPop(pView);
462}
463
464/*
465** Generate a human-readable explanation of an expression list.
466*/
467void sqlite3TreeViewExprList(
468 TreeView *pView,
469 const ExprList *pList,
470 u8 moreToFollow,
471 const char *zLabel
472){
473 int i;
474 pView = sqlite3TreeViewPush(pView, moreToFollow);
475 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
476 if( pList==0 ){
477 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
478 }else{
479 sqlite3TreeViewLine(pView, "%s", zLabel);
480 for(i=0; i<pList->nExpr; i++){
drh5579d592015-08-26 14:01:41 +0000481 int j = pList->a[i].u.x.iOrderByCol;
482 if( j ){
483 sqlite3TreeViewPush(pView, 0);
484 sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
485 }
drh38b41492015-06-08 15:08:15 +0000486 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
drh5579d592015-08-26 14:01:41 +0000487 if( j ) sqlite3TreeViewPop(pView);
drh38b41492015-06-08 15:08:15 +0000488 }
489 }
490 sqlite3TreeViewPop(pView);
491}
492
493#endif /* SQLITE_DEBUG */