Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add plantransformer tool to convert any plan to any format #98

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading