blob: 1ea2f24f2fb8f9c3b3e79b54a09725c72547ac9b [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 }
drh0cdbe1a2018-05-09 13:46:26 +0000114 sqlite3_str_appendf(&x, " AS");
drh2476a6f2015-11-07 15:19:59 +0000115 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
drh145d0a32018-11-08 22:53:06 +0000124/*
125** Generate a human-readable description of a SrcList object.
126*/
127void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
128 int i;
129 for(i=0; i<pSrc->nSrc; i++){
130 const struct SrcList_item *pItem = &pSrc->a[i];
131 StrAccum x;
132 char zLine[100];
133 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
drhdbf1c4b2019-11-13 16:50:06 +0000134 sqlite3_str_appendf(&x, "{%d:*}", pItem->iCursor);
drh145d0a32018-11-08 22:53:06 +0000135 if( pItem->zDatabase ){
136 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
137 }else if( pItem->zName ){
138 sqlite3_str_appendf(&x, " %s", pItem->zName);
139 }
140 if( pItem->pTab ){
drh7eb2c912018-12-27 00:30:42 +0000141 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p",
142 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab);
drh145d0a32018-11-08 22:53:06 +0000143 }
144 if( pItem->zAlias ){
145 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
146 }
147 if( pItem->fg.jointype & JT_LEFT ){
148 sqlite3_str_appendf(&x, " LEFT-JOIN");
149 }
drhb7e51992020-01-08 14:39:57 +0000150 if( pItem->fg.fromDDL ){
151 sqlite3_str_appendf(&x, " DDL");
152 }
drh145d0a32018-11-08 22:53:06 +0000153 sqlite3StrAccumFinish(&x);
154 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
155 if( pItem->pSelect ){
156 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
157 }
158 if( pItem->fg.isTabFunc ){
159 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
160 }
161 sqlite3TreeViewPop(pView);
162 }
163}
drh38b41492015-06-08 15:08:15 +0000164
165/*
drh2e5c5052016-08-27 20:21:51 +0000166** Generate a human-readable description of a Select object.
drh38b41492015-06-08 15:08:15 +0000167*/
168void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
169 int n = 0;
drh1c4505d2015-08-26 11:34:31 +0000170 int cnt = 0;
drh510b7ff2017-03-13 17:37:13 +0000171 if( p==0 ){
172 sqlite3TreeViewLine(pView, "nil-SELECT");
173 return;
174 }
drh38b41492015-06-08 15:08:15 +0000175 pView = sqlite3TreeViewPush(pView, moreToFollow);
drh2476a6f2015-11-07 15:19:59 +0000176 if( p->pWith ){
177 sqlite3TreeViewWith(pView, p->pWith, 1);
178 cnt = 1;
179 sqlite3TreeViewPush(pView, 1);
180 }
drh1c4505d2015-08-26 11:34:31 +0000181 do{
drh55b4c822019-08-03 16:17:46 +0000182 if( p->selFlags & SF_WhereBegin ){
183 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
184 }else{
185 sqlite3TreeViewLine(pView,
186 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
187 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
188 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
189 p->selId, p, p->selFlags,
190 (int)p->nSelectRow
191 );
192 }
drh1c4505d2015-08-26 11:34:31 +0000193 if( cnt++ ) sqlite3TreeViewPop(pView);
194 if( p->pPrior ){
195 n = 1000;
196 }else{
197 n = 0;
198 if( p->pSrc && p->pSrc->nSrc ) n++;
199 if( p->pWhere ) n++;
200 if( p->pGroupBy ) n++;
201 if( p->pHaving ) n++;
202 if( p->pOrderBy ) n++;
203 if( p->pLimit ) n++;
drha1fd4b52018-07-10 06:32:53 +0000204#ifndef SQLITE_OMIT_WINDOWFUNC
205 if( p->pWin ) n++;
206 if( p->pWinDefn ) n++;
207#endif
drh1c4505d2015-08-26 11:34:31 +0000208 }
drh55b4c822019-08-03 16:17:46 +0000209 if( p->pEList ){
210 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
211 }
212 n--;
drha1fd4b52018-07-10 06:32:53 +0000213#ifndef SQLITE_OMIT_WINDOWFUNC
214 if( p->pWin ){
215 Window *pX;
216 pView = sqlite3TreeViewPush(pView, (n--)>0);
217 sqlite3TreeViewLine(pView, "window-functions");
218 for(pX=p->pWin; pX; pX=pX->pNextWin){
219 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
220 }
221 sqlite3TreeViewPop(pView);
222 }
223#endif
drh1c4505d2015-08-26 11:34:31 +0000224 if( p->pSrc && p->pSrc->nSrc ){
drh1c4505d2015-08-26 11:34:31 +0000225 pView = sqlite3TreeViewPush(pView, (n--)>0);
226 sqlite3TreeViewLine(pView, "FROM");
drh145d0a32018-11-08 22:53:06 +0000227 sqlite3TreeViewSrcList(pView, p->pSrc);
drh38b41492015-06-08 15:08:15 +0000228 sqlite3TreeViewPop(pView);
229 }
drh1c4505d2015-08-26 11:34:31 +0000230 if( p->pWhere ){
231 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
232 sqlite3TreeViewExpr(pView, p->pWhere, 0);
233 sqlite3TreeViewPop(pView);
drh38b41492015-06-08 15:08:15 +0000234 }
drh1c4505d2015-08-26 11:34:31 +0000235 if( p->pGroupBy ){
236 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
237 }
238 if( p->pHaving ){
239 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
240 sqlite3TreeViewExpr(pView, p->pHaving, 0);
241 sqlite3TreeViewPop(pView);
242 }
drha1fd4b52018-07-10 06:32:53 +0000243#ifndef SQLITE_OMIT_WINDOWFUNC
244 if( p->pWinDefn ){
245 Window *pX;
246 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
247 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
248 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
249 }
250 sqlite3TreeViewPop(pView);
251 }
252#endif
drh1c4505d2015-08-26 11:34:31 +0000253 if( p->pOrderBy ){
254 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
255 }
256 if( p->pLimit ){
257 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
drh8c0833f2017-11-14 23:48:23 +0000258 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
259 if( p->pLimit->pRight ){
260 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
261 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
262 sqlite3TreeViewPop(pView);
263 }
drh1c4505d2015-08-26 11:34:31 +0000264 sqlite3TreeViewPop(pView);
265 }
266 if( p->pPrior ){
267 const char *zOp = "UNION";
268 switch( p->op ){
269 case TK_ALL: zOp = "UNION ALL"; break;
270 case TK_INTERSECT: zOp = "INTERSECT"; break;
271 case TK_EXCEPT: zOp = "EXCEPT"; break;
272 }
273 sqlite3TreeViewItem(pView, zOp, 1);
274 }
275 p = p->pPrior;
276 }while( p!=0 );
drh38b41492015-06-08 15:08:15 +0000277 sqlite3TreeViewPop(pView);
278}
279
drha1fd4b52018-07-10 06:32:53 +0000280#ifndef SQLITE_OMIT_WINDOWFUNC
281/*
282** Generate a description of starting or stopping bounds
283*/
284void sqlite3TreeViewBound(
285 TreeView *pView, /* View context */
286 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
287 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
288 u8 moreToFollow /* True if more to follow */
289){
290 switch( eBound ){
291 case TK_UNBOUNDED: {
292 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
293 sqlite3TreeViewPop(pView);
294 break;
295 }
296 case TK_CURRENT: {
297 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
298 sqlite3TreeViewPop(pView);
299 break;
300 }
301 case TK_PRECEDING: {
302 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
303 sqlite3TreeViewExpr(pView, pExpr, 0);
304 sqlite3TreeViewPop(pView);
305 break;
306 }
307 case TK_FOLLOWING: {
308 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
309 sqlite3TreeViewExpr(pView, pExpr, 0);
310 sqlite3TreeViewPop(pView);
311 break;
312 }
313 }
314}
315#endif /* SQLITE_OMIT_WINDOWFUNC */
316
317#ifndef SQLITE_OMIT_WINDOWFUNC
318/*
319** Generate a human-readable explanation for a Window object
320*/
321void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
drhfc15f4c2019-03-28 13:03:41 +0000322 int nElement = 0;
drh0dc0e9c2019-03-28 13:35:28 +0000323 if( pWin->pFilter ){
324 sqlite3TreeViewItem(pView, "FILTER", 1);
325 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
326 sqlite3TreeViewPop(pView);
327 }
drha1fd4b52018-07-10 06:32:53 +0000328 pView = sqlite3TreeViewPush(pView, more);
329 if( pWin->zName ){
drh6f1644c2019-03-28 13:53:12 +0000330 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
drha1fd4b52018-07-10 06:32:53 +0000331 }else{
drh6f1644c2019-03-28 13:53:12 +0000332 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
drha1fd4b52018-07-10 06:32:53 +0000333 }
drhfc15f4c2019-03-28 13:03:41 +0000334 if( pWin->zBase ) nElement++;
335 if( pWin->pOrderBy ) nElement++;
336 if( pWin->eFrmType ) nElement++;
337 if( pWin->eExclude ) nElement++;
drhfc15f4c2019-03-28 13:03:41 +0000338 if( pWin->zBase ){
339 sqlite3TreeViewPush(pView, (--nElement)>0);
340 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
341 sqlite3TreeViewPop(pView);
342 }
drha1fd4b52018-07-10 06:32:53 +0000343 if( pWin->pPartition ){
drhfc15f4c2019-03-28 13:03:41 +0000344 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
drha1fd4b52018-07-10 06:32:53 +0000345 }
346 if( pWin->pOrderBy ){
drhfc15f4c2019-03-28 13:03:41 +0000347 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
drha1fd4b52018-07-10 06:32:53 +0000348 }
drhfc15f4c2019-03-28 13:03:41 +0000349 if( pWin->eFrmType ){
drh0dc0e9c2019-03-28 13:35:28 +0000350 char zBuf[30];
drhfc15f4c2019-03-28 13:03:41 +0000351 const char *zFrmType = "ROWS";
352 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
353 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
drh0dc0e9c2019-03-28 13:35:28 +0000354 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
355 pWin->bImplicitFrame ? " (implied)" : "");
356 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
drha1fd4b52018-07-10 06:32:53 +0000357 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
358 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
359 sqlite3TreeViewPop(pView);
360 }
drhfc15f4c2019-03-28 13:03:41 +0000361 if( pWin->eExclude ){
362 char zBuf[30];
363 const char *zExclude;
364 switch( pWin->eExclude ){
365 case TK_NO: zExclude = "NO OTHERS"; break;
366 case TK_CURRENT: zExclude = "CURRENT ROW"; break;
367 case TK_GROUP: zExclude = "GROUP"; break;
368 case TK_TIES: zExclude = "TIES"; break;
369 default:
370 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
371 zExclude = zBuf;
372 break;
373 }
374 sqlite3TreeViewPush(pView, 0);
375 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
376 sqlite3TreeViewPop(pView);
377 }
drha1fd4b52018-07-10 06:32:53 +0000378 sqlite3TreeViewPop(pView);
379}
380#endif /* SQLITE_OMIT_WINDOWFUNC */
381
382#ifndef SQLITE_OMIT_WINDOWFUNC
383/*
384** Generate a human-readable explanation for a Window Function object
385*/
386void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
387 pView = sqlite3TreeViewPush(pView, more);
388 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
389 pWin->pFunc->zName, pWin->pFunc->nArg);
390 sqlite3TreeViewWindow(pView, pWin, 0);
391 sqlite3TreeViewPop(pView);
392}
393#endif /* SQLITE_OMIT_WINDOWFUNC */
394
drh38b41492015-06-08 15:08:15 +0000395/*
396** Generate a human-readable explanation of an expression tree.
397*/
398void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
399 const char *zBinOp = 0; /* Binary operator */
400 const char *zUniOp = 0; /* Unary operator */
drhd97cda42017-04-14 14:02:14 +0000401 char zFlgs[60];
drh38b41492015-06-08 15:08:15 +0000402 pView = sqlite3TreeViewPush(pView, moreToFollow);
403 if( pExpr==0 ){
404 sqlite3TreeViewLine(pView, "nil");
405 sqlite3TreeViewPop(pView);
406 return;
407 }
drh11949042019-08-05 18:01:42 +0000408 if( pExpr->flags || pExpr->affExpr ){
drhb7e51992020-01-08 14:39:57 +0000409 StrAccum x;
410 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
411 sqlite3_str_appendf(&x, " fg.af=%x.%c",
412 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
drhd97cda42017-04-14 14:02:14 +0000413 if( ExprHasProperty(pExpr, EP_FromJoin) ){
drhb7e51992020-01-08 14:39:57 +0000414 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
drhd97cda42017-04-14 14:02:14 +0000415 }
drhb7e51992020-01-08 14:39:57 +0000416 if( ExprHasProperty(pExpr, EP_FromDDL) ){
417 sqlite3_str_appendf(&x, " DDL");
418 }
419 sqlite3StrAccumFinish(&x);
drhb3d903e2015-06-18 14:09:13 +0000420 }else{
421 zFlgs[0] = 0;
422 }
drh38b41492015-06-08 15:08:15 +0000423 switch( pExpr->op ){
424 case TK_AGG_COLUMN: {
drhb3d903e2015-06-18 14:09:13 +0000425 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
426 pExpr->iTable, pExpr->iColumn, zFlgs);
drh38b41492015-06-08 15:08:15 +0000427 break;
428 }
429 case TK_COLUMN: {
430 if( pExpr->iTable<0 ){
431 /* This only happens when coding check constraints */
drhd4933532019-10-31 12:30:38 +0000432 char zOp2[16];
433 if( pExpr->op2 ){
434 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
435 }else{
436 zOp2[0] = 0;
437 }
438 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
439 pExpr->iColumn, zFlgs, zOp2);
drh38b41492015-06-08 15:08:15 +0000440 }else{
drha513e592019-12-20 20:08:56 +0000441 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
442 pExpr->iTable, pExpr->iColumn,
443 pExpr->y.pTab, zFlgs);
drh38b41492015-06-08 15:08:15 +0000444 }
drhefad2e22018-07-27 16:57:11 +0000445 if( ExprHasProperty(pExpr, EP_FixedCol) ){
446 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
447 }
drh38b41492015-06-08 15:08:15 +0000448 break;
449 }
450 case TK_INTEGER: {
451 if( pExpr->flags & EP_IntValue ){
452 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
453 }else{
454 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
455 }
456 break;
457 }
458#ifndef SQLITE_OMIT_FLOATING_POINT
459 case TK_FLOAT: {
460 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
461 break;
462 }
463#endif
464 case TK_STRING: {
465 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
466 break;
467 }
468 case TK_NULL: {
469 sqlite3TreeViewLine(pView,"NULL");
470 break;
471 }
drh34328212018-02-26 19:03:25 +0000472 case TK_TRUEFALSE: {
drh43c4ac82018-02-26 21:26:27 +0000473 sqlite3TreeViewLine(pView,
drh96acafb2018-02-27 14:49:25 +0000474 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
drh34328212018-02-26 19:03:25 +0000475 break;
476 }
drh38b41492015-06-08 15:08:15 +0000477#ifndef SQLITE_OMIT_BLOB_LITERAL
478 case TK_BLOB: {
479 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
480 break;
481 }
482#endif
483 case TK_VARIABLE: {
484 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
485 pExpr->u.zToken, pExpr->iColumn);
486 break;
487 }
488 case TK_REGISTER: {
489 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
490 break;
491 }
drh38b41492015-06-08 15:08:15 +0000492 case TK_ID: {
493 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
494 break;
495 }
496#ifndef SQLITE_OMIT_CAST
497 case TK_CAST: {
498 /* Expressions of the form: CAST(pLeft AS token) */
499 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
500 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
501 break;
502 }
503#endif /* SQLITE_OMIT_CAST */
504 case TK_LT: zBinOp = "LT"; break;
505 case TK_LE: zBinOp = "LE"; break;
506 case TK_GT: zBinOp = "GT"; break;
507 case TK_GE: zBinOp = "GE"; break;
508 case TK_NE: zBinOp = "NE"; break;
509 case TK_EQ: zBinOp = "EQ"; break;
510 case TK_IS: zBinOp = "IS"; break;
511 case TK_ISNOT: zBinOp = "ISNOT"; break;
512 case TK_AND: zBinOp = "AND"; break;
513 case TK_OR: zBinOp = "OR"; break;
514 case TK_PLUS: zBinOp = "ADD"; break;
515 case TK_STAR: zBinOp = "MUL"; break;
516 case TK_MINUS: zBinOp = "SUB"; break;
517 case TK_REM: zBinOp = "REM"; break;
518 case TK_BITAND: zBinOp = "BITAND"; break;
519 case TK_BITOR: zBinOp = "BITOR"; break;
520 case TK_SLASH: zBinOp = "DIV"; break;
521 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
522 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
523 case TK_CONCAT: zBinOp = "CONCAT"; break;
524 case TK_DOT: zBinOp = "DOT"; break;
525
526 case TK_UMINUS: zUniOp = "UMINUS"; break;
527 case TK_UPLUS: zUniOp = "UPLUS"; break;
528 case TK_BITNOT: zUniOp = "BITNOT"; break;
529 case TK_NOT: zUniOp = "NOT"; break;
530 case TK_ISNULL: zUniOp = "ISNULL"; break;
531 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
532
drh34328212018-02-26 19:03:25 +0000533 case TK_TRUTH: {
drh43c4ac82018-02-26 21:26:27 +0000534 int x;
535 const char *azOp[] = {
536 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
537 };
drh34328212018-02-26 19:03:25 +0000538 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
539 assert( pExpr->pRight );
dan6ece3532019-06-12 13:49:32 +0000540 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
drh96acafb2018-02-27 14:49:25 +0000541 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
drh43c4ac82018-02-26 21:26:27 +0000542 zUniOp = azOp[x];
drh34328212018-02-26 19:03:25 +0000543 break;
544 }
545
drh94fa9c42016-02-27 21:16:04 +0000546 case TK_SPAN: {
547 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
548 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
549 break;
550 }
551
drh38b41492015-06-08 15:08:15 +0000552 case TK_COLLATE: {
drhc204d812019-09-10 17:51:27 +0000553 /* COLLATE operators without the EP_Collate flag are intended to
drh018dbb12019-09-28 16:14:55 +0000554 ** emulate collation associated with a table column. These show
555 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
556 ** operators that appear in the original SQL always have the
557 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
drhc204d812019-09-10 17:51:27 +0000558 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
559 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
560 pExpr->u.zToken, zFlgs);
drh38b41492015-06-08 15:08:15 +0000561 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
562 break;
563 }
564
565 case TK_AGG_FUNCTION:
566 case TK_FUNCTION: {
567 ExprList *pFarg; /* List of function arguments */
drha1fd4b52018-07-10 06:32:53 +0000568 Window *pWin;
drh38b41492015-06-08 15:08:15 +0000569 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
570 pFarg = 0;
drha1fd4b52018-07-10 06:32:53 +0000571 pWin = 0;
drh38b41492015-06-08 15:08:15 +0000572 }else{
573 pFarg = pExpr->x.pList;
drha1fd4b52018-07-10 06:32:53 +0000574#ifndef SQLITE_OMIT_WINDOWFUNC
drheda079c2018-09-20 19:02:15 +0000575 pWin = pExpr->y.pWin;
drha1fd4b52018-07-10 06:32:53 +0000576#else
577 pWin = 0;
578#endif
drh38b41492015-06-08 15:08:15 +0000579 }
580 if( pExpr->op==TK_AGG_FUNCTION ){
drh42d2fce2019-08-15 20:04:09 +0000581 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s",
582 pExpr->op2, pExpr->u.zToken, zFlgs);
drhd4933532019-10-31 12:30:38 +0000583 }else if( pExpr->op2!=0 ){
584 const char *zOp2;
585 char zBuf[8];
586 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
587 zOp2 = zBuf;
588 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
589 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
590 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
591 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
592 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
593 pExpr->u.zToken, zFlgs, zOp2);
drh38b41492015-06-08 15:08:15 +0000594 }else{
drh42d2fce2019-08-15 20:04:09 +0000595 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
drh38b41492015-06-08 15:08:15 +0000596 }
597 if( pFarg ){
drha1fd4b52018-07-10 06:32:53 +0000598 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
drh38b41492015-06-08 15:08:15 +0000599 }
mistachkin14897852018-07-23 18:53:49 +0000600#ifndef SQLITE_OMIT_WINDOWFUNC
drha1fd4b52018-07-10 06:32:53 +0000601 if( pWin ){
602 sqlite3TreeViewWindow(pView, pWin, 0);
603 }
604#endif
drh38b41492015-06-08 15:08:15 +0000605 break;
606 }
607#ifndef SQLITE_OMIT_SUBQUERY
608 case TK_EXISTS: {
drh9f6e14c2017-07-10 13:24:58 +0000609 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000610 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
611 break;
612 }
613 case TK_SELECT: {
drh9f6e14c2017-07-10 13:24:58 +0000614 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000615 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
616 break;
617 }
618 case TK_IN: {
drh9f6e14c2017-07-10 13:24:58 +0000619 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
drh38b41492015-06-08 15:08:15 +0000620 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
621 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
622 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
623 }else{
624 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
625 }
626 break;
627 }
628#endif /* SQLITE_OMIT_SUBQUERY */
629
630 /*
631 ** x BETWEEN y AND z
632 **
633 ** This is equivalent to
634 **
635 ** x>=y AND x<=z
636 **
637 ** X is stored in pExpr->pLeft.
638 ** Y is stored in pExpr->pList->a[0].pExpr.
639 ** Z is stored in pExpr->pList->a[1].pExpr.
640 */
641 case TK_BETWEEN: {
642 Expr *pX = pExpr->pLeft;
643 Expr *pY = pExpr->x.pList->a[0].pExpr;
644 Expr *pZ = pExpr->x.pList->a[1].pExpr;
645 sqlite3TreeViewLine(pView, "BETWEEN");
646 sqlite3TreeViewExpr(pView, pX, 1);
647 sqlite3TreeViewExpr(pView, pY, 1);
648 sqlite3TreeViewExpr(pView, pZ, 0);
649 break;
650 }
651 case TK_TRIGGER: {
652 /* If the opcode is TK_TRIGGER, then the expression is a reference
653 ** to a column in the new.* or old.* pseudo-tables available to
654 ** trigger programs. In this case Expr.iTable is set to 1 for the
655 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
656 ** is set to the column of the pseudo-table to read, or to -1 to
657 ** read the rowid field.
658 */
659 sqlite3TreeViewLine(pView, "%s(%d)",
660 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
661 break;
662 }
663 case TK_CASE: {
664 sqlite3TreeViewLine(pView, "CASE");
665 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
666 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
667 break;
668 }
669#ifndef SQLITE_OMIT_TRIGGER
670 case TK_RAISE: {
671 const char *zType = "unk";
drh11949042019-08-05 18:01:42 +0000672 switch( pExpr->affExpr ){
drh38b41492015-06-08 15:08:15 +0000673 case OE_Rollback: zType = "rollback"; break;
674 case OE_Abort: zType = "abort"; break;
675 case OE_Fail: zType = "fail"; break;
676 case OE_Ignore: zType = "ignore"; break;
677 }
678 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
679 break;
680 }
681#endif
drhc84a4022016-05-27 12:30:20 +0000682 case TK_MATCH: {
683 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
684 pExpr->iTable, pExpr->iColumn, zFlgs);
685 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
686 break;
687 }
drhdb97e562016-08-18 17:55:57 +0000688 case TK_VECTOR: {
drh269d3222019-10-23 18:09:39 +0000689 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
690 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
691 sqlite3_free(z);
drhdb97e562016-08-18 17:55:57 +0000692 break;
693 }
drh48cb3a72016-08-18 18:09:10 +0000694 case TK_SELECT_COLUMN: {
695 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
696 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
697 break;
698 }
drh31d6fd52017-04-14 19:03:10 +0000699 case TK_IF_NULL_ROW: {
700 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
701 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
702 break;
703 }
drh38b41492015-06-08 15:08:15 +0000704 default: {
705 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
706 break;
707 }
708 }
709 if( zBinOp ){
drhb3d903e2015-06-18 14:09:13 +0000710 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
drh38b41492015-06-08 15:08:15 +0000711 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
712 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
713 }else if( zUniOp ){
drhb3d903e2015-06-18 14:09:13 +0000714 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
drh11949042019-08-05 18:01:42 +0000715 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
drh38b41492015-06-08 15:08:15 +0000716 }
717 sqlite3TreeViewPop(pView);
718}
719
drhdb97e562016-08-18 17:55:57 +0000720
drh38b41492015-06-08 15:08:15 +0000721/*
722** Generate a human-readable explanation of an expression list.
723*/
drhdb97e562016-08-18 17:55:57 +0000724void sqlite3TreeViewBareExprList(
drh38b41492015-06-08 15:08:15 +0000725 TreeView *pView,
726 const ExprList *pList,
drh38b41492015-06-08 15:08:15 +0000727 const char *zLabel
728){
drh38b41492015-06-08 15:08:15 +0000729 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
730 if( pList==0 ){
731 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
732 }else{
drhdb97e562016-08-18 17:55:57 +0000733 int i;
drh38b41492015-06-08 15:08:15 +0000734 sqlite3TreeViewLine(pView, "%s", zLabel);
735 for(i=0; i<pList->nExpr; i++){
drh5579d592015-08-26 14:01:41 +0000736 int j = pList->a[i].u.x.iOrderByCol;
drh41cee662019-12-12 20:22:34 +0000737 char *zName = pList->a[i].zEName;
drhfbe07532018-04-23 20:04:38 +0000738 int moreToFollow = i<pList->nExpr - 1;
drhe1f49b82020-01-03 00:28:14 +0000739 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
drh5a699a02017-12-22 19:53:02 +0000740 if( j || zName ){
drhfbe07532018-04-23 20:04:38 +0000741 sqlite3TreeViewPush(pView, moreToFollow);
742 moreToFollow = 0;
743 sqlite3TreeViewLine(pView, 0);
744 if( zName ){
745 fprintf(stdout, "AS %s ", zName);
746 }
747 if( j ){
748 fprintf(stdout, "iOrderByCol=%d", j);
749 }
750 fprintf(stdout, "\n");
751 fflush(stdout);
drh5a699a02017-12-22 19:53:02 +0000752 }
drhfbe07532018-04-23 20:04:38 +0000753 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
drh5a699a02017-12-22 19:53:02 +0000754 if( j || zName ){
755 sqlite3TreeViewPop(pView);
756 }
drh38b41492015-06-08 15:08:15 +0000757 }
758 }
drhdb97e562016-08-18 17:55:57 +0000759}
760void sqlite3TreeViewExprList(
761 TreeView *pView,
762 const ExprList *pList,
763 u8 moreToFollow,
764 const char *zLabel
765){
766 pView = sqlite3TreeViewPush(pView, moreToFollow);
767 sqlite3TreeViewBareExprList(pView, pList, zLabel);
drh38b41492015-06-08 15:08:15 +0000768 sqlite3TreeViewPop(pView);
769}
770
771#endif /* SQLITE_DEBUG */