MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
memo.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_set.h"
14#include "common/lang/vector.h"
15#include "common/lang/memory.h"
16#include "common/log/log.h"
17#include "sql/optimizer/cascade/group_expr.h"
18#include "sql/optimizer/cascade/group.h"
19
20const int UNDEFINED_GROUP = -1;
21
28class Memo
29{
30public:
31 Memo() = default;
32
33 ~Memo() = default;
34
35 GroupExpr *insert_expression(GroupExpr *gexpr) { return insert_expression(gexpr, -1); }
36
37 GroupExpr *insert_expression(GroupExpr *gexpr, int target_group);
38
39 Group *get_group_by_id(int id) const
40 {
41 auto idx = id;
42 ASSERT(idx >= 0 && static_cast<size_t>(idx) < groups_.size(), "group_id out of bounds");
43 return groups_[idx].get();
44 }
45
46 void dump() const;
47
48 void record_operator(unique_ptr<OperatorNode> &&node) { operator_nodes_.emplace(node.get(), std::move(node)); }
49
50 void release_operator(OperatorNode *node)
51 {
52 auto it = operator_nodes_.find(node);
53 if (it != operator_nodes_.end()) {
54 it->second.release();
55 }
56 }
57
58private:
59 int add_new_group(GroupExpr *gexpr);
60
62 {
63 std::size_t operator()(GroupExpr *const &s) const
64 {
65 if (s == nullptr)
66 return 0;
67 return s->hash();
68 }
69 };
70
72 {
73 bool operator()(GroupExpr *t1, GroupExpr *t2) const
74 {
75 if (t1 && t2) { // 确保非空检查
76 return (*t1 == *t2);
77 }
78 return false; // 防止空指针解引用
79 }
80 };
81
85 std::unordered_set<GroupExpr *, GExprPtrHash, GExprPtrEq> group_expressions_;
86
87 vector<unique_ptr<Group>> groups_;
88
89 // TODO: 这是用来存储在 optimize
90 // 过程中生成的临时物理算子节点的,有些物理算子节点的所有权会转移到外面,有些物理算子的所有权还在memo,需要删除。 用
91 // shared_ptr 更加合适,但是改动比较大,先暂时不改了。
92 std::unordered_map<OperatorNode *, unique_ptr<OperatorNode>> operator_nodes_;
93};
Definition: group_expr.h:25
A class representing a group within cascade optimizer.
Definition: group.h:32
: memorization
Definition: memo.h:29
std::unordered_set< GroupExpr *, GExprPtrHash, GExprPtrEq > group_expressions_
Definition: memo.h:85
Definition: operator_node.h:69
Definition: memo.h:72
Definition: memo.h:62