]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/ruby/red-arrow/lib/arrow/array.rb
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / ruby / red-arrow / lib / arrow / array.rb
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 module Arrow
19 class Array
20 include Enumerable
21 include GenericFilterable
22 include GenericTakeable
23
24 class << self
25 def new(*args)
26 _builder_class = builder_class
27 return super if _builder_class.nil?
28 return super unless _builder_class.buildable?(args)
29 _builder_class.build(*args)
30 end
31
32 def builder_class
33 builder_class_name = "#{name}Builder"
34 return nil unless const_defined?(builder_class_name)
35 const_get(builder_class_name)
36 end
37 end
38
39 # @param i [Integer]
40 # The index of the value to be gotten.
41 #
42 # You can specify negative index like for `::Array#[]`.
43 #
44 # @return [Object, nil]
45 # The `i`-th value.
46 #
47 # `nil` for NULL value or out of range `i`.
48 def [](i)
49 i += length if i < 0
50 return nil if i < 0 or i >= length
51 if null?(i)
52 nil
53 else
54 get_value(i)
55 end
56 end
57
58 # @param other [Arrow::Array] The array to be compared.
59 # @param options [Arrow::EqualOptions, Hash] (nil)
60 # The options to custom how to compare.
61 #
62 # @return [Boolean]
63 # `true` if both of them have the same data, `false` otherwise.
64 #
65 # @since 5.0.0
66 def equal_array?(other, options=nil)
67 equal_options(other, options)
68 end
69
70 def each
71 return to_enum(__method__) unless block_given?
72
73 length.times do |i|
74 yield(self[i])
75 end
76 end
77
78 def reverse_each
79 return to_enum(__method__) unless block_given?
80
81 (length - 1).downto(0) do |i|
82 yield(self[i])
83 end
84 end
85
86 def to_arrow
87 self
88 end
89
90 alias_method :value_data_type_raw, :value_data_type
91 def value_data_type
92 @value_data_type ||= value_data_type_raw
93 end
94
95 def to_a
96 values
97 end
98
99 alias_method :is_in_raw, :is_in
100 def is_in(values)
101 case values
102 when ::Array
103 if self.class.builder_class.buildable?([values])
104 values = self.class.new(values)
105 else
106 values = self.class.new(value_data_type, values)
107 end
108 is_in_raw(values)
109 when ChunkedArray
110 is_in_chunked_array(values)
111 else
112 is_in_raw(values)
113 end
114 end
115
116 # @api private
117 alias_method :concatenate_raw, :concatenate
118 # Concatenates the given other arrays to the array.
119 #
120 # @param other_arrays [::Array, Arrow::Array] The arrays to be
121 # concatenated.
122 #
123 # Each other array is processed by {#resolve} before they're
124 # concatenated.
125 #
126 # @example Raw Ruby Array
127 # array = Arrow::Int32Array.new([1])
128 # array.concatenate([2, 3], [4]) # => Arrow::Int32Array.new([1, 2, 3, 4])
129 #
130 # @example Arrow::Array
131 # array = Arrow::Int32Array.new([1])
132 # array.concatenate(Arrow::Int32Array.new([2, 3]),
133 # Arrow::Int8Array.new([4])) # => Arrow::Int32Array.new([1, 2, 3, 4])
134 #
135 # @since 4.0.0
136 def concatenate(*other_arrays)
137 other_arrays = other_arrays.collect do |other_array|
138 resolve(other_array)
139 end
140 concatenate_raw(other_arrays)
141 end
142
143 # Concatenates the given other array to the array.
144 #
145 # If you have multiple arrays to be concatenated, you should use
146 # {#concatenate} to concatenate multiple arrays at once.
147 #
148 # @param other_array [::Array, Arrow::Array] The array to be concatenated.
149 #
150 # `@other_array` is processed by {#resolve} before it's
151 # concatenated.
152 #
153 # @example Raw Ruby Array
154 # Arrow::Int32Array.new([1]) + [2, 3] # => Arrow::Int32Array.new([1, 2, 3])
155 #
156 # @example Arrow::Array
157 # Arrow::Int32Array.new([1]) +
158 # Arrow::Int32Array.new([2, 3]) # => Arrow::Int32Array.new([1, 2, 3])
159 #
160 # @since 4.0.0
161 def +(other_array)
162 concatenate(other_array)
163 end
164
165 # Ensures returning the same data type array from the given array.
166 #
167 # @return [Arrow::Array]
168 #
169 # @overload resolve(other_raw_array)
170 #
171 # @param other_raw_array [::Array] A raw Ruby Array. A new Arrow::Array
172 # is built by `self.class.new`.
173 #
174 # @example Raw Ruby Array
175 # int32_array = Arrow::Int32Array.new([1])
176 # other_array = int32_array.resolve([2, 3, 4])
177 # other_array # => Arrow::Int32Array.new([2, 3, 4])
178 #
179 # @overload resolve(other_array)
180 #
181 # @param other_array [Arrow::Array] Another Arrow::Array.
182 #
183 # If the given other array is an same data type array of
184 # `self`, the given other array is returned as-is.
185 #
186 # If the given other array isn't an same data type array of
187 # `self`, the given other array is casted.
188 #
189 # @example Same data type
190 # int32_array = Arrow::Int32Array.new([1])
191 # other_int32_array = Arrow::Int32Array.new([2, 3, 4])
192 # other_array = int32_array.resolve(other_int32_array)
193 # other_array.object_id == other_int32_array.object_id
194 #
195 # @example Other data type
196 # int32_array = Arrow::Int32Array.new([1])
197 # other_int8_array = Arrow::Int8Array.new([2, 3, 4])
198 # other_array = int32_array.resolve(other_int32_array)
199 # other_array #=> Arrow::Int32Array.new([2, 3, 4])
200 #
201 # @since 4.0.0
202 def resolve(other_array)
203 if other_array.is_a?(::Array)
204 builder_class = self.class.builder_class
205 if builder_class.nil?
206 message =
207 "[array][resolve] can't build #{value_data_type} array " +
208 "from raw Ruby Array"
209 raise ArgumentError, message
210 end
211 if builder_class.buildable?([other_array])
212 other_array = builder_class.build(other_array)
213 elsif builder_class.buildable?([value_data_type, other_array])
214 other_array = builder_class.build(value_data_type, other_array)
215 else
216 message =
217 "[array][resolve] need to implement " +
218 "a feature that building #{value_data_type} array " +
219 "from raw Ruby Array"
220 raise NotImpelemented, message
221 end
222 other_array
223 elsif other_array.respond_to?(:value_data_type)
224 return other_array if value_data_type == other_array.value_data_type
225 other_array.cast(value_data_type)
226 else
227 message =
228 "[array][resolve] can't build #{value_data_type} array: " +
229 "#{other_array.inspect}"
230 raise ArgumentError, message
231 end
232 end
233 end
234 end