From f328e434c8c41064c223d83e4af541c7b47f7397 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Fri, 28 Dec 2018 18:03:43 -0800 Subject: [PATCH 01/10] Separate memory allocation from bitmap inversion. --- cpp/src/arrow/array.h | 7 +++- cpp/src/arrow/buffer-test.cc | 18 ++++++++++ cpp/src/arrow/buffer.cc | 7 +++- cpp/src/arrow/buffer.h | 13 +++++++- cpp/src/arrow/compute/kernel.h | 15 ++++++++- cpp/src/arrow/compute/kernels/boolean-test.cc | 3 +- cpp/src/arrow/compute/kernels/boolean.cc | 33 ++++++++++--------- cpp/src/arrow/compute/kernels/cast.cc | 8 +++++ .../arrow/compute/kernels/util-internal.cc | 30 +++++++++++++++++ cpp/src/arrow/compute/kernels/util-internal.h | 23 +++++++++++++ 10 files changed, 136 insertions(+), 21 deletions(-) diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index aead17f133d7..005ede311a79 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -47,6 +47,11 @@ using BufferVector = std::vector>; // to -1 (any negative number will do). When Array::null_count is called the // first time, the null count will be computed. See ARROW-33 constexpr int64_t kUnknownNullCount = -1; +// When applying Kernels to array it might not be possible determine the length +// of array without executing the kernel. This constant is used with ArrayData +// so the structure of a result can still be communicated even if the length +// is not known. +constexpr int64_t kUnknownLength = -1; class MemoryPool; class Status; @@ -88,7 +93,7 @@ class Status; struct ARROW_EXPORT ArrayData { ArrayData() : length(0), null_count(0), offset(0) {} - ArrayData(const std::shared_ptr& type, int64_t length, + ArrayData(const std::shared_ptr& type, int64_t length = kUnknownLength, int64_t null_count = kUnknownNullCount, int64_t offset = 0) : type(type), length(length), null_count(null_count), offset(offset) {} diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc index 7c54e136195f..921d12f61dbc 100644 --- a/cpp/src/arrow/buffer-test.cc +++ b/cpp/src/arrow/buffer-test.cc @@ -24,6 +24,7 @@ #include #include +#include #include #include "arrow/buffer-builder.h" @@ -33,9 +34,26 @@ #include "arrow/test-util.h" using std::string; +using testing::Each; +using testing::Ge; namespace arrow { +TEST(TestAllocate, Bitmap) { + std::shared_ptr new_buffer; + EXPECT_OK(AllocateBitmap(default_memory_pool(), 100, &new_buffer)); + EXPECT_THAT(new_buffer->size(), Ge(13)); +} + +TEST(TestAllocate, EmptyBitmap) { + std::shared_ptr new_buffer; + EXPECT_OK(AllocateEmptyBitmap(default_memory_pool(), 100, &new_buffer)); + EXPECT_THAT(new_buffer->size(), Ge(13)); + EXPECT_THAT( + std::vector(new_buffer->data(), new_buffer->data() + new_buffer->size()), + Each(0)); +} + TEST(TestBuffer, FromStdString) { std::string val = "hello, world"; diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 9534d2b94f3e..28a93a89f989 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -212,9 +212,14 @@ Status AllocateResizableBuffer(const int64_t size, return AllocateResizableBuffer(default_memory_pool(), size, out); } +Status AllocateBitmap(MemoryPool* pool, int64_t length, std::shared_ptr* out) { + RETURN_NOT_OK(AllocateBuffer(pool, BitUtil::BytesForBits(length), out)); + return Status::OK(); +} + Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr* out) { - RETURN_NOT_OK(AllocateBuffer(pool, BitUtil::BytesForBits(length), out)); + RETURN_NOT_OK(AllocateBitmap(pool, length, out)); memset((*out)->mutable_data(), 0, static_cast((*out)->size())); return Status::OK(); } diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h index f0042897b027..e00c314fdd30 100644 --- a/cpp/src/arrow/buffer.h +++ b/cpp/src/arrow/buffer.h @@ -370,7 +370,8 @@ Status AllocateResizableBuffer(const int64_t size, std::shared_ptr* out); -/// \brief Allocate a zero-initialized bitmap buffer from a memory pool +/// \brief Allocate a bitmap buffer from a memory pool +/// no guarantee on values is provided. /// /// \param[in] pool memory pool to allocate memory from /// \param[in] length size in bits of bitmap to allocate @@ -378,6 +379,16 @@ Status AllocateResizableBuffer(const int64_t size, std::unique_ptr* out); + +/// \brief Allocate a zero-initialized bitmap buffer from a memory pool +/// +/// \param[in] pool memory pool to allocate memory from +/// \param[in] length size in bits of bitmap to allocate +/// \param[out] out the resulting buffer (zero-initialized). +/// +/// \return Status message +ARROW_EXPORT Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length, std::shared_ptr* out); diff --git a/cpp/src/arrow/compute/kernel.h b/cpp/src/arrow/compute/kernel.h index 87080b1000d5..9644ec7a9a19 100644 --- a/cpp/src/arrow/compute/kernel.h +++ b/cpp/src/arrow/compute/kernel.h @@ -151,9 +151,22 @@ struct ARROW_EXPORT Datum { }; /// \class UnaryKernel -/// \brief An array-valued function of a single input argument +/// \brief An function of a single input argument. +/// +/// Note to implementors: Try to avoid making kernels that allocate memory if +/// the output size is a determinstic function of the Input Datum's metadata. +/// Instead separate the logic of the kernel and allocations necessary into +/// two different kernels. Some reusable kernels that allocate buffers without +/// and delegate computation to another kernel are available in util-internal.h. class ARROW_EXPORT UnaryKernel : public OpKernel { public: + /// \brief Executes the kernel. + /// + /// \param[out] out The output of the function. Each implementation of this + /// function might assume different things about the existing contents of out + /// (e.g. which buffers are preallocated). In the future it is expected that + /// there will be a more generic mechansim for understanding the necessary + /// contracts. virtual Status Call(FunctionContext* ctx, const Datum& input, Datum* out) = 0; }; diff --git a/cpp/src/arrow/compute/kernels/boolean-test.cc b/cpp/src/arrow/compute/kernels/boolean-test.cc index 24b3c68aa1cf..8299488a4de7 100644 --- a/cpp/src/arrow/compute/kernels/boolean-test.cc +++ b/cpp/src/arrow/compute/kernels/boolean-test.cc @@ -104,6 +104,7 @@ TEST_F(TestBooleanKernel, Invert) { auto type = boolean(); auto a1 = _MakeArray(type, values1, {}); + auto a1_copy = _MakeArray(type, values1, {}); auto a2 = _MakeArray(type, values2, {}); // Plain array @@ -111,7 +112,7 @@ TEST_F(TestBooleanKernel, Invert) { ASSERT_OK(Invert(&this->ctx_, a1, &result)); ASSERT_EQ(Datum::ARRAY, result.kind()); std::shared_ptr result_array = result.make_array(); - ASSERT_TRUE(result_array->Equals(a2)); + ASSERT_TRUE(result_array->Equals(a2)) << result_array->ToString() << a2->ToString(); // Array with offset ASSERT_OK(Invert(&this->ctx_, a1->Slice(1), &result)); diff --git a/cpp/src/arrow/compute/kernels/boolean.cc b/cpp/src/arrow/compute/kernels/boolean.cc index e1aa1669572d..91a0e9344305 100644 --- a/cpp/src/arrow/compute/kernels/boolean.cc +++ b/cpp/src/arrow/compute/kernels/boolean.cc @@ -43,34 +43,35 @@ namespace compute { class InvertKernel : public UnaryKernel { Status Call(FunctionContext* ctx, const Datum& input, Datum* out) override { DCHECK_EQ(Datum::ARRAY, input.kind()); + constexpr int64_t kZeroDestOffset = 0; const ArrayData& in_data = *input.array(); - ArrayData* result; - - out->value = ArrayData::Make(boolean(), in_data.length); - result = out->array().get(); + std::shared_ptr result = out->array(); + result->type = boolean(); - // Allocate or copy bitmap + // Handle validity bitmap result->null_count = in_data.null_count; - std::shared_ptr validity_bitmap = in_data.buffers[0]; + const std::shared_ptr& validity_bitmap = in_data.buffers[0]; if (in_data.offset != 0) { - RETURN_NOT_OK(CopyBitmap(ctx->memory_pool(), validity_bitmap->data(), - in_data.offset, in_data.length, &validity_bitmap)); + DCHECK_LE(BitUtil::BytesForBits(in_data.length), validity_bitmap->size()); + CopyBitmap(validity_bitmap->data(), in_data.offset, in_data.length, + result->buffers[0]->mutable_data(), kZeroDestOffset); + } else { + result->buffers[0] = validity_bitmap; } - result->buffers.push_back(validity_bitmap); - - // Allocate output data buffer - std::shared_ptr data_buffer; - RETURN_NOT_OK(InvertBitmap(ctx->memory_pool(), in_data.buffers[1]->data(), - in_data.offset, in_data.length, &data_buffer)); - result->buffers.push_back(data_buffer); + // Handle output data buffer + const std::shared_ptr& data_buffer = in_data.buffers[1]; + DCHECK_LE(BitUtil::BytesForBits(in_data.length), data_buffer->size()); + InvertBitmap(data_buffer->data(), in_data.offset, in_data.length, + result->buffers[1]->mutable_data(), kZeroDestOffset); return Status::OK(); } }; Status Invert(FunctionContext* ctx, const Datum& value, Datum* out) { - InvertKernel kernel; + detail::PrimitiveAllocatingUnaryKernel kernel( + std::unique_ptr(new InvertKernel())); std::vector result; RETURN_NOT_OK(detail::InvokeUnaryArrayKernel(ctx, &kernel, value, &result)); diff --git a/cpp/src/arrow/compute/kernels/cast.cc b/cpp/src/arrow/compute/kernels/cast.cc index 092aebc8c3d2..6bd8a120cbe3 100644 --- a/cpp/src/arrow/compute/kernels/cast.cc +++ b/cpp/src/arrow/compute/kernels/cast.cc @@ -725,6 +725,10 @@ class ListCastKernel : public UnaryKernel { return Status::OK(); } + Status ComputeOutputShape(const Datum& input, Datum* output_shape) { + return Status::NotImplemented("Implementation pending"); + } + private: std::unique_ptr child_caster_; std::shared_ptr out_type_; @@ -1194,6 +1198,10 @@ class CastKernel : public UnaryKernel { return Status::OK(); } + Status ComputeOutputShape(const Datum& input, Datum* output_shape) { + return Status::NotImplemented("TODO(ARROW-1185)"); + } + private: CastOptions options_; CastFunction func_; diff --git a/cpp/src/arrow/compute/kernels/util-internal.cc b/cpp/src/arrow/compute/kernels/util-internal.cc index 81fafcba6167..282214e3c56d 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.cc +++ b/cpp/src/arrow/compute/kernels/util-internal.cc @@ -27,6 +27,7 @@ #include "arrow/table.h" #include "arrow/util/logging.h" +#include "arrow/compute/context.h" #include "arrow/compute/kernel.h" namespace arrow { @@ -162,6 +163,35 @@ Datum WrapDatumsLike(const Datum& value, const std::vector& datums) { } } +PrimitiveAllocatingUnaryKernel::PrimitiveAllocatingUnaryKernel( + std::unique_ptr delegate) + : delegate_(std::move(delegate)) {} + +Status PrimitiveAllocatingUnaryKernel::Call(FunctionContext* ctx, const Datum& input, + Datum* out) { + std::vector> data_buffers; + const ArrayData& in_data = *input.array(); + MemoryPool* pool = ctx->memory_pool(); + + // Handle the validity buffer. + if (in_data.offset == 0) { + // Validity bitmap will be zero copied + data_buffers.emplace_back(); + } else { + std::shared_ptr buffer; + RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); + data_buffers.push_back(buffer); + } + // Allocate the boolean value buffer. + std::shared_ptr buffer; + RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); + data_buffers.push_back(buffer); + + out->value = ArrayData::Make(null(), in_data.length, data_buffers); + + return delegate_->Call(ctx, input, out); +} + } // namespace detail } // namespace compute } // namespace arrow diff --git a/cpp/src/arrow/compute/kernels/util-internal.h b/cpp/src/arrow/compute/kernels/util-internal.h index d71e36d9c42b..aa1488361ea7 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.h +++ b/cpp/src/arrow/compute/kernels/util-internal.h @@ -44,6 +44,9 @@ static inline void ZeroCopyData(const ArrayData& input, ArrayData* output) { namespace detail { +/// \brief Invoke the kernel on value using the ctx and store results in outputs. +/// +/// \param[out] outputs One ArrayData datum for each ArrayData available in value. ARROW_EXPORT Status InvokeUnaryArrayKernel(FunctionContext* ctx, UnaryKernel* kernel, const Datum& value, std::vector* outputs); @@ -63,6 +66,26 @@ Datum WrapArraysLike(const Datum& value, ARROW_EXPORT Datum WrapDatumsLike(const Datum& value, const std::vector& datums); +/// \brief Buffer used to preallocate outputs for primitive types. +class PrimitiveAllocatingUnaryKernel : public UnaryKernel { + public: + explicit PrimitiveAllocatingUnaryKernel(std::unique_ptr delegate); + /// \brief Sets out to be of type ArrayData with the necessary + /// data buffers prepopulated. + /// + /// This method does not populate types on arrays and sets type to null. + /// + /// The current implementation only supports primitive boolean outputs and + /// assumes validity bitmaps that are not sliced will be zero copied (i.e. + /// no allocation happens for them). + /// + /// TODO(ARROW-1896): Make this generic enough to support casts. + Status Call(FunctionContext* ctx, const Datum& input, Datum* out) override; + + private: + std::unique_ptr delegate_; +}; + } // namespace detail } // namespace compute From 206187f317c9b112c9972c0d365c5c667945f879 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 00:31:43 -0800 Subject: [PATCH 02/10] revert kUnknownLength --- cpp/src/arrow/array.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index 005ede311a79..aead17f133d7 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -47,11 +47,6 @@ using BufferVector = std::vector>; // to -1 (any negative number will do). When Array::null_count is called the // first time, the null count will be computed. See ARROW-33 constexpr int64_t kUnknownNullCount = -1; -// When applying Kernels to array it might not be possible determine the length -// of array without executing the kernel. This constant is used with ArrayData -// so the structure of a result can still be communicated even if the length -// is not known. -constexpr int64_t kUnknownLength = -1; class MemoryPool; class Status; @@ -93,7 +88,7 @@ class Status; struct ARROW_EXPORT ArrayData { ArrayData() : length(0), null_count(0), offset(0) {} - ArrayData(const std::shared_ptr& type, int64_t length = kUnknownLength, + ArrayData(const std::shared_ptr& type, int64_t length, int64_t null_count = kUnknownNullCount, int64_t offset = 0) : type(type), length(length), null_count(null_count), offset(offset) {} From 90570aabe005eda55ffb55115ceb7c23c9763cc5 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 00:33:52 -0800 Subject: [PATCH 03/10] revert random boolean test --- cpp/src/arrow/compute/kernels/boolean-test.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/boolean-test.cc b/cpp/src/arrow/compute/kernels/boolean-test.cc index 8299488a4de7..24b3c68aa1cf 100644 --- a/cpp/src/arrow/compute/kernels/boolean-test.cc +++ b/cpp/src/arrow/compute/kernels/boolean-test.cc @@ -104,7 +104,6 @@ TEST_F(TestBooleanKernel, Invert) { auto type = boolean(); auto a1 = _MakeArray(type, values1, {}); - auto a1_copy = _MakeArray(type, values1, {}); auto a2 = _MakeArray(type, values2, {}); // Plain array @@ -112,7 +111,7 @@ TEST_F(TestBooleanKernel, Invert) { ASSERT_OK(Invert(&this->ctx_, a1, &result)); ASSERT_EQ(Datum::ARRAY, result.kind()); std::shared_ptr result_array = result.make_array(); - ASSERT_TRUE(result_array->Equals(a2)) << result_array->ToString() << a2->ToString(); + ASSERT_TRUE(result_array->Equals(a2)); // Array with offset ASSERT_OK(Invert(&this->ctx_, a1->Slice(1), &result)); From 931195fe1e52536b1bbd6a49fd4e5c789e7a7f19 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 00:37:00 -0800 Subject: [PATCH 04/10] remove ComputeOutputShape --- cpp/src/arrow/compute/kernels/cast.cc | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/cast.cc b/cpp/src/arrow/compute/kernels/cast.cc index 6bd8a120cbe3..092aebc8c3d2 100644 --- a/cpp/src/arrow/compute/kernels/cast.cc +++ b/cpp/src/arrow/compute/kernels/cast.cc @@ -725,10 +725,6 @@ class ListCastKernel : public UnaryKernel { return Status::OK(); } - Status ComputeOutputShape(const Datum& input, Datum* output_shape) { - return Status::NotImplemented("Implementation pending"); - } - private: std::unique_ptr child_caster_; std::shared_ptr out_type_; @@ -1198,10 +1194,6 @@ class CastKernel : public UnaryKernel { return Status::OK(); } - Status ComputeOutputShape(const Datum& input, Datum* output_shape) { - return Status::NotImplemented("TODO(ARROW-1185)"); - } - private: CastOptions options_; CastFunction func_; From 5bc4f8934d429a35ed5199946c596393180278ae Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 00:46:58 -0800 Subject: [PATCH 05/10] include --- cpp/src/arrow/compute/kernels/util-internal.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/src/arrow/compute/kernels/util-internal.cc b/cpp/src/arrow/compute/kernels/util-internal.cc index 282214e3c56d..d75907078463 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.cc +++ b/cpp/src/arrow/compute/kernels/util-internal.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "arrow/array.h" From 26367e672e779f2b32ea91b28b6e893a92ebba17 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 01:11:07 -0800 Subject: [PATCH 06/10] live without gmock for now --- cpp/src/arrow/buffer-test.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc index 921d12f61dbc..b63ac401bafd 100644 --- a/cpp/src/arrow/buffer-test.cc +++ b/cpp/src/arrow/buffer-test.cc @@ -24,7 +24,6 @@ #include #include -#include #include #include "arrow/buffer-builder.h" @@ -34,24 +33,22 @@ #include "arrow/test-util.h" using std::string; -using testing::Each; -using testing::Ge; namespace arrow { TEST(TestAllocate, Bitmap) { std::shared_ptr new_buffer; EXPECT_OK(AllocateBitmap(default_memory_pool(), 100, &new_buffer)); - EXPECT_THAT(new_buffer->size(), Ge(13)); + EXPECT_GE(new_buffer->size(), 13); } TEST(TestAllocate, EmptyBitmap) { std::shared_ptr new_buffer; EXPECT_OK(AllocateEmptyBitmap(default_memory_pool(), 100, &new_buffer)); - EXPECT_THAT(new_buffer->size(), Ge(13)); - EXPECT_THAT( + EXPECT_GE(new_buffer->size(), 13); + EXPECT_EQ( std::vector(new_buffer->data(), new_buffer->data() + new_buffer->size()), - Each(0)); + std::vector(new_buffer->size(), 0)); } TEST(TestBuffer, FromStdString) { From f907cdb5525f278388bf1884f663981287b0cd91 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Thu, 10 Jan 2019 07:44:46 -0800 Subject: [PATCH 07/10] fix valgrind --- cpp/src/arrow/compute/kernels/util-internal.cc | 7 +++++++ cpp/src/arrow/util/bit-util.cc | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/util-internal.cc b/cpp/src/arrow/compute/kernels/util-internal.cc index d75907078463..f85ff3193ba0 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.cc +++ b/cpp/src/arrow/compute/kernels/util-internal.cc @@ -181,11 +181,18 @@ Status PrimitiveAllocatingUnaryKernel::Call(FunctionContext* ctx, const Datum& i } else { std::shared_ptr buffer; RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); + // Make the last byte deterministic because underlying bit functions + // try to restore it. + *(buffer->mutable_data() + (buffer->size() - 1)) = 0; data_buffers.push_back(buffer); } // Allocate the boolean value buffer. std::shared_ptr buffer; RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); + // Make the last byte deterministic because underlying bit functions + // try to restore it. + *(buffer->mutable_data() + (buffer->size() - 1)) = 0; + data_buffers.push_back(buffer); out->value = ArrayData::Make(null(), in_data.length, data_buffers); diff --git a/cpp/src/arrow/util/bit-util.cc b/cpp/src/arrow/util/bit-util.cc index 7b7a7261fad1..862b1fd050bc 100644 --- a/cpp/src/arrow/util/bit-util.cc +++ b/cpp/src/arrow/util/bit-util.cc @@ -196,8 +196,8 @@ Status TransferBitmap(MemoryPool* pool, const uint8_t* data, int64_t offset, TransferBitmap(data, offset, length, 0, dest); - // As we have freshly allocated this bitmap, we should take care of zeroing the remaing - // bits. + // As we have freshly allocated this bitmap, we should take care of zeroing the + // remaining bits. int64_t num_bytes = BitUtil::BytesForBits(length); int64_t bits_to_zero = num_bytes * 8 - length; for (int64_t i = length; i < length + bits_to_zero; ++i) { From 9dbeb5093f1af6deb1f85c6f38fe415109324853 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Fri, 11 Jan 2019 19:56:40 -0800 Subject: [PATCH 08/10] cleanup unittests/nullability --- cpp/src/arrow/buffer-test.cc | 9 ++++---- .../arrow/compute/kernels/util-internal.cc | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc index b63ac401bafd..809dfa4f55d9 100644 --- a/cpp/src/arrow/buffer-test.cc +++ b/cpp/src/arrow/buffer-test.cc @@ -40,15 +40,16 @@ TEST(TestAllocate, Bitmap) { std::shared_ptr new_buffer; EXPECT_OK(AllocateBitmap(default_memory_pool(), 100, &new_buffer)); EXPECT_GE(new_buffer->size(), 13); + EXPECT_EQ(new_buffer->capacity() % 8, 0); } TEST(TestAllocate, EmptyBitmap) { std::shared_ptr new_buffer; EXPECT_OK(AllocateEmptyBitmap(default_memory_pool(), 100, &new_buffer)); - EXPECT_GE(new_buffer->size(), 13); - EXPECT_EQ( - std::vector(new_buffer->data(), new_buffer->data() + new_buffer->size()), - std::vector(new_buffer->size(), 0)); + EXPECT_EQ(new_buffer->size(), 13); + EXPECT_EQ(new_buffer->capacity() % 8, 0); + EXPECT_TRUE(std::all_of(new_buffer->data(), new_buffer->data() + new_buffer->capacity(), + [](int8_t byte) { return byte == 0; })); } TEST(TestBuffer, FromStdString) { diff --git a/cpp/src/arrow/compute/kernels/util-internal.cc b/cpp/src/arrow/compute/kernels/util-internal.cc index f85ff3193ba0..04ee9c02f495 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.cc +++ b/cpp/src/arrow/compute/kernels/util-internal.cc @@ -168,6 +168,10 @@ PrimitiveAllocatingUnaryKernel::PrimitiveAllocatingUnaryKernel( std::unique_ptr delegate) : delegate_(std::move(delegate)) {} +inline void ZeroLastByte(Buffer* buffer) { + *(buffer->mutable_data() + (buffer->size() - 1)) = 0; +} + Status PrimitiveAllocatingUnaryKernel::Call(FunctionContext* ctx, const Datum& input, Datum* out) { std::vector> data_buffers; @@ -181,20 +185,21 @@ Status PrimitiveAllocatingUnaryKernel::Call(FunctionContext* ctx, const Datum& i } else { std::shared_ptr buffer; RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); - // Make the last byte deterministic because underlying bit functions - // try to restore it. - *(buffer->mutable_data() + (buffer->size() - 1)) = 0; + // Per spec all trailing bits should indicate nullness, since + // the last byte might only be partially set, we ensure the + // remaining bit is set. + ZeroLastByte(buffer.get()); + buffer->ZeroPadding(); data_buffers.push_back(buffer); } // Allocate the boolean value buffer. std::shared_ptr buffer; RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); - // Make the last byte deterministic because underlying bit functions - // try to restore it. - *(buffer->mutable_data() + (buffer->size() - 1)) = 0; - + // Some utility methods access the last byte before it might be + // initialized this makes valgrind/asan unhappy, so we proactively + // zero it. + ZeroLastByte(buffer.get()); data_buffers.push_back(buffer); - out->value = ArrayData::Make(null(), in_data.length, data_buffers); return delegate_->Call(ctx, input, out); From 236272292150490f23b0abdc60c0e73694902e49 Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Mon, 14 Jan 2019 20:12:25 -0800 Subject: [PATCH 09/10] address code review comments --- cpp/src/arrow/buffer.cc | 3 +-- cpp/src/arrow/compute/kernel.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 28a93a89f989..8f05912b8041 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -213,8 +213,7 @@ Status AllocateResizableBuffer(const int64_t size, } Status AllocateBitmap(MemoryPool* pool, int64_t length, std::shared_ptr* out) { - RETURN_NOT_OK(AllocateBuffer(pool, BitUtil::BytesForBits(length), out)); - return Status::OK(); + return AllocateBuffer(pool, BitUtil::BytesForBits(length), out); } Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length, diff --git a/cpp/src/arrow/compute/kernel.h b/cpp/src/arrow/compute/kernel.h index 9644ec7a9a19..bf6a54682511 100644 --- a/cpp/src/arrow/compute/kernel.h +++ b/cpp/src/arrow/compute/kernel.h @@ -154,9 +154,9 @@ struct ARROW_EXPORT Datum { /// \brief An function of a single input argument. /// /// Note to implementors: Try to avoid making kernels that allocate memory if -/// the output size is a determinstic function of the Input Datum's metadata. +/// the output size is a deterministic function of the Input Datum's metadata. /// Instead separate the logic of the kernel and allocations necessary into -/// two different kernels. Some reusable kernels that allocate buffers without +/// two different kernels. Some reusable kernels that allocate buffers /// and delegate computation to another kernel are available in util-internal.h. class ARROW_EXPORT UnaryKernel : public OpKernel { public: From d9644e7fd9f2bd5f9e1b0e194ef52d59b12ec9fa Mon Sep 17 00:00:00 2001 From: Micah Kornfield Date: Mon, 14 Jan 2019 20:16:37 -0800 Subject: [PATCH 10/10] another doc fix --- cpp/src/arrow/compute/kernels/util-internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/kernels/util-internal.h b/cpp/src/arrow/compute/kernels/util-internal.h index aa1488361ea7..2dd8c0288a7e 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.h +++ b/cpp/src/arrow/compute/kernels/util-internal.h @@ -66,7 +66,7 @@ Datum WrapArraysLike(const Datum& value, ARROW_EXPORT Datum WrapDatumsLike(const Datum& value, const std::vector& datums); -/// \brief Buffer used to preallocate outputs for primitive types. +/// \brief Kernel used to preallocate outputs for primitive types. class PrimitiveAllocatingUnaryKernel : public UnaryKernel { public: explicit PrimitiveAllocatingUnaryKernel(std::unique_ptr delegate);