|
| 1 | +use std::time::{Duration, Instant}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use log::trace; |
| 5 | +use url::Url; |
| 6 | +use vortex_duckdb_ext::duckdb::{Connection, Database}; |
| 7 | + |
| 8 | +use crate::{BenchmarkDataset, Format}; |
| 9 | + |
| 10 | +// TODO: handle S3 |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +enum DuckDBObject { |
| 14 | + Table, |
| 15 | + View, |
| 16 | +} |
| 17 | + |
| 18 | +impl DuckDBObject { |
| 19 | + fn to_str(&self) -> &str { |
| 20 | + match self { |
| 21 | + DuckDBObject::Table => "TABLE", |
| 22 | + DuckDBObject::View => "VIEW", |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// DuckDB context for benchmarks. |
| 28 | +pub struct DuckDBCtx { |
| 29 | + pub db: Database, |
| 30 | + pub connection: Connection, |
| 31 | +} |
| 32 | + |
| 33 | +impl DuckDBCtx { |
| 34 | + pub fn new() -> Result<Self> { |
| 35 | + let db = Database::open_in_memory()?; |
| 36 | + let connection = db.connect()?; |
| 37 | + vortex_duckdb_ext::init(&connection)?; |
| 38 | + Ok(Self { db, connection }) |
| 39 | + } |
| 40 | + |
| 41 | + /// Execute DuckDB queries for benchmarks using the internal connection |
| 42 | + pub fn execute_query(&self, query: &str) -> Result<Duration> { |
| 43 | + // TODO: handle multiple queries |
| 44 | + trace!("execute duckdb query: {}", query); |
| 45 | + let time_instant = Instant::now(); |
| 46 | + self.connection.execute(query)?; |
| 47 | + let query_time = time_instant.elapsed(); |
| 48 | + trace!("query completed in {:.3}s", query_time.as_secs_f64()); |
| 49 | + |
| 50 | + Ok(query_time) |
| 51 | + } |
| 52 | + |
| 53 | + /// Register tables for benchmarks using the internal connection |
| 54 | + pub fn register_tables( |
| 55 | + &self, |
| 56 | + base_url: &Url, |
| 57 | + file_format: Format, |
| 58 | + dataset: BenchmarkDataset, |
| 59 | + ) -> Result<()> { |
| 60 | + let object = match file_format { |
| 61 | + Format::Parquet | Format::OnDiskVortex => DuckDBObject::View, |
| 62 | + Format::OnDiskDuckDB => DuckDBObject::Table, |
| 63 | + format => anyhow::bail!("Format {format} isn't supported for DuckDB"), |
| 64 | + }; |
| 65 | + |
| 66 | + let load_format = match file_format { |
| 67 | + // Duckdb loads values from parquet to duckdb |
| 68 | + Format::Parquet | Format::OnDiskDuckDB => Format::Parquet, |
| 69 | + f => f, |
| 70 | + }; |
| 71 | + |
| 72 | + let effective_url = self.resolve_storage_url(base_url, load_format, dataset)?; |
| 73 | + let extension = match load_format { |
| 74 | + Format::Parquet => "parquet", |
| 75 | + Format::OnDiskVortex => "vortex", |
| 76 | + other => anyhow::bail!("Format {other} isn't supported for DuckDB"), |
| 77 | + }; |
| 78 | + |
| 79 | + // Generate and execute table registration commands |
| 80 | + let commands = self.generate_table_commands(&effective_url, extension, dataset, object); |
| 81 | + self.execute_query(&commands)?; |
| 82 | + trace!("Executing table registration commands: {}", commands); |
| 83 | + |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + /// Resolves the storage URL based on dataset and format requirements |
| 88 | + fn resolve_storage_url( |
| 89 | + &self, |
| 90 | + base_url: &Url, |
| 91 | + file_format: Format, |
| 92 | + dataset: BenchmarkDataset, |
| 93 | + ) -> Result<Url> { |
| 94 | + if file_format == Format::OnDiskVortex { |
| 95 | + match dataset.vortex_path(base_url) { |
| 96 | + Ok(vortex_url) => { |
| 97 | + // Check if the directory exists (for file:// URLs) |
| 98 | + if vortex_url.scheme() == "file" { |
| 99 | + let path = std::path::Path::new(vortex_url.path()); |
| 100 | + if !path.exists() { |
| 101 | + log::warn!( |
| 102 | + "Vortex directory doesn't exist at: {}. Run with DataFusion engine first to generate Vortex files.", |
| 103 | + path.display() |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + Ok(vortex_url) |
| 108 | + } |
| 109 | + Err(_) => Ok(base_url.clone()), |
| 110 | + } |
| 111 | + } else if file_format == Format::Parquet { |
| 112 | + match dataset.parquet_path(base_url) { |
| 113 | + Ok(parquet_url) => Ok(parquet_url), |
| 114 | + Err(_) => Ok(base_url.clone()), |
| 115 | + } |
| 116 | + } else { |
| 117 | + Ok(base_url.clone()) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Generate SQL commands for table registration. |
| 122 | + fn generate_table_commands( |
| 123 | + &self, |
| 124 | + base_url: &Url, |
| 125 | + extension: &str, |
| 126 | + dataset: BenchmarkDataset, |
| 127 | + duckdb_object: DuckDBObject, |
| 128 | + ) -> String { |
| 129 | + // Base path contains trailing /. |
| 130 | + let base_dir = base_url.as_str(); |
| 131 | + let base_dir = base_dir.strip_prefix("file://").unwrap_or(base_dir); |
| 132 | + |
| 133 | + match dataset { |
| 134 | + BenchmarkDataset::TpcH => { |
| 135 | + let mut commands = String::new(); |
| 136 | + let tables = [ |
| 137 | + "customer", "lineitem", "nation", "orders", "part", "partsupp", "region", |
| 138 | + "supplier", |
| 139 | + ]; |
| 140 | + |
| 141 | + for table_name in &tables { |
| 142 | + let table_path = format!("{base_dir}{table_name}.{extension}"); |
| 143 | + commands.push_str(&format!( |
| 144 | + "CREATE {} IF NOT EXISTS {table_name} AS SELECT * FROM read_{extension}('{table_path}');\n", |
| 145 | + duckdb_object.to_str(), |
| 146 | + )); |
| 147 | + } |
| 148 | + commands |
| 149 | + } |
| 150 | + BenchmarkDataset::ClickBench { single_file } => { |
| 151 | + let file_glob = if single_file { |
| 152 | + format!("{base_dir}hits.{extension}") |
| 153 | + } else { |
| 154 | + format!("{base_dir}*.{extension}") |
| 155 | + }; |
| 156 | + |
| 157 | + format!( |
| 158 | + "CREATE {} IF NOT EXISTS hits AS SELECT * FROM read_{extension}('{file_glob}');", |
| 159 | + duckdb_object.to_str() |
| 160 | + ) |
| 161 | + } |
| 162 | + BenchmarkDataset::TpcDS => { |
| 163 | + let mut commands = String::new(); |
| 164 | + let tables = BenchmarkDataset::TpcDS.tables(); |
| 165 | + |
| 166 | + for table_name in tables { |
| 167 | + let table_path = format!("{base_dir}{table_name}.{extension}"); |
| 168 | + commands.push_str(&format!( |
| 169 | + "CREATE {} IF NOT EXISTS {table_name} AS SELECT * FROM read_{extension}('{table_path}');\n", |
| 170 | + duckdb_object.to_str(), |
| 171 | + )); |
| 172 | + } |
| 173 | + commands |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments