Class: SparkConnect::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spark_connect/row.rb

Overview

An ordered collection of named fields representing a single row of a DataFrame, returned by DataFrame#collect, DataFrame#take, etc.

Fields are accessible positionally (row[0]), by name (row["id"] or row.id), and the whole row converts cleanly to a Hash or Array.

Examples:

row = SparkConnect::Row.new({ "id" => 1, "name" => "alice" })
row[0]        #=> 1
row["name"]   #=> "alice"
row.name      #=> "alice"
row.to_h      #=> {"id"=>1, "name"=>"alice"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Row #initialize(values, fields:) ⇒ Row

Returns a new instance of Row.

Overloads:

  • #initialize(hash) ⇒ Row

    Parameters:

    • hash (Hash)

      an ordered mapping of field name to value.

  • #initialize(values, fields:) ⇒ Row

    Parameters:

    • values (Array)

      positional values

    • fields (Array<String>)

      field names



30
31
32
33
34
35
36
37
38
# File 'lib/spark_connect/row.rb', line 30

def initialize(data = {}, fields: nil)
  if fields
    @fields = fields.map(&:to_s)
    @values = data
  else
    @fields = data.keys.map(&:to_s)
    @values = data.values
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Allows row.field_name access for field names that are valid method names.



87
88
89
90
91
92
93
94
# File 'lib/spark_connect/row.rb', line 87

def method_missing(name, *args)
  key = name.to_s
  if args.empty? && @fields.include?(key)
    self[key]
  else
    super
  end
end

Instance Attribute Details

#fieldsArray<String> (readonly)

Returns the field names, in order.

Returns:

  • (Array<String>)

    the field names, in order.



20
21
22
# File 'lib/spark_connect/row.rb', line 20

def fields
  @fields
end

#valuesArray (readonly)

Returns the field values, in order.

Returns:

  • (Array)

    the field values, in order.



23
24
25
# File 'lib/spark_connect/row.rb', line 23

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



79
80
81
# File 'lib/spark_connect/row.rb', line 79

def ==(other)
  other.is_a?(Row) && other.fields == fields && other.values == values
end

#[](key) ⇒ Object?

Look up a value by zero-based index or by field name.

Parameters:

  • key (Integer, String, Symbol)

Returns:

  • (Object, nil)


44
45
46
47
48
49
50
51
# File 'lib/spark_connect/row.rb', line 44

def [](key)
  case key
  when Integer then @values[key]
  else
    idx = @fields.index(key.to_s)
    idx && @values[idx]
  end
end

#eachObject

Iterate over the values in order.



65
# File 'lib/spark_connect/row.rb', line 65

def each(&) = @values.each(&)

#field(name) ⇒ Object

Returns the value for name, raising if the field is absent.

Returns:

  • (Object)

    the value for name, raising if the field is absent.

Raises:



72
73
74
75
76
77
# File 'lib/spark_connect/row.rb', line 72

def field(name)
  idx = @fields.index(name.to_s)
  raise IllegalArgumentError, "No such field: #{name}" unless idx

  @values[idx]
end

#hashObject



84
# File 'lib/spark_connect/row.rb', line 84

def hash = [fields, values].hash

#lengthInteger Also known as: size

Returns number of fields.

Returns:

  • (Integer)

    number of fields.



68
# File 'lib/spark_connect/row.rb', line 68

def length = @values.length

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/spark_connect/row.rb', line 96

def respond_to_missing?(name, include_private = false)
  @fields.include?(name.to_s) || super
end

#to_aArray

Returns the row's values, in order.

Returns:

  • (Array)

    the row's values, in order.



60
61
62
# File 'lib/spark_connect/row.rb', line 60

def to_a
  @values.dup
end

#to_hHash Also known as: as_dict

Returns an ordered Hash of field name to value.

Returns:

  • (Hash)

    an ordered Hash of field name to value.



54
55
56
# File 'lib/spark_connect/row.rb', line 54

def to_h
  @fields.zip(@values).to_h
end

#to_sObject Also known as: inspect



100
101
102
# File 'lib/spark_connect/row.rb', line 100

def to_s
  "Row(#{@fields.zip(@values).map { |k, v| "#{k}=#{v.inspect}" }.join(', ')})"
end