kore_fileformat 1.1.1

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
/// KORE Full Distributed Query Engine
/// Complete SQL support with JOINs, GROUP BY, ORDER BY, aggregations, and distributed execution

use std::collections::{HashMap, BTreeMap};

/// Advanced SQL operations
#[derive(Debug, Clone, PartialEq)]
pub enum JoinType {
    Inner,
    Left,
    Right,
    Full,
}

/// Aggregation functions
#[derive(Debug, Clone, PartialEq)]
pub enum AggFunc {
    Count,
    Sum,
    Avg,
    Min,
    Max,
}

/// Advanced query structure
#[derive(Debug, Clone)]
pub struct AdvancedQuery {
    pub select_cols: Vec<SelectCol>,
    pub from_table: String,
    pub joins: Vec<JoinClause>,
    pub where_filters: Vec<FilterClause>,
    pub group_by: Vec<String>,
    pub having: Vec<FilterClause>,
    pub order_by: Vec<OrderByClause>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

/// Select column with optional aggregation and alias
#[derive(Debug, Clone)]
pub struct SelectCol {
    pub column: String,
    pub aggregation: Option<AggFunc>,
    pub alias: Option<String>,
}

/// JOIN clause
#[derive(Debug, Clone)]
pub struct JoinClause {
    pub table: String,
    pub join_type: JoinType,
    pub on_condition: FilterClause,
}

/// Filter/WHERE clause
#[derive(Debug, Clone)]
pub struct FilterClause {
    pub left: String,
    pub operator: String,
    pub right: String,
    pub logic_op: Option<String>, // AND/OR
}

/// ORDER BY clause
#[derive(Debug, Clone)]
pub struct OrderByClause {
    pub column: String,
    pub descending: bool,
}

/// Query execution plan
#[derive(Debug, Clone)]
pub struct ExecutionPlan {
    pub plan_id: String,
    pub operations: Vec<Operation>,
    pub estimated_rows: u64,
    pub distributed: bool,
}

/// Query operation
#[derive(Debug, Clone)]
pub enum Operation {
    Scan {
        table: String,
        columns: Vec<String>,
    },
    Filter {
        condition: FilterClause,
    },
    Join {
        with_table: String,
        join_type: JoinType,
        condition: FilterClause,
    },
    GroupBy {
        columns: Vec<String>,
        aggregations: Vec<AggFunc>,
    },
    OrderBy {
        columns: Vec<OrderByClause>,
    },
    Limit {
        count: usize,
        offset: Option<usize>,
    },
    Aggregate {
        func: AggFunc,
        column: String,
    },
}

/// Query optimizer
pub struct QueryOptimizer;

impl QueryOptimizer {
    /// Optimize query execution plan
    pub fn optimize(query: &AdvancedQuery) -> ExecutionPlan {
        let mut operations = Vec::new();

        // 1. Scan base table
        let scan_cols: Vec<String> = query.select_cols.iter()
            .map(|col| col.column.clone())
            .collect();
        operations.push(Operation::Scan {
            table: query.from_table.clone(),
            columns: scan_cols,
        });

        // 2. Apply WHERE filters (early filtering for performance)
        for filter in &query.where_filters {
            operations.push(Operation::Filter {
                condition: filter.clone(),
            });
        }

        // 3. Apply JOINs
        for join in &query.joins {
            operations.push(Operation::Join {
                with_table: join.table.clone(),
                join_type: join.join_type.clone(),
                condition: join.on_condition.clone(),
            });
        }

        // 4. GROUP BY with aggregations
        if !query.group_by.is_empty() {
            let aggs: Vec<AggFunc> = query.select_cols.iter()
                .filter_map(|col| col.aggregation.clone())
                .collect();
            
            operations.push(Operation::GroupBy {
                columns: query.group_by.clone(),
                aggregations: aggs,
            });
        }

        // 5. ORDER BY
        if !query.order_by.is_empty() {
            operations.push(Operation::OrderBy {
                columns: query.order_by.clone(),
            });
        }

        // 6. LIMIT/OFFSET
        if query.limit.is_some() {
            operations.push(Operation::Limit {
                count: query.limit.unwrap_or(0),
                offset: query.offset,
            });
        }

        ExecutionPlan {
            plan_id: format!("plan_{}", uuid_short()),
            operations,
            estimated_rows: estimate_rows(query),
            distributed: !query.joins.is_empty() || !query.group_by.is_empty(),
        }
    }

    /// Estimate row count
    pub fn estimate_row_count(query: &AdvancedQuery) -> u64 {
        estimate_rows(query)
    }
}

/// Distributed query executor
pub struct DistributedExecutor;

impl DistributedExecutor {
    /// Execute distributed query across multiple KORE files
    pub fn execute_distributed(
        plan: &ExecutionPlan,
        data_partitions: Vec<Vec<HashMap<String, String>>>,
    ) -> QueryResult {
        let mut results = Vec::new();

        // Execute on each partition in parallel (simulated)
        for partition in data_partitions {
            let mut partition_results = Vec::new();

            // Execute each operation in the plan
            let mut current_data = partition;

            for operation in &plan.operations {
                match operation {
                    Operation::Filter { condition } => {
                        current_data = Self::apply_filter(&current_data, condition);
                    }
                    Operation::GroupBy { columns, aggregations } => {
                        partition_results = Self::apply_groupby(&current_data, columns, aggregations);
                        break;
                    }
                    Operation::OrderBy { columns } => {
                        current_data = Self::apply_orderby(&current_data, columns);
                    }
                    Operation::Limit { count, offset } => {
                        current_data = Self::apply_limit(&current_data, *count, *offset);
                    }
                    _ => {}
                }
            }

            results.extend(current_data);
            results.extend(partition_results);
        }

        let rows_processed = results.len() as u64;
        QueryResult {
            rows: results,
            execution_time_ms: 0.0,
            rows_processed,
        }
    }

    fn apply_filter(
        data: &[HashMap<String, String>],
        condition: &FilterClause,
    ) -> Vec<HashMap<String, String>> {
        data.iter()
            .filter(|row| {
                if let Some(left_val) = row.get(&condition.left) {
                    if let Some(right_val) = row.get(&condition.right) {
                        match condition.operator.as_str() {
                            "=" => left_val == right_val,
                            ">" => left_val > right_val,
                            "<" => left_val < right_val,
                            _ => true,
                        }
                    } else {
                        false
                    }
                } else {
                    false
                }
            })
            .cloned()
            .collect()
    }

    fn apply_groupby(
        data: &[HashMap<String, String>],
        group_cols: &[String],
        aggs: &[AggFunc],
    ) -> Vec<HashMap<String, String>> {
        let mut groups: BTreeMap<Vec<String>, Vec<HashMap<String, String>>> = BTreeMap::new();

        // Group data
        for row in data {
            let key: Vec<String> = group_cols
                .iter()
                .filter_map(|col| row.get(col).cloned())
                .collect();
            
            groups.entry(key).or_insert_with(Vec::new).push(row.clone());
        }

        // Apply aggregations
        let mut results = Vec::new();
        for (_key, group) in groups {
            let mut result = HashMap::new();
            
            // Store group keys
            for col in group_cols.iter() {
                if let Some(first_row) = group.first() {
                    if let Some(val) = first_row.get(col) {
                        result.insert(col.clone(), val.clone());
                    }
                }
            }

            // Store aggregations
            for agg in aggs {
                let agg_result = match agg {
                    AggFunc::Count => group.len().to_string(),
                    AggFunc::Sum => "0".to_string(), // Simplified
                    AggFunc::Avg => "0".to_string(),
                    AggFunc::Min => "0".to_string(),
                    AggFunc::Max => "0".to_string(),
                };
                result.insert(format!("{:?}", agg), agg_result);
            }

            results.push(result);
        }

        results
    }

    fn apply_orderby(
        data: &[HashMap<String, String>],
        order_cols: &[OrderByClause],
    ) -> Vec<HashMap<String, String>> {
        let mut result = data.to_vec();
        
        for order in order_cols.iter().rev() {
            result.sort_by(|a, b| {
                let a_val = a.get(&order.column).map(|s| s.as_str()).unwrap_or("");
                let b_val = b.get(&order.column).map(|s| s.as_str()).unwrap_or("");
                
                if order.descending {
                    b_val.cmp(a_val)
                } else {
                    a_val.cmp(b_val)
                }
            });
        }
        
        result
    }

    fn apply_limit(
        data: &[HashMap<String, String>],
        count: usize,
        offset: Option<usize>,
    ) -> Vec<HashMap<String, String>> {
        let start = offset.unwrap_or(0);
        data.iter()
            .skip(start)
            .take(count)
            .cloned()
            .collect()
    }
}

/// Query result
#[derive(Debug, Clone)]
pub struct QueryResult {
    pub rows: Vec<HashMap<String, String>>,
    pub execution_time_ms: f64,
    pub rows_processed: u64,
}

/// Helper function to generate short UUID
fn uuid_short() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    format!("{:x}", now % 0xFFFFFF)
}

/// Estimate row count for query
fn estimate_rows(query: &AdvancedQuery) -> u64 {
    let base_rows = 10_000_000u64; // Assume 10M rows per table
    
    // Estimate selectivity based on filters
    let mut selectivity = 1.0;
    for _filter in &query.where_filters {
        selectivity *= 0.5; // Each filter reduces by ~50%
    }
    
    (base_rows as f64 * selectivity) as u64
}

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

    #[test]
    fn test_query_optimizer() {
        let query = AdvancedQuery {
            select_cols: vec![SelectCol {
                column: "id".to_string(),
                aggregation: None,
                alias: None,
            }],
            from_table: "users".to_string(),
            joins: vec![],
            where_filters: vec![],
            group_by: vec![],
            having: vec![],
            order_by: vec![],
            limit: Some(100),
            offset: None,
        };

        let plan = QueryOptimizer::optimize(&query);
        assert!(!plan.operations.is_empty());
    }

    #[test]
    fn test_row_estimation() {
        let query = AdvancedQuery {
            select_cols: vec![],
            from_table: "table".to_string(),
            joins: vec![],
            where_filters: vec![FilterClause {
                left: "col".to_string(),
                operator: ">".to_string(),
                right: "100".to_string(),
                logic_op: None,
            }],
            group_by: vec![],
            having: vec![],
            order_by: vec![],
            limit: None,
            offset: None,
        };

        let estimate = QueryOptimizer::estimate_row_count(&query);
        assert!(estimate > 0);
        assert!(estimate < 10_000_000);
    }

    #[test]
    fn test_execution_plan_distributed() {
        let query = AdvancedQuery {
            select_cols: vec![],
            from_table: "table".to_string(),
            joins: vec![JoinClause {
                table: "other".to_string(),
                join_type: JoinType::Inner,
                on_condition: FilterClause {
                    left: "id".to_string(),
                    operator: "=".to_string(),
                    right: "other_id".to_string(),
                    logic_op: None,
                },
            }],
            where_filters: vec![],
            group_by: vec![],
            having: vec![],
            order_by: vec![],
            limit: None,
            offset: None,
        };

        let plan = QueryOptimizer::optimize(&query);
        assert!(plan.distributed);
    }

    #[test]
    fn test_distributed_executor() {
        let mut partition = Vec::new();
        let mut row = HashMap::new();
        row.insert("id".to_string(), "1".to_string());
        row.insert("name".to_string(), "Alice".to_string());
        partition.push(row);

        let plan = ExecutionPlan {
            plan_id: "test".to_string(),
            operations: vec![Operation::Limit {
                count: 10,
                offset: None,
            }],
            estimated_rows: 1,
            distributed: false,
        };

        let result = DistributedExecutor::execute_distributed(&plan, vec![partition]);
        assert_eq!(result.rows.len(), 1);
    }

    #[test]
    fn test_join_type_variants() {
        assert_ne!(JoinType::Inner, JoinType::Left);
        assert_ne!(JoinType::Left, JoinType::Right);
    }

    #[test]
    fn test_agg_func_variants() {
        assert_ne!(AggFunc::Count, AggFunc::Sum);
        assert_eq!(AggFunc::Count, AggFunc::Count);
    }
}