Created query execution result

This commit is contained in:
Josip Mrden 2024-02-28 14:56:38 +01:00
parent 2b30ba3fef
commit 2c0fc680de
5 changed files with 73 additions and 24 deletions

View File

@ -851,6 +851,8 @@ inline void func_result_set_value(mgp_func_result *res, mgp_value *value, mgp_me
MgInvokeVoid(mgp_func_result_set_value, res, value, memory);
}
inline void execute_query(mgp_graph *graph, const char *query) { MgInvokeVoid(mgp_execute_query, graph, query); }
inline void execute_query(mgp_graph *graph, const char *query, mgp_memory *memory) {
MgInvokeVoid(mgp_execute_query, graph, memory, query);
}
} // namespace mgp

View File

@ -1801,7 +1801,10 @@ enum mgp_error mgp_func_result_set_error_msg(struct mgp_func_result *result, con
enum mgp_error mgp_func_result_set_value(struct mgp_func_result *result, struct mgp_value *value,
struct mgp_memory *memory);
enum mgp_error mgp_execute_query(mgp_graph *graph, const char *query);
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);
/// @}

View File

@ -94,6 +94,7 @@ struct MapItem;
class Duration;
class Value;
class QueryExecution;
class QueryExecutionResult;
struct StealType {};
inline constexpr StealType steal{};
@ -1554,12 +1555,19 @@ class Return {
class QueryExecution {
public:
QueryExecution(mgp_graph *graph);
void ExecuteQuery(std::string_view query) const;
QueryExecutionResult ExecuteQuery(std::string_view query) const;
private:
mgp_graph *graph_;
};
class QueryExecutionResult {
mgp_query_execution_result *result_;
public:
QueryExecutionResult(mgp_query_execution_result *result);
};
enum class ProcedureType : uint8_t {
Read,
Write,
@ -4298,7 +4306,11 @@ inline mgp_type *Return::GetMGPType() const {
inline QueryExecution::QueryExecution(mgp_graph *graph) : graph_(graph) {}
inline void QueryExecution::ExecuteQuery(std::string_view query) const { mgp::execute_query(graph_, query.data()); }
inline QueryExecutionResult QueryExecution::ExecuteQuery(std::string_view query) const {
mgp::MemHandlerCallback(execute_query, graph_, query.data());
}
inline QueryExecutionResult::QueryExecutionResult(mgp_query_execution_result *result) : result_(result) {}
// do not enter
namespace detail {

View File

@ -28,7 +28,6 @@
#include "mg_procedure.h"
#include "module.hpp"
#include "query/db_accessor.hpp"
#include "query/discard_value_stream.hpp"
#include "query/frontend/ast/ast.hpp"
#include "query/interpreter.hpp"
#include "query/interpreter_context.hpp"
@ -4026,22 +4025,52 @@ mgp_error mgp_untrack_current_thread_allocations(mgp_graph *graph) {
});
}
mgp_error mgp_execute_query(mgp_graph *graph, const char *query) {
return WrapExceptions([&]() {
auto query_string = std::string(query);
auto *instance = memgraph::query::InterpreterContext::getInstance();
struct MgProcedureResultStream final {
using Row = std::vector<memgraph::query::TypedValue>;
using Rows = std::vector<Row>;
Rows rows;
void Result(const Row &row) { rows.push_back(row); }
};
memgraph::query::Interpreter interpreter(instance);
interpreter.SetUser(graph->ctx->user_or_role);
std::vector<std::vector<mgp_value>> GetMgpRowsFromTypedValues(
std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph, mgp_memory *memory) {
std::vector<std::vector<mgp_value>> mgp_rows;
for (auto &row : tv_rows) {
std::vector<mgp_value> vals;
for (auto &val : row) {
vals.emplace_back(val, graph, memory->impl);
}
mgp_rows.emplace_back(std::move(vals));
}
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.insert(&interpreter); });
memgraph::utils::OnScopeExit erase_interpreter([&] {
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.erase(&interpreter); });
});
auto results = interpreter.Prepare(query_string, {}, {});
memgraph::query::DiscardValueResultStream stream;
interpreter.Pull(&stream, {}, results.qid);
});
return mgp_rows;
}
mgp_query_execution_result::mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows,
mgp_graph *graph, mgp_memory *memory)
: 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) {
return WrapExceptions(
[&]() {
auto query_string = std::string(query);
auto *instance = memgraph::query::InterpreterContext::getInstance();
memgraph::query::Interpreter interpreter(instance);
interpreter.SetUser(graph->ctx->user_or_role);
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.insert(&interpreter); });
memgraph::utils::OnScopeExit erase_interpreter([&] {
instance->interpreters.WithLock([&interpreter](auto &interpreters) { interpreters.erase(&interpreter); });
});
auto results = interpreter.Prepare(query_string, {}, {});
MgProcedureResultStream stream;
interpreter.Pull(&stream, {}, results.qid);
return NewRawMgpObject<mgp_query_execution_result>(memory, std::move(stream.rows), graph, memory);
},
result);
}

View File

@ -995,8 +995,11 @@ bool ContainsDeleted(const mgp_value *val);
memgraph::query::TypedValue ToTypedValue(const mgp_value &val, memgraph::utils::MemoryResource *memory);
struct mgp_query_execution {
explicit mgp_query_execution(){};
struct mgp_query_execution_result {
explicit mgp_query_execution_result(std::vector<std::vector<memgraph::query::TypedValue>> tv_rows, mgp_graph *graph,
mgp_memory *memory);
~mgp_query_execution() = default;
~mgp_query_execution_result() = default;
std::vector<std::vector<mgp_value>> rows;
};