Evaluate an expression within a specific row context
(
&self,
expr: &Expression,
row: &Row,
context: &ExecutionContext,
)
| 2526 | |
| 2527 | /// Evaluate an expression within a specific row context |
| 2528 | fn evaluate_expression_in_row( |
| 2529 | &self, |
| 2530 | expr: &Expression, |
| 2531 | row: &Row, |
| 2532 | context: &ExecutionContext, |
| 2533 | ) -> Result<Value, ExecutionError> { |
| 2534 | log::debug!("Evaluating expression in row context: {:?}", expr); |
| 2535 | |
| 2536 | match expr { |
| 2537 | Expression::Variable(var) => { |
| 2538 | // First check the row values, then context variables |
| 2539 | if let Some(value) = row.values.get(&var.name) { |
| 2540 | Ok(value.clone()) |
| 2541 | } else if let Some(value) = context.variables.get(&var.name) { |
| 2542 | Ok(value.clone()) |
| 2543 | } else { |
| 2544 | Err(ExecutionError::ExpressionError(format!( |
| 2545 | "Variable not found: {}", |
| 2546 | var.name |
| 2547 | ))) |
| 2548 | } |
| 2549 | } |
| 2550 | Expression::FunctionCall(func_call) => { |
| 2551 | // Handle both aggregation and non-aggregation functions |
| 2552 | log::debug!("Evaluating function call: '{}'", func_call.name); |
| 2553 | |
| 2554 | // For aggregation functions, use simplified evaluation |
| 2555 | match func_call.name.to_lowercase().as_str() { |
| 2556 | "count" => { |
| 2557 | // For count, we need to count the number of items |
| 2558 | // This is a simplification - proper count should work with the aggregation context |
| 2559 | Ok(Value::Number(1.0)) // For now, return 1 as placeholder |
| 2560 | } |
| 2561 | "avg" | "sum" | "min" | "max" => { |
| 2562 | // These need proper aggregation implementation |
| 2563 | // For SUM of empty result sets, return NULL instead of 0.0 |
| 2564 | let func_name = func_call.name.to_lowercase(); |
| 2565 | if func_name == "sum" { |
| 2566 | log::debug!("DEBUG: evaluate_expression_in_row - SUM returning NULL"); |
| 2567 | Ok(Value::Null) |
| 2568 | } else if func_name == "avg" { |
| 2569 | log::debug!("DEBUG: evaluate_expression_in_row - AVG returning 0.0 placeholder - THIS IS THE PROBLEM!"); |
| 2570 | Ok(Value::Number(0.0)) |
| 2571 | } else { |
| 2572 | // For other aggregates, return a placeholder |
| 2573 | log::debug!( |
| 2574 | "DEBUG: evaluate_expression_in_row - {} returning 0.0 placeholder", |
| 2575 | func_name |
| 2576 | ); |
| 2577 | Ok(Value::Number(0.0)) |
| 2578 | } |
| 2579 | } |
| 2580 | _ => { |
| 2581 | // For non-aggregation functions, use the regular function evaluation |
| 2582 | log::debug!( |
| 2583 | "Delegating to regular function evaluation for: {}", |
| 2584 | func_call.name |
| 2585 | ); |
no test coverage detected