Class: SparkConnect::Types::StructType

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

Overview

A struct (row) type: an ordered collection of StructFields. This is the type of every DataFrame's schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DataType

#hash, #inspect, #json, #to_s

Constructor Details

#initialize(fields = []) ⇒ StructType

Returns a new instance of StructType.



362
363
364
365
# File 'lib/spark_connect/types.rb', line 362

def initialize(fields = [])
  super()
  @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



360
361
362
# File 'lib/spark_connect/types.rb', line 360

def fields
  @fields
end

Instance Method Details

#==(other) ⇒ Object



406
# File 'lib/spark_connect/types.rb', line 406

def ==(other) = other.is_a?(StructType) && other.fields == fields

#[](key) ⇒ Object



381
# File 'lib/spark_connect/types.rb', line 381

def [](key) = key.is_a?(Integer) ? fields[key] : fields.find { |f| f.name == key.to_s }

#add(name, data_type = nil, nullable: true, metadata: nil) ⇒ StructType

Append a field and return self (chainable builder).

Parameters:

Returns:



371
372
373
374
375
376
377
378
# File 'lib/spark_connect/types.rb', line 371

def add(name, data_type = nil, nullable: true, metadata: nil)
  @fields << if name.is_a?(StructField)
               name
             else
               StructField.new(name, data_type, nullable: nullable, metadata: )
             end
  self
end

#eachObject



380
# File 'lib/spark_connect/types.rb', line 380

def each(&) = fields.each(&)

#json_valueObject



389
390
391
# File 'lib/spark_connect/types.rb', line 389

def json_value
  { "type" => "struct", "fields" => fields.map(&:json_value) }
end

#lengthObject Also known as: size



383
# File 'lib/spark_connect/types.rb', line 383

def length = fields.length

#namesObject



382
# File 'lib/spark_connect/types.rb', line 382

def names = fields.map(&:name)

#simple_stringObject



386
# File 'lib/spark_connect/types.rb', line 386

def simple_string = "struct<#{fields.map(&:simple_string).join(',')}>"

#to_protoObject



393
394
395
# File 'lib/spark_connect/types.rb', line 393

def to_proto
  Types.wrap(struct: Proto::DataType::Struct.new(fields: fields.map(&:to_proto)))
end

#tree_stringString

A human-readable, indented tree (used by DataFrame#print_schema).

Returns:

  • (String)


400
401
402
403
404
# File 'lib/spark_connect/types.rb', line 400

def tree_string
  lines = ["root"]
  fields.each { |f| append_tree(lines, f, " |") }
  "#{lines.join("\n")}\n"
end

#type_nameObject



387
# File 'lib/spark_connect/types.rb', line 387

def type_name = "struct"