From 48138bb52f3d9335bc8f6c8cedf767bb5d6f15c5 Mon Sep 17 00:00:00 2001 From: Josip Mrden Date: Tue, 20 Jun 2023 11:35:39 +0200 Subject: [PATCH] Rewrite code to have IndexStats in methods instead of having it separated --- src/query/db_accessor.hpp | 14 ++++++------- src/query/interpreter.cpp | 10 ++++----- src/query/plan/cost_estimator.hpp | 2 +- src/query/plan/rewrite/index_lookup.hpp | 11 +++++----- src/query/plan/vertex_count_cache.hpp | 8 +++---- src/storage/v2/indices.cpp | 28 +++++++++++++------------ src/storage/v2/indices.hpp | 10 ++++----- src/storage/v2/storage.hpp | 11 +++++----- tests/manual/interactive_planning.cpp | 11 +++++----- tests/unit/query_plan_checker.hpp | 8 +++---- 10 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index f9595b589..e8fbe5d68 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -430,12 +430,12 @@ class DbAccessor final { return accessor_->LabelPropertyIndexExists(label, prop); } - std::optional GetLabelIndexStats(const storage::LabelId &label) const { - return accessor_->GetLabelIndexStats(label); + std::optional GetIndexStats(const storage::LabelId &label) const { + return accessor_->GetIndexStats(label); } - std::optional GetIndexStats(const storage::LabelId &label, - const storage::PropertyId &property) const { + std::optional GetIndexStats(const storage::LabelId &label, + const storage::PropertyId &property) const { return accessor_->GetIndexStats(label, property); } @@ -451,12 +451,12 @@ class DbAccessor final { return accessor_->DeleteIndexStatsForLabels(labels); } - void SetLabelIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) { - accessor_->SetLabelIndexStats(label, stats); + void SetIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) { + accessor_->SetIndexStats(label, stats); } void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, - const storage::IndexStats &stats) { + const storage::LabelPropertyIndexStats &stats) { accessor_->SetIndexStats(label, property, stats); } diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index cddfbd64d..d8887d780 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -1558,7 +1558,7 @@ std::vector> AnalyzeGraphQueryHandler::AnalyzeGraphCreat }); auto average_degree = (double)total_degree / no_vertices; - execution_db_accessor->SetLabelIndexStats( + execution_db_accessor->SetIndexStats( index_info, storage::LabelIndexStats{.count = no_vertices, .avg_degree = average_degree}); }); @@ -1594,10 +1594,10 @@ std::vector> AnalyzeGraphQueryHandler::AnalyzeGraphCreat }); double average_degree = (double)vertex_degree_counter[label_property] / count_property_value; execution_db_accessor->SetIndexStats(label_property.first, label_property.second, - storage::IndexStats{.count = count_property_value, - .statistic = chi_squared_stat, - .avg_group_size = avg_group_size, - .avg_degree = average_degree}); + storage::LabelPropertyIndexStats{.count = count_property_value, + .statistic = chi_squared_stat, + .avg_group_size = avg_group_size, + .avg_degree = average_degree}); // Save result result.emplace_back(execution_db_accessor->LabelToName(label_property.first)); result.emplace_back(execution_db_accessor->PropertyToName(label_property.second)); diff --git a/src/query/plan/cost_estimator.hpp b/src/query/plan/cost_estimator.hpp index 15684299a..c14d591b8 100644 --- a/src/query/plan/cost_estimator.hpp +++ b/src/query/plan/cost_estimator.hpp @@ -106,7 +106,7 @@ class CostEstimator : public HierarchicalLogicalOperatorVisitor { } bool PostVisit(ScanAllByLabel &scan_all_by_label) override { - auto index_stats = db_accessor_->GetLabelIndexStats(scan_all_by_label.label_); + auto index_stats = db_accessor_->GetIndexStats(scan_all_by_label.label_); if (index_stats) { scope_.symbol_stats[scan_all_by_label.output_symbol_.name()] = SymbolStatistics{ .name = scan_all_by_label.output_symbol_.name(), diff --git a/src/query/plan/rewrite/index_lookup.hpp b/src/query/plan/rewrite/index_lookup.hpp index e3ade7a08..7c285dc94 100644 --- a/src/query/plan/rewrite/index_lookup.hpp +++ b/src/query/plan/rewrite/index_lookup.hpp @@ -525,7 +525,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { // FilterInfo with PropertyFilter. FilterInfo filter; int64_t vertex_count; - std::optional index_stats; + std::optional index_stats; }; bool DefaultPreVisit() override { throw utils::NotYetImplemented("optimizing index lookup"); } @@ -592,8 +592,8 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { * @param vertex_count: New index's number of vertices. * @return -1 if the new index is better, 0 if they are equal and 1 if the existing one is better. */ - auto compare_indices = [](std::optional &found, std::optional &new_stats, - int vertex_count) { + auto compare_indices = [](std::optional &found, + std::optional &new_stats, int vertex_count) { if (!new_stats.has_value()) { return 0; } @@ -630,7 +630,8 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { }; int64_t vertex_count = db_->VerticesCount(GetLabel(label), GetProperty(property)); - std::optional new_stats = db_->GetIndexStats(GetLabel(label), GetProperty(property)); + std::optional new_stats = + db_->GetIndexStats(GetLabel(label), GetProperty(property)); // Conditions, from more to less important: // the index with 10x less vertices is better. @@ -769,7 +770,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { std::vector removed_expressions; filters_.EraseLabelFilter(node_symbol, label, &removed_expressions); filter_exprs_for_removal_.insert(removed_expressions.begin(), removed_expressions.end()); - auto index_stats = db_->GetLabelIndexStats(GetLabel(label)); + auto index_stats = db_->GetIndexStats(GetLabel(label)); if (index_stats.has_value()) { scope_.symbol_stats[node_symbol.name()] = SymbolStatistics{.name = node_symbol.name(), .cardinality = index_stats.value().count, diff --git a/src/query/plan/vertex_count_cache.hpp b/src/query/plan/vertex_count_cache.hpp index cebefcc99..ff19ee95a 100644 --- a/src/query/plan/vertex_count_cache.hpp +++ b/src/query/plan/vertex_count_cache.hpp @@ -78,12 +78,12 @@ class VertexCountCache { return db_->LabelPropertyIndexExists(label, property); } - std::optional GetLabelIndexStats(const storage::LabelId &label) const { - return db_->GetLabelIndexStats(label); + std::optional GetIndexStats(const storage::LabelId &label) const { + return db_->GetIndexStats(label); } - std::optional GetIndexStats(const storage::LabelId &label, - const storage::PropertyId &property) const { + std::optional GetIndexStats(const storage::LabelId &label, + const storage::PropertyId &property) const { return db_->GetIndexStats(label, property); } diff --git a/src/storage/v2/indices.cpp b/src/storage/v2/indices.cpp index 99e0dda32..d2cdb7bca 100644 --- a/src/storage/v2/indices.cpp +++ b/src/storage/v2/indices.cpp @@ -478,7 +478,9 @@ void LabelIndex::RunGC() { } } -void LabelIndex::SetIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) { stats_[label] = stats; } +void LabelIndex::SetIndexStats(const storage::LabelId &label, const storage::LabelIndexStats &stats) { + stats_[label] = stats; +} std::optional LabelIndex::GetIndexStats(const storage::LabelId &label) const { if (auto it = stats_.find(label); it != stats_.end()) { @@ -487,6 +489,15 @@ std::optional LabelIndex::GetIndexStats(const storage::LabelId return {}; } +std::vector LabelIndex::ClearIndexStats() { + std::vector deleted_indexes; + deleted_indexes.reserve(stats_.size()); + std::transform(stats_.begin(), stats_.end(), std::back_inserter(deleted_indexes), + [](const auto &elem) { return elem.first; }); + stats_.clear(); + return deleted_indexes; +} + bool LabelPropertyIndex::Entry::operator<(const Entry &rhs) { if (value < rhs.value) { return true; @@ -837,15 +848,6 @@ std::vector> LabelPropertyIndex::DeleteIndexStats return deleted_indexes; } -std::vector LabelIndex::ClearIndexStats() { - std::vector deleted_indexes; - deleted_indexes.reserve(stats_.size()); - std::transform(stats_.begin(), stats_.end(), std::back_inserter(deleted_indexes), - [](const auto &elem) { return elem.first; }); - stats_.clear(); - return deleted_indexes; -} - std::vector> LabelPropertyIndex::ClearIndexStats() { std::vector> deleted_indexes; deleted_indexes.reserve(stats_.size()); @@ -856,12 +858,12 @@ std::vector> LabelPropertyIndex::ClearIndexStats( } void LabelPropertyIndex::SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, - const IndexStats &stats) { + const storage::LabelPropertyIndexStats &stats) { stats_[{label, property}] = stats; } -std::optional LabelPropertyIndex::GetIndexStats(const storage::LabelId &label, - const storage::PropertyId &property) const { +std::optional LabelPropertyIndex::GetIndexStats( + const storage::LabelId &label, const storage::PropertyId &property) const { if (auto it = stats_.find({label, property}); it != stats_.end()) { return it->second; } diff --git a/src/storage/v2/indices.hpp b/src/storage/v2/indices.hpp index 860c1e875..78e27a953 100644 --- a/src/storage/v2/indices.hpp +++ b/src/storage/v2/indices.hpp @@ -147,7 +147,7 @@ class LabelIndex { Config::Items config_; }; -struct IndexStats { +struct LabelPropertyIndexStats { int64_t count; double statistic, avg_group_size, avg_degree; }; @@ -264,10 +264,10 @@ class LabelPropertyIndex { std::vector> DeleteIndexStatsForLabel(const storage::LabelId &label); void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, - const storage::IndexStats &stats); + const storage::LabelPropertyIndexStats &stats); - std::optional GetIndexStats(const storage::LabelId &label, - const storage::PropertyId &property) const; + std::optional GetIndexStats(const storage::LabelId &label, + const storage::PropertyId &property) const; void Clear() { index_.clear(); } @@ -275,7 +275,7 @@ class LabelPropertyIndex { private: std::map, utils::SkipList> index_; - std::map, storage::IndexStats> stats_; + std::map, storage::LabelPropertyIndexStats> stats_; Indices *indices_; Constraints *constraints_; Config::Items config_; diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index d022394d3..6452fd8a8 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -268,12 +268,12 @@ class Storage final { return storage_->indices_.label_property_index.ApproximateVertexCount(label, property, lower, upper); } - std::optional GetLabelIndexStats(const storage::LabelId &label) const { + std::optional GetIndexStats(const storage::LabelId &label) const { return storage_->indices_.label_index.GetIndexStats(label); } - std::optional GetIndexStats(const storage::LabelId &label, - const storage::PropertyId &property) const { + std::optional GetIndexStats(const storage::LabelId &label, + const storage::PropertyId &property) const { return storage_->indices_.label_property_index.GetIndexStats(label, property); } @@ -294,11 +294,12 @@ class Storage final { return deleted_indexes; } - void SetLabelIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) { + void SetIndexStats(const storage::LabelId &label, const LabelIndexStats &stats) { storage_->indices_.label_index.SetIndexStats(label, stats); } - void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, const IndexStats &stats) { + void SetIndexStats(const storage::LabelId &label, const storage::PropertyId &property, + const LabelPropertyIndexStats &stats) { storage_->indices_.label_property_index.SetIndexStats(label, property, stats); } diff --git a/tests/manual/interactive_planning.cpp b/tests/manual/interactive_planning.cpp index abf36c803..1a1909ba3 100644 --- a/tests/manual/interactive_planning.cpp +++ b/tests/manual/interactive_planning.cpp @@ -27,6 +27,7 @@ #include "query/plan/planner.hpp" #include "query/plan/pretty_print.hpp" #include "query/typed_value.hpp" +#include "storage/v2/indices.hpp" #include "storage/v2/property_value.hpp" #include "utils/string.hpp" @@ -213,13 +214,13 @@ class InteractiveDbAccessor { return label_property_index_.at(key); } - std::optional GetIndexStats(memgraph::storage::LabelId label, - memgraph::storage::PropertyId property) const { - return dba_->GetIndexStats(label, property); + std::optional GetIndexStats(const memgraph::storage::LabelId label) const { + return dba_->GetIndexStats(label); } - std::optional GetLabelIndexStats(memgraph::storage::LabelId label) const { - return dba_->GetLabelIndexStats(label); + std::optional GetIndexStats( + const memgraph::storage::LabelId label, const memgraph::storage::PropertyId property) const { + return dba_->GetIndexStats(label, property); } // Save the cached vertex counts to a stream. diff --git a/tests/unit/query_plan_checker.hpp b/tests/unit/query_plan_checker.hpp index e4902d331..0a8b4d3ab 100644 --- a/tests/unit/query_plan_checker.hpp +++ b/tests/unit/query_plan_checker.hpp @@ -500,12 +500,12 @@ class FakeDbAccessor { return false; } - std::optional GetIndexStats(memgraph::storage::LabelId label, - memgraph::storage::PropertyId property) const { - return memgraph::storage::IndexStats{.statistic = 0, .avg_group_size = 1}; // unique id + std::optional GetIndexStats( + const memgraph::storage::LabelId label, const memgraph::storage::PropertyId property) const { + return memgraph::storage::LabelPropertyIndexStats{.statistic = 0, .avg_group_size = 1}; // unique id } - std::optional GetLabelIndexStats(memgraph::storage::LabelId label) const { + std::optional GetIndexStats(const memgraph::storage::LabelId label) const { return memgraph::storage::LabelIndexStats{.count = 0, .avg_degree = 0}; // unique id }