From 0acac77d8494b0573399006c411f5d63c0ce9150 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Mon, 4 Mar 2024 10:48:04 -0800 Subject: [PATCH] Adds the plantransformer tool to convert from any plan format to the specified format. --- src/substrait/common/CMakeLists.txt | 35 +++++++++++-------- src/substrait/common/Tool.cpp | 54 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/substrait/common/Tool.cpp diff --git a/src/substrait/common/CMakeLists.txt b/src/substrait/common/CMakeLists.txt index 38338e7e..dc67481e 100644 --- a/src/substrait/common/CMakeLists.txt +++ b/src/substrait/common/CMakeLists.txt @@ -5,25 +5,30 @@ target_link_libraries(substrait_common fmt::fmt-header-only) add_library(substrait_io STATIC Io.cpp) add_dependencies( - substrait_io - substrait_proto - substrait_textplan_converter - substrait_textplan_loader - fmt::fmt-header-only - absl::status - absl::statusor) + substrait_io + substrait_proto + substrait_textplan_converter + substrait_textplan_loader + fmt::fmt-header-only + absl::status + absl::statusor) target_include_directories( - substrait_io - INTERFACE - $ - $) + substrait_io + INTERFACE + $ + $) target_link_libraries(substrait_io substrait_proto substrait_textplan_converter - substrait_textplan_loader absl::status absl::statusor) + substrait_textplan_loader absl::status absl::statusor) -if(${SUBSTRAIT_CPP_BUILD_TESTING}) - add_subdirectory(tests) -endif() +if (${SUBSTRAIT_CPP_BUILD_TESTING}) + add_subdirectory(tests) +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 Tool.cpp) + +target_link_libraries(plantransformer substrait_io) diff --git a/src/substrait/common/Tool.cpp b/src/substrait/common/Tool.cpp new file mode 100644 index 00000000..8717916d --- /dev/null +++ b/src/substrait/common/Tool.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; +}