1#include "duckdb/common/vector_operations/vector_operations.hpp"
2#include "duckdb/execution/expression_executor.hpp"
3#include "duckdb/function/scalar_function.hpp"
4#include "duckdb/planner/expression/bound_cast_expression.hpp"
5
6namespace duckdb {
7
8unique_ptr<ExpressionState> ExpressionExecutor::InitializeState(const BoundCastExpression &expr,
9 ExpressionExecutorState &root) {
10 auto result = make_uniq<ExecuteFunctionState>(args: expr, args&: root);
11 result->AddChild(expr: expr.child.get());
12 result->Finalize();
13 if (expr.bound_cast.init_local_state) {
14 CastLocalStateParameters parameters(root.executor->GetContext(), expr.bound_cast.cast_data);
15 result->local_state = expr.bound_cast.init_local_state(parameters);
16 }
17 return std::move(result);
18}
19
20void ExpressionExecutor::Execute(const BoundCastExpression &expr, ExpressionState *state, const SelectionVector *sel,
21 idx_t count, Vector &result) {
22 auto lstate = ExecuteFunctionState::GetFunctionState(state&: *state);
23
24 // resolve the child
25 state->intermediate_chunk.Reset();
26
27 auto &child = state->intermediate_chunk.data[0];
28 auto child_state = state->child_states[0].get();
29
30 Execute(expr: *expr.child, state: child_state, sel, count, result&: child);
31 if (expr.try_cast) {
32 string error_message;
33 CastParameters parameters(expr.bound_cast.cast_data.get(), false, &error_message, lstate);
34 expr.bound_cast.function(child, result, count, parameters);
35 } else {
36 // cast it to the type specified by the cast expression
37 D_ASSERT(result.GetType() == expr.return_type);
38 CastParameters parameters(expr.bound_cast.cast_data.get(), false, nullptr, lstate);
39 expr.bound_cast.function(child, result, count, parameters);
40 }
41}
42
43} // namespace duckdb
44