penberg / limbo

Limbo is a work-in-progress, in-process OLTP database management system, compatible with SQLite.
MIT License
896 stars 49 forks source link

align columns in `EXPLAIN` command #112

Closed pereman2 closed 1 month ago

pereman2 commented 1 month ago
> explain select max(price) from users, products limit 1;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0    Init           0     20    0       0   Start at 20
1    Integer        0     1     0       0
2    OpenReadAsync  0     2     0       0   root=2
3    OpenReadAwait  0     0     0       0
4    OpenReadAsync  1     3     0       0   root=3
5    OpenReadAwait  0     0     0       0
6    RewindAsync    0     0     0       0
7    RewindAwait    0     16    0       0
8      RewindAsync    1     0     0       0
9      RewindAwait    1     14    0       0
10       Column         1     2     2       0   r[2]= cursor 1 column 2
11       AggStep        0     2     1     max  0   accum=r[1] step(2)
12     NextAsync      1     0     0       0
13     NextAwait      1     9     0       0
14   NextAsync      0     0     0       0
15   NextAwait      0     7     0       0
16   AggFinal       0     1     0     max  0   accum=r[1]
17   ResultRow      1     1     0       0   output=r[1..2]
18   DecrJumpZero   0     19    0       0
19   Halt           0     0     0       0
20   Transaction    0     0     0       0
21   Goto           0     1     0       0

Columns should be vertically aligned

penberg commented 1 month ago

@pereman2 What we do is actually aligned with SQLite, but we can fix that if we want

pereman2 commented 1 month ago

@penberg

sqlite> explain select max(price) from users, products limit 1;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     18    0                    0   Start at 18
1     Integer        1     1     0                    0   r[1]=1; LIMIT counter
2     Null           0     2     3                    0   r[2..3]=NULL
3     OpenRead       0     2     0     0              0   root=2 iDb=0; users
4     OpenRead       1     3     0     3              0   root=3 iDb=0; products
5     Rewind         0     13    0                    0   
6       Rewind         1     13    0                    0   
7         Column         1     2     4                    0   r[4]= cursor 1 column 2
8         RealAffinity   4     0     0                    0   
9         CollSeq        0     0     0     BINARY-8       0   
10        AggStep        0     4     2     max(1)         1   accum=r[2] step(r[4])
11      Next           1     7     0                    1   
12    Next           0     6     0                    1   
13    AggFinal       2     1     0     max(1)         0   accum=r[2] N=1
14    Copy           2     5     0                    0   r[5]=r[2]
15    ResultRow      5     1     0                    0   output=r[5]
16    DecrJumpZero   1     17    0                    0   if (--r[1])==0 goto 17
17    Halt           0     0     0                    0   
18    Transaction    0     0     2     0              1   usesStmtJournal=0
19    Goto           0     1     0                    0   

sqlite pads the columns so that max(1) doesn't destroy the alignment. It is true I would even prefer full alignment in the Y axis for columns so that identation only happens on op code

Ramkarthik commented 1 month ago

@pereman2 @penberg Is this the preferred output?

> explain select max(price) from users, products limit 1;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0    Init           0     20    0       0   Start at 20
1    Integer        0     1     0       0
2    OpenReadAsync  0     2     0       0   root=2
3    OpenReadAwait  0     0     0       0
4    OpenReadAsync  1     3     0       0   root=3
5    OpenReadAwait  0     0     0       0
6    RewindAsync    0     0     0       0
7    RewindAwait    0     16    0       0
8      RewindAsync  1     0     0       0
9      RewindAwait  1     14    0       0
10       Column     1     2     2       0   r[2]= cursor 1 column 2
11       AggStep    0     2     1     max  0   accum=r[1] step(2)
12     NextAsync    1     0     0       0
13     NextAwait    1     9     0       0
14   NextAsync      0     0     0       0
15   NextAwait      0     7     0       0
16   AggFinal       0     1     0     max  0   accum=r[1]
17   ResultRow      1     1     0       0   output=r[1..2]
18   DecrJumpZero   0     19    0       0
19   Halt           0     0     0       0
20   Transaction    0     0     0       0
21   Goto           0     1     0       0

If yes, I can send a PR where we only indent the opcode.

pereman2 commented 1 month ago
> explain select max(price) from users, products limit 1;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0    Init            0     20    0     0              0    Start at 20
1    Integer         0     1     0     0              0
2    OpenReadAsync   0     2     0     0              0    root=2
3    OpenReadAwait   0     0     0     0              0
4    OpenReadAsync   1     3     0     0              0    root=3
5    OpenReadAwait   0     0     0     0              0
6    RewindAsync     0     0     0     0              0
7    RewindAwait     0     16    0     0              0
8      RewindAsync   1     0     0     0              0
9      RewindAwait   1     14    0     0              0
10       Column      1     2     2     0              0    r[2]= cursor 1 column 2
11       AggStep     0     2     1     max            0    accum=r[1] step(2)
12     NextAsync     1     0     0     0              0
13     NextAwait     1     9     0     0              0
14   NextAsync       0     0     0     0              0
15   NextAwait       0     7     0     0              0
16   AggFinal        0     1     0     max            0    accum=r[1]
17   ResultRow       1     1     0     0              0    output=r[1..2]
18   DecrJumpZero    0     19    0     0              0
19   Halt            0     0     0     0              0
20   Transaction     0     0     0     0              0
21   Goto            0     1     0     0              0

this is my preference but I'll let @penberg decide.

penberg commented 1 month ago

Let's do it @Ramkarthik!

Ramkarthik commented 1 month ago

@penberg I've sent a PR (#117)