blob: 222d4b0987844da32993d500d439bac52b399031 [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++){
drh0cdbe1a2018-05-09 13:46:26 +000061 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
drh38b41492015-06-08 15:08:15 +000062 }
drh0cdbe1a2018-05-09 13:46:26 +000063 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
drh38b41492015-06-08 15:08:15 +000064 }
drhfbe07532018-04-23 20:04:38 +000065 if( zFormat!=0 ){
66 va_start(ap, zFormat);
drh0cdbe1a2018-05-09 13:46:26 +000067 sqlite3_str_vappendf(&acc, zFormat, ap);
drhfbe07532018-04-23 20:04:38 +000068 va_end(ap);
drh10c0e712019-04-25 18:15:38 +000069 assert( acc.nChar>0 || acc.accError );
drh0cdbe1a2018-05-09 13:46:26 +000070 sqlite3_str_append(&acc, "\n", 1);
drhfbe07532018-04-23 20:04:38 +000071 }
drh38b41492015-06-08 15:08:15 +000072 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*/
80static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
81 p = sqlite3TreeViewPush(p, moreFollows);
82 sqlite3TreeViewLine(p, "%s", zLabel);
83}
84
drh2476a6f2015-11-07 15:19:59 +000085/*
86** Generate a human-readable description of a WITH clause.
87*/
88void 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);
drh0cdbe1a2018-05-09 13:46:26 +0000104 sqlite3_str_appendf(&x, "%s", pCte->zName);
drh2476a6f2015-11-07 15:19:59 +0000105 if( pCte->pCols && pCte->pCols->nExpr>0 ){
106 char cSep = '(';
107 int j;
108 for(j=0; j<pCte->pCols->nExpr; j++){
drh41cee662019-12-12 20:22:34 +0000109 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
drh2476a6f2015-11-07 15:19:59 +0000110 cSep = ',';
111 }
drh0cdbe1a2018-05-09 13:46:26 +0000112 sqlite3_str_appendf(&x, ")");
drh2476a6f2015-11-07 15:19:59 +0000113 }
drha79e2a22021-02-21 23:44:14 +0000114 if( pCte->pUse ){
115 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
116 pCte->pUse->nUse);
117 }
drh2476a6f2015-11-07 15:19:59 +0000118 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
drh145d0a32018-11-08 22:53:06 +0000127/*
128** Generate a human-readable description of a SrcList object.
129*/
130void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
131 int i;
132 for(i=0; i<pSrc->nSrc; i++){
drh76012942021-02-21 21:04:54 +0000133 const SrcItem *pItem = &pSrc->a[i];
drh145d0a32018-11-08 22:53:06 +0000134 StrAccum x;
135 char zLine[100];
136 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
drhff374912021-04-12 12:58:55 +0000137 x.printfFlags |= SQLITE_PRINTF_INTERNAL;
drh6610e6a2021-03-19 19:44:56 +0000138 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
drh145d0a32018-11-08 22:53:06 +0000139 if( pItem->pTab ){
drhf7f6dbf2020-03-21 22:03:32 +0000140 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
141 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
drh145d0a32018-11-08 22:53:06 +0000142 }
drh145d0a32018-11-08 22:53:06 +0000143 if( pItem->fg.jointype & JT_LEFT ){
144 sqlite3_str_appendf(&x, " LEFT-JOIN");
drh9b9f2352021-06-23 11:39:00 +0000145 }else if( pItem->fg.jointype & JT_CROSS ){
146 sqlite3_str_appendf(&x, " CROSS-JOIN");
drh145d0a32018-11-08 22:53:06 +0000147 }
drhb7e51992020-01-08 14:39:57 +0000148 if( pItem->fg.fromDDL ){
149 sqlite3_str_appendf(&x, " DDL");
150 }
drha79e2a22021-02-21 23:44:14 +0000151 if( pItem->fg.isCte ){
152 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
153 }
drh145d0a32018-11-08 22:53:06 +0000154 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}
drh38b41492015-06-08 15:08:15 +0000165
166/*
drh2e5c5052016-08-27 20:21:51 +0000167** Generate a human-readable description of a Select object.
drh38b41492015-06-08 15:08:15 +0000168*/
169void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
170 int n = 0;
drh1c4505d2015-08-26 11:34:31 +0000171 int cnt = 0;
drh510b7ff2017-03-13 17:37:13 +0000172 if( p==0 ){
173 sqlite3TreeViewLine(pView, "nil-SELECT");
174 return;
175 }
drh38b41492015-06-08 15:08:15 +0000176 pView = sqlite3TreeViewPush(pView, moreToFollow);
drh2476a6f2015-11-07 15:19:59 +0000177 if( p->pWith ){
178 sqlite3TreeViewWith(pView, p->pWith, 1);
179 cnt = 1;
180 sqlite3TreeViewPush(pView, 1);
181 }
drh1c4505d2015-08-26 11:34:31 +0000182 do{
drh55b4c822019-08-03 16:17:46 +0000183 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 }
drh1c4505d2015-08-26 11:34:31 +0000194 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++;
drha1fd4b52018-07-10 06:32:53 +0000205#ifndef SQLITE_OMIT_WINDOWFUNC
206 if( p->pWin ) n++;
207 if( p->pWinDefn ) n++;
208#endif
drh1c4505d2015-08-26 11:34:31 +0000209 }
drh55b4c822019-08-03 16:17:46 +0000210 if( p->pEList ){
211 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
212 }
213 n--;
drha1fd4b52018-07-10 06:32:53 +0000214#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
drh1c4505d2015-08-26 11:34:31 +0000225 if( p->pSrc && p->pSrc->nSrc ){
drh1c4505d2015-08-26 11:34:31 +0000226 pView = sqlite3TreeViewPush(pView, (n--)>0);
227 sqlite3TreeViewLine(pView, "FROM");
drh145d0a32018-11-08 22:53:06 +0000228 sqlite3TreeViewSrcList(pView, p->pSrc);
drh38b41492015-06-08 15:08:15 +0000229 sqlite3TreeViewPop(pView);
230 }
drh1c4505d2015-08-26 11:34:31 +0000231 if( p->pWhere ){
232 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
233 sqlite3TreeViewExpr(pView, p->pWhere, 0);
234 sqlite3TreeViewPop(pView);
drh38b41492015-06-08 15:08:15 +0000235 }
drh1c4505d2015-08-26 11:34:31 +0000236 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 }
drha1fd4b52018-07-10 06:32:53 +0000244#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
drh1c4505d2015-08-26 11:34:31 +0000254 if( p->pOrderBy ){
255 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
256 }
257 if( p->pLimit ){
258 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
drh8c0833f2017-11-14 23:48:23 +0000259 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 }
drh1c4505d2015-08-26 11:34:31 +0000265 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 );
drh38b41492015-06-08 15:08:15 +0000278 sqlite3TreeViewPop(pView);
279}
280
drha1fd4b52018-07-10 06:32:53 +0000281#ifndef SQLITE_OMIT_WINDOWFUNC
282/*
283** Generate a description of starting or stopping bounds
284*/
285void 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*/
322void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
drhfc15f4c2019-03-28 13:03:41 +0000323 int nElement = 0;
drh0dc0e9c2019-03-28 13:35:28 +0000324 if( pWin->pFilter ){
325 sqlite3TreeViewItem(pView, "FILTER", 1);
326 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
327 sqlite3TreeViewPop(pView);
328 }
drha1fd4b52018-07-10 06:32:53 +0000329 pView = sqlite3TreeViewPush(pView, more);
330 if( pWin->zName ){
drh6f1644c2019-03-28 13:53:12 +0000331 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
drha1fd4b52018-07-10 06:32:53 +0000332 }else{
drh6f1644c2019-03-28 13:53:12 +0000333 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
drha1fd4b52018-07-10 06:32:53 +0000334 }
drhfc15f4c2019-03-28 13:03:41 +0000335 if( pWin->zBase ) nElement++;
336 if( pWin->pOrderBy ) nElement++;
337 if( pWin->eFrmType ) nElement++;
338 if( pWin->eExclude ) nElement++;
drhfc15f4c2019-03-28 13:03:41 +0000339 if( pWin->zBase ){
340 sqlite3TreeViewPush(pView, (--nElement)>0);
341 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
342 sqlite3TreeViewPop(pView);
343 }
drha1fd4b52018-07-10 06:32:53 +0000344 if( pWin->pPartition ){
drhfc15f4c2019-03-28 13:03:41 +0000345 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
drha1fd4b52018-07-10 06:32:53 +0000346 }
347 if( pWin->pOrderBy ){
drhfc15f4c2019-03-28 13:03:41 +0000348 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
drha1fd4b52018-07-10 06:32:53 +0000349 }
drhfc15f4c2019-03-28 13:03:41 +0000350 if( pWin->eFrmType ){
drh0dc0e9c2019-03-28 13:35:28 +0000351 char zBuf[30];
drhfc15f4c2019-03-28 13:03:41 +0000352 const char *zFrmType = "ROWS";
353 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
354 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
drh0dc0e9c2019-03-28 13:35:28 +0000355 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
356 pWin->bImplicitFrame ? " (implied)" : "");
357 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
drha1fd4b52018-07-10 06:32:53 +0000358 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
359 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
360 sqlite3TreeViewPop(pView);
361 }
drhfc15f4c2019-03-28 13:03:41 +0000362 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 }
drha1fd4b52018-07-10 06:32:53 +0000379 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*/
387void 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
drh38b41492015-06-08 15:08:15 +0000396/*
397** Generate a human-readable explanation of an expression tree.
398*/
399void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
400 const char *zBinOp = 0; /* Binary operator */
401 const char *zUniOp = 0; /* Unary operator */
drhe7375bf2020-03-10 19:24:38 +0000402 char zFlgs[200];
drh38b41492015-06-08 15:08:15 +0000403 pView = sqlite3TreeViewPush(pView, moreToFollow);
404 if( pExpr==0 ){
405 sqlite3TreeViewLine(pView, "nil");
406 sqlite3TreeViewPop(pView);
407 return;
408 }
drhe7375bf2020-03-10 19:24:38 +0000409 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
drhb7e51992020-01-08 14:39:57 +0000410 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');
drhd97cda42017-04-14 14:02:14 +0000414 if( ExprHasProperty(pExpr, EP_FromJoin) ){
drhb7e51992020-01-08 14:39:57 +0000415 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
drhd97cda42017-04-14 14:02:14 +0000416 }
drhb7e51992020-01-08 14:39:57 +0000417 if( ExprHasProperty(pExpr, EP_FromDDL) ){
418 sqlite3_str_appendf(&x, " DDL");
419 }
drhe7375bf2020-03-10 19:24:38 +0000420 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
421 sqlite3_str_appendf(&x, " IMMUTABLE");
422 }
drhb7e51992020-01-08 14:39:57 +0000423 sqlite3StrAccumFinish(&x);
drhb3d903e2015-06-18 14:09:13 +0000424 }else{
425 zFlgs[0] = 0;
426 }
drh38b41492015-06-08 15:08:15 +0000427 switch( pExpr->op ){
428 case TK_AGG_COLUMN: {
drhb3d903e2015-06-18 14:09:13 +0000429 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
430 pExpr->iTable, pExpr->iColumn, zFlgs);
drh38b41492015-06-08 15:08:15 +0000431 break;
432 }
433 case TK_COLUMN: {
434 if( pExpr->iTable<0 ){
435 /* This only happens when coding check constraints */
drhd4933532019-10-31 12:30:38 +0000436 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);
drh38b41492015-06-08 15:08:15 +0000444 }else{
drha513e592019-12-20 20:08:56 +0000445 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
446 pExpr->iTable, pExpr->iColumn,
447 pExpr->y.pTab, zFlgs);
drh38b41492015-06-08 15:08:15 +0000448 }
drhefad2e22018-07-27 16:57:11 +0000449 if( ExprHasProperty(pExpr, EP_FixedCol) ){
450 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
451 }
drh38b41492015-06-08 15:08:15 +0000452 break;
453 }
454 case TK_INTEGER: {
455 if( pExpr->flags & EP_IntValue ){
456 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
457 }else{
458 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
459 }
460 break;
461 }
462#ifndef SQLITE_OMIT_FLOATING_POINT
463 case TK_FLOAT: {
464 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
465 break;
466 }
467#endif
468 case TK_STRING: {
469 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
470 break;
471 }
472 case TK_NULL: {
473 sqlite3TreeViewLine(pView,"NULL");
474 break;
475 }
drh34328212018-02-26 19:03:25 +0000476 case TK_TRUEFALSE: {
drh43c4ac82018-02-26 21:26:27 +0000477 sqlite3TreeViewLine(pView,
drh96acafb2018-02-27 14:49:25 +0000478 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
drh34328212018-02-26 19:03:25 +0000479 break;
480 }
drh38b41492015-06-08 15:08:15 +0000481#ifndef SQLITE_OMIT_BLOB_LITERAL
482 case TK_BLOB: {
483 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
484 break;
485 }
486#endif
487 case TK_VARIABLE: {
488 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
489 pExpr->u.zToken, pExpr->iColumn);
490 break;
491 }
492 case TK_REGISTER: {
493 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
494 break;
495 }
drh38b41492015-06-08 15:08:15 +0000496 case TK_ID: {
497 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
498 break;
499 }
500#ifndef SQLITE_OMIT_CAST
501 case TK_CAST: {
502 /* Expressions of the form: CAST(pLeft AS token) */
503 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
504 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
505 break;
506 }
507#endif /* SQLITE_OMIT_CAST */
508 case TK_LT: zBinOp = "LT"; break;
509 case TK_LE: zBinOp = "LE"; break;
510 case TK_GT: zBinOp = "GT"; break;
511 case TK_GE: zBinOp = "GE"; break;
512 case TK_NE: zBinOp = "NE"; break;
513 case TK_EQ: zBinOp = "EQ"; break;
514 case TK_IS: zBinOp = "IS"; break;
515 case TK_ISNOT: zBinOp = "ISNOT"; break;
516 case TK_AND: zBinOp = "AND"; break;
517 case TK_OR: zBinOp = "OR"; break;
518 case TK_PLUS: zBinOp = "ADD"; break;
519 case TK_STAR: zBinOp = "MUL"; break;
520 case TK_MINUS: zBinOp = "SUB"; break;
521 case TK_REM: zBinOp = "REM"; break;
522 case TK_BITAND: zBinOp = "BITAND"; break;
523 case TK_BITOR: zBinOp = "BITOR"; break;
524 case TK_SLASH: zBinOp = "DIV"; break;
525 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
526 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
527 case TK_CONCAT: zBinOp = "CONCAT"; break;
528 case TK_DOT: zBinOp = "DOT"; break;
drhe7375bf2020-03-10 19:24:38 +0000529 case TK_LIMIT: zBinOp = "LIMIT"; break;
drh38b41492015-06-08 15:08:15 +0000530
531 case TK_UMINUS: zUniOp = "UMINUS"; break;
532 case TK_UPLUS: zUniOp = "UPLUS"; break;
533 case TK_BITNOT: zUniOp = "BITNOT"; break;
534 case TK_NOT: zUniOp = "NOT"; break;
535 case TK_ISNULL: zUniOp = "ISNULL"; break;
536 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
537
drh34328212018-02-26 19:03:25 +0000538 case TK_TRUTH: {
drh43c4ac82018-02-26 21:26:27 +0000539 int x;
540 const char *azOp[] = {
541 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
542 };
drh34328212018-02-26 19:03:25 +0000543 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
544 assert( pExpr->pRight );
dan6ece3532019-06-12 13:49:32 +0000545 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
drh96acafb2018-02-27 14:49:25 +0000546 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
drh43c4ac82018-02-26 21:26:27 +0000547 zUniOp = azOp[x];
drh34328212018-02-26 19:03:25 +0000548 break;
549 }
550
drh94fa9c42016-02-27 21:16:04 +0000551 case TK_SPAN: {
552 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
553 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
554 break;
555 }
556
drh38b41492015-06-08 15:08:15 +0000557 case TK_COLLATE: {
drhc204d812019-09-10 17:51:27 +0000558 /* COLLATE operators without the EP_Collate flag are intended to
drh018dbb12019-09-28 16:14:55 +0000559 ** emulate collation associated with a table column. These show
560 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
561 ** operators that appear in the original SQL always have the
562 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
drhc204d812019-09-10 17:51:27 +0000563 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
564 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
565 pExpr->u.zToken, zFlgs);
drh38b41492015-06-08 15:08:15 +0000566 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
567 break;
568 }
569
570 case TK_AGG_FUNCTION:
571 case TK_FUNCTION: {
572 ExprList *pFarg; /* List of function arguments */
drha1fd4b52018-07-10 06:32:53 +0000573 Window *pWin;
drh38b41492015-06-08 15:08:15 +0000574 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
575 pFarg = 0;
drha1fd4b52018-07-10 06:32:53 +0000576 pWin = 0;
drh38b41492015-06-08 15:08:15 +0000577 }else{
578 pFarg = pExpr->x.pList;
drha1fd4b52018-07-10 06:32:53 +0000579#ifndef SQLITE_OMIT_WINDOWFUNC
drh014fff22020-01-08 22:22:36 +0000580 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
drha1fd4b52018-07-10 06:32:53 +0000581#else
582 pWin = 0;
583#endif
drh38b41492015-06-08 15:08:15 +0000584 }
585 if( pExpr->op==TK_AGG_FUNCTION ){
drhe26d4282020-06-09 11:59:15 +0000586 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
drhca74fbf2020-05-24 02:05:04 +0000587 pExpr->op2, pExpr->u.zToken, zFlgs,
drhe26d4282020-06-09 11:59:15 +0000588 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
drhca74fbf2020-05-24 02:05:04 +0000589 pExpr->iAgg, pExpr->pAggInfo);
drhd4933532019-10-31 12:30:38 +0000590 }else if( pExpr->op2!=0 ){
591 const char *zOp2;
592 char zBuf[8];
593 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
594 zOp2 = zBuf;
595 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
596 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
597 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
598 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
599 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
600 pExpr->u.zToken, zFlgs, zOp2);
drh38b41492015-06-08 15:08:15 +0000601 }else{
drh42d2fce2019-08-15 20:04:09 +0000602 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
drh38b41492015-06-08 15:08:15 +0000603 }
604 if( pFarg ){
drha1fd4b52018-07-10 06:32:53 +0000605 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
drh38b41492015-06-08 15:08:15 +0000606 }
mistachkin14897852018-07-23 18:53:49 +0000607#ifndef SQLITE_OMIT_WINDOWFUNC
drha1fd4b52018-07-10 06:32:53 +0000608 if( pWin ){
609 sqlite3TreeViewWindow(pView, pWin, 0);
610 }
611#endif
drh38b41492015-06-08 15:08:15 +0000612 break;
613 }
614#ifndef SQLITE_OMIT_SUBQUERY
615 case TK_EXISTS: {
drh9f6e14c2017-07-10 13:24:58 +0000616 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000617 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
618 break;
619 }
620 case TK_SELECT: {
drha0365c42020-06-05 04:01:50 +0000621 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000622 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
623 break;
624 }
625 case TK_IN: {
drh9f6e14c2017-07-10 13:24:58 +0000626 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000627 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
628 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
629 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
630 }else{
631 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
632 }
633 break;
634 }
635#endif /* SQLITE_OMIT_SUBQUERY */
636
637 /*
638 ** x BETWEEN y AND z
639 **
640 ** This is equivalent to
641 **
642 ** x>=y AND x<=z
643 **
644 ** X is stored in pExpr->pLeft.
645 ** Y is stored in pExpr->pList->a[0].pExpr.
646 ** Z is stored in pExpr->pList->a[1].pExpr.
647 */
648 case TK_BETWEEN: {
649 Expr *pX = pExpr->pLeft;
650 Expr *pY = pExpr->x.pList->a[0].pExpr;
651 Expr *pZ = pExpr->x.pList->a[1].pExpr;
652 sqlite3TreeViewLine(pView, "BETWEEN");
653 sqlite3TreeViewExpr(pView, pX, 1);
654 sqlite3TreeViewExpr(pView, pY, 1);
655 sqlite3TreeViewExpr(pView, pZ, 0);
656 break;
657 }
658 case TK_TRIGGER: {
659 /* If the opcode is TK_TRIGGER, then the expression is a reference
660 ** to a column in the new.* or old.* pseudo-tables available to
661 ** trigger programs. In this case Expr.iTable is set to 1 for the
662 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
663 ** is set to the column of the pseudo-table to read, or to -1 to
664 ** read the rowid field.
665 */
666 sqlite3TreeViewLine(pView, "%s(%d)",
667 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
668 break;
669 }
670 case TK_CASE: {
671 sqlite3TreeViewLine(pView, "CASE");
672 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
673 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
674 break;
675 }
676#ifndef SQLITE_OMIT_TRIGGER
677 case TK_RAISE: {
678 const char *zType = "unk";
drh11949042019-08-05 18:01:42 +0000679 switch( pExpr->affExpr ){
drh38b41492015-06-08 15:08:15 +0000680 case OE_Rollback: zType = "rollback"; break;
681 case OE_Abort: zType = "abort"; break;
682 case OE_Fail: zType = "fail"; break;
683 case OE_Ignore: zType = "ignore"; break;
684 }
685 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
686 break;
687 }
688#endif
drhc84a4022016-05-27 12:30:20 +0000689 case TK_MATCH: {
690 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
691 pExpr->iTable, pExpr->iColumn, zFlgs);
692 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
693 break;
694 }
drhdb97e562016-08-18 17:55:57 +0000695 case TK_VECTOR: {
drh269d3222019-10-23 18:09:39 +0000696 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
697 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
698 sqlite3_free(z);
drhdb97e562016-08-18 17:55:57 +0000699 break;
700 }
drh48cb3a72016-08-18 18:09:10 +0000701 case TK_SELECT_COLUMN: {
702 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
703 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
704 break;
705 }
drh31d6fd52017-04-14 19:03:10 +0000706 case TK_IF_NULL_ROW: {
707 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
708 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
709 break;
710 }
drhbf7f3a02021-05-24 11:35:16 +0000711 case TK_ERROR: {
712 Expr tmp;
713 sqlite3TreeViewLine(pView, "ERROR");
714 tmp = *pExpr;
715 tmp.op = pExpr->op2;
716 sqlite3TreeViewExpr(pView, &tmp, 0);
717 break;
718 }
drh4a4e02b2021-07-04 22:33:08 +0000719 case TK_ROW: {
720 if( pExpr->iColumn<=0 ){
721 sqlite3TreeViewLine(pView, "First FROM table rowid");
722 }else{
723 sqlite3TreeViewLine(pView, "First FROM table column %d",
724 pExpr->iColumn-1);
725 }
726 break;
727 }
drh38b41492015-06-08 15:08:15 +0000728 default: {
729 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
730 break;
731 }
732 }
733 if( zBinOp ){
drhb3d903e2015-06-18 14:09:13 +0000734 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
drh38b41492015-06-08 15:08:15 +0000735 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
736 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
737 }else if( zUniOp ){
drhb3d903e2015-06-18 14:09:13 +0000738 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
drh11949042019-08-05 18:01:42 +0000739 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
drh38b41492015-06-08 15:08:15 +0000740 }
741 sqlite3TreeViewPop(pView);
742}
743
drhdb97e562016-08-18 17:55:57 +0000744
drh38b41492015-06-08 15:08:15 +0000745/*
746** Generate a human-readable explanation of an expression list.
747*/
drhdb97e562016-08-18 17:55:57 +0000748void sqlite3TreeViewBareExprList(
drh38b41492015-06-08 15:08:15 +0000749 TreeView *pView,
750 const ExprList *pList,
drh38b41492015-06-08 15:08:15 +0000751 const char *zLabel
752){
drh38b41492015-06-08 15:08:15 +0000753 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
754 if( pList==0 ){
755 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
756 }else{
drhdb97e562016-08-18 17:55:57 +0000757 int i;
drh38b41492015-06-08 15:08:15 +0000758 sqlite3TreeViewLine(pView, "%s", zLabel);
759 for(i=0; i<pList->nExpr; i++){
drh5579d592015-08-26 14:01:41 +0000760 int j = pList->a[i].u.x.iOrderByCol;
drh41cee662019-12-12 20:22:34 +0000761 char *zName = pList->a[i].zEName;
drhfbe07532018-04-23 20:04:38 +0000762 int moreToFollow = i<pList->nExpr - 1;
drhe1f49b82020-01-03 00:28:14 +0000763 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
drh5a699a02017-12-22 19:53:02 +0000764 if( j || zName ){
drhfbe07532018-04-23 20:04:38 +0000765 sqlite3TreeViewPush(pView, moreToFollow);
766 moreToFollow = 0;
767 sqlite3TreeViewLine(pView, 0);
768 if( zName ){
769 fprintf(stdout, "AS %s ", zName);
770 }
771 if( j ){
772 fprintf(stdout, "iOrderByCol=%d", j);
773 }
774 fprintf(stdout, "\n");
775 fflush(stdout);
drh5a699a02017-12-22 19:53:02 +0000776 }
drhfbe07532018-04-23 20:04:38 +0000777 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
drh5a699a02017-12-22 19:53:02 +0000778 if( j || zName ){
779 sqlite3TreeViewPop(pView);
780 }
drh38b41492015-06-08 15:08:15 +0000781 }
782 }
drhdb97e562016-08-18 17:55:57 +0000783}
784void sqlite3TreeViewExprList(
785 TreeView *pView,
786 const ExprList *pList,
787 u8 moreToFollow,
788 const char *zLabel
789){
790 pView = sqlite3TreeViewPush(pView, moreToFollow);
791 sqlite3TreeViewBareExprList(pView, pList, zLabel);
drh38b41492015-06-08 15:08:15 +0000792 sqlite3TreeViewPop(pView);
793}
794
795#endif /* SQLITE_DEBUG */