|
| 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 |
0 commit comments