|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::fs; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +use anyhow::Context; |
| 9 | +use anyhow::Result; |
| 10 | +use anyhow::anyhow; |
| 11 | +use anyhow::bail; |
| 12 | +use glob::Pattern; |
| 13 | +use tracing::debug; |
| 14 | +use url::Url; |
| 15 | +use vortex::error::VortexExpect; |
| 16 | + |
| 17 | +use crate::Benchmark; |
| 18 | +use crate::BenchmarkDataset; |
| 19 | +use crate::Format; |
| 20 | +use crate::TableSpec; |
| 21 | +use crate::utils::file::data_dir; |
| 22 | + |
| 23 | +// Path to script that creates Parquet data |
| 24 | +const INIT_SQL: &str = "init.sql"; |
| 25 | + |
| 26 | +pub struct VortexBenchmark { |
| 27 | + data_url: Url, |
| 28 | + queries_dir: PathBuf, |
| 29 | + query: Option<PathBuf>, |
| 30 | +} |
| 31 | + |
| 32 | +impl VortexBenchmark { |
| 33 | + pub fn new() -> Result<Self> { |
| 34 | + let queries_dir = data_dir().join("vortex"); |
| 35 | + let data_url = Url::from_directory_path(&queries_dir) |
| 36 | + .map_err(|_| anyhow!("cannot build URL for {}", queries_dir.display()))?; |
| 37 | + Ok(Self { |
| 38 | + data_url, |
| 39 | + queries_dir, |
| 40 | + query: None, |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + // Same as new(), but run only for "query" SQL file |
| 45 | + pub fn with_query(mut self, query: &str) -> Result<Self> { |
| 46 | + let as_path = PathBuf::from(query); |
| 47 | + let path = if as_path.is_file() { |
| 48 | + as_path |
| 49 | + } else if query.ends_with(".sql") { |
| 50 | + self.queries_dir.join(query) |
| 51 | + } else { |
| 52 | + self.queries_dir.join(format!("{query}.sql")) |
| 53 | + }; |
| 54 | + if !path.is_file() { |
| 55 | + bail!("{query} file not found in {}", self.queries_dir.display()); |
| 56 | + } |
| 57 | + self.query = Some(path); |
| 58 | + Ok(self) |
| 59 | + } |
| 60 | + |
| 61 | + fn query_files(&self) -> Result<Vec<PathBuf>> { |
| 62 | + if let Some(query) = &self.query { |
| 63 | + return Ok(vec![query.clone()]); |
| 64 | + } |
| 65 | + |
| 66 | + let entries = fs::read_dir(&self.queries_dir) |
| 67 | + .with_context(|| format!("cannot list queries in {}", self.queries_dir.display()))? |
| 68 | + .collect::<std::io::Result<Vec<_>>>()?; |
| 69 | + |
| 70 | + let mut files: Vec<PathBuf> = entries |
| 71 | + .into_iter() |
| 72 | + .map(|entry| entry.path()) |
| 73 | + .filter(|path| { |
| 74 | + path.extension().is_some_and(|ext| ext == "sql") |
| 75 | + && path.file_name().is_some_and(|name| name != INIT_SQL) |
| 76 | + }) |
| 77 | + .collect(); |
| 78 | + files.sort(); |
| 79 | + |
| 80 | + if files.is_empty() { |
| 81 | + bail!("no query files found in {}", self.queries_dir.display()); |
| 82 | + } |
| 83 | + Ok(files) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[async_trait::async_trait] |
| 88 | +impl Benchmark for VortexBenchmark { |
| 89 | + fn queries(&self) -> Result<Vec<(usize, String)>> { |
| 90 | + self.query_files()? |
| 91 | + .iter() |
| 92 | + .map(|path| { |
| 93 | + let idx: usize = path |
| 94 | + .to_string_lossy() |
| 95 | + .split_once("_") |
| 96 | + .vortex_expect("query without a number") |
| 97 | + .0 |
| 98 | + .parse::<usize>()?; |
| 99 | + let query = fs::read_to_string(path) |
| 100 | + .with_context(|| format!("cannot read query {}", path.display()))? |
| 101 | + .replace("test.vortex", "$1"); |
| 102 | + debug!(idx, file = %path.display(), "Loaded vortex query"); |
| 103 | + Ok((idx, query)) |
| 104 | + }) |
| 105 | + .collect() |
| 106 | + } |
| 107 | + |
| 108 | + async fn generate_base_data(&self) -> Result<()> { |
| 109 | + let parquet_dir = self.queries_dir.join(Format::Parquet.name()); |
| 110 | + fs::create_dir_all(&parquet_dir)?; |
| 111 | + let parquet_file = parquet_dir.join(format!("test.parquet")); |
| 112 | + |
| 113 | + if parquet_file.exists() { |
| 114 | + debug!("Parquet data present in {}", parquet_dir.display()); |
| 115 | + return Ok(()); |
| 116 | + } |
| 117 | + |
| 118 | + let init_path = self.queries_dir.join(INIT_SQL); |
| 119 | + let script = fs::read_to_string(&init_path) |
| 120 | + .with_context(|| format!("cannot read {}", init_path.display()))?; |
| 121 | + |
| 122 | + let output = Command::new("duckdb") |
| 123 | + .current_dir(&parquet_dir) |
| 124 | + .arg("-c") |
| 125 | + .arg(&script) |
| 126 | + .output() |
| 127 | + .context("cannot run duckdb")?; |
| 128 | + if !output.status.success() { |
| 129 | + bail!( |
| 130 | + "duckdb {INIT_SQL} failed: stdout={:?} stderr={:?}", |
| 131 | + String::from_utf8_lossy(&output.stdout), |
| 132 | + String::from_utf8_lossy(&output.stderr), |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + if !parquet_file.exists() { |
| 137 | + bail!("{INIT_SQL} did not create Parquet files"); |
| 138 | + } |
| 139 | + |
| 140 | + debug!("Parquet data generated in {}", parquet_dir.display()); |
| 141 | + Ok(()) |
| 142 | + } |
| 143 | + |
| 144 | + fn dataset(&self) -> BenchmarkDataset { |
| 145 | + BenchmarkDataset::VortexQueries |
| 146 | + } |
| 147 | + |
| 148 | + fn dataset_name(&self) -> &str { |
| 149 | + "vortex" |
| 150 | + } |
| 151 | + |
| 152 | + fn dataset_display(&self) -> String { |
| 153 | + "vortex".to_owned() |
| 154 | + } |
| 155 | + |
| 156 | + fn data_url(&self) -> &Url { |
| 157 | + &self.data_url |
| 158 | + } |
| 159 | + |
| 160 | + fn table_specs(&self) -> Vec<TableSpec> { |
| 161 | + vec![TableSpec::new("test", None)] |
| 162 | + } |
| 163 | + |
| 164 | + fn pattern(&self, table_name: &str, format: Format) -> Option<Pattern> { |
| 165 | + Some( |
| 166 | + Pattern::new(&format!("{table_name}.{}", format.ext())) |
| 167 | + .expect("table name is a valid identifier"), |
| 168 | + ) |
| 169 | + } |
| 170 | +} |
0 commit comments