Skip to content

SQL front end: lexer, syntax tree and parser - #2

Merged
sahilkalgutkar merged 1 commit into
mainfrom
feature/sql-frontend
Aug 28, 2026
Merged

sahilkalgutkar merged 1 commit into
mainfrom
feature/sql-frontend

Conversation

@sahilkalgutkar

Copy link
Copy Markdown
Owner

Text in, syntax tree out. No dependencies — the lexer, the AST and the parser are all written here.

The lexer handles what separates a toy tokeniser from a usable one: "quoted identifiers" that may hold keywords or spaces, doubled quotes as escapes inside both strings and identifiers, -- line comments, exponent notation, and an integer literal too large for i64 falling back to a float instead of failing the query. Tokens carry their byte offset so parse errors can point at the text that caused them.

The parser is recursive descent for statements and precedence climbing for expressions. The precedence table is the part worth arguing about, and both non-obvious decisions have tests:

  • NOT sits between AND and comparison, so NOT a = 1 groups as NOT (a = 1) and NOT a AND b as (NOT a) AND b.
  • BETWEEN parses its bounds at comparison precedence so the AND separating them is not swallowed as a conjunction — a BETWEEN 1 AND 5 AND b = 2 has to come out as (a BETWEEN 1 AND 5) AND (b = 2), and the test asserts exactly that.

Supported grammar: SELECT [DISTINCT] with * / t.* / aliases, FROM with inner/left/right/full/cross joins (left-deep, parenthesised groups allowed), WHERE, GROUP BY, HAVING, ORDER BY with ASC/DESC/NULLS FIRST|LAST, LIMIT/OFFSET; expressions covering arithmetic, comparison, AND/OR/NOT, IS [NOT] NULL, [NOT] BETWEEN, [NOT] IN, [NOT] LIKE, CAST, CASE in both forms, and function calls with DISTINCT. Statements: CREATE TABLE, INSERT, COPY t FROM 'file.csv', DROP TABLE, EXPLAIN [ANALYZE].

Two deliberate refusals, because both are almost always mistakes rather than intent: a HAVING with neither a GROUP BY nor an aggregate is rejected with "did you mean WHERE?", and a non-cross JOIN without ON is rejected rather than silently becoming a cross product.

Verification. 65 tests, including one asserting each join flavour, one per precedence rule, and a batch of malformed queries that must produce errors naming what was expected and where.

Hand-written throughout. The lexer handles the cases that separate a toy
tokeniser from a usable one: quoted identifiers that may hold keywords or
spaces, doubled quotes as escapes inside both strings and identifiers, line
comments, exponent notation, and an integer too large for i64 falling back to
a float rather than failing the query.

The parser is recursive descent for statements and precedence climbing for
expressions. The precedence table is the part worth reading: NOT sits between
AND and comparison so `NOT a = 1` groups as `NOT (a = 1)`, and BETWEEN parses
its bounds above AND's precedence so the AND separating them is not mistaken
for a conjunction.

The tree stays purely syntactic — a column is a name the user typed, not a
position — so that resolving names against a schema happens in exactly one
place later.
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.40013% with 41 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/qf-sql/src/parser.rs 95.57% 34 Missing ⚠️
crates/qf-sql/src/ast.rs 98.37% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sahilkalgutkar
sahilkalgutkar merged commit 2fb4501 into main Aug 28, 2026
3 checks passed
@sahilkalgutkar
sahilkalgutkar deleted the feature/sql-frontend branch September 9, 2026 18:13
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.

1 participant