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