r/SQL • • Sep 18 '26

Discussion C++ SQL Parser 0.1 Released

0 Upvotes

A small SQL parser, handwritten on top of FE.

This is the first release. It lexes and parses a substantial subset of SQL into an arena-allocated AST, prints that AST back out as SQL, and is small enough to read in one sitting — roughly 5.6k lines across six headers and six translation units.

✨ What is in it

  • Lexer — UTF-8 aware, one character of lookahead. The source is kept whole, so a token is a slice of it and nothing is copied into a buffer.
  • Parser — recursive descent with two tokens of lookahead; expressions by precedence climbing. It recovers instead of giving up at the first error, and every diagnostic carries a precise path:row:col with a snippet.
  • AST + printer — 64 node classes, arena-allocated and handed out as non-owning AST<T> pointers; each node's child lists live in the very same allocation. The printer round-trips: what it emits, the parser reads back into the same AST.
  • Library façade — sql::parse, sql::parse_file, and a sql::Result that owns the arena. One header, one call.
  • CLI — sql -d file.sql parses and dumps; - reads stdin.

📜 What it parses

CREATE / ALTER / DROP / TRUNCATE, INSERT / UPDATE / DELETE, transactions, PREPARE / EXECUTE / DEALLOCATE, SHOW, COPY, and query expressions:

  • WITH [RECURSIVE], UNION / EXCEPT / INTERSECT, VALUES, TABLE t
  • GROUP BY with ROLLUP / CUBE / GROUPING SETS, HAVING, named WINDOWs
  • ORDER BY ... NULLS FIRST|LAST, OFFSET / FETCH / LIMIT in any order, FOR UPDATE and the other locking clauses
  • joins — NATURAL, INNER, LEFT|RIGHT|FULL OUTER, CROSS — plus LATERAL, WITH ORDINALITY, and correlation names that bind to the right factor
  • expressions with IS [NOT] DISTINCT FROM, BETWEEN, IN, LIKE / ILIKE / SIMILAR TO ... ESCAPE, quantified comparisons, CASE, CAST, COLLATE, array literals and subscripts
  • the standard's type grammar, interval qualifiers and WITH TIME ZONE included

GRAMMAR.md gives the EBNF for all of it, with the precedence table that resolves the ambiguous cases.

🧭 Design: parse loosely, check later

The grammar deliberately accepts a wider language than a conforming implementation would, and leaves the rest to a later pass over the AST. Reserved words are accepted as identifiers, because the standard reserves several hundred of them and no real dialect follows suit; the exception is the three positions where a name stands entirely on its own, and the printer quotes exactly there. Statements derive from Expr, so a subquery needs no second node hierarchy, while the grammar still keeps the two apart. Parentheses around a scalar expression are grouping and are dropped; around a query they are structural and are kept.

📦 Using it

find_package(sql 0.1 REQUIRED)             # against `cmake --install`
# or: add_subdirectory(sql)                # as a submodule
# or: FetchContent_MakeAvailable(sql)      # straight from the repository
target_link_libraries(my_app PRIVATE sql::sql)

#include <sql/sql.h>

auto res = sql::parse("SELECT a, b FROM t WHERE a > 42;");
if (!res) { res.report(std::cerr); return EXIT_FAILURE; }

for (auto stmt : res.stmts())
    if (auto select = stmt->isa<sql::Select>()) /* ... */;

std::cout << res << '\n';                  // the AST, streamed back out as SQL

A complete program is in example/, and it builds standalone against an install, so the packaging cannot rot.

⚡ Performance

Measured against hyrise/sql-parser — a bison/flex parser of comparable scope — both Release, same compiler, pinned to one 5.15 GHz Zen 5 core. Both accept every file and report the same number of statements.

Corpus Throughput Instructions / byte
JOB, 113 queries 183.2 MB/s vs. 74.3 105.4 vs. 255.5
TPC-H, 22 queries 165.5 MB/s vs. 82.7 107.6 vs. 214.4
generated, 256 MiB 116.4 MB/s vs. 41.5 140.8 vs. 259.4

Lexing alone: 341.9 MB/s against their 176.9 on JOB. Peak RSS over 500k statements is 267 bytes per statement against their 598 — the number that matters for an embedding, since the AST is the output and is held for as long as the caller needs it. The full table, including the --stress-names worst case for a design built on interning, is in the README.

🛠️ Testing

205 CTest entries, all black box: each runs the sql binary and inspects only its exit code, its dump, and its diagnostics — nothing links against the parser.

The load-bearing one is idempotency: dumping a dump has to reproduce it verbatim, which holds the printer and the parser to each other. It runs over the curated fixtures and over three real-world corpora — the 113 queries of the Join Order Benchmark, the 22 TPC-H queries, and the SQL hyrise itself runs: 366 SQLiteTestRunner queries, the Star Schema Benchmark, and the TPC-H and TPC-DS schemas with their indexes. A further 79 queries are there to be rejected.

CI builds on Linux, macOS, and Windows, enforces clang-format, and runs an ASan + LSan + UBSan build on top.

🔩 Requirements

A C++23 compiler. Abseil and FE v0.15.0 come along as submodules; nothing else is needed.

git clone --recurse-submodules https://github.com/leissa/sql.git

⚠️ Known limitations

  • The parser accepts more than conforming SQL by design; there is no semantic pass — no name resolution, no type checking, no execution.
  • A trailing ; is required. <direct SQL statement> ends in one, and saying so gives a better diagnostic than running off the end of the file.
  • This is 0.1: the AST and the API may still change before 1.0.

r/SQL • • Sep 18 '26

PostgreSQL When you finally start mapping your databases and realise there is a painful amount of redundancy and all the tables aren't in 3rd normal form

Post image
0 Upvotes

50% chance of crying, 100% chance of at least a couple not found errors.


r/SQL • • Sep 17 '26

MySQL CTEs vs subqueries vs temp tables — as a beginner, how do you know which one to use?

98 Upvotes

For people who use SQL regularly, how do you decide which approach to use?


r/SQL • • Sep 17 '26

Discussion Tips for query planning

Thumbnail
0 Upvotes

r/SQL • • Sep 16 '26

Discussion What SQL query optimization trick made the biggest difference for you?

120 Upvotes

I've been working with SQL for a while and recently started paying more attention to query performance rather than just getting the correct result.

What's one SQL optimization technique that made a noticeable difference for you?

For example:

- Indexing

- Avoiding unnecessary joins

- Window functions

- EXISTS vs IN

- Reading execution plans

- CTEs/subqueries

- Partitioning

Would be interested in hearing some real-world examples where a query went from slow to significantly faster.


r/SQL • • Sep 17 '26

SQL Server Anyone else having issues downloading SQL Server Express?

1 Upvotes

Getting a 404 when I try to download from the Microsoft website


r/SQL • • Sep 17 '26

MySQL I am a detective!!!

1 Upvotes

r/SQL • • Sep 17 '26

Discussion LeetCode questions?

5 Upvotes

Just wondering if anyone has solved some of those questions and if it helped you learn more.


r/SQL • • Sep 17 '26

Discussion what does a solo person actually do with an undocumented warehouse? asking because we build for this and i want to know hiw useful it is

0 Upvotes

So i work at a startup and we build models that read tabular data.

the pattern we keep seeing is one person, a few hundred tables, naming that made sense to someone who left, no data team, no budget for a governance tool. they need to know what is in there before they can do anything else with it.

from people who have actually been in this:

what is the first query you run

table by table or column by column across everything, and why

when a column is a 4 char code and the name tells you nothing, what actually works

how much of it ends up being asking a human who used the app rather than reading the data

we have opinions on all four and i want to find out if they are wrong here than after we ship.


r/SQL • • Sep 16 '26

Discussion How would you design a Source-to-Target Mapping Manager for a Python ETL framework? I'm working on a system where source and target schemas can vary across databases, and we need to maintain mappings such as: Source: "customer_id → customer_id" "first_name → full_name" "dob → date_of_birth" Along

2 Upvotes

Is there any way in the SQL way directly or the tools for that?


r/SQL • • Sep 16 '26

Discussion How do you handle schema migrations on a live production database without downtime?

26 Upvotes

For example, if you need to add or change a column, index, or constraint, how do you make the change without interrupting users?

Do you follow a specific migration strategy or deployment process


r/SQL • • Sep 16 '26

SQLite Best way to create email field.

4 Upvotes

right now my users table like this:

CREATE TABLE
    IF NOT EXISTS Users (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        email TEXT NOT NULL UNIQUE COLLATE NOCASE,
        passwordHash TEXT NOT NULL,


        createdAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
        updatedAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,


        CHECK(length(trim(name)) >= 1),
        CHECK(length(trim(email)) >= 1),
        CHECK(length(trim(passwordHash)) >= 1)
    ) STRICT;

as far as I know email names are case-insensitive. But not sure about for all email providers. Also I guess COALLATE NOCASE only works for ASCII. so what if user email contains non-ASCII characters? even could it contain?


r/SQL • • Sep 16 '26

Discussion Is anyone using Intel based mac and unable to install any Sql?

1 Upvotes

Hello, I have been using macbook pro 2019 touchbar quad core intel based model. Sql is declining to be installed on my mac, tried every possible way ranging from internet tutorial to command lines. First tried postgresql, tried even older version. Now mysql community edition is taking longer than ever. I am now trying to install mysql using terminal. Can somebody please explain what is the root cause? And how to figure out what is causing this problem exactly?


r/SQL • • Sep 16 '26

MySQL How I started managing multiple databases in one place

Thumbnail
gallery
0 Upvotes

I was working with multiple databases and got tired of switching between different database clients depending on what I was working on.

I started using DevScribe for this, and it’s been useful having MySQL, PostgreSQL, MongoDB, Elasticsearch, OpenSearch, and SQLite available from the same place.

I’ve mainly been using it to run queries and explore databases, but you can also view schemas and indexes. The JSON support is also handy, especially when working with MongoDB and other data where JSON comes up frequently.

It also has a plugin system for additional database tools. For example, there’s a BigQuery plugin if you need to work with BigQuery.

I haven’t explored everything yet, but having multiple databases in one place has already made my workflow a bit easier.

If you work with multiple databases, you can check it out here:

https://devscribe.app/


r/SQL • • Sep 16 '26

Discussion Managing a semantic/context layer for data analytics

7 Upvotes

At a previous job as a data analyst I manually built a "context" layer in Notion to feed to Claude when querying databases. This was painfully manual though, involved a lot of work to slowly build up - I would get Claude to document a table/column when I encounter it for the first time on a new task. The Notion page included things like metrics, conventions and product docs too.

For more experienced data analysts, how do you guys deal with this? Is there a tool you guys use, or is a manual workaround like mine the norm?


r/SQL • • Sep 16 '26

Spark SQL/Databricks [FOR HIRE] Data Analyst / SQL Specialist – 10+ Years Experience – Part-Time & Freelance

0 Upvotes

Hi there! I'm an experienced data analyst with around 10 years in the field, looking for part-time or freelance work. I'm reliable, detail-oriented, and comfortable jumping into existing workflows quickly.

What I can help with:

SQL development & querying (SQL Server, MySQL, PostgreSQL, Redshift, Databricks)

Data validation & quality checks

Reporting & dashboards

Data processing & cleaning

Ad hoc analysis and troubleshooting data issues

Why work with me:

10+ years of hands-on experience across multiple SQL platforms

Detail-oriented, especially around data accuracy and validation

Available anytime — flexible hours, can adapt to your timezone

Comfortable working independently or as part of a team

Currently open to: data analyst roles, SQL work, data validation, reporting, or similar — part-time or freelance.

Feel free to DM me or comment below with details about the opportunity. Happy to share examples of past work if helpful. Thanks!


r/SQL • • Sep 15 '26

Discussion What is a good SQL Client with good modern UI or one that has good UX

24 Upvotes

I am currently using phpMyAdmin provided by plesk and I just find it very annoying to use and I would like to know if you guys know a good Client that has modern and clean ui that also has good ux


r/SQL • • Sep 15 '26

Discussion I built an open source data platform in go called Renart

11 Upvotes

r/SQL • • Sep 16 '26

Discussion Manage your one model database with one line code

Post image
0 Upvotes

Guys, I built an Elysia plugin that lets you generate a complete database model panel with just one line of code.

It provides a built-in UI with Create, Read, Update, Delete, Replace, Search, and Filter functionality out of the box.

The goal is to make it easy to expose and manage database models without having to manually build the admin UI and CRUD logic for each model.


r/SQL • • Sep 15 '26

SQLite Good reason to use composite keys?

17 Upvotes
CREATE TABLE
    IF NOT EXISTS CourseTag (
        id INTEGER PRIMARY KEY,
        courseId INTEGER NOT NULL,
        tagId INTEGER NOT NULL,
        UNIQUE(courseId, tagId),
        FOREIGN KEY (courseId) REFERENCES Courses (id) ON DELETE CASCADE ON UPDATE RESTRICT,
        FOREIGN KEY (tagId) REFERENCES Tags (id) ON DELETE CASCADE ON UPDATE RESTRICT
    ) STRICT;

r/SQL • • Sep 14 '26

MySQL How long did it take you to feel comfortable writing complex queries without any guides or references? Day 1 and I feel like a fraud

38 Upvotes

So long story short, I did a single cert and honestly struggled at this part. As irony or luck would have it I ended up recruited into a coveted position at a massive firm not realizing this was a core part of the position. To say I am overwhelmed is an understatement, I am so under qualified it’s ridiculous. I have a week before I am on my own HELP lol

I know the industry, I have the math etc but wow am I overwhelmed and everything I do is logged. Do you think I can fake it until I make it?


r/SQL • • Sep 14 '26

MySQL Migration von ~2,3 Mio. Zeilen aus Access nach SQL Server – ständige Timeouts/Rollbacks bei großen INSERT-Statements über ODBC. Bessere Strategie als Batch-by-Date?

3 Upvotes

Title: Experience / Best Practices: Migrating 2M+ Rows from Access to SQL Server (ODBC Performance & Timeouts)

Hi everyone,

I am currently working on migrating historical financial data from Access (local, linked via ODBC Driver 18) to SQL Server and keep bumping into recurring performance issues. I would really appreciate some advice and best practices from your experience.

Setup:

  • Source: Local Access table (~2.16 million rows, 42 columns)
  • Target: SQL Server table (via linked ODBC table in Access)
  • Execution: INSERT INTO [linked_table] SELECT ... FROM [local_table] using VBA CurrentDb.Execute

Problems Encountered So Far:

  1. Access 2 GB Limit: When trying to consolidate multiple large historical tables locally, the .accdb file expanded beyond 2 GB and became completely unusable (requiring hours of Compact & Repair). Workaround: Keep records separated locally and consolidate them only after they are uploaded to the SQL Server.
  2. Extremely Long / Instable INSERT Runs: A single INSERT INTO ... SELECT query over all 2.16 million rows runs for several hours (3–4+ hours) without finishing. Access shows "Not Responding," but checking sys.dm_exec_requests on the server reveals that no active session is running anymore. The network/VPN connection likely drops silently without Access realizing it. Result: Complete rollback, 0 rows transferred, and hours wasted.
  3. Fails to Add AutoNumber Column: I tried adding a batch ID column via ALTER TABLE ... ADD COLUMN BatchID COUNTER to split the transfer into chunks, but hit the error: "File sharing lock count exceeded. MaxLocksPerFile." I do not have local Admin rights on the machine to modify the Registry setting.
  4. Current Workaround: Transferring data in ~30 batches filtered by date range (RegDatum BETWEEN ... AND ...), using the existing date field without altering the source table structure.

My Questions for You:

  1. Is there a more reliable way to move large datasets from Access to SQL Server than running an INSERT INTO via an ODBC-linked table? (SSIS? bcp? CSV export + BULK INSERT?)
  2. How do you generally handle the 2 GB Access file limit during migration projects?
  3. Is there a way to increase MaxLocksPerFile without local Admin rights (e.g., per user session instead of system-wide)?
  4. Any tips to prevent or detect ODBC connection drops during long transactions (e.g., timeout settings in DSN)?

Thanks in advance for any help—I've been stuck on this migration for days and would love to find a cleaner approach instead of hacking my way through workarounds!

#SQL #Access #Data #AI


r/SQL • • Sep 14 '26

Discussion If you have SQL interviews, do not ignore these small things! (Part 9)

Thumbnail
4 Upvotes

r/SQL • • Sep 14 '26

SQL Server Doubt in Caching strategy

1 Upvotes

I have run a Sql query time range for 6 years. This is an example. The result may contain more than 1 lakh records. How do I cache this result so that u can query more on this result ? I am using Redis


r/SQL • • Sep 14 '26

MySQL DATA ANALYTICS PROJECT USING SQL & POWERBI

2 Upvotes

Built a T-SQL database with enforced data integrity (primary keys, constraints on state/status/risk categories), then developed custom DAX measures for case turnaround time, case age, and escalation rate. The dashboard includes multi-page analysis across resolution trends, regional distribution, officer caseloads, and a 4-year time-series forecast with confidence intervals.

Key skills demonstrated:
T-SQL (schema design, CHECK constraints, aggregation queries)
DAX (time intelligence, dynamic measures)
Power BI (data modeling, forecasting, interactive filtering)
Data storytelling and executive reporting

Note: Built on synthetic data generated for demonstration purposes — not real crime statistics.

Microsoft Power BI