OmniSciDB  bf83d84833
 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 1775 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 2575 of file RelAlgDagBuilder.cpp.

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

2578  : cat_(cat), render_info_(render_info) {
2579  rapidjson::Document query_ast;
2580  query_ast.Parse(query_ra.c_str());
2581  VLOG(2) << "Parsing query RA JSON: " << query_ra;
2582  if (query_ast.HasParseError()) {
2583  query_ast.GetParseError();
2584  LOG(ERROR) << "Failed to parse RA tree from Calcite (offset "
2585  << query_ast.GetErrorOffset() << "):\n"
2586  << rapidjson::GetParseError_En(query_ast.GetParseError());
2587  VLOG(1) << "Failed to parse query RA: " << query_ra;
2588  throw std::runtime_error(
2589  "Failed to parse relational algebra tree. Possible query syntax error.");
2590  }
2591  CHECK(query_ast.IsObject());
2593  build(query_ast, *this);
2594 }
#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_
#define CHECK(condition)
Definition: Logger.h:197
#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 2596 of file RelAlgDagBuilder.cpp.

References build().

2600  : cat_(cat), render_info_(render_info) {
2601  build(query_ast, root_dag_builder);
2602 }
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 2604 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().

2605  {
2606  const auto& rels = field(query_ast, "rels");
2607  CHECK(rels.IsArray());
2608  try {
2609  nodes_ = details::RelAlgDispatcher(cat_).run(rels, lead_dag_builder);
2610  } catch (const QueryNotSupported&) {
2611  throw;
2612  }
2613  CHECK(!nodes_.empty());
2615 
2616  if (render_info_) {
2617  // Alter the RA for render. Do this before any flattening/optimizations are done to
2618  // the tree.
2620  }
2621 
2622  handleQueryHint(nodes_, this);
2623  mark_nops(nodes_);
2628  std::vector<const RelAlgNode*> filtered_left_deep_joins;
2629  std::vector<const RelAlgNode*> left_deep_joins;
2630  for (const auto& node : nodes_) {
2631  const auto left_deep_join_root = get_left_deep_join_root(node);
2632  // The filter which starts a left-deep join pattern must not be coalesced
2633  // since it contains (part of) the join condition.
2634  if (left_deep_join_root) {
2635  left_deep_joins.push_back(left_deep_join_root.get());
2636  if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
2637  filtered_left_deep_joins.push_back(left_deep_join_root.get());
2638  }
2639  }
2640  }
2641  if (filtered_left_deep_joins.empty()) {
2643  }
2644  eliminate_dead_columns(nodes_);
2645  eliminate_dead_subqueries(subqueries_, nodes_.back().get());
2647  if (g_cluster) {
2649  }
2650  coalesce_nodes(nodes_, left_deep_joins);
2651  CHECK(nodes_.back().use_count() == 1);
2652  create_left_deep_join(nodes_);
2653 }
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_
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
#define CHECK(condition)
Definition: Logger.h:197
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 2655 of file RelAlgDagBuilder.cpp.

References nodes_.

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

Definition at line 1837 of file RelAlgDagBuilder.h.

References query_hint_.

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

Returns the root node of the DAG.

Definition at line 1808 of file RelAlgDagBuilder.h.

References CHECK, and nodes_.

1808  {
1809  CHECK(nodes_.size());
1810  const auto& last_ptr = nodes_.back();
1811  CHECK(last_ptr);
1812  return *last_ptr;
1813  }
std::vector< std::shared_ptr< RelAlgNode > > nodes_
#define CHECK(condition)
Definition: Logger.h:197
std::shared_ptr<const RelAlgNode> RelAlgDagBuilder::getRootNodeShPtr ( ) const
inline

Definition at line 1815 of file RelAlgDagBuilder.h.

References CHECK, and nodes_.

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

1815  {
1816  CHECK(nodes_.size());
1817  return nodes_.back();
1818  }
std::vector< std::shared_ptr< RelAlgNode > > nodes_
#define CHECK(condition)
Definition: Logger.h:197

+ 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 1831 of file RelAlgDagBuilder.h.

References subqueries_.

1831  {
1832  return subqueries_;
1833  }
std::vector< std::shared_ptr< RexSubQuery > > subqueries_
void RelAlgDagBuilder::registerQueryHints ( QueryHint query_hint)
inline

Definition at line 1835 of file RelAlgDagBuilder.h.

References query_hint_.

1835 { 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 1824 of file RelAlgDagBuilder.h.

References subqueries_.

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

1824  {
1825  subqueries_.push_back(subquery);
1826  }
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 2664 of file RelAlgDagBuilder.cpp.

References nodes_.

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

Member Data Documentation

const Catalog_Namespace::Catalog& RelAlgDagBuilder::cat_
private

Definition at line 1847 of file RelAlgDagBuilder.h.

Referenced by build().

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

Definition at line 1851 of file RelAlgDagBuilder.h.

Referenced by getQueryHints(), and registerQueryHints().

const RenderInfo* RelAlgDagBuilder::render_info_
private

Definition at line 1850 of file RelAlgDagBuilder.h.

Referenced by build().

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

Definition at line 1849 of file RelAlgDagBuilder.h.

Referenced by build(), getSubqueries(), and registerSubquery().


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