MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
rules.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include "common/lang/unordered_map.h"
14#include "sql/optimizer/cascade/optimizer_context.h"
15#include "sql/optimizer/cascade/pattern.h"
16
20enum class RuleType : uint32_t
21{
22 // Transformation rules (logical -> logical)
23 // TODO: currently, we don't have transformation rules
24
25 // Don't move this one
26 LogicalPhysicalDelimiter,
27
28 // Implementation rules (logical -> physical)
29 GET_TO_SEQ_SCAN,
30 GET_TO_INDEX_SCAN,
31 DELETE_TO_PHYSICAL,
32 UPDATE_TO_PHYSICAL,
33 INSERT_TO_PHYSICAL,
34 AGGREGATE_TO_PHYSICAL,
35 INNER_JOIN_TO_NL_JOIN,
36 INNER_JOIN_TO_HASH_JOIN,
37 IMPLEMENT_LIMIT,
38 PROJECTION_TO_PHYSOCAL,
39 ANALYZE_TO_PHYSICAL,
40 EXPLAIN_TO_PHYSICAL,
41 CALC_TO_PHYSICAL,
42 PREDICATE_TO_PHYSICAL,
43 GROUP_BY_TO_PHYSICAL_AGGREGATION,
44 GROUP_BY_TO_PHYSICL_HASH_GROUP_BY,
45
46 NUM_RULES
47};
48
52enum class RuleSetName : uint32_t
53{
54 // TODO: add more rule sets
55 PHYSICAL_IMPLEMENTATION
56};
57
62enum class RulePromise : uint32_t
63{
64
68 LOGICAL_PROMISE = 0,
69
73 PHYSICAL_PROMISE = 1
74};
75
79class Rule
80{
81public:
82 virtual ~Rule() {}
87 Pattern *get_match_pattern() const { return match_pattern_.get(); }
88
92 bool is_physical() const { return type_ > RuleType::LogicalPhysicalDelimiter; }
93
97 bool is_logical() const { return type_ < RuleType::LogicalPhysicalDelimiter; }
98
102 RuleType get_type() { return type_; }
103
107 uint32_t get_rule_idx() { return static_cast<uint32_t>(type_); }
108
115 virtual RulePromise promise(GroupExpr *group_expr) const
116 {
117 if (is_physical())
118 return RulePromise::PHYSICAL_PROMISE;
119 return RulePromise::LOGICAL_PROMISE;
120 }
121
129 virtual void transform(OperatorNode *input, std::vector<std::unique_ptr<OperatorNode>> *transformed,
130 OptimizerContext *context) const = 0;
131
132protected:
133 RuleType type_;
134 unique_ptr<Pattern> match_pattern_;
135};
136
138{
139public:
140 RuleWithPromise(Rule *rule, RulePromise promise) : rule_(rule), promise_(promise) {}
141
142 Rule *get_rule() { return rule_; }
143
148 RulePromise get_promise() { return promise_; }
149
150 bool operator<(const RuleWithPromise &r) const { return promise_ < r.promise_; }
151
152 bool operator>(const RuleWithPromise &r) const { return promise_ > r.promise_; }
153
154private:
159
163 RulePromise promise_;
164};
165
167{
168public:
169 RuleSet();
170
171 ~RuleSet()
172 {
173 for (auto &it : rules_map_) {
174 for (auto rule : it.second) {
175 delete rule;
176 }
177 }
178 }
179
183 void add_rule(RuleSetName set, Rule *rule) { rules_map_[static_cast<uint32_t>(set)].push_back(rule); }
184
188 std::vector<Rule *> &get_rules_by_name(RuleSetName set) { return rules_map_[static_cast<uint32_t>(set)]; }
189
190private:
195 std::unordered_map<uint32_t, std::vector<Rule *>> rules_map_;
196};
Definition: group_expr.h:25
Definition: operator_node.h:69
Definition: optimizer_context.h:27
Definition: pattern.h:18
Definition: rules.h:167
std::vector< Rule * > & get_rules_by_name(RuleSetName set)
Definition: rules.h:188
void add_rule(RuleSetName set, Rule *rule)
Definition: rules.h:183
std::unordered_map< uint32_t, std::vector< Rule * > > rules_map_
Definition: rules.h:195
Definition: rules.h:138
Rule * rule_
Definition: rules.h:158
RulePromise get_promise()
Definition: rules.h:148
RulePromise promise_
Definition: rules.h:163
Definition: rules.h:80
uint32_t get_rule_idx()
Definition: rules.h:107
bool is_logical() const
Definition: rules.h:97
virtual void transform(OperatorNode *input, std::vector< std::unique_ptr< OperatorNode > > *transformed, OptimizerContext *context) const =0
bool is_physical() const
Definition: rules.h:92
RuleType get_type()
Definition: rules.h:102
virtual RulePromise promise(GroupExpr *group_expr) const
Definition: rules.h:115
Pattern * get_match_pattern() const
Definition: rules.h:87