From be936e46aef85638f0a4878b7c657a851aac344b Mon Sep 17 00:00:00 2001 From: Josip Mrden Date: Fri, 23 Jun 2023 13:19:22 +0200 Subject: [PATCH] Add symbol table in front of some arguments --- src/query/plan/cost_estimator.hpp | 29 +++++++++++++++++++---------- src/query/plan/planner.hpp | 4 ++-- tests/benchmark/query/planner.cpp | 4 ++-- tests/unit/query_cost_estimator.cpp | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/query/plan/cost_estimator.hpp b/src/query/plan/cost_estimator.hpp index e666f0f49..9fe824f7c 100644 --- a/src/query/plan/cost_estimator.hpp +++ b/src/query/plan/cost_estimator.hpp @@ -19,11 +19,20 @@ namespace memgraph::query::plan { +/** + * The symbol statistics specify essential DB statistics which + * help the query planner (namely here the cost estimator), to decide + * how to do expands and other types of Cypher manipulations. + */ struct SymbolStatistics { int64_t cardinality; double degree; }; +/** + * Scope of the statistics for every scanned symbol in + * the operator tree. + */ struct Scope { std::unordered_map symbol_stats; }; @@ -91,11 +100,11 @@ class CostEstimator : public HierarchicalLogicalOperatorVisitor { using HierarchicalLogicalOperatorVisitor::PostVisit; using HierarchicalLogicalOperatorVisitor::PreVisit; - CostEstimator(TDbAccessor *db_accessor, const Parameters ¶meters, const SymbolTable &table) - : db_accessor_(db_accessor), parameters(parameters), table_(table), scopes_{Scope()} {} + CostEstimator(TDbAccessor *db_accessor, const SymbolTable &table, const Parameters ¶meters) + : db_accessor_(db_accessor), table_(table), parameters(parameters), scopes_{Scope()} {} - CostEstimator(TDbAccessor *db_accessor, const Parameters ¶meters, const SymbolTable &table, Scope scope) - : db_accessor_(db_accessor), parameters(parameters), table_(table), scopes_{scope} {} + CostEstimator(TDbAccessor *db_accessor, const SymbolTable &table, const Parameters ¶meters, Scope scope) + : db_accessor_(db_accessor), table_(table), parameters(parameters), scopes_{scope} {} bool PostVisit(ScanAll &) override { cardinality_ *= db_accessor_->VerticesCount(); @@ -324,20 +333,20 @@ class CostEstimator : public HierarchicalLogicalOperatorVisitor { // accessor used for cardinality estimates in ScanAll and ScanAllByLabel TDbAccessor *db_accessor_; - const Parameters ¶meters; const SymbolTable &table_; + const Parameters ¶meters; std::vector scopes_; void IncrementCost(double param) { cost_ += param * cardinality_; } double EstimateCostOnBranch(std::shared_ptr *branch) { - CostEstimator cost_estimator(db_accessor_, parameters, table_); + CostEstimator cost_estimator(db_accessor_, table_, parameters); (*branch)->Accept(cost_estimator); return cost_estimator.cost(); } double EstimateCostOnBranch(std::shared_ptr *branch, Scope scope) { - CostEstimator cost_estimator(db_accessor_, parameters, table_, scope); + CostEstimator cost_estimator(db_accessor_, table_, parameters, scope); (*branch)->Accept(cost_estimator); return cost_estimator.cost(); } @@ -395,9 +404,9 @@ class CostEstimator : public HierarchicalLogicalOperatorVisitor { /** Returns the estimated cost of the given plan. */ template -double EstimatePlanCost(TDbAccessor *db, const Parameters ¶meters, LogicalOperator &plan, - const SymbolTable &table) { - CostEstimator estimator(db, parameters, table); +double EstimatePlanCost(TDbAccessor *db, const SymbolTable &table, const Parameters ¶meters, + LogicalOperator &plan) { + CostEstimator estimator(db, table, parameters); plan.Accept(estimator); return estimator.cost(); } diff --git a/src/query/plan/planner.hpp b/src/query/plan/planner.hpp index 3f1916d72..443680c37 100644 --- a/src/query/plan/planner.hpp +++ b/src/query/plan/planner.hpp @@ -48,8 +48,8 @@ class PostProcessor final { template double EstimatePlanCost(const std::unique_ptr &plan, TVertexCounts *vertex_counts, - SymbolTable &table) { - return query::plan::EstimatePlanCost(vertex_counts, parameters_, *plan, table); + const SymbolTable &table) { + return query::plan::EstimatePlanCost(vertex_counts, table, parameters_, *plan); } }; diff --git a/tests/benchmark/query/planner.cpp b/tests/benchmark/query/planner.cpp index 8d898c1b6..4fe4ee28c 100644 --- a/tests/benchmark/query/planner.cpp +++ b/tests/benchmark/query/planner.cpp @@ -131,7 +131,7 @@ static void BM_PlanAndEstimateIndexedMatching(benchmark::State &state) { auto plans = memgraph::query::plan::MakeLogicalPlanForSingleQuery( query_parts, &ctx); for (auto plan : plans) { - memgraph::query::plan::EstimatePlanCost(&dba, parameters, *plan, symbol_table); + memgraph::query::plan::EstimatePlanCost(&dba, symbol_table, parameters, *plan); } } } @@ -161,7 +161,7 @@ static void BM_PlanAndEstimateIndexedMatchingWithCachedCounts(benchmark::State & auto plans = memgraph::query::plan::MakeLogicalPlanForSingleQuery( query_parts, &ctx); for (auto plan : plans) { - memgraph::query::plan::EstimatePlanCost(&vertex_counts, parameters, *plan, symbol_table); + memgraph::query::plan::EstimatePlanCost(&vertex_counts, symbol_table, parameters, *plan); } } } diff --git a/tests/unit/query_cost_estimator.cpp b/tests/unit/query_cost_estimator.cpp index c1b457099..bd1fee60b 100644 --- a/tests/unit/query_cost_estimator.cpp +++ b/tests/unit/query_cost_estimator.cpp @@ -74,7 +74,7 @@ class QueryCostEstimator : public ::testing::Test { } auto Cost() { - CostEstimator cost_estimator(&*dba, parameters_, symbol_table_); + CostEstimator cost_estimator(&*dba, symbol_table_, parameters_); last_op_->Accept(cost_estimator); return cost_estimator.cost(); }