use serde::{Deserialize, Serialize};
use crate::ast::Expression;
use crate::HyperQLError;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InRadiusNode {
pub target: Box<Expression>,
pub center: Box<Expression>,
pub radius: f64,
pub include_boundary: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InRadiusConfig {
pub epsilon: f64,
pub approximate: bool,
pub use_spatial_index: bool,
pub max_candidates: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RadiusTestType {
OpenBall,
ClosedBall,
}
impl InRadiusNode {
pub fn new(target: Expression, center: Expression, radius: f64) -> Self {
Self {
target: Box::new(target),
center: Box::new(center),
radius,
include_boundary: false,
}
}
pub fn new_with_boundary(
target: Expression,
center: Expression,
radius: f64,
include_boundary: bool,
) -> Self {
Self {
target: Box::new(target),
center: Box::new(center),
radius,
include_boundary,
}
}
pub fn validate(&self) -> Result<(), HyperQLError> {
if self.radius <= 0.0 {
return Err(HyperQLError::ValidationError {
message: "IN_RADIUS radius must be positive".to_string(),
field: Some("radius".to_string()),
});
}
if self.radius.is_infinite() || self.radius.is_nan() {
return Err(HyperQLError::ValidationError {
message: "IN_RADIUS radius must be a finite positive number".to_string(),
field: Some("radius".to_string()),
});
}
Ok(())
}
pub fn test_type(&self) -> RadiusTestType {
if self.include_boundary {
RadiusTestType::ClosedBall
} else {
RadiusTestType::OpenBall
}
}
pub fn estimate_selectivity(&self) -> f64 {
let normalized_radius = self.radius.min(8.0) / 8.0;
let exponential_factor = (normalized_radius * 2.0).exp() - 1.0;
let max_exponential = (2.0_f64 * 2.0).exp() - 1.0;
(exponential_factor / max_exponential).min(1.0)
}
pub fn should_use_spatial_index(&self) -> bool {
self.radius > 1.0 && self.radius < 10.0
}
pub fn to_within_operation(&self) -> crate::ast::geometric::GeometricExpression {
use crate::ast::geometric::GeometricExpression;
GeometricExpression::Within {
target: self.target.clone(),
radius: self.radius,
reference: self.center.clone(),
}
}
}
impl Default for InRadiusConfig {
fn default() -> Self {
Self {
epsilon: 1e-10,
approximate: false,
use_spatial_index: true,
max_candidates: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::{Literal, Expression};
#[test]
fn test_in_radius_node_creation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let radius = 4.0;
let in_radius_node = InRadiusNode::new(target, center, radius);
assert_eq!(in_radius_node.radius, 4.0);
assert_eq!(in_radius_node.include_boundary, false);
assert_eq!(in_radius_node.test_type(), RadiusTestType::OpenBall);
}
#[test]
fn test_in_radius_node_with_boundary() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let radius = 3.5;
let closed_ball_node = InRadiusNode::new_with_boundary(target, center, radius, true);
assert_eq!(closed_ball_node.radius, 3.5);
assert_eq!(closed_ball_node.include_boundary, true);
assert_eq!(closed_ball_node.test_type(), RadiusTestType::ClosedBall);
}
#[test]
fn test_in_radius_validation_positive_radius() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let valid_node = InRadiusNode::new(target.clone(), center.clone(), 1.0);
assert!(valid_node.validate().is_ok());
let invalid_node = InRadiusNode::new(target, center, -1.0);
assert!(invalid_node.validate().is_err());
}
#[test]
fn test_in_radius_validation_finite_radius() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let infinite_node = InRadiusNode::new(target.clone(), center.clone(), f64::INFINITY);
assert!(infinite_node.validate().is_err());
let nan_node = InRadiusNode::new(target, center, f64::NAN);
assert!(nan_node.validate().is_err());
}
#[test]
fn test_selectivity_estimation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let small_radius_node = InRadiusNode::new(target.clone(), center.clone(), 1.0);
let large_radius_node = InRadiusNode::new(target, center, 6.0);
let small_selectivity = small_radius_node.estimate_selectivity();
let large_selectivity = large_radius_node.estimate_selectivity();
assert!(small_selectivity < large_selectivity);
assert!(small_selectivity >= 0.0 && small_selectivity <= 1.0);
assert!(large_selectivity >= 0.0 && large_selectivity <= 1.0);
}
#[test]
fn test_spatial_index_recommendation() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let very_small_radius = InRadiusNode::new(target.clone(), center.clone(), 0.5);
let optimal_radius = InRadiusNode::new(target.clone(), center.clone(), 3.0);
let very_large_radius = InRadiusNode::new(target, center, 15.0);
assert!(!very_small_radius.should_use_spatial_index());
assert!(optimal_radius.should_use_spatial_index());
assert!(!very_large_radius.should_use_spatial_index());
}
#[test]
fn test_to_within_conversion() {
let target = Expression::Literal(Literal::String("position".to_string()));
let center = Expression::Literal(Literal::String("center_pos".to_string()));
let radius = 2.0;
let in_radius_node = InRadiusNode::new(target.clone(), center.clone(), radius);
let within_op = in_radius_node.to_within_operation();
match within_op {
crate::ast::geometric::GeometricExpression::Within { target: within_target, radius: within_radius, reference } => {
assert_eq!(*within_target, target);
assert_eq!(within_radius, radius);
assert_eq!(*reference, center);
}
_ => panic!("Expected Within operation"),
}
}
}