diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-27 14:19:59 -0700 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 15:44:12 -0400 |
commit | 2fe585328d6a24df310d3e60059c9c7b05b64bac (patch) | |
tree | 546895f0729a612ec3e004ee08ddb1b703b43b47 | |
parent | b7f58db57a535806e0cfc3057fbab80ca43b1a53 (diff) | |
download | rails-2fe585328d6a24df310d3e60059c9c7b05b64bac.tar.gz rails-2fe585328d6a24df310d3e60059c9c7b05b64bac.tar.bz2 rails-2fe585328d6a24df310d3e60059c9c7b05b64bac.zip |
performing in memory joins
-rw-r--r-- | doc/TODO | 8 | ||||
-rw-r--r-- | lib/arel/algebra/relations/row.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/memory/relations/operations.rb | 21 | ||||
-rw-r--r-- | spec/arel/engines/memory/unit/relations/join_spec.rb | 32 |
4 files changed, 61 insertions, 4 deletions
@@ -1,16 +1,13 @@ todo: -- reorganize memory tests -- implement joins in memory - result sets should be array relations - deal with table tests in algebra - cross-engine joins - blocks for joins - implement mnesia adapter -- fix grouping - fix AR - rename externalize to derived. -- result sets to attr correlation too +- fix grouping - audit unit coverage of algebra - data objects - remove all explicit aliasing @@ -92,6 +89,9 @@ done: - clean up block_given stuff - reorganize sql tests - recursive memory operations +- reorganize memory tests +- result sets to attr correlation too +- implement joins in memory icebox: - #bind in Attribute and Expression should be doing a descend? diff --git a/lib/arel/algebra/relations/row.rb b/lib/arel/algebra/relations/row.rb index 2d63498452..3731dd9696 100644 --- a/lib/arel/algebra/relations/row.rb +++ b/lib/arel/algebra/relations/row.rb @@ -17,5 +17,9 @@ module Arel def bind(relation) Row.new(relation, tuple) end + + def combine(other, relation) + Row.new(relation, tuple + other.tuple) + end end end
\ No newline at end of file diff --git a/lib/arel/engines/memory/relations/operations.rb b/lib/arel/engines/memory/relations/operations.rb index 8e03aca7b1..e35fbe3234 100644 --- a/lib/arel/engines/memory/relations/operations.rb +++ b/lib/arel/engines/memory/relations/operations.rb @@ -37,4 +37,25 @@ module Arel raise NotImplementedError end end + + class Alias < Compound + def eval + unoperated_rows + end + end + + class Join < Relation + def eval + result = [] + relation1.eval.each do |row1| + relation2.eval.each do |row2| + combined_row = row1.combine(row2, self) + if predicates.all? { |p| p.eval(combined_row) } + result << combined_row + end + end + end + result + end + end end
\ No newline at end of file diff --git a/spec/arel/engines/memory/unit/relations/join_spec.rb b/spec/arel/engines/memory/unit/relations/join_spec.rb new file mode 100644 index 0000000000..920cc55d0a --- /dev/null +++ b/spec/arel/engines/memory/unit/relations/join_spec.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @relation1 = Array.new([ + [1, 'duck' ], + [2, 'duck' ], + [3, 'goose'] + ], [:id, :name]) + @relation2 = @relation1.alias + @relation3 = @relation1.alias + end + + describe InnerJoin do + describe '#call' do + it 'combines the two tables where the predicate obtains' do + @relation1 \ + .join(@relation2) \ + .on(@relation1[:id].eq(@relation2[:id])) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, [1, 'duck', 1, 'duck' ]), + Row.new(relation, [2, 'duck', 2, 'duck' ]), + Row.new(relation, [3, 'goose', 3, 'goose']) + ] + end + end + end + end + end +end
\ No newline at end of file |