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
//! Predicate Pushdown and Query Filtering
//!
//! Provides abstractions for predicates (WHERE clauses) and column selection
//! to enable query optimization and early data filtering.

use std::collections::HashSet;

/// A comparison operator for predicates
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum ComparisonOp {
    Equals,
    NotEquals,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
}

/// A value that can be compared in predicates
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum PredicateValue {
    Int64(i64),
    Float64(f64),
    String(String),
    Boolean(bool),
}

impl PredicateValue {
    /// Check if this value matches the comparison
    pub fn matches(&self, op: &ComparisonOp, other: &PredicateValue) -> bool {
        match (self, other) {
            (PredicateValue::Int64(a), PredicateValue::Int64(b)) => match op {
                ComparisonOp::Equals => a == b,
                ComparisonOp::NotEquals => a != b,
                ComparisonOp::LessThan => a < b,
                ComparisonOp::LessThanOrEqual => a <= b,
                ComparisonOp::GreaterThan => a > b,
                ComparisonOp::GreaterThanOrEqual => a >= b,
            },
            (PredicateValue::Float64(a), PredicateValue::Float64(b)) => match op {
                ComparisonOp::Equals => (a - b).abs() < f64::EPSILON,
                ComparisonOp::NotEquals => (a - b).abs() >= f64::EPSILON,
                ComparisonOp::LessThan => a < b,
                ComparisonOp::LessThanOrEqual => a <= b,
                ComparisonOp::GreaterThan => a > b,
                ComparisonOp::GreaterThanOrEqual => a >= b,
            },
            (PredicateValue::String(a), PredicateValue::String(b)) => match op {
                ComparisonOp::Equals => a == b,
                ComparisonOp::NotEquals => a != b,
                ComparisonOp::LessThan => a < b,
                ComparisonOp::LessThanOrEqual => a <= b,
                ComparisonOp::GreaterThan => a > b,
                ComparisonOp::GreaterThanOrEqual => a >= b,
            },
            (PredicateValue::Boolean(a), PredicateValue::Boolean(b)) => match op {
                ComparisonOp::Equals => a == b,
                ComparisonOp::NotEquals => a != b,
                _ => false, // Boolean comparisons only make sense with Equals/NotEquals
            },
            _ => false, // Type mismatch
        }
    }
}

/// A single column predicate (filter condition)
#[derive(Debug, Clone)]
pub struct ColumnPredicate {
    pub column_name: String,
    pub op: ComparisonOp,
    pub value: PredicateValue,
}

impl ColumnPredicate {
    /// Create a new predicate
    pub fn new(column_name: String, op: ComparisonOp, value: PredicateValue) -> Self {
        ColumnPredicate {
            column_name,
            op,
            value,
        }
    }

    /// Check if a value matches this predicate
    pub fn matches(&self, actual_value: &PredicateValue) -> bool {
        actual_value.matches(&self.op, &self.value)
    }
}

/// Range predicates for min/max filtering (used for block skipping)
#[derive(Debug, Clone)]
pub struct RangePredicate {
    pub column_name: String,
    pub min_value: PredicateValue,
    pub max_value: PredicateValue,
}

impl RangePredicate {
    /// Create a new range predicate
    pub fn new(column_name: String, min_value: PredicateValue, max_value: PredicateValue) -> Self {
        RangePredicate {
            column_name,
            min_value,
            max_value,
        }
    }

    /// Check if a value is in this range
    pub fn contains(&self, value: &PredicateValue) -> bool {
        value >= &self.min_value && value <= &self.max_value
    }
}

/// A combined predicate expression (multiple conditions combined with AND)
#[derive(Debug, Clone)]
pub struct PredicateExpression {
    predicates: Vec<ColumnPredicate>,
}

impl PredicateExpression {
    /// Create a new empty predicate expression
    pub fn new() -> Self {
        PredicateExpression {
            predicates: Vec::new(),
        }
    }

    /// Add a predicate to the expression
    pub fn add(&mut self, predicate: ColumnPredicate) {
        self.predicates.push(predicate);
    }

    /// Check if a set of values matches all predicates
    pub fn matches(&self, values: &[(String, PredicateValue)]) -> bool {
        for predicate in &self.predicates {
            let matches = values
                .iter()
                .find(|(name, _)| name == &predicate.column_name)
                .map(|(_, value)| predicate.matches(value))
                .unwrap_or(false);

            if !matches {
                return false;
            }
        }
        true
    }

    /// Get the number of predicates
    pub fn len(&self) -> usize {
        self.predicates.len()
    }

    /// Check if expression is empty
    pub fn is_empty(&self) -> bool {
        self.predicates.is_empty()
    }

    /// Get all column names referenced in predicates
    pub fn referenced_columns(&self) -> HashSet<String> {
        self.predicates
            .iter()
            .map(|p| p.column_name.clone())
            .collect()
    }
}

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

/// Column selection for pruning
#[derive(Debug, Clone)]
pub struct ColumnSelection {
    /// Specific columns to select. If empty, all columns are selected
    selected_columns: HashSet<String>,
}

impl ColumnSelection {
    /// Create a new column selection that selects all columns
    pub fn all() -> Self {
        ColumnSelection {
            selected_columns: HashSet::new(),
        }
    }

    /// Create a new column selection with specific columns
    pub fn new(columns: Vec<String>) -> Self {
        ColumnSelection {
            selected_columns: columns.into_iter().collect(),
        }
    }

    /// Check if a column is selected
    pub fn is_selected(&self, column_name: &str) -> bool {
        // If selection is empty, all columns are selected
        if self.selected_columns.is_empty() {
            return true;
        }
        self.selected_columns.contains(column_name)
    }

    /// Get selected column names
    pub fn columns(&self) -> Vec<&str> {
        self.selected_columns.iter().map(|s| s.as_str()).collect()
    }

    /// Check if all columns are selected
    pub fn is_all(&self) -> bool {
        self.selected_columns.is_empty()
    }

    /// Add a column to selection
    pub fn add(&mut self, column_name: String) {
        self.selected_columns.insert(column_name);
    }
}

impl Default for ColumnSelection {
    fn default() -> Self {
        Self::all()
    }
}

/// Query filter combining predicates and column selection
#[derive(Debug, Clone)]
pub struct QueryFilter {
    pub predicates: PredicateExpression,
    pub column_selection: ColumnSelection,
}

impl QueryFilter {
    /// Create a new query filter
    pub fn new(predicates: PredicateExpression, column_selection: ColumnSelection) -> Self {
        QueryFilter {
            predicates,
            column_selection,
        }
    }

    /// Create an empty filter (no predicates, all columns)
    pub fn empty() -> Self {
        QueryFilter {
            predicates: PredicateExpression::new(),
            column_selection: ColumnSelection::all(),
        }
    }

    /// Check if filter is empty (no predicates and all columns selected)
    pub fn is_empty(&self) -> bool {
        self.predicates.is_empty() && self.column_selection.is_all()
    }
}

impl Default for QueryFilter {
    fn default() -> Self {
        Self::empty()
    }
}

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

    #[test]
    fn test_predicate_value_int_equals() {
        let a = PredicateValue::Int64(42);
        let b = PredicateValue::Int64(42);
        assert!(a.matches(&ComparisonOp::Equals, &b));
    }

    #[test]
    fn test_predicate_value_int_not_equals() {
        let a = PredicateValue::Int64(42);
        let b = PredicateValue::Int64(10);
        assert!(a.matches(&ComparisonOp::NotEquals, &b));
    }

    #[test]
    fn test_predicate_value_int_greater_than() {
        let a = PredicateValue::Int64(42);
        let b = PredicateValue::Int64(10);
        assert!(a.matches(&ComparisonOp::GreaterThan, &b));
    }

    #[test]
    fn test_predicate_value_float_equals() {
        let a = PredicateValue::Float64(3.14);
        let b = PredicateValue::Float64(3.14);
        assert!(a.matches(&ComparisonOp::Equals, &b));
    }

    #[test]
    fn test_predicate_value_string_equals() {
        let a = PredicateValue::String("hello".to_string());
        let b = PredicateValue::String("hello".to_string());
        assert!(a.matches(&ComparisonOp::Equals, &b));
    }

    #[test]
    fn test_column_predicate_matches() {
        let pred = ColumnPredicate::new(
            "age".to_string(),
            ComparisonOp::GreaterThan,
            PredicateValue::Int64(30),
        );

        assert!(pred.matches(&PredicateValue::Int64(35)));
        assert!(!pred.matches(&PredicateValue::Int64(25)));
    }

    #[test]
    fn test_predicate_expression_empty() {
        let expr = PredicateExpression::new();
        assert!(expr.is_empty());
        assert_eq!(expr.len(), 0);
    }

    #[test]
    fn test_predicate_expression_single() {
        let mut expr = PredicateExpression::new();
        expr.add(ColumnPredicate::new(
            "age".to_string(),
            ComparisonOp::GreaterThan,
            PredicateValue::Int64(30),
        ));

        assert!(!expr.is_empty());
        assert_eq!(expr.len(), 1);

        let matches = expr.matches(&[("age".to_string(), PredicateValue::Int64(35))]);
        assert!(matches);
    }

    #[test]
    fn test_predicate_expression_multiple() {
        let mut expr = PredicateExpression::new();
        expr.add(ColumnPredicate::new(
            "age".to_string(),
            ComparisonOp::GreaterThan,
            PredicateValue::Int64(30),
        ));
        expr.add(ColumnPredicate::new(
            "score".to_string(),
            ComparisonOp::LessThan,
            PredicateValue::Int64(100),
        ));

        let matches = expr.matches(&[
            ("age".to_string(), PredicateValue::Int64(35)),
            ("score".to_string(), PredicateValue::Int64(90)),
        ]);
        assert!(matches);

        let no_match = expr.matches(&[
            ("age".to_string(), PredicateValue::Int64(35)),
            ("score".to_string(), PredicateValue::Int64(105)),
        ]);
        assert!(!no_match);
    }

    #[test]
    fn test_column_selection_all() {
        let selection = ColumnSelection::all();
        assert!(selection.is_selected("col1"));
        assert!(selection.is_selected("col2"));
        assert!(selection.is_all());
    }

    #[test]
    fn test_column_selection_specific() {
        let selection = ColumnSelection::new(vec!["col1".to_string(), "col2".to_string()]);
        assert!(selection.is_selected("col1"));
        assert!(selection.is_selected("col2"));
        assert!(!selection.is_selected("col3"));
        assert!(!selection.is_all());
    }

    #[test]
    fn test_column_selection_add() {
        let mut selection = ColumnSelection::all();
        selection.add("col1".to_string());
        assert!(selection.is_selected("col1"));
    }

    #[test]
    fn test_query_filter_empty() {
        let filter = QueryFilter::empty();
        assert!(filter.is_empty());
    }

    #[test]
    fn test_query_filter_with_predicates() {
        let mut predicates = PredicateExpression::new();
        predicates.add(ColumnPredicate::new(
            "age".to_string(),
            ComparisonOp::GreaterThan,
            PredicateValue::Int64(30),
        ));

        let filter = QueryFilter::new(predicates, ColumnSelection::all());
        assert!(!filter.is_empty());
    }

    #[test]
    fn test_query_filter_with_column_selection() {
        let filter = QueryFilter::new(
            PredicateExpression::new(),
            ColumnSelection::new(vec!["col1".to_string()]),
        );
        assert!(!filter.is_empty());
    }

    #[test]
    fn test_range_predicate_contains() {
        let range = RangePredicate::new(
            "age".to_string(),
            PredicateValue::Int64(20),
            PredicateValue::Int64(40),
        );

        assert!(range.contains(&PredicateValue::Int64(30)));
        assert!(range.contains(&PredicateValue::Int64(20)));
        assert!(range.contains(&PredicateValue::Int64(40)));
        assert!(!range.contains(&PredicateValue::Int64(50)));
    }
}