SQL front end: lexer, syntax tree and parser - #2
Merged
Merged
Conversation
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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 fori64falling 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:
NOTsits betweenANDand comparison, soNOT a = 1groups asNOT (a = 1)andNOT a AND bas(NOT a) AND b.BETWEENparses its bounds at comparison precedence so theANDseparating them is not swallowed as a conjunction —a BETWEEN 1 AND 5 AND b = 2has 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,FROMwith inner/left/right/full/cross joins (left-deep, parenthesised groups allowed),WHERE,GROUP BY,HAVING,ORDER BYwithASC/DESC/NULLS FIRST|LAST,LIMIT/OFFSET; expressions covering arithmetic, comparison,AND/OR/NOT,IS [NOT] NULL,[NOT] BETWEEN,[NOT] IN,[NOT] LIKE,CAST,CASEin both forms, and function calls withDISTINCT. 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
HAVINGwith neither aGROUP BYnor an aggregate is rejected with "did you mean WHERE?", and a non-crossJOINwithoutONis 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.