OmniSciDB  a47db9e897
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anonymous_namespace{RelAlgOptimizer.cpp} Namespace Reference

Classes

class  RexProjectInputRedirector
 
class  RexRebindInputsVisitor
 
class  RexInputCollector
 
class  AvailabilityChecker
 
class  RexInputRenumberVisitor
 
class  RexInputSinker
 
class  SubConditionReplacer
 
class  RexInputRedirector
 

Functions

size_t get_actual_source_size (const RelProject *curr_project, const std::unordered_set< const RelProject * > &projects_to_remove)
 
bool safe_to_redirect (const RelProject *project, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
 
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)
 
void propagate_rex_input_renumber (const RelFilter *excluded_root, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
 
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)
 
void cleanup_dead_nodes (std::vector< std::shared_ptr< RelAlgNode >> &nodes)
 
std::unordered_set< const
RelProject * > 
get_visible_projects (const RelAlgNode *root)
 
bool is_distinct (const size_t input_idx, const RelAlgNode *node)
 
size_t pick_always_live_col_idx (const RelAlgNode *node)
 
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)
 
bool any_dead_col_in (const RelAlgNode *node, const std::unordered_set< size_t > &live_outs)
 
bool does_redef_cols (const RelAlgNode *node)
 
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::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)
 
std::unordered_map< const
RelAlgNode
*, std::unordered_set< size_t > > 
mark_live_columns (std::vector< std::shared_ptr< RelAlgNode >> &nodes)
 
std::string get_field_name (const RelAlgNode *node, size_t index)
 
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::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)
 
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 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)
 

Function Documentation

void anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 729 of file RelAlgOptimizer.cpp.

References CHECK(), CHECK_GT, does_redef_cols(), RelAlgNode::getInput(), logger::INFO, RelAlgNode::inputCount(), LOG, RelAlgNode::size(), src, and RelAlgNode::toString().

Referenced by propagate_input_renumbering(), and sweep_dead_columns().

735  {
736  auto live_fields = old_liveouts;
737  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
738  for (size_t i = 0; i < aggregate->getGroupByCount(); ++i) {
739  live_fields.insert(i);
740  }
741  }
742  auto it_ok =
743  new_liveouts.insert(std::make_pair(node, std::unordered_map<size_t, size_t>{}));
744  CHECK(it_ok.second);
745  auto& new_indices = it_ok.first->second;
746  if (intact_nodes.count(node)) {
747  for (size_t i = 0, e = node->size(); i < e; ++i) {
748  new_indices.insert(std::make_pair(i, i));
749  }
750  return;
751  }
752  if (does_redef_cols(node)) {
753  auto node_sz_it = orig_node_sizes.find(node);
754  CHECK(node_sz_it != orig_node_sizes.end());
755  const auto node_size = node_sz_it->second;
756  CHECK_GT(node_size, live_fields.size());
757  LOG(INFO) << node->toString() << " eliminated " << node_size - live_fields.size()
758  << " columns.";
759  std::vector<size_t> ordered_indices(live_fields.begin(), live_fields.end());
760  std::sort(ordered_indices.begin(), ordered_indices.end());
761  for (size_t i = 0; i < ordered_indices.size(); ++i) {
762  new_indices.insert(std::make_pair(ordered_indices[i], i));
763  }
764  return;
765  }
766  std::vector<size_t> ordered_indices;
767  for (size_t i = 0, old_base = 0, new_base = 0; i < node->inputCount(); ++i) {
768  auto src = node->getInput(i);
769  auto src_renum_it = new_liveouts.find(src);
770  if (src_renum_it != new_liveouts.end()) {
771  for (auto m : src_renum_it->second) {
772  new_indices.insert(std::make_pair(old_base + m.first, new_base + m.second));
773  }
774  new_base += src_renum_it->second.size();
775  } else if (dynamic_cast<const RelScan*>(src) || intact_nodes.count(src)) {
776  for (size_t i = 0; i < src->size(); ++i) {
777  new_indices.insert(std::make_pair(old_base + i, new_base + i));
778  }
779  new_base += src->size();
780  } else {
781  CHECK(false);
782  }
783  auto src_sz_it = orig_node_sizes.find(src);
784  CHECK(src_sz_it != orig_node_sizes.end());
785  old_base += src_sz_it->second;
786  }
787 }
#define LOG(tag)
Definition: Logger.h:185
bool does_redef_cols(const RelAlgNode *node)
int64_t * src
#define CHECK_GT(x, y)
Definition: Logger.h:202
CHECK(cgen_state)
const RelAlgNode * getInput(const size_t idx) const
virtual size_t size() const =0
virtual std::string toString() const =0
const size_t inputCount() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool anonymous_namespace{RelAlgOptimizer.cpp}::any_dead_col_in ( const RelAlgNode node,
const std::unordered_set< size_t > &  live_outs 
)

Definition at line 685 of file RelAlgOptimizer.cpp.

References CHECK(), and RelAlgNode::size().

Referenced by eliminate_dead_columns(), and try_insert_coalesceable_proj().

686  {
687  CHECK(!dynamic_cast<const RelScan*>(node));
688  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
689  for (size_t i = aggregate->getGroupByCount(); i < aggregate->size(); ++i) {
690  if (!live_outs.count(i)) {
691  return true;
692  }
693  }
694  return false;
695  }
696 
697  return node->size() > live_outs.size();
698 }
CHECK(cgen_state)
virtual size_t size() const =0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::cleanup_dead_nodes ( std::vector< std::shared_ptr< RelAlgNode >> &  nodes)

Definition at line 316 of file RelAlgOptimizer.cpp.

References logger::INFO, and LOG.

Referenced by eliminate_identical_copy(), and fold_filters().

316  {
317  for (auto nodeIt = nodes.rbegin(); nodeIt != nodes.rend(); ++nodeIt) {
318  if (nodeIt->unique()) {
319  LOG(INFO) << "ID=" << (*nodeIt)->getId() << " " << (*nodeIt)->toString()
320  << " deleted!";
321  nodeIt->reset();
322  }
323  }
324 
325  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
326  for (auto node : nodes) {
327  if (!node) {
328  continue;
329  }
330  new_nodes.push_back(node);
331  }
332  nodes.swap(new_nodes);
333 }
#define LOG(tag)
Definition: Logger.h:185

+ Here is the caller graph for this function:

bool anonymous_namespace{RelAlgOptimizer.cpp}::does_redef_cols ( const RelAlgNode node)

Definition at line 700 of file RelAlgOptimizer.cpp.

Referenced by add_new_indices_for(), eliminate_dead_columns(), propagate_input_renumbering(), sweep_dead_columns(), and try_insert_coalesceable_proj().

700  {
701  return dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelProject*>(node);
702 }

+ Here is the caller graph for this function:

size_t anonymous_namespace{RelAlgOptimizer.cpp}::get_actual_source_size ( const RelProject curr_project,
const std::unordered_set< const RelProject * > &  projects_to_remove 
)

Definition at line 97 of file RelAlgOptimizer.cpp.

References RelAlgNode::getInput(), and RelAlgNode::size().

Referenced by is_identical_copy().

99  {
100  auto source = curr_project->getInput(0);
101  while (auto filter = dynamic_cast<const RelFilter*>(source)) {
102  source = filter->getInput(0);
103  }
104  if (auto src_project = dynamic_cast<const RelProject*>(source)) {
105  if (projects_to_remove.count(src_project)) {
106  return get_actual_source_size(src_project, projects_to_remove);
107  }
108  }
109  return curr_project->getInput(0)->size();
110 }
size_t get_actual_source_size(const RelProject *curr_project, const std::unordered_set< const RelProject * > &projects_to_remove)
const RelAlgNode * getInput(const size_t idx) const
virtual size_t size() const =0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string anonymous_namespace{RelAlgOptimizer.cpp}::get_field_name ( const RelAlgNode node,
size_t  index 
)

Definition at line 897 of file RelAlgOptimizer.cpp.

References CHECK(), CHECK_EQ, CHECK_LT, RelAlgNode::getInput(), join(), and RelAlgNode::size().

Referenced by try_insert_coalesceable_proj().

897  {
898  CHECK_LT(index, node->size());
899  if (auto scan = dynamic_cast<const RelScan*>(node)) {
900  return scan->getFieldName(index);
901  }
902  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
903  CHECK_EQ(aggregate->size(), aggregate->getFields().size());
904  return aggregate->getFieldName(index);
905  }
906  if (auto join = dynamic_cast<const RelJoin*>(node)) {
907  const auto lhs_size = join->getInput(0)->size();
908  if (index < lhs_size) {
909  return get_field_name(join->getInput(0), index);
910  }
911  return get_field_name(join->getInput(1), index - lhs_size);
912  }
913  if (auto project = dynamic_cast<const RelProject*>(node)) {
914  return project->getFieldName(index);
915  }
916  CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node));
917  return get_field_name(node->getInput(0), index);
918 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::string join(T const &container, std::string const &delim)
CHECK(cgen_state)
const RelAlgNode * getInput(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:200
virtual size_t size() const =0
std::string get_field_name(const RelAlgNode *node, size_t index)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<std::unordered_set<size_t> > anonymous_namespace{RelAlgOptimizer.cpp}::get_live_ins ( const RelAlgNode node,
const std::unordered_map< const RelAlgNode *, std::unordered_set< size_t >> &  live_outs 
)

Definition at line 570 of file RelAlgOptimizer.cpp.

References CHECK(), CHECK_EQ, CHECK_LT, join(), pick_always_live_col_idx(), and RexVisitorBase< T >::visit().

Referenced by mark_live_columns().

572  {
573  if (!node || dynamic_cast<const RelScan*>(node)) {
574  return {};
575  }
576  RexInputCollector collector(node);
577  auto it = live_outs.find(node);
578  CHECK(it != live_outs.end());
579  auto live_out = it->second;
580  if (auto project = dynamic_cast<const RelProject*>(node)) {
581  CHECK_EQ(size_t(1), project->inputCount());
582  std::unordered_set<size_t> live_in;
583  for (const auto& idx : live_out) {
584  CHECK_LT(idx, project->size());
585  auto partial_in = collector.visit(project->getProjectAt(idx));
586  for (auto rex_in : partial_in) {
587  live_in.insert(rex_in.getIndex());
588  }
589  }
590  if (project->size() == 1 &&
591  dynamic_cast<const RexLiteral*>(project->getProjectAt(0))) {
592  CHECK(live_in.empty());
593  live_in.insert(pick_always_live_col_idx(project->getInput(0)));
594  }
595  return {live_in};
596  }
597  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
598  CHECK_EQ(size_t(1), aggregate->inputCount());
599  const auto group_key_count = static_cast<size_t>(aggregate->getGroupByCount());
600  const auto agg_expr_count = static_cast<size_t>(aggregate->getAggExprsCount());
601  std::unordered_set<size_t> live_in;
602  for (size_t i = 0; i < group_key_count; ++i) {
603  live_in.insert(i);
604  }
605  bool has_count_star_only{false};
606  for (const auto& idx : live_out) {
607  if (idx < group_key_count) {
608  continue;
609  }
610  const auto agg_idx = idx - group_key_count;
611  CHECK_LT(agg_idx, agg_expr_count);
612  const auto& cur_agg_expr = aggregate->getAggExprs()[agg_idx];
613  const auto n_operands = cur_agg_expr->size();
614  for (size_t i = 0; i < n_operands; ++i) {
615  live_in.insert(static_cast<size_t>(cur_agg_expr->getOperand(i)));
616  }
617  if (n_operands == 0) {
618  has_count_star_only = true;
619  }
620  }
621  if (has_count_star_only && !group_key_count) {
622  live_in.insert(size_t(0));
623  }
624  return {live_in};
625  }
626  if (auto join = dynamic_cast<const RelJoin*>(node)) {
627  std::unordered_set<size_t> lhs_live_ins;
628  std::unordered_set<size_t> rhs_live_ins;
629  CHECK_EQ(size_t(2), join->inputCount());
630  auto lhs = join->getInput(0);
631  auto rhs = join->getInput(1);
632  const auto rhs_idx_base = lhs->size();
633  for (const auto idx : live_out) {
634  if (idx < rhs_idx_base) {
635  lhs_live_ins.insert(idx);
636  } else {
637  rhs_live_ins.insert(idx - rhs_idx_base);
638  }
639  }
640  auto rex_ins = collector.visit(join->getCondition());
641  for (const auto& rex_in : rex_ins) {
642  const auto in_idx = static_cast<size_t>(rex_in.getIndex());
643  if (rex_in.getSourceNode() == lhs) {
644  lhs_live_ins.insert(in_idx);
645  continue;
646  }
647  if (rex_in.getSourceNode() == rhs) {
648  rhs_live_ins.insert(in_idx);
649  continue;
650  }
651  CHECK(false);
652  }
653  return {lhs_live_ins, rhs_live_ins};
654  }
655  if (auto sort = dynamic_cast<const RelSort*>(node)) {
656  CHECK_EQ(size_t(1), sort->inputCount());
657  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
658  for (size_t i = 0; i < sort->collationCount(); ++i) {
659  live_in.insert(sort->getCollation(i).getField());
660  }
661  return {live_in};
662  }
663  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
664  CHECK_EQ(size_t(1), filter->inputCount());
665  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
666  auto rex_ins = collector.visit(filter->getCondition());
667  for (const auto& rex_in : rex_ins) {
668  live_in.insert(static_cast<size_t>(rex_in.getIndex()));
669  }
670  return {live_in};
671  }
672  if (auto table_func = dynamic_cast<const RelTableFunction*>(node)) {
673  CHECK_EQ(size_t(1), table_func->inputCount());
674 
675  const auto input_count = table_func->size();
676  std::unordered_set<size_t> live_in;
677  for (size_t i = 0; i < input_count; i++) {
678  live_in.insert(i);
679  }
680  return {live_in};
681  }
682  return {};
683 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
size_t pick_always_live_col_idx(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
CHECK(cgen_state)
#define CHECK_LT(x, y)
Definition: Logger.h:200

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::unordered_set<const RelProject*> anonymous_namespace{RelAlgOptimizer.cpp}::get_visible_projects ( const RelAlgNode root)

Definition at line 335 of file RelAlgOptimizer.cpp.

References CHECK(), RelAlgNode::getInput(), and join().

Referenced by eliminate_identical_copy().

335  {
336  if (auto project = dynamic_cast<const RelProject*>(root)) {
337  return {project};
338  }
339 
340  if (dynamic_cast<const RelAggregate*>(root) || dynamic_cast<const RelScan*>(root) ||
341  dynamic_cast<const RelLogicalValues*>(root) ||
342  dynamic_cast<const RelModify*>(root)) {
343  return std::unordered_set<const RelProject*>{};
344  }
345 
346  if (auto join = dynamic_cast<const RelJoin*>(root)) {
347  auto lhs_projs = get_visible_projects(join->getInput(0));
348  auto rhs_projs = get_visible_projects(join->getInput(1));
349  lhs_projs.insert(rhs_projs.begin(), rhs_projs.end());
350  return lhs_projs;
351  }
352 
353  CHECK(dynamic_cast<const RelFilter*>(root) || dynamic_cast<const RelSort*>(root));
354  return get_visible_projects(root->getInput(0));
355 }
std::string join(T const &container, std::string const &delim)
std::unordered_set< const RelProject * > get_visible_projects(const RelAlgNode *root)
CHECK(cgen_state)
const RelAlgNode * getInput(const size_t idx) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct ( const size_t  input_idx,
const RelAlgNode node 
)

Definition at line 358 of file RelAlgOptimizer.cpp.

References CHECK(), CHECK_EQ, CHECK_LT, RelAlgNode::getInput(), and RelAlgNode::inputCount().

Referenced by Parser::FunctionRef::analyze(), Parser::QuerySpec::analyze(), eliminate_identical_copy(), get_target_info(), Parser::QuerySpec::to_string(), anonymous_namespace{CalciteAdapter.cpp}::CalciteAdapter::translateAggregate(), and RelAlgTranslator::translateAggregateRex().

358  {
359  if (dynamic_cast<const RelFilter*>(node) || dynamic_cast<const RelSort*>(node)) {
360  CHECK_EQ(size_t(1), node->inputCount());
361  return is_distinct(input_idx, node->getInput(0));
362  }
363  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
364  CHECK_EQ(size_t(1), node->inputCount());
365  if (aggregate->getGroupByCount() == 1 && !input_idx) {
366  return true;
367  }
368  if (input_idx < aggregate->getGroupByCount()) {
369  return is_distinct(input_idx, node->getInput(0));
370  }
371  return false;
372  }
373  if (auto project = dynamic_cast<const RelProject*>(node)) {
374  CHECK_LT(input_idx, project->size());
375  if (auto input = dynamic_cast<const RexInput*>(project->getProjectAt(input_idx))) {
376  CHECK_EQ(size_t(1), node->inputCount());
377  return is_distinct(input->getIndex(), project->getInput(0));
378  }
379  return false;
380  }
381  CHECK(dynamic_cast<const RelJoin*>(node) || dynamic_cast<const RelScan*>(node));
382  return false;
383 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
CHECK(cgen_state)
const RelAlgNode * getInput(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:200
const size_t inputCount() const
bool is_distinct(const size_t input_idx, const RelAlgNode *node)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 129 of file RelAlgOptimizer.cpp.

References CHECK(), get_actual_source_size(), RelProject::getProjectAt(), safe_to_redirect(), and RelProject::size().

Referenced by eliminate_identical_copy().

134  {
135  auto source_size = get_actual_source_size(project, projects_to_remove);
136  if (project->size() > source_size) {
137  return false;
138  }
139 
140  if (project->size() < source_size) {
141  auto usrs_it = du_web.find(project);
142  CHECK(usrs_it != du_web.end());
143  bool guard_found = false;
144  while (usrs_it->second.size() == size_t(1)) {
145  auto only_usr = *usrs_it->second.begin();
146  if (dynamic_cast<const RelProject*>(only_usr)) {
147  guard_found = true;
148  break;
149  }
150  if (dynamic_cast<const RelAggregate*>(only_usr) ||
151  dynamic_cast<const RelSort*>(only_usr) ||
152  dynamic_cast<const RelJoin*>(only_usr) ||
153  dynamic_cast<const RelTableFunction*>(only_usr)) {
154  return false;
155  }
156  CHECK(dynamic_cast<const RelFilter*>(only_usr));
157  usrs_it = du_web.find(only_usr);
158  CHECK(usrs_it != du_web.end());
159  }
160 
161  if (!guard_found) {
162  return false;
163  }
164  }
165 
166  bool identical = true;
167  for (size_t i = 0; i < project->size(); ++i) {
168  auto target = dynamic_cast<const RexInput*>(project->getProjectAt(i));
169  CHECK(target);
170  if (i != target->getIndex()) {
171  identical = false;
172  break;
173  }
174  }
175 
176  if (identical) {
177  return true;
178  }
179 
180  if (safe_to_redirect(project, du_web)) {
181  permutating_projects.insert(project);
182  return true;
183  }
184 
185  return false;
186 }
size_t get_actual_source_size(const RelProject *curr_project, const std::unordered_set< const RelProject * > &projects_to_remove)
size_t size() const override
bool safe_to_redirect(const RelProject *project, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)
CHECK(cgen_state)
const RexScalar * getProjectAt(const size_t idx) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::unordered_map<const RelAlgNode*, std::unordered_set<size_t> > anonymous_namespace{RelAlgOptimizer.cpp}::mark_live_columns ( std::vector< std::shared_ptr< RelAlgNode >> &  nodes)

Definition at line 846 of file RelAlgOptimizer.cpp.

References CHECK(), CHECK_EQ, get_live_ins(), and src.

Referenced by eliminate_dead_columns(), and sink_projected_boolean_expr_to_join().

847  {
848  std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>> live_outs;
849  std::vector<const RelAlgNode*> work_set;
850  for (auto node_it = nodes.rbegin(); node_it != nodes.rend(); ++node_it) {
851  auto node = node_it->get();
852  if (dynamic_cast<const RelScan*>(node) || live_outs.count(node) ||
853  dynamic_cast<const RelModify*>(node)) {
854  continue;
855  }
856  std::vector<size_t> all_live(node->size());
857  std::iota(all_live.begin(), all_live.end(), size_t(0));
858  live_outs.insert(std::make_pair(
859  node, std::unordered_set<size_t>(all_live.begin(), all_live.end())));
860  work_set.push_back(node);
861  while (!work_set.empty()) {
862  auto walker = work_set.back();
863  work_set.pop_back();
864  CHECK(!dynamic_cast<const RelScan*>(walker));
865  CHECK(live_outs.count(walker));
866  auto live_ins = get_live_ins(walker, live_outs);
867  CHECK_EQ(live_ins.size(), walker->inputCount());
868  for (size_t i = 0; i < walker->inputCount(); ++i) {
869  auto src = walker->getInput(i);
870  if (dynamic_cast<const RelScan*>(src) || live_ins[i].empty()) {
871  continue;
872  }
873  if (!live_outs.count(src)) {
874  live_outs.insert(std::make_pair(src, std::unordered_set<size_t>{}));
875  }
876  auto src_it = live_outs.find(src);
877  CHECK(src_it != live_outs.end());
878  auto& live_out = src_it->second;
879  bool changed = false;
880  if (!live_out.empty()) {
881  live_out.insert(live_ins[i].begin(), live_ins[i].end());
882  changed = true;
883  } else {
884  for (int idx : live_ins[i]) {
885  changed |= live_out.insert(idx).second;
886  }
887  }
888  if (changed) {
889  work_set.push_back(src);
890  }
891  }
892  }
893  }
894  return live_outs;
895 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
int64_t * src
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)
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t anonymous_namespace{RelAlgOptimizer.cpp}::pick_always_live_col_idx ( const RelAlgNode node)

Definition at line 541 of file RelAlgOptimizer.cpp.

References CHECK(), join(), and RelAlgNode::size().

Referenced by get_live_ins().

541  {
542  CHECK(node->size());
543  RexInputCollector collector(node);
544  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
545  auto rex_ins = collector.visit(filter->getCondition());
546  if (!rex_ins.empty()) {
547  return static_cast<size_t>(rex_ins.begin()->getIndex());
548  }
549  return pick_always_live_col_idx(filter->getInput(0));
550  } else if (auto join = dynamic_cast<const RelJoin*>(node)) {
551  auto rex_ins = collector.visit(join->getCondition());
552  if (!rex_ins.empty()) {
553  return static_cast<size_t>(rex_ins.begin()->getIndex());
554  }
555  if (auto lhs_idx = pick_always_live_col_idx(join->getInput(0))) {
556  return lhs_idx;
557  }
558  if (auto rhs_idx = pick_always_live_col_idx(join->getInput(0))) {
559  return rhs_idx + join->getInput(0)->size();
560  }
561  } else if (auto sort = dynamic_cast<const RelSort*>(node)) {
562  if (sort->collationCount()) {
563  return sort->getCollation(0).getField();
564  }
565  return pick_always_live_col_idx(sort->getInput(0));
566  }
567  return size_t(0);
568 }
size_t pick_always_live_col_idx(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
CHECK(cgen_state)
virtual size_t size() const =0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 1042 of file RelAlgOptimizer.cpp.

References add_new_indices_for(), CHECK(), does_redef_cols(), anonymous_namespace{RelAlgOptimizer.cpp}::AvailabilityChecker::hasAllSrcReady(), join(), renumber_rex_aggs(), renumber_sort_field(), and RexVisitorBase< T >::visit().

Referenced by eliminate_dead_columns().

1050  {
1051  RexInputRenumberVisitor renumberer(liveout_renumbering);
1052  AvailabilityChecker checker(liveout_renumbering, intact_nodes);
1053  std::deque<const RelAlgNode*> work_set(ready_nodes.begin(), ready_nodes.end());
1054  while (!work_set.empty()) {
1055  auto walker = work_set.front();
1056  work_set.pop_front();
1057  CHECK(!dynamic_cast<const RelScan*>(walker));
1058  auto node = const_cast<RelAlgNode*>(walker);
1059  if (auto project = dynamic_cast<RelProject*>(node)) {
1060  auto old_exprs = project->getExpressionsAndRelease();
1061  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1062  for (auto& expr : old_exprs) {
1063  new_exprs.push_back(renumberer.visit(expr.get()));
1064  }
1065  project->setExpressions(new_exprs);
1066  } else if (auto aggregate = dynamic_cast<RelAggregate*>(node)) {
1067  auto src_it = liveout_renumbering.find(node->getInput(0));
1068  CHECK(src_it != liveout_renumbering.end());
1069  auto old_exprs = aggregate->getAggExprsAndRelease();
1070  auto new_exprs = renumber_rex_aggs(old_exprs, src_it->second);
1071  aggregate->setAggExprs(new_exprs);
1072  } else if (auto join = dynamic_cast<RelJoin*>(node)) {
1073  auto new_condition = renumberer.visit(join->getCondition());
1074  join->setCondition(new_condition);
1075  } else if (auto filter = dynamic_cast<RelFilter*>(node)) {
1076  auto new_condition = renumberer.visit(filter->getCondition());
1077  filter->setCondition(new_condition);
1078  } else if (auto sort = dynamic_cast<RelSort*>(node)) {
1079  auto src_it = liveout_renumbering.find(node->getInput(0));
1080  CHECK(src_it != liveout_renumbering.end());
1081  std::vector<SortField> new_collations;
1082  for (size_t i = 0; i < sort->collationCount(); ++i) {
1083  new_collations.push_back(
1084  renumber_sort_field(sort->getCollation(i), src_it->second));
1085  }
1086  sort->setCollation(std::move(new_collations));
1087  } else {
1088  CHECK(false);
1089  }
1090 
1091  // Ignore empty live_out due to some invalid node
1092  if (does_redef_cols(node) || intact_nodes.count(node)) {
1093  continue;
1094  }
1095  auto live_pair = old_liveouts.find(node);
1096  CHECK(live_pair != old_liveouts.end());
1097  auto live_out = live_pair->second;
1099  node, liveout_renumbering, live_out, intact_nodes, orig_node_sizes);
1100  auto usrs_it = du_web.find(walker);
1101  CHECK(usrs_it != du_web.end());
1102  for (auto usr : usrs_it->second) {
1103  if (checker.hasAllSrcReady(usr)) {
1104  work_set.push_back(usr);
1105  }
1106  }
1107  }
1108 }
bool does_redef_cols(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
CHECK(cgen_state)
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)
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::propagate_rex_input_renumber ( const RelFilter excluded_root,
const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &  du_web 
)

Definition at line 188 of file RelAlgOptimizer.cpp.

References CHECK(), and RelAlgNode::getInput().

Referenced by redirect_inputs_of().

191  {
192  CHECK(excluded_root);
193  auto src_project = dynamic_cast<const RelProject*>(excluded_root->getInput(0));
194  CHECK(src_project && src_project->isSimple());
195  const auto indirect_join_src = dynamic_cast<const RelJoin*>(src_project->getInput(0));
196  std::unordered_map<size_t, size_t> old_to_new_idx;
197  for (size_t i = 0; i < src_project->size(); ++i) {
198  auto rex_in = dynamic_cast<const RexInput*>(src_project->getProjectAt(i));
199  CHECK(rex_in);
200  size_t src_base = 0;
201  if (indirect_join_src != nullptr &&
202  indirect_join_src->getInput(1) == rex_in->getSourceNode()) {
203  src_base = indirect_join_src->getInput(0)->size();
204  }
205  old_to_new_idx.insert(std::make_pair(i, src_base + rex_in->getIndex()));
206  old_to_new_idx.insert(std::make_pair(i, rex_in->getIndex()));
207  }
208  CHECK(old_to_new_idx.size());
209  RexInputRenumber<false> renumber(old_to_new_idx);
210  auto usrs_it = du_web.find(excluded_root);
211  CHECK(usrs_it != du_web.end());
212  std::vector<const RelAlgNode*> work_set(usrs_it->second.begin(), usrs_it->second.end());
213  while (!work_set.empty()) {
214  auto node = work_set.back();
215  work_set.pop_back();
216  auto modified_node = const_cast<RelAlgNode*>(node);
217  if (auto filter = dynamic_cast<RelFilter*>(modified_node)) {
218  auto new_condition = renumber.visit(filter->getCondition());
219  filter->setCondition(new_condition);
220  auto usrs_it = du_web.find(filter);
221  CHECK(usrs_it != du_web.end() && usrs_it->second.size() == 1);
222  work_set.push_back(*usrs_it->second.begin());
223  continue;
224  }
225  if (auto project = dynamic_cast<RelProject*>(modified_node)) {
226  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
227  for (size_t i = 0; i < project->size(); ++i) {
228  new_exprs.push_back(renumber.visit(project->getProjectAt(i)));
229  }
230  project->setExpressions(new_exprs);
231  continue;
232  }
233  CHECK(false);
234  }
235 }
CHECK(cgen_state)
const RelAlgNode * getInput(const size_t idx) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 237 of file RelAlgOptimizer.cpp.

References CHECK(), join(), propagate_rex_input_renumber(), RexVisitorBase< T >::visit(), and anonymous_namespace{RelAlgOptimizer.cpp}::RexRebindInputsVisitor::visitNode().

Referenced by eliminate_identical_copy().

242  {
243  std::shared_ptr<const RelProject> src_project = nullptr;
244  for (size_t i = 0; i < node->inputCount(); ++i) {
245  if (auto project =
246  std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(i))) {
247  if (projects.count(project.get())) {
248  src_project = project;
249  break;
250  }
251  }
252  }
253  if (!src_project) {
254  return;
255  }
256  if (auto join = std::dynamic_pointer_cast<RelJoin>(node)) {
257  auto other_project =
258  src_project == node->getAndOwnInput(0)
259  ? std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(1))
260  : std::dynamic_pointer_cast<const RelProject>(node->getAndOwnInput(0));
261  join->replaceInput(src_project, src_project->getAndOwnInput(0));
262  RexRebindInputsVisitor rebinder(src_project.get(), src_project->getInput(0));
263  auto usrs_it = du_web.find(join.get());
264  CHECK(usrs_it != du_web.end());
265  for (auto usr : usrs_it->second) {
266  rebinder.visitNode(usr);
267  }
268 
269  if (other_project && projects.count(other_project.get())) {
270  join->replaceInput(other_project, other_project->getAndOwnInput(0));
271  RexRebindInputsVisitor other_rebinder(other_project.get(),
272  other_project->getInput(0));
273  for (auto usr : usrs_it->second) {
274  other_rebinder.visitNode(usr);
275  }
276  }
277  return;
278  }
279  if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
280  project->RelAlgNode::replaceInput(src_project, src_project->getAndOwnInput(0));
281  RexProjectInputRedirector redirector(projects);
282  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
283  for (size_t i = 0; i < project->size(); ++i) {
284  new_exprs.push_back(redirector.visit(project->getProjectAt(i)));
285  }
286  project->setExpressions(new_exprs);
287  return;
288  }
289  if (auto filter = std::dynamic_pointer_cast<RelFilter>(node)) {
290  const bool is_permutating_proj = permutating_projects.count(src_project.get());
291  if (is_permutating_proj || dynamic_cast<const RelJoin*>(src_project->getInput(0))) {
292  if (is_permutating_proj) {
293  propagate_rex_input_renumber(filter.get(), du_web);
294  }
295  filter->RelAlgNode::replaceInput(src_project, src_project->getAndOwnInput(0));
296  RexProjectInputRedirector redirector(projects);
297  auto new_condition = redirector.visit(filter->getCondition());
298  filter->setCondition(new_condition);
299  } else {
300  filter->replaceInput(src_project, src_project->getAndOwnInput(0));
301  }
302  return;
303  }
304  if (std::dynamic_pointer_cast<RelSort>(node) &&
305  dynamic_cast<const RelScan*>(src_project->getInput(0))) {
306  return;
307  }
308  if (std::dynamic_pointer_cast<RelModify>(node)) {
309  return; // NOTE: Review this. Not sure about this.
310  }
311  CHECK(std::dynamic_pointer_cast<RelAggregate>(node) ||
312  std::dynamic_pointer_cast<RelSort>(node));
313  node->replaceInput(src_project, src_project->getAndOwnInput(0));
314 }
std::string join(T const &container, std::string const &delim)
CHECK(cgen_state)
void propagate_rex_input_renumber(const RelFilter *excluded_root, const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &du_web)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<std::unique_ptr<const RexAgg> > anonymous_namespace{RelAlgOptimizer.cpp}::renumber_rex_aggs ( std::vector< std::unique_ptr< const RexAgg >> &  agg_exprs,
const std::unordered_map< size_t, size_t > &  new_numbering 
)

Definition at line 812 of file RelAlgOptimizer.cpp.

Referenced by propagate_input_renumbering().

814  {
815  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
816  for (auto& expr : agg_exprs) {
817  if (expr->size() >= 1) {
818  auto old_idx = expr->getOperand(0);
819  auto idx_it = new_numbering.find(old_idx);
820  if (idx_it != new_numbering.end()) {
821  std::vector<size_t> operands;
822  operands.push_back(idx_it->second);
823  if (expr->size() == 2) {
824  operands.push_back(expr->getOperand(1));
825  }
826  new_exprs.push_back(boost::make_unique<RexAgg>(
827  expr->getKind(), expr->isDistinct(), expr->getType(), operands));
828  continue;
829  }
830  }
831  new_exprs.push_back(std::move(expr));
832  }
833  return new_exprs;
834 }

+ Here is the caller graph for this function:

SortField anonymous_namespace{RelAlgOptimizer.cpp}::renumber_sort_field ( const SortField old_field,
const std::unordered_map< size_t, size_t > &  new_numbering 
)

Definition at line 836 of file RelAlgOptimizer.cpp.

References SortField::getField(), SortField::getNullsPosition(), and SortField::getSortDir().

Referenced by propagate_input_renumbering().

837  {
838  auto field_idx = old_field.getField();
839  auto idx_it = new_numbering.find(field_idx);
840  if (idx_it != new_numbering.end()) {
841  field_idx = idx_it->second;
842  }
843  return SortField(field_idx, old_field.getSortDir(), old_field.getNullsPosition());
844 }
SortDirection getSortDir() const
size_t getField() const
NullSortedPosition getNullsPosition() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 1343 of file RelAlgOptimizer.cpp.

References CHECK().

Referenced by fold_filters().

1348  {
1349  auto usrs_it = du_web.find(old_def_node.get());
1350  RexInputRedirector redirector(new_def_node.get(), old_def_node.get());
1351  CHECK(usrs_it != du_web.end());
1352  for (auto usr : usrs_it->second) {
1353  auto usr_it = deconst_mapping.find(usr);
1354  CHECK(usr_it != deconst_mapping.end());
1355  usr_it->second->replaceInput(old_def_node, new_def_node);
1356  }
1357  auto new_usrs_it = du_web.find(new_def_node.get());
1358  CHECK(new_usrs_it != du_web.end());
1359  new_usrs_it->second.insert(usrs_it->second.begin(), usrs_it->second.end());
1360  usrs_it->second.clear();
1361 }
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool anonymous_namespace{RelAlgOptimizer.cpp}::safe_to_redirect ( const RelProject project,
const std::unordered_map< const RelAlgNode *, std::unordered_set< const RelAlgNode * >> &  du_web 
)

Definition at line 112 of file RelAlgOptimizer.cpp.

References CHECK(), and RelProject::isSimple().

Referenced by is_identical_copy().

115  {
116  if (!project->isSimple()) {
117  return false;
118  }
119  auto usrs_it = du_web.find(project);
120  CHECK(usrs_it != du_web.end());
121  for (auto usr : usrs_it->second) {
122  if (!dynamic_cast<const RelProject*>(usr) && !dynamic_cast<const RelFilter*>(usr)) {
123  return false;
124  }
125  }
126  return true;
127 }
CHECK(cgen_state)
bool isSimple() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::pair<std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t> >, std::vector<const RelAlgNode*> > anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 979 of file RelAlgOptimizer.cpp.

References add_new_indices_for(), CHECK(), does_redef_cols(), and anonymous_namespace{RelAlgOptimizer.cpp}::AvailabilityChecker::hasAllSrcReady().

Referenced by eliminate_dead_columns().

985  {
986  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>
987  liveouts_renumbering;
988  std::vector<const RelAlgNode*> ready_nodes;
989  AvailabilityChecker checker(liveouts_renumbering, intact_nodes);
990  for (auto node : nodes) {
991  // Ignore empty live_out due to some invalid node
992  if (!does_redef_cols(node.get()) || intact_nodes.count(node.get())) {
993  continue;
994  }
995  auto live_pair = live_outs.find(node.get());
996  CHECK(live_pair != live_outs.end());
997  auto old_live_outs = live_pair->second;
999  node.get(), liveouts_renumbering, old_live_outs, intact_nodes, orig_node_sizes);
1000  if (auto aggregate = std::dynamic_pointer_cast<RelAggregate>(node)) {
1001  auto old_exprs = aggregate->getAggExprsAndRelease();
1002  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
1003  auto key_name_it = aggregate->getFields().begin();
1004  std::vector<std::string> new_fields(key_name_it,
1005  key_name_it + aggregate->getGroupByCount());
1006  for (size_t i = aggregate->getGroupByCount(), j = 0;
1007  i < aggregate->getFields().size() && j < old_exprs.size();
1008  ++i, ++j) {
1009  if (old_live_outs.count(i)) {
1010  new_exprs.push_back(std::move(old_exprs[j]));
1011  new_fields.push_back(aggregate->getFieldName(i));
1012  }
1013  }
1014  aggregate->setAggExprs(new_exprs);
1015  aggregate->setFields(new_fields);
1016  } else if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
1017  auto old_exprs = project->getExpressionsAndRelease();
1018  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1019  std::vector<std::string> new_fields;
1020  for (size_t i = 0; i < old_exprs.size(); ++i) {
1021  if (old_live_outs.count(i)) {
1022  new_exprs.push_back(std::move(old_exprs[i]));
1023  new_fields.push_back(project->getFieldName(i));
1024  }
1025  }
1026  project->setExpressions(new_exprs);
1027  project->setFields(new_fields);
1028  } else {
1029  CHECK(false);
1030  }
1031  auto usrs_it = du_web.find(node.get());
1032  CHECK(usrs_it != du_web.end());
1033  for (auto usr : usrs_it->second) {
1034  if (checker.hasAllSrcReady(usr)) {
1035  ready_nodes.push_back(usr);
1036  }
1037  }
1038  }
1039  return {liveouts_renumbering, ready_nodes};
1040 }
bool does_redef_cols(const RelAlgNode *node)
CHECK(cgen_state)
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{RelAlgOptimizer.cpp}::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 
)

Definition at line 920 of file RelAlgOptimizer.cpp.

References any_dead_col_in(), CHECK(), does_redef_cols(), get_field_name(), RelAlgNode::replaceInput(), and anonymous_namespace{RelAlgOptimizer.cpp}::RexRebindInputsVisitor::visitNode().

Referenced by eliminate_dead_columns().

924  {
925  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
926  for (auto node : nodes) {
927  new_nodes.push_back(node);
928  if (!std::dynamic_pointer_cast<RelFilter>(node)) {
929  continue;
930  }
931  const auto filter = node.get();
932  auto liveout_it = liveouts.find(filter);
933  CHECK(liveout_it != liveouts.end());
934  auto& outs = liveout_it->second;
935  if (!any_dead_col_in(filter, outs)) {
936  continue;
937  }
938  auto usrs_it = du_web.find(filter);
939  CHECK(usrs_it != du_web.end());
940  auto& usrs = usrs_it->second;
941  if (usrs.size() != 1 || does_redef_cols(*usrs.begin())) {
942  continue;
943  }
944  auto only_usr = const_cast<RelAlgNode*>(*usrs.begin());
945 
946  std::vector<std::unique_ptr<const RexScalar>> exprs;
947  std::vector<std::string> fields;
948  for (size_t i = 0; i < filter->size(); ++i) {
949  exprs.push_back(boost::make_unique<RexInput>(filter, i));
950  fields.push_back(get_field_name(filter, i));
951  }
952  auto project_owner = std::make_shared<RelProject>(exprs, fields, node);
953  auto project = project_owner.get();
954 
955  only_usr->replaceInput(node, project_owner);
956  if (dynamic_cast<const RelJoin*>(only_usr)) {
957  RexRebindInputsVisitor visitor(filter, project);
958  for (auto usr : du_web[only_usr]) {
959  visitor.visitNode(usr);
960  }
961  }
962 
963  liveouts.insert(std::make_pair(project, outs));
964 
965  usrs.clear();
966  usrs.insert(project);
967  du_web.insert(
968  std::make_pair(project, std::unordered_set<const RelAlgNode*>{only_usr}));
969 
970  new_nodes.push_back(project_owner);
971  }
972  if (new_nodes.size() > nodes.size()) {
973  nodes.swap(new_nodes);
974  }
975 }
bool does_redef_cols(const RelAlgNode *node)
CHECK(cgen_state)
virtual void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input)
bool any_dead_col_in(const RelAlgNode *node, const std::unordered_set< size_t > &live_outs)
std::string get_field_name(const RelAlgNode *node, size_t index)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: