hyperQL 0.1.0

Query language for hyperspatial graph databases with temporal and spatial semantics
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
use crate::ast::*;
use crate::error::Result;
use crate::validator::{
    ValidationConfig, ValidationResult, ValidationError, ValidationWarning,
    ValidationErrorKind, ValidationWarningKind,
};
use std::collections::HashSet;

/// Statement-level validator for different query types
pub struct StatementValidator {
    config: ValidationConfig,
}

impl StatementValidator {
    /// Create a new statement validator
    pub fn new(config: &ValidationConfig) -> Self {
        Self {
            config: config.clone(),
        }
    }

    /// Update configuration
    pub fn update_config(&mut self, config: &ValidationConfig) {
        self.config = config.clone();
    }

    /// Validate a statement
    pub fn validate(&self, statement: &Statement, result: &mut ValidationResult) -> Result<()> {
        result.metadata.phases_completed.push("statement".to_string());
        
        match statement {
            Statement::Select(select) => self.validate_select_statement(select, result),
            Statement::Insert(insert) => self.validate_insert_statement(insert, result),
            Statement::Update(update) => self.validate_update_statement(update, result),
            Statement::Delete(delete) => self.validate_delete_statement(delete, result),
            Statement::Schema(_schema_op) => {
                // TODO: Implement schema DDL validation
                // Schema statements need validation for field definitions, cascade configs, etc.
                Ok(())
            },
            Statement::Stream(_stream_op) => {
                // TODO: Implement stream DDL validation
                // Stream statements need validation for stream configs and consumer actions
                Ok(())
            },
        }
    }

    /// Validate SELECT statement structure and clauses
    fn validate_select_statement(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
        // Validate projection
        self.validate_select_projection(&select.select_list, result)?;
        
        // Validate FROM clause
        if let Some(from_clause) = &select.from {
            self.validate_from_clause(from_clause, result)?;
        }
        
        // Validate clause relationships and ordering
        self.validate_clause_relationships(select, result)?;

        // Validate DISTINCT usage
        if select.distinct {
            self.validate_distinct_usage(select, result)?;
        }

        // Validate multi-paradigm specific constructs
        if let Some(traverse) = &select.traverse_clause {
            self.validate_traverse_statement(traverse, result)?;
        }

        Ok(())
    }

    /// Validate INSERT statement structure
    fn validate_insert_statement(&self, insert: &InsertStatement, result: &mut ValidationResult) -> Result<()> {
        // Validate table name
        if insert.table.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "INSERT statement requires a table name".to_string(),
                )
            );
        }

        // Validate column/value relationship
        if !insert.columns.is_empty() {
            // Validate column count matches value count for each row
            for (row_idx, value_row) in insert.values.iter().enumerate() {
                if insert.columns.len() != value_row.len() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::StructuralError,
                            format!(
                                "Row {}: Column count ({}) does not match value count ({})",
                                row_idx + 1,
                                insert.columns.len(),
                                value_row.len()
                            ),
                        )
                    );
                }
            }

            // Check for duplicate columns
            let mut seen_columns = HashSet::new();
            for column in &insert.columns {
                if !seen_columns.insert(column) {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::StructuralError,
                            format!("Duplicate column '{}' in INSERT statement", column),
                        )
                    );
                }
            }
        }

        // Validate that we have values to insert
        if insert.values.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "INSERT statement must specify values to insert".to_string(),
                )
            );
        }

        Ok(())
    }

    /// Validate UPDATE statement structure
    fn validate_update_statement(&self, update: &UpdateStatement, result: &mut ValidationResult) -> Result<()> {
        // Validate table name
        if update.table.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "UPDATE statement requires a table name".to_string(),
                )
            );
        }

        // Validate assignments
        if update.assignments.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "UPDATE statement requires at least one assignment".to_string(),
                )
            );
        }

        // Check for duplicate column assignments
        let mut assigned_columns = HashSet::new();
        for assignment in &update.assignments {
            if !assigned_columns.insert(&assignment.column) {
                result.add_error(
                    ValidationError::new(
                        ValidationErrorKind::StructuralError,
                        format!("Column '{}' assigned multiple times in UPDATE", assignment.column),
                    )
                );
            }
        }

        // Validate self-assignment patterns
        for assignment in &update.assignments {
            if let Expression::Column(col_ref) = &assignment.value {
                if col_ref.table.is_none() && col_ref.name == assignment.column {
                    result.add_warning(
                        ValidationWarning::new(
                            ValidationWarningKind::UnusualPattern,
                            format!("Self-assignment detected: {} = {}", assignment.column, assignment.column),
                        )
                        .with_suggestion("Self-assignments have no effect".to_string())
                    );
                }
            }
        }

        Ok(())
    }

    /// Validate DELETE statement structure
    fn validate_delete_statement(&self, delete: &DeleteStatement, result: &mut ValidationResult) -> Result<()> {
        // Validate table name
        if delete.table.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "DELETE statement requires a table name".to_string(),
                )
            );
        }

        Ok(())
    }

    /// Validate SELECT projection
    fn validate_select_projection(&self, projection: &[SelectItem], result: &mut ValidationResult) -> Result<()> {
        if projection.is_empty() {
            result.add_error(
                ValidationError::new(
                    ValidationErrorKind::StructuralError,
                    "SELECT statement requires at least one projection item".to_string(),
                )
            );
        }

        // Check for mixed wildcard and explicit columns
        let has_wildcard = projection.iter().any(|item| matches!(item, SelectItem::Wildcard));
        let has_explicit = projection.iter().any(|item| matches!(item, SelectItem::Expression { .. }));

        if has_wildcard && has_explicit {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    "Mixing wildcard (*) with explicit columns - wildcard will include all columns".to_string(),
                )
                .with_suggestion("Use either wildcard or explicit columns, not both".to_string())
            );
        }

        // Check for duplicate aliases
        let mut aliases = HashSet::new();
        for item in projection {
            if let SelectItem::Expression { alias: Some(alias), .. } = item {
                if !aliases.insert(alias) {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::StructuralError,
                            format!("Duplicate alias '{}' in SELECT projection", alias),
                        )
                    );
                }
            }
        }

        Ok(())
    }

    /// Validate FROM clause
    fn validate_from_clause(&self, from: &FromClause, result: &mut ValidationResult) -> Result<()> {
        // Basic FROM clause validation
        match from {
            FromClause::Table { collection, entity_type, alias } => {
                // Check for empty collection name
                if collection.is_empty() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::StructuralError,
                            "Collection name cannot be empty".to_string(),
                        )
                    );
                }

                // Build table reference for alias comparison
                let table_ref = match entity_type {
                    Some(et) => format!("{}.{}", collection, et),
                    None => collection.clone(),
                };

                // Check if alias conflicts with table name
                if let Some(alias_name) = alias {
                    if table_ref == *alias_name {
                        result.add_warning(
                            ValidationWarning::new(
                                ValidationWarningKind::AmbiguousReference,
                                format!("Table alias '{}' is the same as table name", alias_name),
                            )
                            .with_suggestion("Use different alias to avoid confusion".to_string())
                        );
                    }
                }
            }
            FromClause::Subquery { alias, .. } => {
                // Subquery must have an alias
                if alias.is_empty() {
                    result.add_error(
                        ValidationError::new(
                            ValidationErrorKind::StructuralError,
                            "Subquery must have an alias".to_string(),
                        )
                    );
                }
            }
        }

        Ok(())
    }

    /// Validate clause relationships and ordering
    fn validate_clause_relationships(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
        // HAVING without GROUP BY (handled in semantic validator, but double-check here)
        if select.having.is_some() && select.group_by.is_empty() {
            // Check if all SELECT items are aggregates
            let all_aggregates = select.select_list.iter().all(|item| {
                match item {
                    SelectItem::Expression { expr, .. } => self.is_aggregate_expression(expr),
                    SelectItem::Wildcard => false,
                }
            });

            if !all_aggregates {
                result.add_error(
                    ValidationError::new(
                        ValidationErrorKind::StructuralError,
                        "HAVING clause requires GROUP BY clause unless all SELECT items are aggregates".to_string(),
                    )
                );
            }
        }

        // ORDER BY with LIMIT recommendations
        if !select.order_by.is_empty() && select.limit.is_none() {
            if self.config.warn_performance_issues {
                result.add_warning(
                    ValidationWarning::new(
                        ValidationWarningKind::PerformanceWarning,
                        "ORDER BY without LIMIT may be inefficient for large result sets".to_string(),
                    )
                    .with_suggestion("Consider adding LIMIT if you don't need all results".to_string())
                );
            }
        }

        // OFFSET without LIMIT
        if select.offset.is_some() && select.limit.is_none() {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    "OFFSET without LIMIT may have database-specific behavior".to_string(),
                )
                .with_suggestion("Add LIMIT clause for predictable results".to_string())
            );
        }

        Ok(())
    }

    /// Validate DISTINCT usage
    fn validate_distinct_usage(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
        // DISTINCT with ORDER BY - check if ORDER BY columns are in SELECT
        if !select.order_by.is_empty() {
            for order_item in &select.order_by {
                if !self.is_expression_in_projection(&order_item.expr, &select.select_list) {
                    result.add_warning(
                        ValidationWarning::new(
                            ValidationWarningKind::UnusualPattern,
                            "ORDER BY expression not in SELECT list with DISTINCT".to_string(),
                        )
                        .with_suggestion("Add ORDER BY expressions to SELECT list when using DISTINCT".to_string())
                    );
                }
            }
        }

        // DISTINCT with wildcard might be inefficient
        if select.select_list.iter().any(|item| matches!(item, SelectItem::Wildcard)) {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::PerformanceWarning,
                    "DISTINCT with wildcard (*) may be inefficient".to_string(),
                )
                .with_suggestion("Consider selecting only necessary columns with DISTINCT".to_string())
            );
        }

        Ok(())
    }

    /// Validate TRAVERSE clause (graph operations)
    fn validate_traverse_statement(&self, _traverse: &TraverseClause, result: &mut ValidationResult) -> Result<()> {
        // TODO: Implement specific traverse validation based on AST structure
        // For now, just add placeholder validation
        
        result.metadata.phases_completed.push("traverse_statement".to_string());
        
        // Basic validation - ensure we have required components
        // This would be expanded based on the actual TraverseClause structure
        
        Ok(())
    }

    // Helper methods

    /// Check if expression is an aggregate
    fn is_aggregate_expression(&self, expr: &Expression) -> bool {
        match expr {
            Expression::Function { name, .. } => {
                matches!(name.to_uppercase().as_str(),
                    "COUNT" | "SUM" | "AVG" | "MIN" | "MAX" | 
                    "STDDEV" | "VARIANCE" | "GROUP_CONCAT" | "STRING_AGG"
                )
            }
            _ => false,
        }
    }

    /// Check if expression is in projection list
    fn is_expression_in_projection(&self, expr: &Expression, projection: &[SelectItem]) -> bool {
        for item in projection {
            match item {
                SelectItem::Expression { expr: proj_expr, .. } => {
                    if self.expressions_equivalent(expr, proj_expr) {
                        return true;
                    }
                }
                SelectItem::Wildcard => {
                    // Wildcard includes all columns, so ORDER BY columns might be included
                    return true;
                }
            }
        }
        false
    }

    /// Compare expressions for equivalence (simplified)
    fn expressions_equivalent(&self, expr1: &Expression, expr2: &Expression) -> bool {
        match (expr1, expr2) {
            (Expression::Column(col1), Expression::Column(col2)) => {
                col1.name == col2.name && col1.table == col2.table
            }
            (Expression::Literal(lit1), Expression::Literal(lit2)) => lit1 == lit2,
            (Expression::Function { name: n1, args: a1 }, Expression::Function { name: n2, args: a2 }) => {
                n1 == n2 && a1.len() == a2.len() && 
                a1.iter().zip(a2.iter()).all(|(arg1, arg2)| self.expressions_equivalent(arg1, arg2))
            }
            // Add more cases as needed
            _ => false,
        }
    }

    /// Validate reserved words and identifiers
    #[allow(dead_code)]
    fn validate_identifier(&self, name: &str, context: &str, result: &mut ValidationResult) {
        // Check for SQL reserved words
        let reserved_words = [
            "SELECT", "FROM", "WHERE", "GROUP", "ORDER", "BY", "HAVING", "DISTINCT",
            "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER", "TABLE",
            "INDEX", "VIEW", "UNION", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER",
            "ON", "AS", "AND", "OR", "NOT", "NULL", "TRUE", "FALSE", "LIKE",
            "IN", "EXISTS", "BETWEEN", "CASE", "WHEN", "THEN", "ELSE", "END"
        ];

        if reserved_words.contains(&name.to_uppercase().as_str()) {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::SafetyWarning,
                    format!("'{}' is a reserved word used as {}", name, context),
                )
                .with_suggestion("Consider using quoted identifiers or different names".to_string())
            );
        }

        // Check identifier naming conventions
        if name.chars().any(|c| c.is_whitespace()) {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    format!("{} '{}' contains whitespace", context, name),
                )
                .with_suggestion("Consider using underscores instead of spaces".to_string())
            );
        }

        if name.len() > 64 {
            result.add_warning(
                ValidationWarning::new(
                    ValidationWarningKind::UnusualPattern,
                    format!("{} '{}' is very long ({}+ characters)", context, name, name.len()),
                )
                .with_suggestion("Consider shorter, more descriptive names".to_string())
            );
        }
    }
}

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

    #[test]
    fn test_select_projection_validation() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        // Empty projection should error
        let empty_projection: Vec<SelectItem> = vec![];
        validator.validate_select_projection(&empty_projection, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("at least one projection")));
    }

    #[test]
    fn test_mixed_wildcard_projection_warning() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let mixed_projection = vec![
            SelectItem::Wildcard,
            SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "name".to_string(),
                }),
                alias: None,
            },
        ];

        validator.validate_select_projection(&mixed_projection, &mut result).unwrap();

        assert!(!result.warnings.is_empty());
        assert!(result.warnings.iter().any(|w| w.message.contains("Mixing wildcard")));
    }

    #[test]
    fn test_duplicate_alias_error() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let duplicate_alias_projection = vec![
            SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "name".to_string(),
                }),
                alias: Some("alias1".to_string()),
            },
            SelectItem::Expression {
                expr: Expression::Column(ColumnRef {
                    table: None,
                    name: "age".to_string(),
                }),
                alias: Some("alias1".to_string()), // Duplicate alias
            },
        ];

        validator.validate_select_projection(&duplicate_alias_projection, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Duplicate alias 'alias1'")));
    }

    #[test]
    fn test_insert_column_value_mismatch() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let insert = InsertStatement {
            table: "users".to_string(),
            columns: vec!["name".to_string(), "age".to_string()], // 2 columns
            values: vec![vec![Expression::Literal(Literal::String("John".to_string()))]], // 1 value in 1 row
        };

        validator.validate_insert_statement(&insert, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Column count (2) does not match value count (1)")));
    }

    #[test]
    fn test_update_duplicate_assignment() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let update = UpdateStatement {
            table: "users".to_string(),
            assignments: vec![
                Assignment {
                    column: "name".to_string(),
                    value: Expression::Literal(Literal::String("John".to_string())),
                },
                Assignment {
                    column: "name".to_string(), // Duplicate assignment
                    value: Expression::Literal(Literal::String("Jane".to_string())),
                },
            ],
            where_clause: None,
        };

        validator.validate_update_statement(&update, &mut result).unwrap();

        assert!(!result.errors.is_empty());
        assert!(result.errors.iter().any(|e| e.message.contains("Column 'name' assigned multiple times")));
    }

    #[test]
    fn test_self_assignment_warning() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let update = UpdateStatement {
            table: "users".to_string(),
            assignments: vec![
                Assignment {
                    column: "name".to_string(),
                    value: Expression::Column(ColumnRef {
                        table: None,
                        name: "name".to_string(), // Self-assignment
                    }),
                },
            ],
            where_clause: None,
        };

        validator.validate_update_statement(&update, &mut result).unwrap();

        assert!(!result.warnings.is_empty());
        assert!(result.warnings.iter().any(|w| w.message.contains("Self-assignment detected")));
    }

    #[test]
    fn test_distinct_with_wildcard_warning() {
        let config = ValidationConfig::default();
        let validator = StatementValidator::new(&config);
        let mut result = ValidationResult::new();

        let select = SelectStatement {
            select_list: vec![SelectItem::Wildcard],
            from: Some(FromClause::Table {
                collection: "users".to_string(),
                entity_type: None,
                alias: None,
            }),
            joins: Vec::new(),
            where_clause: None,
            group_by: vec![],
            having: None,
            order_by: vec![],
            limit: None,
            offset: None,
            distinct: true, // DISTINCT with wildcard
            traverse_clause: None,
        };

        validator.validate_distinct_usage(&select, &mut result).unwrap();

        assert!(!result.warnings.is_empty());
        assert!(result.warnings.iter().any(|w| w.message.contains("DISTINCT with wildcard")));
    }
}