kou commented on code in PR #50452:
URL: https://github.com/apache/arrow/pull/50452#discussion_r3555959797
##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -577,6 +587,55 @@ def to_a
end
private
+ def build_data(data)
+ type = self.class.type
+
+ n = 0
+ validity_buffer_builder = nil
+
+ values = +"".b
+ offsets = [0]
+
+ data.each_with_index do |value, i|
+ if value.nil?
+ validity_buffer_builder ||= SparseBitmapBuilder.new
+ validity_buffer_builder.unset(i)
+
+ offsets << values.bytesize
+ else
+ value = value.b if type.encoding == Encoding::ASCII_8BIT
+ value = value.encode(type.encoding)
+
+ values.append_as_bytes(value)
+ offsets << values.bytesize
+ end
+
+ n += 1
+ end
+
+ validity_buffer = validity_buffer_builder&.finish(n)
+
+ size = IO::Buffer.size_of(type.offset_buffer_type)
+ offsets_buffer = IO::Buffer.new(size * offsets.size)
+
+ offsets.each_with_index do |offset, i|
+ offsets_buffer.set_value(type.offset_buffer_type,
+ size * i,
+ offset)
+ end
+
+ pad!(values, buffer_padding_size(values))
+ values.freeze
+
+ values_buffer = IO::Buffer.for(values)
+
+ [
+ n,
+ validity_buffer,
+ offsets_buffer,
+ values_buffer,
+ ]
+ end
Review Comment:
```suggestion
end
```
##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -577,6 +587,55 @@ def to_a
end
private
+ def build_data(data)
+ type = self.class.type
+
+ n = 0
+ validity_buffer_builder = nil
+
+ values = +"".b
+ offsets = [0]
+
+ data.each_with_index do |value, i|
+ if value.nil?
+ validity_buffer_builder ||= SparseBitmapBuilder.new
+ validity_buffer_builder.unset(i)
+
+ offsets << values.bytesize
+ else
+ value = value.b if type.encoding == Encoding::ASCII_8BIT
+ value = value.encode(type.encoding)
+
Review Comment:
We don't need them.
```suggestion
```
##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -577,6 +587,55 @@ def to_a
end
private
+ def build_data(data)
+ type = self.class.type
+
+ n = 0
+ validity_buffer_builder = nil
+
+ values = +"".b
+ offsets = [0]
+
+ data.each_with_index do |value, i|
+ if value.nil?
+ validity_buffer_builder ||= SparseBitmapBuilder.new
+ validity_buffer_builder.unset(i)
+
+ offsets << values.bytesize
+ else
+ value = value.b if type.encoding == Encoding::ASCII_8BIT
+ value = value.encode(type.encoding)
+
+ values.append_as_bytes(value)
+ offsets << values.bytesize
+ end
+
+ n += 1
+ end
+
+ validity_buffer = validity_buffer_builder&.finish(n)
+
+ size = IO::Buffer.size_of(type.offset_buffer_type)
+ offsets_buffer = IO::Buffer.new(size * offsets.size)
+
+ offsets.each_with_index do |offset, i|
+ offsets_buffer.set_value(type.offset_buffer_type,
+ size * i,
+ offset)
+ end
+
+ pad!(values, buffer_padding_size(values))
+ values.freeze
+
Review Comment:
```suggestion
```
##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -577,6 +587,55 @@ def to_a
end
private
+ def build_data(data)
+ type = self.class.type
+
+ n = 0
+ validity_buffer_builder = nil
+
+ values = +"".b
+ offsets = [0]
+
+ data.each_with_index do |value, i|
+ if value.nil?
+ validity_buffer_builder ||= SparseBitmapBuilder.new
+ validity_buffer_builder.unset(i)
+
+ offsets << values.bytesize
+ else
+ value = value.b if type.encoding == Encoding::ASCII_8BIT
+ value = value.encode(type.encoding)
+
+ values.append_as_bytes(value)
+ offsets << values.bytesize
+ end
+
+ n += 1
+ end
+
+ validity_buffer = validity_buffer_builder&.finish(n)
+
+ size = IO::Buffer.size_of(type.offset_buffer_type)
+ offsets_buffer = IO::Buffer.new(size * offsets.size)
+
+ offsets.each_with_index do |offset, i|
+ offsets_buffer.set_value(type.offset_buffer_type,
+ size * i,
+ offset)
+ end
Review Comment:
How about using `pack`?
```suggestion
offsets_data = offsets.pack("#{type.offset_pack_template}*")
pad!(offsets_data, buffer_padding_size(offsets_data))
offsets_data.freeze
offsets_buffer = IO::Buffer.for(offsets_data)
```
##########
ruby/red-arrow-format/test/test-binary-array.rb:
##########
@@ -0,0 +1,88 @@
+# 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 TestBinaryArray < Test::Unit::TestCase
+ [
+ ArrowFormat::BinaryArray,
+ ArrowFormat::LargeBinaryArray,
+ ArrowFormat::UTF8Array,
+ ArrowFormat::LargeUTF8Array,
+ ].each do |array_class|
+ sub_test_case(array_class.name.split("::").last) do
+ setup do
+ @value1 = "Hello"
+ @value2 = "World"
Review Comment:
We want to use binary data not UTF-8 data for `BinaryArray` and
`LargeBinaryArray`.
##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -577,6 +587,55 @@ def to_a
end
private
+ def build_data(data)
+ type = self.class.type
+
+ n = 0
+ validity_buffer_builder = nil
+
+ values = +"".b
+ offsets = [0]
+
+ data.each_with_index do |value, i|
+ if value.nil?
+ validity_buffer_builder ||= SparseBitmapBuilder.new
+ validity_buffer_builder.unset(i)
+
+ offsets << values.bytesize
+ else
+ value = value.b if type.encoding == Encoding::ASCII_8BIT
+ value = value.encode(type.encoding)
+
+ values.append_as_bytes(value)
+ offsets << values.bytesize
+ end
+
+ n += 1
+ end
+
+ validity_buffer = validity_buffer_builder&.finish(n)
+
+ size = IO::Buffer.size_of(type.offset_buffer_type)
Review Comment:
Could you prepend `offset_` for easy to understand?
```suggestion
offset_size = IO::Buffer.size_of(type.offset_buffer_type)
```
##########
ruby/red-arrow-format/test/test-binary-array.rb:
##########
@@ -0,0 +1,88 @@
+# 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 TestBinaryArray < Test::Unit::TestCase
+ [
+ ArrowFormat::BinaryArray,
+ ArrowFormat::LargeBinaryArray,
+ ArrowFormat::UTF8Array,
+ ArrowFormat::LargeUTF8Array,
Review Comment:
Could you create one test file per array?
* `test-binary-array.rb`
* `test-large-binary-array.rb`
* `test-utf8-array.rb`
* `test-large-utf8-array.rb`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]