OmniSciDB  2b310ab3b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RelAlgDagBuilder Class Reference

#include <RelAlgDagBuilder.h>

+ Inheritance diagram for RelAlgDagBuilder:
+ Collaboration diagram for RelAlgDagBuilder:

Public Member Functions

 RelAlgDagBuilder ()=delete
 
 RelAlgDagBuilder (const std::string &query_ra, const Catalog_Namespace::Catalog &cat, const RenderInfo *render_info)
 
 RelAlgDagBuilder (RelAlgDagBuilder &root_dag_builder, const rapidjson::Value &query_ast, const Catalog_Namespace::Catalog &cat, const RenderInfo *render_opts)
 
void eachNode (std::function< void(RelAlgNode const *)> const &) const
 
const RelAlgNodegetRootNode () const
 
std::shared_ptr< const RelAlgNodegetRootNodeShPtr () const
 
void registerSubquery (std::shared_ptr< RexSubQuery > subquery)
 
const std::vector
< std::shared_ptr< RexSubQuery > > & 
getSubqueries () const
 
void registerQueryHints (QueryHint &query_hint)
 
const QueryHint getQueryHints () const
 
void resetQueryExecutionState ()
 

Private Member Functions

void build (const rapidjson::Value &query_ast, RelAlgDagBuilder &root_dag_builder)
 

Private Attributes

const Catalog_Namespace::Catalogcat_
 
std::vector< std::shared_ptr
< RelAlgNode > > 
nodes_
 
std::vector< std::shared_ptr
< RexSubQuery > > 
subqueries_
 
const RenderInforender_info_
 
QueryHint query_hint_
 

Detailed Description

Builder class to create an in-memory, easy-to-navigate relational algebra DAG interpreted from a JSON representation from Calcite. Also, applies high level optimizations which can be expressed through relational algebra extended with RelCompound. The RelCompound node is an equivalent representation for sequences of RelFilter, RelProject and RelAggregate nodes. This coalescing minimizes the amount of intermediate buffers required to evaluate a query. Lower level optimizations are taken care by lower levels, mainly RelAlgTranslator and the IR code generation.

Definition at line 1819 of file RelAlgDagBuilder.h.

Constructor & Destructor Documentation

RelAlgDagBuilder::RelAlgDagBuilder ( )
delete
RelAlgDagBuilder::RelAlgDagBuilder ( const std::string &  query_ra,
const Catalog_Namespace::Catalog cat,
const RenderInfo render_info 
)

Constructs a RelAlg DAG from a JSON representation.

Parameters
query_raA JSON string representation of an RA tree from Calcite.
catDB catalog for the current user.
render_optsAdditional build options for render queries.

Definition at line 2576 of file RelAlgDagBuilder.cpp.

References build(), CHECK(), logger::ERROR, LOG, RelAlgNode::resetRelAlgFirstId(), and VLOG.

2579  : cat_(cat), render_info_(render_info) {
2580  rapidjson::Document query_ast;
2581  query_ast.Parse(query_ra.c_str());
2582  VLOG(2) << "Parsing query RA JSON: " << query_ra;
2583  if (query_ast.HasParseError()) {
2584  query_ast.GetParseError();
2585  LOG(ERROR) << "Failed to parse RA tree from Calcite (offset "
2586  << query_ast.GetErrorOffset() << "):\n"
2587  << rapidjson::GetParseError_En(query_ast.GetParseError());
2588  VLOG(1) << "Failed to parse query RA: " << query_ra;
2589  throw std::runtime_error(
2590  "Failed to parse relational algebra tree. Possible query syntax error.");
2591  }
2592  CHECK(query_ast.IsObject());
2594  build(query_ast, *this);
2595 }
#define LOG(tag)
Definition: Logger.h:188
void build(const rapidjson::Value &query_ast, RelAlgDagBuilder &root_dag_builder)
const Catalog_Namespace::Catalog & cat_
const RenderInfo * render_info_
CHECK(cgen_state)
#define VLOG(n)
Definition: Logger.h:291
static void resetRelAlgFirstId() noexcept

+ Here is the call graph for this function:

RelAlgDagBuilder::RelAlgDagBuilder ( RelAlgDagBuilder root_dag_builder,
const rapidjson::Value &  query_ast,
const Catalog_Namespace::Catalog cat,
const RenderInfo render_opts 
)

Constructs a sub-DAG for any subqueries. Should only be called during DAG building.

Parameters
root_dag_builderThe root DAG builder. The root stores pointers to all subqueries.
query_astThe current JSON node to build a DAG for.
catDB catalog for the current user.
render_optsAdditional build options for render queries.

Definition at line 2597 of file RelAlgDagBuilder.cpp.

References build().

2601  : cat_(cat), render_info_(render_info) {
2602  build(query_ast, root_dag_builder);
2603 }
void build(const rapidjson::Value &query_ast, RelAlgDagBuilder &root_dag_builder)
const Catalog_Namespace::Catalog & cat_
const RenderInfo * render_info_

+ Here is the call graph for this function:

Member Function Documentation

void RelAlgDagBuilder::build ( const rapidjson::Value &  query_ast,
RelAlgDagBuilder root_dag_builder 
)
private

Definition at line 2605 of file RelAlgDagBuilder.cpp.

References anonymous_namespace{RelAlgDagBuilder.cpp}::add_window_function_pre_project(), alterRAForRender(), anonymous_namespace{RelAlgDagBuilder.cpp}::bind_inputs(), cat_, CHECK(), anonymous_namespace{RelAlgDagBuilder.cpp}::coalesce_nodes(), anonymous_namespace{RelLeftDeepInnerJoin.cpp}::create_left_deep_join(), eliminate_dead_columns(), eliminate_dead_subqueries(), eliminate_identical_copy(), field(), fold_filters(), g_cluster, get_left_deep_join_root(), anonymous_namespace{RelAlgDagBuilder.cpp}::handleQueryHint(), hoist_filter_cond_to_cross_join(), anonymous_namespace{RelAlgDagBuilder.cpp}::mark_nops(), nodes_, render_info_, details::RelAlgDispatcher::run(), anonymous_namespace{RelAlgDagBuilder.cpp}::separate_window_function_expressions(), simplify_sort(), sink_projected_boolean_expr_to_join(), and subqueries_.

Referenced by RelAlgDagBuilder().

2606  {
2607  const auto& rels = field(query_ast, "rels");
2608  CHECK(rels.IsArray());
2609  try {
2610  nodes_ = details::RelAlgDispatcher(cat_).run(rels, lead_dag_builder);
2611  } catch (const QueryNotSupported&) {
2612  throw;
2613  }
2614  CHECK(!nodes_.empty());
2616 
2617  if (render_info_) {
2618  // Alter the RA for render. Do this before any flattening/optimizations are done to
2619  // the tree.
2621  }
2622 
2623  handleQueryHint(nodes_, this);
2624  mark_nops(nodes_);
2629  std::vector<const RelAlgNode*> filtered_left_deep_joins;
2630  std::vector<const RelAlgNode*> left_deep_joins;
2631  for (const auto& node : nodes_) {
2632  const auto left_deep_join_root = get_left_deep_join_root(node);
2633  // The filter which starts a left-deep join pattern must not be coalesced
2634  // since it contains (part of) the join condition.
2635  if (left_deep_join_root) {
2636  left_deep_joins.push_back(left_deep_join_root.get());
2637  if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
2638  filtered_left_deep_joins.push_back(left_deep_join_root.get());
2639  }
2640  }
2641  }
2642  if (filtered_left_deep_joins.empty()) {
2644  }
2645  eliminate_dead_columns(nodes_);
2646  eliminate_dead_subqueries(subqueries_, nodes_.back().get());
2648  if (g_cluster) {
2650  }
2651  coalesce_nodes(nodes_, left_deep_joins);
2652  CHECK(nodes_.back().unique());
2653  create_left_deep_join(nodes_);
2654 }
void handleQueryHint(const std::vector< std::shared_ptr< RelAlgNode >> &nodes, RelAlgDagBuilder *dag_builder) noexcept
void bind_inputs(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void hoist_filter_cond_to_cross_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::shared_ptr< const RelAlgNode > get_left_deep_join_root(const std::shared_ptr< RelAlgNode > &node)
void sink_projected_boolean_expr_to_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void eliminate_identical_copy(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::pair< std::shared_ptr< RelLeftDeepInnerJoin >, std::shared_ptr< const RelAlgNode > > create_left_deep_join(const std::shared_ptr< RelAlgNode > &left_deep_join_root)
const Catalog_Namespace::Catalog & cat_
void simplify_sort(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::vector< std::shared_ptr< RexSubQuery > > subqueries_
const RenderInfo * render_info_
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
std::vector< std::shared_ptr< RelAlgNode > > nodes_
CHECK(cgen_state)
void add_window_function_pre_project(std::vector< std::shared_ptr< RelAlgNode >> &nodes)
void separate_window_function_expressions(std::vector< std::shared_ptr< RelAlgNode >> &nodes)
void mark_nops(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
bool g_cluster
void alterRAForRender(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const RenderInfo &render_info)
std::vector< std::shared_ptr< RelAlgNode > > run(const rapidjson::Value &rels, RelAlgDagBuilder &root_dag_builder)
void fold_filters(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void eliminate_dead_subqueries(std::vector< std::shared_ptr< RexSubQuery >> &subqueries, RelAlgNode const *root)
void eliminate_dead_columns(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void coalesce_nodes(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const std::vector< const RelAlgNode * > &left_deep_joins)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RelAlgDagBuilder::eachNode ( std::function< void(RelAlgNode const *)> const &  callback) const

Definition at line 2656 of file RelAlgDagBuilder.cpp.

References nodes_.

2657  {
2658  for (auto const& node : nodes_) {
2659  if (node) {
2660  callback(node.get());
2661  }
2662  }
2663 }
std::vector< std::shared_ptr< RelAlgNode > > nodes_
const QueryHint RelAlgDagBuilder::getQueryHints ( ) const
inline

Definition at line 1881 of file RelAlgDagBuilder.h.

1881 { return query_hint_; }
const RelAlgNode& RelAlgDagBuilder::getRootNode ( ) const
inline

Returns the root node of the DAG.

Definition at line 1852 of file RelAlgDagBuilder.h.

References CHECK().

1852  {
1853  CHECK(nodes_.size());
1854  const auto& last_ptr = nodes_.back();
1855  CHECK(last_ptr);
1856  return *last_ptr;
1857  }
std::vector< std::shared_ptr< RelAlgNode > > nodes_
CHECK(cgen_state)

+ Here is the call graph for this function:

std::shared_ptr<const RelAlgNode> RelAlgDagBuilder::getRootNodeShPtr ( ) const
inline

Definition at line 1859 of file RelAlgDagBuilder.h.

References CHECK().

Referenced by anonymous_namespace{RelAlgDagBuilder.cpp}::parse_subquery().

1859  {
1860  CHECK(nodes_.size());
1861  return nodes_.back();
1862  }
std::vector< std::shared_ptr< RelAlgNode > > nodes_
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::vector<std::shared_ptr<RexSubQuery> >& RelAlgDagBuilder::getSubqueries ( ) const
inline

Gets all registered subqueries. Only the root DAG can contain subqueries.

Definition at line 1875 of file RelAlgDagBuilder.h.

1875  {
1876  return subqueries_;
1877  }
std::vector< std::shared_ptr< RexSubQuery > > subqueries_
void RelAlgDagBuilder::registerQueryHints ( QueryHint query_hint)
inline

Definition at line 1879 of file RelAlgDagBuilder.h.

1879 { query_hint_ = query_hint; }
void RelAlgDagBuilder::registerSubquery ( std::shared_ptr< RexSubQuery subquery)
inline

Registers a subquery with a root DAG builder. Should only be called during DAG building and registration should only occur on the root.

Definition at line 1868 of file RelAlgDagBuilder.h.

Referenced by anonymous_namespace{RelAlgDagBuilder.cpp}::parse_subquery().

1868  {
1869  subqueries_.push_back(subquery);
1870  }
std::vector< std::shared_ptr< RexSubQuery > > subqueries_

+ Here is the caller graph for this function:

void RelAlgDagBuilder::resetQueryExecutionState ( )

Gets all registered subqueries. Only the root DAG can contain subqueries.

Definition at line 2665 of file RelAlgDagBuilder.cpp.

References nodes_.

2665  {
2666  for (auto& node : nodes_) {
2667  if (node) {
2668  node->resetQueryExecutionState();
2669  }
2670  }
2671 }
std::vector< std::shared_ptr< RelAlgNode > > nodes_

Member Data Documentation

const Catalog_Namespace::Catalog& RelAlgDagBuilder::cat_
private

Definition at line 1891 of file RelAlgDagBuilder.h.

Referenced by build().

std::vector<std::shared_ptr<RelAlgNode> > RelAlgDagBuilder::nodes_
private

Definition at line 1892 of file RelAlgDagBuilder.h.

Referenced by build(), eachNode(), and resetQueryExecutionState().

QueryHint RelAlgDagBuilder::query_hint_
private

Definition at line 1895 of file RelAlgDagBuilder.h.

const RenderInfo* RelAlgDagBuilder::render_info_
private

Definition at line 1894 of file RelAlgDagBuilder.h.

Referenced by build().

std::vector<std::shared_ptr<RexSubQuery> > RelAlgDagBuilder::subqueries_
private

Definition at line 1893 of file RelAlgDagBuilder.h.

Referenced by build().


The documentation for this class was generated from the following files: