From abad7423db9e9612e428b264de484f88763a96c5 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Wed, 6 Mar 2024 16:10:01 -0800 Subject: [PATCH] feat: add plantransformer tool to convert any plan to any format (#98) --- src/substrait/common/CMakeLists.txt | 4 ++ src/substrait/common/PlanTransformerTool.cpp | 54 ++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/substrait/common/PlanTransformerTool.cpp diff --git a/src/substrait/common/CMakeLists.txt b/src/substrait/common/CMakeLists.txt index 38338e7e..d706c63f 100644 --- a/src/substrait/common/CMakeLists.txt +++ b/src/substrait/common/CMakeLists.txt @@ -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) diff --git a/src/substrait/common/PlanTransformerTool.cpp b/src/substrait/common/PlanTransformerTool.cpp new file mode 100644 index 00000000..8717916d --- /dev/null +++ b/src/substrait/common/PlanTransformerTool.cpp @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +#include + +#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 [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; +}