-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
184 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,47 @@ | ||
#ifndef __JOIN_H__ | ||
#define __JOIN_H__ | ||
|
||
#include <optional> | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
|
||
#include "komfydb/execution/join_predicate.h" | ||
#include "komfydb/execution/op_iterator.h" | ||
#include "komfydb/storage/page_id.h" | ||
#include "komfydb/storage/record_id.h" | ||
|
||
namespace { | ||
|
||
using komfydb::common::TupleDesc; | ||
using komfydb::execution::JoinPredicate; | ||
using komfydb::storage::RecordId; | ||
|
||
}; | ||
} // namespace | ||
|
||
namespace komfydb::execution { | ||
|
||
class Join : public OpIterator { | ||
public: | ||
static absl::StatusOr<std::unique_ptr<Join>> Create( | ||
std::unique_ptr<OpIterator> l_child, JoinPredicate join_predicate, | ||
std::unique_ptr<OpIterator> r_child); | ||
Join(TupleDesc& tuple_desc, std::vector<std::string> fields_table_ids, | ||
JoinPredicate join_predicate, std::unique_ptr<OpIterator> l_child, | ||
std::unique_ptr<OpIterator> r_child); | ||
|
||
JoinPredicate GetJoinPredicate(); | ||
|
||
absl::StatusOr<std::string> GetJoinField1Name(); | ||
|
||
absl::StatusOr<std::string> GetJoinField2Name(); | ||
|
||
absl::Status Open() override; | ||
|
||
void Close() override; | ||
|
||
absl::Status Rewind() override; | ||
|
||
void Explain(std::ostream& os, int indent = 0) override; | ||
// virtual ~Join() = default; | ||
|
||
protected: | ||
Join(std::unique_ptr<OpIterator> l_child, JoinPredicate join_predicate, | ||
std::unique_ptr<OpIterator> r_child, TupleDesc tuple_desc); | ||
|
||
absl::Status FetchNext() override; | ||
|
||
JoinPredicate join_predicate; | ||
std::unique_ptr<OpIterator> l_child; | ||
std::unique_ptr<OpIterator> r_child; | ||
std::unique_ptr<Record> l_child_next; | ||
const RecordId joined_record_id; | ||
}; | ||
|
||
}; // namespace komfydb::execution | ||
|
||
#endif // __JOIN_H__ | ||
#endif // __JOIN_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
|
||
#include "komfydb/utils/status_macros.h" | ||
|
||
#include "komfydb/common/tuple_desc.h" | ||
#include "komfydb/execution/join_predicate.h" | ||
#include "komfydb/execution/loops_join.h" | ||
#include "komfydb/storage/record.h" | ||
#include "komfydb/utils/utility.h" | ||
|
||
namespace { | ||
|
||
using komfydb::common::JoinVectors; | ||
using komfydb::common::TupleDesc; | ||
using komfydb::storage::PageId; | ||
using komfydb::storage::RecordId; | ||
|
||
} // namespace | ||
|
||
namespace komfydb::execution { | ||
|
||
LoopsJoin::LoopsJoin(std::unique_ptr<OpIterator> l_child, | ||
JoinPredicate join_predicate, | ||
std::unique_ptr<OpIterator> r_child, TupleDesc tuple_desc) | ||
: Join(tuple_desc, | ||
JoinVectors(*l_child->GetFieldsTableAliases(), | ||
*r_child->GetFieldsTableAliases()), | ||
join_predicate, std::move(l_child), std::move(r_child)), | ||
l_child_next(nullptr) {} | ||
|
||
absl::StatusOr<std::unique_ptr<LoopsJoin>> LoopsJoin::Create( | ||
std::unique_ptr<OpIterator> l_child, JoinPredicate join_predicate, | ||
std::unique_ptr<OpIterator> r_child) { | ||
TupleDesc tuple_desc(*l_child->GetTupleDesc(), *r_child->GetTupleDesc()); | ||
return std::unique_ptr<LoopsJoin>(new LoopsJoin( | ||
std::move(l_child), join_predicate, std::move(r_child), tuple_desc)); | ||
} | ||
|
||
absl::Status LoopsJoin::Open() { | ||
RETURN_IF_ERROR(l_child->Open()); | ||
RETURN_IF_ERROR(r_child->Open()); | ||
return absl::OkStatus(); | ||
} | ||
|
||
absl::Status LoopsJoin::Rewind() { | ||
RETURN_IF_ERROR(l_child->Rewind()); | ||
RETURN_IF_ERROR(r_child->Rewind()); | ||
l_child_next = {}; | ||
next_record = {}; | ||
return absl::OkStatus(); | ||
} | ||
|
||
absl::Status LoopsJoin::FetchNext() { | ||
if (next_record) | ||
return absl::OkStatus(); | ||
|
||
absl::Status status; | ||
|
||
while (!next_record) { | ||
if (!l_child_next) { | ||
if ((status = l_child->HasNext()).ok()) { | ||
l_child_next = *(l_child->Next()); | ||
} else { | ||
return status; | ||
} | ||
} | ||
while ((status = r_child->HasNext()).ok()) { | ||
std::unique_ptr<Record> potential_match = *(r_child->Next()); | ||
if (join_predicate.Filter(*l_child_next, *potential_match)) { | ||
next_record = std::make_unique<Record>( | ||
Record(Tuple(*l_child_next, std::move(*potential_match)), | ||
joined_record_id)); | ||
return absl::OkStatus(); | ||
} | ||
} | ||
RETURN_IF_NOT_OOR(status); | ||
l_child_next = {}; | ||
RETURN_IF_ERROR(r_child->Rewind()); | ||
continue; | ||
} | ||
|
||
return absl::OkStatus(); | ||
} | ||
|
||
void LoopsJoin::Explain(std::ostream& os, int indent) { | ||
os << Indent(indent) << "-> Nested loops join: " << join_predicate << "\n"; | ||
os << Indent(indent + td_indent) << "TD: " << tuple_desc << "\n"; | ||
l_child->Explain(os, indent + child_indent); | ||
r_child->Explain(os, indent + child_indent); | ||
} | ||
|
||
} // namespace komfydb::execution |
Oops, something went wrong.