Class: Arrow::ArrayBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/array-builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(values) ⇒ Object



23
24
25
26
27
28
29
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
80
81
82
83
84
85
86
87
# File 'lib/arrow/array-builder.rb', line 23

def build(values)
  if self != ArrayBuilder
    builder = new
    return builder.build(values)
  end

  builder_class = nil
  builder_class_arguments = []
  values.each do |value|
    case value
    when nil
      # Ignore
    when true, false
      return BooleanArray.new(values)
    when String
      return StringArray.new(values)
    when Float
      return DoubleArray.new(values)
    when Integer
      if value < 0
        builder = IntArrayBuilder.new
        return builder.build(values)
      else
        builder_class = UIntArrayBuilder
        builder_class_arguments = []
      end
    when Time
      data_type = value.data_type
      case data_type.unit
      when TimeUnit::SECOND
        if builder.nil?
          builder = Time32ArrayBuilder
          builder_class_arguments = [data_type]
        end
      when TimeUnit::MILLI
        if builder != Time64ArrayBuilder
          builder = Time32ArrayBuilder
          builder_class_arguments = [data_type]
        end
      when TimeUnit::MICRO
        builder = Time64ArrayBuilder
        builder_class_arguments = [data_type]
      when TimeUnit::NANO
        builder = Time64ArrayBuilder.new(data_type)
        return builder.build(values)
      end
    when ::Time
      data_type = TimestampDataType.new(:nano)
      builder = TimestampArrayBuilder.new(data_type)
      return builder.build(values)
    when DateTime
      return Date64Array.new(values)
    when Date
      return Date32Array.new(values)
    else
      return StringArray.new(values)
    end
  end
  if builder_class
    builder = builder_class.new(*builder_class_arguments)
    builder.build(values)
  else
    Arrow::StringArray.new(values)
  end
end

.buildable?(args) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/arrow/array-builder.rb', line 89

def buildable?(args)
  args.size == method(:build).arity
end

Instance Method Details

#append(*values) ⇒ Object

Since:

  • 0.12.0



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/arrow/array-builder.rb', line 100

def append(*values)
  value_convertable = respond_to?(:convert_to_arrow_value, true)
  start_index = 0
  current_index = 0
  status = :value

  values.each do |value|
    if value.nil?
      if status == :value
        if start_index != current_index
          target_values = values[start_index...current_index]
          if value_convertable
            target_values = target_values.collect do |v|
              convert_to_arrow_value(v)
            end
          end
          append_values(target_values, nil)
          start_index = current_index
        end
        status = :null
      end
    else
      if status == :null
        append_nulls(current_index - start_index)
        start_index = current_index
        status = :value
      end
    end
    current_index += 1
  end
  if start_index != current_index
    if status == :value
      if start_index == 0 and current_index == values.size
        target_values = values
      else
        target_values = values[start_index...current_index]
      end
      if value_convertable
        target_values = target_values.collect do |v|
          convert_to_arrow_value(v)
        end
      end
      append_values(target_values, nil)
    else
      append_nulls(current_index - start_index)
    end
  end
end

#append_nulls(n) ⇒ Object



149
150
151
152
153
# File 'lib/arrow/array-builder.rb', line 149

def append_nulls(n)
  n.times do
    append_null
  end
end

#build(values) ⇒ Object



94
95
96
97
# File 'lib/arrow/array-builder.rb', line 94

def build(values)
  append(*values)
  finish
end