diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index f4306aa3b..8b6075086 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -624,11 +624,11 @@ class DbAccessor final { return accessor_->DropUniqueConstraint(label, properties); } - bool UniqueConstraintExists(storage::LabelId label, storage::PropertyId prop) const { + bool UniqueConstraintExists(const storage::LabelId &label, const storage::PropertyId &prop) const { return accessor_->UniqueConstraintExists(label, prop); } - bool IndexedScanExists(storage::LabelId label, storage::PropertyId prop) const { + bool IndexedScanExists(const storage::LabelId &label, const storage::PropertyId &prop) const { return LabelPropertyIndexExists(label, prop) || UniqueConstraintExists(label, prop); } }; diff --git a/src/query/plan/rewrite/index_lookup.hpp b/src/query/plan/rewrite/index_lookup.hpp index 06d3236e2..4191112b8 100644 --- a/src/query/plan/rewrite/index_lookup.hpp +++ b/src/query/plan/rewrite/index_lookup.hpp @@ -587,8 +587,7 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor { continue; } const auto &property = filter.property_filter->property_; - if (!db_->LabelPropertyIndexExists(GetLabel(label), GetProperty(property)) && - !db_->UniqueConstraintExists(GetLabel(label), GetProperty(property))) { + if (!db_->IndexedScanExists(GetLabel(label), GetProperty(property))) { continue; } auto is_better_type = [&found](PropertyFilter::Type type) { diff --git a/src/query/plan/vertex_count_cache.hpp b/src/query/plan/vertex_count_cache.hpp index 3a295349f..a568e7f8e 100644 --- a/src/query/plan/vertex_count_cache.hpp +++ b/src/query/plan/vertex_count_cache.hpp @@ -87,10 +87,14 @@ class VertexCountCache { return db_->GetIndexStats(label, property); } - bool UniqueConstraintExists(storage::LabelId label, storage::PropertyId prop) const { + bool UniqueConstraintExists(const storage::LabelId &label, const storage::PropertyId &prop) const { return db_->UniqueConstraintExists(label, prop); } + bool IndexedScanExists(const storage::LabelId &label, const storage::PropertyId &prop) const { + return db_->IndexedScanExists(label, prop); + } + private: typedef std::pair LabelPropertyKey; diff --git a/src/storage/v2/constraints/unique_constraints.hpp b/src/storage/v2/constraints/unique_constraints.hpp index 1d0971b41..2b9a332c3 100644 --- a/src/storage/v2/constraints/unique_constraints.hpp +++ b/src/storage/v2/constraints/unique_constraints.hpp @@ -59,11 +59,12 @@ class UniqueConstraints { virtual std::vector>> ListConstraints() const = 0; - virtual uint64_t ApproximateVertexCount(LabelId label, PropertyId property) const = 0; - virtual uint64_t ApproximateVertexCount(LabelId label, PropertyId property, const PropertyValue &value) const = 0; - virtual uint64_t ApproximateVertexCount(LabelId label, PropertyId property, - const std::optional> &lower, - const std::optional> &upper) const = 0; + virtual uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property) const = 0; + virtual uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, + const PropertyValue &value) const = 0; + virtual uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, + const std::optional> &, + const std::optional> &) const = 0; virtual void Clear() = 0; diff --git a/src/storage/v2/disk/storage.cpp b/src/storage/v2/disk/storage.cpp index f2ba0235b..0cc1bcd84 100644 --- a/src/storage/v2/disk/storage.cpp +++ b/src/storage/v2/disk/storage.cpp @@ -1957,7 +1957,9 @@ UniqueConstraints::DeletionStatus DiskStorage::DiskAccessor::DropUniqueConstrain return UniqueConstraints::DeletionStatus::SUCCESS; } -bool DiskStorage::DiskAccessor::UniqueConstraintExists(LabelId label, PropertyId property) const { return false; } +bool DiskStorage::DiskAccessor::UniqueConstraintExists(const LabelId &label, const PropertyId &property) const { + return false; +} Transaction DiskStorage::CreateTransaction(IsolationLevel isolation_level, StorageMode storage_mode) { /// We acquire the transaction engine lock here because we access (and diff --git a/src/storage/v2/disk/storage.hpp b/src/storage/v2/disk/storage.hpp index 18db2db01..067f61e11 100644 --- a/src/storage/v2/disk/storage.hpp +++ b/src/storage/v2/disk/storage.hpp @@ -169,7 +169,7 @@ class DiskStorage final : public Storage { UniqueConstraints::DeletionStatus DropUniqueConstraint(LabelId label, const std::set &properties) override; - bool UniqueConstraintExists(LabelId label, PropertyId property) const override; + bool UniqueConstraintExists(const LabelId &label, const PropertyId &property) const override; private: /// Flushes vertices and edges to the disk with the commit timestamp. diff --git a/src/storage/v2/disk/unique_constraints.cpp b/src/storage/v2/disk/unique_constraints.cpp index d241e3625..025494457 100644 --- a/src/storage/v2/disk/unique_constraints.cpp +++ b/src/storage/v2/disk/unique_constraints.cpp @@ -310,12 +310,14 @@ std::vector>> DiskUniqueConstraints::Lis return {constraints_.begin(), constraints_.end()}; } -uint64_t DiskUniqueConstraints::ApproximateVertexCount(LabelId label, PropertyId property) const { return 10; }; -uint64_t DiskUniqueConstraints::ApproximateVertexCount(LabelId label, PropertyId property, +uint64_t DiskUniqueConstraints::ApproximateVertexCount(const LabelId &label, const PropertyId &property) const { + return 10; +}; +uint64_t DiskUniqueConstraints::ApproximateVertexCount(const LabelId &label, const PropertyId &property, const PropertyValue &value) const { return 10; }; -uint64_t DiskUniqueConstraints::ApproximateVertexCount(LabelId label, PropertyId property, +uint64_t DiskUniqueConstraints::ApproximateVertexCount(const LabelId &label, const PropertyId &property, const std::optional> &lower, const std::optional> &upper) const { return 10; diff --git a/src/storage/v2/disk/unique_constraints.hpp b/src/storage/v2/disk/unique_constraints.hpp index dec45e4ac..2fc6427d9 100644 --- a/src/storage/v2/disk/unique_constraints.hpp +++ b/src/storage/v2/disk/unique_constraints.hpp @@ -54,9 +54,10 @@ class DiskUniqueConstraints : public UniqueConstraints { std::vector>> ListConstraints() const override; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property) const override; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property, const PropertyValue &value) const override; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property, + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property) const override; + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, + const PropertyValue &value) const override; + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, const std::optional> &lower, const std::optional> &upper) const override; void Clear() override; diff --git a/src/storage/v2/inmemory/storage.cpp b/src/storage/v2/inmemory/storage.cpp index c5adab04f..6b458803b 100644 --- a/src/storage/v2/inmemory/storage.cpp +++ b/src/storage/v2/inmemory/storage.cpp @@ -1107,7 +1107,7 @@ UniqueConstraints::DeletionStatus InMemoryStorage::InMemoryAccessor::DropUniqueC return UniqueConstraints::DeletionStatus::SUCCESS; } -bool InMemoryStorage::InMemoryAccessor::UniqueConstraintExists(LabelId label, PropertyId property) const { +bool InMemoryStorage::InMemoryAccessor::UniqueConstraintExists(const LabelId &label, const PropertyId &property) const { auto *in_memory = static_cast(storage_); auto *mem_unique_constraints = static_cast(in_memory->constraints_.unique_constraints_.get()); diff --git a/src/storage/v2/inmemory/storage.hpp b/src/storage/v2/inmemory/storage.hpp index 0ba7c36fd..17d80e9b3 100644 --- a/src/storage/v2/inmemory/storage.hpp +++ b/src/storage/v2/inmemory/storage.hpp @@ -203,7 +203,7 @@ class InMemoryStorage final : public Storage { return static_cast(storage_)->indices_.label_property_index_->IndexExists(label, property); } - bool UniqueConstraintExists(LabelId label, PropertyId property) const override; + bool UniqueConstraintExists(const LabelId &label, const PropertyId &property) const override; IndicesInfo ListAllIndices() const override; diff --git a/src/storage/v2/inmemory/unique_constraints.cpp b/src/storage/v2/inmemory/unique_constraints.cpp index 9a6dbb132..622d6748f 100644 --- a/src/storage/v2/inmemory/unique_constraints.cpp +++ b/src/storage/v2/inmemory/unique_constraints.cpp @@ -410,14 +410,14 @@ std::vector>> InMemoryUniqueConstraints: return ret; } -uint64_t InMemoryUniqueConstraints::ApproximateVertexCount(LabelId label, PropertyId property) const { +uint64_t InMemoryUniqueConstraints::ApproximateVertexCount(const LabelId &label, const PropertyId &property) const { auto it = constraints_.find({label, {property}}); MG_ASSERT(it != constraints_.end(), "Unique constraints for label {} and property {} doesn't exist", label.AsUint(), property.AsUint()); return 1; }; -uint64_t InMemoryUniqueConstraints::ApproximateVertexCount(LabelId label, PropertyId property, +uint64_t InMemoryUniqueConstraints::ApproximateVertexCount(const LabelId &label, const PropertyId &property, const PropertyValue &value) const { auto it = constraints_.find({label, {property}}); MG_ASSERT(it != constraints_.end(), "Unique constraints for label {} and property {} doesn't exist", label.AsUint(), @@ -426,7 +426,7 @@ uint64_t InMemoryUniqueConstraints::ApproximateVertexCount(LabelId label, Proper }; uint64_t InMemoryUniqueConstraints::ApproximateVertexCount( - LabelId label, PropertyId property, const std::optional> &lower, + const LabelId &label, const PropertyId &property, const std::optional> &lower, const std::optional> &upper) const { auto it = constraints_.find({label, {property}}); MG_ASSERT(it != constraints_.end(), "Unique constraints for label {} and property {} doesn't exist", label.AsUint(), diff --git a/src/storage/v2/inmemory/unique_constraints.hpp b/src/storage/v2/inmemory/unique_constraints.hpp index 166c26d58..d88a15a20 100644 --- a/src/storage/v2/inmemory/unique_constraints.hpp +++ b/src/storage/v2/inmemory/unique_constraints.hpp @@ -131,9 +131,10 @@ class InMemoryUniqueConstraints : public UniqueConstraints { Transaction *transaction_; }; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property) const override; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property, const PropertyValue &value) const override; - uint64_t ApproximateVertexCount(LabelId label, PropertyId property, + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property) const override; + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, + const PropertyValue &value) const override; + uint64_t ApproximateVertexCount(const LabelId &label, const PropertyId &property, const std::optional> &lower, const std::optional> &upper) const override; diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index 9a6c7792c..bd9179532 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -255,7 +255,7 @@ class Storage { virtual UniqueConstraints::DeletionStatus DropUniqueConstraint(LabelId label, const std::set &properties) = 0; - virtual bool UniqueConstraintExists(LabelId label, PropertyId property) const = 0; + virtual bool UniqueConstraintExists(const LabelId &label, const PropertyId &property) const = 0; protected: Storage *storage_; diff --git a/tests/manual/interactive_planning.cpp b/tests/manual/interactive_planning.cpp index 6c0ae75d4..e89c77ae3 100644 --- a/tests/manual/interactive_planning.cpp +++ b/tests/manual/interactive_planning.cpp @@ -213,7 +213,8 @@ class InteractiveDbAccessor { return label_property_index_.at(key); } - bool UniqueConstraintExists(memgraph::storage::LabelId label_id, memgraph::storage::PropertyId property_id) { + bool UniqueConstraintExists(const memgraph::storage::LabelId &label_id, + const memgraph::storage::PropertyId &property_id) { return true; } @@ -226,6 +227,10 @@ class InteractiveDbAccessor { return dba_->GetIndexStats(label, property); } + bool IndexedScanExists(const memgraph::storage::LabelId &label, const memgraph::storage::PropertyId &prop) const { + return dba_->IndexedScanExists(label, prop); + } + // Save the cached vertex counts to a stream. void Save(std::ostream &out) { out << "vertex-count " << vertices_count_ << std::endl; diff --git a/tests/unit/query_plan_checker.hpp b/tests/unit/query_plan_checker.hpp index a53931260..cf03a0107 100644 --- a/tests/unit/query_plan_checker.hpp +++ b/tests/unit/query_plan_checker.hpp @@ -487,7 +487,7 @@ class FakeDbAccessor { return 0; } - int64_t VerticesCount(memgraph::storage::LabelId label, memgraph::storage::PropertyId property, + int64_t VerticesCount(const memgraph::storage::LabelId &label, const memgraph::storage::PropertyId &property, const memgraph::storage::PropertyValue &value) const { return 0; } @@ -496,10 +496,15 @@ class FakeDbAccessor { return label_index_.find(label) != label_index_.end(); } - bool UniqueConstraintExists(memgraph::storage::LabelId label, memgraph::storage::PropertyId property) const { + bool UniqueConstraintExists(const memgraph::storage::LabelId &label, + const memgraph::storage::PropertyId &property) const { return false; } + bool IndexedScanExists(const memgraph::storage::LabelId &label, const memgraph::storage::PropertyId &prop) const { + return LabelPropertyIndexExists(label, prop) || UniqueConstraintExists(label, prop); + } + bool LabelPropertyIndexExists(memgraph::storage::LabelId label, memgraph::storage::PropertyId property) const { for (auto &index : label_property_index_) { if (std::get<0>(index) == label && std::get<1>(index) == property) {