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

Fix `EXPLAIN` to indent loops like SQLite does #108

Closed penberg closed 1 month ago

penberg commented 1 month ago

Limbo:

> EXPLAIN SELECT id FROM users;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0     Init           0     10    0       0   Start at 10
1     OpenReadAsync  0     2     0       0   root=2
2     OpenReadAwait  0     0     0       0
3     RewindAsync    0     0     0       0
4     RewindAwait    0     9     0       0
5     RowId          0     0     0       0
6     ResultRow      0     1     0       0   output=r[0..1]
7     NextAsync      0     0     0       0
8     NextAwait      0     4     0       0
9     Halt           0     0     0       0
10    Transaction    0     0     0       0
11    Goto           0     1     0       0

SQLite:

sqlite> EXPLAIN SELECT id FROM users;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     7     0                    0   Start at 7
1     OpenRead       0     2     0     0              0   root=2 iDb=0; users
2     Rewind         0     6     0                    0
3       Rowid          0     1     0                    0   r[1]=users.rowid
4       ResultRow      1     1     0                    0   output=r[1]
5     Next           0     3     0                    1
6     Halt           0     0     0                    0
7     Transaction    0     0     1     0              1   usesStmtJournal=0
8     Goto           0     1     0                    0
Ramkarthik commented 1 month ago

@penberg I'm fairly new to Rust and completely new to limbo (playing with it for the last few hours). I have an approach where I calculate the indent based on the opcode (increase indent after RewindAwait and SorterSort, decrease indent on the same line as NextAsync and SorterNext). With this, I get the following output:

> EXPLAIN SELECT id FROM users;

addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------
0    Init           0     10    0       0   Start at 10
1    OpenReadAsync  0     2     0       0   root=2
2    OpenReadAwait  0     0     0       0
3    RewindAsync    0     0     0       0
4    RewindAwait    0     9     0       0
5      RowId          0     0     0       0
6      ResultRow      0     1     0       0   output=r[0..1]
7    NextAsync      0     0     0       0
8    NextAwait      0     4     0       0
9    Halt           0     0     0       0
10   Transaction    0     0     0       0
11   Goto           0     1     0       0

If this is the right approach, I can create a PR (code).

penberg commented 1 month ago

Hey @Ramkarthik! Looks reasonable, send a PR!