Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::QueryGraphManager Class Reference
Collaboration diagram for duckdb::QueryGraphManager:

Public Member Functions

 QueryGraphManager (ClientContext &context)
bool Build (JoinOrderOptimizer &optimizer, LogicalOperator &op)
 Extract the join relations, optimizing non-reoderable relations when encountered.
unique_ptr< LogicalOperator > Reconstruct (unique_ptr< LogicalOperator > plan)
 Reconstruct the logical plan using the plan found by the plan enumerator.
const QueryGraphEdges & GetQueryGraphEdges () const
const vector< unique_ptr< FilterInfo > > & GetFilterBindings () const
void CreateQueryGraphCrossProduct (JoinRelationSet &left, JoinRelationSet &right)

Public Attributes

RelationManager relation_manager
 manage relations and the logical operators they represent
JoinRelationSetManager set_manager
 A structure holding all the created JoinRelationSet objects.
ClientContext & context
optional_ptr< const reference_map_t< JoinRelationSet, unique_ptr< DPJoinNode > > > plans
 A map to store the optimal join plan found for a specific JoinRelationSet*.

Private Member Functions

void GetColumnBinding (Expression &expression, ColumnBinding &binding)
void CreateHyperGraphEdges ()
GenerateJoinRelation GenerateJoins (vector< unique_ptr< LogicalOperator > > &extracted_relations, JoinRelationSet &set)

Private Attributes

vector< reference< LogicalOperator > > filter_operators
vector< unique_ptr< FilterInfo > > filters_and_bindings
QueryGraphEdges query_graph

Detailed Description

The QueryGraphManager manages the process of extracting the reorderable and nonreorderable operations from the logical plan and creating the intermediate structures needed by the plan enumerator. When the plan enumerator finishes, the Query Graph Manger can then recreate the logical plan.

Constructor & Destructor Documentation

◆ QueryGraphManager()

duckdb::QueryGraphManager::QueryGraphManager ( ClientContext & context)
inlineexplicit
311135 : relation_manager(context), context(context) {
311136 }
RelationManager relation_manager
manage relations and the logical operators they represent
Definition duckdb.cpp:311139

Member Function Documentation

◆ Build()

bool duckdb::QueryGraphManager::Build ( JoinOrderOptimizer & optimizer,
LogicalOperator & op )

Extract the join relations, optimizing non-reoderable relations when encountered.

312879 {
312880 // have the relation manager extract the join relations and create a reference list of all the
312881 // filter operators.
312882 auto can_reorder = relation_manager.ExtractJoinRelations(optimizer, op, filter_operators);
312883 auto num_relations = relation_manager.NumRelations();
312884 if (num_relations <= 1 || !can_reorder) {
312885 // nothing to optimize/reorder
312886 return false;
312887 }
312888 // extract the edges of the hypergraph, creating a list of filters and their associated bindings.
312889 filters_and_bindings = relation_manager.ExtractEdges(op, filter_operators, set_manager);
312890 // Create the query_graph hyper edges
312891 CreateHyperGraphEdges();
312892 return true;
312893}
JoinRelationSetManager set_manager
A structure holding all the created JoinRelationSet objects.
Definition duckdb.cpp:311142
vector< unique_ptr< FilterInfo > > filters_and_bindings
Definition duckdb.cpp:311172

◆ Reconstruct()

unique_ptr< LogicalOperator > duckdb::QueryGraphManager::Reconstruct ( unique_ptr< LogicalOperator > plan)

Reconstruct the logical plan using the plan found by the plan enumerator.

313027 {
313028 // now we have to rewrite the plan
313029 bool root_is_join = plan->children.size() > 1;
313030
313031 unordered_set<idx_t> bindings;
313032 for (idx_t i = 0; i < relation_manager.NumRelations(); i++) {
313033 bindings.insert(i);
313034 }
313035 auto &total_relation = set_manager.GetJoinRelation(bindings);
313036
313037 // first we will extract all relations from the main plan
313038 vector<unique_ptr<LogicalOperator>> extracted_relations;
313039 extracted_relations.reserve(relation_manager.NumRelations());
313040 for (auto &relation : relation_manager.GetRelations()) {
313041 extracted_relations.push_back(ExtractJoinRelation(relation));
313042 }
313043
313044 // now we generate the actual joins
313045 auto join_tree = GenerateJoins(extracted_relations, total_relation);
313046
313047 // perform the final pushdown of remaining filters
313048 for (auto &filter : filters_and_bindings) {
313049 // check if the filter has already been extracted
313050 if (filter->filter) {
313051 // if not we need to push it
313052 join_tree.op = PushFilter(std::move(join_tree.op), std::move(filter->filter));
313053 }
313054 }
313055
313056 // find the first join in the relation to know where to place this node
313057 if (root_is_join) {
313058 // first node is the join, return it immediately
313059 return std::move(join_tree.op);
313060 }
313061 D_ASSERT(plan->children.size() == 1);
313062 // have to move up through the relations
313063 auto op = plan.get();
313064 auto parent = plan.get();
313065 while (op->type != LogicalOperatorType::LOGICAL_CROSS_PRODUCT &&
313066 op->type != LogicalOperatorType::LOGICAL_COMPARISON_JOIN &&
313067 op->type != LogicalOperatorType::LOGICAL_ASOF_JOIN) {
313068 D_ASSERT(op->children.size() == 1);
313069 parent = op;
313070 op = op->children[0].get();
313071 }
313072 // have to replace at this node
313073 parent->children[0] = std::move(join_tree.op);
313074 return plan;
313075}
void filter(InputArray image, InputArray kernel, OutputArray output)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetQueryGraphEdges()

const QueryGraphEdges & duckdb::QueryGraphManager::GetQueryGraphEdges ( ) const

Get a reference to the QueryGraphEdges structure that stores edges between nodes and hypernodes.

313258 {
313259 return query_graph;
313260}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFilterBindings()

const vector< unique_ptr< FilterInfo > > & duckdb::QueryGraphManager::GetFilterBindings ( ) const

Get a list of the join filters in the join plan than eventually are transformed into the query graph edges

312908 {
312909 return filters_and_bindings;
312910}

◆ CreateQueryGraphCrossProduct()

void duckdb::QueryGraphManager::CreateQueryGraphCrossProduct ( JoinRelationSet & left,
JoinRelationSet & right )

Plan enumerator may not find a full plan and therefore will need to create cross products to create edges.

313262 {
313263 query_graph.CreateEdge(left, right, nullptr);
313264 query_graph.CreateEdge(right, left, nullptr);
313265}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetColumnBinding()

void duckdb::QueryGraphManager::GetColumnBinding ( Expression & expression,
ColumnBinding & binding )
private
312895 {
312896 ExpressionIterator::VisitExpression<BoundColumnRefExpression>(
312897 root_expr, [&](const BoundColumnRefExpression &colref) {
312898 D_ASSERT(colref.depth == 0);
312899 D_ASSERT(colref.binding.table_index != DConstants::INVALID_INDEX);
312900 // map the base table index to the relation index used by the JoinOrderOptimizer
312901 D_ASSERT(relation_manager.relation_mapping.find(colref.binding.table_index) !=
312902 relation_manager.relation_mapping.end());
312903 binding = ColumnBinding(relation_manager.relation_mapping[colref.binding.table_index],
312904 colref.binding.column_index);
312905 });
312906}
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

◆ CreateHyperGraphEdges()

void duckdb::QueryGraphManager::CreateHyperGraphEdges ( )
private
312936 {
312937 // create potential edges from the comparisons
312938 for (auto &filter_info : filters_and_bindings) {
312939 auto &filter = filter_info->filter;
312940 // now check if it can be used as a join predicate
312941 if (filter->GetExpressionClass() == ExpressionClass::BOUND_COMPARISON) {
312942 auto &comparison = filter->Cast<BoundComparisonExpression>();
312943 // extract the bindings that are required for the left and right side of the comparison
312944 unordered_set<idx_t> left_bindings, right_bindings;
312945 relation_manager.ExtractBindings(*comparison.left, left_bindings);
312946 relation_manager.ExtractBindings(*comparison.right, right_bindings);
312947 GetColumnBinding(*comparison.left, filter_info->left_binding);
312948 GetColumnBinding(*comparison.right, filter_info->right_binding);
312949 if (!left_bindings.empty() && !right_bindings.empty()) {
312950 // both the left and the right side have bindings
312951 // first create the relation sets, if they do not exist
312952 if (!filter_info->left_set) {
312953 filter_info->left_set = &set_manager.GetJoinRelation(left_bindings);
312954 }
312955 if (!filter_info->right_set) {
312956 filter_info->right_set = &set_manager.GetJoinRelation(right_bindings);
312957 }
312958 // we can only create a meaningful edge if the sets are not exactly the same
312959 if (filter_info->left_set != filter_info->right_set) {
312960 // check if the sets are disjoint
312961 if (Disjoint(left_bindings, right_bindings)) {
312962 // they are disjoint, we only need to create one set of edges in the join graph
312963 query_graph.CreateEdge(*filter_info->left_set, *filter_info->right_set, filter_info);
312964 query_graph.CreateEdge(*filter_info->right_set, *filter_info->left_set, filter_info);
312965 }
312966 }
312967 }
312968 } else if (filter->GetExpressionClass() == ExpressionClass::BOUND_CONJUNCTION) {
312969 auto &conjunction = filter->Cast<BoundConjunctionExpression>();
312970 if (conjunction.GetExpressionType() == ExpressionType::CONJUNCTION_OR ||
312971 filter_info->join_type == JoinType::INNER || filter_info->join_type == JoinType::INVALID) {
312972 // Currently we do not interpret Conjunction expressions as INNER joins
312973 // for hyper graph edges. These are most likely OR conjunctions, and
312974 // will be pushed down into a join later in the optimizer.
312975 // Conjunction filters are mostly to help plan semi and anti joins at the moment.
312976 continue;
312977 }
312978 unordered_set<idx_t> left_bindings, right_bindings;
312979 D_ASSERT(filter_info->left_set);
312980 D_ASSERT(filter_info->right_set);
312981 D_ASSERT(filter_info->join_type == JoinType::SEMI || filter_info->join_type == JoinType::ANTI);
312982 for (auto &child_comp : conjunction.children) {
312983 if (child_comp->GetExpressionClass() != ExpressionClass::BOUND_COMPARISON) {
312984 continue;
312985 }
312986 auto &comparison = child_comp->Cast<BoundComparisonExpression>();
312987 // extract the bindings that are required for the left and right side of the comparison
312988 relation_manager.ExtractBindings(*comparison.left, left_bindings);
312989 relation_manager.ExtractBindings(*comparison.right, right_bindings);
312990 if (filter_info->left_binding.table_index == DConstants::INVALID_INDEX &&
312991 filter_info->left_binding.column_index == DConstants::INVALID_INDEX) {
312992 GetColumnBinding(*comparison.left, filter_info->left_binding);
312993 }
312994 if (filter_info->right_binding.table_index == DConstants::INVALID_INDEX &&
312995 filter_info->right_binding.column_index == DConstants::INVALID_INDEX) {
312996 GetColumnBinding(*comparison.right, filter_info->right_binding);
312997 }
312998 }
312999 if (!left_bindings.empty() && !right_bindings.empty()) {
313000 // we can only create a meaningful edge if the sets are not exactly the same
313001 if (filter_info->left_set != filter_info->right_set) {
313002 // check if the sets are disjoint
313003 if (Disjoint(left_bindings, right_bindings)) {
313004 // they are disjoint, we only need to create one set of edges in the join graph
313005 query_graph.CreateEdge(*filter_info->left_set, *filter_info->right_set, filter_info);
313006 query_graph.CreateEdge(*filter_info->right_set, *filter_info->left_set, filter_info);
313007 }
313008 }
313009 }
313010 }
313011 }
313012}
static bool Disjoint(const unordered_set< T > &a, const unordered_set< T > &b)
Returns true if A and B are disjoint, false otherwise.
Definition duckdb.cpp:312873

◆ GenerateJoins()

GenerateJoinRelation duckdb::QueryGraphManager::GenerateJoins ( vector< unique_ptr< LogicalOperator > > & extracted_relations,
JoinRelationSet & set )
private
313091 {
313092 optional_ptr<JoinRelationSet> left_node;
313093 optional_ptr<JoinRelationSet> right_node;
313094 optional_ptr<JoinRelationSet> result_relation;
313095 unique_ptr<LogicalOperator> result_operator;
313096
313097 auto dp_entry = plans->find(set);
313098 if (dp_entry == plans->end()) {
313099 throw InternalException("Join Order Optimizer Error: No full plan was created");
313100 }
313101 auto &node = dp_entry->second;
313102 if (!dp_entry->second->is_leaf) {
313103 // generate the left and right children
313104 auto left = GenerateJoins(extracted_relations, node->left_set);
313105 auto right = GenerateJoins(extracted_relations, node->right_set);
313106 if (dp_entry->second->info->filters.empty()) {
313107 // no filters, create a cross product
313108 auto cardinality = left.op->estimated_cardinality * right.op->estimated_cardinality;
313109 result_operator = LogicalCrossProduct::Create(std::move(left.op), std::move(right.op));
313110 result_operator->SetEstimatedCardinality(cardinality);
313111 } else {
313112 // we have filters, create a join node
313113 auto chosen_filter = node->info->filters.at(0);
313114 for (idx_t i = 0; i < node->info->filters.size(); i++) {
313115 if (node->info->filters.at(i)->join_type == JoinType::INNER) {
313116 chosen_filter = node->info->filters.at(i);
313117 break;
313118 }
313119 }
313120 auto join = make_uniq<LogicalComparisonJoin>(chosen_filter->join_type);
313121 // Here we optimize build side probe side. Our build side is the right side
313122 // So the right plans should have lower cardinalities.
313123 join->children.push_back(std::move(left.op));
313124 join->children.push_back(std::move(right.op));
313125
313126 // set the join conditions from the join node
313127 for (auto &filter_ref : node->info->filters) {
313128 auto f = filter_ref.get();
313129 // extract the filter from the operator it originally belonged to
313130 D_ASSERT(filters_and_bindings[f->filter_index]->filter);
313131 auto &filter_and_binding = filters_and_bindings.at(f->filter_index);
313132 auto condition = std::move(filter_and_binding->filter);
313133 // now create the actual join condition
313134 D_ASSERT((JoinRelationSet::IsSubset(*left.set, *f->left_set) &&
313135 JoinRelationSet::IsSubset(*right.set, *f->right_set)) ||
313136 (JoinRelationSet::IsSubset(*left.set, *f->right_set) &&
313137 JoinRelationSet::IsSubset(*right.set, *f->left_set)));
313138
313139 bool invert = !JoinRelationSet::IsSubset(*left.set, *f->left_set);
313140 // If the left and right set are inverted AND it is a semi or anti join
313141 // swap left and right children back.
313142
313143 if (invert && (f->join_type == JoinType::SEMI || f->join_type == JoinType::ANTI)) {
313144 std::swap(join->children[0], join->children[1]);
313145 invert = false;
313146 }
313147
313148 if (condition->GetExpressionClass() == ExpressionClass::BOUND_COMPARISON) {
313149 auto cond = MaybeInvertConditions(std::move(condition), invert);
313150 join->conditions.push_back(std::move(cond));
313151 } else if (condition->GetExpressionClass() == ExpressionClass::BOUND_CONJUNCTION) {
313152 auto &conjunction = condition->Cast<BoundConjunctionExpression>();
313153 for (auto &child : conjunction.children) {
313154 D_ASSERT(child->GetExpressionClass() == ExpressionClass::BOUND_COMPARISON);
313155 auto cond = MaybeInvertConditions(std::move(child), invert);
313156 join->conditions.push_back(std::move(cond));
313157 }
313158 }
313159 }
313160 D_ASSERT(!join->conditions.empty());
313161 result_operator = std::move(join);
313162 }
313163 left_node = left.set;
313164 right_node = right.set;
313165 result_relation = &set_manager.Union(*left.set, *right.set);
313166 } else {
313167 // base node, get the entry from the list of extracted relations
313168 D_ASSERT(node->set.count == 1);
313169 D_ASSERT(extracted_relations[node->set.relations[0]]);
313170 result_relation = &node->set;
313171 result_operator = std::move(extracted_relations[result_relation->relations[0]]);
313172 }
313173 // TODO: this is where estimated properties start coming into play.
313174 // when creating the result operator, we should ask the cost model and cardinality estimator what
313175 // the cost and cardinality are
313176 // result_operator->estimated_props = node.estimated_props->Copy();
313177 result_operator->estimated_cardinality = node->cardinality;
313178 result_operator->has_estimated_cardinality = true;
313179 // check if we should do a pushdown on this node
313180 // basically, any remaining filter that is a subset of the current relation will no longer be used in joins
313181 // hence we should push it here
313182 for (auto &filter_info : filters_and_bindings) {
313183 // check if the filter has already been extracted
313184 auto &info = *filter_info;
313185 if (filters_and_bindings[info.filter_index]->filter) {
313186 // now check if the filter is a subset of the current relation
313187 // note that infos with an empty relation set are a special case and we do not push them down
313188 if (info.set.get().count > 0 && JoinRelationSet::IsSubset(*result_relation, info.set)) {
313189 auto &filter_and_binding = filters_and_bindings[info.filter_index];
313190 auto filter = std::move(filter_and_binding->filter);
313191 // if it is, we can push the filter
313192 // we can push it either into a join or as a filter
313193 // check if we are in a join or in a base table
313194 if (!left_node || !info.left_set) {
313195 // base table or non-comparison expression, push it as a filter
313196 result_operator = PushFilter(std::move(result_operator), std::move(filter));
313197 continue;
313198 }
313199 // the node below us is a join or cross product and the expression is a comparison
313200 // check if the nodes can be split up into left/right
313201 bool found_subset = false;
313202 bool invert = false;
313203 if (JoinRelationSet::IsSubset(*left_node, *info.left_set) &&
313204 JoinRelationSet::IsSubset(*right_node, *info.right_set)) {
313205 found_subset = true;
313206 } else if (JoinRelationSet::IsSubset(*right_node, *info.left_set) &&
313207 JoinRelationSet::IsSubset(*left_node, *info.right_set)) {
313208 invert = true;
313209 found_subset = true;
313210 }
313211 if (!found_subset) {
313212 // could not be split up into left/right
313213 result_operator = PushFilter(std::move(result_operator), std::move(filter));
313214 continue;
313215 }
313216 // create the join condition
313217 JoinCondition cond;
313218 D_ASSERT(filter->GetExpressionClass() == ExpressionClass::BOUND_COMPARISON);
313219 auto &comparison = filter->Cast<BoundComparisonExpression>();
313220 // we need to figure out which side is which by looking at the relations available to us
313221 cond.left = !invert ? std::move(comparison.left) : std::move(comparison.right);
313222 cond.right = !invert ? std::move(comparison.right) : std::move(comparison.left);
313223 cond.comparison = comparison.GetExpressionType();
313224 if (invert) {
313225 // reverse comparison expression if we reverse the order of the children
313226 cond.comparison = FlipComparisonExpression(comparison.GetExpressionType());
313227 }
313228 // now find the join to push it into
313229 auto node = result_operator.get();
313230 if (node->type == LogicalOperatorType::LOGICAL_FILTER) {
313231 node = node->children[0].get();
313232 }
313233 if (node->type == LogicalOperatorType::LOGICAL_CROSS_PRODUCT) {
313234 // turn into comparison join
313235 auto comp_join = make_uniq<LogicalComparisonJoin>(JoinType::INNER);
313236 comp_join->children.push_back(std::move(node->children[0]));
313237 comp_join->children.push_back(std::move(node->children[1]));
313238 comp_join->conditions.push_back(std::move(cond));
313239 if (node == result_operator.get()) {
313240 result_operator = std::move(comp_join);
313241 } else {
313242 D_ASSERT(result_operator->type == LogicalOperatorType::LOGICAL_FILTER);
313243 result_operator->children[0] = std::move(comp_join);
313244 }
313245 } else {
313246 D_ASSERT(node->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
313247 node->type == LogicalOperatorType::LOGICAL_ASOF_JOIN);
313248 auto &comp_join = node->Cast<LogicalComparisonJoin>();
313249 comp_join.conditions.push_back(std::move(cond));
313250 }
313251 }
313252 }
313253 }
313254 auto result = GenerateJoinRelation(result_relation, std::move(result_operator));
313255 return result;
313256}
optional_ptr< const reference_map_t< JoinRelationSet, unique_ptr< DPJoinNode > > > plans
A map to store the optimal join plan found for a specific JoinRelationSet*.
Definition duckdb.cpp:311165
double invert(InputArray src, OutputArray dst, int flags=DECOMP_LU)
cv::String join(const cv::String &base, const cv::String &path)
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052
static bool IsSubset(JoinRelationSet &super, JoinRelationSet &sub)
Returns true if sub is a subset of super.
Definition duckdb.cpp:312070

Member Data Documentation

◆ filters_and_bindings

vector<unique_ptr<FilterInfo> > duckdb::QueryGraphManager::filters_and_bindings
private

Filter information including the column_bindings that join filters used by the cardinality estimator to estimate distinct counts


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