GH-50390: [Ruby] Add int/float array builder for red-arrow-format#50391
Conversation
|
|
There was a problem hiding this comment.
Pull request overview
This PR adds a shared “builder” path for Ruby primitive arrays in red-arrow-format by introducing ArrowFormat::PrimitiveArray#build_data, reducing duplicated construction logic across int/float arrays and consolidating primitive-array tests.
Changes:
- Add
PrimitiveArray#build_dataand updatePrimitiveArray#initializeto support constructing from Ruby arrays for numeric primitives. - Add
pack_templateto numeric type classes to drive binary packing for the new builder. - Consolidate boolean and numeric primitive array tests into
test-primitive-array.rb(removing the standalone boolean test file).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| ruby/red-arrow-format/test/test-primitive-array.rb | Adds consolidated coverage for boolean + numeric primitive array roundtrips via .new(values).to_a. |
| ruby/red-arrow-format/test/test-boolean-array.rb | Removes now-redundant boolean-only test file. |
| ruby/red-arrow-format/lib/arrow-format/type.rb | Introduces pack_template for numeric types to support the new builder’s packing. |
| ruby/red-arrow-format/lib/arrow-format/array.rb | Implements shared primitive build/initialization logic and removes redundant subclass initializers. |
| def initialize(*args) | ||
| if args.size == 1 | ||
| args = build_data(args[0]) | ||
| end | ||
| n_args = args.size | ||
| if args.size != 3 | ||
| message = "wrong number of arguments (given #{n_args}, expected 1 or 3)" | ||
| raise ArgumentError, message | ||
| end | ||
| size, validity_buffer, values_buffer = args | ||
| super(self.class.type, size, validity_buffer) | ||
| @values_buffer = values_buffer | ||
| end |
| def pack_template | ||
| "s" | ||
| end |
667033a to
3c43272
Compare
3c43272 to
f548db1
Compare
| def initialize(*args) | ||
| n_args = args.size | ||
| if self.class.respond_to?(:type) | ||
| type = self.class.type | ||
| expected_n_args = "1 or 3" | ||
| else | ||
| type = args.shift | ||
| expected_n_args = "2 or 4" | ||
| end | ||
| args = build_data(args[0]) if args.size == 1 | ||
| if args.size != 3 | ||
| message = | ||
| "wrong number of arguments (given #{n_args}, #{expected_n_args})" | ||
| raise ArgumentError, message | ||
| end | ||
| size, validity_buffer, values_buffer = args | ||
| super(type, size, validity_buffer) | ||
| @values_buffer = values_buffer | ||
| end |
| def build_data(data) | ||
| n = 0 | ||
| validity_buffer_builder = nil | ||
| buffer = +"".b | ||
| pack_template = self.class.type.pack_template | ||
| data.each_with_index do |value, i| |
| def pack_template | ||
| "s" | ||
| end | ||
|
|
||
| def build_array(...) | ||
| Int16Array.new(...) | ||
| end |
| def test_float32 | ||
| values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] | ||
| assert_equal(values, | ||
| ArrowFormat::Float32Array.new(values).to_a) | ||
| end | ||
|
|
||
| def test_float64 | ||
| values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] | ||
| assert_equal(values, | ||
| ArrowFormat::Float64Array.new(values).to_a) | ||
| end |
f548db1 to
2e51534
Compare
|
+1 |
|
After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit dd69699. There were no benchmark performance regressions. 🎉 The full Conbench report has more details. It also includes information about 4 possible false positives for unstable benchmarks that are known to sometimes produce them. |
Rationale for this change
Int array and float array use similar structure. We can share common code for builder API for them.
What changes are included in this PR?
Add
ArrowFormat::PrimitiveArray#build_data.Are these changes tested?
Yes.
Are there any user-facing changes?
Yes.