| 1 | #include "duckdb/parser/expression/case_expression.hpp" |
| 2 | #include "duckdb/planner/expression/bound_case_expression.hpp" |
| 3 | #include "duckdb/planner/expression/bound_cast_expression.hpp" |
| 4 | #include "duckdb/planner/expression_binder.hpp" |
| 5 | |
| 6 | using namespace duckdb; |
| 7 | using namespace std; |
| 8 | |
| 9 | BindResult ExpressionBinder::BindExpression(CaseExpression &expr, idx_t depth) { |
| 10 | // first try to bind the children of the case expression |
| 11 | string error; |
| 12 | BindChild(expr.check, depth, error); |
| 13 | BindChild(expr.result_if_true, depth, error); |
| 14 | BindChild(expr.result_if_false, depth, error); |
| 15 | if (!error.empty()) { |
| 16 | return BindResult(error); |
| 17 | } |
| 18 | // the children have been successfully resolved |
| 19 | // now resolve the type of the result |
| 20 | auto &check = (BoundExpression &)*expr.check; |
| 21 | auto &result_if_true = (BoundExpression &)*expr.result_if_true; |
| 22 | auto &result_if_false = (BoundExpression &)*expr.result_if_false; |
| 23 | // add a cast to BOOLEAN in the CHECK condition |
| 24 | check.expr = BoundCastExpression::AddCastToType(move(check.expr), check.sql_type, SQLType(SQLTypeId::BOOLEAN)); |
| 25 | // now obtain the result type of the input types |
| 26 | auto return_type = MaxSQLType(result_if_true.sql_type, result_if_false.sql_type); |
| 27 | // add casts (if necessary) |
| 28 | result_if_true.expr = |
| 29 | BoundCastExpression::AddCastToType(move(result_if_true.expr), result_if_true.sql_type, return_type); |
| 30 | result_if_false.expr = |
| 31 | BoundCastExpression::AddCastToType(move(result_if_false.expr), result_if_false.sql_type, return_type); |
| 32 | // now create the bound case expression |
| 33 | return BindResult( |
| 34 | make_unique<BoundCaseExpression>(move(check.expr), move(result_if_true.expr), move(result_if_false.expr)), |
| 35 | return_type); |
| 36 | } |
| 37 | |