use crate::compiler::{CompiledExpression, JoinType};
use crate::types::{ResultRow, Value};
use crate::error::Result;
use super::expression_eval::ExpressionEvaluator;
use std::collections::HashMap;
pub struct JoinExecutor;
impl JoinExecutor {
pub fn execute_join(
left_rows: Vec<ResultRow>,
right_rows: Vec<ResultRow>,
join_type: &JoinType,
on_condition: &CompiledExpression,
evaluator: &ExpressionEvaluator,
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> Result<Vec<ResultRow>> {
if Self::is_equality_join(on_condition) {
Self::hash_join(left_rows, right_rows, join_type, on_condition, evaluator, left_alias, right_alias)
} else {
Self::nested_loop_join(left_rows, right_rows, join_type, on_condition, evaluator, left_alias, right_alias)
}
}
fn is_equality_join(condition: &CompiledExpression) -> bool {
match condition {
CompiledExpression::Binary { op, .. } => {
matches!(op, crate::ast::BinaryOperator::Equal)
}
_ => false,
}
}
fn hash_join(
left_rows: Vec<ResultRow>,
right_rows: Vec<ResultRow>,
join_type: &JoinType,
on_condition: &CompiledExpression,
_evaluator: &ExpressionEvaluator,
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> Result<Vec<ResultRow>> {
let (left_key, right_key) = Self::extract_join_keys(on_condition)?;
let mut hash_table: HashMap<String, Vec<ResultRow>> = HashMap::new();
for right_row in &right_rows {
if let Some(key_value) = Self::lookup_column_flexible(right_row, &right_key) {
let key_str = Self::value_to_key_string(key_value);
hash_table.entry(key_str).or_insert_with(Vec::new).push(right_row.clone());
}
}
let mut matched_right_indices: std::collections::HashSet<usize> = std::collections::HashSet::new();
let right_rows_indexed: Vec<_> = right_rows.iter().enumerate().collect();
let mut result = Vec::new();
for left_row in &left_rows {
if let Some(key_value) = Self::lookup_column_flexible(left_row, &left_key) {
let key_str = Self::value_to_key_string(key_value);
if let Some(matching_right_rows) = hash_table.get(&key_str) {
for right_row in matching_right_rows {
if let Some((idx, _)) = right_rows_indexed.iter().find(|(_, r)| *r == right_row) {
matched_right_indices.insert(*idx);
}
let joined_row = Self::combine_rows(left_row, right_row, left_alias, right_alias);
result.push(joined_row);
}
} else {
match join_type {
JoinType::Left | JoinType::FullOuter => {
let joined_row = Self::combine_rows_with_null_right(left_row, &right_rows, left_alias, right_alias);
result.push(joined_row);
}
_ => {
}
}
}
} else {
match join_type {
JoinType::Left | JoinType::FullOuter => {
let joined_row = Self::combine_rows_with_null_right(left_row, &right_rows, left_alias, right_alias);
result.push(joined_row);
}
_ => {
}
}
}
}
match join_type {
JoinType::Right | JoinType::FullOuter => {
for (idx, right_row) in right_rows_indexed {
if !matched_right_indices.contains(&idx) {
let joined_row = Self::combine_rows_with_null_left(&left_rows, right_row, left_alias, right_alias);
result.push(joined_row);
}
}
}
_ => {
}
}
Ok(result)
}
fn nested_loop_join(
left_rows: Vec<ResultRow>,
right_rows: Vec<ResultRow>,
join_type: &JoinType,
on_condition: &CompiledExpression,
evaluator: &ExpressionEvaluator,
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> Result<Vec<ResultRow>> {
let mut result = Vec::new();
let mut matched_right_indices: std::collections::HashSet<usize> = std::collections::HashSet::new();
for left_row in &left_rows {
let mut found_match = false;
for (right_idx, right_row) in right_rows.iter().enumerate() {
let combined_row = Self::combine_rows(left_row, right_row, left_alias, right_alias);
if evaluator.evaluate_predicate(on_condition, &combined_row)? {
found_match = true;
matched_right_indices.insert(right_idx);
result.push(combined_row);
}
}
if !found_match {
match join_type {
JoinType::Left | JoinType::FullOuter => {
let joined_row = Self::combine_rows_with_null_right(left_row, &right_rows, left_alias, right_alias);
result.push(joined_row);
}
_ => {
}
}
}
}
match join_type {
JoinType::Right | JoinType::FullOuter => {
for (right_idx, right_row) in right_rows.iter().enumerate() {
if !matched_right_indices.contains(&right_idx) {
let joined_row = Self::combine_rows_with_null_left(&left_rows, right_row, left_alias, right_alias);
result.push(joined_row);
}
}
}
_ => {
}
}
Ok(result)
}
fn extract_join_keys(condition: &CompiledExpression) -> Result<(String, String)> {
match condition {
CompiledExpression::Binary { left, op, right, .. } => {
if !matches!(op, crate::ast::BinaryOperator::Equal) {
return Err(crate::error::HyperQLError::SemanticError {
message: "Hash join requires equality predicate".to_string(),
context: vec!["join".to_string()],
});
}
let left_key = Self::extract_column_name(left)?;
let right_key = Self::extract_column_name(right)?;
Ok((left_key, right_key))
}
_ => Err(crate::error::HyperQLError::SemanticError {
message: "Invalid join condition format".to_string(),
context: vec!["join".to_string()],
}),
}
}
fn extract_column_name(expr: &CompiledExpression) -> Result<String> {
match expr {
CompiledExpression::Column { name, table, .. } => {
if let Some(table_name) = table {
Ok(format!("{}_{}", table_name, name))
} else {
Ok(name.clone())
}
}
_ => Err(crate::error::HyperQLError::SemanticError {
message: "Join key must be a column reference".to_string(),
context: vec!["join".to_string()],
}),
}
}
fn lookup_column_flexible<'a>(row: &'a ResultRow, key: &str) -> Option<&'a Value> {
if let Some(value) = row.columns.get(key) {
return Some(value);
}
if key.contains('_') {
if let Some(pos) = key.find('_') {
let bare_name = &key[pos + 1..];
if let Some(value) = row.columns.get(bare_name) {
return Some(value);
}
}
}
None
}
fn value_to_key_string(value: &Value) -> String {
match value {
Value::Null => "NULL".to_string(),
Value::Bool(b) => b.to_string(),
Value::Int(i) => i.to_string(),
Value::Float(f) => f.to_string(),
Value::String(s) => s.clone(),
Value::EntityId(id) => id.0.clone(),
Value::Position(pos) => format!("({},{},{})", pos.x, pos.y, pos.z),
Value::Distance(d) => d.0.to_string(),
Value::Timestamp(ts) => ts.to_string(),
Value::Duration(dur) => dur.to_string(),
_ => format!("{:?}", value),
}
}
fn combine_rows(
left: &ResultRow,
right: &ResultRow,
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> ResultRow {
let mut columns = HashMap::new();
for (key, value) in &left.columns {
let prefixed_key = if let Some(alias) = left_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
columns.insert(prefixed_key, value.clone());
}
for (key, value) in &right.columns {
let prefixed_key = if let Some(alias) = right_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
columns.insert(prefixed_key, value.clone());
}
ResultRow { columns }
}
fn combine_rows_with_null_right(
left: &ResultRow,
right_sample: &[ResultRow],
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> ResultRow {
let mut columns = HashMap::new();
for (key, value) in &left.columns {
let prefixed_key = if let Some(alias) = left_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
columns.insert(prefixed_key, value.clone());
}
if let Some(sample_row) = right_sample.first() {
for key in sample_row.columns.keys() {
let prefixed_key = if let Some(alias) = right_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
if !columns.contains_key(&prefixed_key) {
columns.insert(prefixed_key, Value::Null);
}
}
}
ResultRow { columns }
}
fn combine_rows_with_null_left(
left_sample: &[ResultRow],
right: &ResultRow,
left_alias: &Option<String>,
right_alias: &Option<String>,
) -> ResultRow {
let mut columns = HashMap::new();
if let Some(sample_row) = left_sample.first() {
for key in sample_row.columns.keys() {
let prefixed_key = if let Some(alias) = left_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
columns.insert(prefixed_key, Value::Null);
}
}
for (key, value) in &right.columns {
let prefixed_key = if let Some(alias) = right_alias {
format!("{}_{}", alias, key)
} else {
key.clone()
};
columns.insert(prefixed_key, value.clone());
}
ResultRow { columns }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compiler::CompiledExpression;
use crate::types::{Value, ResultRow};
use std::collections::HashMap;
fn create_test_row(id: i64, name: &str) -> ResultRow {
let mut columns = HashMap::new();
columns.insert("id".to_string(), Value::Int(id));
columns.insert("name".to_string(), Value::String(name.to_string()));
ResultRow { columns }
}
fn create_test_row_with_table(table: &str, id: i64, value: &str) -> ResultRow {
let mut columns = HashMap::new();
columns.insert(format!("{}.id", table), Value::Int(id));
columns.insert(format!("{}.value", table), Value::String(value.to_string()));
ResultRow { columns }
}
#[test]
fn test_is_equality_join() {
let eq_condition = CompiledExpression::Binary {
left: Box::new(CompiledExpression::Column {
table: Some("a".to_string()),
name: "id".to_string(),
value_type: crate::compiler::ValueType::Int,
}),
op: crate::ast::BinaryOperator::Equal,
right: Box::new(CompiledExpression::Column {
table: Some("b".to_string()),
name: "id".to_string(),
value_type: crate::compiler::ValueType::Int,
}),
result_type: crate::compiler::ValueType::Bool,
};
assert!(JoinExecutor::is_equality_join(&eq_condition));
}
#[test]
fn test_extract_join_keys() {
let condition = CompiledExpression::Binary {
left: Box::new(CompiledExpression::Column {
table: Some("a".to_string()),
name: "id".to_string(),
value_type: crate::compiler::ValueType::Int,
}),
op: crate::ast::BinaryOperator::Equal,
right: Box::new(CompiledExpression::Column {
table: Some("b".to_string()),
name: "id".to_string(),
value_type: crate::compiler::ValueType::Int,
}),
result_type: crate::compiler::ValueType::Bool,
};
let (left_key, right_key) = JoinExecutor::extract_join_keys(&condition).unwrap();
assert_eq!(left_key, "a_id");
assert_eq!(right_key, "b_id");
}
#[test]
fn test_combine_rows() {
let left = create_test_row(1, "Alice");
let mut right_columns = HashMap::new();
right_columns.insert("age".to_string(), Value::Int(30));
let right = ResultRow { columns: right_columns };
let left_alias = Some("l".to_string());
let right_alias = Some("r".to_string());
let combined = JoinExecutor::combine_rows(&left, &right, &left_alias, &right_alias);
assert_eq!(combined.columns.get("l_id"), Some(&Value::Int(1)));
assert_eq!(combined.columns.get("l_name"), Some(&Value::String("Alice".to_string())));
assert_eq!(combined.columns.get("r_age"), Some(&Value::Int(30)));
}
}