graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Type inference engine for ISO GQL
//!
//! Implements type inference for expressions and operations using TypeSpec

use crate::types::{GqlType, TypeError, TypeResult};
use std::collections::HashMap;

/// Type inference engine
#[derive(Debug)]
#[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for query optimization and static analysis
pub struct TypeInferenceEngine {
    /// Variable type bindings
    #[allow(dead_code)] // ROADMAP v0.5.0 - Variable type tracking for static analysis
    variable_types: HashMap<String, GqlType>,
    /// Function signatures
    #[allow(dead_code)] // ROADMAP v0.5.0 - Builtin function signatures for type checking
    function_signatures: HashMap<String, FunctionSignature>,
}

/// Function signature for type inference
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.5.0 - Function signature tracking for type inference
pub struct FunctionSignature {
    pub param_types: Vec<GqlType>,
    pub return_type: GqlType,
    pub variadic: bool,
}

impl TypeInferenceEngine {
    pub fn new() -> Self {
        let mut engine = Self {
            variable_types: HashMap::new(),
            function_signatures: HashMap::new(),
        };
        engine.register_builtin_functions();
        engine
    }

    /// Register built-in function signatures
    fn register_builtin_functions(&mut self) {
        // Aggregate functions
        self.register_function(
            "COUNT",
            vec![],
            GqlType::BigInt,
            true, // variadic
        );
        self.register_function("SUM", vec![GqlType::Integer], GqlType::BigInt, false);
        self.register_function("AVG", vec![GqlType::Integer], GqlType::Double, false);
        self.register_function(
            "MIN",
            vec![],           // Will be inferred from arguments
            GqlType::Integer, // Placeholder
            true,             // variadic
        );
        self.register_function(
            "MAX",
            vec![],           // Will be inferred from arguments
            GqlType::Integer, // Placeholder
            true,             // variadic
        );

        // String functions
        self.register_function(
            "UPPER",
            vec![GqlType::String { max_length: None }],
            GqlType::String { max_length: None },
            false,
        );
        self.register_function(
            "LOWER",
            vec![GqlType::String { max_length: None }],
            GqlType::String { max_length: None },
            false,
        );
        self.register_function(
            "LENGTH",
            vec![GqlType::String { max_length: None }],
            GqlType::Integer,
            false,
        );
        self.register_function(
            "SUBSTRING",
            vec![
                GqlType::String { max_length: None },
                GqlType::Integer,
                GqlType::Integer,
            ],
            GqlType::String { max_length: None },
            false,
        );

        // Temporal functions
        self.register_function("CURRENT_DATE", vec![], GqlType::Date, false);
        self.register_function(
            "CURRENT_TIME",
            vec![],
            GqlType::Time {
                precision: None,
                with_timezone: true,
            },
            false,
        );
        self.register_function(
            "CURRENT_TIMESTAMP",
            vec![],
            GqlType::Timestamp {
                precision: None,
                with_timezone: true,
            },
            false,
        );

        // Type check functions
        self.register_function(
            "IS_NULL",
            vec![],
            GqlType::Boolean,
            true, // variadic - accepts any type
        );
        self.register_function(
            "IS_BOOLEAN",
            vec![],
            GqlType::Boolean,
            true, // variadic - accepts any type
        );
        self.register_function(
            "IS_STRING",
            vec![],
            GqlType::Boolean,
            true, // variadic - accepts any type
        );
        self.register_function(
            "IS_NUMERIC",
            vec![],
            GqlType::Boolean,
            true, // variadic - accepts any type
        );
    }

    /// Register a function signature
    pub fn register_function(
        &mut self,
        name: &str,
        param_types: Vec<GqlType>,
        return_type: GqlType,
        variadic: bool,
    ) {
        self.function_signatures.insert(
            name.to_uppercase(),
            FunctionSignature {
                param_types,
                return_type,
                variadic,
            },
        );
    }

    /// Bind a variable to a type
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn bind_variable(&mut self, name: String, var_type: GqlType) {
        self.variable_types.insert(name, var_type);
    }

    /// Get the type of a variable
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn get_variable_type(&self, name: &str) -> Option<&GqlType> {
        self.variable_types.get(name)
    }

    /// Infer type from a literal value
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn infer_literal_type(&self, literal: &str) -> GqlType {
        // Boolean literals
        if literal.eq_ignore_ascii_case("true") || literal.eq_ignore_ascii_case("false") {
            return GqlType::Boolean;
        }

        // String literal (quoted)
        if (literal.starts_with('\'') && literal.ends_with('\''))
            || (literal.starts_with('"') && literal.ends_with('"'))
        {
            return GqlType::String { max_length: None };
        }

        // Numeric literals
        if let Ok(_) = literal.parse::<i64>() {
            return GqlType::Integer;
        }
        if let Ok(_) = literal.parse::<f64>() {
            return GqlType::Double;
        }

        // Date/Time literals (prefixed)
        if literal.starts_with("DATE ") {
            return GqlType::Date;
        }
        if literal.starts_with("TIME ") {
            return GqlType::Time {
                precision: None,
                with_timezone: false,
            };
        }
        if literal.starts_with("TIMESTAMP ") {
            return GqlType::Timestamp {
                precision: None,
                with_timezone: false,
            };
        }
        if literal.starts_with("DURATION ") {
            return GqlType::Duration { precision: None };
        }

        // Default to integer for now (should be unknown type)
        GqlType::Integer
    }

    /// Infer type of a binary operation
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn infer_binary_op_type(
        &self,
        op: &str,
        left_type: &GqlType,
        right_type: &GqlType,
    ) -> TypeResult<GqlType> {
        match op {
            // Arithmetic operators
            "+" | "-" | "*" | "/" | "%" => self.infer_arithmetic_op_type(op, left_type, right_type),

            // Comparison operators
            "=" | "<>" | "!=" | "<" | ">" | "<=" | ">=" => {
                self.infer_comparison_op_type(left_type, right_type)
            }

            // Logical operators
            "AND" | "OR" | "XOR" => self.infer_logical_op_type(left_type, right_type),

            // String concatenation
            "||" => self.infer_string_concat_type(left_type, right_type),

            // IN operator
            "IN" => self.infer_in_op_type(left_type, right_type),

            _ => Err(TypeError::UnknownType(format!("Unknown operator: {}", op))),
        }
    }

    /// Infer type of arithmetic operation
    fn infer_arithmetic_op_type(
        &self,
        op: &str,
        left_type: &GqlType,
        right_type: &GqlType,
    ) -> TypeResult<GqlType> {
        match (left_type, right_type) {
            // Numeric arithmetic
            (left, right) if left.is_numeric() && right.is_numeric() => {
                Ok(self.promote_numeric_types(left, right))
            }

            // Duration arithmetic
            (GqlType::Duration { .. }, GqlType::Duration { .. }) if op == "+" || op == "-" => {
                Ok(GqlType::Duration { precision: None })
            }

            // Temporal + Duration
            (left, GqlType::Duration { .. }) if left.is_temporal() && (op == "+" || op == "-") => {
                Ok(left.clone()) // Result is same temporal type
            }
            (GqlType::Duration { .. }, right) if right.is_temporal() && op == "+" => {
                Ok(right.clone()) // Result is same temporal type
            }

            // Temporal - Temporal = Duration
            (left, right) if left.is_temporal() && right.is_temporal() && op == "-" => {
                Ok(GqlType::Duration { precision: None })
            }

            _ => Err(TypeError::IncompatibleTypes(
                format!("{}", left_type),
                format!("{}", right_type),
            )),
        }
    }

    /// Infer type of comparison operation
    fn infer_comparison_op_type(
        &self,
        left_type: &GqlType,
        right_type: &GqlType,
    ) -> TypeResult<GqlType> {
        // Comparisons always return boolean
        if self.are_comparable(left_type, right_type) {
            Ok(GqlType::Boolean)
        } else {
            Err(TypeError::IncompatibleTypes(
                format!("{}", left_type),
                format!("{}", right_type),
            ))
        }
    }

    /// Infer type of logical operation
    fn infer_logical_op_type(
        &self,
        left_type: &GqlType,
        right_type: &GqlType,
    ) -> TypeResult<GqlType> {
        match (left_type, right_type) {
            (GqlType::Boolean, GqlType::Boolean) => Ok(GqlType::Boolean),
            _ => Err(TypeError::TypeMismatch {
                expected: "BOOLEAN".to_string(),
                actual: format!("{} and {}", left_type, right_type),
            }),
        }
    }

    /// Infer type of string concatenation
    fn infer_string_concat_type(
        &self,
        left_type: &GqlType,
        right_type: &GqlType,
    ) -> TypeResult<GqlType> {
        match (left_type, right_type) {
            (GqlType::String { .. }, GqlType::String { .. }) => {
                Ok(GqlType::String { max_length: None })
            }
            _ => Err(TypeError::TypeMismatch {
                expected: "STRING".to_string(),
                actual: format!("{} and {}", left_type, right_type),
            }),
        }
    }

    /// Infer type of IN operation
    fn infer_in_op_type(&self, left_type: &GqlType, right_type: &GqlType) -> TypeResult<GqlType> {
        match right_type {
            GqlType::List { element_type, .. } => {
                if self.are_comparable(left_type, element_type) {
                    Ok(GqlType::Boolean)
                } else {
                    Err(TypeError::CollectionTypeMismatch(format!(
                        "Cannot check if {} is in list of {}",
                        left_type, element_type
                    )))
                }
            }
            _ => Err(TypeError::TypeMismatch {
                expected: "LIST".to_string(),
                actual: format!("{}", right_type),
            }),
        }
    }

    /// Infer type of function call
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn infer_function_type(
        &self,
        function_name: &str,
        arg_types: &[GqlType],
    ) -> TypeResult<GqlType> {
        let name = function_name.to_uppercase();

        if let Some(signature) = self.function_signatures.get(&name) {
            // Check argument count
            if !signature.variadic && arg_types.len() != signature.param_types.len() {
                return Err(TypeError::InvalidTypeSpecification(format!(
                    "Function {} expects {} arguments, got {}",
                    function_name,
                    signature.param_types.len(),
                    arg_types.len()
                )));
            }

            // For aggregate functions with polymorphic return type, infer from input
            if signature.return_type == GqlType::Double {
                // Use Double as polymorphic marker
                if name == "MIN" || name == "MAX" {
                    if !arg_types.is_empty() {
                        return Ok(arg_types[0].clone());
                    }
                }
            }

            Ok(signature.return_type.clone())
        } else {
            // Unknown function - return String as default
            Ok(GqlType::String { max_length: None })
        }
    }

    /// Check if two types are comparable
    fn are_comparable(&self, left: &GqlType, right: &GqlType) -> bool {
        match (left, right) {
            // Same type is always comparable
            _ if left == right => true,

            // Remove null comparison as TypeSpec doesn't have Null variant

            // String is comparable with most types
            (GqlType::String { .. }, _) | (_, GqlType::String { .. }) => true,

            // Numeric types are comparable
            (left, right) if left.is_numeric() && right.is_numeric() => true,

            // Temporal types are comparable if they're both temporal
            (left, right) if left.is_temporal() && right.is_temporal() => {
                left.has_date_component() == right.has_date_component()
                    && left.has_time_component() == right.has_time_component()
            }

            _ => false,
        }
    }

    /// Promote numeric types to common type using TypeSpec
    fn promote_numeric_types(&self, left: &GqlType, right: &GqlType) -> GqlType {
        match (left, right) {
            // Same type
            _ if left == right => left.clone(),

            // Promote to wider integer type
            (GqlType::SmallInt, GqlType::Integer) | (GqlType::Integer, GqlType::SmallInt) => {
                GqlType::Integer
            }
            (GqlType::SmallInt, GqlType::BigInt) | (GqlType::BigInt, GqlType::SmallInt) => {
                GqlType::BigInt
            }
            (GqlType::Integer, GqlType::BigInt) | (GqlType::BigInt, GqlType::Integer) => {
                GqlType::BigInt
            }

            // Promote to Int128/Int256
            (_, GqlType::Int256) | (GqlType::Int256, _) => GqlType::Int256,
            (_, GqlType::Int128) | (GqlType::Int128, _) => GqlType::Int128,

            // Promote to floating point
            (_, GqlType::Double) | (GqlType::Double, _) => GqlType::Double,
            (_, GqlType::Float { .. }) | (GqlType::Float { .. }, _) => GqlType::Double,
            (_, GqlType::Real) | (GqlType::Real, _) => GqlType::Real,

            // Promote to decimal
            (
                GqlType::Decimal {
                    precision: p1,
                    scale: s1,
                },
                GqlType::Decimal {
                    precision: p2,
                    scale: s2,
                },
            ) => GqlType::Decimal {
                precision: match (p1, p2) {
                    (Some(x), Some(y)) => Some((*x).max(*y)),
                    _ => None,
                },
                scale: match (s1, s2) {
                    (Some(x), Some(y)) => Some((*x).max(*y)),
                    _ => None,
                },
            },

            // Default to double for mixed types
            _ => GqlType::Double,
        }
    }

    /// Clear all type bindings
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn clear_bindings(&mut self) {
        self.variable_types.clear();
    }

    /// Create a new scope with current bindings
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn push_scope(&self) -> HashMap<String, GqlType> {
        self.variable_types.clone()
    }

    /// Restore previous scope
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn pop_scope(&mut self, scope: HashMap<String, GqlType>) {
        self.variable_types = scope;
    }

    /// Infer type for unary operations
    #[allow(dead_code)] // ROADMAP v0.5.0 - Type inference for static analysis (see ROADMAP.md ยง7)
    pub fn infer_unary_operation_type(
        &self,
        operand_type: &GqlType,
        operator: &crate::ast::ast::Operator,
    ) -> TypeResult<GqlType> {
        use crate::ast::ast::Operator;

        match operator {
            Operator::Not => {
                // NOT requires boolean operand and returns boolean
                if matches!(operand_type, GqlType::Boolean) {
                    Ok(GqlType::Boolean)
                } else {
                    Err(TypeError::TypeMismatch {
                        expected: "BOOLEAN".to_string(),
                        actual: format!("{}", operand_type),
                    })
                }
            }
            Operator::Minus => {
                // Unary minus requires numeric operand and preserves type
                if operand_type.is_numeric() {
                    Ok(operand_type.clone())
                } else {
                    Err(TypeError::TypeMismatch {
                        expected: "NUMERIC".to_string(),
                        actual: format!("{}", operand_type),
                    })
                }
            }
            Operator::Plus => {
                // Unary plus requires numeric operand and preserves type
                if operand_type.is_numeric() {
                    Ok(operand_type.clone())
                } else {
                    Err(TypeError::TypeMismatch {
                        expected: "NUMERIC".to_string(),
                        actual: format!("{}", operand_type),
                    })
                }
            }
            _ => Err(TypeError::InvalidTypeSpecification(format!(
                "Operator {:?} is not valid for unary operations",
                operator
            ))),
        }
    }
}

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

    #[test]
    fn test_literal_inference() {
        let engine = TypeInferenceEngine::new();

        assert_eq!(engine.infer_literal_type("true"), GqlType::Boolean);
        assert_eq!(engine.infer_literal_type("FALSE"), GqlType::Boolean);
        // Null doesn't have its own type in TypeSpec, defaults to Integer
        assert_eq!(engine.infer_literal_type("null"), GqlType::Integer);
        assert_eq!(
            engine.infer_literal_type("'hello'"),
            GqlType::String { max_length: None }
        );
        assert_eq!(engine.infer_literal_type("42"), GqlType::Integer);
        assert_eq!(engine.infer_literal_type("3.14"), GqlType::Double);
        assert_eq!(
            engine.infer_literal_type("DATE '2024-01-01'"),
            GqlType::Date
        );
    }

    #[test]
    fn test_arithmetic_inference() {
        let engine = TypeInferenceEngine::new();

        let int_type = GqlType::Integer;
        let float_type = GqlType::Double;

        let result = engine
            .infer_binary_op_type("+", &int_type, &int_type)
            .unwrap();
        assert_eq!(result, GqlType::Integer);

        let result = engine
            .infer_binary_op_type("+", &int_type, &float_type)
            .unwrap();
        assert_eq!(result, GqlType::Double);
    }

    #[test]
    fn test_comparison_inference() {
        let engine = TypeInferenceEngine::new();

        let int_type = GqlType::Integer;
        let string_type = GqlType::String { max_length: None };

        let result = engine
            .infer_binary_op_type("=", &int_type, &int_type)
            .unwrap();
        assert_eq!(result, GqlType::Boolean);

        let result = engine
            .infer_binary_op_type("<", &string_type, &string_type)
            .unwrap();
        assert_eq!(result, GqlType::Boolean);
    }

    #[test]
    fn test_function_inference() {
        let engine = TypeInferenceEngine::new();

        let result = engine.infer_function_type("COUNT", &[]).unwrap();
        assert_eq!(result, GqlType::BigInt);

        let result = engine
            .infer_function_type("UPPER", &[GqlType::String { max_length: None }])
            .unwrap();
        assert_eq!(result, GqlType::String { max_length: None });

        let result = engine.infer_function_type("CURRENT_DATE", &[]).unwrap();
        assert_eq!(result, GqlType::Date);
    }
}