|< < 30 > >|

Joins

Nested Loops Join

Idea 3: More complex code

JoinNestedLoops::JoinNestedLoops(Iterator* R, Iterator* S) { this->R = R; this->S = S; this->r_row = NULL; this->s_row = NULL; } void JoinNestedLoops::open() { R->open(); S->open(); this->r_row = R->next(); this->s_row = S->next(); }
Row* JoinNestedLoops::next() { while (r_row != NULL && s_row != NULL && !match(r_row, s_row)) { // Rows don't match on join columns, advance S. s_row = S->next(); if (s_row == NULL) { // Finished the inner loop (on S). Advance R, and if there is another R row, then restart the scan of S. r_row = R->next(); if (r_row != NULL) { S->close(); S->open(); s_row = s->next(); } } } // We either have an r_row and s_row that match and should be joined, // or there is no more output. return r_row != NULL && s_row != NULL ? join_rows(r_row, s_row) : NULL; } void JoinNestedLoops::close() { R->close(); S->close(); }

|< < 30 > >|