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

add basic join (inner join) example #32

Open
wants to merge 2 commits 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
36 changes: 36 additions & 0 deletions src/include/utils/innerjoin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <iostream>
#include <ostream>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

namespace buzzdb {
namespace utils {

class Table : public std::vector<std::vector<std::string>> {
public:
Table() : std::vector<std::vector<std::string>>() {}
Table(std::initializer_list<std::vector<std::string>> list)
: std::vector<std::vector<std::string>>(list) {}

friend std::ostream &operator<<(std::ostream &os, const Table &table);
};

class InnerJoin {
public:
InnerJoin(Table &left, Table &right, size_t left_attr_index,
size_t right_attr_index);
~InnerJoin();

friend std::ostream &operator<<(std::ostream &os, const Table &table);

Table joined_table;

private:
std::unordered_map<std::string, std::vector<std::string>> hashmap;
};

} // namespace utils
} // namespace buzzdb
39 changes: 39 additions & 0 deletions src/utils/innerjoin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../include/utils/innerjoin.h"
#include <iostream>
#include <string>
#include <vector>

#define UNUSED(p) ((void)(p))

namespace buzzdb {
namespace utils {
InnerJoin::InnerJoin(Table &left, Table &right, size_t left_attr_index,
size_t right_attr_index) {
for (size_t i = 0; i < left.size(); i++) {
hashmap[left[i][left_attr_index]] = left[i];
}

for (auto &tuple : right) {
if (hashmap.find(tuple[right_attr_index]) != hashmap.end()) {
std::vector<std::string> row = hashmap[tuple[right_attr_index]];
row.insert(row.end(), tuple.begin(), tuple.end());

joined_table.push_back(row);
}
}
}

InnerJoin::~InnerJoin() {}

std::ostream &operator<<(std::ostream &os, const buzzdb::utils::Table &table) {
for (size_t i = 0; i < table.size(); i++) {
for (size_t j = 0; j < table[i].size(); j++) {
os << table[i][j] << '\t';
}
os << std::endl;
}
return os;
}

} // namespace utils
} // namespace buzzdb
48 changes: 48 additions & 0 deletions test/unit/utils/innerjoin_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "utils/innerjoin.h"
#include <algorithm>
#include <gtest/gtest.h>
#include <iostream>
#include <string>

namespace {
using buzzdb::utils::InnerJoin;
using buzzdb::utils::Table;

Table relation_students{
// ID, Name, Age, Major
{"1", "Alice", "19", "CS"}, {"2", "Jonah", "20", "EE"},
{"3", "Charlie", "19", "CS"}, {"4", "David", "22", "EE"},
{"5", "Eve", "18", "CS"},
};

Table relation_grades{
// ID, Course, Grade
{"1", "CS101", "A"},
{"1", "EE200", "B"},
{"2", "CS101", "C"},
{"4", "EE200", "B"},
};

TEST(AlgorithmTest, InnerJoin) {
const Table expected_table = Table{
// ID, Name, Age, Major, ID, Course, Grade
{"1", "Alice", "19", "CS", "1", "CS101", "A"},
{"1", "Alice", "19", "CS", "1", "EE200", "B"},
{"2", "Jonah", "20", "EE", "2", "CS101", "C"},
{"4", "David", "22", "EE", "4", "EE200", "B"},
};

InnerJoin join{relation_students, relation_grades, 0, 0};

EXPECT_EQ(join.joined_table, expected_table);

std::cout << "Expected table:" << std::endl << expected_table << std::endl;
std::cout << "Joined table:" << std::endl << join.joined_table << std::endl;
}

} // namespace

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