use crate::ast::*;
use crate::error::*;
use super::expression::ExpressionCompiler;
use super::{CompiledProjection, CompiledSortKey, CompiledExpression, ExecutionPlan, VectorOpType};
use std::collections::HashMap;
pub struct SelectCompiler {
expression_compiler: ExpressionCompiler,
}
impl SelectCompiler {
pub fn new() -> Self {
Self {
expression_compiler: ExpressionCompiler::new(),
}
}
pub fn compile_select(&self, select: SelectStatement) -> Result<ExecutionPlan> {
let mut plan = self.create_base_scan(&select)?;
if !select.joins.is_empty() {
plan = self.compile_joins(plan, &select.joins)?;
}
if let Some(traverse_clause) = select.traverse_clause {
let compiled_patterns = self.compile_traverse_patterns(&traverse_clause.patterns)?;
let traverse_plan = ExecutionPlan::Traverse {
patterns: compiled_patterns,
};
if let ExecutionPlan::Scan { .. } = plan {
plan = traverse_plan;
} else {
plan = traverse_plan;
}
}
if let Some(where_expr) = select.where_clause {
let compiled_predicate = self.expression_compiler.compile_expression(where_expr)?;
if self.is_geometric_function(&compiled_predicate) {
plan = self.create_geometric_operation_plan(compiled_predicate, plan)?;
} else if self.is_vector_function(&compiled_predicate) {
plan = self.create_vector_operation_plan(compiled_predicate, plan)?;
} else {
plan = ExecutionPlan::Filter {
input: Box::new(plan),
predicate: compiled_predicate,
};
}
}
let projections = self.compile_select_list(&select.select_list)?;
let has_aggregates = projections.iter().any(|proj| self.contains_aggregate(&proj.expression));
if !select.group_by.is_empty() || has_aggregates {
let group_expressions = if !select.group_by.is_empty() {
select.group_by.iter()
.map(|expr| self.expression_compiler.compile_expression(expr.clone()))
.collect::<Result<Vec<_>>>()?
} else {
vec![]
};
plan = ExecutionPlan::GroupBy {
input: Box::new(plan),
group_expressions,
aggregate_expressions: projections,
};
} else {
if !projections.is_empty() {
plan = ExecutionPlan::Project {
input: Box::new(plan),
expressions: projections,
distinct: select.distinct,
};
}
}
if let Some(having_expr) = select.having {
let compiled_having = self.expression_compiler.compile_expression(having_expr)?;
plan = ExecutionPlan::Having {
input: Box::new(plan),
predicate: compiled_having,
};
}
if !select.order_by.is_empty() {
if let Some(vector_plan) = self.try_create_knn_plan(&select.order_by, select.limit, plan.clone())? {
plan = vector_plan;
} else {
let sort_keys = self.compile_order_by(&select.order_by)?;
plan = ExecutionPlan::Sort {
input: Box::new(plan),
sort_keys,
};
}
}
if let Some(limit) = select.limit {
plan = ExecutionPlan::Limit {
input: Box::new(plan),
count: limit,
offset: select.offset,
};
}
Ok(plan)
}
fn create_base_scan(&self, select: &SelectStatement) -> Result<ExecutionPlan> {
let (table_name, entity_type, alias) = match &select.from {
Some(FromClause::Table { collection, entity_type, alias }) => {
let entity_type_str = entity_type.as_ref().map(|s| s.clone()).unwrap_or_default();
(collection.clone(), entity_type_str, alias.clone())
}
Some(FromClause::Subquery { .. }) => {
return Err(HyperQLError::SemanticError {
message: "Subqueries are not yet supported".to_string(),
context: vec!["compiler".to_string()],
});
}
None => {
return Err(HyperQLError::SemanticError {
message: "FROM clause is required".to_string(),
context: vec!["compiler".to_string()],
});
}
};
let scan_limit = if select.where_clause.is_none()
&& select.order_by.is_empty()
&& select.group_by.is_empty()
&& select.having.is_none() {
select.limit
} else if select.where_clause.is_some()
&& select.order_by.is_empty()
&& select.group_by.is_empty()
&& select.having.is_none() {
select.limit
} else {
None
};
Ok(ExecutionPlan::Scan {
table: table_name,
entity_type,
alias,
filter: None,
projection: vec![],
limit: scan_limit,
})
}
fn compile_select_list(&self, select_list: &[SelectItem]) -> Result<Vec<CompiledProjection>> {
let mut projections = Vec::new();
for item in select_list {
match item {
SelectItem::Wildcard => {
projections.push(CompiledProjection {
expression: CompiledExpression::Literal(crate::types::Value::String("*".to_string())),
alias: None,
output_name: "*".to_string(),
});
}
SelectItem::Expression { expr, alias } => {
let compiled_expr = self.expression_compiler.compile_expression(expr.clone())?;
let output_name = alias.clone().unwrap_or_else(|| {
self.generate_expression_name(expr)
});
projections.push(CompiledProjection {
expression: compiled_expr,
alias: alias.clone(),
output_name,
});
}
}
}
Ok(projections)
}
fn compile_order_by(&self, order_by: &[OrderByItem]) -> Result<Vec<CompiledSortKey>> {
let mut sort_keys = Vec::new();
for item in order_by {
let compiled_expr = self.expression_compiler.compile_expression(item.expr.clone())?;
sort_keys.push(CompiledSortKey {
expression: compiled_expr,
direction: item.direction.clone(),
});
}
Ok(sort_keys)
}
fn generate_expression_name(&self, expr: &Expression) -> String {
match expr {
Expression::Column(col_ref) => {
if let Some(ref table) = col_ref.table {
format!("{}.{}", table, col_ref.name)
} else {
col_ref.name.clone()
}
}
Expression::Function { name, args } => {
if args.is_empty() {
format!("{}(*)", name)
} else if args.len() == 1 {
if let Expression::Column(col_ref) = &args[0] {
if col_ref.name == "*" {
return format!("{}(*)", name);
}
return format!("{}({})", name, self.generate_expression_name(&args[0]));
}
format!("{}({})", name, self.generate_expression_name(&args[0]))
} else {
format!("{}(...)", name)
}
}
Expression::Literal(literal) => format!("{:?}", literal),
_ => "expr".to_string(),
}
}
fn compile_traverse_patterns(&self, patterns: &[crate::ast::TraversePattern]) -> Result<Vec<super::CompiledTraversePattern>> {
let mut compiled_patterns = Vec::new();
for pattern in patterns {
let compiled_start_node = super::CompiledNodePattern {
variable: pattern.start_node.variable.clone(),
label: pattern.start_node.label.clone(),
properties: if let Some(props) = &pattern.start_node.properties {
Some(self.expression_compiler.compile_expression(props.clone())?)
} else {
None
},
};
let compiled_end_node = super::CompiledNodePattern {
variable: pattern.end_node.variable.clone(),
label: pattern.end_node.label.clone(),
properties: if let Some(props) = &pattern.end_node.properties {
Some(self.expression_compiler.compile_expression(props.clone())?)
} else {
None
},
};
let compiled_relationship = super::CompiledRelationshipPattern {
variable: pattern.relationship.variable.clone(),
rel_type: pattern.relationship.rel_type.clone(),
direction: pattern.relationship.direction.clone(),
variable_length: pattern.relationship.variable_length.clone(),
optional: pattern.relationship.optional,
properties: if let Some(props) = &pattern.relationship.properties {
Some(self.expression_compiler.compile_expression(props.clone())?)
} else {
None
},
};
compiled_patterns.push(super::CompiledTraversePattern {
start_node: compiled_start_node,
relationship: compiled_relationship,
end_node: compiled_end_node,
});
}
Ok(compiled_patterns)
}
fn contains_aggregate(&self, expr: &CompiledExpression) -> bool {
match expr {
CompiledExpression::Function { name, args, .. } => {
let is_aggregate = matches!(
name.to_uppercase().as_str(),
"COUNT" | "SUM" | "AVG" | "MIN" | "MAX" | "ARRAY_AGG" | "STRING_AGG"
);
if is_aggregate {
return true;
}
args.iter().any(|arg| self.contains_aggregate(arg))
}
CompiledExpression::Binary { left, right, .. } => {
self.contains_aggregate(left) || self.contains_aggregate(right)
}
CompiledExpression::Unary { expr, .. } => {
self.contains_aggregate(expr)
}
_ => false,
}
}
fn compile_joins(&self, left_plan: ExecutionPlan, joins: &[JoinClause]) -> Result<ExecutionPlan> {
let mut current_plan = left_plan;
for join_clause in joins {
let entity_type_str = join_clause.entity_type.as_ref().map(|s| s.clone()).unwrap_or_default();
let right_plan = ExecutionPlan::Scan {
table: join_clause.collection.clone(),
entity_type: entity_type_str,
alias: join_clause.alias.clone(),
filter: None,
projection: vec![],
limit: None,
};
let compiled_condition = self.expression_compiler.compile_expression(join_clause.on_condition.clone())?;
current_plan = ExecutionPlan::Join {
left: Box::new(current_plan),
right: Box::new(right_plan),
join_type: join_clause.join_type.clone(),
on_condition: compiled_condition,
};
}
Ok(current_plan)
}
fn is_geometric_function(&self, expr: &CompiledExpression) -> bool {
match expr {
CompiledExpression::Function { name, args, .. } => {
let is_geometric = matches!(
name.to_uppercase().as_str(),
"HYPERBOLIC_DISTANCE" | "GEODESIC_DISTANCE" |
"WITHIN_RADIUS" | "NEAR_POSITIONS" |
"CONTAINS" | "INTERSECTS" |
"NEAR" | "DISTANCE" );
if is_geometric {
return true;
}
args.iter().any(|arg| self.is_geometric_function(arg))
}
CompiledExpression::Binary { left, right, .. } => {
self.is_geometric_function(left) || self.is_geometric_function(right)
}
CompiledExpression::Unary { expr, .. } => {
self.is_geometric_function(expr)
}
_ => false,
}
}
fn function_name_to_geometric_op_type(&self, name: &str) -> Option<super::GeometricOpType> {
match name.to_uppercase().as_str() {
"HYPERBOLIC_DISTANCE" => Some(super::GeometricOpType::HyperbolicDistance),
"GEODESIC_DISTANCE" => Some(super::GeometricOpType::GeodesicDistance),
"WITHIN_RADIUS" => Some(super::GeometricOpType::WithinRadius),
"NEAR_POSITIONS" => Some(super::GeometricOpType::NearPositions),
"CONTAINS" => Some(super::GeometricOpType::Contains),
"INTERSECTS" => Some(super::GeometricOpType::Intersects),
"NEAR" => Some(super::GeometricOpType::NearPositions),
"DISTANCE" => Some(super::GeometricOpType::HyperbolicDistance),
_ => None,
}
}
fn extract_geometric_function(&self, expr: &CompiledExpression) -> Option<(super::GeometricOpType, Vec<CompiledExpression>)> {
match expr {
CompiledExpression::Function { name, args, .. } => {
if let Some(op_type) = self.function_name_to_geometric_op_type(name) {
return Some((op_type, args.clone()));
}
for arg in args {
if let Some(result) = self.extract_geometric_function(arg) {
return Some(result);
}
}
None
}
CompiledExpression::Binary { left, right, .. } => {
self.extract_geometric_function(left)
.or_else(|| self.extract_geometric_function(right))
}
CompiledExpression::Unary { expr, .. } => {
self.extract_geometric_function(expr)
}
_ => None,
}
}
fn create_geometric_operation_plan(
&self,
expr: CompiledExpression,
input: ExecutionPlan,
) -> Result<ExecutionPlan> {
let (op_type, args) = self.extract_geometric_function(&expr)
.ok_or_else(|| HyperQLError::SemanticError {
message: "No geometric function found in expression".to_string(),
context: vec!["create_geometric_operation_plan".to_string()],
})?;
let mut params = HashMap::new();
match op_type {
super::GeometricOpType::HyperbolicDistance | super::GeometricOpType::GeodesicDistance => {
if args.len() == 1 {
params.insert("entity1".to_string(), CompiledExpression::Column {
table: None,
name: "position".to_string(),
value_type: super::ValueType::Position,
});
params.insert("entity2".to_string(), args[0].clone());
} else if args.len() >= 2 {
params.insert("entity1".to_string(), args[0].clone());
params.insert("entity2".to_string(), args[1].clone());
}
if args.len() >= 3 {
params.insert("max_distance".to_string(), args[2].clone());
}
}
super::GeometricOpType::WithinRadius => {
if args.len() >= 3 {
params.insert("target".to_string(), args[0].clone());
params.insert("center".to_string(), args[1].clone());
params.insert("radius".to_string(), args[2].clone());
}
}
super::GeometricOpType::NearPositions => {
if args.len() >= 2 {
params.insert("reference".to_string(), args[0].clone());
params.insert("radius".to_string(), args[1].clone());
} else if !args.is_empty() {
params.insert("reference".to_string(), args[0].clone());
if args.len() >= 2 {
params.insert("limit".to_string(), args[1].clone());
}
}
}
super::GeometricOpType::Contains | super::GeometricOpType::Intersects => {
if args.len() >= 2 {
params.insert("geometry1".to_string(), args[0].clone());
params.insert("geometry2".to_string(), args[1].clone());
}
}
}
params.insert("_full_expression".to_string(), expr);
Ok(ExecutionPlan::GeometricOperation {
op_type,
params,
input: Some(Box::new(input)),
})
}
fn is_vector_function(&self, expr: &CompiledExpression) -> bool {
match expr {
CompiledExpression::Function { name, args, .. } => {
let is_vector = matches!(
name.to_uppercase().as_str(),
"COSINE_SIMILARITY" | "EUCLIDEAN_DISTANCE" |
"DOT_PRODUCT" | "NORMALIZE" |
"KNN" | "SIMILARITY_SEARCH"
);
if is_vector {
return true;
}
args.iter().any(|arg| self.is_vector_function(arg))
}
CompiledExpression::Binary { left, right, .. } => {
self.is_vector_function(left) || self.is_vector_function(right)
}
CompiledExpression::Unary { expr, .. } => {
self.is_vector_function(expr)
}
_ => false,
}
}
fn function_name_to_vector_op_type(&self, name: &str) -> Option<VectorOpType> {
match name.to_uppercase().as_str() {
"COSINE_SIMILARITY" => Some(VectorOpType::CosineSimilarity),
"EUCLIDEAN_DISTANCE" => Some(VectorOpType::EuclideanDistance),
"DOT_PRODUCT" => Some(VectorOpType::DotProduct),
"NORMALIZE" => Some(VectorOpType::Normalize),
"KNN" => Some(VectorOpType::KNN),
"SIMILARITY_SEARCH" => Some(VectorOpType::SimilaritySearch),
_ => None,
}
}
fn extract_vector_function(&self, expr: &CompiledExpression) -> Option<(VectorOpType, Vec<CompiledExpression>)> {
match expr {
CompiledExpression::Function { name, args, .. } => {
if let Some(op_type) = self.function_name_to_vector_op_type(name) {
return Some((op_type, args.clone()));
}
for arg in args {
if let Some(result) = self.extract_vector_function(arg) {
return Some(result);
}
}
None
}
CompiledExpression::Binary { left, right, .. } => {
self.extract_vector_function(left)
.or_else(|| self.extract_vector_function(right))
}
CompiledExpression::Unary { expr, .. } => {
self.extract_vector_function(expr)
}
_ => None,
}
}
fn create_vector_operation_plan(
&self,
expr: CompiledExpression,
input: ExecutionPlan,
) -> Result<ExecutionPlan> {
let (op_type, args) = self.extract_vector_function(&expr)
.ok_or_else(|| HyperQLError::SemanticError {
message: "No vector function found in expression".to_string(),
context: vec!["create_vector_operation_plan".to_string()],
})?;
let mut params = HashMap::new();
match op_type {
VectorOpType::CosineSimilarity | VectorOpType::EuclideanDistance | VectorOpType::DotProduct => {
if args.len() >= 2 {
params.insert("vector_name".to_string(), args[0].clone());
params.insert("reference".to_string(), args[1].clone());
}
if args.len() >= 3 {
params.insert("metric".to_string(), args[2].clone());
}
if args.len() >= 4 {
params.insert("threshold".to_string(), args[3].clone());
}
if args.len() >= 5 {
params.insert("vector_type".to_string(), args[4].clone());
}
}
VectorOpType::KNN => {
if args.len() >= 3 {
params.insert("vector_name".to_string(), args[0].clone());
params.insert("reference".to_string(), args[1].clone());
params.insert("k".to_string(), args[2].clone());
}
if args.len() >= 4 {
params.insert("metric".to_string(), args[3].clone());
}
if args.len() >= 5 {
params.insert("vector_type".to_string(), args[4].clone());
}
}
VectorOpType::Normalize => {
if !args.is_empty() {
params.insert("vector".to_string(), args[0].clone());
}
}
VectorOpType::SimilaritySearch => {
if args.len() >= 2 {
params.insert("vector_name".to_string(), args[0].clone());
params.insert("reference".to_string(), args[1].clone());
}
if args.len() >= 3 {
params.insert("metric".to_string(), args[2].clone());
}
if args.len() >= 4 {
params.insert("threshold".to_string(), args[3].clone());
}
}
}
params.insert("_full_expression".to_string(), expr);
Ok(ExecutionPlan::VectorOperation {
op_type,
params,
input: Some(Box::new(input)),
})
}
fn try_create_knn_plan(
&self,
order_by: &[OrderByItem],
limit: Option<u64>,
input: ExecutionPlan,
) -> Result<Option<ExecutionPlan>> {
if order_by.len() != 1 {
return Ok(None);
}
let order_expr = self.expression_compiler.compile_expression(order_by[0].expr.clone())?;
if !self.is_vector_function(&order_expr) {
return Ok(None);
}
let (op_type, args) = match self.extract_vector_function(&order_expr) {
Some(result) => result,
None => return Ok(None),
};
if !matches!(op_type, VectorOpType::CosineSimilarity | VectorOpType::EuclideanDistance | VectorOpType::DotProduct) {
return Ok(None);
}
let mut params = HashMap::new();
if args.len() >= 2 {
params.insert("vector_name".to_string(), args[0].clone());
params.insert("reference".to_string(), args[1].clone());
}
if args.len() >= 3 {
params.insert("metric".to_string(), args[2].clone());
}
if args.len() >= 5 {
params.insert("vector_type".to_string(), args[4].clone());
}
if let Some(k) = limit {
params.insert("k".to_string(), CompiledExpression::Literal(crate::types::Value::Int(k as i64)));
}
let direction = match order_by[0].direction {
OrderDirection::Asc => "asc",
OrderDirection::Desc => "desc",
};
params.insert("direction".to_string(), CompiledExpression::Literal(crate::types::Value::String(direction.to_string())));
Ok(Some(ExecutionPlan::VectorOperation {
op_type: VectorOpType::KNN,
params,
input: Some(Box::new(input)),
}))
}
}
impl Default for SelectCompiler {
fn default() -> Self {
Self::new()
}
}