nirv-engine 0.1.0

Universal data virtualization and compute orchestration engine with SQL Server, PostgreSQL, REST API, and file system connectors
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
use async_trait::async_trait;
use crate::utils::{InternalQuery, QueryOperation, DataSource, Column, Predicate, PredicateOperator, PredicateValue, OrderBy, OrderColumn, OrderDirection};
use crate::utils::error::{QueryParsingError, NirvResult};
use sqlparser::ast::{Statement, Query, SelectItem, Expr, BinaryOperator, Value as SqlValue, OrderByExpr, FunctionArg, FunctionArgExpr};
use sqlparser::dialect::{PostgreSqlDialect, MySqlDialect, SQLiteDialect, GenericDialect};
use sqlparser::parser::Parser;
use regex::Regex;

/// Trait for SQL query parsing functionality
#[async_trait]
pub trait QueryParser: Send + Sync {
    /// Parse SQL query string into internal representation
    async fn parse_sql(&self, sql: &str) -> NirvResult<InternalQuery>;
    
    /// Validate SQL syntax without full parsing
    async fn validate_syntax(&self, sql: &str) -> NirvResult<bool>;
    
    /// Extract source specifications from SQL
    async fn extract_sources(&self, sql: &str) -> NirvResult<Vec<String>>;
}

/// Default SQL Query Parser that converts SQL statements to internal representation
pub struct DefaultQueryParser {
    postgres_dialect: PostgreSqlDialect,
    mysql_dialect: MySqlDialect,
    sqlite_dialect: SQLiteDialect,
    generic_dialect: GenericDialect,
    source_regex: Regex,
}

impl DefaultQueryParser {
    /// Create a new query parser instance
    pub fn new() -> NirvResult<Self> {
        let source_regex = Regex::new(r#"source\s*\(\s*['"]([^'"]+)['"]\s*\)"#)
            .map_err(|e| QueryParsingError::InvalidSyntax(format!("Failed to compile source regex: {}", e)))?;
        
        Ok(Self {
            postgres_dialect: PostgreSqlDialect {},
            mysql_dialect: MySqlDialect {},
            sqlite_dialect: SQLiteDialect {},
            generic_dialect: GenericDialect {},
            source_regex,
        })
    }

    /// Parse SQL query string into internal representation
    pub fn parse(&self, sql: &str) -> NirvResult<InternalQuery> {
        // Try parsing with different dialects
        let statement = self.try_parse_with_dialects(sql)?;
        
        match statement {
            Statement::Query(query) => self.convert_query(*query),
            _ => Err(QueryParsingError::UnsupportedFeature("Only SELECT queries are currently supported".to_string()).into()),
        }
    }

    /// Try parsing with multiple SQL dialects
    fn try_parse_with_dialects(&self, sql: &str) -> NirvResult<Statement> {
        // Try PostgreSQL dialect first
        if let Ok(statements) = Parser::parse_sql(&self.postgres_dialect, sql) {
            if let Some(statement) = statements.into_iter().next() {
                return Ok(statement);
            }
        }

        // Try MySQL dialect
        if let Ok(statements) = Parser::parse_sql(&self.mysql_dialect, sql) {
            if let Some(statement) = statements.into_iter().next() {
                return Ok(statement);
            }
        }

        // Try SQLite dialect
        if let Ok(statements) = Parser::parse_sql(&self.sqlite_dialect, sql) {
            if let Some(statement) = statements.into_iter().next() {
                return Ok(statement);
            }
        }

        // Try generic dialect as fallback
        if let Ok(statements) = Parser::parse_sql(&self.generic_dialect, sql) {
            if let Some(statement) = statements.into_iter().next() {
                return Ok(statement);
            }
        }

        Err(QueryParsingError::InvalidSyntax("Failed to parse SQL with any supported dialect".to_string()).into())
    }

    /// Convert sqlparser Query to internal representation
    fn convert_query(&self, query: Query) -> NirvResult<InternalQuery> {
        let mut internal_query = InternalQuery::new(QueryOperation::Select);

        if let sqlparser::ast::SetExpr::Select(body) = query.body.as_ref() {
            // Extract projections
            internal_query.projections = self.extract_projections(&body.projection)?;
            
            // Extract data sources from FROM clause
            internal_query.sources = self.extract_sources(&body.from)?;
            
            // Extract WHERE clause predicates
            if let Some(selection) = &body.selection {
                internal_query.predicates = self.extract_predicates(selection)?;
            }
            
            // Extract ORDER BY clause
            if !query.order_by.is_empty() {
                internal_query.ordering = Some(self.extract_order_by(&query.order_by)?);
            }
            
            // Extract LIMIT clause
            if let Some(limit) = &query.limit {
                internal_query.limit = Some(self.extract_limit(limit)?);
            }
        } else {
            return Err(QueryParsingError::UnsupportedFeature("Only SELECT queries are supported".to_string()).into());
        }

        Ok(internal_query)
    }

    /// Extract column projections from SELECT clause
    fn extract_projections(&self, projection: &[SelectItem]) -> NirvResult<Vec<Column>> {
        let mut columns = Vec::new();

        for item in projection {
            match item {
                SelectItem::UnnamedExpr(expr) => {
                    let column = self.extract_column_from_expr(expr, None)?;
                    columns.push(column);
                }
                SelectItem::ExprWithAlias { expr, alias } => {
                    let column = self.extract_column_from_expr(expr, Some(alias.value.clone()))?;
                    columns.push(column);
                }
                SelectItem::Wildcard(_) => {
                    columns.push(Column {
                        name: "*".to_string(),
                        alias: None,
                        source: None,
                    });
                }
                SelectItem::QualifiedWildcard(object_name, _) => {
                    columns.push(Column {
                        name: "*".to_string(),
                        alias: None,
                        source: Some(object_name.to_string()),
                    });
                }
            }
        }

        Ok(columns)
    }

    /// Extract column information from expression
    fn extract_column_from_expr(&self, expr: &Expr, alias: Option<String>) -> NirvResult<Column> {
        match expr {
            Expr::Identifier(ident) => {
                Ok(Column {
                    name: ident.value.clone(),
                    alias,
                    source: None,
                })
            }
            Expr::CompoundIdentifier(idents) => {
                if idents.len() == 2 {
                    Ok(Column {
                        name: idents[1].value.clone(),
                        alias,
                        source: Some(idents[0].value.clone()),
                    })
                } else {
                    Ok(Column {
                        name: idents.last().unwrap().value.clone(),
                        alias,
                        source: None,
                    })
                }
            }
            Expr::Function(func) => {
                // Handle source() function specially
                if func.name.to_string().to_lowercase() == "source" {
                    return Err(QueryParsingError::InvalidSourceFormat("source() function should be used in FROM clause, not SELECT".to_string()).into());
                }
                
                Ok(Column {
                    name: func.name.to_string(),
                    alias,
                    source: None,
                })
            }
            _ => {
                Ok(Column {
                    name: "expr".to_string(),
                    alias,
                    source: None,
                })
            }
        }
    }

    /// Extract data sources from FROM clause
    fn extract_sources(&self, from: &[sqlparser::ast::TableWithJoins]) -> NirvResult<Vec<DataSource>> {
        let mut sources = Vec::new();

        for table_with_joins in from {
            let source = self.extract_source_from_table(&table_with_joins.relation)?;
            sources.push(source);
        }

        if sources.is_empty() {
            return Err(QueryParsingError::MissingSource.into());
        }

        Ok(sources)
    }

    /// Extract data source from table reference
    fn extract_source_from_table(&self, table: &sqlparser::ast::TableFactor) -> NirvResult<DataSource> {
        match table {
            sqlparser::ast::TableFactor::Table { name, alias, args, .. } => {
                let table_name = name.to_string();
                
                // Check if this is a source() function call (table with args)
                if table_name.to_lowercase() == "source" && args.is_some() {
                    let source_spec = self.extract_source_from_function_args(args.as_ref().unwrap())?;
                    Ok(DataSource {
                        object_type: source_spec.0,
                        identifier: source_spec.1,
                        alias: alias.as_ref().map(|a| a.name.value.clone()),
                    })
                } else {
                    // Regular table name - assume it's a database table
                    Ok(DataSource {
                        object_type: "table".to_string(),
                        identifier: table_name,
                        alias: alias.as_ref().map(|a| a.name.value.clone()),
                    })
                }
            }
            sqlparser::ast::TableFactor::Derived { alias, .. } => {
                // Handle subqueries - for now, just create a placeholder
                Ok(DataSource {
                    object_type: "subquery".to_string(),
                    identifier: "derived".to_string(),
                    alias: alias.as_ref().map(|a| a.name.value.clone()),
                })
            }
            sqlparser::ast::TableFactor::Function { name, args, alias, .. } => {
                // Handle function calls like source()
                if name.to_string().to_lowercase() == "source" {
                    let source_spec = self.extract_source_from_function_args(args)?;
                    Ok(DataSource {
                        object_type: source_spec.0,
                        identifier: source_spec.1,
                        alias: alias.as_ref().map(|a| a.name.value.clone()),
                    })
                } else {
                    Err(QueryParsingError::UnsupportedFeature(format!("Function {} not supported in FROM clause", name)).into())
                }
            }
            _ => Err(QueryParsingError::UnsupportedFeature("Unsupported table reference type".to_string()).into()),
        }
    }

    /// Extract source specification from source() function
    fn extract_source_function(&self, table_name: &str) -> NirvResult<Option<(String, String)>> {
        if let Some(captures) = self.source_regex.captures(table_name) {
            if let Some(source_spec) = captures.get(1) {
                let spec = source_spec.as_str();
                if let Some(dot_pos) = spec.find('.') {
                    let object_type = spec[..dot_pos].to_string();
                    let identifier = spec[dot_pos + 1..].to_string();
                    Ok(Some((object_type, identifier)))
                } else {
                    // No dot found, treat entire spec as identifier with default type
                    Ok(Some(("table".to_string(), spec.to_string())))
                }
            } else {
                Err(QueryParsingError::InvalidSourceFormat("Empty source specification".to_string()).into())
            }
        } else {
            Ok(None)
        }
    }

    /// Extract source specification from function arguments
    fn extract_source_from_function_args(&self, args: &[FunctionArg]) -> NirvResult<(String, String)> {
        if args.len() != 1 {
            return Err(QueryParsingError::InvalidSourceFormat("source() function requires exactly one argument".to_string()).into());
        }

        if let FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(SqlValue::SingleQuotedString(spec)))) = &args[0] {
            if let Some(dot_pos) = spec.find('.') {
                let object_type = spec[..dot_pos].to_string();
                let identifier = spec[dot_pos + 1..].to_string();
                Ok((object_type, identifier))
            } else {
                // No dot found, treat entire spec as identifier with default type
                Ok(("table".to_string(), spec.to_string()))
            }
        } else {
            Err(QueryParsingError::InvalidSourceFormat("source() function argument must be a string literal".to_string()).into())
        }
    }

    /// Extract predicates from WHERE clause
    fn extract_predicates(&self, expr: &Expr) -> NirvResult<Vec<Predicate>> {
        let mut predicates = Vec::new();
        self.extract_predicates_recursive(expr, &mut predicates)?;
        Ok(predicates)
    }

    /// Recursively extract predicates from expression tree
    fn extract_predicates_recursive(&self, expr: &Expr, predicates: &mut Vec<Predicate>) -> NirvResult<()> {
        match expr {
            Expr::BinaryOp { left, op, right } => {
                match op {
                    BinaryOperator::And => {
                        // Handle AND - recursively process both sides
                        self.extract_predicates_recursive(left, predicates)?;
                        self.extract_predicates_recursive(right, predicates)?;
                    }
                    BinaryOperator::Or => {
                        // For now, treat OR as separate predicates (simplified)
                        self.extract_predicates_recursive(left, predicates)?;
                        self.extract_predicates_recursive(right, predicates)?;
                    }
                    _ => {
                        // Handle comparison operators
                        let predicate = self.create_predicate_from_binary_op(left, op, right)?;
                        predicates.push(predicate);
                    }
                }
            }
            Expr::IsNull(expr) => {
                let column = self.extract_column_name_from_expr(expr)?;
                predicates.push(Predicate {
                    column,
                    operator: PredicateOperator::IsNull,
                    value: PredicateValue::Null,
                });
            }
            Expr::IsNotNull(expr) => {
                let column = self.extract_column_name_from_expr(expr)?;
                predicates.push(Predicate {
                    column,
                    operator: PredicateOperator::IsNotNull,
                    value: PredicateValue::Null,
                });
            }
            _ => {
                // For other expression types, we'll skip for now
            }
        }
        Ok(())
    }

    /// Create predicate from binary operation
    fn create_predicate_from_binary_op(&self, left: &Expr, op: &BinaryOperator, right: &Expr) -> NirvResult<Predicate> {
        let column = self.extract_column_name_from_expr(left)?;
        let operator = self.convert_binary_operator(op)?;
        let value = self.extract_predicate_value_from_expr(right)?;

        Ok(Predicate {
            column,
            operator,
            value,
        })
    }

    /// Extract column name from expression
    fn extract_column_name_from_expr(&self, expr: &Expr) -> NirvResult<String> {
        match expr {
            Expr::Identifier(ident) => Ok(ident.value.clone()),
            Expr::CompoundIdentifier(idents) => {
                if idents.len() >= 2 {
                    Ok(format!("{}.{}", idents[0].value, idents[1].value))
                } else {
                    Ok(idents[0].value.clone())
                }
            }
            _ => Err(QueryParsingError::InvalidSyntax("Expected column identifier in predicate".to_string()).into()),
        }
    }

    /// Convert sqlparser binary operator to internal representation
    fn convert_binary_operator(&self, op: &BinaryOperator) -> NirvResult<PredicateOperator> {
        match op {
            BinaryOperator::Eq => Ok(PredicateOperator::Equal),
            BinaryOperator::NotEq => Ok(PredicateOperator::NotEqual),
            BinaryOperator::Gt => Ok(PredicateOperator::GreaterThan),
            BinaryOperator::GtEq => Ok(PredicateOperator::GreaterThanOrEqual),
            BinaryOperator::Lt => Ok(PredicateOperator::LessThan),
            BinaryOperator::LtEq => Ok(PredicateOperator::LessThanOrEqual),
            // Note: LIKE operator handling will be added when we determine the correct variant name
            _ => Err(QueryParsingError::UnsupportedFeature(format!("Operator {:?} not supported", op)).into()),
        }
    }

    /// Extract predicate value from expression
    fn extract_predicate_value_from_expr(&self, expr: &Expr) -> NirvResult<PredicateValue> {
        match expr {
            Expr::Value(sql_value) => self.convert_sql_value(sql_value),
            Expr::Identifier(ident) => Ok(PredicateValue::String(ident.value.clone())),
            _ => Err(QueryParsingError::UnsupportedFeature("Complex expressions in predicates not yet supported".to_string()).into()),
        }
    }

    /// Convert sqlparser Value to internal PredicateValue
    fn convert_sql_value(&self, value: &SqlValue) -> NirvResult<PredicateValue> {
        match value {
            SqlValue::SingleQuotedString(s) | SqlValue::DoubleQuotedString(s) => {
                Ok(PredicateValue::String(s.clone()))
            }
            SqlValue::Number(n, _) => {
                if let Ok(int_val) = n.parse::<i64>() {
                    Ok(PredicateValue::Integer(int_val))
                } else if let Ok(float_val) = n.parse::<f64>() {
                    Ok(PredicateValue::Number(float_val))
                } else {
                    Err(QueryParsingError::InvalidSyntax(format!("Invalid number format: {}", n)).into())
                }
            }
            SqlValue::Boolean(b) => Ok(PredicateValue::Boolean(*b)),
            SqlValue::Null => Ok(PredicateValue::Null),
            _ => Err(QueryParsingError::UnsupportedFeature(format!("Value type {:?} not supported", value)).into()),
        }
    }

    /// Extract ORDER BY clause
    fn extract_order_by(&self, order_by: &[OrderByExpr]) -> NirvResult<OrderBy> {
        let mut columns = Vec::new();

        for order_expr in order_by {
            let column_name = self.extract_column_name_from_expr(&order_expr.expr)?;
            let direction = if order_expr.asc.unwrap_or(true) {
                OrderDirection::Ascending
            } else {
                OrderDirection::Descending
            };

            columns.push(OrderColumn {
                column: column_name,
                direction,
            });
        }

        Ok(OrderBy { columns })
    }

    /// Extract LIMIT value
    fn extract_limit(&self, limit_expr: &Expr) -> NirvResult<u64> {
        match limit_expr {
            Expr::Value(SqlValue::Number(n, _)) => {
                n.parse::<u64>()
                    .map_err(|_| QueryParsingError::InvalidSyntax(format!("Invalid LIMIT value: {}", n)).into())
            }
            _ => Err(QueryParsingError::InvalidSyntax("LIMIT must be a number".to_string()).into()),
        }
    }
}

impl Default for DefaultQueryParser {
    fn default() -> Self {
        Self::new().expect("Failed to create default QueryParser")
    }
}

#[async_trait]
impl QueryParser for DefaultQueryParser {
    async fn parse_sql(&self, sql: &str) -> NirvResult<InternalQuery> {
        self.parse(sql)
    }
    
    async fn validate_syntax(&self, sql: &str) -> NirvResult<bool> {
        match self.try_parse_with_dialects(sql) {
            Ok(_) => Ok(true),
            Err(_) => Ok(false),
        }
    }
    
    async fn extract_sources(&self, sql: &str) -> NirvResult<Vec<String>> {
        let query = self.parse(sql)?;
        Ok(query.sources.into_iter()
            .map(|source| format!("{}.{}", source.object_type, source.identifier))
            .collect())
    }
}
#[cfg
(test)]
mod tests {
    use super::*;
    use crate::utils::{QueryOperation, DataSource, Column, Predicate, PredicateOperator, PredicateValue, OrderDirection};

    fn create_parser() -> DefaultQueryParser {
        DefaultQueryParser::new().expect("Failed to create parser")
    }

    #[test]
    fn test_parser_creation() {
        let parser = DefaultQueryParser::new();
        assert!(parser.is_ok());
    }

    #[test]
    fn test_simple_select_all() {
        let parser = create_parser();
        let sql = "SELECT * FROM source('postgres.users')";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert_eq!(query.operation, QueryOperation::Select);
        assert_eq!(query.projections.len(), 1);
        assert_eq!(query.projections[0].name, "*");
        assert_eq!(query.sources.len(), 1);
        assert_eq!(query.sources[0].object_type, "postgres");
        assert_eq!(query.sources[0].identifier, "users");
    }

    #[test]
    fn test_select_with_columns() {
        let parser = create_parser();
        let sql = "SELECT id, name, email FROM source('postgres.users')";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert_eq!(query.projections.len(), 3);
        assert_eq!(query.projections[0].name, "id");
        assert_eq!(query.projections[1].name, "name");
        assert_eq!(query.projections[2].name, "email");
    }

    #[test]
    fn test_select_with_aliases() {
        let parser = create_parser();
        let sql = "SELECT id as user_id, name as full_name FROM source('postgres.users') as u";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert_eq!(query.projections.len(), 2);
        assert_eq!(query.projections[0].name, "id");
        assert_eq!(query.projections[0].alias, Some("user_id".to_string()));
        assert_eq!(query.projections[1].name, "name");
        assert_eq!(query.projections[1].alias, Some("full_name".to_string()));
        assert_eq!(query.sources[0].alias, Some("u".to_string()));
    }

    #[test]
    fn test_source_function_parsing() {
        let parser = create_parser();
        
        // Test different source formats
        let test_cases = vec![
            ("SELECT * FROM source('postgres.users')", "postgres", "users"),
            ("SELECT * FROM source('file.data.csv')", "file", "data.csv"),
            ("SELECT * FROM source('api.endpoint')", "api", "endpoint"),
            ("SELECT * FROM source('users')", "table", "users"), // Default type
        ];

        for (sql, expected_type, expected_id) in test_cases {
            let result = parser.parse(sql);
            assert!(result.is_ok(), "Failed to parse: {}", sql);
            
            let query = result.unwrap();
            assert_eq!(query.sources.len(), 1);
            assert_eq!(query.sources[0].object_type, expected_type);
            assert_eq!(query.sources[0].identifier, expected_id);
        }
    }

    #[test]
    fn test_where_clause_parsing() {
        let parser = create_parser();
        let sql = "SELECT * FROM source('postgres.users') WHERE age > 18 AND name = 'John'";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert_eq!(query.predicates.len(), 2);
        
        // First predicate: age > 18
        assert_eq!(query.predicates[0].column, "age");
        assert_eq!(query.predicates[0].operator, PredicateOperator::GreaterThan);
        assert_eq!(query.predicates[0].value, PredicateValue::Integer(18));
        
        // Second predicate: name = 'John'
        assert_eq!(query.predicates[1].column, "name");
        assert_eq!(query.predicates[1].operator, PredicateOperator::Equal);
        assert_eq!(query.predicates[1].value, PredicateValue::String("John".to_string()));
    }

    #[test]
    fn test_various_operators() {
        let parser = create_parser();
        
        let test_cases = vec![
            ("SELECT * FROM source('test') WHERE id = 1", PredicateOperator::Equal),
            ("SELECT * FROM source('test') WHERE id != 1", PredicateOperator::NotEqual),
            ("SELECT * FROM source('test') WHERE id > 1", PredicateOperator::GreaterThan),
            ("SELECT * FROM source('test') WHERE id >= 1", PredicateOperator::GreaterThanOrEqual),
            ("SELECT * FROM source('test') WHERE id < 1", PredicateOperator::LessThan),
            ("SELECT * FROM source('test') WHERE id <= 1", PredicateOperator::LessThanOrEqual),
            // LIKE operator test removed until we determine correct variant name
        ];

        for (sql, expected_op) in test_cases {
            let result = parser.parse(sql);
            assert!(result.is_ok(), "Failed to parse: {}", sql);
            
            let query = result.unwrap();
            assert_eq!(query.predicates.len(), 1);
            assert_eq!(query.predicates[0].operator, expected_op);
        }
    }

    #[test]
    fn test_null_predicates() {
        let parser = create_parser();
        
        let sql1 = "SELECT * FROM source('test') WHERE name IS NULL";
        let result1 = parser.parse(sql1);
        assert!(result1.is_ok());
        let query1 = result1.unwrap();
        assert_eq!(query1.predicates.len(), 1);
        assert_eq!(query1.predicates[0].operator, PredicateOperator::IsNull);
        
        let sql2 = "SELECT * FROM source('test') WHERE name IS NOT NULL";
        let result2 = parser.parse(sql2);
        assert!(result2.is_ok());
        let query2 = result2.unwrap();
        assert_eq!(query2.predicates.len(), 1);
        assert_eq!(query2.predicates[0].operator, PredicateOperator::IsNotNull);
    }

    #[test]
    fn test_order_by_clause() {
        let parser = create_parser();
        let sql = "SELECT * FROM source('postgres.users') ORDER BY name ASC, age DESC";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert!(query.ordering.is_some());
        let ordering = query.ordering.unwrap();
        assert_eq!(ordering.columns.len(), 2);
        
        assert_eq!(ordering.columns[0].column, "name");
        assert_eq!(ordering.columns[0].direction, OrderDirection::Ascending);
        
        assert_eq!(ordering.columns[1].column, "age");
        assert_eq!(ordering.columns[1].direction, OrderDirection::Descending);
    }

    #[test]
    fn test_limit_clause() {
        let parser = create_parser();
        let sql = "SELECT * FROM source('postgres.users') LIMIT 10";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        assert!(query.limit.is_some());
        assert_eq!(query.limit.unwrap(), 10);
    }

    #[test]
    fn test_complex_query() {
        let parser = create_parser();
        let sql = "SELECT id, name as full_name FROM source('postgres.users') as u WHERE age >= 21 AND status = 'active' ORDER BY name ASC LIMIT 50";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        // Check projections
        assert_eq!(query.projections.len(), 2);
        assert_eq!(query.projections[0].name, "id");
        assert_eq!(query.projections[1].alias, Some("full_name".to_string()));
        
        // Check source
        assert_eq!(query.sources.len(), 1);
        assert_eq!(query.sources[0].object_type, "postgres");
        assert_eq!(query.sources[0].identifier, "users");
        assert_eq!(query.sources[0].alias, Some("u".to_string()));
        
        // Check predicates
        assert_eq!(query.predicates.len(), 2);
        
        // Check ordering
        assert!(query.ordering.is_some());
        
        // Check limit
        assert_eq!(query.limit, Some(50));
    }

    #[test]
    fn test_postgresql_dialect() {
        let parser = create_parser();
        let sql = "SELECT id, name FROM source('postgres.users') WHERE created_at > '2023-01-01'";
        let result = parser.parse(sql);
        
        // Should parse successfully with basic PostgreSQL syntax
        assert!(result.is_ok());
    }

    #[test]
    fn test_mysql_dialect() {
        let parser = create_parser();
        let sql = "SELECT id, name FROM source('mysql.users') WHERE created_at > '2023-01-01'";
        let result = parser.parse(sql);
        
        // Should parse successfully with basic MySQL syntax
        assert!(result.is_ok());
    }

    #[test]
    fn test_sqlite_dialect() {
        let parser = create_parser();
        let sql = "SELECT id, name FROM source('sqlite.users') WHERE created_at > '2023-01-01'";
        let result = parser.parse(sql);
        
        // Should parse successfully with basic SQLite syntax
        assert!(result.is_ok());
    }

    #[test]
    fn test_invalid_sql_syntax() {
        let parser = create_parser();
        let sql = "INVALID SQL SYNTAX";
        let result = parser.parse(sql);
        
        assert!(result.is_err());
        match result.unwrap_err() {
            crate::utils::error::NirvError::QueryParsing(QueryParsingError::InvalidSyntax(_)) => {},
            _ => panic!("Expected InvalidSyntax error"),
        }
    }

    #[test]
    fn test_missing_source_function() {
        let parser = create_parser();
        let sql = "SELECT * FROM users"; // No source() function
        let result = parser.parse(sql);
        
        assert!(result.is_ok()); // Should still parse, treating as regular table
        let query = result.unwrap();
        assert_eq!(query.sources[0].object_type, "table");
        assert_eq!(query.sources[0].identifier, "users");
    }

    #[test]
    fn test_invalid_source_format() {
        let parser = create_parser();
        let sql = "SELECT * FROM source()"; // Empty source function
        let result = parser.parse(sql);
        
        assert!(result.is_err());
        // The error could be InvalidSourceFormat or InvalidSyntax depending on parsing
        assert!(matches!(result.unwrap_err(), 
            crate::utils::error::NirvError::QueryParsing(QueryParsingError::InvalidSourceFormat(_)) |
            crate::utils::error::NirvError::QueryParsing(QueryParsingError::InvalidSyntax(_))
        ));
    }

    #[test]
    fn test_unsupported_query_type() {
        let parser = create_parser();
        let sql = "INSERT INTO users (name) VALUES ('John')";
        let result = parser.parse(sql);
        
        assert!(result.is_err());
        match result.unwrap_err() {
            crate::utils::error::NirvError::QueryParsing(QueryParsingError::UnsupportedFeature(_)) => {},
            _ => panic!("Expected UnsupportedFeature error"),
        }
    }

    #[test]
    fn test_source_function_in_select_clause() {
        let parser = create_parser();
        let sql = "SELECT source('test') FROM users";
        let result = parser.parse(sql);
        
        assert!(result.is_err());
        match result.unwrap_err() {
            crate::utils::error::NirvError::QueryParsing(QueryParsingError::InvalidSourceFormat(_)) => {},
            _ => panic!("Expected InvalidSourceFormat error"),
        }
    }

    #[test]
    fn test_compound_identifiers() {
        let parser = create_parser();
        let sql = "SELECT u.id, u.name FROM source('postgres.users') as u WHERE u.age > 18";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        
        // Check projections with table prefixes
        assert_eq!(query.projections.len(), 2);
        assert_eq!(query.projections[0].name, "id");
        assert_eq!(query.projections[0].source, Some("u".to_string()));
        assert_eq!(query.projections[1].name, "name");
        assert_eq!(query.projections[1].source, Some("u".to_string()));
        
        // Check predicate with table prefix
        assert_eq!(query.predicates.len(), 1);
        assert_eq!(query.predicates[0].column, "u.age");
    }

    #[test]
    fn test_various_value_types() {
        let parser = create_parser();
        
        let test_cases = vec![
            ("SELECT * FROM source('test') WHERE str_col = 'text'", PredicateValue::String("text".to_string())),
            ("SELECT * FROM source('test') WHERE int_col = 42", PredicateValue::Integer(42)),
            ("SELECT * FROM source('test') WHERE float_col = 3.14", PredicateValue::Number(3.14)),
            ("SELECT * FROM source('test') WHERE bool_col = true", PredicateValue::Boolean(true)),
            ("SELECT * FROM source('test') WHERE null_col = NULL", PredicateValue::Null),
        ];

        for (sql, expected_value) in test_cases {
            let result = parser.parse(sql);
            assert!(result.is_ok(), "Failed to parse: {}", sql);
            
            let query = result.unwrap();
            assert_eq!(query.predicates.len(), 1);
            assert_eq!(query.predicates[0].value, expected_value);
        }
    }

    #[test]
    fn test_double_quoted_strings() {
        let parser = create_parser();
        let sql = r#"SELECT * FROM source('postgres.users') WHERE name = "John""#;
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        assert_eq!(query.sources[0].object_type, "postgres");
        assert_eq!(query.sources[0].identifier, "users");
        assert_eq!(query.predicates[0].value, PredicateValue::String("John".to_string()));
    }

    #[test]
    fn test_qualified_wildcard() {
        let parser = create_parser();
        let sql = "SELECT u.* FROM source('postgres.users') as u";
        let result = parser.parse(sql);
        
        assert!(result.is_ok());
        let query = result.unwrap();
        assert_eq!(query.projections.len(), 1);
        assert_eq!(query.projections[0].name, "*");
        assert_eq!(query.projections[0].source, Some("u".to_string()));
    }
}