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 | namespace duckdb { |
7 | |
8 | BindResult ExpressionBinder::BindExpression(CaseExpression &expr, idx_t depth) { |
9 | // first try to bind the children of the case expression |
10 | string error; |
11 | for (auto &check : expr.case_checks) { |
12 | BindChild(expr&: check.when_expr, depth, error); |
13 | BindChild(expr&: check.then_expr, depth, error); |
14 | } |
15 | BindChild(expr&: expr.else_expr, depth, error); |
16 | if (!error.empty()) { |
17 | return BindResult(error); |
18 | } |
19 | // the children have been successfully resolved |
20 | // figure out the result type of the CASE expression |
21 | auto &else_expr = BoundExpression::GetExpression(expr&: *expr.else_expr); |
22 | auto return_type = else_expr->return_type; |
23 | for (auto &check : expr.case_checks) { |
24 | auto &then_expr = BoundExpression::GetExpression(expr&: *check.then_expr); |
25 | return_type = LogicalType::MaxLogicalType(left: return_type, right: then_expr->return_type); |
26 | } |
27 | |
28 | // bind all the individual components of the CASE statement |
29 | auto result = make_uniq<BoundCaseExpression>(args&: return_type); |
30 | for (idx_t i = 0; i < expr.case_checks.size(); i++) { |
31 | auto &check = expr.case_checks[i]; |
32 | auto &when_expr = BoundExpression::GetExpression(expr&: *check.when_expr); |
33 | auto &then_expr = BoundExpression::GetExpression(expr&: *check.then_expr); |
34 | BoundCaseCheck result_check; |
35 | result_check.when_expr = |
36 | BoundCastExpression::AddCastToType(context, expr: std::move(when_expr), target_type: LogicalType::BOOLEAN); |
37 | result_check.then_expr = BoundCastExpression::AddCastToType(context, expr: std::move(then_expr), target_type: return_type); |
38 | result->case_checks.push_back(x: std::move(result_check)); |
39 | } |
40 | result->else_expr = BoundCastExpression::AddCastToType(context, expr: std::move(else_expr), target_type: return_type); |
41 | return BindResult(std::move(result)); |
42 | } |
43 | } // namespace duckdb |
44 | |