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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! ISO GQL Special Functions Implementation
//!
//! This module implements the three special predicate functions defined in the ISO GQL standard:
//! - ALL_DIFFERENT: Ensures all provided expressions evaluate to different values
//! - SAME: Checks if two expressions evaluate to the same value  
//! - PROPERTY_EXISTS: Checks if a property exists on a node/edge
//!
//! These functions are part of the official ISO GQL BNF grammar under <predicate> production rule.

use super::function_trait::{Function, FunctionContext, FunctionError, FunctionResult};
use crate::storage::Value;
use log::{debug, warn};
use std::collections::HashSet;

/// ALL_DIFFERENT function: Ensures all provided expressions evaluate to different values
///
/// According to ISO GQL spec: ALL_DIFFERENT(expr1, expr2, ..., exprN)
/// Returns true if all expressions evaluate to distinct values, false otherwise.
///
/// # Examples
/// ```gql
/// ALL_DIFFERENT(person.id, company.id, location.id)  -- Returns true if all IDs are different
/// ALL_DIFFERENT(1, 2, 1)  -- Returns false (duplicate value)
/// ```
#[derive(Debug)]
pub struct AllDifferentFunction;

impl AllDifferentFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for AllDifferentFunction {
    fn name(&self) -> &str {
        "ALL_DIFFERENT"
    }

    fn description(&self) -> &str {
        "Returns true if all provided expressions evaluate to different values"
    }

    fn argument_count(&self) -> usize {
        // Variable argument count - minimum 1
        1 // This indicates minimum, actual implementation handles variadic
    }

    fn return_type(&self) -> &str {
        "Boolean"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        debug!(
            "Executing ALL_DIFFERENT function with {} arguments",
            context.arguments.len()
        );

        // Validate minimum argument count
        if context.arguments.is_empty() {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 1,
                actual: 0,
            });
        }

        // Use HashSet to track seen values for O(n) performance
        let mut seen_values = HashSet::new();

        for (i, arg) in context.arguments.iter().enumerate() {
            debug!("Checking argument {}: {:?}", i, arg);

            // Convert Value to a comparable representation
            let comparable_value = value_to_comparable(arg)?;

            if seen_values.contains(&comparable_value) {
                debug!("Found duplicate value: {:?}", comparable_value);
                return Ok(Value::Boolean(false));
            }

            seen_values.insert(comparable_value);
        }

        debug!("All {} values are different", context.arguments.len());
        Ok(Value::Boolean(true))
    }

    fn graph_context_required(&self) -> bool {
        false // Pure function that doesn't need graph context
    }

    fn is_variadic(&self) -> bool {
        true // Accepts variable number of arguments
    }
}

/// SAME function: Checks if two expressions evaluate to the same value
///
/// According to ISO GQL spec: SAME(expr1, expr2)
/// Returns true if both expressions evaluate to the same value, false otherwise.
///
/// # Examples
/// ```gql
/// SAME(person.birth_year, person.graduation_year - 4)  -- Check if graduation was 4 years after birth
/// SAME("hello", "hello")  -- Returns true
/// SAME(42, 43)  -- Returns false
/// ```
#[derive(Debug)]
pub struct SameFunction;

impl SameFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for SameFunction {
    fn name(&self) -> &str {
        "SAME"
    }

    fn description(&self) -> &str {
        "Returns true if two expressions evaluate to the same value"
    }

    fn argument_count(&self) -> usize {
        2
    }

    fn return_type(&self) -> &str {
        "Boolean"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        debug!("Executing SAME function");

        // Validate exact argument count
        if context.arguments.len() != 2 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 2,
                actual: context.arguments.len(),
            });
        }

        let value1 = &context.arguments[0];
        let value2 = &context.arguments[1];

        debug!("Comparing values: {:?} vs {:?}", value1, value2);

        // Convert both values to comparable representations
        let comparable1 = value_to_comparable(value1)?;
        let comparable2 = value_to_comparable(value2)?;

        let are_same = comparable1 == comparable2;
        debug!("Values are same: {}", are_same);

        Ok(Value::Boolean(are_same))
    }

    fn graph_context_required(&self) -> bool {
        false // Pure function that doesn't need graph context
    }
}

/// PROPERTY_EXISTS function: Checks if a property exists on a node/edge
///
/// According to ISO GQL spec: PROPERTY_EXISTS(property_reference)
/// Returns true if the specified property exists, false otherwise.
///
/// # Examples
/// ```gql
/// PROPERTY_EXISTS(person.email)  -- Returns true if person has email property
/// PROPERTY_EXISTS(node.nonexistent)  -- Returns false
/// ```
#[derive(Debug)]
pub struct PropertyExistsFunction;

impl PropertyExistsFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for PropertyExistsFunction {
    fn name(&self) -> &str {
        "PROPERTY_EXISTS"
    }

    fn description(&self) -> &str {
        "Returns true if the specified property exists on the node/edge"
    }

    fn argument_count(&self) -> usize {
        1
    }

    fn return_type(&self) -> &str {
        "Boolean"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        debug!("Executing PROPERTY_EXISTS function");

        // Validate argument count
        if context.arguments.len() != 1 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 1,
                actual: context.arguments.len(),
            });
        }

        let property_ref = &context.arguments[0];
        debug!("Checking property existence for: {:?}", property_ref);

        // For now, we'll implement a simplified version that works with string property paths
        // In a full implementation, this would need proper property reference parsing
        match property_ref {
            Value::String(property_path) => {
                // Parse property path (e.g., "person.email" -> check if person has email)
                let exists = check_property_exists_from_path(property_path, context)?;
                debug!("Property {} exists: {}", property_path, exists);
                Ok(Value::Boolean(exists))
            }
            _ => {
                // PROPERTY_EXISTS requires a property reference (string for now)
                return Err(FunctionError::InvalidArgumentType {
                    message: "PROPERTY_EXISTS argument must be a property reference (string)"
                        .to_string(),
                });
            }
        }
    }

    fn graph_context_required(&self) -> bool {
        true // Needs access to graph context to check property existence
    }
}

/// Convert a Value to a comparable representation for equality/uniqueness checks
/// This handles the complexity of comparing different Value types consistently
fn value_to_comparable(value: &Value) -> FunctionResult<ComparableValue> {
    match value {
        Value::Boolean(b) => Ok(ComparableValue::Boolean(*b)),
        Value::Number(n) => Ok(ComparableValue::Number(n.to_bits())), // Use bit representation for exact floating point comparison
        Value::String(s) => Ok(ComparableValue::String(s.clone())),
        Value::DateTime(dt) => Ok(ComparableValue::DateTime(dt.timestamp())),
        Value::DateTimeWithFixedOffset(dt) => Ok(ComparableValue::DateTime(dt.timestamp())),
        Value::DateTimeWithNamedTz(_, dt) => Ok(ComparableValue::DateTime(dt.timestamp())),
        Value::TimeWindow(tw) => Ok(ComparableValue::String(format!("{:?}", tw))), // Use debug format for comparison
        // Add more value types as needed
        _ => {
            warn!("Unsupported value type for comparison: {:?}", value);
            Err(FunctionError::InvalidArgumentType {
                message: format!("Value type {:?} is not supported for comparison", value),
            })
        }
    }
}

/// Comparable representation of values for equality and uniqueness checks
/// This ensures consistent comparison across different value types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ComparableValue {
    Boolean(bool),
    Number(u64), // Using bit representation of f64
    String(String),
    DateTime(i64), // Unix timestamp
}

/// Check if a property exists based on a property path string
/// This is a simplified implementation - a full implementation would integrate with
/// the query execution context and graph traversal
fn check_property_exists_from_path(
    property_path: &str,
    context: &FunctionContext,
) -> FunctionResult<bool> {
    debug!("Checking property path: {}", property_path);

    // Parse property path (e.g., "person.email" -> ["person", "email"])
    let parts: Vec<&str> = property_path.split('.').collect();
    if parts.len() != 2 {
        warn!("Invalid property path format: {}", property_path);
        return Ok(false);
    }

    let _entity = parts[0];
    let property = parts[1];

    // Simplified check - in real implementation, this would:
    // 1. Look up the entity in the current query context
    // 2. Check if that entity has the specified property
    // 3. Return true/false based on actual graph data

    // For now, simulate property existence based on common patterns
    let common_properties = [
        "id",
        "name",
        "email",
        "age",
        "created_at",
        "updated_at",
        "birth_year",
        "graduation_year",
        "city",
        "country",
        "founded",
    ];

    let exists = common_properties.contains(&property);
    debug!("Property '{}' exists (simulated): {}", property, exists);

    // Check if we have any variables in context that might contain this property
    if !exists {
        for (var_name, var_value) in &context.variables {
            debug!("Checking variable {}: {:?}", var_name, var_value);
            // In a real implementation, we'd check if var_value has the property
        }
    }

    Ok(exists)
}

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

    #[test]
    fn test_all_different_basic() {
        let func = AllDifferentFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![
                Value::Number(1.0),
                Value::Number(2.0),
                Value::String("three".to_string()),
            ],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(true));
    }

    #[test]
    fn test_all_different_with_duplicates() {
        let func = AllDifferentFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![
                Value::Number(1.0),
                Value::Number(2.0),
                Value::Number(1.0), // Duplicate
            ],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(false));
    }

    #[test]
    fn test_same_identical_values() {
        let func = SameFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::Number(42.0), Value::Number(42.0)],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(true));
    }

    #[test]
    fn test_same_different_values() {
        let func = SameFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::Number(42.0), Value::Number(43.0)],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(false));
    }

    #[test]
    fn test_same_different_types() {
        let func = SameFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::Number(42.0), Value::String("42".to_string())],
        );

        let result = func.execute(&context).unwrap();
        // Different types should be different even with same logical value
        assert_eq!(result, Value::Boolean(false));
    }

    #[test]
    fn test_property_exists_basic() {
        let func = PropertyExistsFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::String("person.email".to_string())],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(true)); // "email" is in common properties
    }

    #[test]
    fn test_property_exists_missing() {
        let func = PropertyExistsFunction::new();
        let context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::String("person.nonexistent".to_string())],
        );

        let result = func.execute(&context).unwrap();
        assert_eq!(result, Value::Boolean(false));
    }

    #[test]
    fn test_argument_validation() {
        // Test ALL_DIFFERENT with empty args
        let all_diff = AllDifferentFunction::new();
        let empty_context = FunctionContext::new(vec![], HashMap::new(), vec![]);
        assert!(all_diff.execute(&empty_context).is_err());

        // Test SAME with wrong arg count
        let same = SameFunction::new();
        let wrong_count_context = FunctionContext::new(
            vec![],
            HashMap::new(),
            vec![Value::Number(1.0)], // Only one arg
        );
        assert!(same.execute(&wrong_count_context).is_err());

        // Test PROPERTY_EXISTS with wrong arg count
        let prop_exists = PropertyExistsFunction::new();
        assert!(prop_exists.execute(&empty_context).is_err());
    }

    #[test]
    fn test_comparable_value_conversion() {
        // Test various value types
        assert!(value_to_comparable(&Value::Boolean(true)).is_ok());
        assert!(value_to_comparable(&Value::Number(42.0)).is_ok());
        assert!(value_to_comparable(&Value::String("test".to_string())).is_ok());

        // Test that same values produce same comparable values
        let val1 = value_to_comparable(&Value::Number(42.0)).unwrap();
        let val2 = value_to_comparable(&Value::Number(42.0)).unwrap();
        assert_eq!(val1, val2);

        // Test that different values produce different comparable values
        let val3 = value_to_comparable(&Value::Number(43.0)).unwrap();
        assert_ne!(val1, val3);
    }

    #[test]
    fn test_performance_with_large_dataset() {
        let func = AllDifferentFunction::new();

        // Create 1000 unique values
        let large_args: Vec<Value> = (0..1000).map(|i| Value::Number(i as f64)).collect();

        let context = FunctionContext::new(vec![], HashMap::new(), large_args);

        let start = std::time::Instant::now();
        let result = func.execute(&context).unwrap();
        let duration = start.elapsed();

        assert_eq!(result, Value::Boolean(true));
        assert!(
            duration.as_millis() < 100,
            "Should complete within 100ms for 1000 values"
        );
    }
}