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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! # TQL - Tellaro Query Language
//!
//! A flexible, human-friendly query language for searching and filtering structured data.
//!
//! TQL provides an intuitive syntax similar to SQL WHERE clauses, with support for:
//! - Nested field access (`user.profile.name`)
//! - Rich set of operators (comparison, logical, collection)
//! - Field transformations via mutators (`field | lowercase`)
//! - Statistical aggregations (`| stats count() by field`)
//! - OpenSearch backend integration
//! - GeoIP lookups (MaxMind and DB-IP)
//!
//! ## Quick Start
//!
//! ```ignore
//! use tql::{Tql, TqlConfig};
//! use serde_json::json;
//!
//! let tql = Tql::new(TqlConfig::default());
//! let records = vec![
//!     json!({"name": "John", "age": 30}),
//!     json!({"name": "Jane", "age": 25}),
//! ];
//!
//! let results = tql.query(&records, "age > 25").unwrap();
//! assert_eq!(results.len(), 1);
//! ```
//!
//! ## Feature Parity
//!
//! This Rust implementation maintains 100% feature parity with the Python version,
//! providing identical syntax, behavior, and capabilities.

pub mod comparator;
pub mod error;
pub mod evaluator;
pub mod field_accessor;
pub mod file_ops;
pub mod mutators;
pub mod parser;
pub mod regex_compat;
pub mod stats_evaluator;

// OpenSearch backend support (optional feature)
#[cfg(feature = "opensearch")]
pub mod opensearch;

// Re-export main types
pub use error::{Result, TqlError};
pub use evaluator::TqlEvaluator;
pub use file_ops::{CsvConfig, FileFormat, FileOps};
pub use parser::{AstNode, TqlParser};
pub use regex_compat::{is_lucene_safe, to_lucene_regex, LuceneRegex};
pub use stats_evaluator::{AggregationSpec, GroupBySpec, StatsEvaluator, StatsQuery};

use serde_json::Value as JsonValue;
use std::path::Path;

/// Main TQL query interface
///
/// Provides a high-level API for parsing and executing TQL queries against JSON data.
///
/// # Examples
///
/// ```
/// use tellaro_query_language::Tql;
/// use serde_json::json;
///
/// let tql = Tql::new();
///
/// let records = vec![
///     json!({"name": "Alice", "age": 30}),
///     json!({"name": "Bob", "age": 25}),
/// ];
///
/// let results = tql.query(&records, "age > 25").unwrap();
/// assert_eq!(results.len(), 1);
/// ```
pub struct Tql {
    parser: TqlParser,
    evaluator: TqlEvaluator,
    stats_evaluator: StatsEvaluator,
}

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

impl Tql {
    /// Create a new TQL instance with default settings
    pub fn new() -> Self {
        Self {
            parser: TqlParser::new(),
            evaluator: TqlEvaluator::new(),
            stats_evaluator: StatsEvaluator::new(),
        }
    }

    /// Create a new TQL instance with custom parser depth limit
    pub fn with_max_depth(max_depth: usize) -> Self {
        Self {
            parser: TqlParser::with_max_depth(max_depth),
            evaluator: TqlEvaluator::with_max_depth(max_depth),
            stats_evaluator: StatsEvaluator::new(),
        }
    }

    /// Execute a TQL query against a list of records
    ///
    /// # Arguments
    ///
    /// * `records` - A slice of JSON values to query against
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// A vector of references to matching records
    ///
    /// # Examples
    ///
    /// ```
    /// use tellaro_query_language::Tql;
    /// use serde_json::json;
    ///
    /// let tql = Tql::new();
    /// let records = vec![
    ///     json!({"name": "Alice", "age": 30}),
    ///     json!({"name": "Bob", "age": 20}),
    /// ];
    ///
    /// let results = tql.query(&records, "age >= 25").unwrap();
    /// assert_eq!(results.len(), 1);
    /// ```
    pub fn query<'a>(&self, records: &'a [JsonValue], query: &str) -> Result<Vec<&'a JsonValue>> {
        let ast = self.parser.parse(query)?;
        self.evaluator.filter(&ast, records)
    }

    /// Execute a TQL query with enrichment (applies field mutators to results)
    ///
    /// # Arguments
    ///
    /// * `records` - A slice of JSON values to query against
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// A vector of owned records with mutators applied
    pub fn query_enriched(&self, records: &[JsonValue], query: &str) -> Result<Vec<JsonValue>> {
        let ast = self.parser.parse(query)?;
        self.evaluator.filter_and_enrich(&ast, records)
    }

    /// Count the number of records matching a query
    ///
    /// # Arguments
    ///
    /// * `records` - A slice of JSON values to query against
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// The number of matching records
    pub fn count(&self, records: &[JsonValue], query: &str) -> Result<usize> {
        let ast = self.parser.parse(query)?;
        self.evaluator.count(&ast, records)
    }

    /// Evaluate a query against a single record
    ///
    /// # Arguments
    ///
    /// * `record` - A JSON value to evaluate against
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// true if the record matches the query, false otherwise
    pub fn matches(&self, record: &JsonValue, query: &str) -> Result<bool> {
        let ast = self.parser.parse(query)?;
        self.evaluator.evaluate(&ast, record)
    }

    /// Parse a TQL query into an AST without executing it
    ///
    /// Useful for pre-compiling queries or validating syntax
    pub fn parse(&self, query: &str) -> Result<AstNode> {
        self.parser.parse(query)
    }

    /// Check if a query contains stats expressions
    ///
    /// # Arguments
    ///
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// true if the query contains stats aggregations
    pub fn is_stats_query(&self, query: &str) -> Result<bool> {
        let ast = self.parser.parse(query)?;
        Ok(matches!(
            ast,
            AstNode::StatsExpr(_) | AstNode::QueryWithStats(_)
        ))
    }

    /// Execute a stats query against a list of records
    ///
    /// # Arguments
    ///
    /// * `records` - A slice of JSON values to aggregate
    /// * `query` - The TQL query string containing stats expression
    ///
    /// # Returns
    ///
    /// Aggregated results as a JSON value
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use tql::Tql;
    /// use serde_json::json;
    ///
    /// let tql = Tql::new();
    /// let records = vec![
    ///     json!({"name": "Alice", "status": "active"}),
    ///     json!({"name": "Bob", "status": "active"}),
    ///     json!({"name": "Charlie", "status": "inactive"}),
    /// ];
    ///
    /// let results = tql.evaluate_stats(&records, "| stats count() by status").unwrap();
    /// ```
    pub fn evaluate_stats(&self, records: &[JsonValue], query: &str) -> Result<JsonValue> {
        use crate::parser::QueryWithStatsNode;

        let ast = self.parser.parse(query)?;

        match ast {
            AstNode::StatsExpr(stats_node) => {
                // Pure stats query (no filter)
                self.evaluate_stats_node(records, &stats_node)
            }
            AstNode::QueryWithStats(QueryWithStatsNode { filter, stats }) => {
                // Filter + stats query
                let filtered = self.evaluator.filter(&filter, records)?;
                let owned_records: Vec<JsonValue> = filtered.iter().map(|&r| r.clone()).collect();
                self.evaluate_stats_node(&owned_records, &stats)
            }
            _ => Err(TqlError::SyntaxError {
                message: "Query does not contain stats expressions".to_string(),
                position: None,
                query: Some(query.to_string()),
                suggestions: vec!["Use '| stats' to add aggregations".to_string()],
            }),
        }
    }

    /// Helper to evaluate a stats node
    fn evaluate_stats_node(
        &self,
        records: &[JsonValue],
        stats_node: &parser::StatsNode,
    ) -> Result<JsonValue> {
        // Convert AST stats node to StatsQuery
        // Both the aggregation-level top-N (`modifier` / `limit`) and the
        // group-by bucket limit (`bucket_size`) are lowered by the `From` impls
        // in `stats_evaluator`, NOT field-by-field here. Two hand-written
        // lowering sites is how `params` was dropped once and `modifier` /
        // `limit` / `bucket_size` were dropped again: a field added to the AST
        // node is invisible to whichever site nobody remembered to edit.
        let aggregations: Vec<AggregationSpec> = stats_node
            .aggregations
            .iter()
            .map(AggregationSpec::from)
            .collect();

        let group_by: Vec<GroupBySpec> =
            stats_node.group_by.iter().map(GroupBySpec::from).collect();

        let stats_query = StatsQuery {
            aggregations,
            group_by,
        };

        let mut result = self.stats_evaluator.evaluate_stats(records, &stats_query)?;

        // Attach visualization metadata to the result
        if let Some(obj) = result.as_object_mut() {
            if let Some(hint) = &stats_node.viz_hint {
                obj.insert("viz_hint".to_string(), serde_json::json!(hint));
            }
            if let Some(params) = &stats_node.viz_params {
                obj.insert(
                    "viz_params".to_string(),
                    serde_json::to_value(params).unwrap_or_default(),
                );
            }
        }

        Ok(result)
    }

    /// Query a file with a TQL query
    ///
    /// Delegates to `FileOps::query_file` for file-based query operations.
    pub fn query_file(
        &self,
        path: &Path,
        query: &str,
        format: FileFormat,
        csv_config: &CsvConfig,
    ) -> Result<Vec<JsonValue>> {
        let ops = FileOps::new();
        ops.query_file(path, query, format, csv_config)
    }

    /// Query multiple files in a folder with a TQL query
    ///
    /// Delegates to `FileOps::query_folder` for folder-based query operations.
    pub fn query_folder(
        &self,
        folder_path: &Path,
        query: &str,
        pattern: &str,
        format: FileFormat,
        csv_config: &CsvConfig,
        parallel: bool,
    ) -> Result<Vec<JsonValue>> {
        let ops = FileOps::new();
        ops.query_folder(folder_path, query, pattern, format, csv_config, parallel)
    }

    /// Check if a query contains mutators that require post-processing.
    ///
    /// Post-processing mutators are transformations that cannot be pushed down to
    /// OpenSearch and must be applied to results after they are fetched. These include:
    /// - String transformations: lowercase, uppercase, trim, split, replace, length
    /// - Encoding operations: b64encode, b64decode, urldecode, hexencode, hexdecode
    /// - Network operations: is_private, is_global, refang, defang
    /// - Enrichment lookups: nslookup, geoip, geo_lookup
    /// - Array operations: any, all, none, avg, sum, min, max, count, unique
    ///
    /// When a query contains post-processing mutators, OpenSearch queries must use
    /// `scan_all=True` to fetch all potential matches, then apply the mutators in-memory.
    ///
    /// # Arguments
    ///
    /// * `query` - The TQL query string
    ///
    /// # Returns
    ///
    /// true if the query contains mutators requiring post-processing
    ///
    /// # Examples
    ///
    /// ```
    /// use tellaro_query_language::Tql;
    ///
    /// let tql = Tql::new();
    ///
    /// // No mutators - no post-processing needed
    /// assert!(!tql.has_post_processing_mutators("age > 25").unwrap());
    ///
    /// // lowercase mutator requires post-processing
    /// assert!(tql.has_post_processing_mutators("name | lowercase eq 'john'").unwrap());
    ///
    /// // is_private mutator requires post-processing
    /// assert!(tql.has_post_processing_mutators("source.ip | is_private eq true").unwrap());
    /// ```
    pub fn has_post_processing_mutators(&self, query: &str) -> Result<bool> {
        let ast = self.parser.parse(query)?;
        Ok(Self::ast_has_mutators(&ast))
    }

    /// Check if a parsed AST contains any mutators.
    ///
    /// This is useful when you've already parsed the query and want to check for mutators.
    pub fn ast_has_post_processing_mutators(&self, ast: &AstNode) -> bool {
        Self::ast_has_mutators(ast)
    }

    /// Extract all field names referenced in a TQL query.
    ///
    /// This is useful for optimizing field mappings by only loading
    /// mappings for fields actually used in the query.
    ///
    /// # Example
    ///
    /// ```
    /// use tellaro_query_language::Tql;
    ///
    /// let tql = Tql::new();
    /// let fields = tql.extract_fields("event.code eq 3 AND destination.ip | is_global eq true").unwrap();
    /// assert!(fields.contains(&"event.code".to_string()));
    /// assert!(fields.contains(&"destination.ip".to_string()));
    /// ```
    pub fn extract_fields(&self, query: &str) -> Result<Vec<String>> {
        self.parser.extract_fields(query)
    }

    /// Extract all mutators from an AST.
    ///
    /// Returns a list of (field_name, mutator_list) tuples for all fields with mutators.
    pub fn extract_mutators_from_ast(ast: &AstNode) -> Vec<(String, Vec<parser::Mutator>)> {
        let mut mutators = Vec::new();
        Self::collect_mutators_recursive(ast, &mut mutators);
        mutators
    }

    /// Recursively check if an AST node contains mutators.
    fn ast_has_mutators(node: &AstNode) -> bool {
        match node {
            AstNode::MatchAll => false,
            AstNode::Comparison(comp) => {
                // Check field_mutators
                if let Some(mutators) = &comp.field_mutators {
                    if !mutators.is_empty() {
                        return true;
                    }
                }
                // Check value_mutators
                if let Some(mutators) = &comp.value_mutators {
                    if !mutators.is_empty() {
                        return true;
                    }
                }
                false
            }
            AstNode::LogicalOp(logical) => {
                Self::ast_has_mutators(&logical.left) || Self::ast_has_mutators(&logical.right)
            }
            AstNode::UnaryOp(unary) => Self::ast_has_mutators(&unary.operand),
            AstNode::CollectionOp(coll) => {
                if let Some(mutators) = &coll.field_mutators {
                    if !mutators.is_empty() {
                        return true;
                    }
                }
                false
            }
            AstNode::GeoExpr(_) => {
                // GeoIP expressions always require post-processing
                true
            }
            AstNode::NslookupExpr(_) => {
                // NSLookup expressions always require post-processing
                true
            }
            AstNode::StatsExpr(stats) => {
                // Check if any aggregation has mutators
                for agg in &stats.aggregations {
                    if let Some(mutators) = &agg.field_mutators {
                        if !mutators.is_empty() {
                            return true;
                        }
                    }
                }
                false
            }
            AstNode::QueryWithStats(qws) => {
                // Check both filter and stats
                if Self::ast_has_mutators(&qws.filter) {
                    return true;
                }
                for agg in &qws.stats.aggregations {
                    if let Some(mutators) = &agg.field_mutators {
                        if !mutators.is_empty() {
                            return true;
                        }
                    }
                }
                false
            }
        }
    }

    /// Recursively collect mutators from an AST.
    fn collect_mutators_recursive(
        node: &AstNode,
        mutators: &mut Vec<(String, Vec<parser::Mutator>)>,
    ) {
        match node {
            AstNode::MatchAll => {}
            AstNode::Comparison(comp) => {
                if let Some(field_mutators) = &comp.field_mutators {
                    if !field_mutators.is_empty() {
                        mutators.push((comp.field.clone(), field_mutators.clone()));
                    }
                }
            }
            AstNode::LogicalOp(logical) => {
                Self::collect_mutators_recursive(&logical.left, mutators);
                Self::collect_mutators_recursive(&logical.right, mutators);
            }
            AstNode::UnaryOp(unary) => {
                Self::collect_mutators_recursive(&unary.operand, mutators);
            }
            AstNode::CollectionOp(coll) => {
                if let Some(field_mutators) = &coll.field_mutators {
                    if !field_mutators.is_empty() {
                        mutators.push((coll.field.clone(), field_mutators.clone()));
                    }
                }
            }
            AstNode::GeoExpr(geo) => {
                // GeoIP is implicitly a mutator
                mutators.push((
                    geo.field.clone(),
                    vec![parser::Mutator {
                        name: "geoip".to_string(),
                        args: vec![],
                        named_args: std::collections::HashMap::new(),
                    }],
                ));
            }
            AstNode::NslookupExpr(nslookup) => {
                // NSLookup is implicitly a mutator
                mutators.push((
                    nslookup.field.clone(),
                    vec![parser::Mutator {
                        name: "nslookup".to_string(),
                        args: vec![],
                        named_args: std::collections::HashMap::new(),
                    }],
                ));
            }
            AstNode::StatsExpr(stats) => {
                for agg in &stats.aggregations {
                    if let Some(field_mutators) = &agg.field_mutators {
                        if !field_mutators.is_empty() {
                            if let Some(field) = &agg.field {
                                mutators.push((field.clone(), field_mutators.clone()));
                            }
                        }
                    }
                }
            }
            AstNode::QueryWithStats(qws) => {
                Self::collect_mutators_recursive(&qws.filter, mutators);
                for agg in &qws.stats.aggregations {
                    if let Some(field_mutators) = &agg.field_mutators {
                        if !field_mutators.is_empty() {
                            if let Some(field) = &agg.field {
                                mutators.push((field.clone(), field_mutators.clone()));
                            }
                        }
                    }
                }
            }
        }
    }
}

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

    #[test]
    fn test_tql_creation() {
        let _tql = Tql::new();
    }

    #[test]
    fn test_tql_query() {
        let tql = Tql::new();
        let records = vec![
            json!({"name": "Alice", "age": 30}),
            json!({"name": "Bob", "age": 20}),
            json!({"name": "Charlie", "age": 35}),
        ];

        let results = tql.query(&records, "age > 25").unwrap();
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_tql_count() {
        let tql = Tql::new();
        let records = vec![
            json!({"status": "active"}),
            json!({"status": "inactive"}),
            json!({"status": "active"}),
        ];

        let count = tql.count(&records, "status eq 'active'").unwrap();
        assert_eq!(count, 2);
    }

    #[test]
    fn test_tql_matches() {
        let tql = Tql::new();
        let record = json!({"name": "John", "age": 30});

        assert!(tql.matches(&record, "age >= 25").unwrap());
        assert!(!tql.matches(&record, "age < 25").unwrap());
    }

    #[test]
    fn test_tql_parse() {
        let tql = Tql::new();
        let ast = tql.parse("age > 25 AND name eq 'John'").unwrap();

        // Verify AST was created (just check it doesn't error)
        assert!(matches!(ast, AstNode::LogicalOp(_)));
    }

    #[test]
    fn test_tql_with_mutators() {
        let tql = Tql::new();
        let records = vec![
            json!({"email": "USER@EXAMPLE.COM"}),
            json!({"email": "user@test.org"}),
        ];

        let results = tql
            .query(&records, "email | lowercase contains '@example.com'")
            .unwrap();
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_has_post_processing_mutators_no_mutators() {
        let tql = Tql::new();

        // Simple queries without mutators
        assert!(!tql.has_post_processing_mutators("age > 25").unwrap());
        assert!(!tql.has_post_processing_mutators("name eq 'John'").unwrap());
        assert!(!tql
            .has_post_processing_mutators("age > 25 AND status eq 'active'")
            .unwrap());
        assert!(!tql.has_post_processing_mutators("NOT (age < 18)").unwrap());
        // Test exists operator
        assert!(!tql.has_post_processing_mutators("name exists").unwrap());
    }

    #[test]
    fn test_has_post_processing_mutators_with_mutators() {
        let tql = Tql::new();

        // String mutators
        assert!(tql
            .has_post_processing_mutators("name | lowercase eq 'john'")
            .unwrap());
        assert!(tql
            .has_post_processing_mutators("name | uppercase eq 'JOHN'")
            .unwrap());
        assert!(tql
            .has_post_processing_mutators("message | trim eq 'hello'")
            .unwrap());

        // Network mutators
        assert!(tql
            .has_post_processing_mutators("source.ip | is_private eq true")
            .unwrap());
        assert!(tql
            .has_post_processing_mutators("dest.ip | is_global eq true")
            .unwrap());

        // Compound queries with mutators
        assert!(tql
            .has_post_processing_mutators("age > 25 AND name | lowercase eq 'john'")
            .unwrap());
        assert!(tql
            .has_post_processing_mutators("NOT (name | lowercase eq 'admin')")
            .unwrap());

        // Chained mutators
        assert!(tql
            .has_post_processing_mutators("message | trim | lowercase eq 'hello'")
            .unwrap());
    }

    #[test]
    fn test_extract_mutators_from_ast() {
        let tql = Tql::new();

        // Query with mutators
        let ast = tql.parse("name | lowercase eq 'john'").unwrap();
        let mutators = Tql::extract_mutators_from_ast(&ast);

        assert_eq!(mutators.len(), 1);
        assert_eq!(mutators[0].0, "name");
        assert_eq!(mutators[0].1.len(), 1);
        assert_eq!(mutators[0].1[0].name, "lowercase");
    }

    #[test]
    fn test_extract_mutators_from_ast_chained() {
        let tql = Tql::new();

        // Query with chained mutators
        let ast = tql.parse("message | trim | lowercase eq 'hello'").unwrap();
        let mutators = Tql::extract_mutators_from_ast(&ast);

        assert_eq!(mutators.len(), 1);
        assert_eq!(mutators[0].0, "message");
        assert_eq!(mutators[0].1.len(), 2);
        assert_eq!(mutators[0].1[0].name, "trim");
        assert_eq!(mutators[0].1[1].name, "lowercase");
    }

    #[test]
    fn test_extract_mutators_from_ast_multiple_fields() {
        let tql = Tql::new();

        // Query with mutators on multiple fields
        let ast = tql
            .parse("name | lowercase eq 'john' AND email | uppercase contains 'TEST'")
            .unwrap();
        let mutators = Tql::extract_mutators_from_ast(&ast);

        assert_eq!(mutators.len(), 2);

        // Order may vary based on AST traversal, so check both are present
        let field_names: Vec<&str> = mutators.iter().map(|(f, _)| f.as_str()).collect();
        assert!(field_names.contains(&"name"));
        assert!(field_names.contains(&"email"));
    }
}