OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgDagBuilder Struct Reference

#include <RelAlgDag.h>

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

Static Public Member Functions

static std::unique_ptr< RelAlgDagbuildDag (const std::string &query_ra, const bool optimize_dag)
 
static std::unique_ptr< RelAlgDagbuildDagForSubquery (RelAlgDag &root_dag, const rapidjson::Value &query_ast)
 
static void optimizeDag (RelAlgDag &rel_alg_dag)
 

Static Private Member Functions

static std::unique_ptr< RelAlgDagbuild (const rapidjson::Value &query_ast, RelAlgDag *root_dag, const bool optimize_dag)
 

Additional Inherited Members

- Static Protected Member Functions inherited from RelAlgDagModifier
static std::vector
< std::shared_ptr< RelAlgNode > > & 
getNodes (RelAlgDag &rel_alg_dag)
 
static std::vector
< std::shared_ptr< RexSubQuery > > & 
getSubqueries (RelAlgDag &rel_alg_dag)
 
static std::unordered_map
< size_t, std::unordered_map
< unsigned,
RegisteredQueryHint > > & 
getQueryHints (RelAlgDag &rel_alg_dag)
 
static void setBuildState (RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
 

Detailed Description

Builder struct to create a RelAlgDag instance. Can optionally apply 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 3432 of file RelAlgDag.h.

Member Function Documentation

std::unique_ptr< RelAlgDag > RelAlgDagBuilder::build ( const rapidjson::Value &  query_ast,
RelAlgDag root_dag,
const bool  optimize_dag 
)
staticprivate

Definition at line 3384 of file RelAlgDag.cpp.

References anonymous_namespace{RelAlgDag.cpp}::bind_inputs(), CHECK, field(), RelAlgDagModifier::getNodes(), RelAlgDag::kBuiltNotOptimized, optimizeDag(), details::RelAlgDispatcher::run(), and RelAlgDagModifier::setBuildState().

Referenced by buildDag(), and buildDagForSubquery().

3386  {
3387  const auto& rels = field(query_ast, "rels");
3388  CHECK(rels.IsArray());
3389 
3390  auto rel_alg_dag_ptr = std::make_unique<RelAlgDag>();
3391  auto& rel_alg_dag = *rel_alg_dag_ptr;
3392  auto& nodes = getNodes(rel_alg_dag);
3393 
3394  try {
3395  nodes = details::RelAlgDispatcher().run(rels, root_dag ? *root_dag : rel_alg_dag);
3396  } catch (const QueryNotSupported&) {
3397  throw;
3398  }
3399  CHECK(!nodes.empty());
3400  bind_inputs(nodes);
3401 
3403 
3404  if (optimize_dag) {
3405  optimizeDag(rel_alg_dag);
3406  }
3407 
3408  return rel_alg_dag_ptr;
3409 }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
void bind_inputs(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
Definition: RelAlgDag.cpp:1532
std::vector< std::shared_ptr< RelAlgNode > > run(const rapidjson::Value &rels, RelAlgDag &root_dag)
Definition: RelAlgDag.cpp:2894
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3404
static void optimizeDag(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.cpp:3411
#define CHECK(condition)
Definition: Logger.h:291
static void setBuildState(RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
Definition: RelAlgDag.h:3418

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::unique_ptr< RelAlgDag > RelAlgDagBuilder::buildDag ( const std::string &  query_ra,
const bool  optimize_dag 
)
static

Constructs a RelAlg DAG from a relative-algebra JSON representation.

Parameters
query_raA JSON string representation of an RA tree from Calcite.
catDB catalog for the current user.

Definition at line 3358 of file RelAlgDag.cpp.

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

3359  {
3360  rapidjson::Document query_ast;
3361  query_ast.Parse(query_ra.c_str());
3362  VLOG(2) << "Parsing query RA JSON: " << query_ra;
3363  if (query_ast.HasParseError()) {
3364  query_ast.GetParseError();
3365  LOG(ERROR) << "Failed to parse RA tree from Calcite (offset "
3366  << query_ast.GetErrorOffset() << "):\n"
3367  << rapidjson::GetParseError_En(query_ast.GetParseError());
3368  VLOG(1) << "Failed to parse query RA: " << query_ra;
3369  throw std::runtime_error(
3370  "Failed to parse relational algebra tree. Possible query syntax error.");
3371  }
3372  CHECK(query_ast.IsObject());
3374 
3375  return build(query_ast, nullptr, optimize_dag);
3376 }
#define LOG(tag)
Definition: Logger.h:285
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, RelAlgDag *root_dag, const bool optimize_dag)
Definition: RelAlgDag.cpp:3384
#define CHECK(condition)
Definition: Logger.h:291
#define VLOG(n)
Definition: Logger.h:388
static void resetRelAlgFirstId() noexcept
Definition: RelAlgDag.cpp:47

+ Here is the call graph for this function:

std::unique_ptr< RelAlgDag > RelAlgDagBuilder::buildDagForSubquery ( RelAlgDag root_dag,
const rapidjson::Value &  query_ast 
)
static

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

Parameters
root_dagThe 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.

Definition at line 3378 of file RelAlgDag.cpp.

References build().

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

3380  {
3381  return build(query_ast, &root_dag, true);
3382 }
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, RelAlgDag *root_dag, const bool optimize_dag)
Definition: RelAlgDag.cpp:3384

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RelAlgDagBuilder::optimizeDag ( RelAlgDag rel_alg_dag)
static

Definition at line 3411 of file RelAlgDag.cpp.

References anonymous_namespace{RelAlgDag.cpp}::add_window_function_pre_project(), CHECK, anonymous_namespace{RelAlgDag.cpp}::coalesce_nodes(), anonymous_namespace{RelAlgDag.cpp}::compute_node_hash(), anonymous_namespace{RelLeftDeepInnerJoin.cpp}::create_left_deep_join(), eliminate_dead_columns(), eliminate_dead_subqueries(), eliminate_identical_copy(), anonymous_namespace{RelAlgDag.cpp}::eliminate_redundant_projection(), fold_filters(), g_cluster, get_left_deep_join_root(), RelAlgDag::getBuildState(), RelAlgDagModifier::getNodes(), RelAlgDagModifier::getQueryHints(), RelAlgDagModifier::getSubqueries(), anonymous_namespace{RelAlgDag.cpp}::handle_agg_over_join(), anonymous_namespace{RelAlgDag.cpp}::handle_query_hint(), hoist_filter_cond_to_cross_join(), RelAlgDag::kBuiltNotOptimized, RelAlgDag::kBuiltOptimized, anonymous_namespace{RelAlgDag.cpp}::mark_nops(), anonymous_namespace{RelAlgDag.cpp}::separate_window_function_expressions(), RelAlgDagModifier::setBuildState(), simplify_sort(), and sink_projected_boolean_expr_to_join().

Referenced by build().

3411  {
3412  auto const build_state = rel_alg_dag.getBuildState();
3413  if (build_state == RelAlgDag::BuildState::kBuiltOptimized) {
3414  return;
3415  }
3416 
3418  << static_cast<int>(build_state);
3419 
3420  auto& nodes = getNodes(rel_alg_dag);
3421  auto& subqueries = getSubqueries(rel_alg_dag);
3422  auto& query_hints = getQueryHints(rel_alg_dag);
3423 
3424  compute_node_hash(nodes);
3425  handle_query_hint(nodes, rel_alg_dag);
3426  mark_nops(nodes);
3427  simplify_sort(nodes);
3429  eliminate_identical_copy(nodes);
3430  fold_filters(nodes);
3431  std::vector<const RelAlgNode*> filtered_left_deep_joins;
3432  std::vector<const RelAlgNode*> left_deep_joins;
3433  for (const auto& node : nodes) {
3434  const auto left_deep_join_root = get_left_deep_join_root(node);
3435  // The filter which starts a left-deep join pattern must not be coalesced
3436  // since it contains (part of) the join condition.
3437  if (left_deep_join_root) {
3438  left_deep_joins.push_back(left_deep_join_root.get());
3439  if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
3440  filtered_left_deep_joins.push_back(left_deep_join_root.get());
3441  }
3442  }
3443  }
3444  if (filtered_left_deep_joins.empty()) {
3446  }
3447  eliminate_dead_columns(nodes);
3448  eliminate_dead_subqueries(subqueries, nodes.back().get());
3449  separate_window_function_expressions(nodes, query_hints);
3451  nodes,
3452  g_cluster /* always_add_project_if_first_project_is_window_expr */,
3453  query_hints);
3454  coalesce_nodes(nodes, left_deep_joins, query_hints);
3455  CHECK(nodes.back().use_count() == 1);
3456  create_left_deep_join(nodes);
3457  handle_agg_over_join(nodes, query_hints);
3459 
3461 }
void mark_nops(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
Definition: RelAlgDag.cpp:1625
static std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint > > & getQueryHints(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3414
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_redundant_projection(std::vector< std::shared_ptr< RelAlgNode >> &nodes)
Definition: RelAlgDag.cpp:2174
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)
void handle_query_hint(const std::vector< std::shared_ptr< RelAlgNode >> &nodes, RelAlgDag &rel_alg_dag) noexcept
Definition: RelAlgDag.cpp:1573
static std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3408
void add_window_function_pre_project(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const bool always_add_project_if_first_project_is_window_expr, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
Definition: RelAlgDag.cpp:2653
void simplify_sort(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
void separate_window_function_expressions(std::vector< std::shared_ptr< RelAlgNode >> &nodes, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
Definition: RelAlgDag.cpp:2459
void coalesce_nodes(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const std::vector< const RelAlgNode * > &left_deep_joins, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
Definition: RelAlgDag.cpp:2011
void compute_node_hash(const std::vector< std::shared_ptr< RelAlgNode >> &nodes)
Definition: RelAlgDag.cpp:1611
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3404
void handle_agg_over_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
Definition: RelAlgDag.cpp:2138
#define CHECK(condition)
Definition: Logger.h:291
bool g_cluster
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)
BuildState getBuildState() const
Definition: RelAlgDag.h:2805
void eliminate_dead_columns(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
static void setBuildState(RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
Definition: RelAlgDag.h:3418

+ Here is the call graph for this function:

+ Here is the caller graph for this function:


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