Sha256: 3dc8235b11a2053ff3a1548f38a1ff95fdc6b01a010c48e8e5c3ed63a7966044

Contents?: true

Size: 1.83 KB

Versions: 30

Compression:

Stored size: 1.83 KB

Contents

module Bmg
  module Operator
    #
    # Group operator.
    #
    # Groups some operand attributes as a new Relation-valued
    # attribute
    #
    class Group
      include Operator::Unary

      DEFAULT_OPTIONS = {

        # Whether we need to convert each group as an Array,
        # instead of keeping a Relation instance
        array: false

      }

      def initialize(type, operand, attrs, as, options)
        @type = type
        @operand = operand
        @attrs = attrs
        @as = as
        @options = DEFAULT_OPTIONS.merge(options)
      end

    protected

      attr_reader :attrs, :as, :options

    public

      def each(&bl)
        index = Hash.new{|h,k| h[k] = k.merge(as => empty_group) }
        operand.each do |tuple|
          key = TupleAlgebra.allbut(tuple, attrs)
          sub = TupleAlgebra.project(tuple, attrs)
          index[key][as].operand << sub
        end
        if options[:array]
          index.values.each do |tuple|
            tuple[as] = tuple[as].to_a
            yield(tuple)
          end
        else
          index.values.each(&bl)
        end
      end

      def to_ast
        [ :group, operand.to_ast, attrs.dup, as, options.dup ]
      end

    protected ### optimization

      def _restrict(type, predicate)
        top, bottom = predicate.and_split([as])
        if top == predicate
          super
        else
          op = operand
          op = op.restrict(bottom)
          op = op.group(attrs, as, options)
          op = op.restrict(top)
          op
        end
      end

    protected ### inspect

      def args
        [ attrs, as, options ]
      end

    private

      def empty_group
        Relation::InMemory.new(group_type, Set.new)
      end

      def group_type
        operand.type.project(attrs)
      end

    end # class Extend
  end # module Operator
end # module Bmg

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
bmg-0.18.2 lib/bmg/operator/group.rb
bmg-0.18.1 lib/bmg/operator/group.rb
bmg-0.18.0 lib/bmg/operator/group.rb
bmg-0.17.8 lib/bmg/operator/group.rb
bmg-0.17.7 lib/bmg/operator/group.rb
bmg-0.17.6 lib/bmg/operator/group.rb
bmg-0.17.5 lib/bmg/operator/group.rb
bmg-0.17.4 lib/bmg/operator/group.rb
bmg-0.17.3 lib/bmg/operator/group.rb
bmg-0.17.2 lib/bmg/operator/group.rb
bmg-0.16.7 lib/bmg/operator/group.rb
bmg-0.17.1 lib/bmg/operator/group.rb
bmg-0.17.0 lib/bmg/operator/group.rb
bmg-0.16.6 lib/bmg/operator/group.rb
bmg-0.16.5 lib/bmg/operator/group.rb
bmg-0.16.4 lib/bmg/operator/group.rb
bmg-0.16.3 lib/bmg/operator/group.rb
bmg-0.16.2 lib/bmg/operator/group.rb
bmg-0.16.1 lib/bmg/operator/group.rb
bmg-0.16.0 lib/bmg/operator/group.rb