From 88d54d4e2f6c8feb46f5a62239810c23d01e5b51 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 15 Jun 2022 18:23:30 +0200 Subject: [PATCH] Enable Create schema ddl --- .../frontend/ast/cypher_main_visitor.cpp | 25 ++++---- src/query/interpreter.cpp | 58 +++++++++---------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index 4662228d0..ebf4a7771 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -2369,27 +2369,30 @@ antlrcpp::Any CypherMainVisitor::visitCreateSchema(MemgraphCypher::CreateSchemaC throw SemanticException("Schema property map must exist!"); } - std::unordered_map schema_property_map; for (auto *property_pair : ctx->schemaTypeMap()->propertyKeyTypePair()) { - if (property_pair->propertyType()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::BOOL}); + if (property_pair->propertyType()->BOOL()) { + schema_query->schema_type_map_.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::BOOL}); } else if (property_pair->propertyType()->STRING()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::STRING}); + schema_query->schema_type_map_.insert( + {property_pair->propertyKeyName()->accept(this), common::SchemaType::STRING}); } else if (property_pair->propertyType()->INTEGER()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::INT}); + schema_query->schema_type_map_.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::INT}); } else if (property_pair->propertyType()->FLOAT()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::FLOAT}); + schema_query->schema_type_map_.insert( + {property_pair->propertyKeyName()->accept(this), common::SchemaType::FLOAT}); } else if (property_pair->propertyType()->DATE()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::DATE}); + schema_query->schema_type_map_.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::DATE}); } else if (property_pair->propertyType()->DURATION()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::DURATION}); + schema_query->schema_type_map_.insert( + {property_pair->propertyKeyName()->accept(this), common::SchemaType::DURATION}); } else if (property_pair->propertyType()->LOCALDATETIME()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::LOCALDATETIME}); + schema_query->schema_type_map_.insert( + {property_pair->propertyKeyName()->accept(this), common::SchemaType::LOCALDATETIME}); } else if (property_pair->propertyType()->LOCALTIME()) { - schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::LOCALTIME}); + schema_query->schema_type_map_.insert( + {property_pair->propertyKeyName()->accept(this), common::SchemaType::LOCALTIME}); } } - schema_query->schema_type_map_ = std::move(schema_property_map); query_ = schema_query; return schema_query; diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index 420e33c4e..aa40236d9 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -827,8 +827,6 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters ¶mete Frame frame(0); SymbolTable symbol_table; EvaluationContext evaluation_context; - // TODO: MemoryResource for EvaluationContext, it should probably be passed as - // the argument to Callback. evaluation_context.timestamp = QueryTimestamp(); evaluation_context.parameters = parameters; ExpressionEvaluator evaluator(&frame, symbol_table, evaluation_context, db_accessor, storage::View::OLD); @@ -867,9 +865,9 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters ¶mete } case SchemaQuery::Action::SHOW_SCHEMA: { callback.header = {"property_name", "property_type"}; - callback.fn = [interpreter_context, schema_query]() { + callback.fn = [interpreter_context, primary_label = schema_query->label_]() { auto *db = interpreter_context->db; - const auto label = db->NameToLabel(schema_query->label_.name); + const auto label = db->NameToLabel(primary_label.name); const auto schemas_info = db->GetSchema(label); MG_ASSERT(schemas_info.schemas.size() < 2, "There can be only one schema under single label!"); std::vector> results; @@ -891,23 +889,26 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters ¶mete return callback; } case SchemaQuery::Action::CREATE_SCHEMA: { - callback.fn = [interpreter_context, schema_query]() { + callback.fn = [interpreter_context, primary_label = schema_query->label_, + schema_type_map = schema_query->schema_type_map_]() { auto *db = interpreter_context->db; - const auto label = db->NameToLabel(schema_query->label_.name); + const auto label = db->NameToLabel(primary_label.name); std::vector schemas_types; - // for (const auto &schema_property : schema_query->) { - // spdlog::info("sasa {}", db->PropertyToName(db->NameToProperty(schema_property.first.name))); - // // schemas_types.emplace_back(db->NameToProperty(schema_property.first.name), schema_property.second); - // } - // const auto res = db->CreateSchema(label, schemas_types); + schemas_types.reserve(schema_type_map.size()); + for (const auto &schema_type : schema_type_map) { + auto property_id = db->NameToProperty(schema_type.first.name); + spdlog::info("sasa {}", db->PropertyToName(db->NameToProperty(schema_type.first.name))); + schemas_types.push_back({schema_type.second, property_id}); + } + const auto res = db->CreateSchema(label, schemas_types); return std::vector>{}; }; return callback; } case SchemaQuery::Action::DROP_SCHEMA: { - callback.fn = [interpreter_context, schema_query]() { + callback.fn = [interpreter_context, primary_label = schema_query->label_]() { auto *db = interpreter_context->db; - const auto label = db->NameToLabel(schema_query->label_.name); + const auto label = db->NameToLabel(primary_label.name); const auto res = db->DeleteSchema(label); return std::vector>{}; @@ -2123,26 +2124,19 @@ PreparedQuery PrepareSchemaQuery(ParsedQuery parsed_query, bool in_explicit_tran MG_ASSERT(schema_query); auto callback = HandleSchemaQuery(schema_query, parsed_query.parameters, interpreter_context, dba, notifications); - // return PreparedQuery{std::move(callback.header), std::move(parsed_query.required_privileges), - // [handler = std::move(callback.fn), action = QueryHandlerResult::NOTHING, - // pull_plan = std::shared_ptr(nullptr)]( - // AnyStream *stream, std::optional n) mutable -> std::optional { - // if (!pull_plan) { - // auto results = handler(); - // pull_plan = std::make_shared(std::move(results)); - // } + return PreparedQuery{std::move(callback.header), std::move(parsed_query.required_privileges), + [handler = std::move(callback.fn), action = QueryHandlerResult::NOTHING, + pull_plan = std::shared_ptr(nullptr)]( + AnyStream *stream, std::optional n) mutable -> std::optional { + if (!pull_plan) { + auto results = handler(); + pull_plan = std::make_shared(std::move(results)); + } - // if (pull_plan->Pull(stream, n)) { - // return action; - // } - // return std::nullopt; - // }, - // RWType::NONE}; - return PreparedQuery{{std::move(callback.header)}, - std::move(parsed_query.required_privileges), - [handler = std::move(callback.fn)](AnyStream * /*stream*/, std::optional /*n*/) mutable { - handler(); - return QueryHandlerResult::COMMIT; + if (pull_plan->Pull(stream, n)) { + return action; + } + return std::nullopt; }, RWType::NONE}; }