use serde::{Deserialize, Serialize};
use crate::ast::Expression;
use crate::HyperQLError;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NearNode {
pub target: Box<Expression>,
pub reference: Box<Expression>,
pub max_distance: f64,
pub order_by_distance: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NearConfig {
pub epsilon: f64,
pub approximate: bool,
pub max_candidates: Option<u64>,
pub use_spatial_index: bool,
}
impl NearNode {
pub fn new(
target: Expression,
reference: Expression,
max_distance: f64,
) -> Self {
Self {
target: Box::new(target),
reference: Box::new(reference),
max_distance,
order_by_distance: true, }
}
pub fn new_with_ordering(
target: Expression,
reference: Expression,
max_distance: f64,
order_by_distance: bool,
) -> Self {
Self {
target: Box::new(target),
reference: Box::new(reference),
max_distance,
order_by_distance,
}
}
pub fn validate(&self) -> Result<(), HyperQLError> {
if self.max_distance <= 0.0 {
return Err(HyperQLError::ValidationError {
message: "NEAR max_distance must be positive".to_string(),
field: Some("max_distance".to_string()),
});
}
if self.max_distance.is_infinite() || self.max_distance.is_nan() {
return Err(HyperQLError::ValidationError {
message: "NEAR max_distance must be a finite positive number".to_string(),
field: Some("max_distance".to_string()),
});
}
Ok(())
}
pub fn estimate_cost(&self) -> f64 {
let base_cost = 1.0;
let distance_factor = if self.max_distance > 5.0 { 2.0 } else { 1.0 };
let ordering_factor = if self.order_by_distance { 1.5 } else { 1.0 };
base_cost * distance_factor * ordering_factor
}
pub fn should_use_spatial_index(&self) -> bool {
self.max_distance > 3.0 || self.order_by_distance
}
}
impl Default for NearConfig {
fn default() -> Self {
Self {
epsilon: 1e-10,
approximate: false,
max_candidates: None,
use_spatial_index: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::{Literal, Expression};
#[test]
fn test_near_node_creation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let max_distance = 3.0;
let near_node = NearNode::new(target, reference, max_distance);
assert_eq!(near_node.max_distance, 3.0);
assert_eq!(near_node.order_by_distance, true);
}
#[test]
fn test_near_node_with_ordering() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let max_distance = 2.5;
let near_node = NearNode::new_with_ordering(target, reference, max_distance, false);
assert_eq!(near_node.max_distance, 2.5);
assert_eq!(near_node.order_by_distance, false);
}
#[test]
fn test_near_validation_positive_distance() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let valid_node = NearNode::new(target.clone(), reference.clone(), 1.0);
assert!(valid_node.validate().is_ok());
let invalid_node = NearNode::new(target, reference, -1.0);
assert!(invalid_node.validate().is_err());
}
#[test]
fn test_near_validation_finite_distance() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let infinite_node = NearNode::new(target.clone(), reference.clone(), f64::INFINITY);
assert!(infinite_node.validate().is_err());
let nan_node = NearNode::new(target, reference, f64::NAN);
assert!(nan_node.validate().is_err());
}
#[test]
fn test_cost_estimation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let small_distance_node = NearNode::new_with_ordering(
target.clone(), reference.clone(), 1.0, false
);
let large_distance_ordered_node = NearNode::new_with_ordering(
target, reference, 10.0, true
);
let small_cost = small_distance_node.estimate_cost();
let large_cost = large_distance_ordered_node.estimate_cost();
assert!(small_cost < large_cost);
}
#[test]
fn test_spatial_index_recommendation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let reference = Expression::Literal(Literal::String("ref_pos".to_string()));
let small_unordered = NearNode::new_with_ordering(
target.clone(), reference.clone(), 1.0, false
);
let large_unordered = NearNode::new_with_ordering(
target.clone(), reference.clone(), 5.0, false
);
let small_ordered = NearNode::new_with_ordering(
target, reference, 1.0, true
);
assert!(!small_unordered.should_use_spatial_index());
assert!(large_unordered.should_use_spatial_index());
assert!(small_ordered.should_use_spatial_index());
}
}