Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit 32ea192

Browse files
committed
Remove const from input nodes.
Signed-off-by: ienkovich <[email protected]>
1 parent 27878e1 commit 32ea192

File tree

4 files changed

+55
-60
lines changed

4 files changed

+55
-60
lines changed

omniscidb/IR/Node.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,17 @@ bool isRenamedInput(const Node* node, const size_t index, const std::string& new
100100

101101
} // namespace
102102

103-
thread_local unsigned Node::crt_id_ = FIRST_NODE_ID;
103+
std::atomic<unsigned> Node::crt_id_ = FIRST_NODE_ID;
104104

105105
Node::Node(NodeInputs inputs)
106106
: inputs_(std::move(inputs))
107-
, id_(crt_id_++)
107+
, id_(crt_id_.fetch_add(1))
108108
, context_data_(nullptr)
109109
, is_nop_(false) {}
110110

111111
void Node::replaceInput(
112-
std::shared_ptr<const Node> old_input,
113-
std::shared_ptr<const Node> input,
112+
NodePtr old_input,
113+
NodePtr input,
114114
std::optional<std::unordered_map<unsigned, unsigned>> old_to_new_index_map) {
115115
InputRewriter rewriter;
116116
if (old_to_new_index_map) {
@@ -121,8 +121,8 @@ void Node::replaceInput(
121121
replaceInput(old_input, input, rewriter);
122122
}
123123

124-
void Node::replaceInput(std::shared_ptr<const Node> old_input,
125-
std::shared_ptr<const Node> input,
124+
void Node::replaceInput(NodePtr old_input,
125+
NodePtr input,
126126
hdk::ir::ExprRewriter& input_redirector) {
127127
bool replaced = false;
128128
for (auto& input_ptr : inputs_) {
@@ -136,10 +136,6 @@ void Node::replaceInput(std::shared_ptr<const Node> old_input,
136136
}
137137
}
138138

139-
void Node::resetRelAlgFirstId() noexcept {
140-
crt_id_ = FIRST_NODE_ID;
141-
}
142-
143139
void Node::print() const {
144140
std::cout << toString() << std::endl;
145141
}

omniscidb/IR/Node.h

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class SortField {
6565
const NullSortedPosition nulls_pos_;
6666
};
6767

68-
using NodeInputs = std::vector<std::shared_ptr<const Node>>;
68+
using NodePtr = std::shared_ptr<Node>;
69+
using NodeInputs = std::vector<NodePtr>;
6970

7071
class Node {
7172
public:
@@ -116,12 +117,22 @@ class Node {
116117
return inputs_[idx].get();
117118
}
118119

120+
Node* getInput(const size_t idx) {
121+
CHECK_LT(idx, inputs_.size());
122+
return inputs_[idx].get();
123+
}
124+
119125
std::shared_ptr<const Node> getAndOwnInput(const size_t idx) const {
120126
CHECK_LT(idx, inputs_.size());
121127
return inputs_[idx];
122128
}
123129

124-
void addManagedInput(std::shared_ptr<const Node> input) { inputs_.push_back(input); }
130+
NodePtr getAndOwnInput(const size_t idx) {
131+
CHECK_LT(idx, inputs_.size());
132+
return inputs_[idx];
133+
}
134+
135+
void addManagedInput(NodePtr input) { inputs_.push_back(input); }
125136

126137
bool hasInput(const Node* needle) const {
127138
for (auto& input_ptr : inputs_) {
@@ -134,12 +145,12 @@ class Node {
134145

135146
virtual void rewriteExprs(hdk::ir::ExprRewriter& rewriter) {}
136147

137-
void replaceInput(std::shared_ptr<const Node> old_input,
138-
std::shared_ptr<const Node> input,
148+
void replaceInput(NodePtr old_input,
149+
NodePtr input,
139150
std::optional<std::unordered_map<unsigned, unsigned>>
140151
old_to_new_index_map = std::nullopt);
141-
void replaceInput(std::shared_ptr<const Node> old_input,
142-
std::shared_ptr<const Node> input,
152+
void replaceInput(NodePtr old_input,
153+
NodePtr input,
143154
hdk::ir::ExprRewriter& input_redirector);
144155

145156
// to keep an assigned DAG node id for data recycler
@@ -162,8 +173,6 @@ class Node {
162173

163174
virtual std::shared_ptr<Node> deepCopy() const = 0;
164175

165-
static void resetRelAlgFirstId() noexcept;
166-
167176
/**
168177
* Clears the ptr to the result for this descriptor. Is only used for overriding step
169178
* results in distributed mode.
@@ -185,7 +194,7 @@ class Node {
185194
mutable const RaExecutionDesc* context_data_;
186195
bool is_nop_;
187196
mutable std::vector<TargetMetaInfo> targets_metainfo_;
188-
static thread_local unsigned crt_id_;
197+
static std::atomic<unsigned> crt_id_;
189198
mutable size_t dag_node_id_;
190199
mutable std::shared_ptr<const ExecutionResult> result_;
191200
};
@@ -203,8 +212,6 @@ inline std::string inputsToString(const NodeInputs& inputs) {
203212
return ss.str();
204213
}
205214

206-
using NodePtr = std::shared_ptr<Node>;
207-
208215
class Scan : public Node {
209216
public:
210217
Scan(TableInfoPtr tinfo, std::vector<ColumnInfoPtr> column_infos)
@@ -280,9 +287,7 @@ class Scan : public Node {
280287
class Project : public Node {
281288
public:
282289
// Takes memory ownership of the expressions.
283-
Project(ExprPtrVector exprs,
284-
std::vector<std::string> fields,
285-
std::shared_ptr<const Node> input)
290+
Project(ExprPtrVector exprs, std::vector<std::string> fields, NodePtr input)
286291
: exprs_(std::move(exprs)), fields_(std::move(fields)) {
287292
inputs_.push_back(input);
288293
}
@@ -364,7 +369,7 @@ class Aggregate : public Node {
364369
Aggregate(const size_t groupby_count,
365370
ExprPtrVector aggs,
366371
std::vector<std::string> fields,
367-
std::shared_ptr<const Node> input)
372+
NodePtr input)
368373
: groupby_count_(groupby_count)
369374
, aggs_(std::move(aggs))
370375
, fields_(std::move(fields)) {
@@ -439,10 +444,7 @@ class Aggregate : public Node {
439444

440445
class Join : public Node {
441446
public:
442-
Join(std::shared_ptr<const Node> lhs,
443-
std::shared_ptr<const Node> rhs,
444-
ExprPtr condition,
445-
const JoinType join_type)
447+
Join(NodePtr lhs, NodePtr rhs, ExprPtr condition, const JoinType join_type)
446448
: condition_(std::move(condition)), join_type_(join_type) {
447449
inputs_.emplace_back(std::move(lhs));
448450
inputs_.emplace_back(std::move(rhs));
@@ -604,8 +606,7 @@ class TranslatedJoin : public Node {
604606
605607
class Filter : public Node {
606608
public:
607-
Filter(ExprPtr condition, std::shared_ptr<const Node> input)
608-
: condition_(std::move(condition)) {
609+
Filter(ExprPtr condition, NodePtr input) : condition_(std::move(condition)) {
609610
CHECK(condition_);
610611
inputs_.emplace_back(std::move(input));
611612
}
@@ -663,7 +664,7 @@ class Sort : public Node {
663664
Sort(std::vector<SortField> collation,
664665
const size_t limit,
665666
const size_t offset,
666-
std::shared_ptr<const Node> input)
667+
NodePtr input)
667668
: collation_(std::move(collation))
668669
, limit_(limit)
669670
, offset_(offset)

omniscidb/QueryEngine/RelAlgDagBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ class RelAlgDispatcher {
23822382
return {key, val};
23832383
}
23842384

2385-
std::shared_ptr<const hdk::ir::Node> prev(const rapidjson::Value& crt_node) {
2385+
hdk::ir::NodePtr prev(const rapidjson::Value& crt_node) {
23862386
const auto id = node_id(crt_node);
23872387
CHECK(id);
23882388
CHECK_EQ(static_cast<size_t>(id), nodes_.size());
@@ -2432,7 +2432,6 @@ RelAlgDagBuilder::RelAlgDagBuilder(const std::string& query_ra,
24322432
"Failed to parse relational algebra tree. Possible query syntax error.");
24332433
}
24342434
CHECK(query_ast.IsObject());
2435-
hdk::ir::Node::resetRelAlgFirstId();
24362435
build(query_ast, *this);
24372436
}
24382437

omniscidb/QueryEngine/RelAlgOptimizer.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ namespace {
3131

3232
class ProjectInputRedirector : public hdk::ir::ExprRewriter {
3333
public:
34-
ProjectInputRedirector(const std::unordered_set<const hdk::ir::Project*>& crt_inputs)
34+
ProjectInputRedirector(const std::unordered_set<hdk::ir::Project*>& crt_inputs)
3535
: crt_projects_(crt_inputs) {}
3636

3737
hdk::ir::ExprPtr visitColumnRef(const hdk::ir::ColumnRef* col_ref) override {
3838
auto source = dynamic_cast<const hdk::ir::Project*>(col_ref->node());
39-
if (source && crt_projects_.count(source)) {
39+
if (source && crt_projects_.count(const_cast<hdk::ir::Project*>(source))) {
4040
auto new_source = source->getInput(0);
4141
auto new_col_ref = dynamic_cast<const hdk::ir::ColumnRef*>(
4242
source->getExpr(col_ref->index()).get());
@@ -49,7 +49,7 @@ class ProjectInputRedirector : public hdk::ir::ExprRewriter {
4949
}
5050

5151
private:
52-
const std::unordered_set<const hdk::ir::Project*>& crt_projects_;
52+
const std::unordered_set<hdk::ir::Project*>& crt_projects_;
5353
};
5454

5555
// TODO: use more generic InputRenumberVisitor instead.
@@ -81,13 +81,13 @@ class InputSimpleRenumberVisitor : public hdk::ir::ExprRewriter {
8181

8282
size_t get_actual_source_size(
8383
const hdk::ir::Project* curr_project,
84-
const std::unordered_set<const hdk::ir::Project*>& projects_to_remove) {
84+
const std::unordered_set<hdk::ir::Project*>& projects_to_remove) {
8585
auto source = curr_project->getInput(0);
8686
while (auto filter = dynamic_cast<const hdk::ir::Filter*>(source)) {
8787
source = filter->getInput(0);
8888
}
8989
if (auto src_project = dynamic_cast<const hdk::ir::Project*>(source)) {
90-
if (projects_to_remove.count(src_project)) {
90+
if (projects_to_remove.count(const_cast<hdk::ir::Project*>(src_project))) {
9191
return get_actual_source_size(src_project, projects_to_remove);
9292
}
9393
}
@@ -113,11 +113,11 @@ bool safe_to_redirect(
113113
}
114114

115115
bool is_identical_copy(
116-
const hdk::ir::Project* project,
116+
hdk::ir::Project* project,
117117
const std::unordered_map<const hdk::ir::Node*,
118118
std::unordered_set<const hdk::ir::Node*>>& du_web,
119-
const std::unordered_set<const hdk::ir::Project*>& projects_to_remove,
120-
std::unordered_set<const hdk::ir::Project*>& permutating_projects) {
119+
const std::unordered_set<hdk::ir::Project*>& projects_to_remove,
120+
std::unordered_set<hdk::ir::Project*>& permutating_projects) {
121121
auto source_size = get_actual_source_size(project, projects_to_remove);
122122
if (project->size() > source_size) {
123123
return false;
@@ -221,17 +221,17 @@ void propagate_rex_input_renumber(
221221
// This function appears to redirect/remove redundant Projection input nodes(?)
222222
void redirect_inputs_of(
223223
std::shared_ptr<hdk::ir::Node> node,
224-
const std::unordered_set<const hdk::ir::Project*>& projects,
225-
const std::unordered_set<const hdk::ir::Project*>& permutating_projects,
224+
const std::unordered_set<hdk::ir::Project*>& projects,
225+
const std::unordered_set<hdk::ir::Project*>& permutating_projects,
226226
const std::unordered_map<const hdk::ir::Node*,
227227
std::unordered_set<const hdk::ir::Node*>>& du_web) {
228228
if (dynamic_cast<hdk::ir::LogicalUnion*>(node.get())) {
229229
return; // UNION keeps all Projection inputs.
230230
}
231-
std::shared_ptr<const hdk::ir::Project> src_project = nullptr;
231+
std::shared_ptr<hdk::ir::Project> src_project = nullptr;
232232
for (size_t i = 0; i < node->inputCount(); ++i) {
233233
if (auto project =
234-
std::dynamic_pointer_cast<const hdk::ir::Project>(node->getAndOwnInput(i))) {
234+
std::dynamic_pointer_cast<hdk::ir::Project>(node->getAndOwnInput(i))) {
235235
if (projects.count(project.get())) {
236236
src_project = project;
237237
break;
@@ -244,8 +244,8 @@ void redirect_inputs_of(
244244
if (auto join = std::dynamic_pointer_cast<hdk::ir::Join>(node)) {
245245
auto other_project =
246246
src_project == node->getAndOwnInput(0)
247-
? std::dynamic_pointer_cast<const hdk::ir::Project>(node->getAndOwnInput(1))
248-
: std::dynamic_pointer_cast<const hdk::ir::Project>(node->getAndOwnInput(0));
247+
? std::dynamic_pointer_cast<hdk::ir::Project>(node->getAndOwnInput(1))
248+
: std::dynamic_pointer_cast<hdk::ir::Project>(node->getAndOwnInput(0));
249249
join->replaceInput(src_project, src_project->getAndOwnInput(0));
250250
// Case when join users have to be adjusted is not expected.
251251
CHECK(src_project != node->getAndOwnInput(0) ||
@@ -459,16 +459,16 @@ bool project_separates_sort(const hdk::ir::Project* project,
459459
// TODO(miyu): allow more chance if proved safe
460460
void eliminate_identical_copy(
461461
std::vector<std::shared_ptr<hdk::ir::Node>>& nodes) noexcept {
462-
std::unordered_set<std::shared_ptr<const hdk::ir::Node>> copies;
462+
std::unordered_set<hdk::ir::NodePtr> copies;
463463
auto sink = nodes.back();
464464
for (auto node : nodes) {
465-
auto aggregate = std::dynamic_pointer_cast<const hdk::ir::Aggregate>(node);
465+
auto aggregate = std::dynamic_pointer_cast<hdk::ir::Aggregate>(node);
466466
if (!aggregate || aggregate == sink ||
467467
!(aggregate->getGroupByCount() == 1 && aggregate->getAggsCount() == 0)) {
468468
continue;
469469
}
470470
auto project =
471-
std::dynamic_pointer_cast<const hdk::ir::Project>(aggregate->getAndOwnInput(0));
471+
std::dynamic_pointer_cast<hdk::ir::Project>(aggregate->getAndOwnInput(0));
472472
if (project && project->size() == aggregate->size() &&
473473
project->getFields() == aggregate->getFields()) {
474474
CHECK_EQ(size_t(0), copies.count(aggregate));
@@ -483,30 +483,29 @@ void eliminate_identical_copy(
483483
if (!copies.count(last_source)) {
484484
continue;
485485
}
486-
auto aggregate = std::dynamic_pointer_cast<const hdk::ir::Aggregate>(last_source);
486+
auto aggregate = std::dynamic_pointer_cast<hdk::ir::Aggregate>(last_source);
487487
CHECK(aggregate);
488-
if (!std::dynamic_pointer_cast<const hdk::ir::Join>(node) || aggregate->size() != 1) {
488+
if (!std::dynamic_pointer_cast<hdk::ir::Join>(node) || aggregate->size() != 1) {
489489
continue;
490490
}
491491
auto project =
492-
std::dynamic_pointer_cast<const hdk::ir::Project>(aggregate->getAndOwnInput(0));
492+
std::dynamic_pointer_cast<hdk::ir::Project>(aggregate->getAndOwnInput(0));
493493
CHECK(project);
494494
CHECK_EQ(size_t(1), project->size());
495495
if (!is_distinct(size_t(0), project.get())) {
496496
continue;
497497
}
498498
auto new_source = project->getAndOwnInput(0);
499-
if (std::dynamic_pointer_cast<const hdk::ir::Sort>(new_source) ||
500-
std::dynamic_pointer_cast<const hdk::ir::Scan>(new_source)) {
499+
if (new_source->is<hdk::ir::Sort>() || new_source->is<hdk::ir::Scan>()) {
501500
node->replaceInput(last_source, new_source);
502501
}
503502
}
504503
decltype(copies)().swap(copies);
505504

506505
auto web = build_du_web(nodes);
507506

508-
std::unordered_set<const hdk::ir::Project*> projects;
509-
std::unordered_set<const hdk::ir::Project*> permutating_projects;
507+
std::unordered_set<hdk::ir::Project*> projects;
508+
std::unordered_set<hdk::ir::Project*> permutating_projects;
510509
auto const visible_projs = get_visible_projects(nodes.back().get());
511510
for (auto node_it = nodes.begin(); node_it != nodes.end(); node_it++) {
512511
auto node = *node_it;
@@ -1350,8 +1349,8 @@ class InputRedirector : public hdk::ir::ExprRewriter {
13501349
};
13511350

13521351
void replace_all_usages(
1353-
std::shared_ptr<const hdk::ir::Node> old_def_node,
1354-
std::shared_ptr<const hdk::ir::Node> new_def_node,
1352+
hdk::ir::NodePtr old_def_node,
1353+
hdk::ir::NodePtr new_def_node,
13551354
std::unordered_map<const hdk::ir::Node*, std::shared_ptr<hdk::ir::Node>>&
13561355
deconst_mapping,
13571356
std::unordered_map<const hdk::ir::Node*, std::unordered_set<const hdk::ir::Node*>>&

0 commit comments

Comments
 (0)