From e5f2ac36cc9f2880adf65873510fba7be686e813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Fri, 5 Jan 2024 00:15:47 +0100 Subject: [PATCH] Add text index Cypher syntax --- src/query/frontend/ast/ast.hpp | 10 +++++++--- .../frontend/ast/cypher_main_visitor.cpp | 20 ++++++++++++++++++- .../frontend/ast/cypher_main_visitor.hpp | 14 +++++++++++-- .../frontend/opencypher/grammar/Cypher.g4 | 6 +++++- .../opencypher/grammar/CypherLexer.g4 | 1 + 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/query/frontend/ast/ast.hpp b/src/query/frontend/ast/ast.hpp index 59860d5b0..33eb30f22 100644 --- a/src/query/frontend/ast/ast.hpp +++ b/src/query/frontend/ast/ast.hpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -2195,17 +2195,21 @@ class IndexQuery : public memgraph::query::Query { enum class Action { CREATE, DROP }; + enum class Type { LOOKUP, TEXT }; + IndexQuery() = default; DEFVISITABLE(QueryVisitor); memgraph::query::IndexQuery::Action action_; + memgraph::query::IndexQuery::Type type_; memgraph::query::LabelIx label_; std::vector properties_; IndexQuery *Clone(AstStorage *storage) const override { IndexQuery *object = storage->Create(); object->action_ = action_; + object->type_ = type_; object->label_ = storage->GetLabelIx(label_.name); object->properties_.resize(properties_.size()); for (auto i = 0; i < object->properties_.size(); ++i) { @@ -2215,8 +2219,8 @@ class IndexQuery : public memgraph::query::Query { } protected: - IndexQuery(Action action, LabelIx label, std::vector properties) - : action_(action), label_(label), properties_(properties) {} + IndexQuery(Action action, Type type, LabelIx label, std::vector properties) + : action_(action), type_(type), label_(label), properties_(properties) {} private: friend class AstStorage; diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index 7002ee4b9..1d976066b 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -246,6 +246,7 @@ antlrcpp::Any CypherMainVisitor::visitIndexQuery(MemgraphCypher::IndexQueryConte antlrcpp::Any CypherMainVisitor::visitCreateIndex(MemgraphCypher::CreateIndexContext *ctx) { auto *index_query = storage_->Create(); index_query->action_ = IndexQuery::Action::CREATE; + index_query->type_ = IndexQuery::Type::LOOKUP; index_query->label_ = AddLabel(std::any_cast(ctx->labelName()->accept(this))); if (ctx->propertyKeyName()) { auto name_key = std::any_cast(ctx->propertyKeyName()->accept(this)); @@ -254,9 +255,18 @@ antlrcpp::Any CypherMainVisitor::visitCreateIndex(MemgraphCypher::CreateIndexCon return index_query; } +antlrcpp::Any CypherMainVisitor::visitCreateTextIndex(MemgraphCypher::CreateTextIndexContext *ctx) { + auto *index_query = storage_->Create(); + index_query->action_ = IndexQuery::Action::CREATE; + index_query->type_ = IndexQuery::Type::TEXT; + index_query->label_ = AddLabel(std::any_cast(ctx->labelName()->accept(this))); + return index_query; +} + antlrcpp::Any CypherMainVisitor::visitDropIndex(MemgraphCypher::DropIndexContext *ctx) { auto *index_query = storage_->Create(); index_query->action_ = IndexQuery::Action::DROP; + index_query->type_ = IndexQuery::Type::LOOKUP; if (ctx->propertyKeyName()) { auto key = std::any_cast(ctx->propertyKeyName()->accept(this)); index_query->properties_ = {key}; @@ -265,6 +275,14 @@ antlrcpp::Any CypherMainVisitor::visitDropIndex(MemgraphCypher::DropIndexContext return index_query; } +antlrcpp::Any CypherMainVisitor::visitDropTextIndex(MemgraphCypher::DropTextIndexContext *ctx) { + auto *index_query = storage_->Create(); + index_query->action_ = IndexQuery::Action::DROP; + index_query->type_ = IndexQuery::Type::TEXT; + index_query->label_ = AddLabel(std::any_cast(ctx->labelName()->accept(this))); + return index_query; +} + antlrcpp::Any CypherMainVisitor::visitAuthQuery(MemgraphCypher::AuthQueryContext *ctx) { MG_ASSERT(ctx->children.size() == 1, "AuthQuery should have exactly one child!"); auto *auth_query = std::any_cast(ctx->children[0]->accept(this)); diff --git a/src/query/frontend/ast/cypher_main_visitor.hpp b/src/query/frontend/ast/cypher_main_visitor.hpp index 9e828674f..9591cc89e 100644 --- a/src/query/frontend/ast/cypher_main_visitor.hpp +++ b/src/query/frontend/ast/cypher_main_visitor.hpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -462,10 +462,20 @@ class CypherMainVisitor : public antlropencypher::MemgraphCypherBaseVisitor { antlrcpp::Any visitCreateIndex(MemgraphCypher::CreateIndexContext *ctx) override; /** - * @return DropIndex* + * @return IndexQuery* + */ + antlrcpp::Any visitCreateTextIndex(MemgraphCypher::CreateTextIndexContext *ctx) override; + + /** + * @return IndexQuery* */ antlrcpp::Any visitDropIndex(MemgraphCypher::DropIndexContext *ctx) override; + /** + * @return IndexQuery* + */ + antlrcpp::Any visitDropTextIndex(MemgraphCypher::DropTextIndexContext *ctx) override; + /** * @return AuthQuery* */ diff --git a/src/query/frontend/opencypher/grammar/Cypher.g4 b/src/query/frontend/opencypher/grammar/Cypher.g4 index d387002d8..b0b86b6b6 100644 --- a/src/query/frontend/opencypher/grammar/Cypher.g4 +++ b/src/query/frontend/opencypher/grammar/Cypher.g4 @@ -63,7 +63,7 @@ profileQuery : PROFILE cypherQuery ; cypherQuery : singleQuery ( cypherUnion )* ( queryMemoryLimit )? ; -indexQuery : createIndex | dropIndex; +indexQuery : createIndex | dropIndex | createTextIndex | dropTextIndex; singleQuery : clause ( clause )* ; @@ -339,6 +339,10 @@ createIndex : CREATE INDEX ON ':' labelName ( '(' propertyKeyName ')' )? ; dropIndex : DROP INDEX ON ':' labelName ( '(' propertyKeyName ')' )? ; +createTextIndex : CREATE TEXT INDEX ON ':' labelName ; + +dropTextIndex : DROP TEXT INDEX ON ':' labelName ; + doubleLiteral : FloatingLiteral ; cypherKeyword : ALL diff --git a/src/query/frontend/opencypher/grammar/CypherLexer.g4 b/src/query/frontend/opencypher/grammar/CypherLexer.g4 index 3428a2191..7866c9107 100644 --- a/src/query/frontend/opencypher/grammar/CypherLexer.g4 +++ b/src/query/frontend/opencypher/grammar/CypherLexer.g4 @@ -129,6 +129,7 @@ SHOW : S H O W ; SINGLE : S I N G L E ; STARTS : S T A R T S ; STORAGE : S T O R A G E ; +TEXT : T E X T ; THEN : T H E N ; TRUE : T R U E ; UNION : U N I O N ;