Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 103c2d3

Browse files
committed
More tests
1 parent 9ee00c9 commit 103c2d3

File tree

4 files changed

+124
-12
lines changed

4 files changed

+124
-12
lines changed

test/codegen/csv_scan_test.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ struct State {
3232
CallbackFn callback;
3333
};
3434

35-
struct TempFileHandle {
36-
std::string name;
37-
TempFileHandle(std::string _name) : name(_name) {}
38-
~TempFileHandle() { boost::filesystem::remove(name); }
39-
};
40-
4135
void CSVRowCallback(void *s) {
4236
auto *state = reinterpret_cast<State *>(s);
4337
state->callback(state->scanner->GetColumns());
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
//
6+
// csv_scan_translator_test.cpp
7+
//
8+
// Identification: test/codegen/csv_scan_translator_test.cpp
9+
//
10+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "codegen/testing_codegen_util.h"
15+
16+
#include "planner/csv_scan_plan.h"
17+
#include "planner/insert_plan.h"
18+
#include "planner/seq_scan_plan.h"
19+
#include "util/file_util.h"
20+
21+
namespace peloton {
22+
namespace test {
23+
24+
class CSVScanTranslatorTest : public PelotonCodeGenTest {
25+
public:
26+
CSVScanTranslatorTest() : PelotonCodeGenTest() {}
27+
28+
oid_t TestTableId1() { return test_table_oids[0]; }
29+
uint32_t NumRowsInTestTable() const { return num_rows_to_insert; }
30+
31+
private:
32+
uint32_t num_rows_to_insert = 64;
33+
};
34+
35+
TEST_F(CSVScanTranslatorTest, IntCsvScan) {
36+
// Test input
37+
std::vector<std::string> rows = {"1,2,3.9,four",
38+
"5,6,7.4,eight",
39+
"9,10,11.1,\"twelve\""};
40+
std::string csv_data;
41+
for (const auto &row : rows) {
42+
csv_data.append(row).append("\n");
43+
}
44+
45+
///////////////////////////////////////////////////
46+
/// First insert contents of CSV into test table
47+
///////////////////////////////////////////////////
48+
{
49+
// Write the contents into a temporary file
50+
TempFileHandle fh{FileUtil::WriteTempFile(csv_data, "", "tmp")};
51+
52+
// clang-format off
53+
// NOTE: this schema has to match that of the test table!
54+
std::vector<planner::CSVScanPlan::ColumnInfo> cols = {
55+
planner::CSVScanPlan::ColumnInfo{.name = "1", .type = peloton::type::TypeId::INTEGER},
56+
planner::CSVScanPlan::ColumnInfo{.name = "2", .type = peloton::type::TypeId::INTEGER},
57+
planner::CSVScanPlan::ColumnInfo{.name = "3", .type = peloton::type::TypeId::DECIMAL},
58+
planner::CSVScanPlan::ColumnInfo{.name = "4", .type = peloton::type::TypeId::VARCHAR},
59+
};
60+
// clang-format on
61+
std::unique_ptr<planner::AbstractPlan> csv_scan{
62+
new planner::CSVScanPlan(fh.name, std::move(cols), ',')};
63+
std::unique_ptr<planner::AbstractPlan> insert{
64+
new planner::InsertPlan(&GetTestTable(TestTableId1()))};
65+
66+
insert->AddChild(std::move(csv_scan));
67+
68+
planner::BindingContext ctx;
69+
insert->PerformBinding(ctx);
70+
71+
codegen::BufferingConsumer consumer{{0, 1, 2, 3}, ctx};
72+
73+
// Execute insert
74+
CompileAndExecute(*insert, consumer);
75+
ASSERT_EQ(0, consumer.GetOutputTuples().size());
76+
}
77+
78+
///////////////////////////////////////////////////
79+
/// Now scan test table, comparing results
80+
///////////////////////////////////////////////////
81+
{
82+
std::unique_ptr<planner::AbstractPlan> scan{new planner::SeqScanPlan(
83+
&GetTestTable(TestTableId1()), nullptr, {0, 1, 2, 3})};
84+
85+
planner::BindingContext ctx;
86+
scan->PerformBinding(ctx);
87+
88+
codegen::BufferingConsumer consumer{{0, 1, 2, 3}, ctx};
89+
90+
// Execute insert
91+
CompileAndExecute(*scan, consumer);
92+
93+
const auto &output = consumer.GetOutputTuples();
94+
ASSERT_EQ(rows.size(), output.size());
95+
for (uint32_t i = 0; i < rows.size(); i++) {
96+
EXPECT_EQ(rows[i], output[i].ToCSV());
97+
}
98+
}
99+
}
100+
101+
} // namespace test
102+
} // namespace peloton

test/codegen/testing_codegen_util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "codegen/testing_codegen_util.h"
1414

15+
#include <boost/filesystem.hpp>
16+
1517
#include "catalog/table_catalog.h"
1618
#include "codegen/proxy/runtime_functions_proxy.h"
1719
#include "codegen/proxy/value_proxy.h"
@@ -28,6 +30,9 @@
2830
namespace peloton {
2931
namespace test {
3032

33+
TempFileHandle::TempFileHandle(std::string _name) : name(_name) {}
34+
TempFileHandle::~TempFileHandle() { boost::filesystem::remove(name); }
35+
3136
//===----------------------------------------------------------------------===//
3237
// PELOTON CODEGEN TEST
3338
//===----------------------------------------------------------------------===//

test/include/codegen/testing_codegen_util.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: test/include/codegen/testing_codegen_util.h
88
//
9-
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -40,11 +40,22 @@ using ConstExpressionPtr =
4040
using PlanPtr = std::unique_ptr<planner::AbstractPlan>;
4141
using ConstPlanPtr = std::unique_ptr<const planner::AbstractPlan>;
4242

43-
//===----------------------------------------------------------------------===//
44-
// Common base class for all codegen tests. This class four test tables that all
45-
// the codegen components use. Their ID's are available through the oid_t
46-
// enumeration.
47-
//===----------------------------------------------------------------------===//
43+
/**
44+
* This is a scoped file handle that automatically deletes/removes the file
45+
* with the given name when the class goes out of scope and the destructor is
46+
* called.
47+
*/
48+
struct TempFileHandle {
49+
std::string name;
50+
TempFileHandle(std::string _name);
51+
~TempFileHandle();
52+
};
53+
54+
/**
55+
* Common base class for all codegen tests. This class four test tables that all
56+
* the codegen components use. Their ID's are available through the oid_t
57+
* enumeration.
58+
*/
4859
class PelotonCodeGenTest : public PelotonTest {
4960
public:
5061
std::string test_db_name = "peloton_codegen";

0 commit comments

Comments
 (0)