1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/execution/physical_plan_generator.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/execution/physical_operator.hpp"
13#include "duckdb/planner/logical_operator.hpp"
14#include "duckdb/planner/logical_tokens.hpp"
15#include "duckdb/planner/operator/logical_limit_percent.hpp"
16#include "duckdb/catalog/dependency_list.hpp"
17#include "duckdb/common/unordered_map.hpp"
18#include "duckdb/common/unordered_set.hpp"
19
20namespace duckdb {
21class ClientContext;
22class ColumnDataCollection;
23
24//! The physical plan generator generates a physical execution plan from a
25//! logical query plan
26class PhysicalPlanGenerator {
27public:
28 explicit PhysicalPlanGenerator(ClientContext &context);
29 ~PhysicalPlanGenerator();
30
31 DependencyList dependencies;
32 //! Recursive CTEs require at least one ChunkScan, referencing the working_table.
33 //! This data structure is used to establish it.
34 unordered_map<idx_t, std::shared_ptr<ColumnDataCollection>> recursive_cte_tables;
35
36public:
37 //! Creates a plan from the logical operator. This involves resolving column bindings and generating physical
38 //! operator nodes.
39 unique_ptr<PhysicalOperator> CreatePlan(unique_ptr<LogicalOperator> logical);
40
41 //! Whether or not we can (or should) use a batch-index based operator for executing the given sink
42 static bool UseBatchIndex(ClientContext &context, PhysicalOperator &plan);
43 //! Whether or not we should preserve insertion order for executing the given sink
44 static bool PreserveInsertionOrder(ClientContext &context, PhysicalOperator &plan);
45
46protected:
47 unique_ptr<PhysicalOperator> CreatePlan(LogicalOperator &op);
48
49 unique_ptr<PhysicalOperator> CreatePlan(LogicalAggregate &op);
50 unique_ptr<PhysicalOperator> CreatePlan(LogicalAnyJoin &op);
51 unique_ptr<PhysicalOperator> CreatePlan(LogicalAsOfJoin &op);
52 unique_ptr<PhysicalOperator> CreatePlan(LogicalColumnDataGet &op);
53 unique_ptr<PhysicalOperator> CreatePlan(LogicalComparisonJoin &op);
54 unique_ptr<PhysicalOperator> CreatePlan(LogicalCreate &op);
55 unique_ptr<PhysicalOperator> CreatePlan(LogicalCreateTable &op);
56 unique_ptr<PhysicalOperator> CreatePlan(LogicalCreateIndex &op);
57 unique_ptr<PhysicalOperator> CreatePlan(LogicalCrossProduct &op);
58 unique_ptr<PhysicalOperator> CreatePlan(LogicalDelete &op);
59 unique_ptr<PhysicalOperator> CreatePlan(LogicalDelimGet &op);
60 unique_ptr<PhysicalOperator> CreatePlan(LogicalDelimJoin &op);
61 unique_ptr<PhysicalOperator> CreatePlan(LogicalDistinct &op);
62 unique_ptr<PhysicalOperator> CreatePlan(LogicalDummyScan &expr);
63 unique_ptr<PhysicalOperator> CreatePlan(LogicalEmptyResult &op);
64 unique_ptr<PhysicalOperator> CreatePlan(LogicalExpressionGet &op);
65 unique_ptr<PhysicalOperator> CreatePlan(LogicalExport &op);
66 unique_ptr<PhysicalOperator> CreatePlan(LogicalFilter &op);
67 unique_ptr<PhysicalOperator> CreatePlan(LogicalGet &op);
68 unique_ptr<PhysicalOperator> CreatePlan(LogicalLimit &op);
69 unique_ptr<PhysicalOperator> CreatePlan(LogicalLimitPercent &op);
70 unique_ptr<PhysicalOperator> CreatePlan(LogicalOrder &op);
71 unique_ptr<PhysicalOperator> CreatePlan(LogicalTopN &op);
72 unique_ptr<PhysicalOperator> CreatePlan(LogicalPositionalJoin &op);
73 unique_ptr<PhysicalOperator> CreatePlan(LogicalProjection &op);
74 unique_ptr<PhysicalOperator> CreatePlan(LogicalInsert &op);
75 unique_ptr<PhysicalOperator> CreatePlan(LogicalCopyToFile &op);
76 unique_ptr<PhysicalOperator> CreatePlan(LogicalExplain &op);
77 unique_ptr<PhysicalOperator> CreatePlan(LogicalSetOperation &op);
78 unique_ptr<PhysicalOperator> CreatePlan(LogicalUpdate &op);
79 unique_ptr<PhysicalOperator> CreatePlan(LogicalPrepare &expr);
80 unique_ptr<PhysicalOperator> CreatePlan(LogicalWindow &expr);
81 unique_ptr<PhysicalOperator> CreatePlan(LogicalExecute &op);
82 unique_ptr<PhysicalOperator> CreatePlan(LogicalPragma &op);
83 unique_ptr<PhysicalOperator> CreatePlan(LogicalSample &op);
84 unique_ptr<PhysicalOperator> CreatePlan(LogicalSet &op);
85 unique_ptr<PhysicalOperator> CreatePlan(LogicalReset &op);
86 unique_ptr<PhysicalOperator> CreatePlan(LogicalShow &op);
87 unique_ptr<PhysicalOperator> CreatePlan(LogicalSimple &op);
88 unique_ptr<PhysicalOperator> CreatePlan(LogicalUnnest &op);
89 unique_ptr<PhysicalOperator> CreatePlan(LogicalRecursiveCTE &op);
90 unique_ptr<PhysicalOperator> CreatePlan(LogicalCTERef &op);
91 unique_ptr<PhysicalOperator> CreatePlan(LogicalPivot &op);
92
93 unique_ptr<PhysicalOperator> ExtractAggregateExpressions(unique_ptr<PhysicalOperator> child,
94 vector<unique_ptr<Expression>> &expressions,
95 vector<unique_ptr<Expression>> &groups);
96
97private:
98 bool PreserveInsertionOrder(PhysicalOperator &plan);
99 bool UseBatchIndex(PhysicalOperator &plan);
100
101private:
102 ClientContext &context;
103};
104} // namespace duckdb
105