build-a-sql-database / lesson-17.md
Lesson 17 · Parsing SQL

Comparison expressions

With atoms in hand, you can parse the comparisons at the heart of every WHERE clause. Today you combine two atoms and an operator into a comparison node.

The goal

Parse a comparison of two atoms into a binary comparison node carrying its operator.

Start here - the target
TO DO
Scenario: Parsing a comparison expression
Giventhe expression "age > 18"
Whenit is parsed
Thenthe result is a Compare node with left ColRef(age), operator ">", right IntLit(18)
And"name = 'bob'" parses to Compare(ColRef(name), =, StrLit(bob))
Background

A comparison joins two atoms with a relational operator - =, <>, <, <=, >, >= - and evaluates to true or false for a given row. Parsing one is a tidy three-step move: parse the left atom, check whether the next token is a comparison operator, and if so consume it and parse the right atom, wrapping all three in a Compare node.

Storing the operator as a small value on the node (a string or an enum) keeps the executor’s job to a single lookup later. If there is no operator after the first atom, the expression is just that atom - so this rule gracefully handles both age > 18 and a bare age. Comparisons are the predicates a WHERE filters on; next you combine several of them with AND and OR.

Make it work
type Compare struct { Left Expr; Op string; Right Expr }
// parse an atom, then if the next token is a comparison operator,
// consume it and parse the right-hand atom into a Compare node
CheckpointDONE
The parser builds comparison expressions from atoms and an operator. Commit and stop here.