use real_rs::algebra::{ColumnRef, CompareOp, Expr, Operand, Predicate};
use real_rs::backends::yottadb::{GlobalMapping, KeyStructure, YDBConnection, YottaDBBackend};
use real_rs::backends::Backend;
use real_rs::schema::{DataType, Schema, Value};
#[cfg(feature = "backend-sqlite")]
use real_rs::backends::sqlite::SQLiteBackend;
#[cfg(feature = "backend-mongodb")]
use real_rs::backends::mongodb::MongoDBBackend;
fn main() {
println!("=== Universal Query Example ===\n");
let users_schema = Schema::new("users")
.with_column("id", DataType::Integer)
.with_column("name", DataType::String)
.with_column("age", DataType::Integer)
.with_column("email", DataType::String);
let query = Expr::relation("users", users_schema.clone())
.select(Predicate::Compare {
left: ColumnRef::new("age"),
op: CompareOp::Gt,
right: Operand::Literal(Value::Integer(25)),
})
.project(vec!["name".to_string(), "email".to_string()]);
println!("Relational Algebra Expression:");
println!(" π(name, email) σ(age > 25) users\n");
println!("--- YottaDB Backend (Hierarchical) ---");
let mut ydb_backend = YottaDBBackend::new();
ydb_backend.register_global(
"users".to_string(),
GlobalMapping {
global: "^Users".to_string(),
schema: users_schema.clone(),
key_structure: KeyStructure::Simple {
id_column: "id".to_string(),
},
},
);
match ydb_backend.compile(&query) {
Ok(ydb_query) => {
println!("Compiled to M code:");
println!("{}", ydb_query.m_code);
println!();
}
Err(e) => println!("Error: {}\n", e),
}
#[cfg(feature = "backend-sqlite")]
{
println!("--- SQLite Backend (Relational) ---");
let sqlite_backend = SQLiteBackend::new();
match sqlite_backend.compile(&query) {
Ok(sql_query) => {
println!("Compiled to SQL:");
println!(" {}", sql_query.sql);
println!(" Params: {:?}", sql_query.params);
println!();
}
Err(e) => println!("Error: {}\n", e),
}
}
#[cfg(not(feature = "backend-sqlite"))]
println!("--- SQLite Backend (not enabled) ---\n");
#[cfg(feature = "backend-mongodb")]
{
println!("--- MongoDB Backend (Document Store) ---");
let mongo_backend = MongoDBBackend::new();
match mongo_backend.compile(&query) {
Ok(mongo_query) => {
println!("Compiled to MongoDB aggregation pipeline:");
println!(" db.{}.aggregate([", mongo_query.collection);
for (i, stage) in mongo_query.pipeline.iter().enumerate() {
println!(" // Stage {}", i);
match stage {
real_rs::backends::mongodb::MongoStage::Match(s) => {
println!(" {{ $match: {} }},", s)
}
real_rs::backends::mongodb::MongoStage::Project(s) => {
println!(" {{ $project: {} }},", s)
}
_ => println!(" {:?},", stage),
}
}
println!(" ])");
println!();
}
Err(e) => println!("Error: {}\n", e),
}
}
#[cfg(not(feature = "backend-mongodb"))]
println!("--- MongoDB Backend (not enabled) ---\n");
println!("\n=== Key Differentiators ===");
println!("1. Algebra-first: SQL is a compile target, not source");
println!("2. Compile-time correctness: Schema validated before runtime");
println!("3. Truly universal: YottaDB proves it handles hierarchical stores");
println!("4. Lightweight: No Arrow, no heavy runtime dependencies");
println!("5. Formal semantics: Each backend faithfully implements relational algebra");
println!("\nRun with features:");
println!(" cargo run --example universal_query --features backend-sqlite,backend-mongodb");
}