1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include "catch.hpp"
#include "duckdb/common/serializer/buffered_file_reader.hpp"
#include "duckdb/common/serializer/buffered_file_writer.hpp"
#include "duckdb/parser/parser.hpp"
#include "duckdb/planner/planner.hpp"
#include "duckdb/parser/statement/logical_plan_statement.hpp"
#include "duckdb/common/serializer/binary_serializer.hpp"
#include "duckdb/common/serializer/binary_deserializer.hpp"
#include "test_helpers.hpp"
#include "tpch_extension.hpp"
#include <fstream>
using namespace duckdb;
using namespace std;
string get_full_file_name(const string &file_name) {
auto my_name = string("test_plan_serialization_bwc.cpp");
auto path = string(__FILE__);
return path.replace(path.rfind(my_name), my_name.length(), file_name);
}
void load_db(Connection &con) {
std::ifstream queries(get_full_file_name("db_load.sql"));
string query;
while (std::getline(queries, query)) {
REQUIRE_NO_FAIL(con.Query(query));
}
}
void test_deserialization(const string &file_location);
const char *PERSISTENT_FILE_NAME = "serialized_plans.binary";
TEST_CASE("Generate serialized plans file", "[.][serialization]") {
string file_location;
if (std::getenv("GEN_PLAN_STORAGE") != nullptr) {
// there is no way in catch2 to only run a test if explicitly requested. Hidden tests will
// run when "*" is used - which we do to run slow tests. To avoid re-generating the bin file
// we require an env variable to be explicitly set.
//
// set `GEN_PLAN_STORAGE` as an environment variable to generate the serialized file
file_location = get_full_file_name(PERSISTENT_FILE_NAME);
} else {
file_location = TestCreatePath("serialized_plans.new.binary");
}
DuckDB db;
Connection con(db);
load_db(con);
BufferedFileWriter target(db.GetFileSystem(), file_location);
std::ifstream queries(get_full_file_name("queries.sql"));
string query;
while (std::getline(queries, query)) {
con.BeginTransaction();
Parser p;
p.ParseQuery(query);
Planner planner(*con.context);
planner.CreatePlan(std::move(p.statements[0]));
auto plan = std::move(planner.plan);
BinarySerializer serializer(target);
serializer.Begin();
plan->Serialize(serializer);
serializer.End();
con.Rollback();
}
target.Sync();
test_deserialization(file_location);
}
TEST_CASE("Test deserialized plans from file", "[.][serialization]") {
test_deserialization(get_full_file_name(PERSISTENT_FILE_NAME));
}
void test_deserialization(const string &file_location) {
DuckDB db;
Connection con(db);
load_db(con);
BufferedFileReader file_source(db.GetFileSystem(), file_location.c_str());
std::ifstream queries(get_full_file_name("queries.sql"));
string query;
while (std::getline(queries, query)) {
INFO("evaluating " << query)
con.BeginTransaction();
BinaryDeserializer deserializer(file_source);
deserializer.Set<ClientContext &>(*con.context);
deserializer.Begin();
auto deserialized_plan = LogicalOperator::Deserialize(deserializer);
deserializer.End();
deserialized_plan->ResolveOperatorTypes();
auto deserialized_results =
con.context->Query(make_uniq<LogicalPlanStatement>(std::move(deserialized_plan)), false);
REQUIRE_NO_FAIL(*deserialized_results);
Parser p;
p.ParseQuery(query);
Planner planner(*con.context);
planner.CreatePlan(std::move(p.statements[0]));
auto expected_plan = std::move(planner.plan);
expected_plan->ResolveOperatorTypes();
auto expected_results = con.Query(query);
REQUIRE_NO_FAIL(*expected_results);
if (deserialized_results->names.size() == expected_results->names.size()) {
// ignore names
deserialized_results->names = expected_results->names;
}
if (!deserialized_results->Equals(*expected_results)) {
fprintf(stderr, "-----------------------------------\n");
fprintf(stderr, "Deserialized result does not match!\n");
fprintf(stderr, "-----------------------------------\n");
fprintf(stderr, "Query: %s\n", query.c_str());
fprintf(stderr, "-------------Deserialized----------\n");
deserialized_results->Print();
fprintf(stderr, "---------------Expected------------\n");
expected_results->Print();
fprintf(stderr, "-----------------------------------\n");
FAIL("Deserialized result does not match");
}
con.Rollback();
}
}
|