(
&self,
sql: SQLExpr,
schema: &DFSchema,
planner_context: &mut PlannerContext,
)
| 69 | Ok(expr) |
| 70 | } |
| 71 | pub(crate) fn sql_expr_to_logical_expr( |
| 72 | &self, |
| 73 | sql: SQLExpr, |
| 74 | schema: &DFSchema, |
| 75 | planner_context: &mut PlannerContext, |
| 76 | ) -> Result<Expr> { |
| 77 | enum StackEntry { |
| 78 | SQLExpr(Box<SQLExpr>), |
| 79 | Operator(BinaryOperator), |
| 80 | } |
| 81 | |
| 82 | // Virtual stack machine to convert SQLExpr to Expr |
| 83 | // This allows visiting the expr tree in a depth-first manner which |
| 84 | // produces expressions in postfix notations, i.e. `a + b` => `a b +`. |
| 85 | // See https://github.com/apache/datafusion/issues/1444 |
| 86 | let mut stack = vec![StackEntry::SQLExpr(Box::new(sql))]; |
| 87 | let mut eval_stack = vec![]; |
| 88 | |
| 89 | while let Some(entry) = stack.pop() { |
| 90 | match entry { |
| 91 | StackEntry::SQLExpr(sql_expr) => { |
| 92 | match *sql_expr { |
| 93 | SQLExpr::BinaryOp { left, op, right } => { |
| 94 | // Note the order that we push the entries to the stack |
| 95 | // is important. We want to visit the left node first. |
| 96 | stack.push(StackEntry::Operator(op)); |
| 97 | stack.push(StackEntry::SQLExpr(right)); |
| 98 | stack.push(StackEntry::SQLExpr(left)); |
| 99 | } |
| 100 | _ => { |
| 101 | let expr = self.sql_expr_to_logical_expr_internal( |
| 102 | *sql_expr, |
| 103 | schema, |
| 104 | planner_context, |
| 105 | )?; |
| 106 | eval_stack.push(expr); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | StackEntry::Operator(op) => { |
| 111 | let right = eval_stack.pop().unwrap(); |
| 112 | let left = eval_stack.pop().unwrap(); |
| 113 | let expr = self.build_logical_expr(op, left, right, schema)?; |
| 114 | eval_stack.push(expr); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | assert_eq!(1, eval_stack.len()); |
| 120 | let expr = eval_stack.pop().unwrap(); |
| 121 | Ok(expr) |
| 122 | } |
| 123 | |
| 124 | fn build_logical_expr( |
| 125 | &self, |
no test coverage detected