Skip to content

Commit

Permalink
feat: add plantransformer tool to convert any plan to any format (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
EpsilonPrime committed Mar 7, 2024
1 parent e3ba635 commit abad742
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/substrait/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ endif()
install(TARGETS substrait_io LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ../../../include/substrait/common/Io.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/substrait/common")

add_executable(plantransformer PlanTransformerTool.cpp)

target_link_libraries(plantransformer substrait_io)
54 changes: 54 additions & 0 deletions src/substrait/common/PlanTransformerTool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* SPDX-License-Identifier: Apache-2.0 */

#include <iostream>

#include "substrait/common/Io.h"

namespace io::substrait {
namespace {

PlanFileFormat planFileFormatFromText(std::string_view str) {
std::string foo;
foo.resize(str.size());
std::transform(str.begin(), str.end(), foo.begin(), [](unsigned char c) {
return std::tolower(c);
});
if (foo == "binary") {
return PlanFileFormat::kBinary;
} else if (foo == "json") {
return PlanFileFormat::kJson;
} else if (foo == "prototext") {
return PlanFileFormat::kProtoText;
} else if (foo == "text") {
return PlanFileFormat::kText;
}
// If the format can't be understood, default to text.
return PlanFileFormat::kText;
}

} // namespace
} // namespace io::substrait

int main(int argc, char* argv[]) {
if (argc <= 3) {
printf(
"Usage: plantransformer <inputfilename> <outputfilename> [BINARY|JSON|PROTOTEXT|TEXT]\n");
return EXIT_FAILURE;
}

auto planOrError = io::substrait::loadPlan(argv[1]);
if (!planOrError.ok()) {
std::cerr << planOrError.status() << std::endl;
return EXIT_FAILURE;
}

auto format = io::substrait::planFileFormatFromText(argv[3]);

auto result = io::substrait::savePlan(*planOrError, argv[2], format);
if (!result.ok()) {
std::cerr << result << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit abad742

Please sign in to comment.