1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/execution/expression_executor_state.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/types/data_chunk.hpp"
13#include "duckdb/common/cycle_counter.hpp"
14#include "duckdb/function/function.hpp"
15
16namespace duckdb {
17class Expression;
18class ExpressionExecutor;
19struct ExpressionExecutorState;
20struct FunctionLocalState;
21
22struct ExpressionState {
23 ExpressionState(const Expression &expr, ExpressionExecutorState &root);
24 virtual ~ExpressionState() {
25 }
26
27 const Expression &expr;
28 ExpressionExecutorState &root;
29 vector<unique_ptr<ExpressionState>> child_states;
30 vector<LogicalType> types;
31 DataChunk intermediate_chunk;
32 CycleCounter profiler;
33
34public:
35 void AddChild(Expression *expr);
36 void Finalize();
37 Allocator &GetAllocator();
38 bool HasContext();
39 DUCKDB_API ClientContext &GetContext();
40
41 void Verify(ExpressionExecutorState &root);
42
43public:
44 template <class TARGET>
45 TARGET &Cast() {
46 D_ASSERT(dynamic_cast<TARGET *>(this));
47 return reinterpret_cast<TARGET &>(*this);
48 }
49 template <class TARGET>
50 const TARGET &Cast() const {
51 D_ASSERT(dynamic_cast<const TARGET *>(this));
52 return reinterpret_cast<const TARGET &>(*this);
53 }
54};
55
56struct ExecuteFunctionState : public ExpressionState {
57 ExecuteFunctionState(const Expression &expr, ExpressionExecutorState &root);
58 ~ExecuteFunctionState();
59
60 unique_ptr<FunctionLocalState> local_state;
61
62public:
63 static optional_ptr<FunctionLocalState> GetFunctionState(ExpressionState &state) {
64 return state.Cast<ExecuteFunctionState>().local_state.get();
65 }
66};
67
68struct ExpressionExecutorState {
69 ExpressionExecutorState();
70
71 unique_ptr<ExpressionState> root_state;
72 ExpressionExecutor *executor = nullptr;
73 CycleCounter profiler;
74
75 void Verify();
76};
77
78} // namespace duckdb
79