OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 798 of file RelAlgOptimizer.cpp.

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

Referenced by propagate_input_renumbering(), and sweep_dead_columns().

804  {
805  auto live_fields = old_liveouts;
806  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
807  for (size_t i = 0; i < aggregate->getGroupByCount(); ++i) {
808  live_fields.insert(i);
809  }
810  }
811  auto it_ok =
812  new_liveouts.insert(std::make_pair(node, std::unordered_map<size_t, size_t>{}));
813  CHECK(it_ok.second);
814  auto& new_indices = it_ok.first->second;
815  if (intact_nodes.count(node)) {
816  for (size_t i = 0, e = node->size(); i < e; ++i) {
817  new_indices.insert(std::make_pair(i, i));
818  }
819  return;
820  }
821  if (does_redef_cols(node)) {
822  auto node_sz_it = orig_node_sizes.find(node);
823  CHECK(node_sz_it != orig_node_sizes.end());
824  const auto node_size = node_sz_it->second;
825  CHECK_GT(node_size, live_fields.size());
826  auto node_str = node->toString(RelRexToStringConfig::defaults());
827  auto [node_substr, post_fix] = ::substring(node_str, g_max_log_length);
828  LOG(INFO) << node_substr << post_fix << " eliminated "
829  << node_size - live_fields.size() << " columns.";
830  std::vector<size_t> ordered_indices(live_fields.begin(), live_fields.end());
831  std::sort(ordered_indices.begin(), ordered_indices.end());
832  for (size_t i = 0; i < ordered_indices.size(); ++i) {
833  new_indices.insert(std::make_pair(ordered_indices[i], i));
834  }
835  return;
836  }
837  std::vector<size_t> ordered_indices;
838  for (size_t i = 0, old_base = 0, new_base = 0; i < node->inputCount(); ++i) {
839  auto src = node->getInput(i);
840  auto src_renum_it = new_liveouts.find(src);
841  if (src_renum_it != new_liveouts.end()) {
842  for (auto m : src_renum_it->second) {
843  new_indices.insert(std::make_pair(old_base + m.first, new_base + m.second));
844  }
845  new_base += src_renum_it->second.size();
846  } else if (dynamic_cast<const RelScan*>(src) || intact_nodes.count(src)) {
847  for (size_t i = 0; i < src->size(); ++i) {
848  new_indices.insert(std::make_pair(old_base + i, new_base + i));
849  }
850  new_base += src->size();
851  } else {
852  CHECK(false);
853  }
854  auto src_sz_it = orig_node_sizes.find(src);
855  CHECK(src_sz_it != orig_node_sizes.end());
856  old_base += src_sz_it->second;
857  }
858 }
#define LOG(tag)
Definition: Logger.h:285
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:305
const RelAlgNode * getInput(const size_t idx) const
Definition: RelAlgDag.h:877
size_t g_max_log_length
Definition: Execute.cpp:176
virtual std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const =0
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:291
const size_t inputCount() const
Definition: RelAlgDag.h:875
std::pair< std::string_view, const char * > substring(const std::string &str, size_t substr_length)
return substring of str with postfix if str.size() &gt; substr_length

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

References CHECK, and RelAlgNode::size().

Referenced by eliminate_dead_columns(), and try_insert_coalesceable_proj().

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

+ 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::DEBUG2, RelRexToStringConfig::defaults(), logger::fast_logging_check(), g_max_log_length, substring(), and VLOG.

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  VLOG(1) << "Node (ID: " << (*nodeIt)->getId() << ") deleted.";
335  auto node_str = (*nodeIt)->toString(RelRexToStringConfig::defaults());
336  auto [node_substr, post_fix] = ::substring(node_str, g_max_log_length);
337  VLOG(2) << "Deleted Node (ID: " << (*nodeIt)->getId()
338  << ") contents: " << node_substr << post_fix;
339  }
340  nodeIt->reset();
341  }
342  }
343 
344  std::vector<std::shared_ptr<RelAlgNode>> new_nodes;
345  for (auto node : nodes) {
346  if (!node) {
347  continue;
348  }
349  new_nodes.push_back(node);
350  }
351  nodes.swap(new_nodes);
352 }
bool fast_logging_check(Channel)
Definition: Logger.h:265
size_t g_max_log_length
Definition: Execute.cpp:176
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
#define VLOG(n)
Definition: Logger.h:388
std::pair< std::string_view, const char * > substring(const std::string &str, size_t substr_length)
return substring of str with postfix if str.size() &gt; substr_length

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 769 of file RelAlgOptimizer.cpp.

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

769  {
770  return dynamic_cast<const RelAggregate*>(node) || dynamic_cast<const RelProject*>(node);
771 }

+ 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
Definition: RelAlgDag.h:877
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 971 of file RelAlgOptimizer.cpp.

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

Referenced by try_insert_coalesceable_proj().

971  {
972  CHECK_LT(index, node->size());
973  if (auto scan = dynamic_cast<const RelScan*>(node)) {
974  return scan->getFieldName(index);
975  }
976  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
977  CHECK_EQ(aggregate->size(), aggregate->getFields().size());
978  return aggregate->getFieldName(index);
979  }
980  if (auto join = dynamic_cast<const RelJoin*>(node)) {
981  const auto lhs_size = join->getInput(0)->size();
982  if (index < lhs_size) {
983  return get_field_name(join->getInput(0), index);
984  }
985  return get_field_name(join->getInput(1), index - lhs_size);
986  }
987  if (auto project = dynamic_cast<const RelProject*>(node)) {
988  return project->getFieldName(index);
989  }
990  CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node));
991  return get_field_name(node->getInput(0), index);
992 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::string join(T const &container, std::string const &delim)
const RelAlgNode * getInput(const size_t idx) const
Definition: RelAlgDag.h:877
#define CHECK_LT(x, y)
Definition: Logger.h:303
virtual size_t size() const =0
#define CHECK(condition)
Definition: Logger.h:291
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 631 of file RelAlgOptimizer.cpp.

References CHECK, CHECK_EQ, CHECK_LT, join(), pick_always_live_col_idx(), run_benchmark_import::result, and gpu_enabled::sort().

Referenced by mark_live_columns().

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

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

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

Referenced by eliminate_identical_copy().

354  {
355  if (auto project = dynamic_cast<const RelProject*>(root)) {
356  return {project};
357  }
358 
359  if (dynamic_cast<const RelAggregate*>(root) || dynamic_cast<const RelScan*>(root) ||
360  dynamic_cast<const RelLogicalValues*>(root) ||
361  dynamic_cast<const RelModify*>(root)) {
362  return std::unordered_set<const RelProject*>{};
363  }
364 
365  if (auto join = dynamic_cast<const RelJoin*>(root)) {
366  auto lhs_projs = get_visible_projects(join->getInput(0));
367  auto rhs_projs = get_visible_projects(join->getInput(1));
368  lhs_projs.insert(rhs_projs.begin(), rhs_projs.end());
369  return lhs_projs;
370  }
371 
372  if (auto logical_union = dynamic_cast<const RelLogicalUnion*>(root)) {
373  auto projections = get_visible_projects(logical_union->getInput(0));
374  for (size_t i = 1; i < logical_union->inputCount(); ++i) {
375  auto next = get_visible_projects(logical_union->getInput(i));
376  projections.insert(next.begin(), next.end());
377  }
378  return projections;
379  }
380 
381  CHECK(dynamic_cast<const RelFilter*>(root) || dynamic_cast<const RelSort*>(root))
382  << "root = " << root->toString(RelRexToStringConfig::defaults());
383  return get_visible_projects(root->getInput(0));
384 }
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
Definition: RelAlgDag.h:877
virtual std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const =0
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
#define CHECK(condition)
Definition: Logger.h:291

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

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

Referenced by Parser::FunctionRef::analyze(), eliminate_identical_copy(), target_info::get_target_info_impl(), and RelAlgTranslator::translateAggregateRex().

387  {
388  if (dynamic_cast<const RelFilter*>(node) || dynamic_cast<const RelSort*>(node)) {
389  CHECK_EQ(size_t(1), node->inputCount());
390  return is_distinct(input_idx, node->getInput(0));
391  }
392  if (auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
393  CHECK_EQ(size_t(1), node->inputCount());
394  if (aggregate->getGroupByCount() == 1 && !input_idx) {
395  return true;
396  }
397  if (input_idx < aggregate->getGroupByCount()) {
398  return is_distinct(input_idx, node->getInput(0));
399  }
400  return false;
401  }
402  if (auto project = dynamic_cast<const RelProject*>(node)) {
403  CHECK_LT(input_idx, project->size());
404  if (auto input = dynamic_cast<const RexInput*>(project->getProjectAt(input_idx))) {
405  CHECK_EQ(size_t(1), node->inputCount());
406  return is_distinct(input->getIndex(), project->getInput(0));
407  }
408  return false;
409  }
410  CHECK(dynamic_cast<const RelJoin*>(node) || dynamic_cast<const RelScan*>(node));
411  return false;
412 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const RelAlgNode * getInput(const size_t idx) const
Definition: RelAlgDag.h:877
#define CHECK_LT(x, y)
Definition: Logger.h:303
#define CHECK(condition)
Definition: Logger.h:291
const size_t inputCount() const
Definition: RelAlgDag.h:875
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, RelRexToStringConfig::defaults(), get_actual_source_size(), RelProject::getProjectAt(), 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(RelRexToStringConfig::defaults());
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
Definition: RelAlgDag.h:1320
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
Definition: RelAlgDag.h:1352
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
#define CHECK(condition)
Definition: Logger.h:291

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

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

Referenced by eliminate_dead_columns(), and sink_projected_boolean_expr_to_join().

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

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

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

Referenced by get_live_ins().

602  {
603  CHECK(node->size());
604  RexInputCollector collector(node);
605  if (auto filter = dynamic_cast<const RelFilter*>(node)) {
606  auto rex_ins = collector.visit(filter->getCondition());
607  if (!rex_ins.empty()) {
608  return static_cast<size_t>(rex_ins.begin()->getIndex());
609  }
610  return pick_always_live_col_idx(filter->getInput(0));
611  } else if (auto join = dynamic_cast<const RelJoin*>(node)) {
612  auto rex_ins = collector.visit(join->getCondition());
613  if (!rex_ins.empty()) {
614  return static_cast<size_t>(rex_ins.begin()->getIndex());
615  }
616  if (auto lhs_idx = pick_always_live_col_idx(join->getInput(0))) {
617  return lhs_idx;
618  }
619  if (auto rhs_idx = pick_always_live_col_idx(join->getInput(0))) {
620  return rhs_idx + join->getInput(0)->size();
621  }
622  } else if (auto sort = dynamic_cast<const RelSort*>(node)) {
623  if (sort->collationCount()) {
624  return sort->getCollation(0).getField();
625  }
626  return pick_always_live_col_idx(sort->getInput(0));
627  }
628  return size_t(0);
629 }
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:291

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

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

Referenced by eliminate_dead_columns().

1124  {
1125  RexInputRenumberVisitor renumberer(liveout_renumbering);
1126  AvailabilityChecker checker(liveout_renumbering, intact_nodes);
1127  std::deque<const RelAlgNode*> work_set(ready_nodes.begin(), ready_nodes.end());
1128  while (!work_set.empty()) {
1129  auto walker = work_set.front();
1130  work_set.pop_front();
1131  CHECK(!dynamic_cast<const RelScan*>(walker));
1132  auto node = const_cast<RelAlgNode*>(walker);
1133  if (auto project = dynamic_cast<RelProject*>(node)) {
1134  auto old_exprs = project->getExpressionsAndRelease();
1135  std::vector<std::unique_ptr<const RexScalar>> new_exprs;
1136  for (auto& expr : old_exprs) {
1137  new_exprs.push_back(renumberer.visit(expr.get()));
1138  }
1139  project->setExpressions(new_exprs);
1140  } else if (auto aggregate = dynamic_cast<RelAggregate*>(node)) {
1141  auto src_it = liveout_renumbering.find(node->getInput(0));
1142  CHECK(src_it != liveout_renumbering.end());
1143  auto old_exprs = aggregate->getAggExprsAndRelease();
1144  auto new_exprs = renumber_rex_aggs(old_exprs, src_it->second);
1145  aggregate->setAggExprs(new_exprs);
1146  } else if (auto join = dynamic_cast<RelJoin*>(node)) {
1147  auto new_condition = renumberer.visit(join->getCondition());
1148  join->setCondition(new_condition);
1149  } else if (auto filter = dynamic_cast<RelFilter*>(node)) {
1150  auto new_condition = renumberer.visit(filter->getCondition());
1151  filter->setCondition(new_condition);
1152  } else if (auto sort = dynamic_cast<RelSort*>(node)) {
1153  auto src_it = liveout_renumbering.find(node->getInput(0));
1154  CHECK(src_it != liveout_renumbering.end());
1155  std::vector<SortField> new_collations;
1156  for (size_t i = 0; i < sort->collationCount(); ++i) {
1157  new_collations.push_back(
1158  renumber_sort_field(sort->getCollation(i), src_it->second));
1159  }
1160  sort->setCollation(std::move(new_collations));
1161  } else if (!dynamic_cast<RelLogicalUnion*>(node)) {
1162  LOG(FATAL) << "Unhandled node type: "
1163  << node->toString(RelRexToStringConfig::defaults());
1164  }
1165 
1166  // Ignore empty live_out due to some invalid node
1167  if (does_redef_cols(node) || intact_nodes.count(node)) {
1168  continue;
1169  }
1170  auto live_pair = old_liveouts.find(node);
1171  CHECK(live_pair != old_liveouts.end());
1172  auto live_out = live_pair->second;
1174  node, liveout_renumbering, live_out, intact_nodes, orig_node_sizes);
1175  auto usrs_it = du_web.find(walker);
1176  CHECK(usrs_it != du_web.end());
1177  for (auto usr : usrs_it->second) {
1178  if (checker.hasAllSrcReady(usr)) {
1179  work_set.push_back(usr);
1180  }
1181  }
1182  }
1183 }
#define LOG(tag)
Definition: Logger.h:285
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
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
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:291
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, and RelAlgNode::getInput().

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
Definition: RelAlgDag.h:877
#define CHECK(condition)
Definition: Logger.h:291

+ 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, 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:291
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 883 of file RelAlgOptimizer.cpp.

Referenced by propagate_input_renumbering().

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

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

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

Referenced by propagate_input_renumbering().

908  {
909  auto field_idx = old_field.getField();
910  auto idx_it = new_numbering.find(field_idx);
911  if (idx_it != new_numbering.end()) {
912  field_idx = idx_it->second;
913  }
914  return SortField(field_idx, old_field.getSortDir(), old_field.getNullsPosition());
915 }
SortDirection getSortDir() const
Definition: RelAlgDag.h:549
size_t getField() const
Definition: RelAlgDag.h:547
NullSortedPosition getNullsPosition() const
Definition: RelAlgDag.h:551

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

References CHECK.

Referenced by fold_filters().

1451  {
1452  auto usrs_it = du_web.find(old_def_node.get());
1453  RexInputRedirector redirector(new_def_node.get(), old_def_node.get());
1454  CHECK(usrs_it != du_web.end());
1455  for (auto usr : usrs_it->second) {
1456  auto usr_it = deconst_mapping.find(usr);
1457  CHECK(usr_it != deconst_mapping.end());
1458  usr_it->second->replaceInput(old_def_node, new_def_node);
1459  }
1460  auto new_usrs_it = du_web.find(new_def_node.get());
1461  CHECK(new_usrs_it != du_web.end());
1462  new_usrs_it->second.insert(usrs_it->second.begin(), usrs_it->second.end());
1463  usrs_it->second.clear();
1464 }
#define CHECK(condition)
Definition: Logger.h:291

+ 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
Definition: RelAlgDag.h:1307
#define CHECK(condition)
Definition: Logger.h:291

+ 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 1053 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().

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

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