SQL frontend
Here “frontend” means the command-line and SQL-processing side of the engine. It accepts text, understands a statement, calls typed engine interfaces, and formats the result.
From input to response
flowchart TB
A([Arguments and stdin]) --> MODE{Startup mode}
MODE -->|init| INIT[Reset only selected data root]
MODE -->|import -f -t -d| IMPORT[Call shared CSV importer]
MODE -->|interactive or batch| READ[Read complete statement]
READ --> PARSE[Lexer and parser]
PARSE --> AST[Statement AST]
AST --> BIND[Resolve database, tables, columns and types]
BIND --> PLAN{Statement category}
PLAN -->|DDL| DDL[Catalog and schema executor]
PLAN -->|INSERT UPDATE DELETE| MUT[Constraint-aware write executor]
PLAN -->|SELECT| QUERY[Access plan, filter, join, projection]
DDL --> RES[Typed Result or Error]
MUT --> RES
QUERY --> RES
IMPORT --> RES
RES --> FORMAT{Output mode}
FORMAT -->|batch| CSV[CSV, @ separator, flush]
FORMAT -->|interactive| HUMAN[Readable table and diagnostics]
CSV --> READ
HUMAN --> READ
Parsing and binding are different tasks
Parsing identifies structure: a table name, projection, comparison and literal. Binding checks meaning against the current catalog: whether the table exists, which column an identifier refers to, and whether operand types match. Keep parser tests independent of disk files by building AST values directly.
The course SQL.g4 includes required and optional syntax. Grammar support alone does not mean an executor is implemented. Each optional feature has its own issue and must be explicitly selected.
Required command families
| Family | Separate specifications |
|---|---|
| Databases | DB-001 CREATE, DB-002 USE, DB-003 SHOW, DB-004 DROP |
| Tables | TABLE-001 CREATE, TABLE-002 SHOW, TABLE-003 DESC, TABLE-004 DROP |
| Row changes | DML-001 single INSERT, DML-002 multiple INSERT, DML-003 UPDATE, DML-004 DELETE |
| Queries | QUERY-001 scan SELECT, QUERY-002 projection, QUERY-003 WHERE, QUERY-004 two-table join |
| Schema changes | ALTER-001/002 primary key, ALTER-003/004 foreign key, INDEX-004 explicit indexes |
| Loading | IMPORT-001 SQL LOAD, IMPORT-002 CLI import |
SHOW INDEXES appears in the supplied grammar but has no dedicated public test. INDEX-005 proposes a team output format rather than inventing a course requirement.
One result type, two formatters
Executors return structured results rather than writing stdout. Batch results follow these rules:
- SELECT always emits a header, even when empty. Projection order follows the SELECT list.
- SHOW DATABASES / TABLES use DATABASES / TABLES headers.
- INSERT, UPDATE, DELETE and LOAD emit
rowsand their affected count, including zero. - UPDATE counts values that actually changed.
- FLOAT is stored as double and printed with two decimal places.
- Every statement ends with a line beginning
@, then flushes stdout immediately. - Diagnostics unrelated to graded output go to stderr.
rows
1
@
Errors and rejected changes
flowchart LR
W[Requested write] --> V{Validate first}
V -->|invalid| E[Typed error; original state retained]
V -->|valid| APPLY[Coordinate row and all indexes]
APPLY --> R[Success result]
E --> F[Result formatter]
R --> F
Constraint errors use !ERROR with a diagnostic containing the appropriate keyword: duplicate, foreign, or primary. Do not include unrelated constraint keywords. A rejected row change must not leave a new record without an index entry or an index pointing at a nonexistent record.
DESC has a separate formatter: Field,Type,Null,Default rows; a blank line; primary, foreign, optional unique, and explicit index lines, each ending in a semicolon. It hides indexes created internally to support keys.
These formatting requirements are defined by the official frontend contract.