tellaro-query-language 2.0.0

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
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
//! Encoding/decoding mutator tests - matching Python test_encoding_decoding_mutators.py
//!
//! Tests for base64 encode/decode and URL decode mutators.

use serde_json::json;
use tellaro_query_language::Tql;

// =============================================================================
// Base64 Encode Tests
// =============================================================================

#[test]
fn test_b64encode_basic_string() {
    let tql = Tql::new();
    let data = vec![json!({"message": "Hello, World!"})];

    // Base64 encode should work
    let results = tql
        .query(&data, "message | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64encode_empty_string() {
    let tql = Tql::new();
    let data = vec![json!({"value": ""})];

    // Empty string encodes to empty string
    let results = tql
        .query(&data, "value | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64encode_unicode() {
    let tql = Tql::new();
    let data = vec![
        json!({"message": "Hello \u{4e16}\u{754c}"}), // Hello 世界
    ];

    // Unicode strings should encode properly
    let results = tql
        .query(&data, "message | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64encode_special_characters() {
    let tql = Tql::new();
    let data = vec![json!({"value": "test@example.com"})];

    // Special characters should encode
    let results = tql
        .query(&data, "value | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64encode_number() {
    let tql = Tql::new();
    let data = vec![json!({"value": 12345})];

    // Numbers should be converted to string and encoded
    let results = tql
        .query(&data, "value | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64encode_boolean() {
    let tql = Tql::new();
    let data = vec![json!({"flag": true})];

    // Booleans should be converted to string and encoded
    let results = tql
        .query(&data, "flag | b64encode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

// =============================================================================
// Base64 Decode Tests
// =============================================================================

#[test]
fn test_b64decode_basic_string() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "SGVsbG8sIFdvcmxkIQ=="}), // "Hello, World!"
    ];

    // Base64 decode should work
    let results = tql
        .query(&data, "encoded | b64decode = 'Hello, World!'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64decode_with_proper_padding() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "SGVsbG8="}), // "Hello" with proper padding
    ];

    // Proper padding should decode correctly
    let results = tql
        .query(&data, "encoded | b64decode = 'Hello'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64decode_without_padding_fails_gracefully() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "SGVsbG8"}), // "Hello" without padding
    ];

    // Note: Rust's base64 decoder is stricter than Python's
    // Without proper padding, this will either error or return no match
    let result = tql.query(&data, "encoded | b64decode = 'Hello'");
    // Either fails or returns no match
    assert!(result.is_err() || result.unwrap().is_empty());
}

#[test]
fn test_b64decode_empty_string() {
    let tql = Tql::new();
    let data = vec![json!({"value": ""})];

    // Empty string decodes to empty string
    let results = tql
        .query(&data, "value | b64decode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_b64decode_unicode() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "SGVsbG8g5LiW55WM"}), // "Hello 世界"
    ];

    // Unicode decoding should work
    let results = tql
        .query(&data, "encoded | b64decode contains 'Hello'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

/// Invalid base64 makes the DECODED field missing: the query succeeds and the
/// record does not match.
///
/// This test asserted `result.is_ok() || result.is_err()` — which is
/// `assert!(true)`, and could not fail for any behaviour whatsoever. It is the
/// one test in this file whose stated subject is exactly the defect that was
/// found here, and it was blind to it: before the fix Rust never ran the mutator
/// at all, so it returned `Ok([the record])`; it now returns `Ok([])`. Both
/// satisfy the old assertion.
///
/// The rest of this file's `| mutator exists` tests remain vacuous for a
/// DIFFERENT reason and are deliberately left alone: `b64encode`, `hexencode`
/// and `urldecode` cannot fail on their inputs, so `exists` is true whether or
/// not the mutator ran. Making those mean something requires asserting on the
/// VALUE, which is a test-quality change rather than part of this fix.
#[test]
fn test_b64decode_invalid_input() {
    let tql = Tql::new();
    let data = vec![json!({"value": "not-valid-base64!!!"})];

    let hits = tql
        .query(&data, "value | b64decode exists")
        .expect("a mutator that cannot process a value is a DATA error, not a query error");
    assert!(
        hits.is_empty(),
        "the decoded field does not exist, so the record must not match: {hits:?}"
    );

    let negated = tql
        .query(&data, "value | b64decode not exists")
        .expect("the negated spelling must not error either");
    assert_eq!(
        negated.len(),
        1,
        "`not exists` is the complement and must include the record"
    );
}

/// The one axis on which the two engines still differ after the existence fix,
/// pinned so it is a recorded fact rather than something rediscovered.
///
/// A NEGATED comparator over a field whose mutator could not process the value:
///
/// ```text
///   f | b64decode not contains 'x'   on {"f": "123"}   Rust false   Python true
///   f | b64decode::number ne 999     on {"f": "123"}   Rust false   Python true
///   f | b64decode is null            on {"f": "123"}   Rust false   Python true
/// ```
///
/// Rust's answer is the one this codebase has already chosen, in this exact
/// function, for the analogous type-hint case: a skipped record matches nothing,
/// positively OR negatively, because anything else turns every unreadable value
/// into a hit for `ne`, `not_contains` and `not_in`.
///
/// The cause is NOT in the evaluator, which is why it is not fixed here.
/// Python's `b64decode` mutator RETURNS `None` instead of raising, so Python's
/// own leniency rule (`return False` for a data error) never runs — the
/// evaluator sees a legitimate null and applies Lucene absent-semantics, which
/// include a record under a negated comparator. Verified by calling
/// `tql.mutators.apply_mutators` directly: `b64decode("123")` returns `None`.
///
/// Closing it means either making the mutators raise on failure in Python (which
/// also closes the `is null` row) or deciding that a failed decode legitimately
/// yields null and relaxing Rust to match. That is a decision about the MUTATOR
/// layer in both engines, and it needs the same sweep across every mutator that
/// returns `None` on failure.
#[test]
fn a_failed_mutator_under_a_negated_comparator_matches_nothing() {
    let tql = Tql::new();
    let data = vec![json!({"value": "not-valid-base64!!!"})];

    for query in [
        "value | b64decode not contains 'x'",
        "value | b64decode::number ne 999",
        "value | b64decode is null",
    ] {
        let hits = tql
            .query(&data, query)
            .unwrap_or_else(|e| panic!("`{query}` must answer rather than error: {e}"));
        assert!(
            hits.is_empty(),
            "`{query}`: a value the mutator could not process matches nothing, \
             positively or negatively — Python answers true here (see the doc comment)"
        );
    }
}

// =============================================================================
// URL Decode Tests
// =============================================================================

#[test]
fn test_urldecode_basic_string() {
    let tql = Tql::new();
    let data = vec![json!({"url": "hello%20world"})];

    // URL decode %20 to space
    let results = tql
        .query(&data, "url | urldecode = 'hello world'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_special_characters() {
    let tql = Tql::new();
    let data = vec![json!({"value": "test%40example.com"})];

    // URL decode @ symbol
    let results = tql
        .query(&data, "value | urldecode = 'test@example.com'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_multiple_encodings() {
    let tql = Tql::new();
    let data = vec![
        json!({"value": "a%3Db%26c%3Dd"}), // a=b&c=d
    ];

    // Multiple URL encoded characters
    let results = tql
        .query(&data, "value | urldecode = 'a=b&c=d'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_plus_as_space() {
    let tql = Tql::new();
    let data = vec![json!({"value": "hello+world"})];

    // Plus sign treated as space in form encoding
    let results = tql
        .query(&data, "value | urldecode contains 'hello'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_unicode() {
    let tql = Tql::new();
    let data = vec![
        json!({"value": "%E4%B8%96%E7%95%8C"}), // 世界
    ];

    // UTF-8 URL encoding
    let results = tql
        .query(&data, "value | urldecode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_empty_string() {
    let tql = Tql::new();
    let data = vec![json!({"value": ""})];

    // Empty string stays empty
    let results = tql
        .query(&data, "value | urldecode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_no_encoding() {
    let tql = Tql::new();
    let data = vec![json!({"value": "plain-text"})];

    // String with no encoding stays the same
    let results = tql
        .query(&data, "value | urldecode = 'plain-text'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_urldecode_partial_encoding() {
    let tql = Tql::new();
    let data = vec![
        json!({"value": "hello%20world%21test"}), // hello world!test
    ];

    // Mixed encoded and plain text
    let results = tql
        .query(&data, "value | urldecode contains 'hello'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

// =============================================================================
// Hex Encode/Decode Tests
// =============================================================================

#[test]
fn test_hexencode_basic_string() {
    let tql = Tql::new();
    let data = vec![json!({"message": "Hello"})];

    // Hex encode should work
    let results = tql
        .query(&data, "message | hexencode exists")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_hexdecode_basic_string() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "48656c6c6f"}), // "Hello"
    ];

    // Hex decode should work
    let results = tql
        .query(&data, "encoded | hexdecode = 'Hello'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_hexdecode_uppercase() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "48454C4C4F"}), // "HELLO" in uppercase hex
    ];

    // Uppercase hex should decode
    let results = tql
        .query(&data, "encoded | hexdecode = 'HELLO'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

// =============================================================================
// Hash Mutator Tests
// =============================================================================

#[test]
fn test_md5_basic_string() {
    let tql = Tql::new();
    let data = vec![json!({"message": "hello"})];

    // MD5 hash should produce 32 character hex string
    let results = tql
        .query(&data, "message | md5 | length = 32")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_md5_known_hash() {
    let tql = Tql::new();
    let data = vec![json!({"message": "hello"})];

    // MD5 of "hello" is 5d41402abc4b2a76b9719d911017c592
    let results = tql
        .query(&data, "message | md5 = '5d41402abc4b2a76b9719d911017c592'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_sha256_basic_string() {
    let tql = Tql::new();
    let data = vec![json!({"message": "hello"})];

    // SHA256 hash should produce 64 character hex string
    let results = tql
        .query(&data, "message | sha256 | length = 64")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_sha256_known_hash() {
    let tql = Tql::new();
    let data = vec![json!({"message": "hello"})];

    // SHA256 of "hello"
    let results = tql
        .query(
            &data,
            "message | sha256 = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'",
        )
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_md5_empty_string() {
    let tql = Tql::new();
    let data = vec![json!({"value": ""})];

    // MD5 of empty string
    let results = tql
        .query(&data, "value | md5 = 'd41d8cd98f00b204e9800998ecf8427e'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

// =============================================================================
// Chained Encoding Operations
// =============================================================================

#[test]
fn test_encode_decode_roundtrip() {
    let tql = Tql::new();
    let data = vec![json!({"message": "test message"})];

    // Encode then decode should return original
    let results = tql
        .query(&data, "message | b64encode | b64decode = 'test message'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_hex_roundtrip() {
    let tql = Tql::new();
    let data = vec![json!({"message": "test"})];

    // Hex encode then decode should return original
    let results = tql
        .query(&data, "message | hexencode | hexdecode = 'test'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_lowercase_after_hash() {
    let tql = Tql::new();
    let data = vec![json!({"message": "HELLO"})];

    // Hash and then lowercase (hashes are already lowercase)
    let results = tql
        .query(&data, "message | md5 | lowercase | length = 32")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}

// =============================================================================
// Integration with Filtering
// =============================================================================

#[test]
fn test_b64decode_in_filter() {
    let tql = Tql::new();
    let data = vec![
        json!({"encoded": "YWRtaW4=", "name": "Admin User"}), // "admin"
        json!({"encoded": "dXNlcg==", "name": "Regular User"}), // "user"
    ];

    // Filter by decoded value
    let results = tql
        .query(&data, "encoded | b64decode = 'admin'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0]["name"], "Admin User");
}

#[test]
fn test_hash_comparison() {
    let tql = Tql::new();
    let data = vec![
        json!({"password_hash": "5d41402abc4b2a76b9719d911017c592", "user": "alice"}),
        json!({"password_hash": "098f6bcd4621d373cade4e832627b4f6", "user": "bob"}),
    ];

    // Find user with known password hash (md5 of "hello")
    let results = tql
        .query(&data, "password_hash = '5d41402abc4b2a76b9719d911017c592'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0]["user"], "alice");
}

#[test]
fn test_encoded_field_contains() {
    let tql = Tql::new();
    let data = vec![
        json!({"url": "https%3A%2F%2Fexample.com%2Fpath"}),
        json!({"url": "https%3A%2F%2Fother.com%2Fpath"}),
    ];

    // URL decode and check contains
    let results = tql
        .query(&data, "url | urldecode contains 'example.com'")
        .expect("Query should succeed");
    assert_eq!(results.len(), 1);
}