Module: Bmg::Relation

Includes:
Algebra, Enumerable
Included in:
Operator, Reader, Empty, InMemory, Sql::Relation
Defined in:
lib/bmg/relation.rb,
lib/bmg/relation/empty.rb,
lib/bmg/relation/proxy.rb,
lib/bmg/relation/spied.rb,
lib/bmg/relation/in_memory.rb,
lib/bmg/relation/materialized.rb

Defined Under Namespace

Modules: Proxy Classes: Empty, InMemory, Materialized, Spied

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #union, #unspied

Methods included from Algebra::Shortcuts

#image, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix

Class Method Details

.empty(type = Type::ANY) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/bmg/relation.rb', line 11

def self.empty(type = Type::ANY)
  raise ArgumentError, "Missing type" if type.nil?
  Relation::Empty.new(type)
end

.new(operand, type = Type::ANY) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/bmg/relation.rb', line 6

def self.new(operand, type = Type::ANY)
  raise ArgumentError, "Missing type" if type.nil?
  operand.is_a?(Relation) ? operand : Bmg.in_memory(operand, type)
end

Instance Method Details

#bind(binding) ⇒ Object



16
17
18
# File 'lib/bmg/relation.rb', line 16

def bind(binding)
  self
end

#debug(max_level = nil, on = STDERR) ⇒ Object

Returns a String representing the query plan



128
129
130
131
# File 'lib/bmg/relation.rb', line 128

def debug(max_level = nil, on = STDERR)
  on.puts(self.inspect)
  self
end

#deleteObject

Raises:



69
70
71
# File 'lib/bmg/relation.rb', line 69

def delete
  raise InvalidUpdateError, "Cannot delete from this Relvar"
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/bmg/relation.rb', line 32

def empty?
  each{|t| return false }
  true
end

#insert(arg) ⇒ Object

Raises:



61
62
63
# File 'lib/bmg/relation.rb', line 61

def insert(arg)
  raise InvalidUpdateError, "Cannot insert into this Relvar"
end

#oneObject

Returns the only tuple that the relation contains. Throws a OneException when there is no tuple or more than one



50
51
52
# File 'lib/bmg/relation.rb', line 50

def one
  one_or_yield{ raise OneError, "Relation is empty" }
end

#one_or_nilObject

Returns the only tuple that the relation contains. Returns nil if the relation is empty. Throws a OneException when the relation contains more than one tuple



57
58
59
# File 'lib/bmg/relation.rb', line 57

def one_or_nil
  one_or_yield{ nil }
end

#to_astObject

Converts to an sexpr expression.



123
124
125
# File 'lib/bmg/relation.rb', line 123

def to_ast
  raise "Bmg is missing a feature!"
end

#to_csv(options = {}, string_or_io = nil, preferences = nil) ⇒ Object

Writes the relation data to CSV.

‘string_or_io` and `options` are what CSV::new itself recognizes, default options are CSV’s.

When no string_or_io is used, the method uses a string.

The method always returns the string_or_io.



116
117
118
119
120
# File 'lib/bmg/relation.rb', line 116

def to_csv(options = {}, string_or_io = nil, preferences = nil)
  options, string_or_io = {}, options unless options.is_a?(Hash)
  string_or_io, preferences = nil, string_or_io if string_or_io.is_a?(Hash)
  Writer::Csv.new(options, preferences).call(self, string_or_io)
end

#to_json(*args, &bl) ⇒ Object

Returns a json representation



104
105
106
# File 'lib/bmg/relation.rb', line 104

def to_json(*args, &bl)
  to_a.to_json(*args, &bl)
end

#update(arg) ⇒ Object

Raises:



65
66
67
# File 'lib/bmg/relation.rb', line 65

def update(arg)
  raise InvalidUpdateError, "Cannot update this Relvar"
end

#visit(&visitor) ⇒ Object



73
74
75
# File 'lib/bmg/relation.rb', line 73

def visit(&visitor)
  _visit(nil, visitor)
end

#with_typecheckObject



20
21
22
23
24
# File 'lib/bmg/relation.rb', line 20

def with_typecheck
  dup.tap{|r|
    r.type = r.type.with_typecheck
  }
end

#without_typecheckObject



26
27
28
29
30
# File 'lib/bmg/relation.rb', line 26

def without_typecheck
  dup.tap{|r|
    r.type = r.type.with_typecheck
  }
end

#y_by_x(y, x, options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/bmg/relation.rb', line 82

def y_by_x(y, x, options = {})
  each_with_object({}) do |tuple, h|
    h[tuple[x]] = tuple[y]
  end
end

#ys_by_x(y, x, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bmg/relation.rb', line 88

def ys_by_x(y, x, options = {})
  ordering = options[:order]
  projection = [y, ordering].compact.uniq
  by_x = each_with_object({}) do |tuple,h|
    h[tuple[x]] ||= []
    h[tuple[x]] << TupleAlgebra.project(tuple, projection)
  end
  by_x.each_with_object({}) do |(x,ys),h|
    ys = ys.sort{|y1,y2| y1[ordering] <=> y2[ordering] } if ordering
    ys = ys.map{|t| t[y] }
    ys = ys.uniq if options[:distinct]
    h[x] = ys
  end
end