Skip to content

feat(BAR-53): Adds a relational algebra graph - #31

Merged
Bare7a merged 6 commits into
Bare7a:masterfrom
romancitodev:feat/relational-algebra-graph
Nov 30, 2026
Merged

Bare7a merged 6 commits into
Bare7a:masterfrom
romancitodev:feat/relational-algebra-graph

Conversation

@romancitodev

@romancitodev romancitodev commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

📝 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 + R opens 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 AND requires 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::select is a hand-written parser for one query, and replaces sqlparser. It reads each engine's own syntax with the connection's dialect:

    • CTEs, recursive ones too
    • set operations, with each engine's precedence
    • every join kind, APPLY and ClickHouse's ARRAY JOIN included
    • PREWHERE and QUALIFY
    • GROUP BY … WITH ROLLUP / CUBE / TOTALS and DISTINCT ON
    • LIMIT, TOP, FETCH … WITH TIES and LIMIT … BY

    Table hints and samples are read past. Every part keeps its byte span. Expressions keep their text, tagged as columns, wildcards, values, calls, comparisons or AND lists, 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, algebra builds the tree of AlgebraNodes from the parse, and columns works out what each operator puts out. lang::tokens::tokenize_with lexes with the given rules. In the UI, algebra_graph draws, previews and searches the graph, and algebra_graph/svg.rs exports it. ResultsPanel::show_algebra hosts it, and QueryTab::show_algebra and reveal draw it and mark picks. The ER diagram shares its pan and zoom (zoom::Viewport, area, wheel_zoom and buttons) and its search box (er_diagram::search::Search) with the graph, along with TextWidths, save_svg, assets::icon_svg and fuzzy::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.

    • The results pane goes from 15% up to where the editor keeps about three lines (5× its font size). Dragging there counts as maximized.
    • The sidebar (from 200px) and the JSON viewer (from 220px) grow as long as the editor keeps 400px.
    • The JSON viewer narrows instead of hiding, until not even 220px fits.
    • A saved width comes back when the window widens again.
  • 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 sqlparser is gone. On its own, the parser adds about 216 KB to a release build. sqlparser added about 6.2 MB, measured the same way.

  • Tests:

    • The parser: each engine's own syntax, where clauses land, limits, join kinds, hints and samples, expression shapes, aliases and set-operation precedence. Also nesting, half-typed queries failing where they stop, every prefix parsing or failing without a panic, and the nesting limit.
    • The algebra: trees, CTE inlining (recursive and capped), spans, items, output columns and types, engine clauses and AND lists. Deep queries run on a stack the size of the app's main thread.
    • The graph: layout, pill rows, multi-word join kinds, search ranking, previews and centred pills and titles. A hover card shows beside its node, even zoomed in.
    • Whole-window cases: Explain's menu (SQLite too) and the shortcut draw the graph in the results. A pick marks the query until the caret moves or the text changes. A pick in a graph of an edited statement says it changed.
    • The results splitter and the panel widths.

🧩 Type of change

  • 🐛 Bug fix
  • ✨ Feature
  • ♻️ Refactor
  • ⬆️ Dependencies
  • 📝 Docs or landing page
  • 🛠️ Build, CI or packaging

🧪 Tested

  • By hand on Windows
  • By hand on macOS
  • By hand on Linux
  • Tests workflow on macOS and Windows

📖 Docs

  • Updated the guide
  • Updated the README or landing page
  • No docs needed

📌 Notes

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.
@romancitodev

Copy link
Copy Markdown
Contributor Author
image This is an example based on this query:
SELECT e.nombre,
         COUNT(*) AS cajas,
         SUM(c.sospechosa) AS sospechosas,
         ROUND(AVG(c.total_contado - c.expected), 2) AS dif_prom,
         ROUND(SUM(m.movido), 2) AS movido
  FROM empleados e
  JOIN cajas c ON c.empleado_id = e.id
  LEFT JOIN (SELECT caja_id, SUM(monto) AS movido FROM movimientos GROUP BY caja_id) m ON m.caja_id = c.id
  WHERE e.activo = 1
  GROUP BY e.id, e.nombre
  HAVING COUNT(*) >= 5
  ORDER BY dif_prom DESC
  LIMIT 10;

@romancitodev romancitodev changed the title feat(algebra): ✨ Adds a relational algebra graph feat(BAR-25): ✨ Adds a relational algebra graph Oct 30, 2026
@romancitodev

romancitodev commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

hey @Bare7a ! Sorry for being away, these last few weeks have been tough, but in this days I will continue the PR!

@Bare7a

Bare7a commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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.

@romancitodev

romancitodev commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@Bare7a

Bare7a commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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?

@romancitodev

Copy link
Copy Markdown
Contributor Author

Yeah, sure! I'm not at home right now, so in a couple hours ill check that.

@romancitodev

Copy link
Copy Markdown
Contributor Author

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

@romancitodev
romancitodev marked this pull request as draft November 29, 2026 11:13
@romancitodev

Copy link
Copy Markdown
Contributor Author

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.

@Bare7a

Bare7a commented Nov 30, 2026

Copy link
Copy Markdown
Owner

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! 🙌

@Bare7a
Bare7a marked this pull request as ready for review November 30, 2026 11:30
@Bare7a Bare7a changed the title feat(BAR-25): ✨ Adds a relational algebra graph feat(BAR-53): Adds a relational algebra graph Nov 30, 2026
@Bare7a
Bare7a merged commit a8fb440 into Bare7a:master Nov 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: relational algebra graph editor

2 participants