Class: DeltaSharing::Schema
- Inherits:
-
Object
- Object
- DeltaSharing::Schema
- Defined in:
- lib/delta_sharing/schema.rb
Instance Attribute Summary collapse
-
#arrow_schema ⇒ Object
readonly
Returns the value of attribute arrow_schema.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Instance Method Summary collapse
- #build_arrow_schema ⇒ Object
- #delta_type_to_arrow_type(delta_type) ⇒ Object
- #field_index(name) ⇒ Object
- #field_names ⇒ Object
-
#initialize(schema_string) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(schema_string) ⇒ Schema
Returns a new instance of Schema.
7 8 9 10 11 |
# File 'lib/delta_sharing/schema.rb', line 7 def initialize(schema_string) schema_json = JSON.parse(schema_string) @fields = schema_json['fields'] || [] @arrow_schema = build_arrow_schema end |
Instance Attribute Details
#arrow_schema ⇒ Object (readonly)
Returns the value of attribute arrow_schema.
5 6 7 |
# File 'lib/delta_sharing/schema.rb', line 5 def arrow_schema @arrow_schema end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/delta_sharing/schema.rb', line 5 def fields @fields end |
Instance Method Details
#build_arrow_schema ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/delta_sharing/schema.rb', line 21 def build_arrow_schema fields = @fields.map do |field| arrow_type = delta_type_to_arrow_type(field['type']) Arrow::Field.new(field['name'], arrow_type, field['nullable']) end Arrow::Schema.new(fields) end |
#delta_type_to_arrow_type(delta_type) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/delta_sharing/schema.rb', line 30 def delta_type_to_arrow_type(delta_type) case delta_type when 'boolean' Arrow::BooleanDataType.new when 'byte' Arrow::Int8DataType.new when 'short' Arrow::Int16DataType.new when 'integer' Arrow::Int32DataType.new when 'long' Arrow::Int64DataType.new when 'float' Arrow::FloatDataType.new when 'double' Arrow::DoubleDataType.new when 'string' Arrow::StringDataType.new when 'binary' Arrow::BinaryDataType.new when 'date' Arrow::Date32DataType.new when 'timestamp' Arrow::TimestampDataType.new(:micro) else if delta_type.is_a?(Hash) case delta_type['type'] when 'decimal' Arrow::DecimalDataType.new(delta_type['precision'], delta_type['scale']) when 'array' element_type = delta_type_to_arrow_type(delta_type['elementType']) Arrow::ListDataType.new(element_type) when 'map' key_type = delta_type_to_arrow_type(delta_type['keyType']) value_type = delta_type_to_arrow_type(delta_type['valueType']) Arrow::MapDataType.new(key_type, value_type) when 'struct' fields = delta_type['fields'].map do |field| field_type = delta_type_to_arrow_type(field['type']) Arrow::Field.new(field['name'], field_type, field['nullable']) end Arrow::StructDataType.new(fields) else Arrow::StringDataType.new # Fallback to string end else Arrow::StringDataType.new # Fallback to string end end end |
#field_index(name) ⇒ Object
17 18 19 |
# File 'lib/delta_sharing/schema.rb', line 17 def field_index(name) @fields.find_index { |f| f['name'] == name } end |
#field_names ⇒ Object
13 14 15 |
# File 'lib/delta_sharing/schema.rb', line 13 def field_names @fields.map { |f| f['name'] } end |