Skip to content

Commit fe11431

Browse files
okadakkkou
andcommitted
ARROW-15935: [Ruby] Add test for Arrow::DictionaryArray#values
Closes apache#12850 from okadakk/arrow-ruby-add-test-for-arrow-dictionary-array-values Lead-authored-by: okadakk <k.suke.jp1990@gmail.com> Co-authored-by: okadak <k.suke.jp1990@gmail.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 71ba864 commit fe11431

1 file changed

Lines changed: 295 additions & 0 deletions

File tree

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
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 ValuesDictionaryArrayTests
19+
def test_null
20+
target = build(Arrow::NullArray.new(4))
21+
assert_equal([nil] * 4, target.values)
22+
end
23+
24+
def test_boolean
25+
values = [true, nil, false]
26+
target = build(Arrow::BooleanArray.new(values))
27+
assert_equal(values, target.values)
28+
end
29+
30+
def test_int8
31+
values = [
32+
-(2 ** 7),
33+
nil,
34+
(2 ** 7) - 1,
35+
]
36+
target = build(Arrow::Int8Array.new(values))
37+
assert_equal(values, target.values)
38+
end
39+
40+
def test_uint8
41+
values = [
42+
0,
43+
nil,
44+
(2 ** 8) - 1,
45+
]
46+
target = build(Arrow::UInt8Array.new(values))
47+
assert_equal(values, target.values)
48+
end
49+
50+
def test_int16
51+
values = [
52+
-(2 ** 15),
53+
nil,
54+
(2 ** 15) - 1,
55+
]
56+
target = build(Arrow::Int16Array.new(values))
57+
assert_equal(values, target.values)
58+
end
59+
60+
def test_uint16
61+
values = [
62+
0,
63+
nil,
64+
(2 ** 16) - 1,
65+
]
66+
target = build(Arrow::UInt16Array.new(values))
67+
assert_equal(values, target.values)
68+
end
69+
70+
def test_int32
71+
values = [
72+
-(2 ** 31),
73+
nil,
74+
(2 ** 31) - 1,
75+
]
76+
target = build(Arrow::Int32Array.new(values))
77+
assert_equal(values, target.values)
78+
end
79+
80+
def test_uint32
81+
values = [
82+
0,
83+
nil,
84+
(2 ** 32) - 1,
85+
]
86+
target = build(Arrow::UInt32Array.new(values))
87+
assert_equal(values, target.values)
88+
end
89+
90+
def test_int64
91+
values = [
92+
-(2 ** 63),
93+
nil,
94+
(2 ** 63) - 1,
95+
]
96+
target = build(Arrow::Int64Array.new(values))
97+
assert_equal(values, target.values)
98+
end
99+
100+
def test_uint64
101+
values = [
102+
0,
103+
nil,
104+
(2 ** 64) - 1,
105+
]
106+
target = build(Arrow::UInt64Array.new(values))
107+
assert_equal(values, target.values)
108+
end
109+
110+
def test_float
111+
values = [
112+
-1.0,
113+
nil,
114+
1.0,
115+
]
116+
target = build(Arrow::FloatArray.new(values))
117+
assert_equal(values, target.values)
118+
end
119+
120+
def test_double
121+
values = [
122+
-1.0,
123+
nil,
124+
1.0,
125+
]
126+
target = build(Arrow::DoubleArray.new(values))
127+
assert_equal(values, target.values)
128+
end
129+
130+
def test_binary
131+
values = [
132+
"\x00".b,
133+
nil,
134+
"\xff".b,
135+
]
136+
target = build(Arrow::BinaryArray.new(values))
137+
assert_equal(values, target.values)
138+
end
139+
140+
def test_string
141+
values = [
142+
"Ruby",
143+
nil,
144+
"\u3042", # U+3042 HIRAGANA LETTER A
145+
]
146+
target = build(Arrow::StringArray.new(values))
147+
assert_equal(values, target.values)
148+
end
149+
150+
def test_date32
151+
values = [
152+
Date.new(1960, 1, 1),
153+
nil,
154+
Date.new(2017, 8, 23),
155+
]
156+
target = build(Arrow::Date32Array.new(values))
157+
assert_equal(values, target.values)
158+
end
159+
160+
def test_date64
161+
values = [
162+
DateTime.new(1960, 1, 1, 2, 9, 30),
163+
nil,
164+
DateTime.new(2017, 8, 23, 14, 57, 2),
165+
]
166+
target = build(Arrow::Date64Array.new(values))
167+
assert_equal(values, target.values)
168+
end
169+
170+
def test_timestamp_second
171+
values = [
172+
Time.parse("1960-01-01T02:09:30Z"),
173+
nil,
174+
Time.parse("2017-08-23T14:57:02Z"),
175+
]
176+
target = build(Arrow::TimestampArray.new(:second, values))
177+
assert_equal(values, target.values)
178+
end
179+
180+
def test_timestamp_milli
181+
values = [
182+
Time.parse("1960-01-01T02:09:30.123Z"),
183+
nil,
184+
Time.parse("2017-08-23T14:57:02.987Z"),
185+
]
186+
target = build(Arrow::TimestampArray.new(:milli, values))
187+
assert_equal(values, target.values)
188+
end
189+
190+
def test_timestamp_micro
191+
values = [
192+
Time.parse("1960-01-01T02:09:30.123456Z"),
193+
nil,
194+
Time.parse("2017-08-23T14:57:02.987654Z"),
195+
]
196+
target = build(Arrow::TimestampArray.new(:micro, values))
197+
assert_equal(values, target.values)
198+
end
199+
200+
def test_timestamp_nano
201+
values = [
202+
Time.parse("1960-01-01T02:09:30.123456789Z"),
203+
nil,
204+
Time.parse("2017-08-23T14:57:02.987654321Z"),
205+
]
206+
target = build(Arrow::TimestampArray.new(:nano, values))
207+
assert_equal(values, target.values)
208+
end
209+
210+
def test_time32_second
211+
unit = Arrow::TimeUnit::SECOND
212+
values = [
213+
Arrow::Time.new(unit, 60 * 10), # 00:10:00
214+
nil,
215+
Arrow::Time.new(unit, 60 * 60 * 2 + 9), # 02:00:09
216+
]
217+
target = build(Arrow::Time32Array.new(unit, values))
218+
assert_equal(values, target.values)
219+
end
220+
221+
def test_time32_milli
222+
unit = Arrow::TimeUnit::MILLI
223+
values = [
224+
Arrow::Time.new(unit, (60 * 10) * 1000 + 123), # 00:10:00.123
225+
nil,
226+
Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1000 + 987), # 02:00:09.987
227+
]
228+
target = build(Arrow::Time32Array.new(unit, values))
229+
assert_equal(values, target.values)
230+
end
231+
232+
def test_time64_micro
233+
unit = Arrow::TimeUnit::MICRO
234+
values = [
235+
# 00:10:00.123456
236+
Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456),
237+
nil,
238+
# 02:00:09.987654
239+
Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000 + 987_654),
240+
]
241+
target = build(Arrow::Time64Array.new(unit, values))
242+
assert_equal(values, target.values)
243+
end
244+
245+
def test_time64_nano
246+
unit = Arrow::TimeUnit::NANO
247+
values = [
248+
# 00:10:00.123456789
249+
Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789),
250+
nil,
251+
# 02:00:09.987654321
252+
Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000_000 + 987_654_321),
253+
]
254+
target = build(Arrow::Time64Array.new(unit, values))
255+
assert_equal(values, target.values)
256+
end
257+
258+
def test_decimal128
259+
values = [
260+
BigDecimal("92.92"),
261+
nil,
262+
BigDecimal("29.29"),
263+
]
264+
data_type = Arrow::Decimal128DataType.new(8, 2)
265+
target = build(Arrow::Decimal128Array.new(data_type, values))
266+
assert_equal(values, target.values)
267+
end
268+
269+
def test_decimal256
270+
values = [
271+
BigDecimal("92.92"),
272+
nil,
273+
BigDecimal("29.29"),
274+
]
275+
data_type = Arrow::Decimal256DataType.new(38, 2)
276+
target = build(Arrow::Decimal256Array.new(data_type, values))
277+
assert_equal(values, target.values)
278+
end
279+
end
280+
281+
class ValuesArrayDictionaryArrayTest < Test::Unit::TestCase
282+
include ValuesDictionaryArrayTests
283+
284+
def build(values)
285+
values.dictionary_encode
286+
end
287+
end
288+
289+
class ValuesChunkedArrayDictionaryArrayTest < Test::Unit::TestCase
290+
include ValuesDictionaryArrayTests
291+
292+
def build(values)
293+
Arrow::ChunkedArray.new([values.dictionary_encode])
294+
end
295+
end

0 commit comments

Comments
 (0)