1#include "duckdb/execution/physical_plan_generator.hpp"
2
3#include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp"
4#include "duckdb/execution/column_binding_resolver.hpp"
5#include "duckdb/main/client_context.hpp"
6#include "duckdb/planner/expression/bound_function_expression.hpp"
7
8using namespace duckdb;
9using namespace std;
10
11namespace duckdb {
12
13class DependencyExtractor : public LogicalOperatorVisitor {
14public:
15 DependencyExtractor(unordered_set<CatalogEntry *> &dependencies) : dependencies(dependencies) {
16 }
17
18protected:
19 unique_ptr<Expression> VisitReplace(BoundFunctionExpression &expr, unique_ptr<Expression> *expr_ptr) override {
20 // extract dependencies from the bound function expression
21 if (expr.function.dependency) {
22 expr.function.dependency(expr, dependencies);
23 }
24 return nullptr;
25 }
26
27private:
28 unordered_set<CatalogEntry *> &dependencies;
29};
30} // namespace duckdb
31
32unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(unique_ptr<LogicalOperator> op) {
33 // first resolve column references
34 context.profiler.StartPhase("column_binding");
35 ColumnBindingResolver resolver;
36 resolver.VisitOperator(*op);
37 context.profiler.EndPhase();
38
39 // now resolve types of all the operators
40 context.profiler.StartPhase("resolve_types");
41 op->ResolveOperatorTypes();
42 context.profiler.EndPhase();
43
44 // extract dependencies from the logical plan
45 DependencyExtractor extractor(dependencies);
46 extractor.VisitOperator(*op);
47
48 // then create the main physical plan
49 context.profiler.StartPhase("create_plan");
50 auto plan = CreatePlan(*op);
51 context.profiler.EndPhase();
52 return plan;
53}
54
55unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalOperator &op) {
56 switch (op.type) {
57 case LogicalOperatorType::GET:
58 return CreatePlan((LogicalGet &)op);
59 case LogicalOperatorType::PROJECTION:
60 return CreatePlan((LogicalProjection &)op);
61 case LogicalOperatorType::EMPTY_RESULT:
62 return CreatePlan((LogicalEmptyResult &)op);
63 case LogicalOperatorType::FILTER:
64 return CreatePlan((LogicalFilter &)op);
65 case LogicalOperatorType::AGGREGATE_AND_GROUP_BY:
66 return CreatePlan((LogicalAggregate &)op);
67 case LogicalOperatorType::WINDOW:
68 return CreatePlan((LogicalWindow &)op);
69 case LogicalOperatorType::UNNEST:
70 return CreatePlan((LogicalUnnest &)op);
71 case LogicalOperatorType::LIMIT:
72 return CreatePlan((LogicalLimit &)op);
73 case LogicalOperatorType::ORDER_BY:
74 return CreatePlan((LogicalOrder &)op);
75 case LogicalOperatorType::TOP_N:
76 return CreatePlan((LogicalTopN &)op);
77 case LogicalOperatorType::COPY_FROM_FILE:
78 return CreatePlan((LogicalCopyFromFile &)op);
79 case LogicalOperatorType::COPY_TO_FILE:
80 return CreatePlan((LogicalCopyToFile &)op);
81 case LogicalOperatorType::TABLE_FUNCTION:
82 return CreatePlan((LogicalTableFunction &)op);
83 case LogicalOperatorType::ANY_JOIN:
84 return CreatePlan((LogicalAnyJoin &)op);
85 case LogicalOperatorType::DELIM_JOIN:
86 return CreatePlan((LogicalDelimJoin &)op);
87 case LogicalOperatorType::COMPARISON_JOIN:
88 return CreatePlan((LogicalComparisonJoin &)op);
89 case LogicalOperatorType::CROSS_PRODUCT:
90 return CreatePlan((LogicalCrossProduct &)op);
91 case LogicalOperatorType::UNION:
92 case LogicalOperatorType::EXCEPT:
93 case LogicalOperatorType::INTERSECT:
94 return CreatePlan((LogicalSetOperation &)op);
95 case LogicalOperatorType::INSERT:
96 return CreatePlan((LogicalInsert &)op);
97 case LogicalOperatorType::DELETE:
98 return CreatePlan((LogicalDelete &)op);
99 case LogicalOperatorType::CHUNK_GET:
100 return CreatePlan((LogicalChunkGet &)op);
101 case LogicalOperatorType::DELIM_GET:
102 return CreatePlan((LogicalDelimGet &)op);
103 case LogicalOperatorType::EXPRESSION_GET:
104 return CreatePlan((LogicalExpressionGet &)op);
105 case LogicalOperatorType::UPDATE:
106 return CreatePlan((LogicalUpdate &)op);
107 case LogicalOperatorType::CREATE_TABLE:
108 return CreatePlan((LogicalCreateTable &)op);
109 case LogicalOperatorType::CREATE_INDEX:
110 return CreatePlan((LogicalCreateIndex &)op);
111 case LogicalOperatorType::EXPLAIN:
112 return CreatePlan((LogicalExplain &)op);
113 case LogicalOperatorType::DISTINCT:
114 return CreatePlan((LogicalDistinct &)op);
115 case LogicalOperatorType::PREPARE:
116 return CreatePlan((LogicalPrepare &)op);
117 case LogicalOperatorType::EXECUTE:
118 return CreatePlan((LogicalExecute &)op);
119 case LogicalOperatorType::INDEX_SCAN:
120 return CreatePlan((LogicalIndexScan &)op);
121 case LogicalOperatorType::CREATE_VIEW:
122 case LogicalOperatorType::CREATE_SEQUENCE:
123 case LogicalOperatorType::CREATE_SCHEMA:
124 return CreatePlan((LogicalCreate &)op);
125 case LogicalOperatorType::TRANSACTION:
126 case LogicalOperatorType::ALTER:
127 case LogicalOperatorType::DROP:
128 case LogicalOperatorType::PRAGMA:
129 case LogicalOperatorType::VACUUM:
130 return CreatePlan((LogicalSimple &)op);
131 case LogicalOperatorType::RECURSIVE_CTE:
132 return CreatePlan((LogicalRecursiveCTE &)op);
133 case LogicalOperatorType::CTE_REF:
134 return CreatePlan((LogicalCTERef &)op);
135 default:
136 throw NotImplementedException("Unimplemented logical operator type!");
137 }
138}
139