Skip to content

Commit

Permalink
Switched to using std::ofstream instead of Google's protobuf stream f…
Browse files Browse the repository at this point in the history
…or JSON.
  • Loading branch information
EpsilonPrime committed Jul 29, 2023
1 parent 7a63886 commit 37b1469
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/substrait/textplan/converter/SaveBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,44 @@ absl::Status savePlanToBinary(
absl::Status savePlanToJson(
const ::substrait::proto::Plan& plan,
std::string_view output_filename) {
int outputFileDescriptor =
creat(std::string{output_filename}.c_str(), S_IREAD | S_IWRITE);
if (outputFileDescriptor == -1) {
return absl::ErrnoToStatus(
errno,
std::ofstream stream(std::string{output_filename});
if ((stream.fail())) {
return absl::UnavailableError(
fmt::format("Failed to open file {} for writing", output_filename));
}
auto stream =
new google::protobuf::io::FileOutputStream(outputFileDescriptor);

std::string output;
auto status = ::google::protobuf::util::MessageToJsonString(plan, &output);
if (!status.ok()) {
return absl::UnknownError("Failed to save plan as a JSON protobuf.");
}
if (!stream->WriteCord(absl::Cord(output))) {
stream << output;
if (stream.fail()) {
return absl::UnknownError("Failed to write the plan as a JSON protobuf.");
}

delete stream;
close(outputFileDescriptor);
stream.close();
return absl::OkStatus();
}

absl::Status savePlanToText(
const ::substrait::proto::Plan& plan,
std::string_view output_filename) {
std::ofstream stream(std::string{output_filename});
if ((stream.fail())) {
return absl::UnavailableError(
fmt::format("Failed to open file {} for writing", output_filename));
}

auto result = parseBinaryPlan(plan);
auto errors = result.getAllErrors();
if (!errors.empty()) {
return absl::UnknownError(joinLines(errors));
}
stream << SymbolTablePrinter::outputToText(result.getSymbolTable());
if (stream.fail()) {
return absl::UnknownError("Failed to write the plan as text.");
}
stream.close();
return absl::OkStatus();
}
Expand Down

0 comments on commit 37b1469

Please sign in to comment.