|
| 1 | +--- |
| 2 | +title: SQL |
| 3 | +description: SQL frontend — same operator pipeline, SQL syntax. |
| 4 | +--- |
| 5 | + |
| 6 | +SQL is another way into the same operator pipeline. The parser compiles SQL to a `QueryDescriptor`, which builds the same pull-based operator chain as the DataFrame API. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +```typescript |
| 11 | +import { QueryMode } from "querymode/local" |
| 12 | + |
| 13 | +const qm = QueryMode.local() |
| 14 | + |
| 15 | +const results = await qm |
| 16 | + .sql("SELECT region, SUM(amount) AS total FROM orders GROUP BY region ORDER BY total DESC") |
| 17 | + .collect() |
| 18 | +``` |
| 19 | + |
| 20 | +## SQL + DataFrame compose |
| 21 | + |
| 22 | +`.sql()` returns a `DataFrame`, so you can chain DataFrame methods after it: |
| 23 | + |
| 24 | +```typescript |
| 25 | +const results = await qm |
| 26 | + .sql("SELECT * FROM events WHERE created_at > '2026-01-01'") |
| 27 | + .filter("country", "eq", "US") |
| 28 | + .sort("amount", "desc") |
| 29 | + .limit(50) |
| 30 | + .collect() |
| 31 | +``` |
| 32 | + |
| 33 | +## Supported syntax |
| 34 | + |
| 35 | +### SELECT |
| 36 | + |
| 37 | +```sql |
| 38 | +SELECT * |
| 39 | +SELECT col1, col2 |
| 40 | +SELECT col1 AS alias |
| 41 | +SELECT DISTINCT col1, col2 |
| 42 | +SELECT COUNT(*), SUM(amount), AVG(score) |
| 43 | +``` |
| 44 | + |
| 45 | +### WHERE |
| 46 | + |
| 47 | +```sql |
| 48 | +WHERE age > 25 |
| 49 | +WHERE status = 'active' AND amount >= 100 |
| 50 | +WHERE dept = 'eng' OR age > 30 |
| 51 | +WHERE name LIKE '%alice%' |
| 52 | +WHERE id IN (1, 2, 3) |
| 53 | +WHERE id NOT IN (4, 5) |
| 54 | +WHERE amount BETWEEN 100 AND 500 |
| 55 | +WHERE email IS NULL |
| 56 | +WHERE email IS NOT NULL |
| 57 | +WHERE NOT (status = 'deleted') |
| 58 | +``` |
| 59 | + |
| 60 | +### GROUP BY / HAVING |
| 61 | + |
| 62 | +```sql |
| 63 | +GROUP BY region |
| 64 | +GROUP BY region, category |
| 65 | +HAVING SUM(amount) > 1000 |
| 66 | +``` |
| 67 | + |
| 68 | +### ORDER BY |
| 69 | + |
| 70 | +```sql |
| 71 | +ORDER BY amount DESC |
| 72 | +ORDER BY region ASC, amount DESC |
| 73 | +``` |
| 74 | + |
| 75 | +### LIMIT / OFFSET |
| 76 | + |
| 77 | +```sql |
| 78 | +LIMIT 100 |
| 79 | +LIMIT 100 OFFSET 50 |
| 80 | +``` |
| 81 | + |
| 82 | +### Expressions |
| 83 | + |
| 84 | +```sql |
| 85 | +SELECT salary / 1000 AS salary_k |
| 86 | +SELECT CASE WHEN age > 30 THEN 'senior' ELSE 'junior' END AS level |
| 87 | +SELECT CAST(age AS text) AS age_str |
| 88 | +``` |
| 89 | + |
| 90 | +### Aggregate functions |
| 91 | + |
| 92 | +`COUNT`, `SUM`, `AVG`, `MIN`, `MAX`, `COUNT(DISTINCT col)`. |
| 93 | + |
| 94 | +### JOINs |
| 95 | + |
| 96 | +```sql |
| 97 | +SELECT * FROM orders JOIN users ON orders.user_id = users.id |
| 98 | +SELECT * FROM orders LEFT JOIN users ON orders.user_id = users.id |
| 99 | +``` |
| 100 | + |
| 101 | +## How it works |
| 102 | + |
| 103 | +``` |
| 104 | +SQL string → lexer → parser → AST → compiler → QueryDescriptor → operator pipeline |
| 105 | +``` |
| 106 | + |
| 107 | +The SQL frontend is a recursive descent parser written in TypeScript. It produces an AST that the compiler maps to the same `QueryDescriptor` the DataFrame API uses. Features that can be expressed as `FilterOp[]` (simple AND conditions with eq/gt/lt/etc) are pushed down to the inner executor. Features that can't (OR, LIKE, NOT IN, HAVING, multi-column ORDER BY, CASE/CAST/arithmetic) are handled by `SqlWrappingExecutor`, which applies them on the result rows. |
| 108 | + |
| 109 | +## Programmatic access |
| 110 | + |
| 111 | +If you need the parsed AST or compiled descriptor directly: |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { parse, compile, compileFull, sqlToDescriptor } from "querymode/sql" |
| 115 | + |
| 116 | +// Parse SQL to AST |
| 117 | +const ast = parse("SELECT * FROM users WHERE age > 25") |
| 118 | + |
| 119 | +// Compile AST to QueryDescriptor |
| 120 | +const descriptor = compile(ast) |
| 121 | + |
| 122 | +// Or do both in one step |
| 123 | +const descriptor = sqlToDescriptor("SELECT * FROM users WHERE age > 25") |
| 124 | + |
| 125 | +// compileFull returns additional metadata for the wrapping executor |
| 126 | +const result = compileFull(ast) |
| 127 | +// result.descriptor — QueryDescriptor |
| 128 | +// result.whereExpr — full WHERE expression (when OR/LIKE/NOT IN present) |
| 129 | +// result.havingExpr — HAVING expression |
| 130 | +// result.allOrderBy — multi-column ORDER BY |
| 131 | +// result.computedExprs — CASE/CAST/arithmetic in SELECT |
| 132 | +``` |
0 commit comments