use hyperQL::*;
fn main() {
println!("๐ HyperQL Query Optimizer Demo\n");
let compiler = Compiler::new();
let optimizer = QueryOptimizer::new();
let custom_config = OptimizerConfig {
enable_predicate_pushdown: true,
enable_projection_pushdown: true,
enable_constant_folding: true,
enable_expression_simplify: true,
};
let full_optimizer = QueryOptimizer::with_config(custom_config);
demo_constant_folding(&compiler, &full_optimizer);
demo_predicate_pushdown(&compiler, &full_optimizer);
demo_expression_simplification(&compiler, &full_optimizer);
demo_multi_optimization(&compiler, &full_optimizer);
println!("\nโ
Query optimizer demo completed!");
}
fn demo_constant_folding(compiler: &Compiler, optimizer: &QueryOptimizer) {
println!("๐ Constant Folding Optimization:\n");
let query = "SELECT name FROM users WHERE age > 20 + 5 AND score > 50.0 * 1.5";
println!("Original Query: {}", query);
let statement = parse_statement(query).expect("Should parse");
let unoptimized = compiler.compile(statement.clone()).expect("Should compile");
println!("\nUnoptimized Plan: {:?}", unoptimized.plan);
let (optimized, stats) = compiler.compile_with_optimizer_stats(statement, optimizer)
.expect("Should compile with optimizer");
println!("\nOptimized Plan: {:?}", optimized.plan);
println!("\nOptimizations Applied: {:?}", stats);
println!("\n๐ก The constants '20 + 5' and '50.0 * 1.5' should be folded to '25' and '75.0'");
println!("{}", "-".repeat(60));
}
fn demo_predicate_pushdown(compiler: &Compiler, optimizer: &QueryOptimizer) {
println!("\n๐ Predicate Pushdown Optimization:\n");
let query = "SELECT name, age FROM users WHERE age > 25 ORDER BY name";
println!("Original Query: {}", query);
let statement = parse_statement(query).expect("Should parse");
let (optimized, stats) = compiler.compile_with_optimizer_stats(statement, optimizer)
.expect("Should compile with optimizer");
println!("\nOptimized Plan: {:?}", optimized.plan);
println!("\nOptimizations Applied: {:?}", stats);
println!("\n๐ก The WHERE filter should be pushed down closer to the data source");
println!("{}", "-".repeat(60));
}
fn demo_expression_simplification(compiler: &Compiler, optimizer: &QueryOptimizer) {
println!("\n๐งน Expression Simplification:\n");
let query = "SELECT * FROM users WHERE age > 20 AND age > 20 AND active = true";
println!("Original Query: {}", query);
let statement = parse_statement(query).expect("Should parse");
let (optimized, stats) = compiler.compile_with_optimizer_stats(statement, optimizer)
.expect("Should compile with optimizer");
println!("\nOptimized Plan: {:?}", optimized.plan);
println!("\nOptimizations Applied: {:?}", stats);
println!("\n๐ก The duplicate 'age > 20' conditions should be simplified to a single condition");
println!("{}", "-".repeat(60));
}
fn demo_multi_optimization(compiler: &Compiler, optimizer: &QueryOptimizer) {
println!("\n๐ฏ Multi-Pass Optimization:\n");
let query = "SELECT name FROM users WHERE (age > 10 + 15) AND (score > 30 + 40) ORDER BY name LIMIT 5";
println!("Original Query: {}", query);
let statement = parse_statement(query).expect("Should parse");
let unoptimized = compiler.compile(statement.clone()).expect("Should compile");
println!("\nUnoptimized Cost: {:.2} CPU, {} estimated rows",
unoptimized.estimated_cost.estimated_cpu_cost,
unoptimized.estimated_cost.estimated_rows);
let (optimized, stats) = compiler.compile_with_optimizer_stats(statement, optimizer)
.expect("Should compile with optimizer");
println!("\nOptimized Plan: {:?}", optimized.plan);
println!("\nOptimized Cost: {:.2} CPU, {} estimated rows",
optimized.estimated_cost.estimated_cpu_cost,
optimized.estimated_cost.estimated_rows);
println!("\nOptimizations Applied: {:?}", stats);
let cpu_improvement = ((unoptimized.estimated_cost.estimated_cpu_cost
- optimized.estimated_cost.estimated_cpu_cost)
/ unoptimized.estimated_cost.estimated_cpu_cost * 100.0).max(0.0);
println!("\n๐ Performance Improvement: {:.1}% CPU cost reduction", cpu_improvement);
println!("\n๐ก Multiple optimizations working together:");
println!(" - Constants folded: '10 + 15' โ '25', '30 + 40' โ '70'");
println!(" - Predicates pushed down to scan level");
println!(" - Unnecessary projections eliminated");
println!("{}", "-".repeat(60));
}
trait Repeat {
fn repeat(&self, n: usize) -> String;
}
impl Repeat for &str {
fn repeat(&self, n: usize) -> String {
self.chars().cycle().take(n).collect()
}
}