clickhouse-arrow 0.2.1

ClickHouse Arrow Client for Rust
Documentation
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
#![expect(unused_crate_dependencies)]
#![allow(
    unused_results,
    clippy::uninlined_format_args,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::doc_markdown,
    clippy::manual_is_multiple_of,
    clippy::unused_enumerate_index,
    clippy::manual_div_ceil,
    clippy::too_many_lines,
    clippy::unused_self,
    clippy::cloned_instead_of_copied
)]
mod common;

use std::time::Instant;

use clickhouse_arrow::prelude::*;
use clickhouse_arrow::test_utils::{ClickHouseContainer, arrow_tests};
use comfy_table::Table;
use comfy_table::presets::UTF8_FULL;
use common::scale_utils::{
    calculate_bytes_per_row, insert_concurrent, print_params_table, print_schema_config,
    print_schema_summary, setup_benchmark_client,
};
use futures_util::StreamExt;

const CONV_THRESHOLD: f64 = 0.05; // 5% improvement threshold for convergence

// Configuration to test
#[derive(Debug, Clone, Copy, PartialEq)]
struct Config {
    workers:    usize,
    batch_size: usize,
}

// Benchmark result for a configuration
#[derive(Debug, Clone)]
struct BenchmarkResult {
    config:          Config,
    #[allow(dead_code)]
    durations:       Vec<f64>, // All iteration times (seconds) - kept for debugging
    avg_throughput:  f64, // Average rows/sec
    best_throughput: f64, // Best rows/sec
    variance:        f64, // Coefficient of variation
}

impl BenchmarkResult {
    fn score(&self) -> f64 {
        // Score = best_throughput with penalty for high variance
        // Lower variance = more predictable = better for production
        self.best_throughput * (1.0 - self.variance.min(0.5))
    }
}

// SGD-like optimizer for finding optimal configuration
struct Optimizer {
    #[allow(dead_code)]
    total_rows: usize, // Kept for future heuristics
    history:    Vec<BenchmarkResult>,
    iteration:  usize,
}

impl Optimizer {
    fn new(total_rows: usize) -> Self { Self { total_rows, history: Vec::new(), iteration: 0 } }

    /// Initial guesses using heuristics
    fn initial_guesses(&self) -> Vec<Config> {
        vec![
            // Conservative: low workers, small batches
            Config { workers: 4, batch_size: 2_000 },
            // Balanced: medium everything
            Config { workers: 8, batch_size: 4_000 },
            // Aggressive: high workers, large batches
            Config { workers: 16, batch_size: 8_000 },
        ]
    }

    /// Generate next guesses, similar to gradient ascent
    fn next_guesses(&self) -> Vec<Config> {
        if self.history.is_empty() {
            return self.initial_guesses();
        }

        // Find best config so far
        let best =
            self.history.iter().max_by(|a, b| a.score().partial_cmp(&b.score()).unwrap()).unwrap();

        eprintln!(
            "  Current best: {:?} ({:.2}M rows/sec, variance: {:.1}%)",
            best.config,
            best.best_throughput / 1_000_000.0,
            best.variance * 100.0
        );

        // Explore around the best configuration
        let mut candidates = Vec::new();
        let base_w = best.config.workers;
        let base_b = best.config.batch_size;

        // Worker variations (±50%, but clamped to valid range)
        for w_mult in [0.5, 1.0, 2.0] {
            let workers = ((base_w as f64 * w_mult) as usize).clamp(4, 16);

            // Batch size variations (±50%, log scale)
            for b_mult in [0.5, 1.0, 2.0] {
                let batch_size = ((base_b as f64 * b_mult) as usize).clamp(1_000, 32_000);

                let config = Config { workers, batch_size };

                // Don't re-test configs we've already tested
                if !self.history.iter().any(|r| r.config == config) {
                    // Also don't add duplicates within candidates
                    if !candidates.contains(&config) {
                        candidates.push(config);
                    }
                }
            }
        }

        // If we've tested everything nearby, expand search
        if candidates.is_empty() {
            eprintln!("  No new candidates near best - expanding search space");
            candidates = vec![
                Config { workers: 4, batch_size: 16_000 },
                Config { workers: 8, batch_size: 16_000 },
                Config { workers: 12, batch_size: 8_000 },
                Config { workers: 16, batch_size: 16_000 },
                Config { workers: 16, batch_size: 32_000 },
            ]
            .into_iter()
            .filter(|c| !self.history.iter().any(|r| r.config == *c))
            .collect();
        }

        // Limit to top 3 candidates to keep iterations fast
        candidates.truncate(3);
        candidates
    }

    fn add_result(&mut self, result: BenchmarkResult) { self.history.push(result); }

    fn has_converged(&self, min_iterations: usize) -> bool {
        if self.iteration < min_iterations {
            return false;
        }

        // Check if best hasn't improved in last 2 iterations
        if self.history.len() < 6 {
            return false;
        }

        let recent: Vec<_> = self.history.iter().rev().take(6).collect();
        let best_recent = recent[0..3].iter().map(|r| r.score()).fold(f64::NEG_INFINITY, f64::max);
        let best_previous =
            recent[3..6].iter().map(|r| r.score()).fold(f64::NEG_INFINITY, f64::max);

        // Converged if improvement < 5%
        (best_recent - best_previous) / best_previous < CONV_THRESHOLD
    }

    fn get_best(&self) -> &BenchmarkResult {
        // Get the configuration with the highest raw throughput
        self.history
            .iter()
            .max_by(|a, b| a.best_throughput.partial_cmp(&b.best_throughput).unwrap())
            .expect("No results")
    }

    fn print_summary(&self, bytes_per_row: f64, config: &arrow_tests::BatchConfig) {
        eprintln!();
        common::print_banner(
            &format!("Tuning Summary - {} configurations tested", self.history.len()),
            Some(72),
        );

        let mut sorted = self.history.clone();
        sorted.sort_by(|a, b| b.score().partial_cmp(&a.score()).unwrap());

        eprintln!("\nTop 5 configurations:");
        let mut table = Table::new();
        table.load_preset(UTF8_FULL).set_header(vec![
            "Workers",
            "Batch Size",
            "Best rows/sec",
            "Best MB/sec",
            "Variance",
            "Score",
        ]);

        for result in sorted.iter().take(5) {
            let mb_sec = result.best_throughput * bytes_per_row / 1_000_000.0;
            table.add_row(vec![
                result.config.workers.to_string(),
                result.config.batch_size.to_string(),
                format!("{:.0}", result.best_throughput),
                format!("{:.2}", mb_sec),
                format!("{:.1}%", result.variance * 100.0),
                format!("{:.0}", result.score()),
            ]);
        }

        eprintln!("{}", table);

        let best = self.get_best();
        let mb_sec = best.best_throughput * bytes_per_row / 1_000_000.0;
        let best_duration = best.durations.iter().cloned().fold(f64::INFINITY, f64::min);

        eprintln!();
        eprintln!("🏆 BEST RESULT:");
        eprintln!(
            "   Configuration: {} workers, {} batch size",
            best.config.workers, best.config.batch_size
        );
        eprintln!(
            "   Throughput:    {:.2}M rows/sec ({:.2} MB/sec)",
            best.best_throughput / 1_000_000.0,
            mb_sec
        );
        eprintln!("   Duration:      {:.3}s", best_duration);
        eprintln!("   Variance:      {:.1}%", best.variance * 100.0);

        // Display test schema
        eprintln!();
        eprintln!("📊 Test Schema:");
        print_schema_summary(config);
        eprintln!("  RecordBatch: {:.2} bytes/row", bytes_per_row);

        // Warning for variable-length types
        if config.utf8 > 0 || config.binary > 0 {
            eprintln!();
            eprintln!(
                "⚠️  Variable-length types detected (UTF8={}, BINARY={})",
                config.utf8, config.binary
            );
            eprintln!("   Bytes/row may vary with batch size due to Arrow overhead.");
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::any::Any + Send>> {
    common::run_example_with_cleanup(|ch| async move { run(ch).await.unwrap() }, None).await?;
    Ok(())
}

async fn run(ch: &'static ClickHouseContainer) -> Result<()> {
    let start_time = Instant::now();

    // Parse parameters
    let total_rows: usize = std::env::var("ROWS")
        .unwrap_or_else(|_| "10000000".to_string())
        .parse()
        .unwrap_or(10_000_000);

    let max_steps: usize =
        std::env::var("STEPS").unwrap_or_else(|_| "5".to_string()).parse().unwrap_or(5);

    let runs_per_config: usize = std::env::var("ITERS")
        .or_else(|_| std::env::var("RUNS"))
        .unwrap_or_else(|_| "3".to_string())
        .parse()
        .unwrap_or(3);

    // Get schema configuration
    let batch_config = arrow_tests::BatchConfig::from_env();

    print_params_table("Dynamic Performance Tuner", &[
        ("Total Rows", format!("{}", total_rows)),
        ("Max Steps", format!("{} (optimizer iterations)", max_steps)),
        ("Iters per config", format!("{} (runs to average)", runs_per_config)),
    ]);
    eprintln!();

    // Display schema configuration
    print_schema_config(&batch_config);
    eprintln!();

    // Measure actual RecordBatch size
    let bytes_per_row = calculate_bytes_per_row(&batch_config);
    eprintln!("RecordBatch: {:.2} bytes/row", bytes_per_row);
    eprintln!();

    let mut optimizer = Optimizer::new(total_rows);
    let db = common::DB_NAME;

    // Setup database schema once
    let schema = arrow_tests::create_test_batch_with_config(1, &batch_config).schema();

    for step in 0..max_steps {
        optimizer.iteration = step;

        eprintln!();
        common::print_banner(
            &format!("Step {}/{} - Testing new configurations", step + 1, max_steps),
            Some(72),
        );
        eprintln!();

        let configs =
            if step == 0 { optimizer.initial_guesses() } else { optimizer.next_guesses() };

        if configs.is_empty() {
            eprintln!("No new configurations to test - converged!");
            break;
        }

        eprintln!("Testing {} configurations:", configs.len());
        for (i, config) in configs.iter().enumerate() {
            eprintln!("  {}. {} workers, {} batch size", i + 1, config.workers, config.batch_size);
        }
        eprintln!();

        // Benchmark each configuration
        for config in configs {
            eprintln!(
                "→ Testing: {} workers, {} batch size ({} runs)",
                config.workers, config.batch_size, runs_per_config
            );

            let result = benchmark_config(
                ch,
                db,
                &schema,
                config,
                total_rows,
                runs_per_config,
                &batch_config,
            )
            .await?;

            eprintln!(
                "  Result: Avg {:.2}M rows/sec, Best {:.2}M rows/sec, Variance {:.1}%",
                result.avg_throughput / 1_000_000.0,
                result.best_throughput / 1_000_000.0,
                result.variance * 100.0
            );
            eprintln!();

            optimizer.add_result(result);
        }

        // Check convergence
        if optimizer.has_converged(2) {
            eprintln!("✓ Converged - optimal configuration found!");
            break;
        }
    }

    optimizer.print_summary(bytes_per_row, &batch_config);

    let total_elapsed = start_time.elapsed();
    eprintln!("\nTotal run time: {:.3}s", total_elapsed.as_secs_f64());

    Ok(())
}

async fn benchmark_config(
    ch: &'static ClickHouseContainer,
    db: &str,
    schema: &arrow::datatypes::SchemaRef,
    config: Config,
    total_rows: usize,
    runs: usize,
    batch_config: &arrow_tests::BatchConfig,
) -> Result<BenchmarkResult> {
    // Setup client with specified worker count
    let client = setup_benchmark_client(ch, config.workers).await?;
    arrow_tests::setup_database(db, &client).await?;

    // Create fresh table
    let table = arrow_tests::setup_table(&client, db, schema).await?;

    let mut durations = Vec::with_capacity(runs);

    // Run benchmark multiple times
    for run in 0..runs {
        let start = Instant::now();
        insert_concurrent(
            client.clone(),
            table.clone(),
            total_rows,
            config.batch_size,
            config.workers,
            batch_config,
        )
        .await;
        let duration = start.elapsed().as_secs_f64();
        durations.push(duration);

        // Truncate for next run (except last)
        if run < runs - 1 {
            drop(
                client
                    .query(format!("TRUNCATE TABLE {table}"), None)
                    .await?
                    .collect::<Vec<_>>()
                    .await,
            );
        }
    }

    // Calculate statistics
    let avg_duration = durations.iter().sum::<f64>() / durations.len() as f64;
    let best_duration = durations.iter().cloned().fold(f64::INFINITY, f64::min);

    let avg_throughput = total_rows as f64 / avg_duration;
    let best_throughput = total_rows as f64 / best_duration;

    // Coefficient of variation
    let variance = if durations.len() > 1 {
        let mean = avg_duration;
        let variance_val = durations.iter().map(|d| (d - mean).powi(2)).sum::<f64>()
            / (durations.len() - 1) as f64;
        (variance_val.sqrt() / mean).abs()
    } else {
        0.0
    };

    // Cleanup
    drop(client.query(format!("DROP TABLE {table}"), None).await?.collect::<Vec<_>>().await);

    Ok(BenchmarkResult { config, durations, avg_throughput, best_throughput, variance })
}