You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "otegami (via GitHub)" <gi...@apache.org> on 2026/08/12 11:39:05 UTC

[PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

otegami opened a new pull request, #50854:
URL: https://github.com/apache/arrow/pull/50854

   ### Rationale for this change
   
   Building a fixed size binary Arrow array from Ruby objects is convenient.
   
   ### What changes are included in this PR?
   
   Accept:
   
   * `ArrowFormat::FixedSizeBinaryArray.new(byte_width, values)`
   * `ArrowFormat::FixedSizeBinaryArray.new(type, values)`
   
   `ArrowFormat::FixedSizeBinaryType.try_convert` is added for the byte width shorthand, in the same way as `ArrowFormat::Time32Array.new(unit, values)`. The existing 4 arguments form is kept.
   
   Decimal arrays are out of scope. `ArrowFormat::DecimalArray#to_a` returns `BigDecimal`, so building one from Ruby objects needs a conversion between `BigDecimal` and two's complement. I would like to open a separate issue for it.
   
   ### Are these changes tested?
   
   Yes.
   
   ### Are there any user-facing changes?
   
   Yes.
   
   * GitHub Issue: #50832


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "otegami (via GitHub)" <gi...@apache.org>.
otegami commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3774458356


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -875,6 +889,44 @@ def to_a
       end

Review Comment:
   This is an existing problem. This PR doesn't change #to_a.
   `#[]` and `#each` are missing too. I'll open a follow-up issue for them.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3841878076


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)
+        type = FixedSizeBinaryType.try_convert(type) || type
+      end
+      if args.size == 1
+        args = build_data(args.first, type)

Review Comment:
   Could you use existing style?
   
   ```suggestion
           args = build_data(args[0], type)
   ```



##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)
+        type = FixedSizeBinaryType.try_convert(type) || type
+      end
+      if args.size == 1
+        args = build_data(args.first, type)
+      elsif args.size != 3

Review Comment:
   Could you use existing style?
   
   ```suggestion
         args = build_data(args.first, type) if args.size == 1
         if args.size != 3
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3766065430


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -875,6 +889,44 @@ def to_a
       end

Review Comment:
   `FixedSizeBinaryArray#to_a` ignores `@offset`, so `slice(...)` will return incorrect values when converted to Ruby objects. Other array implementations apply `@offset` when reading from buffers.



##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)

Review Comment:
   `FixedSizeBinaryArray` inherits `ArrowFormat::Array#==`, which only compares size/validity and can report two arrays as equal even when their values differ (especially when there are no nulls). This can produce incorrect equality results and the current tests don’t cover the “different values, same null pattern” case.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_width, values)` [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou merged PR #50854:
URL: https://github.com/apache/arrow/pull/50854


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3837883314


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)
+        type = FixedSizeBinaryType.try_convert(type) || type
+      end
+      if args.size == 1
+        args = build_data(args.first, type)
+      elsif args.size != 3

Review Comment:
   `FixedSizeBinaryArray.new(type, values)` assumes `type` is a `FixedSizeBinaryType` (has `byte_width`). If a caller passes another `ArrowFormat::Type` (or any non-convertible value), this will currently blow up later with `NoMethodError` inside `build_data`/`each_buffer`. It should fail fast with a clear `ArgumentError` after attempting `FixedSizeBinaryType.try_convert`.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3771761442


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -875,6 +889,44 @@ def to_a
       end
       apply_validity(values)
     end
+
+    private
+    def build_data(data, type)
+      n = 0
+      validity_buffer_builder = nil
+
+      values = +"".b
+      byte_width = type.byte_width
+      null_value = "\x00" * byte_width
+
+      data.each_with_index do |value, i|
+        if value.nil?
+          validity_buffer_builder ||= SparseBitmapBuilder.new
+          validity_buffer_builder.unset(i)
+          values.append_as_bytes(null_value)
+        else
+          unless value.bytesize == byte_width
+            message = "value size must be #{byte_width}: #{value.bytesize}"

Review Comment:
   Could you show the actual value for easy to debug?
   
   ```suggestion
               message = "value size must be #{byte_width}: #{value.inspect}"
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "otegami (via GitHub)" <gi...@apache.org>.
otegami commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3774466680


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)

Review Comment:
   `ArrowFormat::Array#==` compares only the type, the size, and the validity structure. `ArrowFormat::BinaryArray` and `ArrowFormat::UTF8Array` behave the same, so this isn't specific to fixed-size binary.
   Comparing values needs `#each`, which is missing. I'll handle it in the same follow-up issue.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "otegami (via GitHub)" <gi...@apache.org>.
otegami commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3774524476


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -875,6 +889,44 @@ def to_a
       end
       apply_validity(values)
     end
+
+    private
+    def build_data(data, type)
+      n = 0
+      validity_buffer_builder = nil
+
+      values = +"".b
+      byte_width = type.byte_width
+      null_value = "\x00" * byte_width
+
+      data.each_with_index do |value, i|
+        if value.nil?
+          validity_buffer_builder ||= SparseBitmapBuilder.new
+          validity_buffer_builder.unset(i)
+          values.append_as_bytes(null_value)
+        else
+          unless value.bytesize == byte_width
+            message = "value size must be #{byte_width}: #{value.bytesize}"

Review Comment:
   fix: https://github.com/apache/arrow/pull/50854/changes#diff-94226bf2a6f57e453bae1425fc7d92410f15de7effc0bc83fefe8e2d3e87ca29R909 Sure. Thank you for reviewing.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-50832: [Ruby] Add `ArrowFormat::FixedSizeBinaryArray.new(byte_widt…h, values)` [arrow]

Posted by "otegami (via GitHub)" <gi...@apache.org>.
otegami commented on code in PR #50854:
URL: https://github.com/apache/arrow/pull/50854#discussion_r3838001250


##########
ruby/red-arrow-format/lib/arrow-format/array.rb:
##########
@@ -852,7 +852,21 @@ def type
   end
 
   class FixedSizeBinaryArray < Array
-    def initialize(type, size, validity_buffer, values_buffer)
+    include BufferAlignable
+
+    def initialize(type, *args)
+      unless type.is_a?(Type)
+        type = FixedSizeBinaryType.try_convert(type) || type
+      end
+      if args.size == 1
+        args = build_data(args.first, type)
+      elsif args.size != 3

Review Comment:
   This matches the existing convention `PrimitiveArray#initialize`, so `Time32Array.new("foo", [1])` fails the same way today. Validating only here would be inconsistent.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org