Fork me on GitHub

Hello, I posted this in find-my-lib, but cross-posting here as well: does anyone know of a library that parses SQL text (mostly with, from, join, CTEs, and subqueries) into a Graphviz diagram showing how inputs flow?


I think you would want something that visualizes execution plans that you get from the database. Or do you mean something else when you say 'how inputs flow'?


No, nothing to do with execution plans. Most businesses have complex analytical queries to compute analytic targets ( DAU, MAU, churn, etc,,). Often there is complex business logic in the form of SQL queries that provide the basis for such computation (that differ for each business depending on how they wish to compute it though there are industry norms). It is often the case that there are several (or many) CTEs that do joins, groupings, filterings, and compute metrics along the way before the final projection. Hence a graphviz diagram is often helpful. The difficult part is that the SQL needs to be parsed properly before a diagram can be made. Fun project though, if it doesn't already exist.


Here is an example, but it could be much improved


(i.e. it doesn't work very well on files I have tested it with)


e.g. is perhaps a starting point


I've something related before, but only for table relationships based on consuming a bunch of queries and looking at the joins. The code is here, but keep in mind it is just an example:


That is probably a cool idea for Clojure if there is a good SQL Parser available

🔥 2

Nice! My C# isn't too good, but table relationships are an important part. Typically, queries start with tables, then have CTE's and/or subqueries in route to the final projection


exactly, looking for a good SQL parser. Perhaps there is a BNF route with instaparse, not sure


Yea that could work. Or maybe this one, if you can find a good antler file: Maybe from here:


never heard of g4 files


clj-antlr errors out and seems hard to use without a better understanding. Another resource might be


Ah, bummer. If you need T-SQL specifically though, I know the one inside this package works extremely well (we use it for parsing, rewriting and building SQL in production): Bummer if C#/F# is out of the question.


In R, simplifies things without needing a parser. But anyways, a clojure library for this would be useful


There may well be something of use here... (although the upstream EBNF grammar + edn conversion isn't available for distribution)

metal 2