Sha256: a4c58bb86ea785acd1fe04f699aa97d838de28cd3ee8b7a14a166911749c7818

Contents?: true

Size: 1.87 KB

Versions: 31

Compression:

Stored size: 1.87 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)
        return to_enum unless block_given?
        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

31 entries across 31 versions & 1 rubygems

Version Path
bmg-0.23.3 lib/bmg/operator/group.rb
bmg-0.23.2 lib/bmg/operator/group.rb
bmg-0.23.1 lib/bmg/operator/group.rb
bmg-0.23.0 lib/bmg/operator/group.rb
bmg-0.21.5 lib/bmg/operator/group.rb
bmg-0.21.4 lib/bmg/operator/group.rb
bmg-0.20.5 lib/bmg/operator/group.rb
bmg-0.19.3 lib/bmg/operator/group.rb
bmg-0.21.3 lib/bmg/operator/group.rb
bmg-0.21.2 lib/bmg/operator/group.rb
bmg-0.21.0 lib/bmg/operator/group.rb
bmg-0.20.4 lib/bmg/operator/group.rb
bmg-0.20.2 lib/bmg/operator/group.rb
bmg-0.20.1 lib/bmg/operator/group.rb
bmg-0.20.0 lib/bmg/operator/group.rb
bmg-0.19.2 lib/bmg/operator/group.rb
bmg-0.19.1 lib/bmg/operator/group.rb
bmg-0.19.0 lib/bmg/operator/group.rb
bmg-0.18.15 lib/bmg/operator/group.rb
bmg-0.18.14 lib/bmg/operator/group.rb