Skip to content

Commit

Permalink
Added a planloader test.
Browse files Browse the repository at this point in the history
  • Loading branch information
EpsilonPrime committed Feb 6, 2024
1 parent 73bec1f commit 74bf673
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
12 changes: 9 additions & 3 deletions export/planloader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# SPDX-License-Identifier: Apache-2.0

# MEGAHACK -- Given that the compiler options include lots of undesirable stuff in Debug mode, either cleanse it or refuse to build unless we're in release mode.
if(NOT BUILD_SUBDIR_NAME EQUAL "release")
message(SEND_ERROR, "The planloader library does not work in Debug mode due to its dependencies.")
endif()

add_library(planloader SHARED textplan.cpp)
add_library(planloader SHARED planloader.cpp)
add_compile_options(-FPIC)
set_target_properties(planloader PROPERTIES SUBVERSION 1)

add_dependencies(planloader substrait_io)
target_link_libraries(planloader substrait_io)

install(TARGETS planloader LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS planloader LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PRIVATE_HEADER DESTINATION ${CMAKE_INSTALL_INCDIR})

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
/* SPDX-License-Identifier: Apache-2.0 */

#include "planloader.h"

#include <substrait/common/Io.h>

extern "C" {

typedef struct {
char *buffer;
uint32_t size;
char *errorMessage;
} SerializedPlan;

// Load a Substrait plan (in any format) from disk.
// Stores the Substrait plan in planBuffer in serialized form.
// Returns a SerializedPlan structure containing either the serialized plan or
// an error message.
// an error message. errorMessage is nullptr upon success.
SerializedPlan* load_substrait_plan(const char* filename) {
// MEGAHACK -- Consider putting this initialization into a separate function.
auto newPlan = new SerializedPlan();
newPlan->buffer = nullptr;
newPlan->size = 0;
Expand Down Expand Up @@ -48,7 +43,7 @@ void free_substrait_plan(SerializedPlan* plan) {

// Write a serialized Substrait plan to disk in the specified format.
// On error returns a non-empty error message.
// On success an empty string is returned.
// On success a nullptr is returned.
const char* save_substrait_plan(
const char* planData,
uint32_t planDataLength,
Expand All @@ -61,7 +56,7 @@ const char* save_substrait_plan(
if (!result.ok()) {
return result.message().data();
}
return "";
return nullptr;
}

} // extern "C"
30 changes: 30 additions & 0 deletions export/planloader/planloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: Apache-2.0 */

#include <substrait/common/Io.h>

extern "C" {

typedef struct {
char *buffer;
uint32_t size;
char *errorMessage;
} SerializedPlan;

// Load a Substrait plan (in any format) from disk.
// Stores the Substrait plan in planBuffer in serialized form.
// Returns a SerializedPlan structure containing either the serialized plan or
// an error message.
SerializedPlan* load_substrait_plan(const char* filename);

void free_substrait_plan(SerializedPlan* plan);

// Write a serialized Substrait plan to disk in the specified format.
// On error returns a non-empty error message.
// On success an empty string is returned.
const char* save_substrait_plan(
const char* planData,
uint32_t planDataLength,
const char* filename,
io::substrait::PlanFileFormat format);

} // extern "C"
34 changes: 34 additions & 0 deletions export/planloader/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-License-Identifier: Apache-2.0

cmake_path(GET CMAKE_CURRENT_BINARY_DIR PARENT_PATH
CMAKE_CURRENT_BINARY_PARENT_DIR)
cmake_path(GET CMAKE_CURRENT_BINARY_PARENT_DIR PARENT_PATH
CMAKE_CURRENT_BINARY_TOPLEVEL_DIR)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_TOPLEVEL_DIR}/${BUILD_SUBDIR_NAME}")

add_test_case(
planloader_test
SOURCES
PlanLoaderTest.cpp
EXTRA_LINK_LIBS
planloader
gtest
gtest_main)

set(TEXTPLAN_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/substrait/textplan")

add_custom_command(
TARGET planloader_test
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Copying unit test data.."
COMMAND ${CMAKE_COMMAND} -E make_directory
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/data"
COMMAND
${CMAKE_COMMAND} -E copy
"${TEXTPLAN_SOURCE_DIR}/converter/data/q6_first_stage.json"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/data/q6_first_stage.json")

message(
STATUS "test data will be here: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/data")
32 changes: 32 additions & 0 deletions export/planloader/tests/PlanLoaderTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: Apache-2.0 */

#include <gtest/gtest.h>
#include <functional>

#include "../planloader.h"
#include "substrait/proto/plan.pb.h"

namespace io::substrait::textplan {
namespace {

TEST(PlanLoaderTest, LoadAndSave) {
auto serializedPlan = load_substrait_plan("data/q6_first_stage.json");
ASSERT_EQ(serializedPlan->errorMessage, nullptr);

::substrait::proto::Plan plan;
bool parseStatus =
plan.ParseFromArray(serializedPlan->buffer, serializedPlan->size);
ASSERT_TRUE(parseStatus) << "Failed to parse the plan.";

const char* saveStatus = save_substrait_plan(
serializedPlan->buffer,
serializedPlan->size,
"outfile.splan",
PlanFileFormat::kText);
ASSERT_EQ(saveStatus, nullptr);

free_substrait_plan(serializedPlan);
}

} // namespace
} // namespace io::substrait::textplan

0 comments on commit 74bf673

Please sign in to comment.