vibesql-storage 0.2.0

Storage engine for vibesql SQL database
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
427
428
429
430
431
432
433
434
//! Statistical sampling for large table statistics
//!
//! Provides efficient sampling methods for computing statistics on large tables
//! without requiring a full table scan. Implements various sampling strategies:
//! - Random sampling: Randomly select rows
//! - Systematic sampling: Every Nth row
//! - Reservoir sampling: Single-pass streaming sample
//! - Adaptive sampling: Choose sample size based on table size

use rand::{prelude::IndexedRandom, Rng, RngExt};

/// Configuration for statistical sampling
#[derive(Debug, Clone)]
pub struct SamplingConfig {
    /// Sample size strategy
    pub sample_size: SampleSize,

    /// Sampling method to use
    pub method: SamplingMethod,

    /// Statistical confidence level (e.g., 0.95 for 95% confidence)
    pub confidence_level: f64,
}

impl Default for SamplingConfig {
    fn default() -> Self {
        Self {
            sample_size: SampleSize::Adaptive,
            method: SamplingMethod::Random,
            confidence_level: 0.95,
        }
    }
}

/// Sample size specification
#[derive(Debug, Clone, PartialEq)]
pub enum SampleSize {
    /// Exact number of rows to sample
    RowCount(usize),

    /// Percentage of table (0.0 to 1.0)
    Percentage(f64),

    /// Automatically determine sample size based on table size
    ///
    /// Strategy:
    /// - Small tables (< 1000 rows): No sampling, use all rows
    /// - Medium tables (1K-100K rows): 10% sample
    /// - Large tables (> 100K rows): Fixed 10K row sample
    Adaptive,
}

/// Sampling method
#[derive(Debug, Clone, PartialEq)]
pub enum SamplingMethod {
    /// Pure random sampling (requires index-based access)
    Random,

    /// Every Nth row (systematic sampling)
    Systematic,

    /// Reservoir sampling (single-pass, constant memory)
    Reservoir,
}

impl SamplingConfig {
    /// Create a new sampling configuration
    pub fn new(sample_size: SampleSize, method: SamplingMethod) -> Self {
        Self { sample_size, method, confidence_level: 0.95 }
    }

    /// Create configuration for adaptive sampling (default)
    pub fn adaptive() -> Self {
        Self::default()
    }

    /// Create configuration for percentage-based sampling
    pub fn percentage(pct: f64) -> Self {
        Self {
            sample_size: SampleSize::Percentage(pct),
            method: SamplingMethod::Random,
            confidence_level: 0.95,
        }
    }

    /// Create configuration for fixed-size sampling
    pub fn fixed(count: usize) -> Self {
        Self {
            sample_size: SampleSize::RowCount(count),
            method: SamplingMethod::Random,
            confidence_level: 0.95,
        }
    }

    /// Determine actual sample size for a given table
    ///
    /// Returns (sample_size, should_sample) tuple
    /// - sample_size: Number of rows to sample
    /// - should_sample: Whether sampling is needed (false for small tables)
    pub fn determine_sample_size(&self, total_rows: usize) -> (usize, bool) {
        match &self.sample_size {
            SampleSize::RowCount(count) => {
                let sample_size = (*count).min(total_rows);
                (sample_size, sample_size < total_rows)
            }
            SampleSize::Percentage(pct) => {
                let sample_size = (total_rows as f64 * pct).ceil() as usize;
                let sample_size = sample_size.min(total_rows).max(1);
                (sample_size, sample_size < total_rows)
            }
            SampleSize::Adaptive => {
                // Adaptive strategy based on table size
                if total_rows < 1000 {
                    // Small table: no sampling
                    (total_rows, false)
                } else if total_rows < 100_000 {
                    // Medium table: 10% sample
                    let sample_size = (total_rows / 10).max(1000);
                    (sample_size, true)
                } else {
                    // Large table: fixed 10K sample
                    (10_000, true)
                }
            }
        }
    }
}

/// Sample rows from a table using the specified sampling method
pub fn sample_rows<T: Clone>(rows: &[T], config: &SamplingConfig, rng: &mut impl Rng) -> Vec<T> {
    let (sample_size, should_sample) = config.determine_sample_size(rows.len());

    if !should_sample {
        // No sampling needed, return all rows
        return rows.to_vec();
    }

    match config.method {
        SamplingMethod::Random => random_sample(rows, sample_size, rng),
        SamplingMethod::Systematic => systematic_sample(rows, sample_size),
        SamplingMethod::Reservoir => reservoir_sample(rows, sample_size, rng),
    }
}

/// Random sampling: randomly select n rows
fn random_sample<T: Clone>(rows: &[T], n: usize, rng: &mut impl Rng) -> Vec<T> {
    let n = n.min(rows.len());
    let indices: Vec<usize> = (0..rows.len()).collect();
    let sampled_indices = indices.choose_multiple(rng, n);

    sampled_indices.map(|&i| rows[i].clone()).collect()
}

/// Systematic sampling: every Nth row
fn systematic_sample<T: Clone>(rows: &[T], n: usize) -> Vec<T> {
    let n = n.min(rows.len());
    if n == 0 {
        return Vec::new();
    }

    let step = rows.len() / n;
    if step == 0 {
        return rows.to_vec();
    }

    rows.iter().step_by(step).take(n).cloned().collect()
}

/// Reservoir sampling: single-pass, constant memory
///
/// Knuth's Algorithm R - processes stream in one pass,
/// maintaining a reservoir of k samples with equal probability
fn reservoir_sample<T: Clone>(rows: &[T], k: usize, rng: &mut impl Rng) -> Vec<T> {
    let k = k.min(rows.len());
    if k == 0 {
        return Vec::new();
    }

    let mut reservoir: Vec<T> = rows.iter().take(k).cloned().collect();

    for (i, row) in rows.iter().enumerate().skip(k) {
        // Randomly decide whether to include this element
        let j = rng.random_range(0..=i);
        if j < k {
            reservoir[j] = row.clone();
        }
    }

    reservoir
}

/// Metadata about a sample
#[derive(Debug, Clone)]
pub struct SampleMetadata {
    /// Total number of rows in the table
    pub total_rows: usize,

    /// Number of rows in the sample
    pub sample_size: usize,

    /// Sampling ratio (sample_size / total_rows)
    pub sampling_ratio: f64,

    /// Whether sampling was actually used
    pub sampled: bool,

    /// Confidence level for statistical estimates
    pub confidence_level: f64,
}

impl SampleMetadata {
    /// Create metadata for a sample
    pub fn new(
        total_rows: usize,
        sample_size: usize,
        sampled: bool,
        confidence_level: f64,
    ) -> Self {
        let sampling_ratio =
            if total_rows > 0 { sample_size as f64 / total_rows as f64 } else { 1.0 };

        Self { total_rows, sample_size, sampling_ratio, sampled, confidence_level }
    }

    /// Extrapolate a count from sample to full table
    ///
    /// Given a count in the sample, estimate the count in the full table
    pub fn extrapolate_count(&self, sample_count: usize) -> usize {
        if !self.sampled || self.sampling_ratio == 0.0 {
            return sample_count;
        }

        (sample_count as f64 / self.sampling_ratio).round() as usize
    }

    /// Extrapolate a frequency/selectivity from sample to full table
    ///
    /// Frequencies typically don't need adjustment, but we apply
    /// a small confidence adjustment for small samples
    pub fn extrapolate_frequency(&self, sample_frequency: f64) -> f64 {
        if !self.sampled {
            return sample_frequency;
        }

        // For small samples, be more conservative with estimates
        // Apply Laplace smoothing for small samples
        if self.sample_size < 100 {
            let smoothing = 1.0 / self.sample_size as f64;
            (sample_frequency + smoothing) / (1.0 + smoothing)
        } else {
            sample_frequency
        }
    }

    /// Estimate the standard error for a proportion
    ///
    /// Used to provide confidence intervals for selectivity estimates
    pub fn standard_error(&self, proportion: f64) -> f64 {
        if !self.sampled || self.sample_size == 0 {
            return 0.0;
        }

        // Standard error of proportion: sqrt(p(1-p)/n)
        // With finite population correction
        let fpc = if self.total_rows > self.sample_size {
            ((self.total_rows - self.sample_size) as f64 / (self.total_rows - 1) as f64).sqrt()
        } else {
            1.0
        };

        (proportion * (1.0 - proportion) / self.sample_size as f64).sqrt() * fpc
    }

    /// Get confidence interval for a proportion estimate
    ///
    /// Returns (lower_bound, upper_bound) for the confidence level
    pub fn confidence_interval(&self, proportion: f64) -> (f64, f64) {
        if !self.sampled {
            return (proportion, proportion);
        }

        // Use normal approximation (valid for n*p > 5 and n*(1-p) > 5)
        // Z-score for 95% confidence is approximately 1.96
        let z_score = match self.confidence_level {
            x if x >= 0.99 => 2.576,
            x if x >= 0.95 => 1.96,
            x if x >= 0.90 => 1.645,
            _ => 1.96, // default to 95%
        };

        let se = self.standard_error(proportion);
        let margin = z_score * se;

        let lower = (proportion - margin).max(0.0);
        let upper = (proportion + margin).min(1.0);

        (lower, upper)
    }
}

#[cfg(test)]
mod tests {
    use rand::{rngs::StdRng, SeedableRng};

    use super::*;

    #[test]
    fn test_sample_size_adaptive() {
        let config = SamplingConfig::adaptive();

        // Small table: no sampling
        let (size, should_sample) = config.determine_sample_size(500);
        assert_eq!(size, 500);
        assert!(!should_sample);

        // Medium table: 10% sample
        let (size, should_sample) = config.determine_sample_size(50_000);
        assert_eq!(size, 5_000);
        assert!(should_sample);

        // Large table: fixed 10K sample
        let (size, should_sample) = config.determine_sample_size(1_000_000);
        assert_eq!(size, 10_000);
        assert!(should_sample);
    }

    #[test]
    fn test_sample_size_percentage() {
        let config = SamplingConfig::percentage(0.1);

        let (size, should_sample) = config.determine_sample_size(1000);
        assert_eq!(size, 100);
        assert!(should_sample);

        // Edge case: percentage results in 0
        let (size, should_sample) = config.determine_sample_size(5);
        assert_eq!(size, 1); // Clamped to minimum 1
        assert!(should_sample);
    }

    #[test]
    fn test_sample_size_fixed() {
        let config = SamplingConfig::fixed(100);

        let (size, should_sample) = config.determine_sample_size(1000);
        assert_eq!(size, 100);
        assert!(should_sample);

        // Edge case: sample size larger than table
        let (size, should_sample) = config.determine_sample_size(50);
        assert_eq!(size, 50);
        assert!(!should_sample);
    }

    #[test]
    fn test_random_sample() {
        let mut rng = StdRng::seed_from_u64(42);
        let rows: Vec<i32> = (0..100).collect();

        let config = SamplingConfig::fixed(10);
        let sample = sample_rows(&rows, &config, &mut rng);

        assert_eq!(sample.len(), 10);
        // All sampled values should be in range
        for val in sample {
            assert!((0..100).contains(&val));
        }
    }

    #[test]
    fn test_systematic_sample() {
        let rows: Vec<i32> = (0..100).collect();
        let sample = systematic_sample(&rows, 10);

        assert_eq!(sample.len(), 10);
        // Systematic sampling should be evenly spaced
        assert_eq!(sample[0], 0);
        assert_eq!(sample[1], 10);
        assert_eq!(sample[9], 90);
    }

    #[test]
    fn test_reservoir_sample() {
        let mut rng = StdRng::seed_from_u64(42);
        let rows: Vec<i32> = (0..100).collect();
        let sample = reservoir_sample(&rows, 10, &mut rng);

        assert_eq!(sample.len(), 10);
        // All sampled values should be valid
        for val in sample {
            assert!((0..100).contains(&val));
        }
    }

    #[test]
    fn test_sample_metadata_extrapolation() {
        // 10% sample (1000 out of 10000)
        let metadata = SampleMetadata::new(10_000, 1_000, true, 0.95);

        // Count extrapolation
        assert_eq!(metadata.extrapolate_count(100), 1_000);
        assert_eq!(metadata.extrapolate_count(50), 500);

        // Frequency extrapolation (should be mostly unchanged)
        let freq = metadata.extrapolate_frequency(0.5);
        assert!((freq - 0.5).abs() < 0.1);
    }

    #[test]
    fn test_confidence_interval() {
        let metadata = SampleMetadata::new(10_000, 1_000, true, 0.95);

        // For p=0.5 with n=1000, standard error ≈ 0.0158
        // 95% CI margin ≈ 1.96 * 0.0158 ≈ 0.031
        let (lower, upper) = metadata.confidence_interval(0.5);

        assert!(lower < 0.5);
        assert!(upper > 0.5);
        assert!((upper - lower) < 0.1); // Reasonable interval width
    }

    #[test]
    fn test_no_sampling_for_small_table() {
        let mut rng = StdRng::seed_from_u64(42);
        let rows: Vec<i32> = (0..50).collect();

        let config = SamplingConfig::adaptive();
        let sample = sample_rows(&rows, &config, &mut rng);

        // Should return all rows for small table
        assert_eq!(sample.len(), 50);
        assert_eq!(sample, rows);
    }
}