(
context: &mut ParserContext,
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 983 | } |
| 984 | |
| 985 | fn parse_where_statement( |
| 986 | context: &mut ParserContext, |
| 987 | env: &mut Environment, |
| 988 | tokens: &[Token], |
| 989 | position: &mut usize, |
| 990 | ) -> Result<Statement, Box<Diagnostic>> { |
| 991 | *position += 1; |
| 992 | if *position >= tokens.len() { |
| 993 | return Err(Diagnostic::error("Expect expression after `WHERE` keyword") |
| 994 | .add_help("Try to add boolean expression after `WHERE` keyword") |
| 995 | .add_note("`WHERE` statement expects expression as condition") |
| 996 | .with_location(calculate_safe_location(tokens, *position - 1)) |
| 997 | .as_boxed()); |
| 998 | } |
| 999 | |
| 1000 | let aggregations_count_before = context.aggregations.len(); |
| 1001 | |
| 1002 | // Make sure WHERE condition expression has boolean type or can implicit casted to boolean |
| 1003 | let condition_location = tokens[*position].location; |
| 1004 | let mut condition = parse_expression(context, env, tokens, position)?; |
| 1005 | |
| 1006 | // Make sure that the condition type is boolean, or can implicit cast to boolean. |
| 1007 | if !condition.expr_type().is_bool() { |
| 1008 | let expected_type: Box<dyn DataType> = Box::new(BoolType); |
| 1009 | if !expected_type.has_implicit_cast_from(&condition) { |
| 1010 | return Err(Diagnostic::error(&format!( |
| 1011 | "Expect `WHERE` condition to be type {} but got {}", |
| 1012 | "Boolean", |
| 1013 | condition.expr_type().literal() |
| 1014 | )) |
| 1015 | .add_note("`WHERE` statement condition must be Boolean") |
| 1016 | .with_location(condition_location) |
| 1017 | .as_boxed()); |
| 1018 | } |
| 1019 | |
| 1020 | // Implicit cast the condition to boolean |
| 1021 | condition = Box::new(CastExpr { |
| 1022 | value: condition, |
| 1023 | result_type: expected_type.clone(), |
| 1024 | }) |
| 1025 | } |
| 1026 | |
| 1027 | let aggregations_count_after = context.aggregations.len(); |
| 1028 | if aggregations_count_before != aggregations_count_after { |
| 1029 | return Err( |
| 1030 | Diagnostic::error("Can't use Aggregation functions in `WHERE` statement") |
| 1031 | .add_note("Aggregation functions must be used after `GROUP BY` statement") |
| 1032 | .add_note("Aggregation functions evaluated after later after `GROUP BY` statement") |
| 1033 | .with_location(condition_location) |
| 1034 | .as_boxed(), |
| 1035 | ); |
| 1036 | } |
| 1037 | |
| 1038 | Ok(Statement::Where(WhereStatement { condition })) |
| 1039 | } |
| 1040 | |
| 1041 | fn parse_group_by_statement( |
| 1042 | context: &mut ParserContext, |
no test coverage detected
searching dependent graphs…