OmniSciDB  c1a53651b2
 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 3037 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 3263 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().

3265  {
3266  const auto& rels = field(query_ast, "rels");
3267  CHECK(rels.IsArray());
3268 
3269  auto rel_alg_dag_ptr = std::make_unique<RelAlgDag>();
3270  auto& rel_alg_dag = *rel_alg_dag_ptr;
3271  auto& nodes = getNodes(rel_alg_dag);
3272 
3273  try {
3274  nodes = details::RelAlgDispatcher().run(rels, root_dag ? *root_dag : rel_alg_dag);
3275  } catch (const QueryNotSupported&) {
3276  throw;
3277  }
3278  CHECK(!nodes.empty());
3279  bind_inputs(nodes);
3280 
3282 
3283  if (optimize_dag) {
3284  optimizeDag(rel_alg_dag);
3285  }
3286 
3287  return rel_alg_dag_ptr;
3288 }
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
void bind_inputs(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
Definition: RelAlgDag.cpp:1498
std::vector< std::shared_ptr< RelAlgNode > > run(const rapidjson::Value &rels, RelAlgDag &root_dag)
Definition: RelAlgDag.cpp:2775
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3009
static void optimizeDag(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.cpp:3290
#define CHECK(condition)
Definition: Logger.h:291
static void setBuildState(RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
Definition: RelAlgDag.h:3023

+ 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 3237 of file RelAlgDag.cpp.

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

3238  {
3239  rapidjson::Document query_ast;
3240  query_ast.Parse(query_ra.c_str());
3241  VLOG(2) << "Parsing query RA JSON: " << query_ra;
3242  if (query_ast.HasParseError()) {
3243  query_ast.GetParseError();
3244  LOG(ERROR) << "Failed to parse RA tree from Calcite (offset "
3245  << query_ast.GetErrorOffset() << "):\n"
3246  << rapidjson::GetParseError_En(query_ast.GetParseError());
3247  VLOG(1) << "Failed to parse query RA: " << query_ra;
3248  throw std::runtime_error(
3249  "Failed to parse relational algebra tree. Possible query syntax error.");
3250  }
3251  CHECK(query_ast.IsObject());
3253 
3254  return build(query_ast, nullptr, optimize_dag);
3255 }
#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:3263
#define CHECK(condition)
Definition: Logger.h:291
#define VLOG(n)
Definition: Logger.h:387
static void resetRelAlgFirstId() noexcept
Definition: RelAlgDag.cpp:46

+ 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 3257 of file RelAlgDag.cpp.

References build().

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

3259  {
3260  return build(query_ast, &root_dag, true);
3261 }
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, RelAlgDag *root_dag, const bool optimize_dag)
Definition: RelAlgDag.cpp:3263

+ 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 3290 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(), fold_filters(), g_cluster, get_left_deep_join_root(), RelAlgDag::getBuildState(), RelAlgDagModifier::getNodes(), RelAlgDagModifier::getQueryHints(), RelAlgDagModifier::getSubqueries(), 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().

3290  {
3291  auto const build_state = rel_alg_dag.getBuildState();
3292  if (build_state == RelAlgDag::BuildState::kBuiltOptimized) {
3293  return;
3294  }
3295 
3297  << static_cast<int>(build_state);
3298 
3299  auto& nodes = getNodes(rel_alg_dag);
3300  auto& subqueries = getSubqueries(rel_alg_dag);
3301  auto& query_hints = getQueryHints(rel_alg_dag);
3302 
3303  compute_node_hash(nodes);
3304  handle_query_hint(nodes, rel_alg_dag);
3305  mark_nops(nodes);
3306  simplify_sort(nodes);
3308  eliminate_identical_copy(nodes);
3309  fold_filters(nodes);
3310  std::vector<const RelAlgNode*> filtered_left_deep_joins;
3311  std::vector<const RelAlgNode*> left_deep_joins;
3312  for (const auto& node : nodes) {
3313  const auto left_deep_join_root = get_left_deep_join_root(node);
3314  // The filter which starts a left-deep join pattern must not be coalesced
3315  // since it contains (part of) the join condition.
3316  if (left_deep_join_root) {
3317  left_deep_joins.push_back(left_deep_join_root.get());
3318  if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
3319  filtered_left_deep_joins.push_back(left_deep_join_root.get());
3320  }
3321  }
3322  }
3323  if (filtered_left_deep_joins.empty()) {
3325  }
3326  eliminate_dead_columns(nodes);
3327  eliminate_dead_subqueries(subqueries, nodes.back().get());
3328  separate_window_function_expressions(nodes, query_hints);
3330  nodes,
3331  g_cluster /* always_add_project_if_first_project_is_window_expr */,
3332  query_hints);
3333  coalesce_nodes(nodes, left_deep_joins, query_hints);
3334  CHECK(nodes.back().use_count() == 1);
3335  create_left_deep_join(nodes);
3336 
3338 }
void mark_nops(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
Definition: RelAlgDag.cpp:1591
static std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint > > & getQueryHints(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3019
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)
void handle_query_hint(const std::vector< std::shared_ptr< RelAlgNode >> &nodes, RelAlgDag &rel_alg_dag) noexcept
Definition: RelAlgDag.cpp:1539
static std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3013
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:2555
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:2361
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:1991
void compute_node_hash(const std::vector< std::shared_ptr< RelAlgNode >> &nodes)
Definition: RelAlgDag.cpp:1577
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:3009
#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:2507
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:3023

+ 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: