From aebb22f7b66f97a8422f2687982f26252de88b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Mon, 4 Jun 2018 15:06:34 +0200 Subject: [PATCH 1/3] implement getitem on buffer --- python/pyarrow/array.pxi | 2 -- python/pyarrow/io.pxi | 6 ++++-- python/pyarrow/lib.pxd | 1 + python/pyarrow/tests/test_io.py | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 9d5d3885be9a..d98bebc8eed7 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -526,8 +526,6 @@ cdef class Array: raise NotImplemented def __getitem__(self, key): - cdef Py_ssize_t n = len(self) - if PySlice_Check(key): return _normalize_slice(self, key) diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index da9ed518738b..9f54cc7562bc 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -645,8 +645,10 @@ cdef class Buffer: return pyarrow_wrap_buffer(parent_buf) def __getitem__(self, key): - # TODO(wesm): buffer slicing - raise NotImplementedError + return self.getitem(_normalize_index(key, self.size)) + + cdef getitem(self, int64_t i): + return self.buffer.get().data()[i] def equals(self, Buffer other): """ diff --git a/python/pyarrow/lib.pxd b/python/pyarrow/lib.pxd index a7e00b7ed5e4..653cecfe3d27 100644 --- a/python/pyarrow/lib.pxd +++ b/python/pyarrow/lib.pxd @@ -331,6 +331,7 @@ cdef class Buffer: Py_ssize_t strides[1] cdef void init(self, const shared_ptr[CBuffer]& buffer) + cdef getitem(self, int64_t i) cdef class ResizableBuffer(Buffer): diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 5de71d60c8fb..00ca4fdacbff 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -297,6 +297,20 @@ def ne(a, b): eq(buf2, buf5) +def test_buffer_getitem(): + data = b'some data!' + buf = pa.py_buffer(data) + + for ix in [0, 3, -1]: + assert buf[ix] == data[ix] + + with pytest.raises(IndexError): + buf[10] + + with pytest.raises(IndexError): + buf[-11] + + def test_buffer_hashing(): # Buffers are unhashable with pytest.raises(TypeError, match="unhashable"): From 63d4dc34e084c628b0c805d60fdf6e4298047dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Mon, 4 Jun 2018 16:08:40 +0200 Subject: [PATCH 2/3] buffer slicing --- cpp/src/arrow/buffer.h | 6 +++++ python/pyarrow/includes/libarrow.pxd | 5 ++++ python/pyarrow/io.pxi | 31 ++++++++++++++++++++++++ python/pyarrow/tests/test_io.py | 36 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h index 06160d7d4384..e79fc88b8b92 100644 --- a/cpp/src/arrow/buffer.h +++ b/cpp/src/arrow/buffer.h @@ -153,6 +153,12 @@ static inline std::shared_ptr SliceBuffer(const std::shared_ptr& return std::make_shared(buffer, offset, length); } +static inline std::shared_ptr SliceBuffer(const std::shared_ptr& buffer, + const int64_t offset) { + int64_t length = buffer->size() - offset; + return SliceBuffer(buffer, offset, length); +} + /// Construct a mutable buffer slice. If the parent buffer is not mutable, this /// will abort in debug builds ARROW_EXPORT diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index b2a86d7e2eb9..4c67256de74b 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -201,6 +201,11 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: c_bool is_mutable() const c_bool Equals(const CBuffer& other) + shared_ptr[CBuffer] SliceBuffer(const shared_ptr[CBuffer]& buffer, + int64_t offset, int64_t length) + shared_ptr[CBuffer] SliceBuffer(const shared_ptr[CBuffer]& buffer, + int64_t offset) + cdef cppclass CMutableBuffer" arrow::MutableBuffer"(CBuffer): CMutableBuffer(const uint8_t* data, int64_t size) diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index 9f54cc7562bc..7506d638ce2b 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -645,11 +645,42 @@ cdef class Buffer: return pyarrow_wrap_buffer(parent_buf) def __getitem__(self, key): + if PySlice_Check(key): + return _normalize_slice(self, key) + return self.getitem(_normalize_index(key, self.size)) cdef getitem(self, int64_t i): return self.buffer.get().data()[i] + def slice(self, offset=0, length=None): + """ + Compute slice of this buffer + + Parameters + ---------- + offset : int, default 0 + Offset from start of buffer to slice + length : int, default None + Length of slice (default is until end of Buffer starting from + offset) + + Returns + ------- + sliced : Buffer + """ + cdef shared_ptr[CBuffer] result + + if offset < 0: + raise IndexError('Offset must be non-negative') + + if length is None: + result = SliceBuffer(self.buffer, offset) + else: + result = SliceBuffer(self.buffer, offset, max(length, 0)) + + return pyarrow_wrap_buffer(result) + def equals(self, Buffer other): """ Determine if two buffers contain exactly the same data diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 00ca4fdacbff..f46e2a31ffef 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -311,6 +311,42 @@ def test_buffer_getitem(): buf[-11] +def test_buffer_slicing(): + data = b'some data!' + buf = pa.py_buffer(data) + + sliced = buf.slice(2) + expected = pa.py_buffer(b'me data!') + assert sliced.equals(expected) + + sliced2 = buf.slice(2, 4) + expected2 = pa.py_buffer(b'me d') + assert sliced2.equals(expected2) + + # 0 offset + assert buf.slice(0).equals(buf) + + # Slice past end of buffer + assert len(buf.slice(len(buf))) == 0 + + with pytest.raises(IndexError): + buf.slice(-1) + + # Test slice notation + assert buf[2:].equals(buf.slice(2)) + assert buf[2:5].equals(buf.slice(2, 3)) + assert buf[-5:].equals(buf.slice(len(buf) - 5)) + with pytest.raises(IndexError): + buf[::-1] + with pytest.raises(IndexError): + buf[::2] + + n = len(buf) + for start in range(-n * 2, n * 2): + for stop in range(-n * 2, n * 2): + assert buf[start:stop].to_pybytes() == buf.to_pybytes()[start:stop] + + def test_buffer_hashing(): # Buffers are unhashable with pytest.raises(TypeError, match="unhashable"): From c828c05e68b01b4833910c3a4d3ff4ded13f1b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Mon, 4 Jun 2018 16:21:39 +0200 Subject: [PATCH 3/3] make test py2 compatible --- python/pyarrow/tests/test_io.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index f46e2a31ffef..0b67d176884b 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -298,17 +298,18 @@ def ne(a, b): def test_buffer_getitem(): - data = b'some data!' + data = bytearray(b'some data!') buf = pa.py_buffer(data) - for ix in [0, 3, -1]: + n = len(data) + for ix in range(-n, n - 1): assert buf[ix] == data[ix] with pytest.raises(IndexError): - buf[10] + buf[n] with pytest.raises(IndexError): - buf[-11] + buf[-n - 1] def test_buffer_slicing():