(
&self,
op: UnaryOperator,
expr: SQLExpr,
schema: &DFSchema,
planner_context: &mut PlannerContext,
)
| 25 | |
| 26 | impl<S: ContextProvider> SqlToRel<'_, S> { |
| 27 | pub(crate) fn parse_sql_unary_op( |
| 28 | &self, |
| 29 | op: UnaryOperator, |
| 30 | expr: SQLExpr, |
| 31 | schema: &DFSchema, |
| 32 | planner_context: &mut PlannerContext, |
| 33 | ) -> Result<Expr> { |
| 34 | match op { |
| 35 | UnaryOperator::Not => Ok(Expr::Not(Box::new( |
| 36 | self.sql_expr_to_logical_expr(expr, schema, planner_context)?, |
| 37 | ))), |
| 38 | UnaryOperator::Plus => { |
| 39 | let operand = |
| 40 | self.sql_expr_to_logical_expr(expr, schema, planner_context)?; |
| 41 | let field = operand.to_field(schema)?.1; |
| 42 | let data_type = field.data_type(); |
| 43 | if data_type.is_numeric() |
| 44 | || is_interval(data_type) |
| 45 | || is_timestamp(data_type) |
| 46 | { |
| 47 | Ok(operand) |
| 48 | } else { |
| 49 | let span = operand.spans().and_then(|s| s.first()); |
| 50 | let mut diagnostic = Diagnostic::new_error( |
| 51 | format!("+ cannot be used with {data_type}"), |
| 52 | span, |
| 53 | ); |
| 54 | diagnostic.add_note( |
| 55 | "+ can only be used with numbers, intervals, and timestamps", |
| 56 | None, |
| 57 | ); |
| 58 | diagnostic |
| 59 | .add_help(format!("perhaps you need to cast {operand}"), None); |
| 60 | plan_err!("Unary operator '+' only supports numeric, interval and timestamp types"; diagnostic=diagnostic) |
| 61 | } |
| 62 | } |
| 63 | UnaryOperator::Minus => { |
| 64 | match expr { |
| 65 | // Optimization: if it's a number literal, we apply the negative operator |
| 66 | // here directly to calculate the new literal. |
| 67 | SQLExpr::Value(ValueWithSpan { |
| 68 | value: Value::Number(n, _), |
| 69 | span: _, |
| 70 | }) => self.parse_sql_number(&n, true), |
| 71 | SQLExpr::Interval(interval) => { |
| 72 | self.sql_interval_to_expr(true, interval) |
| 73 | } |
| 74 | // Not a literal, apply negative operator on expression |
| 75 | _ => Ok(Expr::Negative(Box::new(self.sql_expr_to_logical_expr( |
| 76 | expr, |
| 77 | schema, |
| 78 | planner_context, |
| 79 | )?))), |
| 80 | } |
| 81 | } |
| 82 | _ => not_impl_err!("Unsupported SQL unary operator {op:?}"), |
| 83 | } |
| 84 | } |
no test coverage detected