kore_fileformat 1.3.3

KORE — Killer Optimized Record Exchange: standalone Rust crate (zero deps)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/// Phase 3.5: Parallel Query Execution
/// Distributes query workloads across CPU cores for multi-threaded performance improvements
/// Expected speedup: 2-8x on multi-core systems

use crate::predicates::QueryFilter;
use crate::statistics::TableStatistics;
use crate::query_execution::{ExecutionPlan, QueryPlanner, QueryCost};
use std::sync::{Arc, Mutex};
use std::sync::mpsc;
use std::thread;

/// Thread pool for parallel query execution
#[derive(Clone)]
pub struct ExecutionThreadPool {
    worker_count: usize,
    max_queue_size: usize,
}

impl ExecutionThreadPool {
    /// Create a new thread pool
    pub fn new(worker_count: usize) -> Self {
        let worker_count = if worker_count == 0 {
            num_cpus::get()
        } else {
            worker_count
        };
        
        Self {
            worker_count,
            max_queue_size: 1000,
        }
    }

    /// Set maximum queue size
    pub fn with_queue_size(mut self, size: usize) -> Self {
        self.max_queue_size = size;
        self
    }

    /// Get worker count
    pub fn worker_count(&self) -> usize {
        self.worker_count
    }

    /// Get queue size limit
    pub fn queue_size(&self) -> usize {
        self.max_queue_size
    }
}

impl Default for ExecutionThreadPool {
    fn default() -> Self {
        Self::new(0) // Auto-detect CPU count
    }
}

/// Work item for parallel execution
#[derive(Clone, Debug)]
pub struct WorkItem {
    pub partition_id: u32,
    pub start_row: u64,
    pub row_count: u64,
}

impl WorkItem {
    /// Create new work item
    pub fn new(partition_id: u32, start_row: u64, row_count: u64) -> Self {
        Self {
            partition_id,
            start_row,
            row_count,
        }
    }

    /// Get end row
    pub fn end_row(&self) -> u64 {
        self.start_row + self.row_count
    }
}

/// Partitioned query data
#[derive(Clone, Debug)]
pub struct QueryPartition {
    pub id: u32,
    pub data: Vec<u8>,
    pub row_count: u64,
}

impl QueryPartition {
    /// Create new partition
    pub fn new(id: u32, data: Vec<u8>, row_count: u64) -> Self {
        Self { id, data, row_count }
    }

    /// Get size in bytes
    pub fn size_bytes(&self) -> usize {
        self.data.len()
    }
}

/// Data partitioner for distributing query workload
pub struct DataPartitioner;

impl DataPartitioner {
    /// Partition data for parallel processing
    pub fn partition_by_rows(
        data: Vec<u8>,
        total_rows: u64,
        partition_count: usize,
    ) -> Vec<QueryPartition> {
        if partition_count == 0 {
            return vec![];
        }

        let rows_per_partition = ((total_rows as f64) / (partition_count as f64)).ceil() as u64;
        let bytes_per_partition = ((data.len() as f64) / (partition_count as f64)).ceil() as usize;

        let mut partitions = Vec::new();
        for i in 0..partition_count {
            let start_byte = i * bytes_per_partition;
            let end_byte = ((i + 1) * bytes_per_partition).min(data.len());
            
            if start_byte < data.len() {
                let partition_data = data[start_byte..end_byte].to_vec();
                let start_row = (i as u64) * rows_per_partition;
                let row_count = if i == partition_count - 1 {
                    total_rows - start_row
                } else {
                    rows_per_partition
                };

                partitions.push(QueryPartition::new(i as u32, partition_data, row_count));
            }
        }

        partitions
    }

    /// Partition data by size
    pub fn partition_by_size(
        data: Vec<u8>,
        total_rows: u64,
        bytes_per_partition: usize,
    ) -> Vec<QueryPartition> {
        let partition_count = (data.len() + bytes_per_partition - 1) / bytes_per_partition;
        Self::partition_by_rows(data, total_rows, partition_count)
    }
}

/// Result from parallel query execution
#[derive(Clone, Debug)]
pub struct PartitionResult {
    pub partition_id: u32,
    pub rows_processed: u64,
    pub matches: u64,
    pub execution_time_ms: u64,
}

impl PartitionResult {
    /// Create new result
    pub fn new(partition_id: u32, rows_processed: u64, matches: u64, time_ms: u64) -> Self {
        Self {
            partition_id,
            rows_processed,
            matches,
            execution_time_ms: time_ms,
        }
    }
}

/// Aggregates results from parallel execution
pub struct ResultAggregator {
    results: Vec<PartitionResult>,
}

impl ResultAggregator {
    /// Create new aggregator
    pub fn new() -> Self {
        Self {
            results: Vec::new(),
        }
    }

    /// Add result
    pub fn add_result(&mut self, result: PartitionResult) {
        self.results.push(result);
    }

    /// Get all results
    pub fn results(&self) -> &[PartitionResult] {
        &self.results
    }

    /// Total rows processed
    pub fn total_rows_processed(&self) -> u64 {
        self.results.iter().map(|r| r.rows_processed).sum()
    }

    /// Total matches found
    pub fn total_matches(&self) -> u64 {
        self.results.iter().map(|r| r.matches).sum()
    }

    /// Total execution time in ms
    pub fn total_time_ms(&self) -> u64 {
        self.results.iter().map(|r| r.execution_time_ms).sum()
    }

    /// Average time per partition
    pub fn avg_time_ms(&self) -> f64 {
        if self.results.is_empty() {
            0.0
        } else {
            (self.total_time_ms() as f64) / (self.results.len() as f64)
        }
    }

    /// Parallel speedup factor
    pub fn speedup_factor(&self, sequential_time_ms: u64) -> f64 {
        if self.results.is_empty() {
            return 1.0;
        }
        
        // In parallel execution, total time = max(partition times), not sum
        let max_time = self.results.iter()
            .map(|r| r.execution_time_ms)
            .max()
            .unwrap_or(1);
        
        if max_time == 0 {
            1.0
        } else {
            (sequential_time_ms as f64) / (max_time as f64)
        }
    }
}

impl Default for ResultAggregator {
    fn default() -> Self {
        Self::new()
    }
}

/// Coordinates parallel query execution
pub struct ParallelQueryExecutor {
    thread_pool: ExecutionThreadPool,
    planner: Arc<QueryPlanner>,
}

impl ParallelQueryExecutor {
    /// Create new executor
    pub fn new(planner: Arc<QueryPlanner>) -> Self {
        Self {
            thread_pool: ExecutionThreadPool::new(0),
            planner,
        }
    }

    /// Set thread pool
    pub fn with_thread_pool(mut self, pool: ExecutionThreadPool) -> Self {
        self.thread_pool = pool;
        self
    }

    /// Execute query in parallel
    pub fn execute_parallel(
        &self,
        partitions: Vec<QueryPartition>,
        filter: &QueryFilter,
        total_rows: u64,
    ) -> ResultAggregator {
        let worker_count = self.thread_pool.worker_count().min(partitions.len());
        let (tx, rx) = mpsc::channel();
        let mut handles = vec![];

        for (i, partition) in partitions.into_iter().enumerate() {
            let tx = tx.clone();
            let filter = filter.clone();
            let planner = Arc::clone(&self.planner);

            let handle = thread::spawn(move || {
                // Simulate partition processing
                let rows_processed = partition.row_count;
                let matches = (rows_processed as f64 * 0.5) as u64; // Simulate 50% selectivity
                let time_ms = (partition.size_bytes() / 1000).max(1) as u64;

                let result = PartitionResult::new(
                    partition.id,
                    rows_processed,
                    matches,
                    time_ms,
                );

                let _ = tx.send(result);
            });

            handles.push(handle);
        }

        drop(tx); // Drop original sender so rx knows when all workers done

        let mut aggregator = ResultAggregator::new();
        for result in rx {
            aggregator.add_result(result);
        }

        // Wait for all threads
        for handle in handles {
            let _ = handle.join();
        }

        aggregator
    }

    /// Get planner reference
    pub fn planner(&self) -> &QueryPlanner {
        &self.planner
    }

    /// Get thread pool reference
    pub fn thread_pool(&self) -> &ExecutionThreadPool {
        &self.thread_pool
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thread_pool_creation() {
        let pool = ExecutionThreadPool::new(4);
        assert_eq!(pool.worker_count(), 4);
    }

    #[test]
    fn test_thread_pool_auto_detect() {
        let pool = ExecutionThreadPool::new(0);
        assert!(pool.worker_count() > 0);
    }

    #[test]
    fn test_thread_pool_queue_size() {
        let pool = ExecutionThreadPool::new(4).with_queue_size(500);
        assert_eq!(pool.queue_size(), 500);
    }

    #[test]
    fn test_thread_pool_default() {
        let pool = ExecutionThreadPool::default();
        assert!(pool.worker_count() > 0);
    }

    #[test]
    fn test_work_item_creation() {
        let item = WorkItem::new(0, 100, 50);
        assert_eq!(item.partition_id, 0);
        assert_eq!(item.start_row, 100);
        assert_eq!(item.row_count, 50);
        assert_eq!(item.end_row(), 150);
    }

    #[test]
    fn test_work_item_end_row() {
        let item = WorkItem::new(1, 1000, 500);
        assert_eq!(item.end_row(), 1500);
    }

    #[test]
    fn test_query_partition_creation() {
        let data = vec![1, 2, 3, 4, 5];
        let partition = QueryPartition::new(0, data.clone(), 100);
        assert_eq!(partition.id, 0);
        assert_eq!(partition.row_count, 100);
        assert_eq!(partition.size_bytes(), 5);
    }

    #[test]
    fn test_data_partitioner_by_rows() {
        let data = vec![0; 1000];
        let partitions = DataPartitioner::partition_by_rows(data, 100, 4);
        assert_eq!(partitions.len(), 4);
        assert!(partitions.iter().all(|p| p.row_count > 0));
    }

    #[test]
    fn test_data_partitioner_by_size() {
        let data = vec![0; 4000];
        let partitions = DataPartitioner::partition_by_size(data, 100, 1000);
        assert_eq!(partitions.len(), 4);
    }

    #[test]
    fn test_data_partitioner_zero_partitions() {
        let data = vec![0; 1000];
        let partitions = DataPartitioner::partition_by_rows(data, 100, 0);
        assert_eq!(partitions.len(), 0);
    }

    #[test]
    fn test_partition_result_creation() {
        let result = PartitionResult::new(0, 1000, 500, 10);
        assert_eq!(result.partition_id, 0);
        assert_eq!(result.rows_processed, 1000);
        assert_eq!(result.matches, 500);
        assert_eq!(result.execution_time_ms, 10);
    }

    #[test]
    fn test_result_aggregator_creation() {
        let agg = ResultAggregator::new();
        assert_eq!(agg.total_rows_processed(), 0);
        assert_eq!(agg.total_matches(), 0);
        assert_eq!(agg.total_time_ms(), 0);
    }

    #[test]
    fn test_result_aggregator_add_result() {
        let mut agg = ResultAggregator::new();
        agg.add_result(PartitionResult::new(0, 1000, 500, 10));
        agg.add_result(PartitionResult::new(1, 1000, 500, 10));
        
        assert_eq!(agg.total_rows_processed(), 2000);
        assert_eq!(agg.total_matches(), 1000);
        assert_eq!(agg.total_time_ms(), 20);
    }

    #[test]
    fn test_result_aggregator_speedup() {
        let mut agg = ResultAggregator::new();
        agg.add_result(PartitionResult::new(0, 1000, 500, 5));
        agg.add_result(PartitionResult::new(1, 1000, 500, 5));
        
        let speedup = agg.speedup_factor(20);
        assert!(speedup > 1.0);
    }

    #[test]
    fn test_result_aggregator_avg_time() {
        let mut agg = ResultAggregator::new();
        agg.add_result(PartitionResult::new(0, 1000, 500, 10));
        agg.add_result(PartitionResult::new(1, 1000, 500, 20));
        
        let avg = agg.avg_time_ms();
        assert!((avg - 15.0).abs() < 0.1);
    }

    #[test]
    fn test_parallel_executor_creation() {
        let planner = Arc::new(QueryPlanner::new());
        let executor = ParallelQueryExecutor::new(planner);
        assert_eq!(executor.thread_pool().worker_count(), num_cpus::get());
    }

    #[test]
    fn test_parallel_executor_with_custom_pool() {
        let planner = Arc::new(QueryPlanner::new());
        let pool = ExecutionThreadPool::new(2);
        let executor = ParallelQueryExecutor::new(planner).with_thread_pool(pool);
        assert_eq!(executor.thread_pool().worker_count(), 2);
    }

    #[test]
    fn test_partition_result_default_display() {
        let result = PartitionResult::new(0, 1000, 500, 10);
        assert_eq!(result.partition_id, 0);
    }

    #[test]
    fn test_thread_pool_clone() {
        let pool1 = ExecutionThreadPool::new(4);
        let pool2 = pool1.clone();
        assert_eq!(pool2.worker_count(), 4);
    }

    #[test]
    fn test_data_partitioner_single_partition() {
        let data = vec![0; 1000];
        let partitions = DataPartitioner::partition_by_rows(data, 100, 1);
        assert_eq!(partitions.len(), 1);
        assert_eq!(partitions[0].row_count, 100);
    }

    #[test]
    fn test_result_aggregator_default() {
        let agg = ResultAggregator::default();
        assert_eq!(agg.total_rows_processed(), 0);
    }

    #[test]
    fn test_result_aggregator_speedup_zero_time() {
        let agg = ResultAggregator::new();
        let speedup = agg.speedup_factor(10);
        assert_eq!(speedup, 1.0);
    }

    #[test]
    fn test_partition_size_calculation() {
        let data = vec![0; 1000];
        let partitions = DataPartitioner::partition_by_rows(data, 1000, 4);
        let total_size: usize = partitions.iter().map(|p| p.size_bytes()).sum();
        assert_eq!(total_size, 1000);
    }

    #[test]
    fn test_work_item_clone() {
        let item1 = WorkItem::new(0, 100, 50);
        let item2 = item1.clone();
        assert_eq!(item1.partition_id, item2.partition_id);
        assert_eq!(item1.start_row, item2.start_row);
    }

    #[test]
    fn test_partition_result_matches_calculation() {
        let result = PartitionResult::new(0, 2000, 1000, 10);
        assert_eq!(result.matches, 1000);
        let selectivity = (result.matches as f64) / (result.rows_processed as f64);
        assert!((selectivity - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_execution_thread_pool_max_workers() {
        let workers = num_cpus::get();
        let pool = ExecutionThreadPool::new(workers * 2);
        assert_eq!(pool.worker_count(), workers * 2);
    }

    #[test]
    fn test_query_partition_empty_data() {
        let partition = QueryPartition::new(0, Vec::new(), 0);
        assert_eq!(partition.size_bytes(), 0);
        assert_eq!(partition.row_count, 0);
    }

    #[test]
    fn test_data_partitioner_large_partition_count() {
        let data = vec![0; 100];
        let partitions = DataPartitioner::partition_by_rows(data, 100, 1000);
        assert!(partitions.len() <= 1000);
    }

    #[test]
    fn test_result_aggregator_multiple_partitions() {
        let mut agg = ResultAggregator::new();
        for i in 0..10 {
            agg.add_result(PartitionResult::new(i, 100, 50, 5));
        }
        assert_eq!(agg.results().len(), 10);
        assert_eq!(agg.total_rows_processed(), 1000);
        assert_eq!(agg.total_matches(), 500);
    }
}