ROADMAP v0.5.0 - Aggregate expression evaluation helper
(
&self,
expr: &Expression,
group_rows: &[&Row],
context: &ExecutionContext,
)
| 1834 | /// Evaluate an aggregate expression over a group of rows |
| 1835 | #[allow(dead_code)] // ROADMAP v0.5.0 - Aggregate expression evaluation helper |
| 1836 | fn evaluate_aggregate_expression( |
| 1837 | &self, |
| 1838 | expr: &Expression, |
| 1839 | group_rows: &[&Row], |
| 1840 | context: &ExecutionContext, |
| 1841 | ) -> Result<Value, ExecutionError> { |
| 1842 | match expr { |
| 1843 | Expression::FunctionCall(func_call) => { |
| 1844 | log::debug!("Evaluating aggregate function: '{}'", func_call.name); |
| 1845 | match func_call.name.to_lowercase().as_str() { |
| 1846 | "count" => { |
| 1847 | // Count non-null values |
| 1848 | if let Some(arg) = func_call.arguments.first() { |
| 1849 | let mut count = 0; |
| 1850 | for row in group_rows { |
| 1851 | let value = self.evaluate_expression_in_row(arg, row, context)?; |
| 1852 | if !matches!(value, Value::Null) { |
| 1853 | count += 1; |
| 1854 | } |
| 1855 | } |
| 1856 | Ok(Value::Number(count as f64)) |
| 1857 | } else { |
| 1858 | // COUNT(*) - count all rows |
| 1859 | Ok(Value::Number(group_rows.len() as f64)) |
| 1860 | } |
| 1861 | } |
| 1862 | "avg" => { |
| 1863 | // Compute average of non-null numeric values |
| 1864 | if let Some(arg) = func_call.arguments.first() { |
| 1865 | let mut sum = 0.0; |
| 1866 | let mut count = 0; |
| 1867 | |
| 1868 | for row in group_rows { |
| 1869 | let value = self.evaluate_expression_in_row(arg, row, context)?; |
| 1870 | match value { |
| 1871 | Value::Number(n) => { |
| 1872 | sum += n; |
| 1873 | count += 1; |
| 1874 | } |
| 1875 | Value::Null => { |
| 1876 | // Skip null values in average computation |
| 1877 | } |
| 1878 | _ => { |
| 1879 | return Err(ExecutionError::ExpressionError(format!( |
| 1880 | "Cannot compute average of non-numeric value: {:?}", |
| 1881 | value |
| 1882 | ))); |
| 1883 | } |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | if count > 0 { |
| 1888 | Ok(Value::Number(sum / count as f64)) |
| 1889 | } else { |
| 1890 | Ok(Value::Null) // Average of no values is NULL |
| 1891 | } |
| 1892 | } else { |
| 1893 | Err(ExecutionError::ExpressionError( |
nothing calls this directly
no test coverage detected