OmniSciDB  c0231cc57d
 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 Catalog_Namespace::Catalog &cat, const bool optimize_dag)
 
static std::unique_ptr< RelAlgDagbuildDagForSubquery (RelAlgDag &root_dag, const rapidjson::Value &query_ast, const Catalog_Namespace::Catalog &cat)
 
static void optimizeDag (RelAlgDag &rel_alg_dag)
 

Static Private Member Functions

static std::unique_ptr< RelAlgDagbuild (const rapidjson::Value &query_ast, const Catalog_Namespace::Catalog &cat, 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 2656 of file RelAlgDag.h.

Member Function Documentation

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

Definition at line 3131 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().

3134  {
3135  const auto& rels = field(query_ast, "rels");
3136  CHECK(rels.IsArray());
3137 
3138  auto rel_alg_dag_ptr = std::make_unique<RelAlgDag>();
3139  auto& rel_alg_dag = *rel_alg_dag_ptr;
3140  auto& nodes = getNodes(rel_alg_dag);
3141 
3142  try {
3143  nodes = details::RelAlgDispatcher(cat).run(rels, root_dag ? *root_dag : rel_alg_dag);
3144  } catch (const QueryNotSupported&) {
3145  throw;
3146  }
3147  CHECK(!nodes.empty());
3148  bind_inputs(nodes);
3149 
3151 
3152  if (optimize_dag) {
3153  optimizeDag(rel_alg_dag);
3154  }
3155 
3156  return rel_alg_dag_ptr;
3157 }
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:1425
std::vector< std::shared_ptr< RelAlgNode > > run(const rapidjson::Value &rels, RelAlgDag &root_dag)
Definition: RelAlgDag.cpp:2646
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:2628
static void optimizeDag(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.cpp:3159
#define CHECK(condition)
Definition: Logger.h:222
static void setBuildState(RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
Definition: RelAlgDag.h:2642

+ 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 Catalog_Namespace::Catalog cat,
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 3102 of file RelAlgDag.cpp.

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

3105  {
3106  rapidjson::Document query_ast;
3107  query_ast.Parse(query_ra.c_str());
3108  VLOG(2) << "Parsing query RA JSON: " << query_ra;
3109  if (query_ast.HasParseError()) {
3110  query_ast.GetParseError();
3111  LOG(ERROR) << "Failed to parse RA tree from Calcite (offset "
3112  << query_ast.GetErrorOffset() << "):\n"
3113  << rapidjson::GetParseError_En(query_ast.GetParseError());
3114  VLOG(1) << "Failed to parse query RA: " << query_ra;
3115  throw std::runtime_error(
3116  "Failed to parse relational algebra tree. Possible query syntax error.");
3117  }
3118  CHECK(query_ast.IsObject());
3120 
3121  return build(query_ast, cat, nullptr, optimize_dag);
3122 }
#define LOG(tag)
Definition: Logger.h:216
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, const Catalog_Namespace::Catalog &cat, RelAlgDag *root_dag, const bool optimize_dag)
Definition: RelAlgDag.cpp:3131
#define CHECK(condition)
Definition: Logger.h:222
#define VLOG(n)
Definition: Logger.h:316
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,
const Catalog_Namespace::Catalog cat 
)
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 3124 of file RelAlgDag.cpp.

References build().

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

3127  {
3128  return build(query_ast, cat, &root_dag, true);
3129 }
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, const Catalog_Namespace::Catalog &cat, RelAlgDag *root_dag, const bool optimize_dag)
Definition: RelAlgDag.cpp:3131

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

3159  {
3160  auto const build_state = rel_alg_dag.getBuildState();
3161  if (build_state == RelAlgDag::BuildState::kBuiltOptimized) {
3162  return;
3163  }
3164 
3166  << static_cast<int>(build_state);
3167 
3168  auto& nodes = getNodes(rel_alg_dag);
3169  auto& subqueries = getSubqueries(rel_alg_dag);
3170  auto& query_hints = getQueryHints(rel_alg_dag);
3171 
3172  compute_node_hash(nodes);
3173  handle_query_hint(nodes, rel_alg_dag);
3174  mark_nops(nodes);
3175  simplify_sort(nodes);
3177  eliminate_identical_copy(nodes);
3178  fold_filters(nodes);
3179  std::vector<const RelAlgNode*> filtered_left_deep_joins;
3180  std::vector<const RelAlgNode*> left_deep_joins;
3181  for (const auto& node : nodes) {
3182  const auto left_deep_join_root = get_left_deep_join_root(node);
3183  // The filter which starts a left-deep join pattern must not be coalesced
3184  // since it contains (part of) the join condition.
3185  if (left_deep_join_root) {
3186  left_deep_joins.push_back(left_deep_join_root.get());
3187  if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
3188  filtered_left_deep_joins.push_back(left_deep_join_root.get());
3189  }
3190  }
3191  }
3192  if (filtered_left_deep_joins.empty()) {
3194  }
3195  eliminate_dead_columns(nodes);
3196  eliminate_dead_subqueries(subqueries, nodes.back().get());
3197  separate_window_function_expressions(nodes, query_hints);
3199  nodes,
3200  g_cluster /* always_add_project_if_first_project_is_window_expr */,
3201  query_hints);
3202  coalesce_nodes(nodes, left_deep_joins, query_hints);
3203  CHECK(nodes.back().use_count() == 1);
3204  create_left_deep_join(nodes);
3205 
3207 }
void mark_nops(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
Definition: RelAlgDag.cpp:1513
static std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint > > & getQueryHints(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:2638
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:1466
static std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:2632
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:2410
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:2267
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:1912
void compute_node_hash(const std::vector< std::shared_ptr< RelAlgNode >> &nodes)
Definition: RelAlgDag.cpp:1499
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
Definition: RelAlgDag.h:2628
#define CHECK(condition)
Definition: Logger.h:222
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:2329
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:2642

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