1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/optimizer/expression_rewriter.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/optimizer/rule.hpp"
12#include "duckdb/planner/logical_operator_visitor.hpp"
13#include "duckdb/common/types/value.hpp"
14
15namespace duckdb {
16class ClientContext;
17
18//! The ExpressionRewriter performs a set of fixed rewrite rules on the expressions that occur in a SQL statement
19class ExpressionRewriter : public LogicalOperatorVisitor {
20public:
21 explicit ExpressionRewriter(ClientContext &context) : context(context) {
22 }
23
24public:
25 //! The set of rules as known by the Expression Rewriter
26 vector<unique_ptr<Rule>> rules;
27
28 ClientContext &context;
29
30public:
31 void VisitOperator(LogicalOperator &op) override;
32 void VisitExpression(unique_ptr<Expression> *expression) override;
33
34 // Generates either a constant_or_null(child) expression
35 static unique_ptr<Expression> ConstantOrNull(unique_ptr<Expression> child, Value value);
36 static unique_ptr<Expression> ConstantOrNull(vector<unique_ptr<Expression>> children, Value value);
37
38private:
39 //! Apply a set of rules to a specific expression
40 static unique_ptr<Expression> ApplyRules(LogicalOperator &op, const vector<reference<Rule>> &rules,
41 unique_ptr<Expression> expr, bool &changes_made, bool is_root = false);
42
43 optional_ptr<LogicalOperator> op;
44 vector<reference<Rule>> to_apply_rules;
45};
46
47} // namespace duckdb
48