diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc index 7c54e136195f..809dfa4f55d9 100644 --- a/cpp/src/arrow/buffer-test.cc +++ b/cpp/src/arrow/buffer-test.cc @@ -36,6 +36,22 @@ using std::string; namespace arrow { +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_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) { std::string val = "hello, world"; diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 9534d2b94f3e..8f05912b8041 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -212,9 +212,13 @@ 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 AllocateBuffer(pool, BitUtil::BytesForBits(length), out); +} + 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..bf6a54682511 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 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 +/// 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.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/util-internal.cc b/cpp/src/arrow/compute/kernels/util-internal.cc index 81fafcba6167..04ee9c02f495 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" @@ -27,6 +28,7 @@ #include "arrow/table.h" #include "arrow/util/logging.h" +#include "arrow/compute/context.h" #include "arrow/compute/kernel.h" namespace arrow { @@ -162,6 +164,47 @@ Datum WrapDatumsLike(const Datum& value, const std::vector& datums) { } } +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; + 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)); + // 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)); + // 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); +} + } // 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..2dd8c0288a7e 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 Kernel 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 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) {