← Back to all stagesStage 1
Stage 20: Advanced WHERE Expressions
Concept
Extend the WHERE clause evaluation from simple column = value to support comparison operators, logical connectives, and pattern matching.
What It Teaches
- Expression trees: Representing
WHERE a > 5 AND name LIKE 'dan%'as a tree of operator nodes that can be recursively evaluated. - Type-aware comparison: Comparing INT values numerically and VARCHAR values lexicographically.
- Short-circuit evaluation:
ANDstops evaluating if the left side is false;ORstops if the left side is true. - Pattern matching: Implementing
LIKEwith%(any sequence) and_(single character) wildcards.
Learning Objectives
- Extend the parser to recognize
>,<,>=,<=,!=,AND,OR,LIKE,IS NULL,IS NOT NULL. - Build an expression AST node that supports binary operators and unary operators.
- Implement
evaluate_expression(row, expr) → boolthat recursively evaluates the expression tree against a row. - Integrate expression evaluation into the SeqScan and IndexScan filter logic.
- Support
IS NULLfor detecting empty/missing values.
New SQL Syntax
SELECT * FROM users WHERE id > 5;
SELECT * FROM users WHERE id >= 1 AND id <= 10;
SELECT * FROM users WHERE name LIKE 'dan%';
SELECT * FROM users WHERE email IS NOT NULL;
SELECT * FROM users WHERE id > 3 OR name = 'alice';