Module: Piglet::Field::Field

Overview

:nodoc:

Defined Under Namespace

Classes: DummyRelation

Constant Summary collapse

SYMBOLIC_OPERATORS =
[:==, :>, :<, :>=, :<=, :%, :+, :-, :*, :/]
FUNCTIONS =
[:avg, :count, :max, :min, :size, :sum, :tokenize]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/piglet/field/field.rb', line 9

def name
  @name
end

#predecessorsObject (readonly)

Returns the value of attribute predecessors.



9
10
11
# File 'lib/piglet/field/field.rb', line 9

def predecessors
  @predecessors
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/piglet/field/field.rb', line 9

def type
  @type
end

Instance Method Details

#and(other) ⇒ Object



59
60
61
# File 'lib/piglet/field/field.rb', line 59

def and(other)
  InfixExpression.new('AND', self, other, :type => :boolean)
end

#as(new_name) ⇒ Object



25
26
27
# File 'lib/piglet/field/field.rb', line 25

def as(new_name)
  Rename.new(new_name, self)
end

#cast(type) ⇒ Object



41
42
43
# File 'lib/piglet/field/field.rb', line 41

def cast(type)
  PrefixExpression.new("(#{type.to_s})", self, true, :type => type.to_sym)
end

#diff(other) ⇒ Object

Raises:



21
22
23
# File 'lib/piglet/field/field.rb', line 21

def diff(other)
  raise NotSupportedError
end

#distinctObject



89
90
91
# File 'lib/piglet/field/field.rb', line 89

def distinct
  DirectExpression.new("DISTINCT #{field_alias}", self)
end

#empty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/piglet/field/field.rb', line 17

def empty?
  CallExpression.new('IsEmpty', self, :type => :boolean)
end

#field(name) ⇒ Object



119
120
121
# File 'lib/piglet/field/field.rb', line 119

def field(name)
  Reference.new(name, self, :explicit_ancestry => true)
end

#field_aliasObject



81
82
83
# File 'lib/piglet/field/field.rb', line 81

def field_alias
  @field_alias ||= generate_field_alias
end

#filter(&block) ⇒ Object



108
109
110
111
112
113
# File 'lib/piglet/field/field.rb', line 108

def filter(&block)
  dummy_relation = DummyRelation.new(self.send(:alias))
  context = Relation::BlockContext.new(dummy_relation, @interpreter)
  expression = context.instance_eval(&block)
  DirectExpression.new("FILTER #{field_alias} BY #{expression}", self)
end

#flattenObject



115
116
117
# File 'lib/piglet/field/field.rb', line 115

def flatten
  DirectExpression.new("FLATTEN(#{field_alias})", self)
end

#generate_field_aliasObject



73
74
75
76
77
78
79
# File 'lib/piglet/field/field.rb', line 73

def generate_field_alias
  if @parent.respond_to?(:next_field_alias)
    @parent.next_field_alias
  elsif predecessors.first.respond_to?(:generate_field_alias)
    predecessors.first.generate_field_alias
  end
end

#get(key) ⇒ Object



123
124
125
# File 'lib/piglet/field/field.rb', line 123

def get(key)
  MapValue.new(key, self)
end

#limit(size) ⇒ Object



93
94
95
# File 'lib/piglet/field/field.rb', line 93

def limit(size)
  DirectExpression.new("LIMIT #{field_alias} #{size}", self)
end

#matches(pattern) ⇒ Object



45
46
47
48
49
# File 'lib/piglet/field/field.rb', line 45

def matches(pattern)
  regex_options_pattern = /^\(\?.+?:(.*)\)$/
  pattern = pattern.to_s.sub(regex_options_pattern, '\1') if pattern.is_a?(Regexp) && pattern.to_s =~ regex_options_pattern
  InfixExpression.new('matches', self, "'#{pattern.to_s}'", :type => :boolean)
end

#ne(other) ⇒ Object



55
56
57
# File 'lib/piglet/field/field.rb', line 55

def ne(other)
  InfixExpression.new('!=', self, other, :type => :boolean)
end

#negObject



51
52
53
# File 'lib/piglet/field/field.rb', line 51

def neg
  PrefixExpression.new('-', self, false, :type => self.type)
end

#notObject



29
30
31
# File 'lib/piglet/field/field.rb', line 29

def not
  PrefixExpression.new('NOT', self, true, :type => :boolean)
end

#not_null?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/piglet/field/field.rb', line 37

def not_null?
  SuffixExpression.new('is not null', self, :type => :boolean)
end

#null?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/piglet/field/field.rb', line 33

def null?
  SuffixExpression.new('is null', self, :type => :boolean)
end

#or(other) ⇒ Object



63
64
65
# File 'lib/piglet/field/field.rb', line 63

def or(other)
  InfixExpression.new('OR', self, other, :type => :boolean)
end

#order(*args) ⇒ Object



101
102
103
104
105
106
# File 'lib/piglet/field/field.rb', line 101

def order(*args)
  fields, options = split_at_options(args)
  fields = *fields
  expression = Relation::Order.new(self, @interpreter, fields, options).to_s
  DirectExpression.new(expression, self)
end

#sample(rate) ⇒ Object



97
98
99
# File 'lib/piglet/field/field.rb', line 97

def sample(rate)
  DirectExpression.new("SAMPLE #{field_alias} #{rate}", self)
end