Enable Create schema ddl

This commit is contained in:
jbajic 2022-06-15 18:23:30 +02:00
parent cb0e072c7d
commit 88d54d4e2f
2 changed files with 40 additions and 43 deletions

View File

@ -2369,27 +2369,30 @@ antlrcpp::Any CypherMainVisitor::visitCreateSchema(MemgraphCypher::CreateSchemaC
throw SemanticException("Schema property map must exist!"); throw SemanticException("Schema property map must exist!");
} }
std::unordered_map<PropertyIx, common::SchemaType> schema_property_map;
for (auto *property_pair : ctx->schemaTypeMap()->propertyKeyTypePair()) { for (auto *property_pair : ctx->schemaTypeMap()->propertyKeyTypePair()) {
if (property_pair->propertyType()) { if (property_pair->propertyType()->BOOL()) {
schema_property_map.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::BOOL}); schema_query->schema_type_map_.insert({property_pair->propertyKeyName()->accept(this), common::SchemaType::BOOL});
} else if (property_pair->propertyType()->STRING()) { } 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()) { } 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()) { } 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()) { } 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()) { } 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()) { } 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()) { } 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; query_ = schema_query;
return schema_query; return schema_query;

View File

@ -827,8 +827,6 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters &paramete
Frame frame(0); Frame frame(0);
SymbolTable symbol_table; SymbolTable symbol_table;
EvaluationContext evaluation_context; EvaluationContext evaluation_context;
// TODO: MemoryResource for EvaluationContext, it should probably be passed as
// the argument to Callback.
evaluation_context.timestamp = QueryTimestamp(); evaluation_context.timestamp = QueryTimestamp();
evaluation_context.parameters = parameters; evaluation_context.parameters = parameters;
ExpressionEvaluator evaluator(&frame, symbol_table, evaluation_context, db_accessor, storage::View::OLD); ExpressionEvaluator evaluator(&frame, symbol_table, evaluation_context, db_accessor, storage::View::OLD);
@ -867,9 +865,9 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters &paramete
} }
case SchemaQuery::Action::SHOW_SCHEMA: { case SchemaQuery::Action::SHOW_SCHEMA: {
callback.header = {"property_name", "property_type"}; 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; 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); const auto schemas_info = db->GetSchema(label);
MG_ASSERT(schemas_info.schemas.size() < 2, "There can be only one schema under single label!"); MG_ASSERT(schemas_info.schemas.size() < 2, "There can be only one schema under single label!");
std::vector<std::vector<TypedValue>> results; std::vector<std::vector<TypedValue>> results;
@ -891,23 +889,26 @@ Callback HandleSchemaQuery(SchemaQuery *schema_query, const Parameters &paramete
return callback; return callback;
} }
case SchemaQuery::Action::CREATE_SCHEMA: { 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; auto *db = interpreter_context->db;
const auto label = db->NameToLabel(schema_query->label_.name); const auto label = db->NameToLabel(primary_label.name);
std::vector<storage::SchemaPropertyType> schemas_types; std::vector<storage::SchemaPropertyType> schemas_types;
// for (const auto &schema_property : schema_query->) { schemas_types.reserve(schema_type_map.size());
// spdlog::info("sasa {}", db->PropertyToName(db->NameToProperty(schema_property.first.name))); for (const auto &schema_type : schema_type_map) {
// // schemas_types.emplace_back(db->NameToProperty(schema_property.first.name), schema_property.second); auto property_id = db->NameToProperty(schema_type.first.name);
// } spdlog::info("sasa {}", db->PropertyToName(db->NameToProperty(schema_type.first.name)));
// const auto res = db->CreateSchema(label, schemas_types); schemas_types.push_back({schema_type.second, property_id});
}
const auto res = db->CreateSchema(label, schemas_types);
return std::vector<std::vector<TypedValue>>{}; return std::vector<std::vector<TypedValue>>{};
}; };
return callback; return callback;
} }
case SchemaQuery::Action::DROP_SCHEMA: { 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; 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); const auto res = db->DeleteSchema(label);
return std::vector<std::vector<TypedValue>>{}; return std::vector<std::vector<TypedValue>>{};
@ -2123,26 +2124,19 @@ PreparedQuery PrepareSchemaQuery(ParsedQuery parsed_query, bool in_explicit_tran
MG_ASSERT(schema_query); MG_ASSERT(schema_query);
auto callback = HandleSchemaQuery(schema_query, parsed_query.parameters, interpreter_context, dba, notifications); auto callback = HandleSchemaQuery(schema_query, parsed_query.parameters, interpreter_context, dba, notifications);
// return PreparedQuery{std::move(callback.header), std::move(parsed_query.required_privileges), return PreparedQuery{std::move(callback.header), std::move(parsed_query.required_privileges),
// [handler = std::move(callback.fn), action = QueryHandlerResult::NOTHING, [handler = std::move(callback.fn), action = QueryHandlerResult::NOTHING,
// pull_plan = std::shared_ptr<PullPlanVector>(nullptr)]( pull_plan = std::shared_ptr<PullPlanVector>(nullptr)](
// AnyStream *stream, std::optional<int> n) mutable -> std::optional<QueryHandlerResult> { AnyStream *stream, std::optional<int> n) mutable -> std::optional<QueryHandlerResult> {
// if (!pull_plan) { if (!pull_plan) {
// auto results = handler(); auto results = handler();
// pull_plan = std::make_shared<PullPlanVector>(std::move(results)); pull_plan = std::make_shared<PullPlanVector>(std::move(results));
// } }
// if (pull_plan->Pull(stream, n)) { if (pull_plan->Pull(stream, n)) {
// return action; return action;
// } }
// return std::nullopt; 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<int> /*n*/) mutable {
handler();
return QueryHandlerResult::COMMIT;
}, },
RWType::NONE}; RWType::NONE};
} }