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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! MongoDB backend - document store
//!
//! Shows how relational algebra compiles to document queries and aggregation pipelines

use crate::algebra::{CompareOp, Expr, Operand, Predicate};
use crate::backends::Backend;
use crate::schema::{DataType, ResultSet, Row, Schema, Value};
use crate::{RealError, Result};

/// Simple base64 encoding for binary data
fn base64_encode(bytes: &[u8]) -> String {
    const BASE64_CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut result = String::new();
    for chunk in bytes.chunks(3) {
        let b1 = chunk[0];
        let b2 = chunk.get(1).copied().unwrap_or(0);
        let b3 = chunk.get(2).copied().unwrap_or(0);

        result.push(BASE64_CHARS[(b1 >> 2) as usize] as char);
        result.push(BASE64_CHARS[(((b1 & 0x03) << 4) | (b2 >> 4)) as usize] as char);
        result.push(if chunk.len() > 1 {
            BASE64_CHARS[(((b2 & 0x0f) << 2) | (b3 >> 6)) as usize] as char
        } else {
            '='
        });
        result.push(if chunk.len() > 2 {
            BASE64_CHARS[(b3 & 0x3f) as usize] as char
        } else {
            '='
        });
    }
    result
}

pub struct MongoDBBackend;

/// Compiled MongoDB query (aggregation pipeline)
#[derive(Debug, Clone)]
pub struct MongoQuery {
    /// Collection name
    pub collection: String,
    /// Aggregation pipeline stages (as JSON-like representation)
    pub pipeline: Vec<MongoStage>,
    pub result_schema: Schema,
}

#[derive(Debug, Clone)]
pub enum MongoStage {
    Match(String),      // $match stage
    Project(String),    // $project stage
    Lookup(String),     // $lookup stage (join)
    Group(String),      // $group stage
    Sort(String),       // $sort stage
    Limit(usize),       // $limit stage
    Skip(usize),        // $skip stage
}

impl MongoDBBackend {
    pub fn new() -> Self {
        Self
    }

    fn compile_predicate(&self, pred: &Predicate) -> Result<String> {
        match pred {
            Predicate::Compare { left, op, right } => {
                let field = &left.name;
                let (mongo_op, value) = match (op, right) {
                    (CompareOp::Eq, Operand::Literal(v)) => ("$eq", self.value_to_json(v)),
                    (CompareOp::NotEq, Operand::Literal(v)) => ("$ne", self.value_to_json(v)),
                    (CompareOp::Lt, Operand::Literal(v)) => ("$lt", self.value_to_json(v)),
                    (CompareOp::Lte, Operand::Literal(v)) => ("$lte", self.value_to_json(v)),
                    (CompareOp::Gt, Operand::Literal(v)) => ("$gt", self.value_to_json(v)),
                    (CompareOp::Gte, Operand::Literal(v)) => ("$gte", self.value_to_json(v)),
                    (op, Operand::Column(col)) => {
                        return Ok(format!(
                            r#"{{ "{}": {{ "{}": "${}" }} }}"#,
                            field,
                            match op {
                                CompareOp::Eq => "$eq",
                                CompareOp::NotEq => "$ne",
                                CompareOp::Lt => "$lt",
                                CompareOp::Lte => "$lte",
                                CompareOp::Gt => "$gt",
                                CompareOp::Gte => "$gte",
                            },
                            col.name
                        ));
                    }
                };
                Ok(format!(r#"{{ "{}": {{ "{}": {} }} }}"#, field, mongo_op, value))
            }
            Predicate::And(left, right) => {
                let left_json = self.compile_predicate(left)?;
                let right_json = self.compile_predicate(right)?;
                Ok(format!(r#"{{ "$and": [{}, {}] }}"#, left_json, right_json))
            }
            Predicate::Or(left, right) => {
                let left_json = self.compile_predicate(left)?;
                let right_json = self.compile_predicate(right)?;
                Ok(format!(r#"{{ "$or": [{}, {}] }}"#, left_json, right_json))
            }
            Predicate::Not(inner) => {
                let inner_json = self.compile_predicate(inner)?;
                Ok(format!(r#"{{ "$not": {} }}"#, inner_json))
            }
            Predicate::In { column, values } => {
                let values_json = values
                    .iter()
                    .map(|v| self.value_to_json(v))
                    .collect::<Vec<_>>()
                    .join(", ");
                Ok(format!(r#"{{ "{}": {{ "$in": [{}] }} }}"#, column.name, values_json))
            }
            Predicate::Like { column, pattern } => {
                // MongoDB uses $regex for pattern matching
                let regex = pattern.replace('%', ".*").replace('_', ".");
                Ok(format!(r#"{{ "{}": {{ "$regex": "{}" }} }}"#, column.name, regex))
            }
            Predicate::IsNull(column) => {
                Ok(format!(r#"{{ "{}": null }}"#, column.name))
            }
            Predicate::Between { column, low, high } => {
                let low_json = self.value_to_json(low);
                let high_json = self.value_to_json(high);
                Ok(format!(
                    r#"{{ "{}": {{ "$gte": {}, "$lte": {} }} }}"#,
                    column.name, low_json, high_json
                ))
            }
        }
    }

    fn value_to_json(&self, value: &Value) -> String {
        match value {
            Value::Integer(i) => i.to_string(),
            Value::Float(f) => f.to_string(),
            Value::String(s) => format!(r#""{}""#, s),
            Value::Boolean(b) => b.to_string(),
            Value::Null => "null".to_string(),
            Value::Bytes(b) => {
                // Properly encode as BSON binary
                let base64 = base64_encode(b);
                format!(r#"{{"$binary": {{"base64": "{}", "subType": "00"}}}}"#, base64)
            }
            Value::Timestamp(ts) => {
                // MongoDB ISODate or timestamp
                format!(r#"{{"$date": {}}}"#, ts)
            }
            Value::Decimal(d) => {
                // MongoDB Decimal128
                format!(r#"{{"$numberDecimal": "{}"}}"#, d)
            }
            Value::Json(j) => {
                // Already JSON, pass through
                j.clone()
            }
            Value::Array(arr) => {
                let items: Vec<String> = arr.iter().map(|v| self.value_to_json(v)).collect();
                format!("[{}]", items.join(", "))
            }
            Value::Vector(v) => {
                let items: Vec<String> = v.iter().map(|f| f.to_string()).collect();
                format!("[{}]", items.join(", "))
            }
        }
    }
}

impl Default for MongoDBBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl Backend for MongoDBBackend {
    type Connection = (); // Would be mongodb::Client in real impl
    type CompiledQuery = MongoQuery;

    fn compile(&self, expr: &Expr) -> Result<Self::CompiledQuery> {
        match expr {
            Expr::Relation { name, schema } => Ok(MongoQuery {
                collection: name.clone(),
                pipeline: vec![],
                result_schema: schema.clone(),
            }),

            Expr::Select { input, predicate } => {
                let mut query = self.compile(input)?;
                let match_stage = self.compile_predicate(predicate)?;
                query.pipeline.push(MongoStage::Match(match_stage));
                Ok(query)
            }

            Expr::Project { input, columns } => {
                let mut query = self.compile(input)?;
                let project_fields = columns
                    .iter()
                    .map(|c| format!(r#""{}": 1"#, c))
                    .collect::<Vec<_>>()
                    .join(", ");
                let project_stage = format!(r#"{{ {} }}"#, project_fields);
                query.pipeline.push(MongoStage::Project(project_stage));
                query.result_schema = expr.infer_schema();
                Ok(query)
            }

            Expr::Join { left, right, condition } => {
                // MongoDB $lookup for joins
                let left_query = self.compile(left)?;
                let right_query = self.compile(right)?;

                // Parse join condition to extract localField and foreignField
                let (local_field, foreign_field) = match condition {
                    crate::algebra::JoinCondition::Using(cols) => {
                        if !cols.is_empty() {
                            (cols[0].clone(), cols[0].clone())
                        } else {
                            ("_id".to_string(), "_id".to_string())
                        }
                    }
                    crate::algebra::JoinCondition::On(_pred) => {
                        // Try to extract field names from predicate
                        // For now, default to _id
                        ("_id".to_string(), "_id".to_string())
                    }
                };

                let lookup = format!(
                    r#"{{ "from": "{}", "localField": "{}", "foreignField": "{}", "as": "joined" }}"#,
                    right_query.collection, local_field, foreign_field
                );

                let mut query = left_query;
                query.pipeline.push(MongoStage::Lookup(lookup));
                Ok(query)
            }

            Expr::Aggregate { input, group_by, aggregates } => {
                let mut query = self.compile(input)?;

                // Build $group stage
                let id_expr = if group_by.is_empty() {
                    "null".to_string()
                } else if group_by.len() == 1 {
                    format!(r#""${}""#, group_by[0])
                } else {
                    let fields: Vec<String> = group_by
                        .iter()
                        .map(|f| format!(r#""{}": "${}"#, f, f))
                        .collect();
                    format!(r#"{{ {} }}"#, fields.join(", "))
                };

                let agg_exprs: Vec<String> = aggregates
                    .iter()
                    .map(|agg| {
                        let mongo_func = match agg.func {
                            crate::algebra::AggregateType::Count => "$sum: 1".to_string(),
                            crate::algebra::AggregateType::Sum => format!("$sum: \"${}\"", agg.input),
                            crate::algebra::AggregateType::Avg => format!("$avg: \"${}\"", agg.input),
                            crate::algebra::AggregateType::Min => format!("$min: \"${}\"", agg.input),
                            crate::algebra::AggregateType::Max => format!("$max: \"${}\"", agg.input),
                        };
                        format!(r#""{}" {{ {} }}"#, agg.name, mongo_func)
                    })
                    .collect();

                let group_stage = format!(
                    r#"{{ "_id": {}, {} }}"#,
                    id_expr,
                    agg_exprs.join(", ")
                );

                query.pipeline.push(MongoStage::Group(group_stage));
                query.result_schema = expr.infer_schema();
                Ok(query)
            }

            Expr::Union { left, right } => {
                let left_query = self.compile(left)?;
                let right_query = self.compile(right)?;

                // MongoDB $unionWith (4.4+)
                let union_stage = format!(r#"{{ "coll": "{}" }}"#, right_query.collection);

                let mut query = left_query;
                query.pipeline.push(MongoStage::Match(union_stage));
                Ok(query)
            }

            Expr::Intersect { left, right } => {
                // MongoDB doesn't have native INTERSECT
                // Would need client-side processing or complex pipeline
                Err(RealError::Backend(
                    "MongoDB INTERSECT requires client-side processing".into(),
                ))
            }

            Expr::Difference { left, right } => {
                // MongoDB doesn't have native EXCEPT
                // Would need client-side processing or complex pipeline
                Err(RealError::Backend(
                    "MongoDB DIFFERENCE requires client-side processing".into(),
                ))
            }

            Expr::Rename { input, from, to } => {
                let mut query = self.compile(input)?;

                // Use $project to rename field
                let rename_stage = format!(
                    r#"{{ "{}": "${}", "{}": 0 }}"#,
                    to, from, from
                );

                query.pipeline.push(MongoStage::Project(rename_stage));
                query.result_schema = expr.infer_schema();
                Ok(query)
            }

            Expr::Sort { input, columns } => {
                let mut query = self.compile(input)?;

                let sort_fields = columns
                    .iter()
                    .map(|(col, order)| {
                        let order_val = match order {
                            crate::algebra::SortOrder::Asc => 1,
                            crate::algebra::SortOrder::Desc => -1,
                        };
                        format!(r#""{}": {}"#, col, order_val)
                    })
                    .collect::<Vec<_>>()
                    .join(", ");

                let sort_stage = format!(r#"{{ {} }}"#, sort_fields);
                query.pipeline.push(MongoStage::Sort(sort_stage));
                Ok(query)
            }

            Expr::Limit { input, count } => {
                let mut query = self.compile(input)?;
                query.pipeline.push(MongoStage::Limit(*count));
                Ok(query)
            }

            Expr::Offset { input, count } => {
                let mut query = self.compile(input)?;
                query.pipeline.push(MongoStage::Skip(*count));
                Ok(query)
            }

            _ => Err(RealError::Backend(format!(
                "MongoDB backend does not yet support: {:?}",
                expr
            ))),
        }
    }

    fn execute(&self, _conn: &mut Self::Connection, query: &Self::CompiledQuery) -> Result<ResultSet> {
        // In production, this would:
        // 1. Build mongodb::bson pipeline
        // 2. Execute db.collection.aggregate(pipeline)
        // 3. Parse results into ResultSet

        println!("MongoDB Query:");
        println!("  Collection: {}", query.collection);
        println!("  Pipeline:");
        for (i, stage) in query.pipeline.iter().enumerate() {
            println!("    Stage {}: {:?}", i, stage);
        }

        Ok(Vec::new())
    }

    fn get_schema(&self, _conn: &mut Self::Connection, _relation: &str) -> Result<Schema> {
        // MongoDB is schemaless, would need to infer or use schema validation
        Err(RealError::Backend(
            "MongoDB schema introspection not implemented".into(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algebra::{ColumnRef, Predicate};

    #[test]
    fn test_mongodb_compile_relation() {
        let backend = MongoDBBackend::new();
        let schema = Schema::new("users").with_column("name", DataType::String);
        let expr = Expr::relation("users", schema);

        let query = backend.compile(&expr).unwrap();
        assert_eq!(query.collection, "users");
        assert_eq!(query.pipeline.len(), 0);
    }

    #[test]
    fn test_mongodb_compile_select() {
        let backend = MongoDBBackend::new();
        let schema = Schema::new("users")
            .with_column("name", DataType::String)
            .with_column("age", DataType::Integer);

        let expr = Expr::relation("users", schema).select(Predicate::Compare {
            left: ColumnRef::new("age"),
            op: CompareOp::Gt,
            right: Operand::Literal(Value::Integer(25)),
        });

        let query = backend.compile(&expr).unwrap();
        assert_eq!(query.collection, "users");
        assert_eq!(query.pipeline.len(), 1);
        if let MongoStage::Match(ref stage) = query.pipeline[0] {
            assert!(stage.contains("$gt"));
            assert!(stage.contains("25"));
        } else {
            panic!("Expected Match stage");
        }
    }

    #[test]
    fn test_mongodb_compile_project() {
        let backend = MongoDBBackend::new();
        let schema = Schema::new("users")
            .with_column("name", DataType::String)
            .with_column("age", DataType::Integer);

        let expr = Expr::relation("users", schema).project(vec!["name".to_string()]);

        let query = backend.compile(&expr).unwrap();
        assert_eq!(query.pipeline.len(), 1);
        if let MongoStage::Project(ref stage) = query.pipeline[0] {
            assert!(stage.contains("name"));
        } else {
            panic!("Expected Project stage");
        }
    }
}