Sha256: 1e36346186da01094eb2fdb6b56b7d134b16341aba2d61a7d76650a793325671

Contents?: true

Size: 1.06 KB

Versions: 12

Compression:

Stored size: 1.06 KB

Contents

module Bmg
  module Operator
    #
    # Join operator.
    #
    # Natural join, following relational algebra
    #
    class Join
      include Operator::Binary

      def initialize(type, left, right, on)
        @type = type
        @left = left
        @right = right
        @on = on
      end

    private

      attr_reader :on

    public

      def each
        index = Hash.new
        right.each_with_object(index) do |t, index|
          key = tuple_project(t, on)
          index[key] ||= []
          index[key] << t
        end
        left.each do |tuple|
          key = tuple_project(tuple, on)
          if to_join = index[key]
            to_join.each do |right|
              yield right.merge(tuple)
            end
          end
        end
      end

      def to_ast
        [ :join, left.to_ast, right.to_ast, on ]
      end

    protected ### inspect

      def args
        [ on ]
      end

    private

      def tuple_project(tuple, on)
        TupleAlgebra.project(tuple, on)
      end

    end # class Join
  end # module Operator
end # module Bmg

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bmg-0.16.0.pre.rc1 lib/bmg/operator/join.rb
bmg-0.15.0 lib/bmg/operator/join.rb
bmg-0.14.6 lib/bmg/operator/join.rb
bmg-0.14.5 lib/bmg/operator/join.rb
bmg-0.14.4 lib/bmg/operator/join.rb
bmg-0.14.3 lib/bmg/operator/join.rb
bmg-0.14.2 lib/bmg/operator/join.rb
bmg-0.14.1 lib/bmg/operator/join.rb
bmg-0.14.0 lib/bmg/operator/join.rb
bmg-0.13.0 lib/bmg/operator/join.rb
bmg-0.12.0 lib/bmg/operator/join.rb
bmg-0.11.0 lib/bmg/operator/join.rb