Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Add SQL lexer and recursive descent parser for SELECT queries, expressions, JOINs, and aggregations.
- feat(storage): implement CSV scanning, batch slicing into typed column vectors, table catalog, and MaxScanBytes limits
- Initial project structure from the factory utility template.
10 changes: 10 additions & 0 deletions docs/PROJECT.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ Measured values only; a value that has not been measured stays "pending measurem
| audit | gemini-3.1-flash-lite | no | 0 | 0 | 0 | 0 | 0.0000 |

Total: $0.5031. Prices: Anthropic list prices.

## Factory run columnar-sql-engine-parser (measured from API usage)

| Phase | Model | Batch | Input | Output | Cache write | Cache read | Cost (USD) |
|---|---|---|---|---|---|---|---|
| generation | gemini-3.8-flash | yes | 8108 | 19854 | 0 | 0 | 0.0403 |
| fix-1 | gemini-3.8-flash | no | 53612 | 625 | 0 | 283913 | 0.0638 |
| audit | gemini-3.1-flash-lite | no | 35828 | 147 | 0 | 18268 | 0.0096 |

Total: $0.1137. Prices: Anthropic list prices.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Recursive descent SQL parser with operator precedence Pratt-style layering

Status: accepted (generated, pending human review)

## Context

The columnar SQL engine requires a zero-external-dependency, deterministic SQL lexer and recursive descent parser that constructs AST structures conforming to interfaces/parser.go while strictly enforcing query size limits and recursion depth limits.

## Decision

Implement internal/parser featuring a handwritten lexer for SQL tokens and a recursive descent parser with precedence climbing for binary/unary expressions, aggregates, joins, orderings, and projections, returning typed interfaces.EngineError on syntax, depth, or size violations.

## Consequences

Provides full AST parsing support conforming to the frozen interfaces without external dependencies, with clear error diagnostics and protective recursion depth enforcement.
271 changes: 271 additions & 0 deletions internal/parser/lexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
package parser

import (
"strings"
"unicode"
)

// Lexer tokenizes SQL source text.
type Lexer struct {
input []rune
pos int
}

// NewLexer creates a new Lexer for the given SQL string.
func NewLexer(sql string) *Lexer {
return &Lexer{
input: []rune(sql),
pos: 0,
}
}

var keywords = map[string]TokenType{
"SELECT": TokenSelect,
"FROM": TokenFrom,
"WHERE": TokenWhere,
"GROUP": TokenGroup,
"BY": TokenBy,
"HAVING": TokenHaving,
"ORDER": TokenOrder,
"ASC": TokenAsc,
"DESC": TokenDesc,
"NULLS": TokenNulls,
"FIRST": TokenFirst,
"LAST": TokenLast,
"LIMIT": TokenLimit,
"OFFSET": TokenOffset,
"JOIN": TokenJoin,
"INNER": TokenInner,
"LEFT": TokenLeft,
"RIGHT": TokenRight,
"FULL": TokenFull,
"CROSS": TokenCross,
"ON": TokenOn,
"AS": TokenAs,
"AND": TokenAnd,
"OR": TokenOr,
"NOT": TokenNot,
"IS": TokenIs,
"NULL": TokenNull,
"TRUE": TokenTrue,
"FALSE": TokenFalse,
"LIKE": TokenLike,
"CASE": TokenCase,
"WHEN": TokenWhen,
"THEN": TokenThen,
"ELSE": TokenElse,
"END": TokenEnd,
"CAST": TokenCast,
"DISTINCT": TokenDistinct,
"INT64": TokenInt64Type,
"INT": TokenInt64Type,
"INTEGER": TokenInt64Type,
"BIGINT": TokenInt64Type,
"FLOAT64": TokenFloat64Type,
"FLOAT": TokenFloat64Type,
"DOUBLE": TokenFloat64Type,
"DECIMAL": TokenDecimalType,
"VARCHAR": TokenVarCharType,
"STRING": TokenVarCharType,
"TEXT": TokenVarCharType,
"DATE": TokenDateType,
"BOOL": TokenBoolType,
"BOOLEAN": TokenBoolType,
}

// NextToken yields the next token from input.
func (l *Lexer) NextToken() Token {
l.skipWhitespaceAndComments()
if l.pos >= len(l.input) {
return Token{Type: TokenEOF, Value: "", Pos: l.pos}
}

startPos := l.pos
ch := l.input[l.pos]

switch ch {
case ',':
l.pos++
return Token{Type: TokenComma, Value: ",", Pos: startPos}
case '.':
l.pos++
return Token{Type: TokenDot, Value: ".", Pos: startPos}
case ';':
l.pos++
return Token{Type: TokenSemicolon, Value: ";", Pos: startPos}
case '(':
l.pos++
return Token{Type: TokenLeftParen, Value: "(", Pos: startPos}
case ')':
l.pos++
return Token{Type: TokenRightParen, Value: ")", Pos: startPos}
case '+':
l.pos++
return Token{Type: TokenPlus, Value: "+", Pos: startPos}
case '-':
l.pos++
return Token{Type: TokenMinus, Value: "-", Pos: startPos}
case '*':
l.pos++
return Token{Type: TokenStar, Value: "*", Pos: startPos}
case '/':
l.pos++
return Token{Type: TokenSlash, Value: "/", Pos: startPos}
case '=':
l.pos++
return Token{Type: TokenEq, Value: "=", Pos: startPos}
case '!':
if l.pos+1 < len(l.input) && l.input[l.pos+1] == '=' {
l.pos += 2
return Token{Type: TokenNotEq, Value: "!=", Pos: startPos}
}
l.pos++
return Token{Type: TokenError, Value: "!", Pos: startPos}
case '<':
if l.pos+1 < len(l.input) {
if l.input[l.pos+1] == '=' {
l.pos += 2
return Token{Type: TokenLtEq, Value: "<=", Pos: startPos}
}
if l.input[l.pos+1] == '>' {
l.pos += 2
return Token{Type: TokenNotEq, Value: "<>", Pos: startPos}
}
}
l.pos++
return Token{Type: TokenLt, Value: "<", Pos: startPos}
case '>':
if l.pos+1 < len(l.input) && l.input[l.pos+1] == '=' {
l.pos += 2
return Token{Type: TokenGtEq, Value: ">=", Pos: startPos}
}
l.pos++
return Token{Type: TokenGt, Value: ">", Pos: startPos}
case '"':
return l.readQuotedIdent('"', startPos)
case '`':
return l.readQuotedIdent('`', startPos)
default:
if ch == '\'' {
return l.readString(startPos)
}
if unicode.IsDigit(ch) {
return l.readNumber(startPos)
}
if unicode.IsLetter(ch) || ch == '_' {
return l.readIdentOrKeyword(startPos)
}
l.pos++
return Token{Type: TokenError, Value: string(ch), Pos: startPos}
}
}

func (l *Lexer) skipWhitespaceAndComments() {
for l.pos < len(l.input) {
ch := l.input[l.pos]
if unicode.IsSpace(ch) {
l.pos++
continue
}
// Single line comment --
if ch == '-' && l.pos+1 < len(l.input) && l.input[l.pos+1] == '-' {
l.pos += 2
for l.pos < len(l.input) && l.input[l.pos] != '\n' {
l.pos++
}
continue
}
// Multi line comment /* ... */
if ch == '/' && l.pos+1 < len(l.input) && l.input[l.pos+1] == '*' {
l.pos += 2
for l.pos+1 < len(l.input) && !(l.input[l.pos] == '*' && l.input[l.pos+1] == '/') {
l.pos++
}
if l.pos+1 < len(l.input) {
l.pos += 2
} else {
l.pos = len(l.input)
}
continue
}
break
}
}

func (l *Lexer) readString(startPos int) Token {
l.pos++ // skip initial '
var b strings.Builder
for l.pos < len(l.input) {
ch := l.input[l.pos]
if ch == '\'' {
if l.pos+1 < len(l.input) && l.input[l.pos+1] == '\'' {
b.WriteRune('\'')
l.pos += 2
continue
}
l.pos++
return Token{Type: TokenString, Value: b.String(), Pos: startPos}
}
b.WriteRune(ch)
l.pos++
}
return Token{Type: TokenError, Value: "unterminated string literal", Pos: startPos}
}

func (l *Lexer) readQuotedIdent(quote rune, startPos int) Token {
l.pos++ // skip leading quote
var b strings.Builder
for l.pos < len(l.input) {
ch := l.input[l.pos]
if ch == quote {
if l.pos+1 < len(l.input) && l.input[l.pos+1] == quote {
b.WriteRune(quote)
l.pos += 2
continue
}
l.pos++
return Token{Type: TokenIdent, Value: b.String(), Pos: startPos}
}
b.WriteRune(ch)
l.pos++
}
return Token{Type: TokenError, Value: "unterminated quoted identifier", Pos: startPos}
}

func (l *Lexer) readNumber(startPos int) Token {
hasDot := false
var b strings.Builder
for l.pos < len(l.input) {
ch := l.input[l.pos]
if unicode.IsDigit(ch) {
b.WriteRune(ch)
l.pos++
} else if ch == '.' && !hasDot && l.pos+1 < len(l.input) && unicode.IsDigit(l.input[l.pos+1]) {
hasDot = true
b.WriteRune('.')
l.pos++
} else {
break
}
}
return Token{Type: TokenNumber, Value: b.String(), Pos: startPos}
}

func (l *Lexer) readIdentOrKeyword(startPos int) Token {
var b strings.Builder
for l.pos < len(l.input) {
ch := l.input[l.pos]
if unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '_' {
b.WriteRune(ch)
l.pos++
} else {
break
}
}
val := b.String()
upper := strings.ToUpper(val)
if tokType, ok := keywords[upper]; ok {
return Token{Type: tokType, Value: val, Pos: startPos}
}
return Token{Type: TokenIdent, Value: val, Pos: startPos}
}
Loading
Loading