Skip to content

IhaveDebt/Rust-SQL-High-performance-OLAP-Vector-Aggregator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

/* Rust + SQL: High-performance OLAP Vector Aggregator Single-file Rust program demonstrating OLAP aggregation with approximate analytics. */

use postgres::{Client, NoTls}; use std::collections::HashSet;

fn init_db() -> Client { let mut client = Client::connect("host=localhost user=postgres password=postgres dbname=olap_db", NoTls).unwrap(); client.batch_execute(" CREATE TABLE IF NOT EXISTS events ( id SERIAL PRIMARY KEY, category TEXT, value INT ); CREATE TABLE IF NOT EXISTS olap_cube ( category TEXT PRIMARY KEY, total INT, count_approx INT ); ").unwrap(); client }

fn seed_data(client: &mut Client) { for i in 1..=100 { let category = format!("Category{}", (i % 5) + 1); client.execute( "INSERT INTO events (category, value) VALUES ($1, $2) ON CONFLICT DO NOTHING", &[&category, &(i*10)], ).unwrap(); } }

fn build_olap_cube(client: &mut Client) { let mut cube: Vec<(String, i32, usize)> = Vec::new(); for row in client.query("SELECT category, value FROM events", &[]).unwrap() { let category: String = row.get(0); let value: i32 = row.get(1); if let Some((, total, count)) = cube.iter_mut().find(|(c,,_)| *c == category) { *total += value; *count += 1; } else { cube.push((category.clone(), value, 1)); } } // Insert into OLAP table for (cat, total, count) in cube { client.execute( "INSERT INTO olap_cube (category, total, count_approx) VALUES ($1,$2,$3) ON CONFLICT (category) DO UPDATE SET total=$2, count_approx=$3", &[&cat, &total, &(count as i32)] ).unwrap(); } }

fn query_cube(client: &mut Client) { println!("OLAP Cube Summary:"); for row in client.query("SELECT category, total, count_approx FROM olap_cube", &[]).unwrap() { let category: String = row.get(0); let total: i32 = row.get(1); let count: i32 = row.get(2); println!("{} -> total={}, approx_count={}", category, total, count); } }

fn main() { let mut client = init_db(); seed_data(&mut client); build_olap_cube(&mut client); query_cube(&mut client); }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published