|
| 1 | +use datafusion::common::{DataFusionError, internal_datafusion_err, internal_err}; |
| 2 | +use datafusion::prelude::{ParquetReadOptions, SessionContext}; |
| 3 | +use std::fs; |
| 4 | +use std::io::Write; |
| 5 | +use std::ops::Range; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use tokio::task::JoinSet; |
| 8 | + |
| 9 | +const URL: &str = |
| 10 | + "https://datasets.clickhouse.com/hits_compatible/athena_partitioned/hits_{}.parquet"; |
| 11 | + |
| 12 | +/// Load a single ClickBench query by ID (0-42). |
| 13 | +pub fn get_test_clickbench_query(id: usize) -> Result<String, DataFusionError> { |
| 14 | + let queries_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("testdata/clickbench/queries"); |
| 15 | + |
| 16 | + if !queries_dir.exists() { |
| 17 | + return internal_err!( |
| 18 | + "TPC-DS queries directory not found: {}", |
| 19 | + queries_dir.display() |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + let query_file = queries_dir.join(format!("q{id}.sql")); |
| 24 | + |
| 25 | + if !query_file.exists() { |
| 26 | + return internal_err!("Query file not found: {}", query_file.display()); |
| 27 | + } |
| 28 | + |
| 29 | + let query_sql = fs::read_to_string(&query_file) |
| 30 | + .map_err(|e| { |
| 31 | + internal_datafusion_err!("Failed to read query file {}: {e}", query_file.display()) |
| 32 | + })? |
| 33 | + .trim() |
| 34 | + .to_string(); |
| 35 | + |
| 36 | + Ok(query_sql) |
| 37 | +} |
| 38 | + |
| 39 | +/// Downloads the datafusion-benchmarks repository as a zip file |
| 40 | +async fn download_benchmark( |
| 41 | + dest_path: PathBuf, |
| 42 | + i: usize, |
| 43 | +) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 44 | + if dest_path.exists() { |
| 45 | + return Ok(()); |
| 46 | + } |
| 47 | + |
| 48 | + // Create directory if it doesn't exist |
| 49 | + if let Some(parent) = dest_path.parent() { |
| 50 | + fs::create_dir_all(parent)?; |
| 51 | + } |
| 52 | + |
| 53 | + // Download the file |
| 54 | + let response = reqwest::get(URL.replace("{}", &i.to_string())).await?; |
| 55 | + let bytes = response.bytes().await?; |
| 56 | + |
| 57 | + // Write to file |
| 58 | + let mut file = fs::File::create(&dest_path)?; |
| 59 | + file.write_all(&bytes)?; |
| 60 | + |
| 61 | + println!("Downloaded to {}", dest_path.display()); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +async fn download_partitioned( |
| 67 | + dest_path: PathBuf, |
| 68 | + range: Range<usize>, |
| 69 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 70 | + let mut join_set = JoinSet::new(); |
| 71 | + for i in range { |
| 72 | + let dest_path = dest_path.clone(); |
| 73 | + join_set.spawn(async move { |
| 74 | + download_benchmark(dest_path.join("hits").join(format!("{i}.parquet")), i).await |
| 75 | + }); |
| 76 | + } |
| 77 | + join_set.join_all().await; |
| 78 | + Ok(()) |
| 79 | +} |
| 80 | + |
| 81 | +pub async fn generate_clickbench_data( |
| 82 | + dest_path: &Path, |
| 83 | + range: Range<usize>, |
| 84 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 85 | + download_partitioned(dest_path.to_path_buf(), range).await?; |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +pub async fn register_tables( |
| 90 | + ctx: &SessionContext, |
| 91 | + data_path: &Path, |
| 92 | +) -> Result<(), DataFusionError> { |
| 93 | + for entry in fs::read_dir(data_path)? { |
| 94 | + let path = entry?.path(); |
| 95 | + if path.is_dir() { |
| 96 | + let table_name = path.file_name().unwrap().to_str().unwrap(); |
| 97 | + ctx.register_parquet( |
| 98 | + table_name, |
| 99 | + path.to_str().unwrap(), |
| 100 | + ParquetReadOptions::default(), |
| 101 | + ) |
| 102 | + .await?; |
| 103 | + } |
| 104 | + } |
| 105 | + Ok(()) |
| 106 | +} |
0 commit comments