Documentation
¶
Overview ¶
Package engine provides SQL parsing, planning, and execution for tinySQL.
This file focuses on the query compilation cache:
- What: A lightweight in-memory cache that stores parsed/compiled representations of SQL statements (CompiledQuery).
- How: Queries are keyed by their exact SQL string. The cache holds a Statement AST plus metadata (ParsedAt) and returns it to callers to avoid re-parsing. A simple FIFO eviction based on oldest ParsedAt keeps the cache within a fixed size.
- Why: Parsing is comparatively expensive and often repeated in loops or hot paths. Caching reduces parse overhead, improves latency, and keeps the execution path predictable while remaining simple and thread-safe.
Package engine implements the tinySQL execution engine.
What: This module evaluates parsed SQL statements (AST) against the storage layer and produces ResultSets. It covers DDL/DML/SELECT, joins, grouping, aggregates, expression evaluation, simple functions (JSON_*, DATEDIFF, etc.), and a minimal tri-state boolean logic (true/false/unknown).
How: The executor converts tables to row maps with both qualified and unqualified column keys, applies WHERE/GROUP/HAVING/ORDER/LIMIT/OFFSET, and optionally combines results with UNION/EXCEPT/INTERSECT. Expression evaluation is recursive over a small algebra of literals, variables, unary/ binary ops, IS NULL, and function calls. Aggregate evaluation runs per- group with reusable helpers shared with the scalar evaluator.
Why: Keeping execution self-contained and data-structure driven (Row maps and simple slices) makes the engine easy to reason about and to extend with new functions, operators, and clauses without introducing heavy planners.
Package engine contains the SQL lexer used by the parser.
What: A minimal, whitespace- and comment-aware tokenizer that recognizes identifiers, keywords, numeric and string literals, and symbols. How: Single-pass rune-based scanner supporting -- and /* */ comments, uppercasing keywords, and preserving identifier case. Keywords are a fixed allow-list tailored to tinySQL features. Why: A compact lexer reduces parser complexity and keeps error messages local and actionable without external dependencies.
Package engine provides a hand-written SQL parser for tinySQL.
What: It parses a practical subset of SQL into an AST (statements and expressions) used by the execution engine. Supported features include DDL, DML, SELECT with JOIN/GROUP/HAVING/ORDER/LIMIT/OFFSET, and set ops. How: A straightforward recursive-descent parser over a small token stream from the lexer. It favors clarity and precise error messages. Ident-like parsing accepts keywords as identifiers to keep the grammar practical for common column names. Why: A small, readable parser is easy to extend and reason about, enabling rapid iteration on language features without a complex generator toolchain.
Index ¶
- type Binary
- type CTE
- type CompiledQuery
- type CreateTable
- type Delete
- type DropTable
- type ExecEnv
- type Expr
- type FromItem
- type FuncCall
- type Insert
- type IsNull
- type JoinClause
- type JoinType
- type Literal
- type OrderItem
- type Parser
- type QueryCache
- type ResultSet
- type Row
- type Select
- type SelectItem
- type Statement
- type Unary
- type UnionClause
- type UnionType
- type Update
- type VarRef
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompiledQuery ¶
CompiledQuery represents a pre-parsed and cached SQL query.
type CreateTable ¶
CreateTable represents a CREATE TABLE statement.
type FromItem ¶
type FromItem struct{ Table, Alias string }
FromItem binds a source table and its alias in FROM/JOIN.
type JoinClause ¶
JoinClause holds a JOIN type with the right side and join condition.
type Literal ¶
type Literal struct{ Val any }
Literal holds a constant value (number, string, bool, NULL).
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser holds the lexer and current/peek tokens for recursive-descent parsing.
func (*Parser) ParseStatement ¶
ParseStatement parses a single SQL statement into an AST.
type QueryCache ¶
type QueryCache struct {
// contains filtered or unexported fields
}
QueryCache manages compiled queries.
func NewQueryCache ¶
func NewQueryCache(maxSize int) *QueryCache
NewQueryCache creates a new query cache with the specified maximum size.
func (*QueryCache) Compile ¶
func (qc *QueryCache) Compile(sql string) (*CompiledQuery, error)
Compile parses and caches a SQL query for reuse.
func (*QueryCache) MustCompile ¶
func (qc *QueryCache) MustCompile(sql string) *CompiledQuery
MustCompile is like Compile but panics on error (similar to regexp.MustCompile).
func (*QueryCache) Size ¶
func (qc *QueryCache) Size() int
Size returns the number of cached queries.
func (*QueryCache) Stats ¶
func (qc *QueryCache) Stats() map[string]interface{}
Stats returns cache statistics.
type ResultSet ¶
ResultSet holds the column order and the returned rows from a query. Cols preserve the display order; Rows store values in a case-insensitive map.
func Execute ¶
func Execute(ctx context.Context, db *storage.DB, tenant string, stmt Statement) (*ResultSet, error)
Execute runs a parsed SQL statement against the given storage DB and tenant. It dispatches to handlers per statement kind and returns a ResultSet for SELECT (nil for DDL/DML). The context is checked at safe points to support cancellation.
type Row ¶
Row represents a single result row mapped by lower-cased column name. Keys include both qualified (table.column) and unqualified (column) names to simplify expression evaluation and projection.
type Select ¶
type Select struct { Distinct bool From FromItem Joins []JoinClause Projs []SelectItem Where Expr GroupBy []VarRef Having Expr OrderBy []OrderItem Limit *int Offset *int Union *UnionClause // For UNION operations CTEs []CTE // Common Table Expressions }
Select represents a SELECT query and its clauses.
type SelectItem ¶
SelectItem represents a projection item, optionally with alias or *.
type Statement ¶
type Statement interface{}
Statement is the root interface for all parsed SQL statements.
type UnionClause ¶
type UnionClause struct { Type UnionType Right *Select Next *UnionClause // For chaining multiple UNIONs }
UnionClause represents a set operation chaining RIGHT select with current one.