OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RelAlgOptimizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RelAlgOptimizer.h"
18 #include "Logger/Logger.h"
19 #include "RexVisitor.h"
21 
22 #include <numeric>
23 #include <string>
24 #include <unordered_map>
25 
26 namespace {
27 
29  public:
30  RexProjectInputRedirector(const std::unordered_set<const RelProject*>& crt_inputs)
31  : crt_projects_(crt_inputs) {}
32 
33  RetType visitInput(const RexInput* input) const override {
34  auto source = dynamic_cast<const RelProject*>(input->getSourceNode());
35  if (!source || !crt_projects_.count(source)) {
36  return input->deepCopy();
37  }
38  auto new_source = source->getInput(0);
39  auto new_input =
40  dynamic_cast<const RexInput*>(source->getProjectAt(input->getIndex()));
41  if (!new_input) {
42  return input->deepCopy();
43  }
44  if (auto join = dynamic_cast<const RelJoin*>(new_source)) {
45  CHECK(new_input->getSourceNode() == join->getInput(0) ||
46  new_input->getSourceNode() == join->getInput(1));
47  } else {
48  CHECK_EQ(new_input->getSourceNode(), new_source);
49  }
50  return new_input->deepCopy();
51  }
52 
53  private:
54  const std::unordered_set<const RelProject*>& crt_projects_;
55 };
56 
57 class RexRebindInputsVisitor : public RexVisitor<void*> {
58  public:
59  RexRebindInputsVisitor(const RelAlgNode* old_input, const RelAlgNode* new_input)
60  : old_input_(old_input), new_input_(new_input) {}
61 
62  void* visitInput(const RexInput* rex_input) const override {
63  const auto old_source = rex_input->getSourceNode();
64  if (old_source == old_input_) {
65  rex_input->setSourceNode(new_input_);
66  }
67  return nullptr;
68  };
69 
70  void visitNode(const RelAlgNode* node) const {
71  if (dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelSort*>(node)) {
72  return;
73  }
74  if (auto join = dynamic_cast<const RelJoin*>(node)) {
75  if (auto condition = join->getCondition()) {
76  visit(condition);
77  }
78  return;
79  }
80  if (auto project = dynamic_cast<const RelProject*>(node)) {
81  for (size_t i = 0; i < project->size(); ++i) {
82  visit(project->getProjectAt(i));
83  }
84  return;
85  }
86  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
87  visit(filter->getCondition());
88  return;
89  }
90  CHECK(false);
91  }
92 
93  private:
96 };
97 
99  const RelProject* curr_project,
100  const std::unordered_set<const RelProject*>& projects_to_remove) {
101  auto source = curr_project->getInput(0);
102  while (auto filter = dynamic_cast<const RelFilter*>(source)) {
103  source = filter->getInput(0);
104  }
105  if (auto src_project = dynamic_cast<const RelProject*>(source)) {
106  if (projects_to_remove.count(src_project)) {
107  return get_actual_source_size(src_project, projects_to_remove);
108  }
109  }
110  return curr_project->getInput(0)->size();
111 }
112 
114  const RelProject* project,
115  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
116  du_web) {
117  if (!project->isSimple()) {
118  return false;
119  }
120  auto usrs_it = du_web.find(project);
121  CHECK(usrs_it != du_web.end());
122  for (auto usr : usrs_it->second) {
123  if (!dynamic_cast<const RelProject*>(usr) && !dynamic_cast<const RelFilter*>(usr)) {
124  return false;
125  }
126  }
127  return true;
128 }
129 
131  const RelProject* project,
132  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
133  du_web,
134  const std::unordered_set<const RelProject*>& projects_to_remove,
135  std::unordered_set<const RelProject*>& permutating_projects) {
136  auto source_size = get_actual_source_size(project, projects_to_remove);
137  if (project->size() > source_size) {
138  return false;
139  }
140 
141  if (project->size() < source_size) {
142  auto usrs_it = du_web.find(project);
143  CHECK(usrs_it != du_web.end());
144  bool guard_found = false;
145  while (usrs_it->second.size() == size_t(1)) {
146  auto only_usr = *usrs_it->second.begin();
147  if (dynamic_cast<const RelProject*>(only_usr)) {
148  guard_found = true;
149  break;
150  }
151  if (dynamic_cast<const RelAggregate*>(only_usr) ||
152  dynamic_cast<const RelSort*>(only_usr) ||
153  dynamic_cast<const RelJoin*>(only_usr) ||
154  dynamic_cast<const RelTableFunction*>(only_usr) ||
155  dynamic_cast<const RelLogicalUnion*>(only_usr)) {
156  return false;
157  }
158  CHECK(dynamic_cast<const RelFilter*>(only_usr))
159  << "only_usr: " << only_usr->toString();
160  usrs_it = du_web.find(only_usr);
161  CHECK(usrs_it != du_web.end());
162  }
163 
164  if (!guard_found) {
165  return false;
166  }
167  }
168 
169  bool identical = true;
170  for (size_t i = 0; i < project->size(); ++i) {
171  auto target = dynamic_cast<const RexInput*>(project->getProjectAt(i));
172  CHECK(target);
173  if (i != target->getIndex()) {
174  identical = false;
175  break;
176  }
177  }
178 
179  if (identical) {
180  return true;
181  }
182 
183  if (safe_to_redirect(project, du_web)) {
184  permutating_projects.insert(project);
185  return true;
186  }
187 
188  return false;
189 }
190 
192  const RelFilter* excluded_root,
193  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
194  du_web) {
195  CHECK(excluded_root);
196  auto src_project = dynamic_cast<const RelProject*>(excluded_root->getInput(0));
197  CHECK(src_project && src_project->isSimple());
198  const auto indirect_join_src = dynamic_cast<const RelJoin*>(src_project->getInput(0));
199  std::unordered_map<size_t, size_t> old_to_new_idx;
200  for (size_t i = 0; i < src_project->size(); ++i) {
201  auto rex_in = dynamic_cast<const RexInput*>(src_project->getProjectAt(i));
202  CHECK(rex_in);
203  size_t src_base = 0;
204  if (indirect_join_src != nullptr &&
205  indirect_join_src->getInput(1) == rex_in->getSourceNode()) {
206  src_base = indirect_join_src->getInput(0)->size();
207  }
208  old_to_new_idx.insert(std::make_pair(i, src_base + rex_in->getIndex()));
209  old_to_new_idx.insert(std::make_pair(i, rex_in->getIndex()));
210  }
211  CHECK(old_to_new_idx.size());
212  RexInputRenumber<false> renumber(old_to_new_idx);
213  auto usrs_it = du_web.find(excluded_root);
214  CHECK(usrs_it != du_web.end());
215  std::vector<const RelAlgNode*> work_set(usrs_it->second.begin(), usrs_it->second.end());
216  while (!work_set.empty()) {
217  auto node = work_set.back();
218  work_set.pop_back();
219  auto modified_node = const_cast<RelAlgNode*>(node);
220  if (auto filter = dynamic_cast<RelFilter*>(modified_node)) {
221  auto new_condition = renumber.visit(filter->getCondition());
222  filter->setCondition(new_condition);
223  auto usrs_it = du_web.find(filter);
224  CHECK(usrs_it != du_web.end() && usrs_it->second.size() == 1);
225  work_set.push_back(*usrs_it->second.begin());
226  continue;
227  }
228  if (auto project = dynamic_cast<RelProject*>(modified_node)) {
229  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
230  for (size_t i = 0; i < project->size(); ++i) {
231  new_exprs.push_back(renumber.visit(project->getProjectAt(i)));
232  }
233  project->setExpressions(new_exprs);
234  continue;
235  }
236  CHECK(false);
237  }
238 }
239 
240 // This function appears to redirect/remove redundant Projection input nodes(?)
242  std::shared_ptr<RelAlgNode> node,
243  const std::unordered_set<const RelProject*>& projects,
244  const std::unordered_set<const RelProject*>& permutating_projects,
245  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
246  du_web) {
247  if (dynamic_cast<RelLogicalUnion*>(node.get())) {
248  return; // UNION keeps all Projection inputs.
249  }
250  std::shared_ptr<const RelProject> src_project = nullptr;
251  for (size_t i = 0; i < node->inputCount(); ++i) {
252  if (auto project =
253  std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(i))) {
254  if (projects.count(project.get())) {
255  src_project = project;
256  break;
257  }
258  }
259  }
260  if (!src_project) {
261  return;
262  }
263  if (auto join = std::dynamic_pointer_cast<RelJoin>(node)) {
264  auto other_project =
265  src_project == node->getAndOwnInput(0)
266  ? std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(1))
267  : std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(0));
268  join->replaceInput(src_project, src_project->getAndOwnInput(0));
269  RexRebindInputsVisitor rebinder(src_project.get(), src_project->getInput(0));
270  auto usrs_it = du_web.find(join.get());
271  CHECK(usrs_it != du_web.end());
272  for (auto usr : usrs_it->second) {
273  rebinder.visitNode(usr);
274  }
275 
276  if (other_project && projects.count(other_project.get())) {
277  join->replaceInput(other_project, other_project->getAndOwnInput(0));
278  RexRebindInputsVisitor other_rebinder(other_project.get(),
279  other_project->getInput(0));
280  for (auto usr : usrs_it->second) {
281  other_rebinder.visitNode(usr);
282  }
283  }
284  return;
285  }
286  if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
287  project->RelAlgNode::replaceInput(src_project, src_project->getAndOwnInput(0));
288  RexProjectInputRedirector redirector(projects);
289  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
290  for (size_t i = 0; i < project->size(); ++i) {
291  new_exprs.push_back(redirector.visit(project->getProjectAt(i)));
292  }
293  project->setExpressions(new_exprs);
294  return;
295  }
296  if (auto filter = std::dynamic_pointer_cast<RelFilter>(node)) {
297  const bool is_permutating_proj = permutating_projects.count(src_project.get());
298  if (is_permutating_proj || dynamic_cast<const RelJoin*>(src_project->getInput(0))) {
299  if (is_permutating_proj) {
300  propagate_rex_input_renumber(filter.get(), du_web);
301  }
302  filter->RelAlgNode::replaceInput(src_project, src_project->getAndOwnInput(0));
303  RexProjectInputRedirector redirector(projects);
304  auto new_condition = redirector.visit(filter->getCondition());
305  filter->setCondition(new_condition);
306  } else {
307  filter->replaceInput(src_project, src_project->getAndOwnInput(0));
308  }
309  return;
310  }
311  if (std::dynamic_pointer_cast<RelSort>(node)) {
312  auto const src_project_input = src_project->getInput(0);
313  if (dynamic_cast<const RelScan*>(src_project_input) ||
314  dynamic_cast<const RelLogicalValues*>(src_project_input) ||
315  dynamic_cast<const RelLogicalUnion*>(src_project_input)) {
316  return;
317  }
318  }
319  if (std::dynamic_pointer_cast<RelModify>(node)) {
320  return; // NOTE: Review this. Not sure about this.
321  }
322  if (std::dynamic_pointer_cast<RelTableFunction>(node)) {
323  return;
324  }
325  CHECK(std::dynamic_pointer_cast<RelAggregate>(node) ||
326  std::dynamic_pointer_cast<RelSort>(node));
327  node->replaceInput(src_project, src_project->getAndOwnInput(0));
328 }
329 
330 void cleanup_dead_nodes(std::vector<std::shared_ptr<RelAlgNode>>& nodes) {
331  for (auto nodeIt = nodes.rbegin(); nodeIt != nodes.rend(); ++nodeIt) {
332  if (nodeIt->use_count() == 1) {
333  LOG(INFO) << "ID=" << (*nodeIt)->getId() << " " << (*nodeIt)->toString()
334  << " deleted!";
335  nodeIt->reset();
336  }
337  }
338 
339  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
340  for (auto node : nodes) {
341  if (!node) {
342  continue;
343  }
344  new_nodes.push_back(node);
345  }
346  nodes.swap(new_nodes);
347 }
348 
349 std::unordered_set<const RelProject*> get_visible_projects(const RelAlgNode* root) {
350  if (auto project = dynamic_cast<const RelProject*>(root)) {
351  return {project};
352  }
353 
354  if (dynamic_cast<const RelAggregate*>(root) || dynamic_cast<const RelScan*>(root) ||
355  dynamic_cast<const RelLogicalValues*>(root) ||
356  dynamic_cast<const RelModify*>(root)) {
357  return std::unordered_set<const RelProject*>{};
358  }
359 
360  if (auto join = dynamic_cast<const RelJoin*>(root)) {
361  auto lhs_projs = get_visible_projects(join->getInput(0));
362  auto rhs_projs = get_visible_projects(join->getInput(1));
363  lhs_projs.insert(rhs_projs.begin(), rhs_projs.end());
364  return lhs_projs;
365  }
366 
367  if (auto logical_union = dynamic_cast<const RelLogicalUnion*>(root)) {
368  auto projections = get_visible_projects(logical_union->getInput(0));
369  for (size_t i = 1; i < logical_union->inputCount(); ++i) {
370  auto next = get_visible_projects(logical_union->getInput(i));
371  projections.insert(next.begin(), next.end());
372  }
373  return projections;
374  }
375 
376  CHECK(dynamic_cast<const RelFilter*>(root) || dynamic_cast<const RelSort*>(root))
377  << "root = " << root->toString();
378  return get_visible_projects(root->getInput(0));
379 }
380 
381 // TODO(miyu): checking this at runtime is more accurate
382 bool is_distinct(const size_t input_idx, const RelAlgNode* node) {
383  if (dynamic_cast<const RelFilter*>(node) || dynamic_cast<const RelSort*>(node)) {
384  CHECK_EQ(size_t(1), node->inputCount());
385  return is_distinct(input_idx, node->getInput(0));
386  }
387  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
388  CHECK_EQ(size_t(1), node->inputCount());
389  if (aggregate->getGroupByCount() == 1 && !input_idx) {
390  return true;
391  }
392  if (input_idx < aggregate->getGroupByCount()) {
393  return is_distinct(input_idx, node->getInput(0));
394  }
395  return false;
396  }
397  if (auto project = dynamic_cast<const RelProject*>(node)) {
398  CHECK_LT(input_idx, project->size());
399  if (auto input = dynamic_cast<const RexInput*>(project->getProjectAt(input_idx))) {
400  CHECK_EQ(size_t(1), node->inputCount());
401  return is_distinct(input->getIndex(), project->getInput(0));
402  }
403  return false;
404  }
405  CHECK(dynamic_cast<const RelJoin*>(node) || dynamic_cast<const RelScan*>(node));
406  return false;
407 }
408 
409 } // namespace
410 
411 std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>> build_du_web(
412  const std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
413  std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>> web;
414  std::unordered_set<const RelAlgNode*> visited;
415  std::vector<const RelAlgNode*> work_set;
416  for (auto node : nodes) {
417  if (std::dynamic_pointer_cast<RelScan>(node) ||
418  std::dynamic_pointer_cast<RelModify>(node) || visited.count(node.get())) {
419  continue;
420  }
421  work_set.push_back(node.get());
422  while (!work_set.empty()) {
423  auto walker = work_set.back();
424  work_set.pop_back();
425  if (visited.count(walker)) {
426  continue;
427  }
428  CHECK(!web.count(walker));
429  auto it_ok =
430  web.insert(std::make_pair(walker, std::unordered_set<const RelAlgNode*>{}));
431  CHECK(it_ok.second);
432  visited.insert(walker);
433  CHECK(dynamic_cast<const RelJoin*>(walker) ||
434  dynamic_cast<const RelProject*>(walker) ||
435  dynamic_cast<const RelAggregate*>(walker) ||
436  dynamic_cast<const RelFilter*>(walker) ||
437  dynamic_cast<const RelSort*>(walker) ||
438  dynamic_cast<const RelLeftDeepInnerJoin*>(walker) ||
439  dynamic_cast<const RelLogicalValues*>(walker) ||
440  dynamic_cast<const RelTableFunction*>(walker) ||
441  dynamic_cast<const RelLogicalUnion*>(walker));
442  for (size_t i = 0; i < walker->inputCount(); ++i) {
443  auto src = walker->getInput(i);
444  if (dynamic_cast<const RelScan*>(src) || dynamic_cast<const RelModify*>(src)) {
445  continue;
446  }
447  if (web.empty() || !web.count(src)) {
448  web.insert(std::make_pair(src, std::unordered_set<const RelAlgNode*>{}));
449  }
450  web[src].insert(walker);
451  work_set.push_back(src);
452  }
453  }
454  }
455  return web;
456 }
457 
466 bool project_separates_sort(const RelProject* project, const RelAlgNode* next_node) {
467  CHECK(project);
468  if (!next_node) {
469  return false;
470  }
471 
472  auto sort = dynamic_cast<const RelSort*>(next_node);
473  if (!sort) {
474  return false;
475  }
476  if (!(project->inputCount() == 1)) {
477  return false;
478  }
479 
480  if (dynamic_cast<const RelSort*>(project->getInput(0))) {
481  return true;
482  }
483  return false;
484 }
485 
486 // For now, the only target to eliminate is restricted to project-aggregate pair between
487 // scan/sort and join
488 // TODO(miyu): allow more chance if proved safe
489 void eliminate_identical_copy(std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
490  std::unordered_set<std::shared_ptr<const RelAlgNode>> copies;
491  auto sink = nodes.back();
492  for (auto node : nodes) {
493  auto aggregate = std::dynamic_pointer_cast<const RelAggregate>(node);
494  if (!aggregate || aggregate == sink ||
495  !(aggregate->getGroupByCount() == 1 && aggregate->getAggExprsCount() == 0)) {
496  continue;
497  }
498  auto project =
499  std::dynamic_pointer_cast<const RelProject>(aggregate->getAndOwnInput(0));
500  if (project && project->size() == aggregate->size() &&
501  project->getFields() == aggregate->getFields()) {
502  CHECK_EQ(size_t(0), copies.count(aggregate));
503  copies.insert(aggregate);
504  }
505  }
506  for (auto node : nodes) {
507  if (!node->inputCount()) {
508  continue;
509  }
510  auto last_source = node->getAndOwnInput(node->inputCount() - 1);
511  if (!copies.count(last_source)) {
512  continue;
513  }
514  auto aggregate = std::dynamic_pointer_cast<const RelAggregate>(last_source);
515  CHECK(aggregate);
516  if (!std::dynamic_pointer_cast<const RelJoin>(node) || aggregate->size() != 1) {
517  continue;
518  }
519  auto project =
520  std::dynamic_pointer_cast<const RelProject>(aggregate->getAndOwnInput(0));
521  CHECK(project);
522  CHECK_EQ(size_t(1), project->size());
523  if (!is_distinct(size_t(0), project.get())) {
524  continue;
525  }
526  auto new_source = project->getAndOwnInput(0);
527  if (std::dynamic_pointer_cast<const RelSort>(new_source) ||
528  std::dynamic_pointer_cast<const RelScan>(new_source)) {
529  node->replaceInput(last_source, new_source);
530  }
531  }
532  decltype(copies)().swap(copies);
533 
534  auto web = build_du_web(nodes);
535 
536  std::unordered_set<const RelProject*> projects;
537  std::unordered_set<const RelProject*> permutating_projects;
538  auto const visible_projs = get_visible_projects(nodes.back().get());
539  for (auto node_it = nodes.begin(); node_it != nodes.end(); node_it++) {
540  auto node = *node_it;
541  auto project = std::dynamic_pointer_cast<RelProject>(node);
542  auto next_node_it = std::next(node_it);
543  if (project && project->isSimple() &&
544  (!visible_projs.count(project.get()) || !project->isRenaming()) &&
545  is_identical_copy(project.get(), web, projects, permutating_projects) &&
547  project.get(), next_node_it == nodes.end() ? nullptr : next_node_it->get())) {
548  projects.insert(project.get());
549  }
550  }
551 
552  for (auto node : nodes) {
553  redirect_inputs_of(node, projects, permutating_projects, web);
554  }
555 
556  cleanup_dead_nodes(nodes);
557 }
558 
559 namespace {
560 
561 class RexInputCollector : public RexVisitor<std::unordered_set<RexInput>> {
562  private:
564 
565  protected:
566  using RetType = std::unordered_set<RexInput>;
567  RetType aggregateResult(const RetType& aggregate,
568  const RetType& next_result) const override {
569  RetType result(aggregate.begin(), aggregate.end());
570  result.insert(next_result.begin(), next_result.end());
571  return result;
572  }
573 
574  public:
575  RexInputCollector(const RelAlgNode* node) : node_(node) {}
576 
577  RetType visitInput(const RexInput* input) const override {
578  RetType result;
579  if (node_->inputCount() == 1) {
580  auto src = node_->getInput(0);
581  if (auto join = dynamic_cast<const RelJoin*>(src)) {
582  CHECK_EQ(join->inputCount(), size_t(2));
583  const auto src2_in_offset = join->getInput(0)->size();
584  if (input->getSourceNode() == join->getInput(1)) {
585  result.emplace(src, input->getIndex() + src2_in_offset);
586  } else {
587  result.emplace(src, input->getIndex());
588  }
589  return result;
590  }
591  }
592  result.insert(*input);
593  return result;
594  }
595 };
596 
598  CHECK(node->size());
599  RexInputCollector collector(node);
600  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
601  auto rex_ins = collector.visit(filter->getCondition());
602  if (!rex_ins.empty()) {
603  return static_cast<size_t>(rex_ins.begin()->getIndex());
604  }
605  return pick_always_live_col_idx(filter->getInput(0));
606  } else if (auto join = dynamic_cast<const RelJoin*>(node)) {
607  auto rex_ins = collector.visit(join->getCondition());
608  if (!rex_ins.empty()) {
609  return static_cast<size_t>(rex_ins.begin()->getIndex());
610  }
611  if (auto lhs_idx = pick_always_live_col_idx(join->getInput(0))) {
612  return lhs_idx;
613  }
614  if (auto rhs_idx = pick_always_live_col_idx(join->getInput(0))) {
615  return rhs_idx + join->getInput(0)->size();
616  }
617  } else if (auto sort = dynamic_cast<const RelSort*>(node)) {
618  if (sort->collationCount()) {
619  return sort->getCollation(0).getField();
620  }
621  return pick_always_live_col_idx(sort->getInput(0));
622  }
623  return size_t(0);
624 }
625 
626 std::vector<std::unordered_set<size_t>> get_live_ins(
627  const RelAlgNode* node,
628  const std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>>& live_outs) {
629  if (!node || dynamic_cast<const RelScan*>(node)) {
630  return {};
631  }
632  RexInputCollector collector(node);
633  auto it = live_outs.find(node);
634  CHECK(it != live_outs.end());
635  auto live_out = it->second;
636  if (auto project = dynamic_cast<const RelProject*>(node)) {
637  CHECK_EQ(size_t(1), project->inputCount());
638  std::unordered_set<size_t> live_in;
639  for (const auto& idx : live_out) {
640  CHECK_LT(idx, project->size());
641  auto partial_in = collector.visit(project->getProjectAt(idx));
642  for (auto rex_in : partial_in) {
643  live_in.insert(rex_in.getIndex());
644  }
645  }
646  if (project->size() == 1 &&
647  dynamic_cast<const RexLiteral*>(project->getProjectAt(0))) {
648  CHECK(live_in.empty());
649  live_in.insert(pick_always_live_col_idx(project->getInput(0)));
650  }
651  return {live_in};
652  }
653  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
654  CHECK_EQ(size_t(1), aggregate->inputCount());
655  const auto group_key_count = static_cast<size_t>(aggregate->getGroupByCount());
656  const auto agg_expr_count = static_cast<size_t>(aggregate->getAggExprsCount());
657  std::unordered_set<size_t> live_in;
658  for (size_t i = 0; i < group_key_count; ++i) {
659  live_in.insert(i);
660  }
661  bool has_count_star_only{false};
662  for (const auto& idx : live_out) {
663  if (idx < group_key_count) {
664  continue;
665  }
666  const auto agg_idx = idx - group_key_count;
667  CHECK_LT(agg_idx, agg_expr_count);
668  const auto& cur_agg_expr = aggregate->getAggExprs()[agg_idx];
669  const auto n_operands = cur_agg_expr->size();
670  for (size_t i = 0; i < n_operands; ++i) {
671  live_in.insert(static_cast<size_t>(cur_agg_expr->getOperand(i)));
672  }
673  if (n_operands == 0) {
674  has_count_star_only = true;
675  }
676  }
677  if (has_count_star_only && !group_key_count) {
678  live_in.insert(size_t(0));
679  }
680  return {live_in};
681  }
682  if (auto join = dynamic_cast<const RelJoin*>(node)) {
683  std::unordered_set<size_t> lhs_live_ins;
684  std::unordered_set<size_t> rhs_live_ins;
685  CHECK_EQ(size_t(2), join->inputCount());
686  auto lhs = join->getInput(0);
687  auto rhs = join->getInput(1);
688  const auto rhs_idx_base = lhs->size();
689  for (const auto idx : live_out) {
690  if (idx < rhs_idx_base) {
691  lhs_live_ins.insert(idx);
692  } else {
693  rhs_live_ins.insert(idx - rhs_idx_base);
694  }
695  }
696  auto rex_ins = collector.visit(join->getCondition());
697  for (const auto& rex_in : rex_ins) {
698  const auto in_idx = static_cast<size_t>(rex_in.getIndex());
699  if (rex_in.getSourceNode() == lhs) {
700  lhs_live_ins.insert(in_idx);
701  continue;
702  }
703  if (rex_in.getSourceNode() == rhs) {
704  rhs_live_ins.insert(in_idx);
705  continue;
706  }
707  CHECK(false);
708  }
709  return {lhs_live_ins, rhs_live_ins};
710  }
711  if (auto sort = dynamic_cast<const RelSort*>(node)) {
712  CHECK_EQ(size_t(1), sort->inputCount());
713  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
714  for (size_t i = 0; i < sort->collationCount(); ++i) {
715  live_in.insert(sort->getCollation(i).getField());
716  }
717  return {live_in};
718  }
719  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
720  CHECK_EQ(size_t(1), filter->inputCount());
721  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
722  auto rex_ins = collector.visit(filter->getCondition());
723  for (const auto& rex_in : rex_ins) {
724  live_in.insert(static_cast<size_t>(rex_in.getIndex()));
725  }
726  return {live_in};
727  }
728  if (auto table_func = dynamic_cast<const RelTableFunction*>(node)) {
729  const auto input_count = table_func->size();
730  std::unordered_set<size_t> live_in;
731  for (size_t i = 0; i < input_count; i++) {
732  live_in.insert(i);
733  }
734 
735  std::vector<std::unordered_set<size_t>> result;
736  // Is the computed result correct in general?
737  for (size_t i = table_func->inputCount(); i > 0; i--) {
738  result.push_back(live_in);
739  }
740 
741  return result;
742  }
743  if (auto logical_union = dynamic_cast<const RelLogicalUnion*>(node)) {
744  return std::vector<std::unordered_set<size_t>>(logical_union->inputCount(), live_out);
745  }
746  return {};
747 }
748 
749 bool any_dead_col_in(const RelAlgNode* node,
750  const std::unordered_set<size_t>& live_outs) {
751  CHECK(!dynamic_cast<const RelScan*>(node));
752  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
753  for (size_t i = aggregate->getGroupByCount(); i < aggregate->size(); ++i) {
754  if (!live_outs.count(i)) {
755  return true;
756  }
757  }
758  return false;
759  }
760 
761  return node->size() > live_outs.size();
762 }
763 
764 bool does_redef_cols(const RelAlgNode* node) {
765  return dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelProject*>(node);
766 }
767 
769  public:
771  const std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
772  liveouts,
773  const std::unordered_set<const RelAlgNode*>& intact_nodes)
774  : liveouts_(liveouts), intact_nodes_(intact_nodes) {}
775 
776  bool hasAllSrcReady(const RelAlgNode* node) const {
777  for (size_t i = 0; i < node->inputCount(); ++i) {
778  auto src = node->getInput(i);
779  if (!dynamic_cast<const RelScan*>(src) && liveouts_.find(src) == liveouts_.end() &&
780  !intact_nodes_.count(src)) {
781  return false;
782  }
783  }
784  return true;
785  }
786 
787  private:
788  const std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
790  const std::unordered_set<const RelAlgNode*>& intact_nodes_;
791 };
792 
794  const RelAlgNode* node,
795  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
796  new_liveouts,
797  const std::unordered_set<size_t>& old_liveouts,
798  const std::unordered_set<const RelAlgNode*>& intact_nodes,
799  const std::unordered_map<const RelAlgNode*, size_t>& orig_node_sizes) {
800  auto live_fields = old_liveouts;
801  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
802  for (size_t i = 0; i < aggregate->getGroupByCount(); ++i) {
803  live_fields.insert(i);
804  }
805  }
806  auto it_ok =
807  new_liveouts.insert(std::make_pair(node, std::unordered_map<size_t, size_t>{}));
808  CHECK(it_ok.second);
809  auto& new_indices = it_ok.first->second;
810  if (intact_nodes.count(node)) {
811  for (size_t i = 0, e = node->size(); i < e; ++i) {
812  new_indices.insert(std::make_pair(i, i));
813  }
814  return;
815  }
816  if (does_redef_cols(node)) {
817  auto node_sz_it = orig_node_sizes.find(node);
818  CHECK(node_sz_it != orig_node_sizes.end());
819  const auto node_size = node_sz_it->second;
820  CHECK_GT(node_size, live_fields.size());
821  LOG(INFO) << node->toString() << " eliminated " << node_size - live_fields.size()
822  << " columns.";
823  std::vector<size_t> ordered_indices(live_fields.begin(), live_fields.end());
824  std::sort(ordered_indices.begin(), ordered_indices.end());
825  for (size_t i = 0; i < ordered_indices.size(); ++i) {
826  new_indices.insert(std::make_pair(ordered_indices[i], i));
827  }
828  return;
829  }
830  std::vector<size_t> ordered_indices;
831  for (size_t i = 0, old_base = 0, new_base = 0; i < node->inputCount(); ++i) {
832  auto src = node->getInput(i);
833  auto src_renum_it = new_liveouts.find(src);
834  if (src_renum_it != new_liveouts.end()) {
835  for (auto m : src_renum_it->second) {
836  new_indices.insert(std::make_pair(old_base + m.first, new_base + m.second));
837  }
838  new_base += src_renum_it->second.size();
839  } else if (dynamic_cast<const RelScan*>(src) || intact_nodes.count(src)) {
840  for (size_t i = 0; i < src->size(); ++i) {
841  new_indices.insert(std::make_pair(old_base + i, new_base + i));
842  }
843  new_base += src->size();
844  } else {
845  CHECK(false);
846  }
847  auto src_sz_it = orig_node_sizes.find(src);
848  CHECK(src_sz_it != orig_node_sizes.end());
849  old_base += src_sz_it->second;
850  }
851 }
852 
854  public:
856  const std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
857  new_numbering)
858  : node_to_input_renum_(new_numbering) {}
859  RetType visitInput(const RexInput* input) const override {
860  auto source = input->getSourceNode();
861  auto node_it = node_to_input_renum_.find(source);
862  if (node_it != node_to_input_renum_.end()) {
863  auto old_to_new_num = node_it->second;
864  auto renum_it = old_to_new_num.find(input->getIndex());
865  CHECK(renum_it != old_to_new_num.end());
866  return boost::make_unique<RexInput>(source, renum_it->second);
867  }
868  return input->deepCopy();
869  }
870 
871  private:
872  const std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
874 };
875 
876 std::vector<std::unique_ptr<const RexAgg>> renumber_rex_aggs(
877  std::vector<std::unique_ptr<const RexAgg>>& agg_exprs,
878  const std::unordered_map<size_t, size_t>& new_numbering) {
879  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
880  for (auto& expr : agg_exprs) {
881  if (expr->size() >= 1) {
882  auto old_idx = expr->getOperand(0);
883  auto idx_it = new_numbering.find(old_idx);
884  if (idx_it != new_numbering.end()) {
885  std::vector<size_t> operands;
886  operands.push_back(idx_it->second);
887  if (expr->size() == 2) {
888  operands.push_back(expr->getOperand(1));
889  }
890  new_exprs.push_back(boost::make_unique<RexAgg>(
891  expr->getKind(), expr->isDistinct(), expr->getType(), operands));
892  continue;
893  }
894  }
895  new_exprs.push_back(std::move(expr));
896  }
897  return new_exprs;
898 }
899 
901  const std::unordered_map<size_t, size_t>& new_numbering) {
902  auto field_idx = old_field.getField();
903  auto idx_it = new_numbering.find(field_idx);
904  if (idx_it != new_numbering.end()) {
905  field_idx = idx_it->second;
906  }
907  return SortField(field_idx, old_field.getSortDir(), old_field.getNullsPosition());
908 }
909 
910 std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>> mark_live_columns(
911  std::vector<std::shared_ptr<RelAlgNode>>& nodes) {
912  std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>> live_outs;
913  std::vector<const RelAlgNode*> work_set;
914  for (auto node_it = nodes.rbegin(); node_it != nodes.rend(); ++node_it) {
915  auto node = node_it->get();
916  if (dynamic_cast<const RelScan*>(node) || live_outs.count(node) ||
917  dynamic_cast<const RelModify*>(node) ||
918  dynamic_cast<const RelTableFunction*>(node)) {
919  continue;
920  }
921  std::vector<size_t> all_live(node->size());
922  std::iota(all_live.begin(), all_live.end(), size_t(0));
923  live_outs.insert(std::make_pair(
924  node, std::unordered_set<size_t>(all_live.begin(), all_live.end())));
925 
926  work_set.push_back(node);
927  while (!work_set.empty()) {
928  auto walker = work_set.back();
929  work_set.pop_back();
930  CHECK(!dynamic_cast<const RelScan*>(walker));
931  CHECK(live_outs.count(walker));
932  auto live_ins = get_live_ins(walker, live_outs);
933  CHECK_EQ(live_ins.size(), walker->inputCount());
934  for (size_t i = 0; i < walker->inputCount(); ++i) {
935  auto src = walker->getInput(i);
936  if (dynamic_cast<const RelScan*>(src) ||
937  dynamic_cast<const RelTableFunction*>(src) || live_ins[i].empty()) {
938  continue;
939  }
940  if (!live_outs.count(src)) {
941  live_outs.insert(std::make_pair(src, std::unordered_set<size_t>{}));
942  }
943  auto src_it = live_outs.find(src);
944  CHECK(src_it != live_outs.end());
945  auto& live_out = src_it->second;
946  bool changed = false;
947  if (!live_out.empty()) {
948  live_out.insert(live_ins[i].begin(), live_ins[i].end());
949  changed = true;
950  } else {
951  for (int idx : live_ins[i]) {
952  changed |= live_out.insert(idx).second;
953  }
954  }
955  if (changed) {
956  work_set.push_back(src);
957  }
958  }
959  }
960  }
961  return live_outs;
962 }
963 
964 std::string get_field_name(const RelAlgNode* node, size_t index) {
965  CHECK_LT(index, node->size());
966  if (auto scan = dynamic_cast<const RelScan*>(node)) {
967  return scan->getFieldName(index);
968  }
969  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
970  CHECK_EQ(aggregate->size(), aggregate->getFields().size());
971  return aggregate->getFieldName(index);
972  }
973  if (auto join = dynamic_cast<const RelJoin*>(node)) {
974  const auto lhs_size = join->getInput(0)->size();
975  if (index < lhs_size) {
976  return get_field_name(join->getInput(0), index);
977  }
978  return get_field_name(join->getInput(1), index - lhs_size);
979  }
980  if (auto project = dynamic_cast<const RelProject*>(node)) {
981  return project->getFieldName(index);
982  }
983  CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node));
984  return get_field_name(node->getInput(0), index);
985 }
986 
988  std::vector<std::shared_ptr<RelAlgNode>>& nodes,
989  std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>>& liveouts,
990  std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
991  du_web) {
992  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
993  for (auto node : nodes) {
994  new_nodes.push_back(node);
995  if (!std::dynamic_pointer_cast<RelFilter>(node)) {
996  continue;
997  }
998  const auto filter = node.get();
999  auto liveout_it = liveouts.find(filter);
1000  CHECK(liveout_it != liveouts.end());
1001  auto& outs = liveout_it->second;
1002  if (!any_dead_col_in(filter, outs)) {
1003  continue;
1004  }
1005  auto usrs_it = du_web.find(filter);
1006  CHECK(usrs_it != du_web.end());
1007  auto& usrs = usrs_it->second;
1008  if (usrs.size() != 1 || does_redef_cols(*usrs.begin())) {
1009  continue;
1010  }
1011  auto only_usr = const_cast<RelAlgNode*>(*usrs.begin());
1012 
1013  std::vector<std::unique_ptr<const RexScalar>> exprs;
1014  std::vector<std::string> fields;
1015  for (size_t i = 0; i < filter->size(); ++i) {
1016  exprs.push_back(boost::make_unique<RexInput>(filter, i));
1017  fields.push_back(get_field_name(filter, i));
1018  }
1019  auto project_owner = std::make_shared<RelProject>(exprs, fields, node);
1020  auto project = project_owner.get();
1021 
1022  only_usr->replaceInput(node, project_owner);
1023  if (dynamic_cast<const RelJoin*>(only_usr)) {
1024  RexRebindInputsVisitor visitor(filter, project);
1025  for (auto usr : du_web[only_usr]) {
1026  visitor.visitNode(usr);
1027  }
1028  }
1029 
1030  liveouts.insert(std::make_pair(project, outs));
1031 
1032  usrs.clear();
1033  usrs.insert(project);
1034  du_web.insert(
1035  std::make_pair(project, std::unordered_set<const RelAlgNode*>{only_usr}));
1036 
1037  new_nodes.push_back(project_owner);
1038  }
1039  if (new_nodes.size() > nodes.size()) {
1040  nodes.swap(new_nodes);
1041  }
1042 }
1043 
1044 std::pair<std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>,
1045  std::vector<const RelAlgNode*>>
1047  const std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>>& live_outs,
1048  const std::vector<std::shared_ptr<RelAlgNode>>& nodes,
1049  const std::unordered_set<const RelAlgNode*>& intact_nodes,
1050  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
1051  du_web,
1052  const std::unordered_map<const RelAlgNode*, size_t>& orig_node_sizes) {
1053  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>
1054  liveouts_renumbering;
1055  std::vector<const RelAlgNode*> ready_nodes;
1056  AvailabilityChecker checker(liveouts_renumbering, intact_nodes);
1057  for (auto node : nodes) {
1058  // Ignore empty live_out due to some invalid node
1059  if (!does_redef_cols(node.get()) || intact_nodes.count(node.get())) {
1060  continue;
1061  }
1062  auto live_pair = live_outs.find(node.get());
1063  CHECK(live_pair != live_outs.end());
1064  auto old_live_outs = live_pair->second;
1066  node.get(), liveouts_renumbering, old_live_outs, intact_nodes, orig_node_sizes);
1067  if (auto aggregate = std::dynamic_pointer_cast<RelAggregate>(node)) {
1068  auto old_exprs = aggregate->getAggExprsAndRelease();
1069  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
1070  auto key_name_it = aggregate->getFields().begin();
1071  std::vector<std::string> new_fields(key_name_it,
1072  key_name_it + aggregate->getGroupByCount());
1073  for (size_t i = aggregate->getGroupByCount(), j = 0;
1074  i < aggregate->getFields().size() && j < old_exprs.size();
1075  ++i, ++j) {
1076  if (old_live_outs.count(i)) {
1077  new_exprs.push_back(std::move(old_exprs[j]));
1078  new_fields.push_back(aggregate->getFieldName(i));
1079  }
1080  }
1081  aggregate->setAggExprs(new_exprs);
1082  aggregate->setFields(new_fields);
1083  } else if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
1084  auto old_exprs = project->getExpressionsAndRelease();
1085  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1086  std::vector<std::string> new_fields;
1087  for (size_t i = 0; i < old_exprs.size(); ++i) {
1088  if (old_live_outs.count(i)) {
1089  new_exprs.push_back(std::move(old_exprs[i]));
1090  new_fields.push_back(project->getFieldName(i));
1091  }
1092  }
1093  project->setExpressions(new_exprs);
1094  project->setFields(new_fields);
1095  } else {
1096  CHECK(false);
1097  }
1098  auto usrs_it = du_web.find(node.get());
1099  CHECK(usrs_it != du_web.end());
1100  for (auto usr : usrs_it->second) {
1101  if (checker.hasAllSrcReady(usr)) {
1102  ready_nodes.push_back(usr);
1103  }
1104  }
1105  }
1106  return {liveouts_renumbering, ready_nodes};
1107 }
1108 
1110  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>&
1111  liveout_renumbering,
1112  const std::vector<const RelAlgNode*>& ready_nodes,
1113  const std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>>& old_liveouts,
1114  const std::unordered_set<const RelAlgNode*>& intact_nodes,
1115  const std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
1116  du_web,
1117  const std::unordered_map<const RelAlgNode*, size_t>& orig_node_sizes) {
1118  RexInputRenumberVisitor renumberer(liveout_renumbering);
1119  AvailabilityChecker checker(liveout_renumbering, intact_nodes);
1120  std::deque<const RelAlgNode*> work_set(ready_nodes.begin(), ready_nodes.end());
1121  while (!work_set.empty()) {
1122  auto walker = work_set.front();
1123  work_set.pop_front();
1124  CHECK(!dynamic_cast<const RelScan*>(walker));
1125  auto node = const_cast<RelAlgNode*>(walker);
1126  if (auto project = dynamic_cast<RelProject*>(node)) {
1127  auto old_exprs = project->getExpressionsAndRelease();
1128  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1129  for (auto& expr : old_exprs) {
1130  new_exprs.push_back(renumberer.visit(expr.get()));
1131  }
1132  project->setExpressions(new_exprs);
1133  } else if (auto aggregate = dynamic_cast<RelAggregate*>(node)) {
1134  auto src_it = liveout_renumbering.find(node->getInput(0));
1135  CHECK(src_it != liveout_renumbering.end());
1136  auto old_exprs = aggregate->getAggExprsAndRelease();
1137  auto new_exprs = renumber_rex_aggs(old_exprs, src_it->second);
1138  aggregate->setAggExprs(new_exprs);
1139  } else if (auto join = dynamic_cast<RelJoin*>(node)) {
1140  auto new_condition = renumberer.visit(join->getCondition());
1141  join->setCondition(new_condition);
1142  } else if (auto filter = dynamic_cast<RelFilter*>(node)) {
1143  auto new_condition = renumberer.visit(filter->getCondition());
1144  filter->setCondition(new_condition);
1145  } else if (auto sort = dynamic_cast<RelSort*>(node)) {
1146  auto src_it = liveout_renumbering.find(node->getInput(0));
1147  CHECK(src_it != liveout_renumbering.end());
1148  std::vector<SortField> new_collations;
1149  for (size_t i = 0; i < sort->collationCount(); ++i) {
1150  new_collations.push_back(
1151  renumber_sort_field(sort->getCollation(i), src_it->second));
1152  }
1153  sort->setCollation(std::move(new_collations));
1154  } else if (!dynamic_cast<RelLogicalUnion*>(node)) {
1155  LOG(FATAL) << "Unhandled node type: " << node->toString();
1156  }
1157 
1158  // Ignore empty live_out due to some invalid node
1159  if (does_redef_cols(node) || intact_nodes.count(node)) {
1160  continue;
1161  }
1162  auto live_pair = old_liveouts.find(node);
1163  CHECK(live_pair != old_liveouts.end());
1164  auto live_out = live_pair->second;
1166  node, liveout_renumbering, live_out, intact_nodes, orig_node_sizes);
1167  auto usrs_it = du_web.find(walker);
1168  CHECK(usrs_it != du_web.end());
1169  for (auto usr : usrs_it->second) {
1170  if (checker.hasAllSrcReady(usr)) {
1171  work_set.push_back(usr);
1172  }
1173  }
1174  }
1175 }
1176 
1177 } // namespace
1178 
1179 void eliminate_dead_columns(std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1180  if (nodes.empty()) {
1181  return;
1182  }
1183  auto root = nodes.back().get();
1184  if (!root) {
1185  return;
1186  }
1187  CHECK(!dynamic_cast<const RelScan*>(root) && !dynamic_cast<const RelJoin*>(root));
1188  // Mark
1189  auto old_liveouts = mark_live_columns(nodes);
1190  std::unordered_set<const RelAlgNode*> intact_nodes;
1191  bool has_dead_cols = false;
1192  for (auto live_pair : old_liveouts) {
1193  auto node = live_pair.first;
1194  const auto& outs = live_pair.second;
1195  if (outs.empty()) {
1196  LOG(WARNING) << "RA node with no used column: " << node->toString();
1197  // Ignore empty live_out due to some invalid node
1198  intact_nodes.insert(node);
1199  }
1200  if (any_dead_col_in(node, outs)) {
1201  has_dead_cols = true;
1202  } else {
1203  intact_nodes.insert(node);
1204  }
1205  }
1206  if (!has_dead_cols) {
1207  return;
1208  }
1209  auto web = build_du_web(nodes);
1210  try_insert_coalesceable_proj(nodes, old_liveouts, web);
1211 
1212  for (auto node : nodes) {
1213  if (intact_nodes.count(node.get()) || does_redef_cols(node.get())) {
1214  continue;
1215  }
1216  bool intact = true;
1217  for (size_t i = 0; i < node->inputCount(); ++i) {
1218  auto source = node->getInput(i);
1219  if (!dynamic_cast<const RelScan*>(source) && !intact_nodes.count(source)) {
1220  intact = false;
1221  break;
1222  }
1223  }
1224  if (intact) {
1225  intact_nodes.insert(node.get());
1226  }
1227  }
1228 
1229  std::unordered_map<const RelAlgNode*, size_t> orig_node_sizes;
1230  for (auto node : nodes) {
1231  orig_node_sizes.insert(std::make_pair(node.get(), node->size()));
1232  }
1233  // Sweep
1234  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>
1235  liveout_renumbering;
1236  std::vector<const RelAlgNode*> ready_nodes;
1237  std::tie(liveout_renumbering, ready_nodes) =
1238  sweep_dead_columns(old_liveouts, nodes, intact_nodes, web, orig_node_sizes);
1239  // Propagate
1241  liveout_renumbering, ready_nodes, old_liveouts, intact_nodes, web, orig_node_sizes);
1242 }
1243 
1244 void eliminate_dead_subqueries(std::vector<std::shared_ptr<RexSubQuery>>& subqueries,
1245  RelAlgNode const* root) {
1246  if (!subqueries.empty()) {
1247  auto live_ids = RexSubQueryIdCollector::getLiveRexSubQueryIds(root);
1248  auto sort_live_ids_first = [&live_ids](auto& a, auto& b) {
1249  return live_ids.count(a->getId()) && !live_ids.count(b->getId());
1250  };
1251  std::stable_sort(subqueries.begin(), subqueries.end(), sort_live_ids_first);
1252  size_t n_dead_subqueries;
1253  if (live_ids.count(subqueries.front()->getId())) {
1254  auto first_dead_itr = std::upper_bound(subqueries.cbegin(),
1255  subqueries.cend(),
1256  subqueries.front(),
1257  sort_live_ids_first);
1258  n_dead_subqueries = subqueries.cend() - first_dead_itr;
1259  } else {
1260  n_dead_subqueries = subqueries.size();
1261  }
1262  if (n_dead_subqueries) {
1263  VLOG(1) << "Eliminating " << n_dead_subqueries
1264  << (n_dead_subqueries == 1 ? " subquery." : " subqueries.");
1265  subqueries.resize(subqueries.size() - n_dead_subqueries);
1266  subqueries.shrink_to_fit();
1267  }
1268  }
1269 }
1270 
1271 namespace {
1272 
1274  public:
1275  RexInputSinker(const std::unordered_map<size_t, size_t>& old_to_new_idx,
1276  const RelAlgNode* new_src)
1277  : old_to_new_in_idx_(old_to_new_idx), target_(new_src) {}
1278 
1279  RetType visitInput(const RexInput* input) const override {
1280  CHECK_EQ(target_->inputCount(), size_t(1));
1281  CHECK_EQ(target_->getInput(0), input->getSourceNode());
1282  auto idx_it = old_to_new_in_idx_.find(input->getIndex());
1283  CHECK(idx_it != old_to_new_in_idx_.end());
1284  return boost::make_unique<RexInput>(target_, idx_it->second);
1285  }
1286 
1287  private:
1288  const std::unordered_map<size_t, size_t>& old_to_new_in_idx_;
1290 };
1291 
1293  public:
1294  SubConditionReplacer(const std::unordered_map<size_t, std::unique_ptr<const RexScalar>>&
1295  idx_to_sub_condition)
1296  : idx_to_subcond_(idx_to_sub_condition) {}
1297  RetType visitInput(const RexInput* input) const override {
1298  auto subcond_it = idx_to_subcond_.find(input->getIndex());
1299  if (subcond_it != idx_to_subcond_.end()) {
1300  return RexDeepCopyVisitor::visit(subcond_it->second.get());
1301  }
1302  return RexDeepCopyVisitor::visitInput(input);
1303  }
1304 
1305  private:
1306  const std::unordered_map<size_t, std::unique_ptr<const RexScalar>>& idx_to_subcond_;
1307 };
1308 
1309 } // namespace
1310 
1312  std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1313  auto web = build_du_web(nodes);
1314  auto liveouts = mark_live_columns(nodes);
1315  for (auto node : nodes) {
1316  auto project = std::dynamic_pointer_cast<RelProject>(node);
1317  // TODO(miyu): relax RelScan limitation
1318  if (!project || project->isSimple() ||
1319  !dynamic_cast<const RelScan*>(project->getInput(0))) {
1320  continue;
1321  }
1322  auto usrs_it = web.find(project.get());
1323  CHECK(usrs_it != web.end());
1324  auto& usrs = usrs_it->second;
1325  if (usrs.size() != 1) {
1326  continue;
1327  }
1328  auto join = dynamic_cast<RelJoin*>(const_cast<RelAlgNode*>(*usrs.begin()));
1329  if (!join) {
1330  continue;
1331  }
1332  auto outs_it = liveouts.find(join);
1333  CHECK(outs_it != liveouts.end());
1334  std::unordered_map<size_t, size_t> in_to_out_index;
1335  std::unordered_set<size_t> boolean_expr_indicies;
1336  bool discarded = false;
1337  for (size_t i = 0; i < project->size(); ++i) {
1338  auto oper = dynamic_cast<const RexOperator*>(project->getProjectAt(i));
1339  if (oper && oper->getType().get_type() == kBOOLEAN) {
1340  boolean_expr_indicies.insert(i);
1341  } else {
1342  // TODO(miyu): relax?
1343  if (auto input = dynamic_cast<const RexInput*>(project->getProjectAt(i))) {
1344  in_to_out_index.insert(std::make_pair(input->getIndex(), i));
1345  } else {
1346  discarded = true;
1347  }
1348  }
1349  }
1350  if (discarded || boolean_expr_indicies.empty()) {
1351  continue;
1352  }
1353  const size_t index_base =
1354  join->getInput(0) == project.get() ? 0 : join->getInput(0)->size();
1355  for (auto i : boolean_expr_indicies) {
1356  auto join_idx = index_base + i;
1357  if (outs_it->second.count(join_idx)) {
1358  discarded = true;
1359  break;
1360  }
1361  }
1362  if (discarded) {
1363  continue;
1364  }
1365  RexInputCollector collector(project.get());
1366  std::vector<size_t> unloaded_input_indices;
1367  std::unordered_map<size_t, std::unique_ptr<const RexScalar>> in_idx_to_new_subcond;
1368  // Given all are dead right after join, safe to sink
1369  for (auto i : boolean_expr_indicies) {
1370  auto rex_ins = collector.visit(project->getProjectAt(i));
1371  for (auto& in : rex_ins) {
1372  CHECK_EQ(in.getSourceNode(), project->getInput(0));
1373  if (!in_to_out_index.count(in.getIndex())) {
1374  auto curr_out_index = project->size() + unloaded_input_indices.size();
1375  in_to_out_index.insert(std::make_pair(in.getIndex(), curr_out_index));
1376  unloaded_input_indices.push_back(in.getIndex());
1377  }
1378  RexInputSinker sinker(in_to_out_index, project.get());
1379  in_idx_to_new_subcond.insert(
1380  std::make_pair(i, sinker.visit(project->getProjectAt(i))));
1381  }
1382  }
1383  if (in_idx_to_new_subcond.empty()) {
1384  continue;
1385  }
1386  std::vector<std::unique_ptr<const RexScalar>> new_projections;
1387  for (size_t i = 0; i < project->size(); ++i) {
1388  if (boolean_expr_indicies.count(i)) {
1389  new_projections.push_back(boost::make_unique<RexInput>(project->getInput(0), 0));
1390  } else {
1391  auto rex_input = dynamic_cast<const RexInput*>(project->getProjectAt(i));
1392  CHECK(rex_input != nullptr);
1393  new_projections.push_back(rex_input->deepCopy());
1394  }
1395  }
1396  for (auto i : unloaded_input_indices) {
1397  new_projections.push_back(boost::make_unique<RexInput>(project->getInput(0), i));
1398  }
1399  project->setExpressions(new_projections);
1400 
1401  SubConditionReplacer replacer(in_idx_to_new_subcond);
1402  auto new_condition = replacer.visit(join->getCondition());
1403  join->setCondition(new_condition);
1404  }
1405 }
1406 
1407 namespace {
1408 
1410  public:
1411  RexInputRedirector(const RelAlgNode* old_src, const RelAlgNode* new_src)
1412  : old_src_(old_src), new_src_(new_src) {}
1413 
1414  RetType visitInput(const RexInput* input) const override {
1415  CHECK_EQ(old_src_, input->getSourceNode());
1416  CHECK_NE(old_src_, new_src_);
1417  auto actual_new_src = new_src_;
1418  if (auto join = dynamic_cast<const RelJoin*>(new_src_)) {
1419  actual_new_src = join->getInput(0);
1420  CHECK_EQ(join->inputCount(), size_t(2));
1421  auto src2_input_base = actual_new_src->size();
1422  if (input->getIndex() >= src2_input_base) {
1423  actual_new_src = join->getInput(1);
1424  return boost::make_unique<RexInput>(actual_new_src,
1425  input->getIndex() - src2_input_base);
1426  }
1427  }
1428 
1429  return boost::make_unique<RexInput>(actual_new_src, input->getIndex());
1430  }
1431 
1432  private:
1435 };
1436 
1438  std::shared_ptr<const RelAlgNode> old_def_node,
1439  std::shared_ptr<const RelAlgNode> new_def_node,
1440  std::unordered_map<const RelAlgNode*, std::shared_ptr<RelAlgNode>>& deconst_mapping,
1441  std::unordered_map<const RelAlgNode*, std::unordered_set<const RelAlgNode*>>&
1442  du_web) {
1443  auto usrs_it = du_web.find(old_def_node.get());
1444  RexInputRedirector redirector(new_def_node.get(), old_def_node.get());
1445  CHECK(usrs_it != du_web.end());
1446  for (auto usr : usrs_it->second) {
1447  auto usr_it = deconst_mapping.find(usr);
1448  CHECK(usr_it != deconst_mapping.end());
1449  usr_it->second->replaceInput(old_def_node, new_def_node);
1450  }
1451  auto new_usrs_it = du_web.find(new_def_node.get());
1452  CHECK(new_usrs_it != du_web.end());
1453  new_usrs_it->second.insert(usrs_it->second.begin(), usrs_it->second.end());
1454  usrs_it->second.clear();
1455 }
1456 
1457 } // namespace
1458 
1459 void fold_filters(std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1460  std::unordered_map<const RelAlgNode*, std::shared_ptr<RelAlgNode>> deconst_mapping;
1461  for (auto node : nodes) {
1462  deconst_mapping.insert(std::make_pair(node.get(), node));
1463  }
1464 
1465  auto web = build_du_web(nodes);
1466  for (auto node_it = nodes.rbegin(); node_it != nodes.rend(); ++node_it) {
1467  auto& node = *node_it;
1468  if (auto filter = std::dynamic_pointer_cast<RelFilter>(node)) {
1469  CHECK_EQ(filter->inputCount(), size_t(1));
1470  auto src_filter = dynamic_cast<const RelFilter*>(filter->getInput(0));
1471  if (!src_filter) {
1472  continue;
1473  }
1474  auto siblings_it = web.find(src_filter);
1475  if (siblings_it == web.end() || siblings_it->second.size() != size_t(1)) {
1476  continue;
1477  }
1478  auto src_it = deconst_mapping.find(src_filter);
1479  CHECK(src_it != deconst_mapping.end());
1480  auto folded_filter = std::dynamic_pointer_cast<RelFilter>(src_it->second);
1481  CHECK(folded_filter);
1482  // TODO(miyu) : drop filter w/ only expression valued constant TRUE?
1483  if (auto rex_operator = dynamic_cast<const RexOperator*>(filter->getCondition())) {
1484  LOG(INFO) << "ID=" << filter->getId() << " " << filter->toString()
1485  << " folded into "
1486  << "ID=" << folded_filter->getId() << " " << folded_filter->toString()
1487  << std::endl;
1488  std::vector<std::unique_ptr<const RexScalar>> operands;
1489  operands.emplace_back(folded_filter->getAndReleaseCondition());
1490  auto old_condition = dynamic_cast<const RexOperator*>(operands.back().get());
1491  CHECK(old_condition && old_condition->getType().get_type() == kBOOLEAN);
1492  RexInputRedirector redirector(folded_filter.get(), folded_filter->getInput(0));
1493  operands.push_back(redirector.visit(rex_operator));
1494  auto other_condition = dynamic_cast<const RexOperator*>(operands.back().get());
1495  CHECK(other_condition && other_condition->getType().get_type() == kBOOLEAN);
1496  const bool notnull = old_condition->getType().get_notnull() &&
1497  other_condition->getType().get_notnull();
1498  auto new_condition = std::unique_ptr<const RexScalar>(
1499  new RexOperator(kAND, operands, SQLTypeInfo(kBOOLEAN, notnull)));
1500  folded_filter->setCondition(new_condition);
1501  replace_all_usages(filter, folded_filter, deconst_mapping, web);
1502  deconst_mapping.erase(filter.get());
1503  web.erase(filter.get());
1504  web[filter->getInput(0)].erase(filter.get());
1505  node.reset();
1506  }
1507  }
1508  }
1509 
1510  if (!nodes.empty()) {
1511  auto sink = nodes.back();
1512  for (auto node_it = std::next(nodes.rbegin()); node_it != nodes.rend(); ++node_it) {
1513  if (sink) {
1514  break;
1515  }
1516  sink = *node_it;
1517  }
1518  CHECK(sink);
1519  cleanup_dead_nodes(nodes);
1520  }
1521 }
1522 
1523 std::vector<const RexScalar*> find_hoistable_conditions(const RexScalar* condition,
1524  const RelAlgNode* source,
1525  const size_t first_col_idx,
1526  const size_t last_col_idx) {
1527  if (auto rex_op = dynamic_cast<const RexOperator*>(condition)) {
1528  switch (rex_op->getOperator()) {
1529  case kAND: {
1530  std::vector<const RexScalar*> subconditions;
1531  size_t complete_subcond_count = 0;
1532  for (size_t i = 0; i < rex_op->size(); ++i) {
1533  auto conds = find_hoistable_conditions(
1534  rex_op->getOperand(i), source, first_col_idx, last_col_idx);
1535  if (conds.size() == size_t(1)) {
1536  ++complete_subcond_count;
1537  }
1538  subconditions.insert(subconditions.end(), conds.begin(), conds.end());
1539  }
1540  if (complete_subcond_count == rex_op->size()) {
1541  return {rex_op};
1542  } else {
1543  return {subconditions};
1544  }
1545  break;
1546  }
1547  case kEQ: {
1548  const auto lhs_conds = find_hoistable_conditions(
1549  rex_op->getOperand(0), source, first_col_idx, last_col_idx);
1550  const auto rhs_conds = find_hoistable_conditions(
1551  rex_op->getOperand(1), source, first_col_idx, last_col_idx);
1552  const auto lhs_in = lhs_conds.size() == 1
1553  ? dynamic_cast<const RexInput*>(*lhs_conds.begin())
1554  : nullptr;
1555  const auto rhs_in = rhs_conds.size() == 1
1556  ? dynamic_cast<const RexInput*>(*rhs_conds.begin())
1557  : nullptr;
1558  if (lhs_in && rhs_in) {
1559  return {rex_op};
1560  }
1561  return {};
1562  break;
1563  }
1564  default:
1565  break;
1566  }
1567  return {};
1568  }
1569  if (auto rex_in = dynamic_cast<const RexInput*>(condition)) {
1570  if (rex_in->getSourceNode() == source) {
1571  const auto col_idx = rex_in->getIndex();
1572  return {col_idx >= first_col_idx && col_idx <= last_col_idx ? condition : nullptr};
1573  }
1574  return {};
1575  }
1576  return {};
1577 }
1578 
1580  public:
1581  JoinTargetRebaser(const RelJoin* join, const unsigned old_base)
1582  : join_(join)
1583  , old_base_(old_base)
1584  , src1_base_(join->getInput(0)->size())
1585  , target_count_(join->size()) {}
1586  RetType visitInput(const RexInput* input) const override {
1587  auto curr_idx = input->getIndex();
1588  CHECK_GE(curr_idx, old_base_);
1589  CHECK_LT(static_cast<size_t>(curr_idx), target_count_);
1590  curr_idx -= old_base_;
1591  if (curr_idx >= src1_base_) {
1592  return boost::make_unique<RexInput>(join_->getInput(1), curr_idx - src1_base_);
1593  } else {
1594  return boost::make_unique<RexInput>(join_->getInput(0), curr_idx);
1595  }
1596  }
1597 
1598  private:
1599  const RelJoin* join_;
1600  const unsigned old_base_;
1601  const size_t src1_base_;
1602  const size_t target_count_;
1603 };
1604 
1606  public:
1607  SubConditionRemover(const std::vector<const RexScalar*> sub_conds)
1608  : sub_conditions_(sub_conds.begin(), sub_conds.end()) {}
1609  RetType visitOperator(const RexOperator* rex_operator) const override {
1610  if (sub_conditions_.count(rex_operator)) {
1611  return boost::make_unique<RexLiteral>(
1612  true, kBOOLEAN, kBOOLEAN, unsigned(-2147483648), 1, unsigned(-2147483648), 1);
1613  }
1614  return RexDeepCopyVisitor::visitOperator(rex_operator);
1615  }
1616 
1617  private:
1618  std::unordered_set<const RexScalar*> sub_conditions_;
1619 };
1620 
1622  std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1623  std::unordered_set<const RelAlgNode*> visited;
1624  auto web = build_du_web(nodes);
1625  for (auto node : nodes) {
1626  if (visited.count(node.get())) {
1627  continue;
1628  }
1629  visited.insert(node.get());
1630  auto join = dynamic_cast<RelJoin*>(node.get());
1631  if (join && join->getJoinType() == JoinType::INNER) {
1632  // Only allow cross join for now.
1633  if (auto literal = dynamic_cast<const RexLiteral*>(join->getCondition())) {
1634  // Assume Calcite always generates an inner join on constant boolean true for
1635  // cross join.
1636  CHECK(literal->getType() == kBOOLEAN && literal->getVal<bool>());
1637  size_t first_col_idx = 0;
1638  const RelFilter* filter = nullptr;
1639  std::vector<const RelJoin*> join_seq{join};
1640  for (const RelJoin* curr_join = join; !filter;) {
1641  auto usrs_it = web.find(curr_join);
1642  CHECK(usrs_it != web.end());
1643  if (usrs_it->second.size() != size_t(1)) {
1644  break;
1645  }
1646  auto only_usr = *usrs_it->second.begin();
1647  if (auto usr_join = dynamic_cast<const RelJoin*>(only_usr)) {
1648  if (join == usr_join->getInput(1)) {
1649  const auto src1_offset = usr_join->getInput(0)->size();
1650  first_col_idx += src1_offset;
1651  }
1652  join_seq.push_back(usr_join);
1653  curr_join = usr_join;
1654  continue;
1655  }
1656 
1657  filter = dynamic_cast<const RelFilter*>(only_usr);
1658  break;
1659  }
1660  if (!filter) {
1661  visited.insert(join_seq.begin(), join_seq.end());
1662  continue;
1663  }
1664  const auto src_join = dynamic_cast<const RelJoin*>(filter->getInput(0));
1665  CHECK(src_join);
1666  auto modified_filter = const_cast<RelFilter*>(filter);
1667 
1668  if (src_join == join) {
1669  std::unique_ptr<const RexScalar> filter_condition(
1670  modified_filter->getAndReleaseCondition());
1671  std::unique_ptr<const RexScalar> true_condition =
1672  boost::make_unique<RexLiteral>(true,
1673  kBOOLEAN,
1674  kBOOLEAN,
1675  unsigned(-2147483648),
1676  1,
1677  unsigned(-2147483648),
1678  1);
1679  modified_filter->setCondition(true_condition);
1680  join->setCondition(filter_condition);
1681  continue;
1682  }
1683  const auto src1_base = src_join->getInput(0)->size();
1684  auto source =
1685  first_col_idx < src1_base ? src_join->getInput(0) : src_join->getInput(1);
1686  first_col_idx =
1687  first_col_idx < src1_base ? first_col_idx : first_col_idx - src1_base;
1688  auto join_conditions =
1690  source,
1691  first_col_idx,
1692  first_col_idx + join->size() - 1);
1693  if (join_conditions.empty()) {
1694  continue;
1695  }
1696 
1697  JoinTargetRebaser rebaser(join, first_col_idx);
1698  if (join_conditions.size() == 1) {
1699  auto new_join_condition = rebaser.visit(*join_conditions.begin());
1700  join->setCondition(new_join_condition);
1701  } else {
1702  std::vector<std::unique_ptr<const RexScalar>> operands;
1703  bool notnull = true;
1704  for (size_t i = 0; i < join_conditions.size(); ++i) {
1705  operands.emplace_back(rebaser.visit(join_conditions[i]));
1706  auto old_subcond = dynamic_cast<const RexOperator*>(join_conditions[i]);
1707  CHECK(old_subcond && old_subcond->getType().get_type() == kBOOLEAN);
1708  notnull = notnull && old_subcond->getType().get_notnull();
1709  }
1710  auto new_join_condition = std::unique_ptr<const RexScalar>(
1711  new RexOperator(kAND, operands, SQLTypeInfo(kBOOLEAN, notnull)));
1712  join->setCondition(new_join_condition);
1713  }
1714 
1715  SubConditionRemover remover(join_conditions);
1716  auto new_filter_condition = remover.visit(filter->getCondition());
1717  modified_filter->setCondition(new_filter_condition);
1718  }
1719  }
1720  }
1721 }
1722 
1723 // For some reason, Calcite generates Sort, Project, Sort sequences where the
1724 // two Sort nodes are identical and the Project is identity. Simplify this
1725 // pattern by re-binding the input of the second sort to the input of the first.
1726 void simplify_sort(std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1727  if (nodes.size() < 3) {
1728  return;
1729  }
1730  for (size_t i = 0; i <= nodes.size() - 3;) {
1731  auto first_sort = std::dynamic_pointer_cast<RelSort>(nodes[i]);
1732  const auto project = std::dynamic_pointer_cast<const RelProject>(nodes[i + 1]);
1733  auto second_sort = std::dynamic_pointer_cast<RelSort>(nodes[i + 2]);
1734  if (first_sort && second_sort && project && project->isIdentity() &&
1735  *first_sort == *second_sort) {
1736  second_sort->replaceInput(second_sort->getAndOwnInput(0),
1737  first_sort->getAndOwnInput(0));
1738  nodes[i].reset();
1739  nodes[i + 1].reset();
1740  i += 3;
1741  } else {
1742  ++i;
1743  }
1744  }
1745 
1746  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
1747  for (auto node : nodes) {
1748  if (!node) {
1749  continue;
1750  }
1751  new_nodes.push_back(node);
1752  }
1753  nodes.swap(new_nodes);
1754 }
DEVICE auto upper_bound(ARGS &&...args)
Definition: gpu_enabled.h:123
bool is_identical_copy(const RelProject *project, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web, const std::unordered_set< const RelProject * > &projects_to_remove, std::unordered_set< const RelProject * > &permutating_projects)
SubConditionReplacer(const std::unordered_map< size_t, std::unique_ptr< const RexScalar >> &idx_to_sub_condition)
std::vector< const RexScalar * > find_hoistable_conditions(const RexScalar *condition, const RelAlgNode *source, const size_t first_col_idx, const size_t last_col_idx)
std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * > > build_du_web(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
RetType visitInput(const RexInput *input) const override
#define CHECK_EQ(x, y)
Definition: Logger.h:211
const size_t target_count_
RexInputRedirector(const RelAlgNode *old_src, const RelAlgNode *new_src)
size_t get_actual_source_size(const RelProject *curr_project, const std::unordered_set< const RelProject * > &projects_to_remove)
size_t pick_always_live_col_idx(const RelAlgNode *node)
void redirect_inputs_of(std::shared_ptr< RelAlgNode > node, const std::unordered_set< const RelProject * > &projects, const std::unordered_set< const RelProject * > &permutating_projects, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
const std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t > > & node_to_input_renum_
void hoist_filter_cond_to_cross_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
RexInputSinker(const std::unordered_map< size_t, size_t > &old_to_new_idx, const RelAlgNode *new_src)
AvailabilityChecker(const std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t >> &liveouts, const std::unordered_set< const RelAlgNode * > &intact_nodes)
size_t size() const override
void sink_projected_boolean_expr_to_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
RetType visitInput(const RexInput *input) const override
void eliminate_identical_copy(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void setCondition(std::unique_ptr< const RexScalar > &condition)
#define LOG(tag)
Definition: Logger.h:194
SortDirection getSortDir() const
bool does_redef_cols(const RelAlgNode *node)
const RexScalar * getCondition() const
RetType visitOperator(const RexOperator *rex_operator) const override
Definition: RexVisitor.h:153
std::string join(T const &container, std::string const &delim)
void propagate_input_renumbering(std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t >> &liveout_renumbering, const std::vector< const RelAlgNode * > &ready_nodes, const std::unordered_map< const RelAlgNode *, std::unordered_set< size_t >> &old_liveouts, const std::unordered_set< const RelAlgNode * > &intact_nodes, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web, const std::unordered_map< const RelAlgNode *, size_t > &orig_node_sizes)
void * visitInput(const RexInput *rex_input) const override
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
#define CHECK_GE(x, y)
Definition: Logger.h:216
size_t getField() const
RetType visitOperator(const RexOperator *rex_operator) const override
Definition: sqldefs.h:30
tuple root
Definition: setup.in.py:14
std::unordered_set< const RelProject * > get_visible_projects(const RelAlgNode *root)
#define CHECK_GT(x, y)
Definition: Logger.h:215
const std::unordered_set< const RelProject * > & crt_projects_
bool project_separates_sort(const RelProject *project, const RelAlgNode *next_node)
std::unordered_map< const RelAlgNode *, std::unordered_set< size_t > > mark_live_columns(std::vector< std::shared_ptr< RelAlgNode >> &nodes)
bool safe_to_redirect(const RelProject *project, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
void simplify_sort(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
virtual T visit(const RexScalar *rex_scalar) const
Definition: RexVisitor.h:27
const std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t > > & liveouts_
unsigned getIndex() const
std::vector< std::unordered_set< size_t > > get_live_ins(const RelAlgNode *node, const std::unordered_map< const RelAlgNode *, std::unordered_set< size_t >> &live_outs)
RetType visitInput(const RexInput *input) const override
void cleanup_dead_nodes(std::vector< std::shared_ptr< RelAlgNode >> &nodes)
RetType visitInput(const RexInput *input) const override
#define CHECK_NE(x, y)
Definition: Logger.h:212
NullSortedPosition getNullsPosition() const
static Ids getLiveRexSubQueryIds(RelAlgNode const *)
const std::unordered_map< size_t, size_t > & old_to_new_in_idx_
const RelAlgNode * getInput(const size_t idx) const
Definition: sqldefs.h:37
bool isSimple() const
RetType visitInput(const RexInput *input) const override
Definition: RexVisitor.h:141
virtual void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input)
RetType aggregateResult(const RetType &aggregate, const RetType &next_result) const override
std::unique_ptr< RexInput > deepCopy() const
bool any_dead_col_in(const RelAlgNode *node, const std::unordered_set< size_t > &live_outs)
const std::unordered_set< const RelAlgNode * > & intact_nodes_
RexRebindInputsVisitor(const RelAlgNode *old_input, const RelAlgNode *new_input)
const RexScalar * getProjectAt(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:213
void setSourceNode(const RelAlgNode *node) const
DEVICE void iota(ARGS &&...args)
Definition: gpu_enabled.h:69
RetType visitInput(const RexInput *input) const override
virtual size_t size() const =0
const RelAlgNode * getSourceNode() const
RetType visitInput(const RexInput *input) const override
void try_insert_coalesceable_proj(std::vector< std::shared_ptr< RelAlgNode >> &nodes, std::unordered_map< const RelAlgNode *, std::unordered_set< size_t >> &liveouts, std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
std::vector< std::unique_ptr< const RexAgg > > renumber_rex_aggs(std::vector< std::unique_ptr< const RexAgg >> &agg_exprs, const std::unordered_map< size_t, size_t > &new_numbering)
SortField renumber_sort_field(const SortField &old_field, const std::unordered_map< size_t, size_t > &new_numbering)
virtual std::string toString() const =0
#define CHECK(condition)
Definition: Logger.h:203
SubConditionRemover(const std::vector< const RexScalar * > sub_conds)
std::unordered_set< const RexScalar * > sub_conditions_
const RelJoin * join_
void propagate_rex_input_renumber(const RelFilter *excluded_root, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
void add_new_indices_for(const RelAlgNode *node, std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t >> &new_liveouts, const std::unordered_set< size_t > &old_liveouts, const std::unordered_set< const RelAlgNode * > &intact_nodes, const std::unordered_map< const RelAlgNode *, size_t > &orig_node_sizes)
std::string get_field_name(const RelAlgNode *node, size_t index)
void fold_filters(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
const std::unordered_map< size_t, std::unique_ptr< const RexScalar > > & idx_to_subcond_
const size_t inputCount() const
void eliminate_dead_subqueries(std::vector< std::shared_ptr< RexSubQuery >> &subqueries, RelAlgNode const *root)
void replace_all_usages(std::shared_ptr< const RelAlgNode > old_def_node, std::shared_ptr< const RelAlgNode > new_def_node, std::unordered_map< const RelAlgNode *, std::shared_ptr< RelAlgNode >> &deconst_mapping, std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
const unsigned old_base_
RexInputRenumberVisitor(const std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t >> &new_numbering)
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
std::pair< std::unordered_map< const RelAlgNode *, std::unordered_map< size_t, size_t > >, std::vector< const RelAlgNode * > > sweep_dead_columns(const std::unordered_map< const RelAlgNode *, std::unordered_set< size_t >> &live_outs, const std::vector< std::shared_ptr< RelAlgNode >> &nodes, const std::unordered_set< const RelAlgNode * > &intact_nodes, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web, const std::unordered_map< const RelAlgNode *, size_t > &orig_node_sizes)
JoinTargetRebaser(const RelJoin *join, const unsigned old_base)
RexProjectInputRedirector(const std::unordered_set< const RelProject * > &crt_inputs)
DEVICE void swap(ARGS &&...args)
Definition: gpu_enabled.h:114
std::unique_ptr< const RexScalar > RetType
Definition: RexVisitor.h:139
#define VLOG(n)
Definition: Logger.h:297
void eliminate_dead_columns(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
RetType visitInput(const RexInput *input) const override