1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/optimizer/remove_unused_columns.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 Binder;
17class BoundColumnRefExpression;
18class ClientContext;
19
20//! The RemoveUnusedColumns optimizer traverses the logical operator tree and removes any columns that are not required
21class RemoveUnusedColumns : public LogicalOperatorVisitor {
22public:
23 RemoveUnusedColumns(Binder &binder, ClientContext &context, bool is_root = false)
24 : binder(binder), context(context), everything_referenced(is_root) {
25 }
26
27 void VisitOperator(LogicalOperator &op) override;
28
29protected:
30 unique_ptr<Expression> VisitReplace(BoundColumnRefExpression &expr, unique_ptr<Expression> *expr_ptr) override;
31 unique_ptr<Expression> VisitReplace(BoundReferenceExpression &expr, unique_ptr<Expression> *expr_ptr) override;
32
33private:
34 Binder &binder;
35 ClientContext &context;
36 //! Whether or not all the columns are referenced. This happens in the case of the root expression (because the
37 //! output implicitly refers all the columns below it)
38 bool everything_referenced;
39 //! The map of column references
40 column_binding_map_t<vector<BoundColumnRefExpression *>> column_references;
41
42private:
43 template <class T>
44 void ClearUnusedExpressions(vector<T> &list, idx_t table_idx, bool replace = true);
45
46 //! Perform a replacement of the ColumnBinding, iterating over all the currently found column references and
47 //! replacing the bindings
48 void ReplaceBinding(ColumnBinding current_binding, ColumnBinding new_binding);
49};
50} // namespace duckdb
51