use crate::ast::*;
use crate::error::{HyperQLError, Result};
use crate::validator::{
ValidationConfig, ValidationResult, ValidationError, ValidationWarning,
ValidationErrorKind, ValidationWarningKind,
};
pub struct SemanticValidator {
config: ValidationConfig,
}
impl SemanticValidator {
pub fn new(config: &ValidationConfig) -> Self {
Self {
config: config.clone(),
}
}
pub fn update_config(&mut self, config: &ValidationConfig) {
self.config = config.clone();
}
pub fn validate(&self, statement: &Statement, result: &mut ValidationResult) -> Result<()> {
result.metadata.phases_completed.push("semantic".to_string());
match statement {
Statement::Select(select) => self.validate_select(select, result),
Statement::Insert(insert) => self.validate_insert(insert, result),
Statement::Update(update) => self.validate_update(update, result),
Statement::Delete(delete) => self.validate_delete(delete, result),
Statement::Schema(_schema_op) => {
Ok(())
},
Statement::Stream(_stream_op) => {
Ok(())
},
}
}
fn validate_select(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
if let Some(from_clause) = &select.from {
self.collect_table_references(from_clause, result);
}
self.validate_select_structure(select, result)?;
self.validate_aggregation_usage(select, result)?;
self.validate_group_having_relationship(select, result)?;
self.validate_order_by_references(select, result)?;
self.validate_limit_offset_usage(select, result)?;
if let Some(traverse) = &select.traverse_clause {
self.validate_traverse_clause(traverse, result)?;
}
Ok(())
}
fn validate_select_structure(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
if select.from.is_none() && !self.is_constant_query(select) {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::UnusualPattern,
"SELECT without FROM clause - query will return single row".to_string(),
)
.with_suggestion("Consider adding FROM clause if you need to query data".to_string())
);
}
Ok(())
}
fn validate_aggregation_usage(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
let _select_aggregates = self.find_aggregates_in_projections(&select.select_list);
let where_aggregates = self.find_aggregates_in_expression(select.where_clause.as_ref());
let having_aggregates = self.find_aggregates_in_expression(select.having.as_ref());
if !where_aggregates.is_empty() {
result.add_error(
ValidationError::new(
ValidationErrorKind::AggregationError,
"Aggregate functions are not allowed in WHERE clause".to_string(),
)
.with_suggestions(vec![
"Use HAVING clause for aggregate conditions".to_string(),
"Move aggregate conditions from WHERE to HAVING".to_string(),
])
);
}
if !having_aggregates.is_empty() && select.group_by.is_empty() && !self.is_fully_aggregated_select(select) {
result.add_error(
ValidationError::new(
ValidationErrorKind::AggregationError,
"HAVING clause requires GROUP BY clause or fully aggregated SELECT".to_string(),
)
.with_suggestions(vec![
"Add GROUP BY clause".to_string(),
"Remove HAVING clause".to_string(),
"Make all SELECT expressions aggregated".to_string(),
])
);
}
if let Err(nested_error) = self.validate_no_nested_aggregates(&select.select_list) {
result.add_error(
ValidationError::new(
ValidationErrorKind::AggregationError,
nested_error.to_string(),
)
.with_suggestions(vec![
"Remove nested aggregate functions".to_string(),
"Use subqueries for complex aggregations".to_string(),
])
);
}
Ok(())
}
fn validate_group_having_relationship(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
if select.group_by.is_empty() {
return Ok(());
}
for projection in &select.select_list {
match projection {
SelectItem::Expression { expr, .. } => {
if !self.is_aggregate_or_constant(expr) && !self.is_in_group_by(expr, &select.group_by) {
result.add_error(
ValidationError::new(
ValidationErrorKind::AggregationError,
"Non-aggregated column in SELECT must appear in GROUP BY clause".to_string(),
)
.with_suggestions(vec![
"Add column to GROUP BY clause".to_string(),
"Use an aggregate function on the column".to_string(),
])
);
}
}
SelectItem::Wildcard => {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::UnusualPattern,
"Wildcard projection with GROUP BY may not work as expected".to_string(),
)
.with_suggestion("Use explicit column selection with GROUP BY".to_string())
);
}
}
}
Ok(())
}
fn validate_order_by_references(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
for order_item in &select.order_by {
if let Err(_) = self.validate_order_by_expression(&order_item.expr, select) {
result.add_error(
ValidationError::new(
ValidationErrorKind::ExpressionError,
"Invalid expression in ORDER BY clause".to_string(),
)
.with_suggestions(vec![
"Use column names or aliases from SELECT clause".to_string(),
"Ensure ORDER BY expression is valid".to_string(),
])
);
}
}
Ok(())
}
fn validate_limit_offset_usage(&self, select: &SelectStatement, result: &mut ValidationResult) -> Result<()> {
if select.limit.is_none() && select.offset.is_some() {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::UnusualPattern,
"OFFSET without LIMIT may have unexpected behavior".to_string(),
)
.with_suggestion("Consider adding LIMIT clause with OFFSET".to_string())
);
}
if let Some(limit_value) = select.limit {
if limit_value == 0 {
result.add_error(
ValidationError::new(
ValidationErrorKind::ExpressionError,
"LIMIT value must be positive".to_string(),
)
);
}
}
Ok(())
}
fn validate_traverse_clause(&self, _traverse: &TraverseClause, result: &mut ValidationResult) -> Result<()> {
result.metadata.phases_completed.push("traverse_validation".to_string());
Ok(())
}
fn validate_insert(&self, insert: &InsertStatement, result: &mut ValidationResult) -> Result<()> {
result.metadata.tables_referenced.push(insert.table.clone());
if !insert.columns.is_empty() {
for (row_idx, value_row) in insert.values.iter().enumerate() {
if insert.columns.len() != value_row.len() {
result.add_error(
ValidationError::new(
ValidationErrorKind::StructuralError,
format!(
"Row {}: Number of columns ({}) does not match number of values ({})",
row_idx + 1,
insert.columns.len(),
value_row.len()
),
)
.with_suggestions(vec![
"Ensure each row has the same number of values as columns".to_string(),
"Check for missing or extra values in INSERT statement".to_string(),
])
);
}
}
}
Ok(())
}
fn validate_update(&self, update: &UpdateStatement, result: &mut ValidationResult) -> Result<()> {
result.metadata.tables_referenced.push(update.table.clone());
for assignment in &update.assignments {
if self.is_system_column(&assignment.column) {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::SafetyWarning,
format!("Updating system column '{}' may have unexpected effects", assignment.column),
)
.with_suggestion("Avoid updating system-managed columns".to_string())
);
}
}
if update.where_clause.is_none() {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::SafetyWarning,
"UPDATE without WHERE clause will update all rows".to_string(),
)
.with_suggestion("Add WHERE clause to limit updates".to_string())
);
}
Ok(())
}
fn validate_delete(&self, delete: &DeleteStatement, result: &mut ValidationResult) -> Result<()> {
result.metadata.tables_referenced.push(delete.table.clone());
if delete.where_clause.is_none() {
result.add_warning(
ValidationWarning::new(
ValidationWarningKind::SafetyWarning,
"DELETE without WHERE clause will delete all rows".to_string(),
)
.with_suggestion("Add WHERE clause to limit deletions".to_string())
);
}
Ok(())
}
fn is_constant_query(&self, select: &SelectStatement) -> bool {
select.from.is_none() &&
select.select_list.iter().all(|proj| {
match proj {
SelectItem::Expression { expr, .. } => self.is_constant_expression(expr),
SelectItem::Wildcard => false,
}
})
}
fn is_constant_expression(&self, expr: &Expression) -> bool {
match expr {
Expression::Literal(_) => true,
Expression::Function { name, args } => {
match name.to_uppercase().as_str() {
"NOW" | "CURRENT_TIMESTAMP" | "PI" => args.is_empty(),
_ => args.iter().all(|arg| self.is_constant_expression(arg)),
}
}
Expression::Binary { left, right, .. } => {
self.is_constant_expression(left) && self.is_constant_expression(right)
}
Expression::Unary { expr, .. } => self.is_constant_expression(expr),
_ => false,
}
}
#[allow(dead_code)]
fn has_wildcard_projection(&self, select: &SelectStatement) -> bool {
select.select_list.iter().any(|proj| matches!(proj, SelectItem::Wildcard))
}
fn find_aggregates_in_projections(&self, projections: &[SelectItem]) -> Vec<String> {
let mut aggregates = Vec::new();
for projection in projections {
match projection {
SelectItem::Expression { expr, .. } => {
aggregates.extend(self.find_aggregates_in_expression(Some(expr)));
}
SelectItem::Wildcard => {}
}
}
aggregates
}
fn find_aggregates_in_expression(&self, expr: Option<&Expression>) -> Vec<String> {
let mut aggregates = Vec::new();
if let Some(expression) = expr {
self.collect_aggregates(expression, &mut aggregates);
}
aggregates
}
fn collect_aggregates(&self, expr: &Expression, aggregates: &mut Vec<String>) {
match expr {
Expression::Function { name, args } => {
if self.is_aggregate_function(name) {
aggregates.push(name.clone());
}
for arg in args {
self.collect_aggregates(arg, aggregates);
}
}
Expression::Binary { left, right, .. } => {
self.collect_aggregates(left, aggregates);
self.collect_aggregates(right, aggregates);
}
Expression::Unary { expr, .. } => {
self.collect_aggregates(expr, aggregates);
}
_ => {}
}
}
fn is_aggregate_function(&self, name: &str) -> bool {
matches!(name.to_uppercase().as_str(),
"COUNT" | "SUM" | "AVG" | "MIN" | "MAX" |
"STDDEV" | "VARIANCE" | "GROUP_CONCAT" | "STRING_AGG"
)
}
fn is_fully_aggregated_select(&self, select: &SelectStatement) -> bool {
select.select_list.iter().all(|proj| {
match proj {
SelectItem::Expression { expr, .. } => self.is_aggregate_or_constant(expr),
SelectItem::Wildcard => false,
}
})
}
fn is_aggregate_or_constant(&self, expr: &Expression) -> bool {
match expr {
Expression::Literal(_) => true,
Expression::Function { name, args } => {
if self.is_aggregate_function(name) {
true
} else if self.is_constant_function(name) {
args.iter().all(|arg| self.is_constant_expression(arg))
} else {
false
}
}
Expression::Binary { left, right, .. } => {
self.is_aggregate_or_constant(left) && self.is_aggregate_or_constant(right)
}
Expression::Unary { expr, .. } => self.is_aggregate_or_constant(expr),
_ => false,
}
}
fn is_constant_function(&self, name: &str) -> bool {
matches!(name.to_uppercase().as_str(),
"NOW" | "CURRENT_TIMESTAMP" | "PI" | "RAND" | "UUID"
)
}
fn validate_no_nested_aggregates(&self, projections: &[SelectItem]) -> Result<()> {
for projection in projections {
if let SelectItem::Expression { expr, .. } = projection {
self.check_nested_aggregates(expr, false)?;
}
}
Ok(())
}
fn check_nested_aggregates(&self, expr: &Expression, in_aggregate: bool) -> Result<()> {
match expr {
Expression::Function { name, args } => {
let is_agg = self.is_aggregate_function(name);
if is_agg && in_aggregate {
return Err(HyperQLError::ValidationError {
message: format!("Nested aggregate function '{}' is not allowed", name),
field: None,
});
}
for arg in args {
self.check_nested_aggregates(arg, is_agg || in_aggregate)?;
}
}
Expression::Binary { left, right, .. } => {
self.check_nested_aggregates(left, in_aggregate)?;
self.check_nested_aggregates(right, in_aggregate)?;
}
Expression::Unary { expr, .. } => {
self.check_nested_aggregates(expr, in_aggregate)?;
}
_ => {}
}
Ok(())
}
fn is_in_group_by(&self, expr: &Expression, group_by: &[Expression]) -> bool {
group_by.iter().any(|gb_expr| self.expressions_equivalent(expr, gb_expr))
}
fn expressions_equivalent(&self, expr1: &Expression, expr2: &Expression) -> bool {
match (expr1, expr2) {
(Expression::Column(col1), Expression::Column(col2)) => {
col1.name == col2.name && col1.table == col2.table
}
(Expression::Literal(lit1), Expression::Literal(lit2)) => lit1 == lit2,
_ => false,
}
}
fn validate_order_by_expression(&self, _expr: &Expression, _select: &SelectStatement) -> Result<()> {
Ok(())
}
fn is_system_column(&self, column: &str) -> bool {
matches!(column.to_lowercase().as_str(),
"id" | "created_at" | "updated_at" | "version" | "rowid"
)
}
fn collect_table_references(&self, from_clause: &FromClause, result: &mut ValidationResult) {
match from_clause {
FromClause::Table { collection, entity_type, .. } => {
let table_ref = match entity_type {
Some(et) => format!("{}.{}", collection, et),
None => collection.clone(),
};
result.metadata.tables_referenced.push(table_ref);
}
FromClause::Subquery { .. } => {
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::*;
#[test]
fn test_aggregation_validation() {
let config = ValidationConfig::default();
let validator = SemanticValidator::new(&config);
let mut result = ValidationResult::new();
let select = SelectStatement {
select_list: vec![SelectItem::Expression {
expr: Expression::Column(ColumnRef {
table: None,
name: "name".to_string(),
}),
alias: None,
}],
from: Some(FromClause::Table {
collection: "users".to_string(),
entity_type: None,
alias: None,
}),
joins: Vec::new(),
where_clause: None,
group_by: vec![], having: Some(Expression::Function {
name: "COUNT".to_string(),
args: vec![Expression::Literal(Literal::Int(1))],
}),
order_by: vec![],
limit: Some(100),
offset: None,
distinct: false,
traverse_clause: None,
};
validator.validate_select(&select, &mut result).unwrap();
assert!(!result.errors.is_empty());
assert!(result.errors.iter().any(|e| matches!(e.kind, ValidationErrorKind::AggregationError)));
}
#[test]
fn test_constant_query_detection() {
let config = ValidationConfig::default();
let validator = SemanticValidator::new(&config);
let select = SelectStatement {
select_list: vec![SelectItem::Expression {
expr: Expression::Literal(Literal::String("Hello".to_string())),
alias: None,
}],
from: None,
joins: Vec::new(),
where_clause: None,
group_by: vec![],
having: None,
order_by: vec![],
limit: None,
offset: None,
distinct: false,
traverse_clause: None,
};
assert!(validator.is_constant_query(&select));
}
#[test]
fn test_update_without_where_warning() {
let config = ValidationConfig::default();
let validator = SemanticValidator::new(&config);
let mut result = ValidationResult::new();
let update = UpdateStatement {
table: "users".to_string(),
assignments: vec![Assignment {
column: "name".to_string(),
value: Expression::Literal(Literal::String("John".to_string())),
}],
where_clause: None,
};
validator.validate_update(&update, &mut result).unwrap();
assert!(!result.warnings.is_empty());
assert!(result.warnings.iter().any(|w| matches!(w.kind, ValidationWarningKind::SafetyWarning)));
}
#[test]
fn test_limit_offset_validation() {
let config = ValidationConfig::default();
let validator = SemanticValidator::new(&config);
let mut result = ValidationResult::new();
let select = SelectStatement {
select_list: vec![SelectItem::Wildcard],
from: Some(FromClause::Table {
collection: "users".to_string(),
entity_type: None,
alias: None,
}),
joins: Vec::new(),
where_clause: None,
group_by: vec![],
having: None,
order_by: vec![],
limit: None,
offset: Some(10), distinct: false,
traverse_clause: None,
};
validator.validate_select(&select, &mut result).unwrap();
assert!(!result.warnings.is_empty());
assert!(result.warnings.iter().any(|w| matches!(w.kind, ValidationWarningKind::UnusualPattern)));
}
}