Files
Edmond AbrahamandSutou Kouhei c68e0aa1fd GH-52021: [Ruby] Add support for sliced arrays in MemoryView (#52286)
### Rationale for this change

MemoryView export of `Arrow::PrimitiveArray` didn't support arrays with non-zero offset (sliced arrays). For example, exporting `Arrow::Int32Array.new([0, 1, 2, 3]).slice(1, 3)` returned wrong values because the data pointer was advanced by `offset` bytes instead of `offset * byte width` bytes.

### What changes are included in this PR?

* Use `offset * bit width / 8` as the byte offset of the data pointer. This supports sliced fixed-width arrays.
* Support sliced boolean arrays only when the offset is byte aligned. MemoryView can't represent a bit offset.
* Implement `available_p` for `Arrow::PrimitiveArray`. It returns `false` for arrays that can't be represented by MemoryView: unsupported types and boolean arrays with not byte aligned offset.

### Are these changes tested?

Ran the complete `test-memory-view.rb` file through the native Ruby/Fiddle interface using Ruby 3.2.3, Fiddle 1.1.8, and Arrow C++/GLib 25.0.1.

The original MemoryView implementation in the 25.0.1 release is identical to the current checkout. The extension was rebuilt with this PR's updated `memory-view.cpp`.

- Revised complete test file: 29 tests, 31 assertions, 0 failures, 0 errors, 0 omissions.
- Both new Boolean cases fail against the previous PR implementation and pass after the revision.
- `git diff --check` passes.

The full repository test suite and a complete build of the 26.0.0-SNAPSHOT dependencies were not run.

### Are there any user-facing changes?

* Sliced fixed-width arrays and byte-aligned sliced boolean arrays can be exported by MemoryView.
* `rb_memory_view_available_p()` returns `false` for unsupported arrays.

### Was AI used for this PR?

AI assisted with the investigation, implementation, regression tests, validation, and PR description.

**PR code and description written by:**

- [ ] Human
- [x] AI

**Reviewed before submission by:**

- [ ] Human
- [x] AI
- [ ] Not reviewed

* GitHub Issue: #52021

Lead-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com>
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
2026-10-09 10:01:12 +09:00
..
2018-05-24 21:13:38 -04:00
2018-05-24 21:13:38 -04:00