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
653
654
655
656
657
658
659
660
661
662
663
664
//! Vector query syntax parser
//!
//! This module implements parsing for vector query expressions including:
//! - SIMILARITY() function for vector similarity calculations
//! - DISTANCE() function for vector distance calculations
//! - SIMILAR TO operator with threshold constraints
//! - ORDER BY SIMILARITY/DISTANCE for k-NN queries
//!
//! These expressions enable vector similarity queries over named embeddings.

use crate::ast::*;
use crate::ast::vector::similarity::{SimilarityMetric, VectorType};
use crate::error::*;
use super::expression::parse_simple_column_or_literal;

/// Parse SIMILARITY function call
///
/// Syntax: `SIMILARITY(vector_name, reference_expr, 'metric')`
///
/// Returns a Vector expression with Similarity variant
///
/// # Example
/// ```text
/// SIMILARITY(text_embedding, query_vector, 'cosine')
/// => Expression::Vector(VectorExpression::Similarity { ... })
/// ```
pub fn parse_similarity_function(input: &str) -> Result<Expression> {
    let input = input.trim();

    // Verify this is a SIMILARITY function call
    let upper = input.to_uppercase();
    if !upper.starts_with("SIMILARITY(") {
        return Err(HyperQLError::simple_parse_error(
            "Expected SIMILARITY function call",
            input,
            1,
            1,
        ));
    }

    // Find matching closing parenthesis
    let start_paren = input.find('(')
        .ok_or_else(|| HyperQLError::simple_parse_error(
            "Missing opening parenthesis in SIMILARITY function",
            input,
            1,
            1,
        ))?;

    let end_paren = find_matching_paren(input, start_paren)?;

    if end_paren != input.len() - 1 {
        return Err(HyperQLError::simple_parse_error(
            "Unexpected characters after SIMILARITY function",
            input,
            1,
            1,
        ));
    }

    // Parse arguments: vector_name, reference, metric
    let args_str = &input[start_paren + 1..end_paren];
    let args = split_function_args(args_str)?;

    if args.len() != 3 {
        return Err(HyperQLError::simple_parse_error(
            "SIMILARITY function requires exactly 3 arguments: (vector_name, reference, metric)",
            input,
            1,
            1,
        ));
    }

    // Parse vector name (first argument - should be identifier)
    let vector_name = args[0].trim().to_string();
    if vector_name.is_empty() {
        return Err(HyperQLError::simple_parse_error(
            "Vector name cannot be empty",
            input,
            1,
            1,
        ));
    }

    // Parse reference expression (second argument)
    let reference = parse_simple_column_or_literal(args[1].trim())?;

    // Parse metric (third argument - should be string literal)
    let metric = parse_metric_string(args[2].trim())?;

    // Default vector type (dimensions will be validated at runtime)
    let vector_type = VectorType::Dense { dimensions: 0 };

    Ok(Expression::Vector(VectorExpression::Similarity {
        vector_name,
        reference: Box::new(reference),
        metric,
        threshold: None,
        vector_type,
    }))
}

/// Parse DISTANCE function call
///
/// Syntax: `DISTANCE(vector_name, reference_expr, 'metric')`
///
/// Returns a Vector expression with Similarity variant (distance is inverted similarity)
///
/// # Example
/// ```text
/// DISTANCE(embedding, target, 'euclidean')
/// => Expression::Vector(VectorExpression::Similarity { ... })
/// ```
pub fn parse_distance_function(input: &str) -> Result<Expression> {
    let input = input.trim();

    // Verify this is a DISTANCE function call
    let upper = input.to_uppercase();
    if !upper.starts_with("DISTANCE(") {
        return Err(HyperQLError::simple_parse_error(
            "Expected DISTANCE function call",
            input,
            1,
            1,
        ));
    }

    // Find matching closing parenthesis
    let start_paren = input.find('(')
        .ok_or_else(|| HyperQLError::simple_parse_error(
            "Missing opening parenthesis in DISTANCE function",
            input,
            1,
            1,
        ))?;

    let end_paren = find_matching_paren(input, start_paren)?;

    if end_paren != input.len() - 1 {
        return Err(HyperQLError::simple_parse_error(
            "Unexpected characters after DISTANCE function",
            input,
            1,
            1,
        ));
    }

    // Parse arguments: vector_name, reference, metric
    let args_str = &input[start_paren + 1..end_paren];
    let args = split_function_args(args_str)?;

    if args.len() != 3 {
        return Err(HyperQLError::simple_parse_error(
            "DISTANCE function requires exactly 3 arguments: (vector_name, reference, metric)",
            input,
            1,
            1,
        ));
    }

    // Parse vector name (first argument)
    let vector_name = args[0].trim().to_string();
    if vector_name.is_empty() {
        return Err(HyperQLError::simple_parse_error(
            "Vector name cannot be empty",
            input,
            1,
            1,
        ));
    }

    // Parse reference expression (second argument)
    let reference = parse_simple_column_or_literal(args[1].trim())?;

    // Parse metric (third argument)
    let metric = parse_metric_string(args[2].trim())?;

    // Default vector type
    let vector_type = VectorType::Dense { dimensions: 0 };

    Ok(Expression::Vector(VectorExpression::Similarity {
        vector_name,
        reference: Box::new(reference),
        metric,
        threshold: None,
        vector_type,
    }))
}

/// Parse SIMILAR TO operator expression
///
/// Syntax: `vector_name SIMILAR TO reference_expr THRESHOLD threshold_value`
///
/// Returns a Vector expression with Similarity variant and threshold
///
/// # Example
/// ```text
/// text_embedding SIMILAR TO query_vec THRESHOLD 0.8
/// => Expression::Vector(VectorExpression::Similarity { threshold: Some(0.8), ... })
/// ```
pub fn parse_similar_to_expression(input: &str) -> Result<Expression> {
    let input = input.trim();

    // Look for "SIMILAR TO" keywords
    let upper = input.to_uppercase();
    let similar_to_pos = upper.find(" SIMILAR TO ")
        .ok_or_else(|| HyperQLError::simple_parse_error(
            "SIMILAR TO keywords not found",
            input,
            1,
            1,
        ))?;

    // Parse vector name before SIMILAR TO
    let vector_name = input[..similar_to_pos].trim().to_string();
    if vector_name.is_empty() {
        return Err(HyperQLError::simple_parse_error(
            "Vector name cannot be empty",
            input,
            1,
            1,
        ));
    }

    // Find THRESHOLD keyword
    let rest = &input[similar_to_pos + 12..]; // " SIMILAR TO " is 12 chars
    let upper_rest = rest.to_uppercase();
    let threshold_pos = upper_rest.find(" THRESHOLD ")
        .ok_or_else(|| HyperQLError::simple_parse_error(
            "THRESHOLD keyword required after SIMILAR TO reference",
            input,
            1,
            1,
        ))?;

    // Parse reference expression between SIMILAR TO and THRESHOLD
    let reference_part = rest[..threshold_pos].trim();
    let reference = parse_simple_column_or_literal(reference_part)?;

    // Parse threshold value after THRESHOLD
    let threshold_part = rest[threshold_pos + 11..].trim(); // " THRESHOLD " is 11 chars
    let threshold_expr = parse_simple_column_or_literal(threshold_part)?;

    // Extract threshold as float
    let threshold = match threshold_expr {
        Expression::Literal(Literal::Float(f)) => f,
        Expression::Literal(Literal::Int(i)) => i as f64,
        _ => {
            return Err(HyperQLError::simple_parse_error(
                "THRESHOLD value must be a numeric literal",
                input,
                1,
                1,
            ));
        }
    };

    // Validate threshold range
    if threshold < -1.0 || threshold > 1.0 {
        return Err(HyperQLError::simple_parse_error(
            "THRESHOLD value must be between -1.0 and 1.0",
            input,
            1,
            1,
        ));
    }

    // Default to cosine similarity for SIMILAR TO operator
    let metric = SimilarityMetric::Cosine;
    let vector_type = VectorType::Dense { dimensions: 0 };

    Ok(Expression::Vector(VectorExpression::Similarity {
        vector_name,
        reference: Box::new(reference),
        metric,
        threshold: Some(threshold),
        vector_type,
    }))
}

/// Parse metric string literal to SimilarityMetric enum
///
/// Supported metrics:
/// - 'cosine' -> Cosine
/// - 'dotproduct', 'dot' -> DotProduct
/// - 'euclidean' -> Euclidean
/// - 'manhattan' -> Manhattan
/// - 'jaccard' -> Jaccard
/// - Other strings -> Custom(String)
fn parse_metric_string(input: &str) -> Result<SimilarityMetric> {
    let input = input.trim();

    // Check for string literal (single or double quotes)
    if !(input.starts_with('\'') && input.ends_with('\'')) &&
       !(input.starts_with('"') && input.ends_with('"')) {
        return Err(HyperQLError::simple_parse_error(
            "Metric must be a string literal (e.g., 'cosine', \"euclidean\")",
            input,
            1,
            1,
        ));
    }

    // Extract string content
    let metric_str = &input[1..input.len() - 1];
    let metric_lower = metric_str.to_lowercase();

    Ok(match metric_lower.as_str() {
        "cosine" => SimilarityMetric::Cosine,
        "dotproduct" | "dot" => SimilarityMetric::DotProduct,
        "euclidean" => SimilarityMetric::Euclidean,
        "manhattan" => SimilarityMetric::Manhattan,
        "jaccard" => SimilarityMetric::Jaccard,
        _ => SimilarityMetric::Custom(metric_str.to_string()),
    })
}

/// Split function arguments handling nested parentheses and quotes
fn split_function_args(input: &str) -> Result<Vec<&str>> {
    let mut args = Vec::new();
    let mut current_start = 0;
    let mut paren_depth = 0;
    let mut in_quote = false;
    let mut quote_char = '"';
    let chars: Vec<char> = input.chars().collect();

    for i in 0..chars.len() {
        let ch = chars[i];

        match ch {
            '\'' | '"' if i == 0 || chars[i - 1] != '\\' => {
                if !in_quote {
                    in_quote = true;
                    quote_char = ch;
                } else if ch == quote_char {
                    in_quote = false;
                }
            }
            '(' if !in_quote => paren_depth += 1,
            ')' if !in_quote => paren_depth -= 1,
            ',' if !in_quote && paren_depth == 0 => {
                args.push(&input[current_start..i]);
                current_start = i + 1;
            }
            _ => {}
        }
    }

    // Add final argument
    if current_start < input.len() {
        args.push(&input[current_start..]);
    }

    Ok(args)
}

/// Find matching closing parenthesis for opening parenthesis at given position
fn find_matching_paren(input: &str, open_pos: usize) -> Result<usize> {
    let chars: Vec<char> = input.chars().collect();
    let mut depth = 0;
    let mut in_quote = false;
    let mut quote_char = '"';

    for i in open_pos..chars.len() {
        let ch = chars[i];

        match ch {
            '\'' | '"' if i == 0 || chars[i - 1] != '\\' => {
                if !in_quote {
                    in_quote = true;
                    quote_char = ch;
                } else if ch == quote_char {
                    in_quote = false;
                }
            }
            '(' if !in_quote => depth += 1,
            ')' if !in_quote => {
                depth -= 1;
                if depth == 0 {
                    return Ok(i);
                }
            }
            _ => {}
        }
    }

    Err(HyperQLError::simple_parse_error(
        "Unmatched opening parenthesis",
        input,
        1,
        1,
    ))
}

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

    #[test]
    fn test_parse_similarity_function_cosine() {
        let input = "SIMILARITY(text_embedding, query_vector, 'cosine')";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity {
                vector_name,
                metric,
                threshold,
                ..
            }) => {
                assert_eq!(vector_name, "text_embedding");
                assert!(matches!(metric, SimilarityMetric::Cosine));
                assert_eq!(threshold, None);
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similarity_function_dotproduct() {
        let input = "SIMILARITY(code_vec, target, \"dotproduct\")";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                assert!(matches!(metric, SimilarityMetric::DotProduct));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similarity_function_euclidean() {
        let input = "SIMILARITY(embedding, ref, 'euclidean')";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                assert!(matches!(metric, SimilarityMetric::Euclidean));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similarity_function_manhattan() {
        let input = "SIMILARITY(vec, reference, 'manhattan')";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                assert!(matches!(metric, SimilarityMetric::Manhattan));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similarity_function_jaccard() {
        let input = "SIMILARITY(sparse_vec, query, 'jaccard')";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                assert!(matches!(metric, SimilarityMetric::Jaccard));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similarity_function_custom() {
        let input = "SIMILARITY(vec, ref, 'custom_metric')";
        let result = parse_similarity_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                match metric {
                    SimilarityMetric::Custom(name) => assert_eq!(name, "custom_metric"),
                    _ => panic!("Expected Custom metric"),
                }
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_distance_function_euclidean() {
        let input = "DISTANCE(embedding, target, 'euclidean')";
        let result = parse_distance_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity {
                vector_name,
                metric,
                ..
            }) => {
                assert_eq!(vector_name, "embedding");
                assert!(matches!(metric, SimilarityMetric::Euclidean));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_distance_function_manhattan() {
        let input = "DISTANCE(vec, query, 'manhattan')";
        let result = parse_distance_function(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { metric, .. }) => {
                assert!(matches!(metric, SimilarityMetric::Manhattan));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similar_to_with_threshold() {
        let input = "text_embedding SIMILAR TO query_vec THRESHOLD 0.8";
        let result = parse_similar_to_expression(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity {
                vector_name,
                threshold,
                metric,
                ..
            }) => {
                assert_eq!(vector_name, "text_embedding");
                assert_eq!(threshold, Some(0.8));
                assert!(matches!(metric, SimilarityMetric::Cosine));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similar_to_with_integer_threshold() {
        let input = "code_embedding SIMILAR TO target THRESHOLD 1";
        let result = parse_similar_to_expression(input);
        assert!(result.is_ok());

        match result.unwrap() {
            Expression::Vector(VectorExpression::Similarity { threshold, .. }) => {
                assert_eq!(threshold, Some(1.0));
            }
            _ => panic!("Expected Vector::Similarity expression"),
        }
    }

    #[test]
    fn test_parse_similar_to_missing_threshold() {
        let input = "text_embedding SIMILAR TO query_vec";
        let result = parse_similar_to_expression(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_similar_to_invalid_threshold_range() {
        let input = "embedding SIMILAR TO query THRESHOLD 1.5";
        let result = parse_similar_to_expression(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_similarity_missing_arguments() {
        let input = "SIMILARITY(vec, ref)";
        let result = parse_similarity_function(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_similarity_invalid_metric() {
        let input = "SIMILARITY(vec, ref, cosine)";
        let result = parse_similarity_function(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_distance_missing_arguments() {
        let input = "DISTANCE(vec, ref)";
        let result = parse_distance_function(input);
        assert!(result.is_err());
    }

    #[test]
    fn test_metric_string_parsing_dot_alias() {
        let result = parse_metric_string("'dot'");
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), SimilarityMetric::DotProduct));
    }

    #[test]
    fn test_metric_string_parsing_case_insensitive() {
        let result = parse_metric_string("'COSINE'");
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), SimilarityMetric::Cosine));
    }

    #[test]
    fn test_split_function_args_simple() {
        let input = "arg1, arg2, arg3";
        let result = split_function_args(input);
        assert!(result.is_ok());
        let args = result.unwrap();
        assert_eq!(args.len(), 3);
        assert_eq!(args[0].trim(), "arg1");
        assert_eq!(args[1].trim(), "arg2");
        assert_eq!(args[2].trim(), "arg3");
    }

    #[test]
    fn test_split_function_args_with_nested_parens() {
        let input = "func(a, b), arg2, arg3";
        let result = split_function_args(input);
        assert!(result.is_ok());
        let args = result.unwrap();
        assert_eq!(args.len(), 3);
        assert_eq!(args[0].trim(), "func(a, b)");
    }

    #[test]
    fn test_split_function_args_with_quotes() {
        let input = "arg1, 'string, with, commas', arg3";
        let result = split_function_args(input);
        assert!(result.is_ok());
        let args = result.unwrap();
        assert_eq!(args.len(), 3);
        assert_eq!(args[1].trim(), "'string, with, commas'");
    }

    #[test]
    fn test_find_matching_paren_simple() {
        let input = "(test)";
        let result = find_matching_paren(input, 0);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 5);
    }

    #[test]
    fn test_find_matching_paren_nested() {
        let input = "(a(b)c)";
        let result = find_matching_paren(input, 0);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 6);
    }

    #[test]
    fn test_find_matching_paren_unmatched() {
        let input = "(test";
        let result = find_matching_paren(input, 0);
        assert!(result.is_err());
    }
}