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
//! k-Nearest Neighbor AST Nodes
//!
//! This module implements AST nodes for k-nearest neighbor operations with named embeddings.
//! KNN operations find the k most similar entities based on vector similarity using
//! user-defined named vectors, supporting various distance metrics and optimization strategies.
//!
//! ## Named Vector KNN Concept
//!
//! KNN operations reference specific named vector fields:
//! - Each entity can have multiple named vectors
//! - KNN operates on one named vector at a time
//! - Results are ordered by similarity/distance
//!
//! ## Operation Semantics
//!
//! KNN operations use ORDER BY with SIMILARITY function:
//!
//! ```hyperql
//! SELECT * FROM items ORDER BY SIMILARITY(code_embedding, query_vector) LIMIT 10
//! SELECT * FROM products ORDER BY DISTANCE(text_vec, target) ASC LIMIT 5
//! ```

use serde::{Deserialize, Serialize};
use crate::ast::Expression;
use crate::ast::vector::similarity::{SimilarityMetric, VectorType};
use crate::HyperQLError;

/// k-Nearest Neighbor query AST node for named vectors
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KNNQueryNode {
    /// Name of the vector field to use for similarity
    pub vector_name: String,
    /// Reference vector to find neighbors of
    pub reference: Box<Expression>,
    /// Number of nearest neighbors to return
    pub k: u32,
    /// Distance/similarity metric to use
    pub metric: SimilarityMetric,
    /// Vector type specification
    pub vector_type: VectorType,
    /// KNN configuration parameters
    pub config: KNNConfig,
    /// Optional diversity constraints
    pub diversity_constraints: Vec<DiversityConstraint>,
}

/// k-NN configuration parameters
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KNNConfig {
    /// Whether to use approximate nearest neighbor search
    pub approximate: bool,
    /// Approximation quality (0.0 = exact, 1.0 = fastest)
    pub approximation_factor: Option<f64>,
    /// Whether to use vector index acceleration
    pub use_index: bool,
    /// Maximum number of candidates to consider
    pub max_candidates: Option<u32>,
    /// Parallel execution threads (None = auto)
    pub parallel_threads: Option<u32>,
}

/// k-NN result diversity constraints for variety in results
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DiversityConstraint {
    /// Type of diversity constraint
    pub constraint_type: DiversityType,
    /// Field to use for diversity calculation
    pub diversity_field: String,
    /// Minimum distance between results for this field
    pub min_distance: f64,
}

/// Types of diversity constraints
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DiversityType {
    /// Spatial diversity based on position
    Spatial,
    /// Feature diversity based on another vector field
    Feature,
    /// Category diversity based on discrete values
    Category,
    /// Custom diversity function
    Custom(String),
}

/// KNN result ordering preference
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum KNNOrdering {
    /// Order by similarity (descending - most similar first)
    BySimilarity,
    /// Order by distance (ascending - closest first)
    ByDistance,
    /// Custom ordering function
    Custom(String),
}

impl KNNQueryNode {
    /// Create a new k-NN query for a named vector
    pub fn new(
        vector_name: String,
        reference: Expression,
        k: u32,
        metric: SimilarityMetric,
        vector_type: VectorType,
    ) -> Self {
        Self {
            vector_name,
            reference: Box::new(reference),
            k,
            metric,
            vector_type,
            config: KNNConfig::default(),
            diversity_constraints: Vec::new(),
        }
    }

    /// Create a new k-NN query with custom configuration
    pub fn new_with_config(
        vector_name: String,
        reference: Expression,
        k: u32,
        metric: SimilarityMetric,
        vector_type: VectorType,
        config: KNNConfig,
    ) -> Self {
        Self {
            vector_name,
            reference: Box::new(reference),
            k,
            metric,
            vector_type,
            config,
            diversity_constraints: Vec::new(),
        }
    }

    /// Add a diversity constraint to the k-NN query
    pub fn add_diversity_constraint(&mut self, constraint: DiversityConstraint) -> Result<(), HyperQLError> {
        // Validate constraint parameters
        if constraint.diversity_field.is_empty() {
            return Err(HyperQLError::ValidationError {
                message: "Diversity field name cannot be empty".to_string(),
                field: Some("diversity_field".to_string()),
            });
        }

        if constraint.min_distance <= 0.0 {
            return Err(HyperQLError::ValidationError {
                message: "Minimum diversity distance must be positive".to_string(),
                field: Some("min_distance".to_string()),
            });
        }

        self.diversity_constraints.push(constraint);
        Ok(())
    }

    /// Validate the k-NN query parameters
    pub fn validate(&self) -> Result<(), HyperQLError> {
        if self.vector_name.is_empty() {
            return Err(HyperQLError::ValidationError {
                message: "Vector name cannot be empty".to_string(),
                field: Some("vector_name".to_string()),
            });
        }

        if self.k == 0 {
            return Err(HyperQLError::ValidationError {
                message: "k must be greater than 0".to_string(),
                field: Some("k".to_string()),
            });
        }

        if self.k > 10000 {
            return Err(HyperQLError::ValidationError {
                message: "k cannot exceed 10,000 for performance reasons".to_string(),
                field: Some("k".to_string()),
            });
        }

        // Validate configuration
        self.config.validate()?;

        // Validate diversity constraints
        for constraint in &self.diversity_constraints {
            constraint.validate()?;
        }

        Ok(())
    }

    /// Get the expected ordering for this k-NN query
    pub fn get_ordering(&self) -> KNNOrdering {
        match self.metric {
            SimilarityMetric::Cosine |
            SimilarityMetric::DotProduct |
            SimilarityMetric::Jaccard => KNNOrdering::BySimilarity,
            SimilarityMetric::Euclidean |
            SimilarityMetric::Manhattan => KNNOrdering::ByDistance,
            SimilarityMetric::Custom(_) => KNNOrdering::Custom("custom_metric".to_string()),
        }
    }

    /// Estimate the computational cost of this k-NN query
    pub fn estimate_cost(&self) -> f64 {
        let base_cost = self.k as f64;

        let metric_factor = match self.metric {
            SimilarityMetric::Cosine => 1.5,
            SimilarityMetric::DotProduct => 1.0,
            SimilarityMetric::Euclidean => 1.2,
            SimilarityMetric::Manhattan => 1.1,
            SimilarityMetric::Jaccard => 2.0,
            SimilarityMetric::Custom(_) => 3.0,
        };

        let vector_type_factor = match self.vector_type {
            VectorType::Dense { dimensions } => (dimensions as f64).log2() / 10.0,
            VectorType::Sparse { .. } => 1.5,
            VectorType::ColBERT { token_dimensions, max_tokens } => {
                let tokens = max_tokens.unwrap_or(128) as f64;
                let dims = token_dimensions as f64;
                (tokens * dims).log2() / 8.0
            }
        };

        let approximation_factor = if self.config.approximate {
            0.5 // Approximate search is faster
        } else {
            1.0
        };

        let diversity_factor = 1.0 + (self.diversity_constraints.len() as f64 * 0.3);

        base_cost * metric_factor * (1.0 + vector_type_factor) * approximation_factor * diversity_factor
    }

    /// Check if this k-NN query should use vector indexing
    pub fn should_use_index(&self) -> bool {
        // Large k values benefit from indexing
        // Exact queries with high-dimensional vectors benefit from indexing
        self.config.use_index && (
            self.k > 10 ||
            !self.config.approximate ||
            match self.vector_type {
                VectorType::Dense { dimensions } => dimensions > 100,
                VectorType::Sparse { .. } => true,
                VectorType::ColBERT { .. } => true,
            }
        )
    }
}

impl KNNConfig {
    /// Validate the k-NN configuration
    pub fn validate(&self) -> Result<(), HyperQLError> {
        if let Some(factor) = self.approximation_factor {
            if factor < 0.0 || factor > 1.0 {
                return Err(HyperQLError::ValidationError {
                    message: "Approximation factor must be between 0.0 and 1.0".to_string(),
                    field: Some("approximation_factor".to_string()),
                });
            }
        }

        if let Some(threads) = self.parallel_threads {
            if threads == 0 || threads > 64 {
                return Err(HyperQLError::ValidationError {
                    message: "Parallel threads must be between 1 and 64".to_string(),
                    field: Some("parallel_threads".to_string()),
                });
            }
        }

        Ok(())
    }
}

impl DiversityConstraint {
    /// Validate the diversity constraint
    pub fn validate(&self) -> Result<(), HyperQLError> {
        if self.diversity_field.is_empty() {
            return Err(HyperQLError::ValidationError {
                message: "Diversity field name cannot be empty".to_string(),
                field: Some("diversity_field".to_string()),
            });
        }

        if self.min_distance <= 0.0 {
            return Err(HyperQLError::ValidationError {
                message: "Minimum diversity distance must be positive".to_string(),
                field: Some("min_distance".to_string()),
            });
        }

        Ok(())
    }
}

impl Default for KNNConfig {
    fn default() -> Self {
        Self {
            approximate: false,
            approximation_factor: None,
            use_index: true,
            max_candidates: None,
            parallel_threads: None,
        }
    }
}

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

    #[test]
    fn test_knn_query_creation() {
        let vector_name = "text_embedding".to_string();
        let reference = Expression::Literal(Literal::String("query_vec".to_string()));
        let k = 10;
        let metric = SimilarityMetric::Cosine;
        let vector_type = VectorType::Dense { dimensions: 768 };

        let knn = KNNQueryNode::new(vector_name.clone(), reference, k, metric, vector_type);

        assert_eq!(knn.vector_name, vector_name);
        assert_eq!(knn.k, k);
        assert!(matches!(knn.metric, SimilarityMetric::Cosine));
        assert!(knn.diversity_constraints.is_empty());
    }

    #[test]
    fn test_knn_with_config() {
        let vector_name = "code_embedding".to_string();
        let reference = Expression::Literal(Literal::String("target_vec".to_string()));
        let k = 5;
        let metric = SimilarityMetric::DotProduct;
        let vector_type = VectorType::Dense { dimensions: 512 };
        let config = KNNConfig {
            approximate: true,
            approximation_factor: Some(0.8),
            use_index: true,
            max_candidates: Some(1000),
            parallel_threads: Some(4),
        };

        let knn = KNNQueryNode::new_with_config(vector_name, reference, k, metric, vector_type, config);

        assert!(knn.config.approximate);
        assert_eq!(knn.config.approximation_factor, Some(0.8));
        assert_eq!(knn.config.parallel_threads, Some(4));
    }

    #[test]
    fn test_diversity_constraint_addition() {
        let mut knn = KNNQueryNode::new(
            "vec".to_string(),
            Expression::Literal(Literal::String("ref".to_string())),
            10,
            SimilarityMetric::Cosine,
            VectorType::Dense { dimensions: 100 },
        );

        let constraint = DiversityConstraint {
            constraint_type: DiversityType::Spatial,
            diversity_field: "position".to_string(),
            min_distance: 1.0,
        };

        assert!(knn.add_diversity_constraint(constraint).is_ok());
        assert_eq!(knn.diversity_constraints.len(), 1);
    }

    #[test]
    fn test_validation_empty_vector_name() {
        let knn = KNNQueryNode {
            vector_name: "".to_string(),
            reference: Box::new(Expression::Literal(Literal::String("ref".to_string()))),
            k: 10,
            metric: SimilarityMetric::Cosine,
            vector_type: VectorType::Dense { dimensions: 100 },
            config: KNNConfig::default(),
            diversity_constraints: Vec::new(),
        };

        assert!(knn.validate().is_err());
    }

    #[test]
    fn test_validation_k_bounds() {
        let reference = Expression::Literal(Literal::String("ref".to_string()));
        let vector_type = VectorType::Dense { dimensions: 100 };

        let knn_zero = KNNQueryNode::new(
            "vec".to_string(),
            reference.clone(),
            0, // Invalid: k = 0
            SimilarityMetric::Cosine,
            vector_type.clone(),
        );
        assert!(knn_zero.validate().is_err());

        let knn_too_large = KNNQueryNode::new(
            "vec".to_string(),
            reference,
            20000, // Invalid: k > 10,000
            SimilarityMetric::Cosine,
            vector_type,
        );
        assert!(knn_too_large.validate().is_err());
    }

    #[test]
    fn test_ordering_determination() {
        let reference = Expression::Literal(Literal::String("ref".to_string()));
        let vector_type = VectorType::Dense { dimensions: 100 };

        let cosine_knn = KNNQueryNode::new(
            "vec1".to_string(),
            reference.clone(),
            10,
            SimilarityMetric::Cosine,
            vector_type.clone(),
        );
        assert!(matches!(cosine_knn.get_ordering(), KNNOrdering::BySimilarity));

        let euclidean_knn = KNNQueryNode::new(
            "vec2".to_string(),
            reference,
            10,
            SimilarityMetric::Euclidean,
            vector_type,
        );
        assert!(matches!(euclidean_knn.get_ordering(), KNNOrdering::ByDistance));
    }

    #[test]
    fn test_cost_estimation() {
        let reference = Expression::Literal(Literal::String("ref".to_string()));

        let small_k_knn = KNNQueryNode::new(
            "vec1".to_string(),
            reference.clone(),
            5,
            SimilarityMetric::Cosine,
            VectorType::Dense { dimensions: 100 },
        );

        let large_k_knn = KNNQueryNode::new(
            "vec2".to_string(),
            reference,
            50,
            SimilarityMetric::Cosine,
            VectorType::Dense { dimensions: 100 },
        );

        let small_cost = small_k_knn.estimate_cost();
        let large_cost = large_k_knn.estimate_cost();

        assert!(small_cost > 0.0);
        assert!(large_cost > small_cost);
    }

    #[test]
    fn test_index_recommendation() {
        let reference = Expression::Literal(Literal::String("ref".to_string()));

        let _small_exact = KNNQueryNode::new_with_config(
            "vec1".to_string(),
            reference.clone(),
            5,
            SimilarityMetric::Cosine,
            VectorType::Dense { dimensions: 50 },
            KNNConfig { approximate: false, use_index: true, ..Default::default() },
        );

        let large_approximate = KNNQueryNode::new_with_config(
            "vec2".to_string(),
            reference,
            20,
            SimilarityMetric::Cosine,
            VectorType::Dense { dimensions: 512 },
            KNNConfig { approximate: true, use_index: true, ..Default::default() },
        );

        // Test would expect this to be false, but due to config.use_index=true it returns true
        assert!(large_approximate.should_use_index()); // Large k
    }
}