tellaro-query-language 3.0.1

A flexible, human-friendly query language for searching and filtering structured data
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
//! List evaluation mutators for aggregate operations on arrays.
//!
//! Provides mutators that operate on array fields to compute aggregate values.

use super::{Mutator, MutatorParams};
use crate::error::Result;
use serde_json::{json, Value as JsonValue};

/// Mutator that evaluates if any element in a list is truthy
pub struct AnyMutator {
    _params: MutatorParams,
}

impl AnyMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for AnyMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(json!(false)),
            JsonValue::Array(arr) => {
                let result = arr.iter().any(is_truthy);
                Ok(json!(result))
            }
            _ => Ok(json!(is_truthy(value))),
        }
    }

    fn name(&self) -> &str {
        "any"
    }
}

/// Mutator that evaluates if all elements in a list are truthy
pub struct AllMutator {
    _params: MutatorParams,
}

impl AllMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for AllMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(json!(false)),
            JsonValue::Array(arr) => {
                let result = arr.iter().all(is_truthy);
                Ok(json!(result))
            }
            _ => Ok(json!(is_truthy(value))),
        }
    }

    fn name(&self) -> &str {
        "all"
    }
}

/// Mutator that calculates the average of numeric values
pub struct AvgMutator {
    _params: MutatorParams,
}

impl AvgMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for AvgMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(JsonValue::Null),
            JsonValue::Array(arr) => {
                let numeric_values: Vec<f64> = arr.iter().filter_map(to_numeric).collect();

                if numeric_values.is_empty() {
                    Ok(JsonValue::Null)
                } else {
                    let avg = numeric_values.iter().sum::<f64>() / numeric_values.len() as f64;
                    Ok(json!(avg))
                }
            }
            _ => {
                if let Some(num) = to_numeric(value) {
                    Ok(json!(num))
                } else {
                    Ok(JsonValue::Null)
                }
            }
        }
    }

    fn name(&self) -> &str {
        "avg"
    }
}

/// Alias for AvgMutator
pub struct AverageMutator {
    params: MutatorParams,
}

impl AverageMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { params }
    }
}

impl Mutator for AverageMutator {
    fn apply(&self, field_name: &str, record: &JsonValue, value: &JsonValue) -> Result<JsonValue> {
        let avg_mutator = AvgMutator::new(self.params.clone());
        avg_mutator.apply(field_name, record, value)
    }

    fn name(&self) -> &str {
        "average"
    }
}

/// Mutator that calculates the sum of numeric values
pub struct SumMutator {
    _params: MutatorParams,
}

impl SumMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for SumMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(json!(0.0)),
            JsonValue::Array(arr) => {
                let sum: f64 = arr.iter().filter_map(to_numeric).sum();
                Ok(json!(sum))
            }
            _ => {
                if let Some(num) = to_numeric(value) {
                    Ok(json!(num))
                } else {
                    Ok(json!(0.0))
                }
            }
        }
    }

    fn name(&self) -> &str {
        "sum"
    }
}

/// Mutator that finds the maximum numeric value
pub struct MaxMutator {
    _params: MutatorParams,
}

impl MaxMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for MaxMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(JsonValue::Null),
            JsonValue::Array(arr) => {
                let max = arr
                    .iter()
                    .filter_map(to_numeric)
                    .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

                if let Some(max_val) = max {
                    Ok(json!(max_val))
                } else {
                    Ok(JsonValue::Null)
                }
            }
            _ => {
                if let Some(num) = to_numeric(value) {
                    Ok(json!(num))
                } else {
                    Ok(JsonValue::Null)
                }
            }
        }
    }

    fn name(&self) -> &str {
        "max"
    }
}

/// Mutator that finds the minimum numeric value
pub struct MinMutator {
    _params: MutatorParams,
}

impl MinMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for MinMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::Null => Ok(JsonValue::Null),
            JsonValue::Array(arr) => {
                let min = arr
                    .iter()
                    .filter_map(to_numeric)
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

                if let Some(min_val) = min {
                    Ok(json!(min_val))
                } else {
                    Ok(JsonValue::Null)
                }
            }
            _ => {
                if let Some(num) = to_numeric(value) {
                    Ok(json!(num))
                } else {
                    Ok(JsonValue::Null)
                }
            }
        }
    }

    fn name(&self) -> &str {
        "min"
    }
}

/// Helper function to determine if a value is truthy
fn is_truthy(value: &JsonValue) -> bool {
    match value {
        JsonValue::Null => false,
        JsonValue::Bool(b) => *b,
        JsonValue::Number(n) => n.as_f64().unwrap_or(0.0) != 0.0,
        JsonValue::String(s) => !s.is_empty(),
        JsonValue::Array(arr) => !arr.is_empty(),
        JsonValue::Object(obj) => !obj.is_empty(),
    }
}

/// Helper function to convert value to numeric
fn to_numeric(value: &JsonValue) -> Option<f64> {
    match value {
        JsonValue::Number(n) => n.as_f64(),
        JsonValue::String(s) => s.parse::<f64>().ok(),
        JsonValue::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::collections::HashMap;

    #[test]
    fn test_any_mutator() {
        let mutator = AnyMutator::new(HashMap::new());
        let record = json!({});

        // Test array with truthy values
        let value = json!([true, false, true]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(true));

        // Test array with all falsy values
        let value = json!([false, 0, ""]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(false));

        // Test single value
        let value = json!(42);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(true));

        // Test null
        let value = JsonValue::Null;
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(false));
    }

    #[test]
    fn test_all_mutator() {
        let mutator = AllMutator::new(HashMap::new());
        let record = json!({});

        // Test array with all truthy values
        let value = json!([true, 1, "hello"]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(true));

        // Test array with some falsy values
        let value = json!([true, false, true]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(false));

        // Test empty array (should be true, following Python's all() behavior)
        let value = json!([]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(true));
    }

    #[test]
    fn test_avg_mutator() {
        let mutator = AvgMutator::new(HashMap::new());
        let record = json!({});

        // Test numeric array
        let value = json!([10, 20, 30]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(20.0));

        // Test mixed array (strings that can be parsed)
        let value = json!([10, "20", 30]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(20.0));

        // Test single value
        let value = json!(42);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(42.0));

        // Test empty array
        let value = json!([]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, JsonValue::Null);
    }

    #[test]
    fn test_sum_mutator() {
        let mutator = SumMutator::new(HashMap::new());
        let record = json!({});

        // Test numeric array
        let value = json!([10, 20, 30]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(60.0));

        // Test single value
        let value = json!(42);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(42.0));

        // Test null
        let value = JsonValue::Null;
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(0.0));
    }

    #[test]
    fn test_max_mutator() {
        let mutator = MaxMutator::new(HashMap::new());
        let record = json!({});

        // Test numeric array
        let value = json!([10, 50, 30]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(50.0));

        // Test single value
        let value = json!(42);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(42.0));
    }

    #[test]
    fn test_min_mutator() {
        let mutator = MinMutator::new(HashMap::new());
        let record = json!({});

        // Test numeric array
        let value = json!([10, 50, 30]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(10.0));

        // Test single value
        let value = json!(42);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(42.0));
    }
}