Skip to content

Commit

Permalink
Adds the plantransformer tool to convert from any plan format to the …
Browse files Browse the repository at this point in the history
…specified format.
  • Loading branch information
EpsilonPrime committed Mar 4, 2024
1 parent e3ba635 commit 0acac77
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/substrait/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../../include/substrait/common>
$<INSTALL_INTERFACE:include/substrait/common>)
substrait_io
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../../include/substrait/common>
$<INSTALL_INTERFACE:include/substrait/common>)
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)
54 changes: 54 additions & 0 deletions src/substrait/common/Tool.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 0acac77

Please sign in to comment.