Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Implementing Theta Join (select/where) using iterators

by jdporter (Paladin)
on Jul 21, 2008 at 21:03 UTC ( [id://699152]=CUFP: print w/replies, xml ) Need Help??

I present here a technique for performing a Θ join on two or more data sets.
This does in perl what SELECT/WHERE does in SQL (approximately).

The input data sets and the result set are presented via iterators. The implementation below leverages the techniques and classes I posted in Using Nested Iterators to find a Cross Product and A Filtering Iterator.

# sample data. I use arrays for illustrative purposes, # but data could come from anywhere. my @author = ( [ 'Alonzo', 'Church', ], [ 'Stephen', 'Kleene', ], [ 'Wilhelm', 'Ackermann', ], [ 'Willard', 'Quine', ], ); my @author_book = ( [ 'Alonzo', '0691029067', ], [ 'Stephen', '0486425339', ], [ 'Wilhelm', '0821820249', ], [ 'Wilhelm', 'B000O5Q8QG', ], [ 'Willard', '0674554515', ], [ 'Willard', '0674802071', ], ); my @book_title = ( [ '0674802071', 'Set Theory and Its Logic', ], [ '0674554515', 'Mathematical Logic', ], [ 'B000O5Q8QG', 'Solvable Cases of the Decision Problem', ], [ '0821820249', 'Principles of Mathematical Logic', ], [ '0486425339', 'Mathematical Logic', ], [ '0691029067', 'Introduction to Mathematical Logic', ], ); my $join_authors_books = # a filter iterator for implementing our join condition: Iterator::Filter->new( # iterator for walking the cross product: Iterator::Product->new( # iterators for each of the above arrays: Iterator::Array->new( \@author ), Iterator::Array->new( \@author_book ), Iterator::Array->new( \@book_title ), ), # our condition: # where author.name = author_book.name # and author_book.isbn = book_title.isbn sub{ my($author,$author_book,$book_title) = @_; # each is an arrayref - a row from the corresponding "table" $author->[0] eq $author_book->[0] && $author_book->[1] eq $book_title->[0] } ); until ( $join_authors_books->is_exhausted ) { my($author,$author_book,$book_title) = $join_authors_books->value; local($,,$\) = ("\t","\n"); # the $author_book array doesn't contain any info not present in t +he other two print @$author, @$book_title; }
Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://699152]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found