Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Example 13] Basic Join Algorithm #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# BuzzDB Snippets

Same setup instructions as BuzzDB
Same setup instructions as BuzzDB. Run `make check-tests` for run tests instead of `ctest`.

Dependencies

```
apt install bison flex
```


27 changes: 27 additions & 0 deletions src/include/utils/join.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <vector>
#include <string>

namespace buzzdb {
namespace join {

class Table {
public:
Table() {};
Table(std::vector<std::vector<std::string>> d) : data(d) {};
~Table() {};

std::vector<std::vector<std::string>> data;
};

class Join {
public:
Join(Table &left, Table &right, size_t left_idx, size_t right_idx);
~Join() {};

Table result_table;
};

}
}
26 changes: 26 additions & 0 deletions src/utils/join.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "utils/join.h"

namespace buzzdb {
namespace join {

Join::Join(Table &left, Table &right, size_t left_idx, size_t right_idx) {
std::vector<std::vector<std::string>> result;
for (auto &left_row : left.data) {
for (auto &right_row : right.data) {
if (left_row[left_idx] == right_row[right_idx]) {
// join the two rows but only include the join column once
std::vector<std::string> row;
row.insert(row.end(), left_row.begin(), left_row.end());
for (size_t i = 0; i < right_row.size(); i++) {
if (i != right_idx) {
row.push_back(right_row[i]);
}
}
result.push_back(row);
}
}
}
result_table = Table(result);
}
}
}
27 changes: 27 additions & 0 deletions test/unit/utils/join_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <gtest/gtest.h>

#include "utils/join.h"

namespace {

TEST(JoinTests, BasicJoin) {
buzzdb::join::Table left({{"1", "2", "3"}, {"4", "5", "6"}});
buzzdb::join::Table right({{"1", "7", "8"}, {"4", "9", "10"}});
buzzdb::join::Join join{left, right, 0, 0};
buzzdb::join::Table expected({{"1", "2", "3", "7", "8"}, {"4", "5", "6", "9", "10"}});
EXPECT_EQ(join.result_table.data, expected.data);
}

TEST(JoinTests, NoJoin) {
buzzdb::join::Table left({{"1", "2", "3"}, {"4", "5", "6"}});
buzzdb::join::Table right({{"7", "8", "9"}, {"10", "11", "12"}});
buzzdb::join::Join join{left, right, 0, 0};
size_t expected_joined_table_length = 0;
EXPECT_EQ(join.result_table.data.size(), expected_joined_table_length);
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}