real-rs 0.1.0

Universal query engine with relational algebra - compile the same query to PostgreSQL, SQLite, MongoDB, and YottaDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# real-rs Design Document

## Executive Summary

**real-rs** is a proof-of-concept demonstrating that relational algebra can serve as a universal abstraction layer for querying **fundamentally different** data storage systems - not just SQL variants.

The key innovation: **YottaDB backend support**. While most "universal" query engines only translate between SQL dialects, real-rs compiles relational algebra to hierarchical global storage, proving genuine universality.

## Architecture

### Three-Layer Design

```
┌─────────────────────────────────────────────┐
│         Application Layer                   │
│  (Build queries using algebra combinators)  │
└──────────────────┬──────────────────────────┘
┌──────────────────▼──────────────────────────┐
│      Relational Algebra Core                │
│  • Expr (algebra AST)                       │
│  • Predicate (boolean expressions)          │
│  • Schema (type information)                │
│  • Type inference & validation              │
└──────────────────┬──────────────────────────┘
        ┌──────────┼──────────┐
        │          │          │
┌───────▼────┐ ┌───▼──────┐ ┌▼─────────┐
│ YottaDB    │ │ SQLite   │ │ MongoDB  │
│ (M code)   │ │ (SQL)    │ │ (aggr.)  │
│ Hier. KV   │ │ Tables   │ │ Document │
└────────────┘ └──────────┘ └──────────┘
```

### Core Relational Algebra

All operations defined formally:

| Symbol | Operation | Meaning |
|--------|-----------|---------|
| σ | Selection | Filter rows by predicate |
| π | Projection | Select specific columns |
|| Join | Combine relations |
|| Union | Set union |
|| Intersection | Set intersection |
| - | Difference | Set difference |
| ρ | Rename | Rename columns |
| γ | Aggregation | Group and aggregate |

## The YottaDB Differentiator

### Why YottaDB Matters

YottaDB/GT.M uses **hierarchical global variables** instead of tables:

```m
^Users(1,"name") = "Alice"
^Users(1,"age") = 30
^Users(2,"name") = "Bob"
^Users(2,"age") = 25
```

This is fundamentally different from:
- **Relational** (rows/columns)
- **Document** (JSON trees)
- **Graph** (nodes/edges)

Supporting this proves the algebra abstraction isn't just SQL-to-SQL translation.

### Mapping Relational → Hierarchical

#### Tables → Globals

```
Relational:
┌────┬──────┬─────┐
│ id │ name │ age │
├────┼──────┼─────┤
│ 1  │Alice │ 30  │
│ 2  │ Bob  │ 25  │
└────┴──────┴─────┘

Hierarchical:
^Users(1) = [subscripts...]
^Users(1,"name") = "Alice"
^Users(1,"age") = 30
^Users(2,"name") = "Bob"
^Users(2,"age") = 25
```

#### Selection → Traversal

**Algebra:** σ(age > 25) users

**SQL:**
```sql
SELECT * FROM users WHERE age > 25
```

**M Code:**
```m
SET id=""
FOR  SET id=$ORDER(^Users(id)) QUIT:id=""  DO
. SET age=$GET(^Users(id,"age"))
. IF age>25 DO
. . ; Emit row
```

**MongoDB:**
```js
db.users.aggregate([
  { $match: { "age": { "$gt": 25 } } }
])
```

Same algebra → Three completely different execution models.

## Type Safety

### Compile-Time Schema Validation

```rust
// Schema defines structure
let users = Schema::new("users")
    .with_column("id", DataType::Integer)
    .with_column("name", DataType::String);

// Query builder knows column names
let query = Expr::relation("users", users)
    .select(col("id").eq(1))  // ✓ id exists
    .project(vec!["name"]);    // ✓ name exists

// This would fail at schema validation:
// .select(col("email").eq("x"))  // ✗ email not in schema
```

### Type Inference

```rust
let query = users_table
    .project(vec!["name", "age"]);

// Infer result schema
let result_schema = query.infer_schema();
// result_schema.columns = ["name": String, "age": Integer]
```

## Backend Trait

The universal interface:

```rust
pub trait Backend {
    type Connection;
    type CompiledQuery;

    // Translate algebra → native query
    fn compile(&self, expr: &Expr) -> Result<Self::CompiledQuery>;

    // Execute native query
    fn execute(&self, conn: &mut Self::Connection,
               query: &Self::CompiledQuery) -> Result<ResultSet>;

    // Get relation schema
    fn get_schema(&self, conn: &mut Self::Connection,
                   relation: &str) -> Result<Schema>;
}
```

This abstraction forces **true universality**. You can't fake hierarchical support.

## Comparison Matrix

| Feature | real-rs | Polars | DataFusion | Malloy | Traditional ORM |
|---------|---------|--------|------------|--------|-----------------|
| Algebra-first || ~ | ~ |||
| Hierarchical DBs ||||||
| Compile-time types || ~ | ~ || ~ |
| No Arrow runtime ||||||
| Formal semantics || ~ || ~ ||
| Backend-agnostic || ~ | ~ || ~ |

Legend:
- ✓ = Full support
- ~ = Partial support
- ✗ = No support

## What Makes This Different

### 1. Not Just SQL Variants

Most "universal" engines support:
- PostgreSQL ✓
- MySQL ✓
- SQLite ✓
- MS SQL ✓

**That's still just SQL.** They're table→table translators.

real-rs supports:
- Relational (SQLite) ✓
- Hierarchical (YottaDB) ✓
- Document (MongoDB) ✓

### 2. Algebra as Source of Truth

```
Other engines:
  SQL → Parse → Maybe optimize → Execute

real-rs:
  Algebra → Validate → Compile to backend → Execute
```

SQL becomes a **compilation target**, not the source language.

### 3. Lightweight

No dependencies on:
- Apache Arrow (multi-GB runtime)
- Complex query planners
- Distributed systems

Just:
- Relational algebra types
- Backend trait
- Compilation logic

### 4. Provable Correctness

Each operation has formal relational algebra semantics. Mission-critical systems can audit:

```
σ(P₁ ∧ P₂) R ≡ σ(P₁) (σ(P₂) R)  // Selection is commutative
π(A) (π(B) R) ≡ π(A) R if A ⊆ B  // Projection elimination
```

## Implementation Status

### ✓ Phase 1: Core (Complete)

- [x] Relational algebra types (`Expr`, `Predicate`)
- [x] Schema system with type information
- [x] Backend trait abstraction
- [x] YottaDB backend (M code generation)
- [x] SQLite backend (SQL generation)
- [x] MongoDB backend (aggregation pipeline)
- [x] Type inference
- [x] Basic tests

### ⧗ Phase 2: Completeness (Planned)

- [ ] Full YottaDB FFI (currently just M code generation)
- [ ] PostgreSQL backend
- [ ] All algebra operations (currently: σ, π, ⨝ basic)
- [ ] Aggregation (γ) with GROUP BY
- [ ] Window functions
- [ ] Subqueries
- [ ] Advanced predicates (LIKE, IN, EXISTS)

### ⧗ Phase 3: Optimization (Future)

- [ ] Query optimization passes
  - Selection pushdown
  - Projection elimination
  - Join reordering
- [ ] Cost-based backend selection
- [ ] Query plan visualization
- [ ] Macro-based DSL for ergonomics:
  ```rust
  query!(SELECT name FROM users WHERE age > 25)
  // Expands to type-safe algebra at compile time
  ```

### ⧗ Phase 4: Advanced (Research)

- [ ] Distributed backends (Cassandra, etc.)
- [ ] Query federation (join across backends!)
- [ ] Streaming/incremental evaluation
- [ ] Approximate query processing

## Technical Challenges

### 1. Impedance Mismatch

Relational algebra assumes:
- Flat relations (2D tables)
- Set semantics (no duplicates)
- Unordered rows

But backends have:
- **YottaDB**: Hierarchical keys, ordered traversal
- **MongoDB**: Nested documents, duplicate-friendly
- **SQLite**: NULL semantics, bag (not set) semantics

**Solution:** Define canonical mapping with documented semantics:
- Hierarchical → Flatten to relation on read
- Documents → Enforce schema via validation
- Bags → Remove duplicates in algebra layer if needed

### 2. Type Systems Mismatch

| Type | SQLite | MongoDB | YottaDB |
|------|--------|---------|---------|
| NULL | Native | `null` | `""` (empty string) |
| Boolean | 0/1 | `true`/`false` | 1/0 |
| Date | Text/Int | ISODate | String |

**Solution:** Canonical type system in `DataType` enum. Backends handle conversion.

### 3. Performance

Direct SQL is faster than algebra→SQL→execution.

**Mitigation:**
- Optimize algebra before compilation
- Backend-specific optimizations (e.g., SQLite can optimize compiled SQL)
- For YottaDB, generate efficient M code (minimize $ORDER calls)

### 4. Feature Coverage

Not all operations make sense everywhere:
- Hierarchical stores: How to represent JOIN?
- Document stores: How to handle relational constraints?

**Solution:** Backend capabilities matrix. Query fails at compile time if unsupported.

## Code Examples

### Basic Query

```rust
let query = Expr::relation("users", users_schema)
    .select(Predicate::Compare {
        left: ColumnRef::new("age"),
        op: CompareOp::Gt,
        right: Operand::Literal(Value::Integer(25)),
    })
    .project(vec!["name".to_string()]);
```

### Compilation

```rust
// To SQLite
let sqlite = SQLiteBackend::new();
let sql = sqlite.compile(&query)?;
println!("{}", sql.sql);
// SELECT name FROM (SELECT * FROM users WHERE age > ?)

// To YottaDB
let ydb = YottaDBBackend::new();
let m = ydb.compile(&query)?;
println!("{}", m.m_code);
// FOR  SET id=$ORDER(^Users(id)) ...
```

### Full Execution

```rust
let mut conn = Connection::open(":memory:")?;
let results = sqlite.query(&mut conn, &query)?;

for row in results {
    println!("{:?}", row);
}
```

## Future Work

### Short Term
1. Complete YottaDB FFI integration
2. Add compile-time column validation via proc macros
3. Implement all relational algebra operations
4. Query optimization passes

### Medium Term
1. Additional backends (Postgres, Redis, DuckDB)
2. Query plan introspection/visualization
3. Benchmark suite
4. Property-based testing (algebra laws)

### Long Term
1. Query federation (join SQLite + MongoDB!)
2. Distributed query execution
3. Adaptive query execution
4. Integration with Polars/Arrow for analytics

## Conclusion

**real-rs proves a controversial thesis:** Relational algebra can be a truly universal query abstraction, not just for SQL variants but for fundamentally different data models.

The YottaDB backend is the differentiator. No other "universal" query engine handles hierarchical storage.

This isn't production-ready, but it demonstrates:
1. ✓ Formal algebra works as abstraction
2. ✓ Compile-time type safety is achievable
3. ✓ Hierarchical DBs can be supported
4. ✓ Lightweight implementation is possible
5. ✓ Backend extensibility via traits

## References

- Codd, E.F. (1970). "A Relational Model of Data for Large Shared Data Banks"
- YottaDB Documentation: https://docs.yottadb.com/
- MongoDB Aggregation: https://www.mongodb.com/docs/manual/aggregation/
- SQLite Query Planner: https://www.sqlite.org/queryplanner.html
- Relational Algebra: https://en.wikipedia.org/wiki/Relational_algebra