From 8fdc199d2ba9a5cb10fb1a581b3b617eb026d44d Mon Sep 17 00:00:00 2001 From: Josip Mrden Date: Wed, 28 Feb 2024 17:08:17 +0100 Subject: [PATCH] Implement headers in mgp --- include/_mgp.hpp | 4 ++++ include/mg_procedure.h | 5 +++++ include/mgp.hpp | 5 +++++ src/query/procedure/mg_procedure_impl.cpp | 21 +++++++++++++++------ src/query/procedure/mg_procedure_impl.hpp | 11 ++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/_mgp.hpp b/include/_mgp.hpp index d20c315c8..5e566cd63 100644 --- a/include/_mgp.hpp +++ b/include/_mgp.hpp @@ -855,4 +855,8 @@ inline mgp_query_execution_result *execute_query(mgp_graph *graph, const char *q return MgInvoke(mgp_execute_query, graph, memory, query); } +inline std::vector get_query_execution_headers(mgp_query_execution_result *query_execution) { + return MgInvoke>(mgp_get_query_execution_headers, query_execution); +} + } // namespace mgp diff --git a/include/mg_procedure.h b/include/mg_procedure.h index aead32b96..40682aab5 100644 --- a/include/mg_procedure.h +++ b/include/mg_procedure.h @@ -1806,6 +1806,11 @@ struct mgp_query_execution_result; enum mgp_error mgp_execute_query(mgp_graph *graph, struct mgp_memory *memory, const char *query, struct mgp_query_execution_result **result); +struct mgp_query_execution_headers; + +enum mgp_error mgp_get_query_execution_headers(struct mgp_query_execution_result *query_execution, + struct mgp_query_execution_headers **result); + /// @} #ifdef __cplusplus diff --git a/include/mgp.hpp b/include/mgp.hpp index 33110b523..0dae89e1b 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -1566,6 +1566,7 @@ class QueryExecutionResult { public: QueryExecutionResult(mgp_query_execution_result *result); + std::vector Headers() const; }; enum class ProcedureType : uint8_t { @@ -4312,6 +4313,10 @@ inline QueryExecutionResult QueryExecution::ExecuteQuery(std::string_view query) inline QueryExecutionResult::QueryExecutionResult(mgp_query_execution_result *result) : result_(result) {} +inline std::vector QueryExecutionResult::Headers() const { + return mgp::get_query_execution_headers(result_); +}; + // do not enter namespace detail { inline void AddParamsReturnsToProc(mgp_proc *proc, std::vector ¶meters, diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index 909951249..c899040a6 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -4046,9 +4046,12 @@ std::vector> GetMgpRowsFromTypedValues( return mgp_rows; } -mgp_query_execution_result::mgp_query_execution_result(std::vector> tv_rows, +mgp_query_execution_headers::mgp_query_execution_headers(std::vector headers) : headers(headers){}; + +mgp_query_execution_result::mgp_query_execution_result(std::vector headers, + std::vector> tv_rows, mgp_graph *graph, mgp_memory *memory) - : rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {} + : headers(headers), rows(GetMgpRowsFromTypedValues(std::move(tv_rows), graph, memory)) {} mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *query, mgp_query_execution_result **result) { @@ -4057,7 +4060,7 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu auto query_string = std::string(query); auto *instance = memgraph::query::InterpreterContext::getInstance(); - memgraph::query::Interpreter interpreter(instance); + memgraph::query::Interpreter interpreter(instance, instance->dbms_handler->Get()); interpreter.SetUser(graph->ctx->user_or_role); instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.insert(&interpreter); }); @@ -4066,11 +4069,17 @@ mgp_error mgp_execute_query(mgp_graph *graph, mgp_memory *memory, const char *qu instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.erase(&interpreter); }); }); - auto results = interpreter.Prepare(query_string, {}, {}); + auto prepare_query_result = interpreter.Prepare(query_string, {}, {}); MgProcedureResultStream stream; - interpreter.Pull(&stream, {}, results.qid); + interpreter.Pull(&stream, {}, prepare_query_result.qid); - return NewRawMgpObject(memory, std::move(stream.rows), graph, memory); + return NewRawMgpObject(memory, std::move(prepare_query_result.headers), + std::move(stream.rows), graph, memory); }, result); } + +mgp_error mgp_get_query_execution_headers(mgp_query_execution_result *execution_result, + mgp_query_execution_headers **result) { + return WrapExceptions([&]() { return &execution_result->headers; }, result); +} diff --git a/src/query/procedure/mg_procedure_impl.hpp b/src/query/procedure/mg_procedure_impl.hpp index 3569d0406..50705351b 100644 --- a/src/query/procedure/mg_procedure_impl.hpp +++ b/src/query/procedure/mg_procedure_impl.hpp @@ -995,11 +995,20 @@ bool ContainsDeleted(const mgp_value *val); memgraph::query::TypedValue ToTypedValue(const mgp_value &val, memgraph::utils::MemoryResource *memory); +struct mgp_query_execution_headers { + explicit mgp_query_execution_headers(std::vector headers); + ~mgp_query_execution_headers() = default; + + std::vector headers; +}; + struct mgp_query_execution_result { - explicit mgp_query_execution_result(std::vector> tv_rows, mgp_graph *graph, + explicit mgp_query_execution_result(std::vector headers, + std::vector> tv_rows, mgp_graph *graph, mgp_memory *memory); ~mgp_query_execution_result() = default; + mgp_query_execution_headers headers; std::vector> rows; };