forked from substrait-io/substrait-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanloader.cpp
More file actions
66 lines (56 loc) · 1.99 KB
/
planloader.cpp
File metadata and controls
66 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* SPDX-License-Identifier: Apache-2.0 */
#include "planloader.h"
#include <substrait/proto/plan.pb.h>
#include <cstdint>
#include <cstring>
#include <limits>
#include <string>
#include "substrait/common/Io.h"
extern "C" {
// Load a Substrait plan (in any format) from disk.
// Stores the Substrait plan in planBuffer in serialized form.
// Returns a SerializedPlan structure containing either the serialized plan or
// an error message. error_message is nullptr upon success.
SerializedPlan* load_substrait_plan(const char* filename) {
auto newPlan = new SerializedPlan();
newPlan->buffer = nullptr;
newPlan->size = 0;
newPlan->error_message = nullptr;
auto planOrError = io::substrait::loadPlan(filename);
if (!planOrError.ok()) {
auto errMsg = planOrError.status().message();
newPlan->error_message = new char[errMsg.length() + 1];
strncpy(newPlan->error_message, errMsg.data(), errMsg.length() + 1);
return newPlan;
}
const ::substrait::proto::Plan& plan = *planOrError;
std::string text = plan.SerializeAsString();
newPlan->buffer = new unsigned char[text.length() + 1];
memcpy(newPlan->buffer, text.data(), text.length() + 1);
newPlan->size =
static_cast<int32_t>(text.length() & std::numeric_limits<int32_t>::max());
return newPlan;
}
void free_substrait_plan(SerializedPlan* plan) {
delete[] plan->buffer;
delete[] plan->error_message;
delete plan;
}
// Write a serialized Substrait plan to disk in the specified format.
// On error returns a non-empty error message.
// On success a nullptr is returned.
const char* save_substrait_plan(
const unsigned char* plan_data,
int32_t plan_data_length,
const char* filename,
io::substrait::PlanFileFormat format) {
::substrait::proto::Plan plan;
std::string data((const char*)plan_data, plan_data_length);
plan.ParseFromString(data);
auto result = io::substrait::savePlan(plan, filename, format);
if (!result.ok()) {
return result.message().data();
}
return nullptr;
}
} // extern "C"