-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathlib.rs
More file actions
442 lines (399 loc) · 13.3 KB
/
lib.rs
File metadata and controls
442 lines (399 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
use std::clone::Clone;
use std::fmt::Display;
use std::str::FromStr;
use std::sync::LazyLock;
use anyhow::bail;
use clap::ValueEnum;
use clickbench::ClickBenchBenchmark;
use clickbench::Flavor;
use fineweb::FinewebBenchmark;
use itertools::Itertools;
use polarsignals::PolarSignalsBenchmark;
use public_bi::PBIDataset;
use public_bi::PublicBiBenchmark;
use realnest::gharchive::GithubArchiveBenchmark;
use serde::Deserialize;
use serde::Serialize;
use statpopgen::StatPopGenBenchmark;
use tpcds::TpcDsBenchmark;
use tpch::benchmark::TpcHBenchmark;
pub use utils::file::*;
pub use utils::logging::*;
use vortex::error::VortexExpect;
use vortex::error::vortex_err;
use vortex::file::VortexWriteOptions;
use vortex::file::WriteStrategyBuilder;
use vortex::utils::aliases::hash_map::HashMap;
pub mod benchmark;
pub mod clickbench;
pub mod compress;
pub mod conversions;
pub mod datasets;
pub mod display;
pub mod downloadable_dataset;
pub mod fineweb;
pub mod measurements;
pub mod memory;
pub mod output;
pub mod polarsignals;
pub mod public_bi;
pub mod random_access;
pub mod realnest;
pub mod runner;
pub mod statpopgen;
pub mod tpcds;
pub mod tpch;
pub mod utils;
pub use benchmark::Benchmark;
pub use benchmark::TableSpec;
pub use datasets::BenchmarkDataset;
pub use output::BenchmarkOutput;
pub use output::create_output_writer;
use vortex::VortexSessionDefault;
pub use vortex::error::vortex_panic;
use vortex::io::session::RuntimeSessionExt;
use vortex::session::VortexSession;
// All benchmarks run with mimalloc for consistency.
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
pub static SESSION: LazyLock<VortexSession> =
LazyLock::new(|| VortexSession::default().with_tokio());
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
pub engine: Engine,
pub format: Format,
}
impl FromStr for Target {
type Err = anyhow::Error;
fn from_str(target_string: &str) -> Result<Self, Self::Err> {
let split = target_string.split(":").collect_vec();
let [engine_str, format_str] = split.as_slice() else {
vortex_panic!("invalid target string {}", target_string);
};
Ok(Self {
engine: Engine::from_str(engine_str, true)
.map_err(|e| {
vortex_err!(
"cannot convert str ({}) to an Engine oneof([{}]), got error {}",
*engine_str,
Engine::value_variants().iter().join(","),
e
)
})
.vortex_expect("operation should succeed in benchmark"),
format: Format::from_str(format_str, true)
.map_err(|e| {
vortex_err!(
"cannot convert str ({}) to a Format oneof([{}]), got error {}",
*format_str,
Format::value_variants().iter().join(","),
e
)
})
.vortex_expect("operation should succeed in benchmark"),
})
}
}
impl Target {
pub fn new(engine: Engine, format: Format) -> Self {
Self { engine, format }
}
}
impl Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.engine, self.format)
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, ValueEnum, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Format {
#[clap(name = "csv")]
Csv,
#[clap(name = "arrow")]
Arrow,
#[clap(name = "parquet")]
Parquet,
#[clap(name = "vortex")]
#[serde(rename = "vortex")]
OnDiskVortex,
#[clap(name = "vortex-compact")]
#[serde(rename = "vortex-compact")]
VortexCompact,
#[clap(name = "vortex-cuda")]
#[serde(rename = "vortex-cuda")]
VortexCuda,
#[clap(name = "duckdb")]
#[serde(rename = "duckdb")]
OnDiskDuckDB,
#[clap(name = "lance")]
#[serde(rename = "lance")]
Lance,
}
impl Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
/// Allowed formats for benchmark CLI arguments.
pub const ALLOWED_FORMATS: &[Format] = &[Format::Parquet, Format::OnDiskVortex, Format::Lance];
impl Format {
/// Clap value parser that only accepts parquet, vortex, and lance.
pub fn parse_allowed(s: &str) -> Result<Format, String> {
let format = Format::from_str(s, true)?;
if ALLOWED_FORMATS.contains(&format) {
Ok(format)
} else {
Err(format!(
"invalid format '{}': allowed values are [{}]",
s,
ALLOWED_FORMATS.iter().map(|f| f.to_string()).join(", "),
))
}
}
pub fn name(&self) -> &'static str {
match self {
Format::Csv => "csv",
Format::Arrow => "arrow",
Format::Parquet => "parquet",
Format::OnDiskVortex => "vortex-file-compressed",
Format::VortexCompact => "vortex-compact",
Format::VortexCuda => "vortex-cuda",
Format::OnDiskDuckDB => "duckdb",
Format::Lance => "lance",
}
}
pub fn ext(&self) -> &'static str {
match self {
Format::Csv => "csv",
Format::Arrow => "arrow",
Format::Parquet => "parquet",
Format::OnDiskVortex => "vortex",
Format::VortexCompact => "vortex",
Format::VortexCuda => "vortex",
Format::OnDiskDuckDB => "duckdb",
Format::Lance => "lance",
}
}
}
#[derive(ValueEnum, Clone, Copy, Debug, Hash, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Engine {
#[default]
Vortex,
Arrow,
#[clap(name = "datafusion")]
#[serde(rename = "datafusion")]
DataFusion,
#[clap(name = "duckdb")]
#[serde(rename = "duckdb")]
DuckDB,
}
impl Display for Engine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Engine::DataFusion => write!(f, "datafusion"),
Engine::DuckDB => write!(f, "duckdb"),
Engine::Vortex => write!(f, "vortex"),
Engine::Arrow => write!(f, "arrow"),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub enum CompactionStrategy {
Compact,
CudaCompatible,
#[default]
Default,
}
impl CompactionStrategy {
pub fn apply_options(&self, options: VortexWriteOptions) -> VortexWriteOptions {
const CUDA_COALESCING_TARGET_BYTES: u64 = 128 * 1024 * 1024;
match self {
CompactionStrategy::Compact => options.with_strategy(
WriteStrategyBuilder::default()
.with_compact_encodings()
.build(),
),
CompactionStrategy::CudaCompatible => options.with_strategy(
WriteStrategyBuilder::default()
.with_cuda_compatible_encodings()
.with_coalescing_block_size(CUDA_COALESCING_TARGET_BYTES)
.build(),
),
CompactionStrategy::Default => options,
}
}
}
/// CLI argument for selecting which benchmark to run.
#[derive(clap::ValueEnum, Clone, Copy)]
pub enum BenchmarkArg {
#[clap(name = "clickbench")]
ClickBench,
#[clap(name = "tpch")]
TpcH,
#[clap(name = "tpcds")]
TpcDS,
#[clap(name = "statpopgen")]
StatPopGen,
#[clap(name = "fineweb")]
Fineweb,
#[clap(name = "gharchive")]
GhArchive,
#[clap(name = "polarsignals")]
PolarSignals,
#[clap(name = "public-bi")]
PublicBi,
}
/// Default scale factor for TPC-related benchmarks
const DEFAULT_SCALE_FACTOR: &str = "1.0";
const SCALE_FACTOR_KEY: &str = "scale-factor";
const REMOTE_DATA_KEY: &str = "remote-data-dir";
/// Factory function to create a benchmark instance from CLI arguments.
pub fn create_benchmark(b: BenchmarkArg, opts: &Opts) -> anyhow::Result<Box<dyn Benchmark>> {
match b {
BenchmarkArg::ClickBench => {
let flavor = opts.get_as::<Flavor>("flavor").unwrap_or_default();
let remote_data_dir = opts.get_as::<String>(REMOTE_DATA_KEY);
let benchmark = ClickBenchBenchmark::new(flavor, None, remote_data_dir)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::TpcH => {
let scale_factor = opts.get(SCALE_FACTOR_KEY).unwrap_or(DEFAULT_SCALE_FACTOR);
let remote_data_dir = opts.get_as::<String>(REMOTE_DATA_KEY);
let benchmark = TpcHBenchmark::new(scale_factor.to_string(), remote_data_dir)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::TpcDS => {
let scale_factor = opts.get(SCALE_FACTOR_KEY).unwrap_or(DEFAULT_SCALE_FACTOR);
let remote_data_dir = opts.get_as::<String>(REMOTE_DATA_KEY);
let benchmark = TpcDsBenchmark::new(scale_factor.to_string(), remote_data_dir)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::StatPopGen => {
let scale_factor = opts.get_as::<u64>(SCALE_FACTOR_KEY).unwrap_or(1);
let benchmark = StatPopGenBenchmark::new(scale_factor)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::Fineweb => {
let remote_data_dir = opts.get_as::<String>(REMOTE_DATA_KEY);
let benchmark = FinewebBenchmark::with_remote_data_dir(remote_data_dir)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::GhArchive => {
let remote_data_dir = opts.get_as::<String>(REMOTE_DATA_KEY);
let benchmark = GithubArchiveBenchmark::with_remote_data_dir(remote_data_dir)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::PolarSignals => {
let scale_factor = opts.get_as::<usize>(SCALE_FACTOR_KEY).unwrap_or(1);
let benchmark = PolarSignalsBenchmark::new(scale_factor)?;
Ok(Box::new(benchmark) as _)
}
BenchmarkArg::PublicBi => {
let dataset = opts.get_as::<PBIDataset>("dataset").ok_or_else(|| {
anyhow::anyhow!("public-bi benchmark requires --opt dataset=<name>")
})?;
let benchmark = PublicBiBenchmark::new(dataset)?;
Ok(Box::new(benchmark) as _)
}
}
}
/// A single key-value option for benchmark configuration.
#[derive(Clone, Debug)]
pub struct Opt {
key: String,
value: String,
}
/// Collection of benchmark configuration options.
pub struct Opts {
inner: HashMap<String, String>,
}
impl Opts {
pub fn get(&self, key: &str) -> Option<&str> {
self.inner.get(key).map(|s| s.as_str())
}
#[expect(clippy::panic)]
pub fn get_as<T>(&self, key: &str) -> Option<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
self.inner.get(key).map(|v| {
v.parse().unwrap_or_else(|_| {
panic!("opts value {key} was parsed into an inappropriate type")
})
})
}
}
impl From<Vec<Opt>> for Opts {
fn from(value: Vec<Opt>) -> Self {
value.into_iter().collect()
}
}
impl FromIterator<Opt> for Opts {
fn from_iter<T: IntoIterator<Item = Opt>>(iter: T) -> Self {
let inner = HashMap::from_iter(iter.into_iter().map(|o| (o.key, o.value)));
Self { inner }
}
}
impl Display for Opt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}={}", self.key, self.value)
}
}
impl FromStr for Opt {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split = s.split([' ', '=']).collect::<Vec<_>>();
let [key, value] = split.as_slice() else {
bail!("invalid option: {}", s);
};
let opt = Opt {
key: key.to_string(),
value: value.to_string(),
};
Ok(opt)
}
}
/// Generate SQL commands to create DuckDB tables/views from data files.
///
/// # Arguments
/// * `benchmark` - The benchmark providing table specs and patterns
/// * `base_dir` - Base directory path (without trailing slash)
/// * `load_format` - The format to load from (determines file extension)
/// * `object_type` - Either "TABLE" or "VIEW"
pub fn generate_duckdb_registration_sql<B>(
benchmark: &B,
base_dir: &str,
load_format: Format,
object_type: &str,
) -> Vec<String>
where
B: Benchmark + ?Sized,
{
let extension = load_format.ext();
let mut sql_statements = Vec::new();
for table_spec in benchmark.table_specs() {
let name = table_spec.name;
let pattern = benchmark
.pattern(name, load_format)
.map(|p| p.to_string())
.unwrap_or_else(|| format!("*.{}", extension));
tracing::info!(
name,
base_dir,
pattern,
format = load_format.name(),
"Registering DuckDB {}",
object_type.to_lowercase()
);
sql_statements.push(format!(
"CREATE {object_type} IF NOT EXISTS {name} AS SELECT * FROM read_{extension}('{base_dir}/{pattern}');\n",
));
}
sql_statements
}