build-a-sql-database / lesson-25.md
Lesson 25 · Executing queries

Evaluating predicates

A WHERE clause is a predicate - an expression that must come out true or false. Today you extend the evaluator to comparisons and boolean operators so it can judge a row.

The goal

Evaluate comparison and AND/OR expressions against a row to a boolean result.

Start here - the target
TO DO
Scenario: Evaluating a predicate against a row
Giventhe schema (age INTEGER) and the row [30]
Whenthe predicate "age > 18" is evaluated
Thenthe result is true
And"age > 18 AND age < 25" evaluates to false for this row
And"age = 30 OR age = 40" evaluates to true
Background

A predicate is an expression whose job is to yield a truth value for a row. Today’s work extends the evaluator with the interior nodes: a Compare evaluates its two sides and applies the operator - =, <>, <, <=, >, >= - and an AND/OR evaluates both sides as booleans and combines them. Integers compare by magnitude, text compares lexicographically, and comparing two different types is simply false.

Represent the boolean however fits your engine - reusing the integer value (0 or

  1. or adding a boolean kind both work; just be consistent so AND and OR can read their operands back. This completes the evaluator: given any parsed condition and a row, it returns true or false. That is precisely the test a WHERE filter applies to every row, which you wire up in two lessons.
Make it work
// extend Eval:
// case Compare: eval Left and Right, compare by Op (=,<>,<,<=,>,>=)
// case And/Or: eval both sides as booleans, combine
// integers compare numerically; texts compare lexically
CheckpointDONE
The evaluator judges comparisons and boolean combinations - the engine can now filter. Commit and stop here.