File: test-table.rb

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites:
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (365 lines) | stat: -rw-r--r-- 11,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
88
89
90
91
92
93
94
95
96
97
98
99
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

class TestTable < Test::Unit::TestCase
  include Helper::Buildable
  include Helper::Omittable

  sub_test_case(".new") do
    def setup
      @fields = [
        Arrow::Field.new("visible", Arrow::BooleanDataType.new),
        Arrow::Field.new("valid", Arrow::BooleanDataType.new),
      ]
      @schema = Arrow::Schema.new(@fields)
    end

    def dump_table(table)
      table.n_columns.times.collect do |i|
        field = table.schema.get_field(i)
        chunked_array = table.get_column_data(i)
        values = []
        chunked_array.chunks.each do |chunk|
          chunk.length.times do |j|
            values << chunk.get_value(j)
          end
        end
        [
          field.name,
          values,
        ]
      end
    end

    def test_arrays
      require_gi_bindings(3, 3, 1)
      arrays = [
        build_boolean_array([true]),
        build_boolean_array([false]),
      ]
      table = Arrow::Table.new(@schema, arrays)
      assert_equal([
                     ["visible", [true]],
                     ["valid", [false]],
                   ],
                   dump_table(table))
    end

    def test_chunked_arrays
      require_gi_bindings(3, 3, 1)
      arrays = [
        Arrow::ChunkedArray.new([build_boolean_array([true]),
                                 build_boolean_array([false])]),
        Arrow::ChunkedArray.new([build_boolean_array([false]),
                                 build_boolean_array([true])]),
      ]
      table = Arrow::Table.new(@schema, arrays)
      assert_equal([
                     ["visible", [true, false]],
                     ["valid", [false, true]],
                   ],
                   dump_table(table))
    end

    def test_record_batches
      require_gi_bindings(3, 3, 1)
      record_batches = [
        build_record_batch({
                             "visible" => build_boolean_array([true]),
                             "valid" => build_boolean_array([false])
                           }),
        build_record_batch({
                             "visible" => build_boolean_array([false]),
                             "valid" => build_boolean_array([true])
                           }),
      ]
      table = Arrow::Table.new(@schema, record_batches)

      assert_equal([
                     ["visible", [true, false]],
                     ["valid", [false, true]],
                   ],
                   dump_table(table))
    end
  end

  sub_test_case("instance methods") do
    def setup
      @fields = [
        Arrow::Field.new("visible", Arrow::BooleanDataType.new),
        Arrow::Field.new("valid", Arrow::BooleanDataType.new),
      ]
      @schema = Arrow::Schema.new(@fields)
      @columns = [
        build_boolean_array([true]),
        build_boolean_array([false]),
      ]
      @table = Arrow::Table.new(@schema, @columns)
    end

    def test_equal
      other_table = Arrow::Table.new(@schema, @columns)
      assert_equal(@table, other_table)
    end

    def test_equal_metadata
      other_table = Arrow::Table.new(@schema, @columns)
      assert do
        @table.equal_metadata(other_table, true)
      end
    end

    def test_schema
      assert_equal(["visible", "valid"],
                   @table.schema.fields.collect(&:name))
    end

    def test_column_data
      assert_equal([
                     Arrow::ChunkedArray.new([build_boolean_array([true])]),
                     Arrow::ChunkedArray.new([build_boolean_array([false])]),
                   ],
                   [
                     @table.get_column_data(0),
                     @table.get_column_data(-1),
                   ])
    end

    def test_n_columns
      assert_equal(2, @table.n_columns)
    end

    def test_n_rows
      assert_equal(1, @table.n_rows)
    end

    def test_add_column
      field = Arrow::Field.new("added", Arrow::BooleanDataType.new)
      chunked_array = Arrow::ChunkedArray.new([build_boolean_array([true])])
      new_table = @table.add_column(1, field, chunked_array)
      assert_equal(["visible", "added", "valid"],
                   new_table.schema.fields.collect(&:name))
    end

    def test_remove_column
      new_table = @table.remove_column(0)
      assert_equal(["valid"],
                   new_table.schema.fields.collect(&:name))
    end

    def test_replace_column
      field = Arrow::Field.new("added", Arrow::BooleanDataType.new)
      chunked_array = Arrow::ChunkedArray.new([build_boolean_array([true])])
      new_table = @table.replace_column(0, field, chunked_array)
      assert_equal(["added", "valid"],
                   new_table.schema.fields.collect(&:name))
    end

    def test_to_s
      table = build_table("valid" => build_boolean_array([true, false, true]))
      assert_equal(<<-TABLE, table.to_s)
valid: bool
----
valid:
  [
    [
      true,
      false,
      true
    ]
  ]
      TABLE
    end

    sub_test_case("#concatenate") do
      def test_without_options
        table = build_table("visible" =>
                            build_boolean_array([true, false, true, false]))
        table1 = build_table("visible" => build_boolean_array([true]))
        table2 = build_table("visible" => build_boolean_array([false, true]))
        table3 = build_table("visible" => build_boolean_array([false]))
        assert_equal(table, table1.concatenate([table2, table3]))
      end

      def test_with_options
        options = Arrow::TableConcatenateOptions.new
        options.unify_schemas = true
        table = build_table("a" => build_int32_array([1, nil, 3]),
                            "b" => build_int32_array([10, nil, 30]),
                            "c" => build_int32_array([nil, 200, nil]))
        table1 = build_table("a" => build_int32_array([1]),
                             "b" => build_int32_array([10]))
        table2 = build_table("c" => build_int32_array([200]))
        table3 = build_table("a" => build_int32_array([3]),
                             "b" => build_int32_array([30]))
        assert_equal(table, table1.concatenate([table2, table3], options))
      end
    end

    sub_test_case("#slice") do
      test("offset: positive") do
        visibles = [true, false, true]
        table = build_table("visible" => build_boolean_array(visibles))
        assert_equal(build_table("visible" => build_boolean_array([false, true])),
                     table.slice(1, 2))
      end

      test("offset: negative") do
        visibles = [true, false, true]
        table = build_table("visible" => build_boolean_array(visibles))
        assert_equal(build_table("visible" => build_boolean_array([false, true])),
                     table.slice(-2, 2))
      end
    end

    def test_combine_chunks
      table = build_table(
        "visible" => Arrow::ChunkedArray::new([build_boolean_array([true, false, true]),
                                               build_boolean_array([false, true]),
                                               build_boolean_array([false])])
      )
      combined_table = table.combine_chunks
      all_values = combined_table.n_columns.times.collect do |i|
        column = combined_table.get_column_data(i)
        column.n_chunks.times.collect do |j|
          column.get_chunk(j).values
        end
      end
      assert_equal([[[true, false, true, false, true, false]]],
                   all_values)
    end

    sub_test_case("#validate") do
      def setup
        @id_field = Arrow::Field.new("id", Arrow::UInt8DataType.new)
        @name_field = Arrow::Field.new("name", Arrow::StringDataType.new)
        @schema = Arrow::Schema.new([@id_field, @name_field])

        @id_array = build_uint_array([1])
        @name_array = build_string_array(["abc"])
        @arrays = [@id_array, @name_array]
      end

      def test_valid
        table = Arrow::Table.new(@schema, @arrays)

        assert do
          table.validate
        end
      end

      def test_invalid
        message = "[table][validate]: Invalid: " +
          "Column 1 named name expected length 1 but got length 2"

        invalid_values = [@id_array, build_string_array(["abc", "def"])]
        table = Arrow::Table.new(@schema, invalid_values)
        error = assert_raise(Arrow::Error::Invalid) do
          table.validate
        end
        assert_equal(message,
                     error.message.lines.first.chomp)
      end
    end

    sub_test_case("#validate_full") do
      def setup
        @id_field = Arrow::Field.new("uint8", Arrow::UInt8DataType.new)
        @name_field = Arrow::Field.new("string", Arrow::StringDataType.new)
        @schema = Arrow::Schema.new([@id_field, @name_field])

        @id_values = build_uint_array([1])
        @valid_name_values = build_string_array(["abc"])

        # U+3042 HIRAGANA LETTER A, U+3044 HIRAGANA LETTER I
        data = "\u3042\u3044".b[0..-2]
        value_offsets = Arrow::Buffer.new([0, data.size].pack("l*"))
        @invalid_name_values = Arrow::StringArray.new(1,
                                                      value_offsets,
                                                      Arrow::Buffer.new(data),
                                                      nil,
                                                      -1)
      end

      def test_valid
        columns = [@id_values, @valid_name_values]
        table = Arrow::Table.new(@schema, columns)

        assert do
          table.validate_full
        end
      end

      def test_invalid
        message = "[table][validate-full]: Invalid: " +
          "Column 1: In chunk 0: Invalid: Invalid UTF8 sequence at string index 0"
        columns = [@id_values, @invalid_name_values]
        table = Arrow::Table.new(@schema, columns)

        error = assert_raise(Arrow::Error::Invalid) do
          table.validate_full
        end
        assert_equal(message,
                     error.message.lines.first.chomp)
      end
    end

    sub_test_case("#write_as_feather") do
      def setup
        super
        @tempfile = Tempfile.open("arrow-table-write-as-feather")
        begin
          yield
        ensure
          @tempfile.close!
        end
      end

      def read_feather
        input = Arrow::MemoryMappedInputStream.new(@tempfile.path)
        reader = Arrow::FeatherFileReader.new(input)
        begin
          yield(reader.read)
        ensure
          input.close
        end
      end

      test("default") do
        output = Arrow::FileOutputStream.new(@tempfile.path, false)
        @table.write_as_feather(output)
        output.close

        read_feather do |read_table|
          assert_equal(@table, read_table)
        end
      end

      test("compression") do
        output = Arrow::FileOutputStream.new(@tempfile.path, false)
        properties = Arrow::FeatherWriteProperties.new
        properties.compression = :zstd
        @table.write_as_feather(output, properties)
        output.close

        read_feather do |read_table|
          assert_equal(@table, read_table)
        end
      end
    end
  end
end