Repository navigation
feat(BAR-53): Adds a relational algebra graph - #31
Conversation
Adds sqlparser and turns a query into operators (selection, projection, join, grouping, sort, limit, set operations), with CTEs inlined. Each operator keeps the bytes of the statement it comes from, its list of items and the columns it outputs.
A tab beside Explain draws the query as a graph in the ER diagram's style, with pills for the projected, grouped and sorted items. Hovering an operator previews the table at that point with example rows, and clicking one marks its part of the query in the editor tab it came from. The tab is stored in the session with the statement and where it began.
…as code Selections list their conditions one a row, a join's kind moves to the header, and aliases and limits show as pills. Bodies are as tall as their text, in the editor's monospace font.
|
hey @Bare7a ! Sorry for being away, these last few weeks have been tough, but in this days I will continue the PR! |
|
Hey, could you look into replacing sqlparser with a lighter alternative? The current dependency (sqlparser = { version = "0.63", default-features = false, features = ["std"] }) adds around 10 MB to the binary, which I don't think is worth the overhead. I'd prefer something more lightweight if possible. |
|
Maybe I could try to parse the sql with as few dependencies as possible. https://lib.rs/crates/sqlparser btw it's weird that adds that much MBs to the binary, because lib.rs says that the dependencies adds between ~0–2.2MB and the lib itself are 3 MBs, so that should add between 3 and 4 i guess? because we are not using all the deps. |
|
If the difference is 3 MB or less, then that's perfectly fine. I might have gotten it wrong when I compared it with the previous version. Could you sync the branch with master and compare the binary size against the latest release (1.2.8) to see what the actual difference is? |
|
Yeah, sure! I'm not at home right now, so in a couple hours ill check that. |
|
Ey! I finally got the time to test this... and you were right, for me, it added 7.1 MB, so I guess I will need to write my own parser so it doesn't bloat the app. @Bare7a |
|
Hey @Bare7a ! Sorry again, I was a bit off this weeks, I had to take some exams. So I'm going to change this to a draft because probably I'm going to write an SQL parser instead of relying on a library. |
|
Hey @romancitodev ! Thanks for the contribution! I'll implement the SQL parser on my side and integrate it with your work, then proceed with merging the PR. Really appreciate your effort! 🙌 |

📝 What changed?
Adds a relational algebra graph for a query, next to Explain. Relational algebra draws the selected text, or the statement at the caret, as a tree of operators in the query tab's results, where a plan would show. Nothing runs. Tables and VALUES lists are the leaves, and above them sit ⋈ joins, × products, σ selections, γ groupings, π projections, δ distinct, τ sorts,
limit(LIMIT, TOP, FETCH), ρ renames for subqueries and CTEs, and ∪ ∩ − for set operations. A CTE is inlined where it's used. Closes #27.Opening it: Relational algebra sits under a separator in the Explain button's menu, which now shows on every engine, SQLite included. It's also in the editor's right-click menu and the command palette, and
Ctrl/⌘ + Shift + Ropens it (remappable). It's off while a query runs, and like Explain it asks about unsaved result edits before taking the results' place. Like a plan, it stays until the next run and isn't kept after a restart.The graph: drawn in the ER diagram's style, with flat boxes and the operator's symbol and name in each header. Leaves sit along the bottom and each operator above what it reads, joined by arrows. Projections, groupings and sorts list their items as pills, up to four rows and then a "+N" pill. A selection lists each condition its
ANDrequires on its own row. A join shows its kind (LEFT,CROSS APPLY,LEFT ARRAY…) in its header and its condition below. Other conditions and expressions show as code. A table's header shows the engine's row estimate where it keeps one, and a VALUES list its row count.Getting around: drag to pan; zoom with
⌘/Ctrl+ scroll, a pinch or the buttons; Fit shows it all. It opens fitted, unless that would be too small to read. The search box finds operators by name, tables, conditions and items, whole matches first. Enter and Shift+Enter step through the hits, the count sits inside the box, and its placeholder counts the operators and tables. Save as SVG writes the graph as drawn, in the theme's colours.Hover: resting on an operator or table as long as a tooltip waits opens a card beside it. The card lists the columns that come out of it with their types, and their tables when more than one table contributes. It shows up to 12 and counts the rest. A selection or join repeats its condition above the list. The columns come from the schema cache the sidebar uses, which loads the tables the query names if they aren't loaded yet. The query itself never runs.
Picking: a click lights an operator in its own colour and marks the text it comes from in the editor above. The caret moves there and the keys stay with the editor. A sort's mark includes its direction, and a join's runs from its table to the end of its condition. Products and set operations mark their whole span. Moving the caret or typing drops the mark and the pick. A click finds the statement wherever it now is, so edits elsewhere in the tab don't matter. If the statement itself changed, the click says so instead of marking the wrong text.
Errors: anything other than a single SELECT, VALUES or WITH query says it can't be drawn. A syntax error names the text where reading stopped, or says the query ends too soon.
Parser:
barsql_sql::selectis a hand-written parser for one query, and replacessqlparser. It reads each engine's own syntax with the connection's dialect:APPLYand ClickHouse'sARRAY JOINincludedPREWHEREandQUALIFYGROUP BY … WITH ROLLUP / CUBE / TOTALSandDISTINCT ONLIMIT,TOP,FETCH … WITH TIESandLIMIT … BYTable hints and samples are read past. Every part keeps its byte span. Expressions keep their text, tagged as columns, wildcards, values, calls, comparisons or
ANDlists, so the visual query builder (feat: visual query builder #29) can import them later. It stops with an error past 128 nested parentheses or 1000 set operators, before the stack runs out. A recursive CTE is inlined once and reads itself as a leaf. Past 4000 copied operators, a CTE shows as a table instead.Code: in barsql-sql,
algebrabuilds the tree ofAlgebraNodes from the parse, andcolumnsworks out what each operator puts out.lang::tokens::tokenize_withlexes with the given rules. In the UI,algebra_graphdraws, previews and searches the graph, andalgebra_graph/svg.rsexports it.ResultsPanel::show_algebrahosts it, andQueryTab::show_algebraandrevealdraw it and mark picks. The ER diagram shares its pan and zoom (zoom::Viewport,area,wheel_zoomandbuttons) and its search box (er_diagram::search::Search) with the graph, along withTextWidths,save_svg,assets::icon_svgandfuzzy::substring_rank. The ER diagram works as before.Panels: the results pane and the side panels lose their fixed maximums of 70%, 520px and 640px.
Docs: a Relational Algebra Graph section in the editor guide, after the Query Plan Viewer, plus the entry in its right-click menu list. The shortcut is in the app guide's table, and the guide index names the graph.
Size: no new crates, and
sqlparseris gone. On its own, the parser adds about 216 KB to a release build.sqlparseradded about 6.2 MB, measured the same way.Tests:
ANDlists. Deep queries run on a stack the size of the app's main thread.🧩 Type of change
🧪 Tested
📖 Docs
📌 Notes