1 | #include "duckdb/execution/operator/helper/physical_explain_analyze.hpp" |
2 | #include "duckdb/main/client_context.hpp" |
3 | #include "duckdb/main/query_profiler.hpp" |
4 | |
5 | namespace duckdb { |
6 | |
7 | //===--------------------------------------------------------------------===// |
8 | // Sink |
9 | //===--------------------------------------------------------------------===// |
10 | class ExplainAnalyzeStateGlobalState : public GlobalSinkState { |
11 | public: |
12 | string analyzed_plan; |
13 | }; |
14 | |
15 | SinkResultType PhysicalExplainAnalyze::Sink(ExecutionContext &context, DataChunk &chunk, |
16 | OperatorSinkInput &input) const { |
17 | return SinkResultType::NEED_MORE_INPUT; |
18 | } |
19 | |
20 | SinkFinalizeType PhysicalExplainAnalyze::Finalize(Pipeline &pipeline, Event &event, ClientContext &context, |
21 | GlobalSinkState &gstate_p) const { |
22 | auto &gstate = gstate_p.Cast<ExplainAnalyzeStateGlobalState>(); |
23 | auto &profiler = QueryProfiler::Get(context); |
24 | gstate.analyzed_plan = profiler.ToString(); |
25 | return SinkFinalizeType::READY; |
26 | } |
27 | |
28 | unique_ptr<GlobalSinkState> PhysicalExplainAnalyze::GetGlobalSinkState(ClientContext &context) const { |
29 | return make_uniq<ExplainAnalyzeStateGlobalState>(); |
30 | } |
31 | |
32 | //===--------------------------------------------------------------------===// |
33 | // Source |
34 | //===--------------------------------------------------------------------===// |
35 | SourceResultType PhysicalExplainAnalyze::GetData(ExecutionContext &context, DataChunk &chunk, |
36 | OperatorSourceInput &input) const { |
37 | auto &gstate = sink_state->Cast<ExplainAnalyzeStateGlobalState>(); |
38 | |
39 | chunk.SetValue(col_idx: 0, index: 0, val: Value("analyzed_plan" )); |
40 | chunk.SetValue(col_idx: 1, index: 0, val: Value(gstate.analyzed_plan)); |
41 | chunk.SetCardinality(1); |
42 | |
43 | return SourceResultType::FINISHED; |
44 | } |
45 | |
46 | } // namespace duckdb |
47 | |