1#include "duckdb/parser/expression/cast_expression.hpp"
2#include "duckdb/planner/expression/bound_cast_expression.hpp"
3#include "duckdb/planner/expression/bound_parameter_expression.hpp"
4#include "duckdb/planner/expression_binder.hpp"
5#include "duckdb/planner/binder.hpp"
6
7namespace duckdb {
8
9BindResult ExpressionBinder::BindExpression(CastExpression &expr, idx_t depth) {
10 // first try to bind the child of the cast expression
11 string error = Bind(expr&: expr.child, depth);
12 if (!error.empty()) {
13 return BindResult(error);
14 }
15 // FIXME: We can also implement 'hello'::schema.custom_type; and pass by the schema down here.
16 // Right now just considering its DEFAULT_SCHEMA always
17 Binder::BindLogicalType(context, type&: expr.cast_type);
18 // the children have been successfully resolved
19 auto &child = BoundExpression::GetExpression(expr&: *expr.child);
20 if (expr.try_cast) {
21 if (child->return_type == expr.cast_type) {
22 // no cast required: type matches
23 return BindResult(std::move(child));
24 }
25 child = BoundCastExpression::AddCastToType(context, expr: std::move(child), target_type: expr.cast_type, try_cast: true);
26 } else {
27 // otherwise add a cast to the target type
28 child = BoundCastExpression::AddCastToType(context, expr: std::move(child), target_type: expr.cast_type);
29 }
30 return BindResult(std::move(child));
31}
32} // namespace duckdb
33