1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/optimizer/expression_heuristics.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/optimizer/optimizer.hpp"
12
13namespace duckdb {
14
15class ExpressionHeuristics : public LogicalOperatorVisitor {
16public:
17 ExpressionHeuristics(Optimizer &optimizer) : optimizer(optimizer) {
18 }
19
20 Optimizer &optimizer;
21 unique_ptr<LogicalOperator> root;
22
23public:
24 //! Search for filters to be reordered
25 unique_ptr<LogicalOperator> Rewrite(unique_ptr<LogicalOperator> op);
26 //! Reorder the expressions of a filter
27 void ReorderExpressions(vector<unique_ptr<Expression>> &expressions);
28 //! Return the cost of an expression
29 idx_t Cost(Expression &expr);
30
31 unique_ptr<Expression> VisitReplace(BoundConjunctionExpression &expr, unique_ptr<Expression> *expr_ptr) override;
32 //! Override this function to search for filter operators
33 void VisitOperator(LogicalOperator &op) override;
34
35private:
36 std::unordered_map<std::string, idx_t> function_costs = {
37 {"+", 5}, {"-", 5}, {"&", 5}, {"#", 5},
38 {">>", 5}, {"<<", 5}, {"abs", 5}, {"*", 10},
39 {"%", 10}, {"/", 15}, {"date_part", 20}, {"year", 20},
40 {"round", 100}, {"~~", 200}, {"!~~", 200}, {"regexp_matches", 200},
41 {"||", 200}};
42
43 idx_t ExpressionCost(BoundBetweenExpression &expr);
44 idx_t ExpressionCost(BoundCaseExpression &expr);
45 idx_t ExpressionCost(BoundCastExpression &expr);
46 idx_t ExpressionCost(BoundComparisonExpression &expr);
47 idx_t ExpressionCost(BoundConjunctionExpression &expr);
48 idx_t ExpressionCost(BoundFunctionExpression &expr);
49 idx_t ExpressionCost(BoundOperatorExpression &expr, ExpressionType &expr_type);
50 idx_t ExpressionCost(TypeId &return_type, idx_t multiplier);
51};
52} // namespace duckdb
53