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