|
| 1 | +use anyhow::Result; |
| 2 | +use clap::Parser; |
| 3 | +use duckdb::Connection; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +#[derive(Parser)] |
| 7 | +#[command(name = "generate-tpch")] |
| 8 | +#[command(about = "Generate TPC-H data in DuckLake format")] |
| 9 | +struct Args { |
| 10 | + /// Path for the DuckLake catalog database |
| 11 | + #[arg(short, long, default_value = "benchmark/data/tpch.ducklake")] |
| 12 | + catalog: PathBuf, |
| 13 | + |
| 14 | + /// Path for data files (Parquet storage) |
| 15 | + #[arg(short, long, default_value = "benchmark/data/tpch_files")] |
| 16 | + data_path: PathBuf, |
| 17 | + |
| 18 | + /// TPC-H scale factor (1 = 1GB, 10 = 10GB, etc.) |
| 19 | + #[arg(short, long, default_value = "1")] |
| 20 | + scale_factor: f64, |
| 21 | +} |
| 22 | + |
| 23 | +fn main() -> Result<()> { |
| 24 | + let args = Args::parse(); |
| 25 | + |
| 26 | + println!("TPC-H DuckLake Data Generator"); |
| 27 | + println!("============================="); |
| 28 | + println!("Catalog: {:?}", args.catalog); |
| 29 | + println!("Data path: {:?}", args.data_path); |
| 30 | + println!( |
| 31 | + "Scale factor: {} (~{}GB)", |
| 32 | + args.scale_factor, args.scale_factor |
| 33 | + ); |
| 34 | + println!(); |
| 35 | + |
| 36 | + // Ensure directories exist |
| 37 | + std::fs::create_dir_all(&args.data_path)?; |
| 38 | + if let Some(parent) = args.catalog.parent() { |
| 39 | + std::fs::create_dir_all(parent)?; |
| 40 | + } |
| 41 | + |
| 42 | + // Remove existing catalog if present |
| 43 | + if args.catalog.exists() { |
| 44 | + std::fs::remove_file(&args.catalog)?; |
| 45 | + } |
| 46 | + |
| 47 | + let conn = Connection::open_in_memory()?; |
| 48 | + |
| 49 | + // Install and load extensions |
| 50 | + println!("Installing extensions..."); |
| 51 | + conn.execute_batch( |
| 52 | + r#" |
| 53 | + INSTALL tpch; |
| 54 | + LOAD tpch; |
| 55 | + INSTALL ducklake; |
| 56 | + LOAD ducklake; |
| 57 | + "#, |
| 58 | + )?; |
| 59 | + |
| 60 | + // Generate TPC-H data in memory |
| 61 | + println!("Generating TPC-H data (SF={})...", args.scale_factor); |
| 62 | + conn.execute_batch(&format!("CALL dbgen(sf={})", args.scale_factor))?; |
| 63 | + |
| 64 | + // Create DuckLake catalog |
| 65 | + println!("Creating DuckLake catalog..."); |
| 66 | + let attach_sql = format!( |
| 67 | + "ATTACH '{}' AS tpch_lake (TYPE ducklake, DATA_PATH '{}')", |
| 68 | + args.catalog.display(), |
| 69 | + args.data_path.display() |
| 70 | + ); |
| 71 | + conn.execute(&attach_sql, [])?; |
| 72 | + |
| 73 | + // Create schema |
| 74 | + conn.execute("CREATE SCHEMA IF NOT EXISTS tpch_lake.main", [])?; |
| 75 | + |
| 76 | + // Copy TPC-H tables to DuckLake |
| 77 | + let tables = |
| 78 | + ["customer", "lineitem", "nation", "orders", "part", "partsupp", "region", "supplier"]; |
| 79 | + |
| 80 | + for table in &tables { |
| 81 | + println!(" Copying {} to DuckLake...", table); |
| 82 | + conn.execute_batch(&format!( |
| 83 | + "CREATE TABLE tpch_lake.main.{} AS SELECT * FROM {}", |
| 84 | + table, table |
| 85 | + ))?; |
| 86 | + } |
| 87 | + |
| 88 | + println!("\nData generation complete!"); |
| 89 | + println!("Catalog saved to: {:?}", args.catalog); |
| 90 | + |
| 91 | + // Print table statistics |
| 92 | + println!("\nTable Statistics:"); |
| 93 | + println!("-----------------"); |
| 94 | + for table in &tables { |
| 95 | + let count: i64 = conn.query_row( |
| 96 | + &format!("SELECT COUNT(*) FROM tpch_lake.main.{}", table), |
| 97 | + [], |
| 98 | + |row| row.get(0), |
| 99 | + )?; |
| 100 | + println!(" {}: {} rows", table, count); |
| 101 | + } |
| 102 | + |
| 103 | + // Print data size |
| 104 | + let total_size = dir_size(&args.data_path)?; |
| 105 | + println!( |
| 106 | + "\nTotal data size: {:.2} MB", |
| 107 | + total_size as f64 / 1_000_000.0 |
| 108 | + ); |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +fn dir_size(path: &PathBuf) -> Result<u64> { |
| 114 | + let mut size = 0; |
| 115 | + for entry in walkdir::WalkDir::new(path) { |
| 116 | + let entry = entry?; |
| 117 | + if entry.file_type().is_file() { |
| 118 | + size += entry.metadata()?.len(); |
| 119 | + } |
| 120 | + } |
| 121 | + Ok(size) |
| 122 | +} |
0 commit comments