1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/optimizer/column_lifetime_optimizer.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/planner/logical_operator_visitor.hpp"
12#include "duckdb/planner/column_binding_map.hpp"
13#include "duckdb/common/vector.hpp"
14
15namespace duckdb {
16class BoundColumnRefExpression;
17
18//! The ColumnLifetimeAnalyzer optimizer traverses the logical operator tree and ensures that columns are removed from
19//! the plan when no longer required
20class ColumnLifetimeAnalyzer : public LogicalOperatorVisitor {
21public:
22 explicit ColumnLifetimeAnalyzer(bool is_root = false) : everything_referenced(is_root) {
23 }
24
25 void VisitOperator(LogicalOperator &op) override;
26
27protected:
28 unique_ptr<Expression> VisitReplace(BoundColumnRefExpression &expr, unique_ptr<Expression> *expr_ptr) override;
29 unique_ptr<Expression> VisitReplace(BoundReferenceExpression &expr, unique_ptr<Expression> *expr_ptr) override;
30
31private:
32 //! Whether or not all the columns are referenced. This happens in the case of the root expression (because the
33 //! output implicitly refers all the columns below it)
34 bool everything_referenced;
35 //! The set of column references
36 column_binding_set_t column_references;
37
38private:
39 void StandardVisitOperator(LogicalOperator &op);
40
41 void ExtractUnusedColumnBindings(vector<ColumnBinding> bindings, column_binding_set_t &unused_bindings);
42 void GenerateProjectionMap(vector<ColumnBinding> bindings, column_binding_set_t &unused_bindings,
43 vector<idx_t> &map);
44};
45} // namespace duckdb
46