OmniSciDB  ab4938a6a3
anonymous_namespace{RelAlgOptimizer.cpp} Namespace Reference

Classes

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

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

◆ add_new_indices_for()

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 783 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().

789  {
790  auto live_fields = old_liveouts;
791  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
792  for (size_t i = 0; i < aggregate->getGroupByCount(); ++i) {
793  live_fields.insert(i);
794  }
795  }
796  auto it_ok =
797  new_liveouts.insert(std::make_pair(node, std::unordered_map<size_t, size_t>{}));
798  CHECK(it_ok.second);
799  auto& new_indices = it_ok.first->second;
800  if (intact_nodes.count(node)) {
801  for (size_t i = 0, e = node->size(); i < e; ++i) {
802  new_indices.insert(std::make_pair(i, i));
803  }
804  return;
805  }
806  if (does_redef_cols(node)) {
807  auto node_sz_it = orig_node_sizes.find(node);
808  CHECK(node_sz_it != orig_node_sizes.end());
809  const auto node_size = node_sz_it->second;
810  CHECK_GT(node_size, live_fields.size());
811  LOG(INFO) << node->toString() << " eliminated " << node_size - live_fields.size()
812  << " columns.";
813  std::vector<size_t> ordered_indices(live_fields.begin(), live_fields.end());
814  std::sort(ordered_indices.begin(), ordered_indices.end());
815  for (size_t i = 0; i < ordered_indices.size(); ++i) {
816  new_indices.insert(std::make_pair(ordered_indices[i], i));
817  }
818  return;
819  }
820  std::vector<size_t> ordered_indices;
821  for (size_t i = 0, old_base = 0, new_base = 0; i < node->inputCount(); ++i) {
822  auto src = node->getInput(i);
823  auto src_renum_it = new_liveouts.find(src);
824  if (src_renum_it != new_liveouts.end()) {
825  for (auto m : src_renum_it->second) {
826  new_indices.insert(std::make_pair(old_base + m.first, new_base + m.second));
827  }
828  new_base += src_renum_it->second.size();
829  } else if (dynamic_cast<const RelScan*>(src) || intact_nodes.count(src)) {
830  for (size_t i = 0; i < src->size(); ++i) {
831  new_indices.insert(std::make_pair(old_base + i, new_base + i));
832  }
833  new_base += src->size();
834  } else {
835  CHECK(false);
836  }
837  auto src_sz_it = orig_node_sizes.find(src);
838  CHECK(src_sz_it != orig_node_sizes.end());
839  old_base += src_sz_it->second;
840  }
841 }
int64_t * src
#define LOG(tag)
Definition: Logger.h:188
bool does_redef_cols(const RelAlgNode *node)
#define CHECK_GT(x, y)
Definition: Logger.h:209
const size_t inputCount() const
virtual size_t size() const =0
virtual std::string toString() const =0
#define CHECK(condition)
Definition: Logger.h:197
const RelAlgNode * getInput(const size_t idx) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ any_dead_col_in()

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

Definition at line 739 of file RelAlgOptimizer.cpp.

References CHECK, and RelAlgNode::size().

Referenced by eliminate_dead_columns(), and try_insert_coalesceable_proj().

740  {
741  CHECK(!dynamic_cast<const RelScan*>(node));
742  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
743  for (size_t i = aggregate->getGroupByCount(); i < aggregate->size(); ++i) {
744  if (!live_outs.count(i)) {
745  return true;
746  }
747  }
748  return false;
749  }
750 
751  return node->size() > live_outs.size();
752 }
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cleanup_dead_nodes()

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

Definition at line 326 of file RelAlgOptimizer.cpp.

References logger::INFO, and LOG.

Referenced by eliminate_identical_copy(), and fold_filters().

326  {
327  for (auto nodeIt = nodes.rbegin(); nodeIt != nodes.rend(); ++nodeIt) {
328  if (nodeIt->unique()) {
329  LOG(INFO) << "ID=" << (*nodeIt)->getId() << " " << (*nodeIt)->toString()
330  << " deleted!";
331  nodeIt->reset();
332  }
333  }
334 
335  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
336  for (auto node : nodes) {
337  if (!node) {
338  continue;
339  }
340  new_nodes.push_back(node);
341  }
342  nodes.swap(new_nodes);
343 }
#define LOG(tag)
Definition: Logger.h:188
+ Here is the caller graph for this function:

◆ does_redef_cols()

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

Definition at line 754 of file RelAlgOptimizer.cpp.

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

754  {
755  return dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelProject*>(node);
756 }
+ Here is the caller graph for this function:

◆ get_actual_source_size()

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)
virtual size_t size() const =0
const RelAlgNode * getInput(const size_t idx) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_field_name()

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

Definition at line 951 of file RelAlgOptimizer.cpp.

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

Referenced by try_insert_coalesceable_proj().

951  {
952  CHECK_LT(index, node->size());
953  if (auto scan = dynamic_cast<const RelScan*>(node)) {
954  return scan->getFieldName(index);
955  }
956  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
957  CHECK_EQ(aggregate->size(), aggregate->getFields().size());
958  return aggregate->getFieldName(index);
959  }
960  if (auto join = dynamic_cast<const RelJoin*>(node)) {
961  const auto lhs_size = join->getInput(0)->size();
962  if (index < lhs_size) {
963  return get_field_name(join->getInput(0), index);
964  }
965  return get_field_name(join->getInput(1), index - lhs_size);
966  }
967  if (auto project = dynamic_cast<const RelProject*>(node)) {
968  return project->getFieldName(index);
969  }
970  CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node));
971  return get_field_name(node->getInput(0), index);
972 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::string join(T const &container, std::string const &delim)
#define CHECK_LT(x, y)
Definition: Logger.h:207
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:197
const RelAlgNode * getInput(const size_t idx) const
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:

◆ get_live_ins()

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 621 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().

623  {
624  if (!node || dynamic_cast<const RelScan*>(node)) {
625  return {};
626  }
627  RexInputCollector collector(node);
628  auto it = live_outs.find(node);
629  CHECK(it != live_outs.end());
630  auto live_out = it->second;
631  if (auto project = dynamic_cast<const RelProject*>(node)) {
632  CHECK_EQ(size_t(1), project->inputCount());
633  std::unordered_set<size_t> live_in;
634  for (const auto& idx : live_out) {
635  CHECK_LT(idx, project->size());
636  auto partial_in = collector.visit(project->getProjectAt(idx));
637  for (auto rex_in : partial_in) {
638  live_in.insert(rex_in.getIndex());
639  }
640  }
641  if (project->size() == 1 &&
642  dynamic_cast<const RexLiteral*>(project->getProjectAt(0))) {
643  CHECK(live_in.empty());
644  live_in.insert(pick_always_live_col_idx(project->getInput(0)));
645  }
646  return {live_in};
647  }
648  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
649  CHECK_EQ(size_t(1), aggregate->inputCount());
650  const auto group_key_count = static_cast<size_t>(aggregate->getGroupByCount());
651  const auto agg_expr_count = static_cast<size_t>(aggregate->getAggExprsCount());
652  std::unordered_set<size_t> live_in;
653  for (size_t i = 0; i < group_key_count; ++i) {
654  live_in.insert(i);
655  }
656  bool has_count_star_only{false};
657  for (const auto& idx : live_out) {
658  if (idx < group_key_count) {
659  continue;
660  }
661  const auto agg_idx = idx - group_key_count;
662  CHECK_LT(agg_idx, agg_expr_count);
663  const auto& cur_agg_expr = aggregate->getAggExprs()[agg_idx];
664  const auto n_operands = cur_agg_expr->size();
665  for (size_t i = 0; i < n_operands; ++i) {
666  live_in.insert(static_cast<size_t>(cur_agg_expr->getOperand(i)));
667  }
668  if (n_operands == 0) {
669  has_count_star_only = true;
670  }
671  }
672  if (has_count_star_only && !group_key_count) {
673  live_in.insert(size_t(0));
674  }
675  return {live_in};
676  }
677  if (auto join = dynamic_cast<const RelJoin*>(node)) {
678  std::unordered_set<size_t> lhs_live_ins;
679  std::unordered_set<size_t> rhs_live_ins;
680  CHECK_EQ(size_t(2), join->inputCount());
681  auto lhs = join->getInput(0);
682  auto rhs = join->getInput(1);
683  const auto rhs_idx_base = lhs->size();
684  for (const auto idx : live_out) {
685  if (idx < rhs_idx_base) {
686  lhs_live_ins.insert(idx);
687  } else {
688  rhs_live_ins.insert(idx - rhs_idx_base);
689  }
690  }
691  auto rex_ins = collector.visit(join->getCondition());
692  for (const auto& rex_in : rex_ins) {
693  const auto in_idx = static_cast<size_t>(rex_in.getIndex());
694  if (rex_in.getSourceNode() == lhs) {
695  lhs_live_ins.insert(in_idx);
696  continue;
697  }
698  if (rex_in.getSourceNode() == rhs) {
699  rhs_live_ins.insert(in_idx);
700  continue;
701  }
702  CHECK(false);
703  }
704  return {lhs_live_ins, rhs_live_ins};
705  }
706  if (auto sort = dynamic_cast<const RelSort*>(node)) {
707  CHECK_EQ(size_t(1), sort->inputCount());
708  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
709  for (size_t i = 0; i < sort->collationCount(); ++i) {
710  live_in.insert(sort->getCollation(i).getField());
711  }
712  return {live_in};
713  }
714  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
715  CHECK_EQ(size_t(1), filter->inputCount());
716  std::unordered_set<size_t> live_in(live_out.begin(), live_out.end());
717  auto rex_ins = collector.visit(filter->getCondition());
718  for (const auto& rex_in : rex_ins) {
719  live_in.insert(static_cast<size_t>(rex_in.getIndex()));
720  }
721  return {live_in};
722  }
723  if (auto table_func = dynamic_cast<const RelTableFunction*>(node)) {
724  CHECK_EQ(size_t(1), table_func->inputCount());
725 
726  const auto input_count = table_func->size();
727  std::unordered_set<size_t> live_in;
728  for (size_t i = 0; i < input_count; i++) {
729  live_in.insert(i);
730  }
731  return {live_in};
732  }
733  if (auto logical_union = dynamic_cast<const RelLogicalUnion*>(node)) {
734  return std::vector<std::unordered_set<size_t>>(logical_union->inputCount(), live_out);
735  }
736  return {};
737 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
size_t pick_always_live_col_idx(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
#define CHECK_LT(x, y)
Definition: Logger.h:207
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_visible_projects()

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

Definition at line 345 of file RelAlgOptimizer.cpp.

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

Referenced by eliminate_identical_copy().

345  {
346  if (auto project = dynamic_cast<const RelProject*>(root)) {
347  return {project};
348  }
349 
350  if (dynamic_cast<const RelAggregate*>(root) || dynamic_cast<const RelScan*>(root) ||
351  dynamic_cast<const RelLogicalValues*>(root) ||
352  dynamic_cast<const RelModify*>(root)) {
353  return std::unordered_set<const RelProject*>{};
354  }
355 
356  if (auto join = dynamic_cast<const RelJoin*>(root)) {
357  auto lhs_projs = get_visible_projects(join->getInput(0));
358  auto rhs_projs = get_visible_projects(join->getInput(1));
359  lhs_projs.insert(rhs_projs.begin(), rhs_projs.end());
360  return lhs_projs;
361  }
362 
363  if (auto logical_union = dynamic_cast<const RelLogicalUnion*>(root)) {
364  auto projections = get_visible_projects(logical_union->getInput(0));
365  for (size_t i = 1; i < logical_union->inputCount(); ++i) {
366  auto next = get_visible_projects(logical_union->getInput(i));
367  projections.insert(next.begin(), next.end());
368  }
369  return projections;
370  }
371 
372  CHECK(dynamic_cast<const RelFilter*>(root) || dynamic_cast<const RelSort*>(root))
373  << "root = " << root->toString();
374  return get_visible_projects(root->getInput(0));
375 }
std::string join(T const &container, std::string const &delim)
std::unordered_set< const RelProject * > get_visible_projects(const RelAlgNode *root)
virtual std::string toString() const =0
#define CHECK(condition)
Definition: Logger.h:197
const RelAlgNode * getInput(const size_t idx) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_distinct()

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

Definition at line 378 of file RelAlgOptimizer.cpp.

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

Referenced by Parser::FunctionRef::analyze(), Parser::QuerySpec::analyze(), Analyzer::AggExpr::deep_copy(), eliminate_identical_copy(), Analyzer::AggExpr::get_is_distinct(), Analyzer::Query::get_is_distinct(), Parser::QuerySpec::get_is_distinct(), Analyzer::AggExpr::operator==(), Analyzer::AggExpr::rewrite_with_child_targetlist(), Analyzer::Query::set_is_distinct(), Parser::QuerySpec::to_string(), Analyzer::AggExpr::toString(), and RelAlgTranslator::translateAggregateRex().

378  {
379  if (dynamic_cast<const RelFilter*>(node) || dynamic_cast<const RelSort*>(node)) {
380  CHECK_EQ(size_t(1), node->inputCount());
381  return is_distinct(input_idx, node->getInput(0));
382  }
383  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
384  CHECK_EQ(size_t(1), node->inputCount());
385  if (aggregate->getGroupByCount() == 1 && !input_idx) {
386  return true;
387  }
388  if (input_idx < aggregate->getGroupByCount()) {
389  return is_distinct(input_idx, node->getInput(0));
390  }
391  return false;
392  }
393  if (auto project = dynamic_cast<const RelProject*>(node)) {
394  CHECK_LT(input_idx, project->size());
395  if (auto input = dynamic_cast<const RexInput*>(project->getProjectAt(input_idx))) {
396  CHECK_EQ(size_t(1), node->inputCount());
397  return is_distinct(input->getIndex(), project->getInput(0));
398  }
399  return false;
400  }
401  CHECK(dynamic_cast<const RelJoin*>(node) || dynamic_cast<const RelScan*>(node));
402  return false;
403 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
#define CHECK_LT(x, y)
Definition: Logger.h:207
const size_t inputCount() const
#define CHECK(condition)
Definition: Logger.h:197
const RelAlgNode * getInput(const size_t idx) 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:

◆ is_identical_copy()

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  dynamic_cast<const RelLogicalUnion*>(only_usr)) {
155  return false;
156  }
157  CHECK(dynamic_cast<const RelFilter*>(only_usr))
158  << "only_usr: " << only_usr->toString();
159  usrs_it = du_web.find(only_usr);
160  CHECK(usrs_it != du_web.end());
161  }
162 
163  if (!guard_found) {
164  return false;
165  }
166  }
167 
168  bool identical = true;
169  for (size_t i = 0; i < project->size(); ++i) {
170  auto target = dynamic_cast<const RexInput*>(project->getProjectAt(i));
171  CHECK(target);
172  if (i != target->getIndex()) {
173  identical = false;
174  break;
175  }
176  }
177 
178  if (identical) {
179  return true;
180  }
181 
182  if (safe_to_redirect(project, du_web)) {
183  permutating_projects.insert(project);
184  return true;
185  }
186 
187  return false;
188 }
size_t size() const override
const RexScalar * getProjectAt(const size_t idx) const
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)
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mark_live_columns()

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 900 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().

901  {
902  std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>> live_outs;
903  std::vector<const RelAlgNode*> work_set;
904  for (auto node_it = nodes.rbegin(); node_it != nodes.rend(); ++node_it) {
905  auto node = node_it->get();
906  if (dynamic_cast<const RelScan*>(node) || live_outs.count(node) ||
907  dynamic_cast<const RelModify*>(node)) {
908  continue;
909  }
910  std::vector<size_t> all_live(node->size());
911  std::iota(all_live.begin(), all_live.end(), size_t(0));
912  live_outs.insert(std::make_pair(
913  node, std::unordered_set<size_t>(all_live.begin(), all_live.end())));
914  work_set.push_back(node);
915  while (!work_set.empty()) {
916  auto walker = work_set.back();
917  work_set.pop_back();
918  CHECK(!dynamic_cast<const RelScan*>(walker));
919  CHECK(live_outs.count(walker));
920  auto live_ins = get_live_ins(walker, live_outs);
921  CHECK_EQ(live_ins.size(), walker->inputCount());
922  for (size_t i = 0; i < walker->inputCount(); ++i) {
923  auto src = walker->getInput(i);
924  if (dynamic_cast<const RelScan*>(src) || live_ins[i].empty()) {
925  continue;
926  }
927  if (!live_outs.count(src)) {
928  live_outs.insert(std::make_pair(src, std::unordered_set<size_t>{}));
929  }
930  auto src_it = live_outs.find(src);
931  CHECK(src_it != live_outs.end());
932  auto& live_out = src_it->second;
933  bool changed = false;
934  if (!live_out.empty()) {
935  live_out.insert(live_ins[i].begin(), live_ins[i].end());
936  changed = true;
937  } else {
938  for (int idx : live_ins[i]) {
939  changed |= live_out.insert(idx).second;
940  }
941  }
942  if (changed) {
943  work_set.push_back(src);
944  }
945  }
946  }
947  }
948  return live_outs;
949 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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)
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pick_always_live_col_idx()

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

Definition at line 592 of file RelAlgOptimizer.cpp.

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

Referenced by get_live_ins().

592  {
593  CHECK(node->size());
594  RexInputCollector collector(node);
595  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
596  auto rex_ins = collector.visit(filter->getCondition());
597  if (!rex_ins.empty()) {
598  return static_cast<size_t>(rex_ins.begin()->getIndex());
599  }
600  return pick_always_live_col_idx(filter->getInput(0));
601  } else if (auto join = dynamic_cast<const RelJoin*>(node)) {
602  auto rex_ins = collector.visit(join->getCondition());
603  if (!rex_ins.empty()) {
604  return static_cast<size_t>(rex_ins.begin()->getIndex());
605  }
606  if (auto lhs_idx = pick_always_live_col_idx(join->getInput(0))) {
607  return lhs_idx;
608  }
609  if (auto rhs_idx = pick_always_live_col_idx(join->getInput(0))) {
610  return rhs_idx + join->getInput(0)->size();
611  }
612  } else if (auto sort = dynamic_cast<const RelSort*>(node)) {
613  if (sort->collationCount()) {
614  return sort->getCollation(0).getField();
615  }
616  return pick_always_live_col_idx(sort->getInput(0));
617  }
618  return size_t(0);
619 }
size_t pick_always_live_col_idx(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ propagate_input_renumbering()

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 1096 of file RelAlgOptimizer.cpp.

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

Referenced by eliminate_dead_columns().

1104  {
1105  RexInputRenumberVisitor renumberer(liveout_renumbering);
1106  AvailabilityChecker checker(liveout_renumbering, intact_nodes);
1107  std::deque<const RelAlgNode*> work_set(ready_nodes.begin(), ready_nodes.end());
1108  while (!work_set.empty()) {
1109  auto walker = work_set.front();
1110  work_set.pop_front();
1111  CHECK(!dynamic_cast<const RelScan*>(walker));
1112  auto node = const_cast<RelAlgNode*>(walker);
1113  if (auto project = dynamic_cast<RelProject*>(node)) {
1114  auto old_exprs = project->getExpressionsAndRelease();
1115  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1116  for (auto& expr : old_exprs) {
1117  new_exprs.push_back(renumberer.visit(expr.get()));
1118  }
1119  project->setExpressions(new_exprs);
1120  } else if (auto aggregate = dynamic_cast<RelAggregate*>(node)) {
1121  auto src_it = liveout_renumbering.find(node->getInput(0));
1122  CHECK(src_it != liveout_renumbering.end());
1123  auto old_exprs = aggregate->getAggExprsAndRelease();
1124  auto new_exprs = renumber_rex_aggs(old_exprs, src_it->second);
1125  aggregate->setAggExprs(new_exprs);
1126  } else if (auto join = dynamic_cast<RelJoin*>(node)) {
1127  auto new_condition = renumberer.visit(join->getCondition());
1128  join->setCondition(new_condition);
1129  } else if (auto filter = dynamic_cast<RelFilter*>(node)) {
1130  auto new_condition = renumberer.visit(filter->getCondition());
1131  filter->setCondition(new_condition);
1132  } else if (auto sort = dynamic_cast<RelSort*>(node)) {
1133  auto src_it = liveout_renumbering.find(node->getInput(0));
1134  CHECK(src_it != liveout_renumbering.end());
1135  std::vector<SortField> new_collations;
1136  for (size_t i = 0; i < sort->collationCount(); ++i) {
1137  new_collations.push_back(
1138  renumber_sort_field(sort->getCollation(i), src_it->second));
1139  }
1140  sort->setCollation(std::move(new_collations));
1141  } else if (!dynamic_cast<RelLogicalUnion*>(node)) {
1142  LOG(FATAL) << "Unhandled node type: " << node->toString();
1143  }
1144 
1145  // Ignore empty live_out due to some invalid node
1146  if (does_redef_cols(node) || intact_nodes.count(node)) {
1147  continue;
1148  }
1149  auto live_pair = old_liveouts.find(node);
1150  CHECK(live_pair != old_liveouts.end());
1151  auto live_out = live_pair->second;
1153  node, liveout_renumbering, live_out, intact_nodes, orig_node_sizes);
1154  auto usrs_it = du_web.find(walker);
1155  CHECK(usrs_it != du_web.end());
1156  for (auto usr : usrs_it->second) {
1157  if (checker.hasAllSrcReady(usr)) {
1158  work_set.push_back(usr);
1159  }
1160  }
1161  }
1162 }
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)
#define LOG(tag)
Definition: Logger.h:188
bool does_redef_cols(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
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)
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ propagate_rex_input_renumber()

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 190 of file RelAlgOptimizer.cpp.

References CHECK, and RelAlgNode::getInput().

Referenced by redirect_inputs_of().

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

◆ redirect_inputs_of()

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 240 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().

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

◆ renumber_rex_aggs()

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 866 of file RelAlgOptimizer.cpp.

Referenced by propagate_input_renumbering().

868  {
869  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
870  for (auto& expr : agg_exprs) {
871  if (expr->size() >= 1) {
872  auto old_idx = expr->getOperand(0);
873  auto idx_it = new_numbering.find(old_idx);
874  if (idx_it != new_numbering.end()) {
875  std::vector<size_t> operands;
876  operands.push_back(idx_it->second);
877  if (expr->size() == 2) {
878  operands.push_back(expr->getOperand(1));
879  }
880  new_exprs.push_back(boost::make_unique<RexAgg>(
881  expr->getKind(), expr->isDistinct(), expr->getType(), operands));
882  continue;
883  }
884  }
885  new_exprs.push_back(std::move(expr));
886  }
887  return new_exprs;
888 }
+ Here is the caller graph for this function:

◆ renumber_sort_field()

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 890 of file RelAlgOptimizer.cpp.

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

Referenced by propagate_input_renumbering().

891  {
892  auto field_idx = old_field.getField();
893  auto idx_it = new_numbering.find(field_idx);
894  if (idx_it != new_numbering.end()) {
895  field_idx = idx_it->second;
896  }
897  return SortField(field_idx, old_field.getSortDir(), old_field.getNullsPosition());
898 }
SortDirection getSortDir() const
NullSortedPosition getNullsPosition() const
size_t getField() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replace_all_usages()

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 1397 of file RelAlgOptimizer.cpp.

References CHECK.

Referenced by fold_filters().

1402  {
1403  auto usrs_it = du_web.find(old_def_node.get());
1404  RexInputRedirector redirector(new_def_node.get(), old_def_node.get());
1405  CHECK(usrs_it != du_web.end());
1406  for (auto usr : usrs_it->second) {
1407  auto usr_it = deconst_mapping.find(usr);
1408  CHECK(usr_it != deconst_mapping.end());
1409  usr_it->second->replaceInput(old_def_node, new_def_node);
1410  }
1411  auto new_usrs_it = du_web.find(new_def_node.get());
1412  CHECK(new_usrs_it != du_web.end());
1413  new_usrs_it->second.insert(usrs_it->second.begin(), usrs_it->second.end());
1414  usrs_it->second.clear();
1415 }
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the caller graph for this function:

◆ safe_to_redirect()

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 }
bool isSimple() const
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sweep_dead_columns()

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 1033 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().

1039  {
1040  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>
1041  liveouts_renumbering;
1042  std::vector<const RelAlgNode*> ready_nodes;
1043  AvailabilityChecker checker(liveouts_renumbering, intact_nodes);
1044  for (auto node : nodes) {
1045  // Ignore empty live_out due to some invalid node
1046  if (!does_redef_cols(node.get()) || intact_nodes.count(node.get())) {
1047  continue;
1048  }
1049  auto live_pair = live_outs.find(node.get());
1050  CHECK(live_pair != live_outs.end());
1051  auto old_live_outs = live_pair->second;
1053  node.get(), liveouts_renumbering, old_live_outs, intact_nodes, orig_node_sizes);
1054  if (auto aggregate = std::dynamic_pointer_cast<RelAggregate>(node)) {
1055  auto old_exprs = aggregate->getAggExprsAndRelease();
1056  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
1057  auto key_name_it = aggregate->getFields().begin();
1058  std::vector<std::string> new_fields(key_name_it,
1059  key_name_it + aggregate->getGroupByCount());
1060  for (size_t i = aggregate->getGroupByCount(), j = 0;
1061  i < aggregate->getFields().size() && j < old_exprs.size();
1062  ++i, ++j) {
1063  if (old_live_outs.count(i)) {
1064  new_exprs.push_back(std::move(old_exprs[j]));
1065  new_fields.push_back(aggregate->getFieldName(i));
1066  }
1067  }
1068  aggregate->setAggExprs(new_exprs);
1069  aggregate->setFields(new_fields);
1070  } else if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
1071  auto old_exprs = project->getExpressionsAndRelease();
1072  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1073  std::vector<std::string> new_fields;
1074  for (size_t i = 0; i < old_exprs.size(); ++i) {
1075  if (old_live_outs.count(i)) {
1076  new_exprs.push_back(std::move(old_exprs[i]));
1077  new_fields.push_back(project->getFieldName(i));
1078  }
1079  }
1080  project->setExpressions(new_exprs);
1081  project->setFields(new_fields);
1082  } else {
1083  CHECK(false);
1084  }
1085  auto usrs_it = du_web.find(node.get());
1086  CHECK(usrs_it != du_web.end());
1087  for (auto usr : usrs_it->second) {
1088  if (checker.hasAllSrcReady(usr)) {
1089  ready_nodes.push_back(usr);
1090  }
1091  }
1092  }
1093  return {liveouts_renumbering, ready_nodes};
1094 }
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)
bool does_redef_cols(const RelAlgNode *node)
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ try_insert_coalesceable_proj()

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 974 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().

978  {
979  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
980  for (auto node : nodes) {
981  new_nodes.push_back(node);
982  if (!std::dynamic_pointer_cast<RelFilter>(node)) {
983  continue;
984  }
985  const auto filter = node.get();
986  auto liveout_it = liveouts.find(filter);
987  CHECK(liveout_it != liveouts.end());
988  auto& outs = liveout_it->second;
989  if (!any_dead_col_in(filter, outs)) {
990  continue;
991  }
992  auto usrs_it = du_web.find(filter);
993  CHECK(usrs_it != du_web.end());
994  auto& usrs = usrs_it->second;
995  if (usrs.size() != 1 || does_redef_cols(*usrs.begin())) {
996  continue;
997  }
998  auto only_usr = const_cast<RelAlgNode*>(*usrs.begin());
999 
1000  std::vector<std::unique_ptr<const RexScalar>> exprs;
1001  std::vector<std::string> fields;
1002  for (size_t i = 0; i < filter->size(); ++i) {
1003  exprs.push_back(boost::make_unique<RexInput>(filter, i));
1004  fields.push_back(get_field_name(filter, i));
1005  }
1006  auto project_owner = std::make_shared<RelProject>(exprs, fields, node);
1007  auto project = project_owner.get();
1008 
1009  only_usr->replaceInput(node, project_owner);
1010  if (dynamic_cast<const RelJoin*>(only_usr)) {
1011  RexRebindInputsVisitor visitor(filter, project);
1012  for (auto usr : du_web[only_usr]) {
1013  visitor.visitNode(usr);
1014  }
1015  }
1016 
1017  liveouts.insert(std::make_pair(project, outs));
1018 
1019  usrs.clear();
1020  usrs.insert(project);
1021  du_web.insert(
1022  std::make_pair(project, std::unordered_set<const RelAlgNode*>{only_usr}));
1023 
1024  new_nodes.push_back(project_owner);
1025  }
1026  if (new_nodes.size() > nodes.size()) {
1027  nodes.swap(new_nodes);
1028  }
1029 }
bool does_redef_cols(const RelAlgNode *node)
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)
#define CHECK(condition)
Definition: Logger.h:197
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: