From 0acac77d8494b0573399006c411f5d63c0ce9150 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Mon, 4 Mar 2024 10:48:04 -0800 Subject: [PATCH 1/4] 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; +} From 1427420f0c7a72fb8c67ed0e52889569aa001847 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Mon, 4 Mar 2024 10:58:10 -0800 Subject: [PATCH 2/4] cmake format fixes --- src/substrait/common/CMakeLists.txt | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/substrait/common/CMakeLists.txt b/src/substrait/common/CMakeLists.txt index dc67481e..e4d6b8d3 100644 --- a/src/substrait/common/CMakeLists.txt +++ b/src/substrait/common/CMakeLists.txt @@ -5,30 +5,29 @@ 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) + 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) From 8b77b970d05c4c9c58332759cb191f1a68fa4260 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Mon, 4 Mar 2024 11:02:35 -0800 Subject: [PATCH 3/4] one more --- src/substrait/common/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/substrait/common/CMakeLists.txt b/src/substrait/common/CMakeLists.txt index e4d6b8d3..2061b526 100644 --- a/src/substrait/common/CMakeLists.txt +++ b/src/substrait/common/CMakeLists.txt @@ -20,9 +20,9 @@ target_include_directories( target_link_libraries(substrait_io substrait_proto substrait_textplan_converter substrait_textplan_loader absl::status absl::statusor) -if (${SUBSTRAIT_CPP_BUILD_TESTING}) +if(${SUBSTRAIT_CPP_BUILD_TESTING}) add_subdirectory(tests) -endif () +endif() install(TARGETS substrait_io LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ../../../include/substrait/common/Io.h From 110f437a9d30739d20d5079d394233155ed739c4 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Wed, 6 Mar 2024 16:01:45 -0800 Subject: [PATCH 4/4] Renamed tool file to include the name of the tool. --- src/substrait/common/CMakeLists.txt | 2 +- src/substrait/common/{Tool.cpp => PlanTransformerTool.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/substrait/common/{Tool.cpp => PlanTransformerTool.cpp} (100%) diff --git a/src/substrait/common/CMakeLists.txt b/src/substrait/common/CMakeLists.txt index 2061b526..d706c63f 100644 --- a/src/substrait/common/CMakeLists.txt +++ b/src/substrait/common/CMakeLists.txt @@ -28,6 +28,6 @@ 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) +add_executable(plantransformer PlanTransformerTool.cpp) target_link_libraries(plantransformer substrait_io) diff --git a/src/substrait/common/Tool.cpp b/src/substrait/common/PlanTransformerTool.cpp similarity index 100% rename from src/substrait/common/Tool.cpp rename to src/substrait/common/PlanTransformerTool.cpp