OmniSciDB  fe05a0c208
 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 793 of file RelAlgOptimizer.cpp.

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

Referenced by propagate_input_renumbering(), and sweep_dead_columns().

799  {
800  auto live_fields = old_liveouts;
801  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
802  for (size_t i = 0; i < aggregate->getGroupByCount(); ++i) {
803  live_fields.insert(i);
804  }
805  }
806  auto it_ok =
807  new_liveouts.insert(std::make_pair(node, std::unordered_map<size_t, size_t>{}));
808  CHECK(it_ok.second);
809  auto& new_indices = it_ok.first->second;
810  if (intact_nodes.count(node)) {
811  for (size_t i = 0, e = node->size(); i < e; ++i) {
812  new_indices.insert(std::make_pair(i, i));
813  }
814  return;
815  }
816  if (does_redef_cols(node)) {
817  auto node_sz_it = orig_node_sizes.find(node);
818  CHECK(node_sz_it != orig_node_sizes.end());
819  const auto node_size = node_sz_it->second;
820  CHECK_GT(node_size, live_fields.size());
821  LOG(INFO) << node->toString() << " eliminated " << node_size - live_fields.size()
822  << " columns.";
823  std::vector<size_t> ordered_indices(live_fields.begin(), live_fields.end());
824  std::sort(ordered_indices.begin(), ordered_indices.end());
825  for (size_t i = 0; i < ordered_indices.size(); ++i) {
826  new_indices.insert(std::make_pair(ordered_indices[i], i));
827  }
828  return;
829  }
830  std::vector<size_t> ordered_indices;
831  for (size_t i = 0, old_base = 0, new_base = 0; i < node->inputCount(); ++i) {
832  auto src = node->getInput(i);
833  auto src_renum_it = new_liveouts.find(src);
834  if (src_renum_it != new_liveouts.end()) {
835  for (auto m : src_renum_it->second) {
836  new_indices.insert(std::make_pair(old_base + m.first, new_base + m.second));
837  }
838  new_base += src_renum_it->second.size();
839  } else if (dynamic_cast<const RelScan*>(src) || intact_nodes.count(src)) {
840  for (size_t i = 0; i < src->size(); ++i) {
841  new_indices.insert(std::make_pair(old_base + i, new_base + i));
842  }
843  new_base += src->size();
844  } else {
845  CHECK(false);
846  }
847  auto src_sz_it = orig_node_sizes.find(src);
848  CHECK(src_sz_it != orig_node_sizes.end());
849  old_base += src_sz_it->second;
850  }
851 }
#define LOG(tag)
Definition: Logger.h:194
bool does_redef_cols(const RelAlgNode *node)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
#define CHECK_GT(x, y)
Definition: Logger.h:215
const RelAlgNode * getInput(const size_t idx) const
virtual size_t size() const =0
virtual std::string toString() const =0
#define CHECK(condition)
Definition: Logger.h:203
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 749 of file RelAlgOptimizer.cpp.

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

Referenced by eliminate_dead_columns(), and try_insert_coalesceable_proj().

750  {
751  CHECK(!dynamic_cast<const RelScan*>(node));
752  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
753  for (size_t i = aggregate->getGroupByCount(); i < aggregate->size(); ++i) {
754  if (!live_outs.count(i)) {
755  return true;
756  }
757  }
758  return false;
759  }
760 
761  return node->size() > live_outs.size();
762 }
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:203

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

References logger::INFO, and LOG.

Referenced by eliminate_identical_copy(), and fold_filters().

330  {
331  for (auto nodeIt = nodes.rbegin(); nodeIt != nodes.rend(); ++nodeIt) {
332  if (nodeIt->use_count() == 1) {
333  LOG(INFO) << "ID=" << (*nodeIt)->getId() << " " << (*nodeIt)->toString()
334  << " deleted!";
335  nodeIt->reset();
336  }
337  }
338 
339  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
340  for (auto node : nodes) {
341  if (!node) {
342  continue;
343  }
344  new_nodes.push_back(node);
345  }
346  nodes.swap(new_nodes);
347 }
#define LOG(tag)
Definition: Logger.h:194

+ Here is the caller graph for this function:

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

Definition at line 764 of file RelAlgOptimizer.cpp.

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

764  {
765  return dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelProject*>(node);
766 }

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

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

Referenced by is_identical_copy().

100  {
101  auto source = curr_project->getInput(0);
102  while (auto filter = dynamic_cast<const RelFilter*>(source)) {
103  source = filter->getInput(0);
104  }
105  if (auto src_project = dynamic_cast<const RelProject*>(source)) {
106  if (projects_to_remove.count(src_project)) {
107  return get_actual_source_size(src_project, projects_to_remove);
108  }
109  }
110  return curr_project->getInput(0)->size();
111 }
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 964 of file RelAlgOptimizer.cpp.

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

Referenced by try_insert_coalesceable_proj().

964  {
965  CHECK_LT(index, node->size());
966  if (auto scan = dynamic_cast<const RelScan*>(node)) {
967  return scan->getFieldName(index);
968  }
969  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
970  CHECK_EQ(aggregate->size(), aggregate->getFields().size());
971  return aggregate->getFieldName(index);
972  }
973  if (auto join = dynamic_cast<const RelJoin*>(node)) {
974  const auto lhs_size = join->getInput(0)->size();
975  if (index < lhs_size) {
976  return get_field_name(join->getInput(0), index);
977  }
978  return get_field_name(join->getInput(1), index - lhs_size);
979  }
980  if (auto project = dynamic_cast<const RelProject*>(node)) {
981  return project->getFieldName(index);
982  }
983  CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node));
984  return get_field_name(node->getInput(0), index);
985 }
#define CHECK_EQ(x, y)
Definition: Logger.h:211
std::string join(T const &container, std::string const &delim)
const RelAlgNode * getInput(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:213
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:203
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 626 of file RelAlgOptimizer.cpp.

References CHECK, CHECK_EQ, CHECK_LT, i, join(), pick_always_live_col_idx(), run_benchmark_import::result, gpu_enabled::sort(), and RexVisitorBase< T >::visit().

Referenced by mark_live_columns().

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

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

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

Referenced by eliminate_identical_copy().

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

+ 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 382 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(), and RelAlgTranslator::translateAggregateRex().

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

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

Referenced by eliminate_identical_copy().

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

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

References CHECK, CHECK_EQ, get_live_ins(), i, and gpu_enabled::iota().

Referenced by eliminate_dead_columns(), and sink_projected_boolean_expr_to_join().

911  {
912  std::unordered_map<const RelAlgNode*, std::unordered_set<size_t>> live_outs;
913  std::vector<const RelAlgNode*> work_set;
914  for (auto node_it = nodes.rbegin(); node_it != nodes.rend(); ++node_it) {
915  auto node = node_it->get();
916  if (dynamic_cast<const RelScan*>(node) || live_outs.count(node) ||
917  dynamic_cast<const RelModify*>(node) ||
918  dynamic_cast<const RelTableFunction*>(node)) {
919  continue;
920  }
921  std::vector<size_t> all_live(node->size());
922  std::iota(all_live.begin(), all_live.end(), size_t(0));
923  live_outs.insert(std::make_pair(
924  node, std::unordered_set<size_t>(all_live.begin(), all_live.end())));
925 
926  work_set.push_back(node);
927  while (!work_set.empty()) {
928  auto walker = work_set.back();
929  work_set.pop_back();
930  CHECK(!dynamic_cast<const RelScan*>(walker));
931  CHECK(live_outs.count(walker));
932  auto live_ins = get_live_ins(walker, live_outs);
933  CHECK_EQ(live_ins.size(), walker->inputCount());
934  for (size_t i = 0; i < walker->inputCount(); ++i) {
935  auto src = walker->getInput(i);
936  if (dynamic_cast<const RelScan*>(src) ||
937  dynamic_cast<const RelTableFunction*>(src) || live_ins[i].empty()) {
938  continue;
939  }
940  if (!live_outs.count(src)) {
941  live_outs.insert(std::make_pair(src, std::unordered_set<size_t>{}));
942  }
943  auto src_it = live_outs.find(src);
944  CHECK(src_it != live_outs.end());
945  auto& live_out = src_it->second;
946  bool changed = false;
947  if (!live_out.empty()) {
948  live_out.insert(live_ins[i].begin(), live_ins[i].end());
949  changed = true;
950  } else {
951  for (int idx : live_ins[i]) {
952  changed |= live_out.insert(idx).second;
953  }
954  }
955  if (changed) {
956  work_set.push_back(src);
957  }
958  }
959  }
960  }
961  return live_outs;
962 }
#define CHECK_EQ(x, y)
Definition: Logger.h:211
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)
DEVICE void iota(ARGS &&...args)
Definition: gpu_enabled.h:69
#define CHECK(condition)
Definition: Logger.h:203

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

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

Referenced by get_live_ins().

597  {
598  CHECK(node->size());
599  RexInputCollector collector(node);
600  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
601  auto rex_ins = collector.visit(filter->getCondition());
602  if (!rex_ins.empty()) {
603  return static_cast<size_t>(rex_ins.begin()->getIndex());
604  }
605  return pick_always_live_col_idx(filter->getInput(0));
606  } else if (auto join = dynamic_cast<const RelJoin*>(node)) {
607  auto rex_ins = collector.visit(join->getCondition());
608  if (!rex_ins.empty()) {
609  return static_cast<size_t>(rex_ins.begin()->getIndex());
610  }
611  if (auto lhs_idx = pick_always_live_col_idx(join->getInput(0))) {
612  return lhs_idx;
613  }
614  if (auto rhs_idx = pick_always_live_col_idx(join->getInput(0))) {
615  return rhs_idx + join->getInput(0)->size();
616  }
617  } else if (auto sort = dynamic_cast<const RelSort*>(node)) {
618  if (sort->collationCount()) {
619  return sort->getCollation(0).getField();
620  }
621  return pick_always_live_col_idx(sort->getInput(0));
622  }
623  return size_t(0);
624 }
size_t pick_always_live_col_idx(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:203

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

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

Referenced by eliminate_dead_columns().

1117  {
1118  RexInputRenumberVisitor renumberer(liveout_renumbering);
1119  AvailabilityChecker checker(liveout_renumbering, intact_nodes);
1120  std::deque<const RelAlgNode*> work_set(ready_nodes.begin(), ready_nodes.end());
1121  while (!work_set.empty()) {
1122  auto walker = work_set.front();
1123  work_set.pop_front();
1124  CHECK(!dynamic_cast<const RelScan*>(walker));
1125  auto node = const_cast<RelAlgNode*>(walker);
1126  if (auto project = dynamic_cast<RelProject*>(node)) {
1127  auto old_exprs = project->getExpressionsAndRelease();
1128  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1129  for (auto& expr : old_exprs) {
1130  new_exprs.push_back(renumberer.visit(expr.get()));
1131  }
1132  project->setExpressions(new_exprs);
1133  } else if (auto aggregate = dynamic_cast<RelAggregate*>(node)) {
1134  auto src_it = liveout_renumbering.find(node->getInput(0));
1135  CHECK(src_it != liveout_renumbering.end());
1136  auto old_exprs = aggregate->getAggExprsAndRelease();
1137  auto new_exprs = renumber_rex_aggs(old_exprs, src_it->second);
1138  aggregate->setAggExprs(new_exprs);
1139  } else if (auto join = dynamic_cast<RelJoin*>(node)) {
1140  auto new_condition = renumberer.visit(join->getCondition());
1141  join->setCondition(new_condition);
1142  } else if (auto filter = dynamic_cast<RelFilter*>(node)) {
1143  auto new_condition = renumberer.visit(filter->getCondition());
1144  filter->setCondition(new_condition);
1145  } else if (auto sort = dynamic_cast<RelSort*>(node)) {
1146  auto src_it = liveout_renumbering.find(node->getInput(0));
1147  CHECK(src_it != liveout_renumbering.end());
1148  std::vector<SortField> new_collations;
1149  for (size_t i = 0; i < sort->collationCount(); ++i) {
1150  new_collations.push_back(
1151  renumber_sort_field(sort->getCollation(i), src_it->second));
1152  }
1153  sort->setCollation(std::move(new_collations));
1154  } else if (!dynamic_cast<RelLogicalUnion*>(node)) {
1155  LOG(FATAL) << "Unhandled node type: " << node->toString();
1156  }
1157 
1158  // Ignore empty live_out due to some invalid node
1159  if (does_redef_cols(node) || intact_nodes.count(node)) {
1160  continue;
1161  }
1162  auto live_pair = old_liveouts.find(node);
1163  CHECK(live_pair != old_liveouts.end());
1164  auto live_out = live_pair->second;
1166  node, liveout_renumbering, live_out, intact_nodes, orig_node_sizes);
1167  auto usrs_it = du_web.find(walker);
1168  CHECK(usrs_it != du_web.end());
1169  for (auto usr : usrs_it->second) {
1170  if (checker.hasAllSrcReady(usr)) {
1171  work_set.push_back(usr);
1172  }
1173  }
1174  }
1175 }
#define LOG(tag)
Definition: Logger.h:194
bool does_redef_cols(const RelAlgNode *node)
std::string join(T const &container, std::string const &delim)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
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:203
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 191 of file RelAlgOptimizer.cpp.

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

Referenced by redirect_inputs_of().

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

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

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

Referenced by eliminate_identical_copy().

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

Referenced by propagate_input_renumbering().

878  {
879  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
880  for (auto& expr : agg_exprs) {
881  if (expr->size() >= 1) {
882  auto old_idx = expr->getOperand(0);
883  auto idx_it = new_numbering.find(old_idx);
884  if (idx_it != new_numbering.end()) {
885  std::vector<size_t> operands;
886  operands.push_back(idx_it->second);
887  if (expr->size() == 2) {
888  operands.push_back(expr->getOperand(1));
889  }
890  new_exprs.push_back(boost::make_unique<RexAgg>(
891  expr->getKind(), expr->isDistinct(), expr->getType(), operands));
892  continue;
893  }
894  }
895  new_exprs.push_back(std::move(expr));
896  }
897  return new_exprs;
898 }

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

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

Referenced by propagate_input_renumbering().

901  {
902  auto field_idx = old_field.getField();
903  auto idx_it = new_numbering.find(field_idx);
904  if (idx_it != new_numbering.end()) {
905  field_idx = idx_it->second;
906  }
907  return SortField(field_idx, old_field.getSortDir(), old_field.getNullsPosition());
908 }
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 1437 of file RelAlgOptimizer.cpp.

References CHECK.

Referenced by fold_filters().

1442  {
1443  auto usrs_it = du_web.find(old_def_node.get());
1444  RexInputRedirector redirector(new_def_node.get(), old_def_node.get());
1445  CHECK(usrs_it != du_web.end());
1446  for (auto usr : usrs_it->second) {
1447  auto usr_it = deconst_mapping.find(usr);
1448  CHECK(usr_it != deconst_mapping.end());
1449  usr_it->second->replaceInput(old_def_node, new_def_node);
1450  }
1451  auto new_usrs_it = du_web.find(new_def_node.get());
1452  CHECK(new_usrs_it != du_web.end());
1453  new_usrs_it->second.insert(usrs_it->second.begin(), usrs_it->second.end());
1454  usrs_it->second.clear();
1455 }
#define CHECK(condition)
Definition: Logger.h:203

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

References CHECK, and RelProject::isSimple().

Referenced by is_identical_copy().

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

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

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

Referenced by eliminate_dead_columns().

1052  {
1053  std::unordered_map<const RelAlgNode*, std::unordered_map<size_t, size_t>>
1054  liveouts_renumbering;
1055  std::vector<const RelAlgNode*> ready_nodes;
1056  AvailabilityChecker checker(liveouts_renumbering, intact_nodes);
1057  for (auto node : nodes) {
1058  // Ignore empty live_out due to some invalid node
1059  if (!does_redef_cols(node.get()) || intact_nodes.count(node.get())) {
1060  continue;
1061  }
1062  auto live_pair = live_outs.find(node.get());
1063  CHECK(live_pair != live_outs.end());
1064  auto old_live_outs = live_pair->second;
1066  node.get(), liveouts_renumbering, old_live_outs, intact_nodes, orig_node_sizes);
1067  if (auto aggregate = std::dynamic_pointer_cast<RelAggregate>(node)) {
1068  auto old_exprs = aggregate->getAggExprsAndRelease();
1069  std::vector<std::unique_ptr<const RexAgg>> new_exprs;
1070  auto key_name_it = aggregate->getFields().begin();
1071  std::vector<std::string> new_fields(key_name_it,
1072  key_name_it + aggregate->getGroupByCount());
1073  for (size_t i = aggregate->getGroupByCount(), j = 0;
1074  i < aggregate->getFields().size() && j < old_exprs.size();
1075  ++i, ++j) {
1076  if (old_live_outs.count(i)) {
1077  new_exprs.push_back(std::move(old_exprs[j]));
1078  new_fields.push_back(aggregate->getFieldName(i));
1079  }
1080  }
1081  aggregate->setAggExprs(new_exprs);
1082  aggregate->setFields(new_fields);
1083  } else if (auto project = std::dynamic_pointer_cast<RelProject>(node)) {
1084  auto old_exprs = project->getExpressionsAndRelease();
1085  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1086  std::vector<std::string> new_fields;
1087  for (size_t i = 0; i < old_exprs.size(); ++i) {
1088  if (old_live_outs.count(i)) {
1089  new_exprs.push_back(std::move(old_exprs[i]));
1090  new_fields.push_back(project->getFieldName(i));
1091  }
1092  }
1093  project->setExpressions(new_exprs);
1094  project->setFields(new_fields);
1095  } else {
1096  CHECK(false);
1097  }
1098  auto usrs_it = du_web.find(node.get());
1099  CHECK(usrs_it != du_web.end());
1100  for (auto usr : usrs_it->second) {
1101  if (checker.hasAllSrcReady(usr)) {
1102  ready_nodes.push_back(usr);
1103  }
1104  }
1105  }
1106  return {liveouts_renumbering, ready_nodes};
1107 }
bool does_redef_cols(const RelAlgNode *node)
#define CHECK(condition)
Definition: Logger.h:203
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 987 of file RelAlgOptimizer.cpp.

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

Referenced by eliminate_dead_columns().

991  {
992  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
993  for (auto node : nodes) {
994  new_nodes.push_back(node);
995  if (!std::dynamic_pointer_cast<RelFilter>(node)) {
996  continue;
997  }
998  const auto filter = node.get();
999  auto liveout_it = liveouts.find(filter);
1000  CHECK(liveout_it != liveouts.end());
1001  auto& outs = liveout_it->second;
1002  if (!any_dead_col_in(filter, outs)) {
1003  continue;
1004  }
1005  auto usrs_it = du_web.find(filter);
1006  CHECK(usrs_it != du_web.end());
1007  auto& usrs = usrs_it->second;
1008  if (usrs.size() != 1 || does_redef_cols(*usrs.begin())) {
1009  continue;
1010  }
1011  auto only_usr = const_cast<RelAlgNode*>(*usrs.begin());
1012 
1013  std::vector<std::unique_ptr<const RexScalar>> exprs;
1014  std::vector<std::string> fields;
1015  for (size_t i = 0; i < filter->size(); ++i) {
1016  exprs.push_back(boost::make_unique<RexInput>(filter, i));
1017  fields.push_back(get_field_name(filter, i));
1018  }
1019  auto project_owner = std::make_shared<RelProject>(exprs, fields, node);
1020  auto project = project_owner.get();
1021 
1022  only_usr->replaceInput(node, project_owner);
1023  if (dynamic_cast<const RelJoin*>(only_usr)) {
1024  RexRebindInputsVisitor visitor(filter, project);
1025  for (auto usr : du_web[only_usr]) {
1026  visitor.visitNode(usr);
1027  }
1028  }
1029 
1030  liveouts.insert(std::make_pair(project, outs));
1031 
1032  usrs.clear();
1033  usrs.insert(project);
1034  du_web.insert(
1035  std::make_pair(project, std::unordered_set<const RelAlgNode*>{only_usr}));
1036 
1037  new_nodes.push_back(project_owner);
1038  }
1039  if (new_nodes.size() > nodes.size()) {
1040  nodes.swap(new_nodes);
1041  }
1042 }
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:203
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: