From d4ee14368617660064b8edce2bc080350f4634ca Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 28 May 2018 17:44:17 +0200 Subject: [PATCH 1/3] ARROW-2641: [C++] Avoid spurious memset() calls --- cpp/cmake_modules/BuildUtils.cmake | 4 +- cpp/src/arrow/array-test.cc | 49 ++++-- cpp/src/arrow/array.h | 2 +- cpp/src/arrow/builder-benchmark.cc | 24 +++ cpp/src/arrow/builder.cc | 214 +++++++++++++++-------- cpp/src/arrow/builder.h | 12 +- cpp/src/arrow/compare.cc | 5 +- cpp/src/arrow/compute/compute-test.cc | 2 + cpp/src/arrow/compute/kernels/cast.cc | 4 +- cpp/src/arrow/compute/kernels/hash.cc | 11 +- cpp/src/arrow/ipc/feather-test.cc | 35 ++-- cpp/src/arrow/ipc/ipc-read-write-test.cc | 4 + cpp/src/arrow/ipc/json-internal.cc | 44 +++-- cpp/src/arrow/type_traits.h | 52 ++++++ cpp/src/arrow/util/bit-util-benchmark.cc | 9 + cpp/src/arrow/util/bit-util-test.cc | 56 +++++- cpp/src/arrow/util/bit-util.h | 72 +++++++- cpp/src/arrow/util/hash.h | 3 + cpp/valgrind.supp | 30 ++++ 19 files changed, 507 insertions(+), 125 deletions(-) create mode 100644 cpp/valgrind.supp diff --git a/cpp/cmake_modules/BuildUtils.cmake b/cpp/cmake_modules/BuildUtils.cmake index f10a7ca1c42e..ca8d285560cd 100644 --- a/cpp/cmake_modules/BuildUtils.cmake +++ b/cpp/cmake_modules/BuildUtils.cmake @@ -318,7 +318,9 @@ function(ADD_ARROW_TEST REL_TEST_NAME) APPEND_STRING PROPERTY COMPILE_FLAGS " -DARROW_VALGRIND") add_test(${TEST_NAME} - bash -c "cd ${EXECUTABLE_OUTPUT_PATH}; valgrind --tool=memcheck --leak-check=full --leak-check-heuristics=stdstring --error-exitcode=1 ${TEST_PATH}") + bash -c "cd '${CMAKE_SOURCE_DIR}'; \ + valgrind --suppressions=valgrind.supp --tool=memcheck --gen-suppressions=all \ + --leak-check=full --leak-check-heuristics=stdstring --error-exitcode=1 ${TEST_PATH}") elseif(MSVC) add_test(${TEST_NAME} ${TEST_PATH}) else() diff --git a/cpp/src/arrow/array-test.cc b/cpp/src/arrow/array-test.cc index 39847e0c40a8..cd329763b9a4 100644 --- a/cpp/src/arrow/array-test.cc +++ b/cpp/src/arrow/array-test.cc @@ -192,6 +192,18 @@ TEST_F(TestArray, TestIsNullIsValid) { } } +TEST_F(TestArray, TestIsNullIsValidNoNulls) { + const int64_t size = 10; + + std::unique_ptr arr; + arr.reset(new Int32Array(size, nullptr, nullptr, 0)); + + for (size_t i = 0; i < size; ++i) { + EXPECT_TRUE(arr->IsValid(i)); + EXPECT_FALSE(arr->IsNull(i)); + } +} + TEST_F(TestArray, BuildLargeInMemoryArray) { const int64_t length = static_cast(std::numeric_limits::max()) + 1; @@ -373,46 +385,50 @@ int64_t TestPrimitiveBuilder::FlipValue(int64_t value) const { template <> void TestPrimitiveBuilder::Check(const std::unique_ptr& builder, bool nullable) { - int64_t size = builder->length(); + const int64_t size = builder->length(); + // Build expected result array std::shared_ptr ex_data; - ASSERT_OK(BitUtil::BytesToBits(draws_, default_memory_pool(), &ex_data)); - std::shared_ptr ex_null_bitmap; int64_t ex_null_count = 0; + ASSERT_OK(BitUtil::BytesToBits(draws_, default_memory_pool(), &ex_data)); if (nullable) { ASSERT_OK(BitUtil::BytesToBits(valid_bytes_, default_memory_pool(), &ex_null_bitmap)); ex_null_count = test::null_count(valid_bytes_); } else { ex_null_bitmap = nullptr; } - auto expected = std::make_shared(size, ex_data, ex_null_bitmap, ex_null_count); + ASSERT_EQ(size, expected->length()); + // Finish builder and check result array std::shared_ptr out; ASSERT_OK(builder->Finish(&out)); std::shared_ptr result = std::dynamic_pointer_cast(out); - // Builder is now reset - ASSERT_EQ(0, builder->length()); - ASSERT_EQ(0, builder->capacity()); - ASSERT_EQ(0, builder->null_count()); - ASSERT_EQ(nullptr, builder->data()); - ASSERT_EQ(ex_null_count, result->null_count()); + ASSERT_EQ(size, result->length()); - ASSERT_EQ(expected->length(), result->length()); - - for (int64_t i = 0; i < result->length(); ++i) { + for (int64_t i = 0; i < size; ++i) { if (nullable) { ASSERT_EQ(valid_bytes_[i] == 0, result->IsNull(i)) << i; + } else { + ASSERT_FALSE(result->IsNull(i)); + } + if (!result->IsNull(i)) { + bool actual = BitUtil::GetBit(result->values()->data(), i); + ASSERT_EQ(draws_[i] != 0, actual) << i; } - bool actual = BitUtil::GetBit(result->values()->data(), i); - ASSERT_EQ(draws_[i] != 0, actual) << i; } ASSERT_TRUE(result->Equals(*expected)); + + // Builder is now reset + ASSERT_EQ(0, builder->length()); + ASSERT_EQ(0, builder->capacity()); + ASSERT_EQ(0, builder->null_count()); + ASSERT_EQ(nullptr, builder->data()); } typedef ::testing::Typesbuilder_->length()); ASSERT_EQ(BitUtil::NextPower2(size), this->builder_->capacity()); + ASSERT_EQ(size, this->builder_nn_->length()); + ASSERT_EQ(BitUtil::NextPower2(size), this->builder_nn_->capacity()); + this->Check(this->builder_, true); this->Check(this->builder_nn_, false); } diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index 6f61511d2d2c..7d2679336aa9 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -210,7 +210,7 @@ class ARROW_EXPORT Array { /// \brief Return true if value at index is valid (not null). Does not /// boundscheck bool IsValid(int64_t i) const { - return null_bitmap_data_ != NULLPTR && + return null_bitmap_data_ == NULLPTR || BitUtil::GetBit(null_bitmap_data_, i + data_->offset); } diff --git a/cpp/src/arrow/builder-benchmark.cc b/cpp/src/arrow/builder-benchmark.cc index 12b3eac1478d..809e6ffab85e 100644 --- a/cpp/src/arrow/builder-benchmark.cc +++ b/cpp/src/arrow/builder-benchmark.cc @@ -115,6 +115,27 @@ static void BM_BuildAdaptiveUIntNoNulls( state.SetBytesProcessed(state.iterations() * data.size() * sizeof(int64_t)); } +static void BM_BuildBooleanArrayNoNulls( + benchmark::State& state) { // NOLINT non-const reference + // 2 MiB block + std::vector data(2 * 1024 * 1024); + constexpr uint8_t bit_pattern = 0xcc; // 0b11001100 + uint64_t index = 0; + std::generate(data.begin(), data.end(), + [&index]() -> uint8_t { return (bit_pattern >> ((index++) % 8)) & 1; }); + + while (state.KeepRunning()) { + BooleanBuilder builder; + for (int i = 0; i < kFinalSize; i++) { + // Build up an array of 512 MiB in size + ABORT_NOT_OK(builder.AppendValues(data.data(), data.size())); + } + std::shared_ptr out; + ABORT_NOT_OK(builder.Finish(&out)); + } + state.SetBytesProcessed(state.iterations() * data.size() * kFinalSize); +} + static void BM_BuildBinaryArray(benchmark::State& state) { // NOLINT non-const reference const int64_t iterations = 1 << 20; @@ -152,6 +173,9 @@ static void BM_BuildFixedSizeBinaryArray( BENCHMARK(BM_BuildPrimitiveArrayNoNulls)->Repetitions(3)->Unit(benchmark::kMicrosecond); BENCHMARK(BM_BuildVectorNoNulls)->Repetitions(3)->Unit(benchmark::kMicrosecond); + +BENCHMARK(BM_BuildBooleanArrayNoNulls)->Repetitions(3)->Unit(benchmark::kMicrosecond); + BENCHMARK(BM_BuildAdaptiveIntNoNulls)->Repetitions(3)->Unit(benchmark::kMicrosecond); BENCHMARK(BM_BuildAdaptiveIntNoNullsScalarAppend) ->Repetitions(3) diff --git a/cpp/src/arrow/builder.cc b/cpp/src/arrow/builder.cc index 6be35fe4357a..c11b04ab8b81 100644 --- a/cpp/src/arrow/builder.cc +++ b/cpp/src/arrow/builder.cc @@ -63,7 +63,7 @@ Status ArrayBuilder::AppendToBitmap(const uint8_t* valid_bytes, int64_t length) } Status ArrayBuilder::Init(int64_t capacity) { - int64_t to_alloc = BitUtil::CeilByte(capacity) / 8; + int64_t to_alloc = BitUtil::BytesForBits(capacity); null_bitmap_ = std::make_shared(pool_); RETURN_NOT_OK(null_bitmap_->Resize(to_alloc)); // Buffers might allocate more then necessary to satisfy padding requirements @@ -78,7 +78,7 @@ Status ArrayBuilder::Resize(int64_t new_bits) { if (!null_bitmap_) { return Init(new_bits); } - int64_t new_bytes = BitUtil::CeilByte(new_bits) / 8; + int64_t new_bytes = BitUtil::BytesForBits(new_bits); int64_t old_bytes = null_bitmap_->size(); RETURN_NOT_OK(null_bitmap_->Resize(new_bytes)); null_bitmap_data_ = null_bitmap_->mutable_data(); @@ -236,8 +236,6 @@ Status PrimitiveBuilder::Init(int64_t capacity) { int64_t nbytes = TypeTraits::bytes_required(capacity); RETURN_NOT_OK(data_->Resize(nbytes)); - // TODO(emkornfield) valgrind complains without this - memset(data_->mutable_data(), 0, static_cast(nbytes)); raw_data_ = reinterpret_cast(data_->mutable_data()); return Status::OK(); @@ -254,13 +252,9 @@ Status PrimitiveBuilder::Resize(int64_t capacity) { RETURN_NOT_OK(Init(capacity)); } else { RETURN_NOT_OK(ArrayBuilder::Resize(capacity)); - const int64_t old_bytes = data_->size(); const int64_t new_bytes = TypeTraits::bytes_required(capacity); RETURN_NOT_OK(data_->Resize(new_bytes)); raw_data_ = reinterpret_cast(data_->mutable_data()); - // TODO(emkornfield) valgrind complains without this - memset(data_->mutable_data() + old_bytes, 0, - static_cast(new_bytes - old_bytes)); } return Status::OK(); } @@ -372,8 +366,6 @@ Status AdaptiveIntBuilderBase::Init(int64_t capacity) { int64_t nbytes = capacity * int_size_; RETURN_NOT_OK(data_->Resize(nbytes)); - // TODO(emkornfield) valgrind complains without this - memset(data_->mutable_data(), 0, static_cast(nbytes)); raw_data_ = reinterpret_cast(data_->mutable_data()); return Status::OK(); @@ -389,13 +381,9 @@ Status AdaptiveIntBuilderBase::Resize(int64_t capacity) { RETURN_NOT_OK(Init(capacity)); } else { RETURN_NOT_OK(ArrayBuilder::Resize(capacity)); - const int64_t old_bytes = data_->size(); const int64_t new_bytes = capacity * int_size_; RETURN_NOT_OK(data_->Resize(new_bytes)); raw_data_ = data_->mutable_data(); - // TODO(emkornfield) valgrind complains without this - memset(data_->mutable_data() + old_bytes, 0, - static_cast(new_bytes - old_bytes)); } return Status::OK(); } @@ -736,8 +724,6 @@ Status BooleanBuilder::Init(int64_t capacity) { int64_t nbytes = BitUtil::BytesForBits(capacity); RETURN_NOT_OK(data_->Resize(nbytes)); - // TODO(emkornfield) valgrind complains without this - memset(data_->mutable_data(), 0, static_cast(nbytes)); raw_data_ = reinterpret_cast(data_->mutable_data()); return Status::OK(); @@ -753,24 +739,31 @@ Status BooleanBuilder::Resize(int64_t capacity) { RETURN_NOT_OK(Init(capacity)); } else { RETURN_NOT_OK(ArrayBuilder::Resize(capacity)); + const int64_t old_bytes = data_->size(); const int64_t new_bytes = BitUtil::BytesForBits(capacity); - RETURN_NOT_OK(data_->Resize(new_bytes)); - raw_data_ = reinterpret_cast(data_->mutable_data()); - memset(data_->mutable_data() + old_bytes, 0, - static_cast(new_bytes - old_bytes)); + if (new_bytes != old_bytes) { + RETURN_NOT_OK(data_->Resize(new_bytes)); + raw_data_ = reinterpret_cast(data_->mutable_data()); + } } return Status::OK(); } Status BooleanBuilder::FinishInternal(std::shared_ptr* out) { const int64_t bytes_required = BitUtil::BytesForBits(length_); - if (bytes_required > 0 && bytes_required < data_->size()) { // Trim buffers RETURN_NOT_OK(data_->Resize(bytes_required)); } + + int64_t bit_offset = length_ % 8; + if (bit_offset > 0) { + // Adjust last byte + data_->mutable_data()[length_ / 8] &= BitUtil::kPrecedingBitmask[bit_offset]; + } + *out = ArrayData::Make(boolean(), length_, {null_bitmap_, data_}, null_count_); data_ = null_bitmap_ = nullptr; @@ -782,9 +775,16 @@ Status BooleanBuilder::AppendValues(const uint8_t* values, int64_t length, const uint8_t* valid_bytes) { RETURN_NOT_OK(Reserve(length)); + internal::FirstTimeBitmapWriter bit_writer(raw_data_, length_, length); for (int64_t i = 0; i < length; ++i) { - BitUtil::SetBitTo(raw_data_, length_ + i, values[i] != 0); + if (values[i] != 0) { + bit_writer.Set(); + } else { + bit_writer.Clear(); + } + bit_writer.Next(); } + bit_writer.Finish(); // this updates length_ ArrayBuilder::UnsafeAppendToBitmap(valid_bytes, length); @@ -801,9 +801,16 @@ Status BooleanBuilder::AppendValues(const uint8_t* values, int64_t length, RETURN_NOT_OK(Reserve(length)); DCHECK_EQ(length, static_cast(is_valid.size())); + internal::FirstTimeBitmapWriter bit_writer(raw_data_, length_, length); for (int64_t i = 0; i < length; ++i) { - BitUtil::SetBitTo(raw_data_, length_ + i, values[i] != 0); + if (values[i]) { + bit_writer.Set(); + } else { + bit_writer.Clear(); + } + bit_writer.Next(); } + bit_writer.Finish(); // this updates length_ ArrayBuilder::UnsafeAppendToBitmap(is_valid); @@ -839,9 +846,16 @@ Status BooleanBuilder::AppendValues(const std::vector& values, RETURN_NOT_OK(Reserve(length)); DCHECK_EQ(length, static_cast(is_valid.size())); + internal::FirstTimeBitmapWriter bit_writer(raw_data_, length_, length); for (int64_t i = 0; i < length; ++i) { - BitUtil::SetBitTo(raw_data_, length_ + i, values[i]); + if (values[i]) { + bit_writer.Set(); + } else { + bit_writer.Clear(); + } + bit_writer.Next(); } + bit_writer.Finish(); // this updates length_ ArrayBuilder::UnsafeAppendToBitmap(is_valid); @@ -857,10 +871,18 @@ Status BooleanBuilder::AppendValues(const std::vector& values) { const int64_t length = static_cast(values.size()); RETURN_NOT_OK(Reserve(length)); + internal::FirstTimeBitmapWriter bit_writer(raw_data_, length_, length); for (int64_t i = 0; i < length; ++i) { - BitUtil::SetBitTo(raw_data_, length_ + i, values[i]); + if (values[i]) { + bit_writer.Set(); + } else { + bit_writer.Clear(); + } + bit_writer.Next(); } + bit_writer.Finish(); + // this updates length_ ArrayBuilder::UnsafeSetNotNull(length); return Status::OK(); } @@ -872,8 +894,42 @@ Status BooleanBuilder::Append(const std::vector& values) { // ---------------------------------------------------------------------- // DictionaryBuilder +using internal::DictionaryScalar; using internal::WrappedBinary; +namespace { +// +// Fetch the value of a dictionary entry stored in a builder. + +// template ::BuilderType, +// typename Scalar = typename std::enable_if::is_primitive, +// typename T::c_type>::type> +// Scalar GetDictionaryValue(Builder& builder, int64_t index) { +// const Scalar* data = reinterpret_cast(builder.data()->data()); +// return data[index]; +// } +// +// template ::BuilderType, +// typename Scalar = typename std::enable_if::is_binary_like, +// WrappedBinary>::type> +// WrappedBinary GetDictionaryValue(Builder& builder, int64_t index) { +// int32_t v_len; +// const uint8_t* v = builder.GetValue(static_cast(index), &v_len); +// return WrappedBinary(v, v_len); +// } +// +// template ::BuilderType, +// typename Scalar = typename std::enable_if::value, const uint8_t*>::type> +// const uint8_t* GetDictionaryValue(Builder& dictionary_builder, int64_t index) { +// return dictionary_builder.GetValue(index); +// } + +} // namespace + template DictionaryBuilder::DictionaryBuilder(const std::shared_ptr& type, MemoryPool* pool) @@ -1036,11 +1092,19 @@ Status DictionaryBuilder::AppendNull() { Status DictionaryBuilder::AppendNull() { return values_builder_.AppendNull(); } +template +int64_t DictionaryBuilder::HashValue(const Scalar& value) { + return HashUtil::Hash(&value, sizeof(Scalar), 0); +} + template Status DictionaryBuilder::DoubleTableSize() { -#define INNER_LOOP \ - Scalar value = GetDictionaryValue(dict_builder_, static_cast(index)); \ - int64_t j = HashValue(value) & new_mod_bitmask; + // const auto GetDictBuilderValue = [this](hash_slot_t index) -> Scalar { + // return GetDictionaryValue(dict_builder_, static_cast(index)); + // }; + +#define INNER_LOOP \ + int64_t j = HashValue(GetDictionaryValue(dict_builder_, index)) & new_mod_bitmask DOUBLE_TABLE_SIZE(, INNER_LOOP); @@ -1054,9 +1118,30 @@ typename DictionaryBuilder::Scalar DictionaryBuilder::GetDictionaryValue( return data[index]; } +template +bool DictionaryBuilder::SlotDifferent(hash_slot_t index, const Scalar& value) { + DCHECK_GE(index, 0); + if (index >= entry_id_offset_) { + // Lookup delta dictionary + DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); + return GetDictionaryValue(dict_builder_, + static_cast(index - entry_id_offset_)) != value; + } else { + DCHECK_LT(index, overflow_dict_builder_.length()); + return GetDictionaryValue(overflow_dict_builder_, static_cast(index)) != + value; + } +} + +template +Status DictionaryBuilder::AppendDictionary(const Scalar& value) { + return dict_builder_.Append(value); +} + template Status DictionaryBuilder::FinishInternal(std::shared_ptr* out) { entry_id_offset_ += dict_builder_.length(); + // Store current dict entries for further uses of this DictionaryBuilder RETURN_NOT_OK(overflow_dict_builder_.AppendValues( reinterpret_cast::Scalar*>(dict_builder_.data()->data()), dict_builder_.length(), nullptr)); @@ -1080,6 +1165,10 @@ Status DictionaryBuilder::FinishInternal(std::shared_ptr* o return Status::OK(); } +// +// FixedSizeBinaryType specializations +// + template <> const uint8_t* DictionaryBuilder::GetDictionaryValue( typename TypeTraits::BuilderType& dictionary_builder, @@ -1109,52 +1198,33 @@ Status DictionaryBuilder::FinishInternal( return Status::OK(); } -template -int64_t DictionaryBuilder::HashValue(const Scalar& value) { - return HashUtil::Hash(&value, sizeof(Scalar), 0); -} - template <> int64_t DictionaryBuilder::HashValue(const Scalar& value) { return HashUtil::Hash(value, byte_width_, 0); } -template -bool DictionaryBuilder::SlotDifferent(hash_slot_t index, const Scalar& value) { - const bool value_found = - index >= entry_id_offset_ && - GetDictionaryValue(dict_builder_, static_cast(index - entry_id_offset_)) == - value; - const bool value_found_overflow = - entry_id_offset_ > 0 && - GetDictionaryValue(overflow_dict_builder_, static_cast(index)) == value; - return !(value_found || value_found_overflow); -} - template <> bool DictionaryBuilder::SlotDifferent(hash_slot_t index, const Scalar& value) { - int32_t width = checked_cast(*type_).byte_width(); - bool value_found = false; + const int32_t width = checked_cast(*type_).byte_width(); + + DCHECK_GE(index, 0); + Scalar other; if (index >= entry_id_offset_) { - const Scalar other = + // Lookup delta dictionary + DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); + other = GetDictionaryValue(dict_builder_, static_cast(index - entry_id_offset_)); - value_found = memcmp(other, value, width) == 0; - } - - bool value_found_overflow = false; - if (entry_id_offset_ > 0) { - const Scalar other_overflow = - GetDictionaryValue(overflow_dict_builder_, static_cast(index)); - value_found_overflow = memcmp(other_overflow, value, width) == 0; + } else { + DCHECK_LT(index, overflow_dict_builder_.length()); + other = GetDictionaryValue(overflow_dict_builder_, static_cast(index)); } - return !(value_found || value_found_overflow); + return memcmp(other, value, width) != 0; } -template -Status DictionaryBuilder::AppendDictionary(const Scalar& value) { - return dict_builder_.Append(value); -} +// +// StringType and BinaryType specializations +// #define BINARY_DICTIONARY_SPECIALIZATIONS(Type) \ template <> \ @@ -1194,24 +1264,20 @@ Status DictionaryBuilder::AppendDictionary(const Scalar& value) { template <> \ bool DictionaryBuilder::SlotDifferent(hash_slot_t index, \ const WrappedBinary& value) { \ + DCHECK_GE(index, 0); \ + const uint8_t* other_value; \ int32_t other_length; \ - bool value_found = false; \ if (index >= entry_id_offset_) { \ - const uint8_t* other_value = dict_builder_.GetValue( \ + DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); \ + other_value = dict_builder_.GetValue( \ static_cast(index - entry_id_offset_), &other_length); \ - value_found = other_length == value.length_ && \ - memcmp(other_value, value.ptr_, value.length_) == 0; \ - } \ - \ - bool value_found_overflow = false; \ - if (entry_id_offset_ > 0) { \ - const uint8_t* other_value_overflow = \ + } else { \ + DCHECK_LT(index, overflow_dict_builder_.length()); \ + other_value = \ overflow_dict_builder_.GetValue(static_cast(index), &other_length); \ - value_found_overflow = \ - other_length == value.length_ && \ - memcmp(other_value_overflow, value.ptr_, value.length_) == 0; \ } \ - return !(value_found || value_found_overflow); \ + return other_length != value.length_ || \ + memcmp(other_value, value.ptr_, value.length_) != 0; \ } \ \ template <> \ diff --git a/cpp/src/arrow/builder.h b/cpp/src/arrow/builder.h index e6de39020936..d504cea926b3 100644 --- a/cpp/src/arrow/builder.h +++ b/cpp/src/arrow/builder.h @@ -984,10 +984,12 @@ class ARROW_EXPORT DictionaryBuilder : public ArrayBuilder { bool is_building_delta() { return entry_id_offset_ > 0; } protected: + // Hash table implementation helpers Status DoubleTableSize(); Scalar GetDictionaryValue(typename TypeTraits::BuilderType& dictionary_builder, int64_t index); int64_t HashValue(const Scalar& value); + // Check whether the dictionary entry in *slot* is equal to the given *value* bool SlotDifferent(hash_slot_t slot, const Scalar& value); Status AppendDictionary(const Scalar& value); @@ -997,16 +999,20 @@ class ARROW_EXPORT DictionaryBuilder : public ArrayBuilder { /// Size of the table. Must be a power of 2. int64_t hash_table_size_; - // offset for the entry ids. Used to build delta dictionaries, - // increased on every InternalFinish by the number of current entries - // in the dictionary + // Offset for the dictionary entries in dict_builder_. + // Increased on every Finish call by the number of current entries + // in the dictionary. int64_t entry_id_offset_; // Store hash_table_size_ - 1, so that j & mod_bitmask_ is equivalent to j % // hash_table_size_, but uses far fewer CPU cycles int64_t mod_bitmask_; + // This builder accumulates new dictionary entries since the last Finish call + // (or since the beginning if Finish hasn't been called). + // In other words, it contains the current delta dictionary. typename TypeTraits::BuilderType dict_builder_; + // This builder stores dictionary entries encountered before the last Finish call. typename TypeTraits::BuilderType overflow_dict_builder_; AdaptiveIntBuilder values_builder_; diff --git a/cpp/src/arrow/compare.cc b/cpp/src/arrow/compare.cc index 7a84b1411ac0..87016ba719d6 100644 --- a/cpp/src/arrow/compare.cc +++ b/cpp/src/arrow/compare.cc @@ -319,7 +319,10 @@ static bool IsEqualPrimitive(const PrimitiveArray& left, const PrimitiveArray& r for (int64_t i = 0; i < left.length(); ++i) { const bool left_null = left.IsNull(i); const bool right_null = right.IsNull(i); - if (!left_null && (memcmp(left_data, right_data, byte_width) != 0 || right_null)) { + if (left_null != right_null) { + return false; + } + if (!left_null && memcmp(left_data, right_data, byte_width) != 0) { return false; } left_data += byte_width; diff --git a/cpp/src/arrow/compute/compute-test.cc b/cpp/src/arrow/compute/compute-test.cc index e8dc2bca83d6..6a9284460fd7 100644 --- a/cpp/src/arrow/compute/compute-test.cc +++ b/cpp/src/arrow/compute/compute-test.cc @@ -927,6 +927,8 @@ TYPED_TEST(TestHashKernelPrimitive, Unique) { auto type = TypeTraits::type_singleton(); CheckUnique(&this->ctx_, type, {2, 1, 2, 1}, {true, false, true, true}, {2, 1}, {}); + CheckUnique(&this->ctx_, type, {2, 1, 3, 1}, {false, false, true, true}, + {3, 1}, {}); } TYPED_TEST(TestHashKernelPrimitive, DictEncode) { diff --git a/cpp/src/arrow/compute/kernels/cast.cc b/cpp/src/arrow/compute/kernels/cast.cc index e5b9eaf5f088..7987a1c95dfa 100644 --- a/cpp/src/arrow/compute/kernels/cast.cc +++ b/cpp/src/arrow/compute/kernels/cast.cc @@ -200,8 +200,8 @@ struct CastFunctor(input, 1); - internal::BitmapWriter writer(output->buffers[1]->mutable_data(), output->offset, - input.length); + internal::FirstTimeBitmapWriter writer(output->buffers[1]->mutable_data(), + output->offset, input.length); for (int64_t i = 0; i < input.length; ++i) { if (*in_data++ != 0) { diff --git a/cpp/src/arrow/compute/kernels/hash.cc b/cpp/src/arrow/compute/kernels/hash.cc index 1c849d0cff98..5ef8ee59fd1f 100644 --- a/cpp/src/arrow/compute/kernels/hash.cc +++ b/cpp/src/arrow/compute/kernels/hash.cc @@ -349,23 +349,24 @@ class HashTableKernel> : public HashTable internal::BitmapReader valid_reader(arr.buffers[0]->data(), arr.offset, arr.length); for (int64_t i = 0; i < arr.length; ++i) { const bool is_null = valid_reader.IsNotSet(); - const bool value = value_reader.IsSet(); - const int j = value ? 1 : 0; - hash_slot_t slot = table_[j]; valid_reader.Next(); - value_reader.Next(); if (is_null) { + value_reader.Next(); action->ObserveNull(); continue; } + const bool value = value_reader.IsSet(); + value_reader.Next(); + const int j = value ? 1 : 0; + hash_slot_t slot = table_[j]; HASH_INNER_LOOP(); } } else { for (int64_t i = 0; i < arr.length; ++i) { const bool value = value_reader.IsSet(); + value_reader.Next(); const int j = value ? 1 : 0; hash_slot_t slot = table_[j]; - value_reader.Next(); HASH_INNER_LOOP(); } } diff --git a/cpp/src/arrow/ipc/feather-test.cc b/cpp/src/arrow/ipc/feather-test.cc index cea5176c41f9..c5badc3f1494 100644 --- a/cpp/src/arrow/ipc/feather-test.cc +++ b/cpp/src/arrow/ipc/feather-test.cc @@ -358,26 +358,41 @@ TEST_F(TestTableWriter, TimeTypes) { auto f3 = field("f3", timestamp(TimeUnit::SECOND, "US/Los_Angeles")); auto schema = ::arrow::schema({f0, f1, f2, f3}); - std::vector values_vec = {0, 1, 2, 3, 4, 5, 6}; - std::shared_ptr values; - ArrayFromVector(is_valid, values_vec, &values); + std::vector values64_vec = {0, 1, 2, 3, 4, 5, 6}; + std::shared_ptr values64; + ArrayFromVector(is_valid, values64_vec, &values64); - std::vector date_values_vec = {0, 1, 2, 3, 4, 5, 6}; + std::vector values32_vec = {10, 11, 12, 13, 14, 15, 16}; + std::shared_ptr values32; + ArrayFromVector(is_valid, values32_vec, &values32); + + std::vector date_values_vec = {20, 21, 22, 23, 24, 25, 26}; std::shared_ptr date_array; ArrayFromVector(is_valid, date_values_vec, &date_array); - const auto& prim_values = checked_cast(*values); - BufferVector buffers = {prim_values.null_bitmap(), prim_values.values()}; + const auto& prim_values64 = checked_cast(*values64); + BufferVector buffers64 = {prim_values64.null_bitmap(), prim_values64.values()}; + + const auto& prim_values32 = checked_cast(*values32); + BufferVector buffers32 = {prim_values32.null_bitmap(), prim_values32.values()}; + // Push date32 ArrayData std::vector> arrays; arrays.push_back(date_array->data()); - for (int i = 1; i < schema->num_fields(); ++i) { - arrays.emplace_back(ArrayData::Make(schema->field(i)->type(), values->length(), - BufferVector(buffers), values->null_count(), 0)); + // Create time32 ArrayData + arrays.emplace_back(ArrayData::Make(schema->field(1)->type(), values32->length(), + BufferVector(buffers32), values32->null_count(), + 0)); + + // Create timestamp ArrayData + for (int i = 2; i < schema->num_fields(); ++i) { + arrays.emplace_back(ArrayData::Make(schema->field(i)->type(), values64->length(), + BufferVector(buffers64), values64->null_count(), + 0)); } - auto batch = RecordBatch::Make(schema, values->length(), std::move(arrays)); + auto batch = RecordBatch::Make(schema, 7, std::move(arrays)); CheckBatch(*batch); } diff --git a/cpp/src/arrow/ipc/ipc-read-write-test.cc b/cpp/src/arrow/ipc/ipc-read-write-test.cc index bbc279f02926..4d959a91a5dc 100644 --- a/cpp/src/arrow/ipc/ipc-read-write-test.cc +++ b/cpp/src/arrow/ipc/ipc-read-write-test.cc @@ -621,6 +621,9 @@ INSTANTIATE_TEST_CASE_P(GenericIpcRoundTripTests, TestIpcRoundTrip, BATCH_CASES( INSTANTIATE_TEST_CASE_P(FileRoundTripTests, TestFileFormat, BATCH_CASES()); INSTANTIATE_TEST_CASE_P(StreamRoundTripTests, TestStreamFormat, BATCH_CASES()); +// This test uses uninitialized memory + +#if !(defined(ARROW_VALGRIND) || defined(ADDRESS_SANITIZER)) TEST_F(TestIpcRoundTrip, LargeRecordBatch) { const int64_t length = static_cast(std::numeric_limits::max()) + 1; @@ -649,6 +652,7 @@ TEST_F(TestIpcRoundTrip, LargeRecordBatch) { ASSERT_EQ(length, result->num_rows()); } +#endif void CheckBatchDictionaries(const RecordBatch& batch) { // Check that dictionaries that should be the same are the same diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index cb9cf51b6afb..829c895c7ba7 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -372,27 +372,42 @@ class ArrayWriter { template typename std::enable_if::value, void>::type WriteDataValues( const T& arr) { + static const char null_string[] = "0"; const auto data = arr.raw_values(); - for (int i = 0; i < arr.length(); ++i) { - writer_->Int64(data[i]); + for (int64_t i = 0; i < arr.length(); ++i) { + if (arr.IsValid(i)) { + writer_->Int64(data[i]); + } else { + writer_->RawNumber(null_string, sizeof(null_string)); + } } } template typename std::enable_if::value, void>::type WriteDataValues( const T& arr) { + static const char null_string[] = "0"; const auto data = arr.raw_values(); - for (int i = 0; i < arr.length(); ++i) { - writer_->Uint64(data[i]); + for (int64_t i = 0; i < arr.length(); ++i) { + if (arr.IsValid(i)) { + writer_->Uint64(data[i]); + } else { + writer_->RawNumber(null_string, sizeof(null_string)); + } } } template typename std::enable_if::value, void>::type WriteDataValues( const T& arr) { + static const char null_string[] = "0."; const auto data = arr.raw_values(); - for (int i = 0; i < arr.length(); ++i) { - writer_->Double(data[i]); + for (int64_t i = 0; i < arr.length(); ++i) { + if (arr.IsValid(i)) { + writer_->Double(data[i]); + } else { + writer_->RawNumber(null_string, sizeof(null_string)); + } } } @@ -424,15 +439,24 @@ class ArrayWriter { } void WriteDataValues(const Decimal128Array& arr) { + static const char null_string[] = "0"; for (int64_t i = 0; i < arr.length(); ++i) { - const Decimal128 value(arr.GetValue(i)); - writer_->String(value.ToIntegerString()); + if (arr.IsValid(i)) { + const Decimal128 value(arr.GetValue(i)); + writer_->String(value.ToIntegerString()); + } else { + writer_->String(null_string, sizeof(null_string)); + } } } void WriteDataValues(const BooleanArray& arr) { - for (int i = 0; i < arr.length(); ++i) { - writer_->Bool(arr.Value(i)); + for (int64_t i = 0; i < arr.length(); ++i) { + if (arr.IsValid(i)) { + writer_->Bool(arr.Value(i)); + } else { + writer_->Bool(false); + } } } diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index ede52e9b84bb..35bd226e46c9 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -34,6 +34,8 @@ struct TypeTraits { using ArrayType = NullArray; using BuilderType = NullBuilder; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; }; template <> @@ -43,6 +45,8 @@ struct TypeTraits { using TensorType = UInt8Tensor; static inline int64_t bytes_required(int64_t elements) { return elements; } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint8(); } }; @@ -53,6 +57,8 @@ struct TypeTraits { using TensorType = Int8Tensor; static inline int64_t bytes_required(int64_t elements) { return elements; } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int8(); } }; @@ -66,6 +72,8 @@ struct TypeTraits { return elements * sizeof(uint16_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint16(); } }; @@ -79,6 +87,8 @@ struct TypeTraits { return elements * sizeof(int16_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int16(); } }; @@ -92,6 +102,8 @@ struct TypeTraits { return elements * sizeof(uint32_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint32(); } }; @@ -105,6 +117,8 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int32(); } }; @@ -118,6 +132,8 @@ struct TypeTraits { return elements * sizeof(uint64_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint64(); } }; @@ -131,6 +147,8 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int64(); } }; @@ -143,6 +161,8 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return date64(); } }; @@ -155,6 +175,8 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return date32(); } }; @@ -167,6 +189,8 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; }; template <> @@ -178,6 +202,8 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; }; template <> @@ -189,6 +215,8 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; }; template <> @@ -201,6 +229,8 @@ struct TypeTraits { return elements * sizeof(uint16_t); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float16(); } }; @@ -214,6 +244,8 @@ struct TypeTraits { return static_cast(elements * sizeof(float)); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float32(); } }; @@ -227,6 +259,8 @@ struct TypeTraits { return static_cast(elements * sizeof(double)); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float64(); } }; @@ -235,6 +269,8 @@ struct TypeTraits { using ArrayType = Decimal128Array; using BuilderType = Decimal128Builder; constexpr static bool is_parameter_free = false; + constexpr static bool is_binary_like = false; + constexpr static bool is_primitive = true; }; template <> @@ -246,6 +282,8 @@ struct TypeTraits { return BitUtil::BytesForBits(elements); } constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = true; + constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return boolean(); } }; @@ -254,6 +292,8 @@ struct TypeTraits { using ArrayType = StringArray; using BuilderType = StringBuilder; constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = true; static inline std::shared_ptr type_singleton() { return utf8(); } }; @@ -262,6 +302,8 @@ struct TypeTraits { using ArrayType = BinaryArray; using BuilderType = BinaryBuilder; constexpr static bool is_parameter_free = true; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = true; static inline std::shared_ptr type_singleton() { return binary(); } }; @@ -270,6 +312,8 @@ struct TypeTraits { using ArrayType = FixedSizeBinaryArray; using BuilderType = FixedSizeBinaryBuilder; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = false; }; template <> @@ -277,6 +321,8 @@ struct TypeTraits { using ArrayType = ListArray; using BuilderType = ListBuilder; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = false; }; template <> @@ -284,18 +330,24 @@ struct TypeTraits { using ArrayType = StructArray; using BuilderType = StructBuilder; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = false; }; template <> struct TypeTraits { using ArrayType = UnionArray; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = false; }; template <> struct TypeTraits { using ArrayType = DictionaryArray; constexpr static bool is_parameter_free = false; + constexpr static bool is_primitive = false; + constexpr static bool is_binary_like = false; }; namespace detail { diff --git a/cpp/src/arrow/util/bit-util-benchmark.cc b/cpp/src/arrow/util/bit-util-benchmark.cc index 06aa2ed773e8..43f461cc8c1d 100644 --- a/cpp/src/arrow/util/bit-util-benchmark.cc +++ b/cpp/src/arrow/util/bit-util-benchmark.cc @@ -166,6 +166,10 @@ static void BM_BitmapWriter(benchmark::State& state) { BenchmarkBitmapWriter(state, state.range(0)); } +static void BM_FirstTimeBitmapWriter(benchmark::State& state) { + BenchmarkBitmapWriter(state, state.range(0)); +} + static void BM_CopyBitmap(benchmark::State& state) { // NOLINT non-const reference const int kBufferSize = state.range(0); std::shared_ptr buffer = CreateRandomBuffer(kBufferSize); @@ -202,5 +206,10 @@ BENCHMARK(BM_NaiveBitmapWriter) BENCHMARK(BM_BitmapWriter)->Args({100000})->MinTime(1.0)->Unit(benchmark::kMicrosecond); +BENCHMARK(BM_FirstTimeBitmapWriter) + ->Args({100000}) + ->MinTime(1.0) + ->Unit(benchmark::kMicrosecond); + } // namespace BitUtil } // namespace arrow diff --git a/cpp/src/arrow/util/bit-util-test.cc b/cpp/src/arrow/util/bit-util-test.cc index c52724412f99..5e0b6963cb25 100644 --- a/cpp/src/arrow/util/bit-util-test.cc +++ b/cpp/src/arrow/util/bit-util-test.cc @@ -44,7 +44,8 @@ static void EnsureCpuInfoInitialized() { } } -void WriteVectorToWriter(internal::BitmapWriter& writer, const std::vector values) { +template +void WriteVectorToWriter(BitmapWriter& writer, const std::vector values) { for (const auto& value : values) { if (value) { writer.Set(); @@ -228,6 +229,59 @@ TEST(BitmapWriter, DoesNotWriteOutOfBounds) { ASSERT_EQ((length - 5), num_values); } +TEST(FirstTimeBitmapWriter, NormalOperation) { + for (const auto fill_byte_int : {0x00, 0xff}) { + const uint8_t fill_byte = static_cast(fill_byte_int); + { + uint8_t bitmap[] = {fill_byte, fill_byte, fill_byte, fill_byte}; + auto writer = internal::FirstTimeBitmapWriter(bitmap, 0, 12); + WriteVectorToWriter(writer, {0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1}); + // {0b00110110, 0b1010, 0, 0} + ASSERT_BYTES_EQ(bitmap, {0x36, 0x0a}); + } + { + uint8_t bitmap[] = {fill_byte, fill_byte, fill_byte, fill_byte}; + auto writer = internal::FirstTimeBitmapWriter(bitmap, 4, 12); + WriteVectorToWriter(writer, {0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1}); + // {0b00110110, 0b1010, 0, 0} + ASSERT_BYTES_EQ(bitmap, {static_cast(0x60 | (fill_byte & 0x0f)), 0xa3}); + } + // Consecutive write chunks + { + uint8_t bitmap[] = {fill_byte, fill_byte, fill_byte, fill_byte}; + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 0, 6); + WriteVectorToWriter(writer, {0, 1, 1, 0, 1, 1}); + } + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 6, 3); + WriteVectorToWriter(writer, {0, 0, 0}); + } + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 9, 3); + WriteVectorToWriter(writer, {1, 0, 1}); + } + ASSERT_BYTES_EQ(bitmap, {0x36, 0x0a}); + } + { + uint8_t bitmap[] = {fill_byte, fill_byte, fill_byte, fill_byte}; + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 4, 6); + WriteVectorToWriter(writer, {0, 1, 1, 0, 1, 1}); + } + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 10, 3); + WriteVectorToWriter(writer, {0, 0, 0}); + } + { + auto writer = internal::FirstTimeBitmapWriter(bitmap, 13, 3); + WriteVectorToWriter(writer, {1, 0, 1}); + } + ASSERT_BYTES_EQ(bitmap, {static_cast(0x60 | (fill_byte & 0x0f)), 0xa3}); + } + } +} + TEST(BitmapAnd, Aligned) { std::shared_ptr left, right, out; int64_t length; diff --git a/cpp/src/arrow/util/bit-util.h b/cpp/src/arrow/util/bit-util.h index 3255ead08184..c012f7c4126f 100644 --- a/cpp/src/arrow/util/bit-util.h +++ b/cpp/src/arrow/util/bit-util.h @@ -85,11 +85,20 @@ class Status; namespace BitUtil { +// +// Utilities for reading and writing individual bits by their index +// in a memory area. +// + +// Bitmask selecting the k-th bit in a byte static constexpr uint8_t kBitmask[] = {1, 2, 4, 8, 16, 32, 64, 128}; -// the ~i byte version of kBitmaks +// the bitwise complement version of kBitmask static constexpr uint8_t kFlippedBitmask[] = {254, 253, 251, 247, 239, 223, 191, 127}; +// Bitmask selecting the (k - 1) preceding bits in a byte +static constexpr uint8_t kPrecedingBitmask[] = {0, 1, 3, 7, 15, 31, 63, 127}; + static inline int64_t CeilByte(int64_t size) { return (size + 7) & ~7; } static inline int64_t BytesForBits(int64_t size) { return CeilByte(size) / 8; } @@ -120,6 +129,8 @@ static inline void SetArrayBit(uint8_t* bits, int i, bool is_set) { static inline void SetBitTo(uint8_t* bits, int64_t i, bool bit_is_set) { // https://graphics.stanford.edu/~seander/bithacks.html // "Conditionally set or clear bits without branching" + // NOTE: this seems to confuse Valgrind as it reads from potentially + // uninitialized memory bits[i / 8] ^= static_cast(-static_cast(bit_is_set) ^ bits[i / 8]) & kBitmask[i % 8]; } @@ -448,14 +459,17 @@ class BitmapReader { }; class BitmapWriter { + // A sequential bitwise writer that preserves surrounding bit values. + public: BitmapWriter(uint8_t* bitmap, int64_t start_offset, int64_t length) : bitmap_(bitmap), position_(0), length_(length) { - current_byte_ = 0; byte_offset_ = start_offset / 8; bit_mask_ = static_cast(1 << (start_offset % 8)); if (length > 0) { current_byte_ = bitmap[byte_offset_]; + } else { + current_byte_ = 0; } } @@ -495,6 +509,60 @@ class BitmapWriter { int64_t byte_offset_; }; +class FirstTimeBitmapWriter { + // Like BitmapWriter, but any bit values *following* the bits written + // might be clobbered. It is hence faster than BitmapWriter, and can + // also avoid false positives with Valgrind. + + public: + FirstTimeBitmapWriter(uint8_t* bitmap, int64_t start_offset, int64_t length) + : bitmap_(bitmap), position_(0), length_(length) { + current_byte_ = 0; + byte_offset_ = start_offset / 8; + bit_mask_ = static_cast(1 << (start_offset % 8)); + if (length > 0) { + current_byte_ = bitmap[byte_offset_] & BitUtil::kPrecedingBitmask[start_offset % 8]; + } else { + current_byte_ = 0; + } + } + + void Set() { current_byte_ |= bit_mask_; } + + void Clear() {} + + void Next() { + bit_mask_ = static_cast(bit_mask_ << 1); + ++position_; + if (bit_mask_ == 0) { + // Finished this byte, need advancing + bit_mask_ = 0x01; + bitmap_[byte_offset_++] = current_byte_; + current_byte_ = 0; + } + } + + void Finish() { + // Store current byte if we didn't went past bitmap storage + if (bit_mask_ != 0x01 || position_ < length_) { + bitmap_[byte_offset_] = current_byte_; + } + } + + int64_t position() const { return position_; } + + private: + uint8_t* bitmap_; + int64_t position_; + int64_t length_; + + uint8_t current_byte_; + uint8_t bit_mask_; + int64_t byte_offset_; +}; + +// TODO: add a std::generate-like function for writing bitmaps? + } // namespace internal // ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/util/hash.h b/cpp/src/arrow/util/hash.h index 359734271638..3a444781574e 100644 --- a/cpp/src/arrow/util/hash.h +++ b/cpp/src/arrow/util/hash.h @@ -39,6 +39,9 @@ static constexpr double kMaxHashTableLoad = 0.5; namespace internal { +// TODO this ugliness should be rewritten as an inline function with +// a callable argument. + #define DOUBLE_TABLE_SIZE(SETUP_CODE, COMPUTE_HASH) \ do { \ int64_t new_size = hash_table_size_ * 2; \ diff --git a/cpp/valgrind.supp b/cpp/valgrind.supp new file mode 100644 index 000000000000..dc78eeab1a74 --- /dev/null +++ b/cpp/valgrind.supp @@ -0,0 +1,30 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +{ + # Casting to/from boolean might read uninitialized data as the null bitmap isn't considered + + Memcheck:Cond + fun:*CastFunctor*BooleanType* +} +{ + # Data can be uninitialized when its null bit is set, but we write raw buffers + + Memcheck:Param + write(buf) + fun:__write_nocancel +} From a6f0ae8782aaaf3849e656064c0a144570a8a4a1 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 30 May 2018 13:10:44 +0200 Subject: [PATCH 2/3] Remove most macro ugliness in DictionaryBuilder with template class helper --- cpp/src/arrow/builder.cc | 371 ++++++++++++++++-------------------- cpp/src/arrow/builder.h | 4 +- cpp/src/arrow/type_traits.h | 2 +- 3 files changed, 171 insertions(+), 206 deletions(-) diff --git a/cpp/src/arrow/builder.cc b/cpp/src/arrow/builder.cc index c11b04ab8b81..775117dfa9c3 100644 --- a/cpp/src/arrow/builder.cc +++ b/cpp/src/arrow/builder.cc @@ -898,35 +898,117 @@ using internal::DictionaryScalar; using internal::WrappedBinary; namespace { -// -// Fetch the value of a dictionary entry stored in a builder. - -// template ::BuilderType, -// typename Scalar = typename std::enable_if::is_primitive, -// typename T::c_type>::type> -// Scalar GetDictionaryValue(Builder& builder, int64_t index) { -// const Scalar* data = reinterpret_cast(builder.data()->data()); -// return data[index]; -// } -// -// template ::BuilderType, -// typename Scalar = typename std::enable_if::is_binary_like, -// WrappedBinary>::type> -// WrappedBinary GetDictionaryValue(Builder& builder, int64_t index) { -// int32_t v_len; -// const uint8_t* v = builder.GetValue(static_cast(index), &v_len); -// return WrappedBinary(v, v_len); -// } -// -// template ::BuilderType, -// typename Scalar = typename std::enable_if::value, const uint8_t*>::type> -// const uint8_t* GetDictionaryValue(Builder& dictionary_builder, int64_t index) { -// return dictionary_builder.GetValue(index); -// } + +// A helper class to manage a hash table embedded in a typed Builder. +template +struct DictionaryHashHelper {}; + +// DictionaryHashHelper implementation for primitive types +template +struct DictionaryHashHelper::is_primitive>::type> { + using Builder = typename TypeTraits::BuilderType; + using Scalar = typename DictionaryScalar::type; + + // Get the dictionary value at the given builder index + static Scalar GetDictionaryValue(const Builder& builder, int64_t index) { + const Scalar* data = reinterpret_cast(builder.data()->data()); + return data[index]; + } + + // Compute the hash of a scalar value + static int64_t HashValue(const Scalar& value, int byte_width) { + return HashUtil::Hash(&value, sizeof(Scalar), 0); + } + + // Return whether the dictionary value at the given builder index is unequal to value + static bool SlotDifferent(const Builder& builder, int64_t index, const Scalar& value) { + return GetDictionaryValue(builder, index) != value; + } + + // Append a value to the builder + static Status AppendValue(Builder& builder, const Scalar& value) { + return builder.Append(value); + } + + // Append another builder's contents to the builder + static Status AppendBuilder(Builder& builder, const Builder& source_builder) { + return builder.AppendValues( + reinterpret_cast(source_builder.data()->data()), + source_builder.length(), nullptr); + } +}; + +// DictionaryHashHelper implementation for StringType / BinaryType +template +struct DictionaryHashHelper< + T, typename std::enable_if::is_binary_like>::type> { + using Builder = typename TypeTraits::BuilderType; + using Scalar = typename DictionaryScalar::type; + + static Scalar GetDictionaryValue(const Builder& builder, int64_t index) { + int32_t v_length; + const uint8_t* v_ptr = builder.GetValue(index, &v_length); + return WrappedBinary(v_ptr, v_length); + } + + static int64_t HashValue(const Scalar& value, int byte_width) { + return HashUtil::Hash(value.ptr_, value.length_, 0); + } + + static bool SlotDifferent(const Builder& builder, int64_t index, const Scalar& value) { + int32_t other_length; + const uint8_t* other_ptr = builder.GetValue(index, &other_length); + return value.length_ != other_length || + memcmp(value.ptr_, other_ptr, other_length) != 0; + } + + static Status AppendValue(Builder& builder, const Scalar& value) { + return builder.Append(value.ptr_, value.length_); + } + + static Status AppendBuilder(Builder& builder, const Builder& source_builder) { + for (uint64_t index = 0, limit = source_builder.length(); index < limit; ++index) { + int32_t length; + const uint8_t* ptr = source_builder.GetValue(index, &length); + RETURN_NOT_OK(builder.Append(ptr, length)); + } + return Status::OK(); + } +}; + +// DictionaryHashHelper implementation for FixedSizeBinaryType +template <> +struct DictionaryHashHelper { + using Builder = typename TypeTraits::BuilderType; + using Scalar = typename DictionaryScalar::type; + + static Scalar GetDictionaryValue(const Builder& builder, int64_t index) { + return builder.GetValue(index); + } + + static int64_t HashValue(const Scalar& value, int byte_width) { + return HashUtil::Hash(value, byte_width, 0); + } + + static bool SlotDifferent(const Builder& builder, int64_t index, const uint8_t* value) { + const int32_t width = builder.byte_width(); + const uint8_t* other_value = builder.GetValue(index); + return memcmp(value, other_value, width) != 0; + } + + static Status AppendValue(Builder& builder, const Scalar& value) { + return builder.Append(value); + } + + static Status AppendBuilder(Builder& builder, const Builder& source_builder) { + for (uint64_t index = 0, limit = source_builder.length(); index < limit; ++index) { + const Scalar value = GetDictionaryValue(source_builder, index); + RETURN_NOT_OK(builder.Append(value)); + } + return Status::OK(); + } +}; } // namespace @@ -1014,6 +1096,37 @@ Status DictionaryBuilder::Resize(int64_t capacity) { } } +template +int64_t DictionaryBuilder::HashValue(const Scalar& value) { + return DictionaryHashHelper::HashValue(value, byte_width_); +} + +template +typename DictionaryBuilder::Scalar DictionaryBuilder::GetDictionaryValue( + typename TypeTraits::BuilderType& dictionary_builder, int64_t index) { + return DictionaryHashHelper::GetDictionaryValue(dictionary_builder, index); +} + +template +bool DictionaryBuilder::SlotDifferent(hash_slot_t index, const Scalar& value) { + DCHECK_GE(index, 0); + if (index >= entry_id_offset_) { + // Lookup delta dictionary + DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); + return DictionaryHashHelper::SlotDifferent( + dict_builder_, static_cast(index - entry_id_offset_), value); + } else { + DCHECK_LT(index, overflow_dict_builder_.length()); + return DictionaryHashHelper::SlotDifferent(overflow_dict_builder_, + static_cast(index), value); + } +} + +template +Status DictionaryBuilder::AppendDictionary(const Scalar& value) { + return DictionaryHashHelper::AppendValue(dict_builder_, value); +} + template Status DictionaryBuilder::Append(const Scalar& value) { RETURN_NOT_OK(Reserve(1)); @@ -1048,6 +1161,13 @@ Status DictionaryBuilder::Append(const Scalar& value) { return Status::OK(); } +template +Status DictionaryBuilder::AppendNull() { + return values_builder_.AppendNull(); +} + +Status DictionaryBuilder::AppendNull() { return values_builder_.AppendNull(); } + template Status DictionaryBuilder::AppendArray(const Array& array) { const auto& numeric_array = checked_cast&>(array); @@ -1085,24 +1205,8 @@ Status DictionaryBuilder::AppendArray(const Array& array) { return Status::OK(); } -template -Status DictionaryBuilder::AppendNull() { - return values_builder_.AppendNull(); -} - -Status DictionaryBuilder::AppendNull() { return values_builder_.AppendNull(); } - -template -int64_t DictionaryBuilder::HashValue(const Scalar& value) { - return HashUtil::Hash(&value, sizeof(Scalar), 0); -} - template Status DictionaryBuilder::DoubleTableSize() { - // const auto GetDictBuilderValue = [this](hash_slot_t index) -> Scalar { - // return GetDictionaryValue(dict_builder_, static_cast(index)); - // }; - #define INNER_LOOP \ int64_t j = HashValue(GetDictionaryValue(dict_builder_, index)) & new_mod_bitmask @@ -1111,40 +1215,13 @@ Status DictionaryBuilder::DoubleTableSize() { return Status::OK(); } -template -typename DictionaryBuilder::Scalar DictionaryBuilder::GetDictionaryValue( - typename TypeTraits::BuilderType& dictionary_builder, int64_t index) { - const Scalar* data = reinterpret_cast(dictionary_builder.data()->data()); - return data[index]; -} - -template -bool DictionaryBuilder::SlotDifferent(hash_slot_t index, const Scalar& value) { - DCHECK_GE(index, 0); - if (index >= entry_id_offset_) { - // Lookup delta dictionary - DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); - return GetDictionaryValue(dict_builder_, - static_cast(index - entry_id_offset_)) != value; - } else { - DCHECK_LT(index, overflow_dict_builder_.length()); - return GetDictionaryValue(overflow_dict_builder_, static_cast(index)) != - value; - } -} - -template -Status DictionaryBuilder::AppendDictionary(const Scalar& value) { - return dict_builder_.Append(value); -} - template Status DictionaryBuilder::FinishInternal(std::shared_ptr* out) { entry_id_offset_ += dict_builder_.length(); // Store current dict entries for further uses of this DictionaryBuilder - RETURN_NOT_OK(overflow_dict_builder_.AppendValues( - reinterpret_cast::Scalar*>(dict_builder_.data()->data()), - dict_builder_.length(), nullptr)); + RETURN_NOT_OK( + DictionaryHashHelper::AppendBuilder(overflow_dict_builder_, dict_builder_)); + DCHECK_EQ(entry_id_offset_, overflow_dict_builder_.length()); std::shared_ptr dictionary; RETURN_NOT_OK(dict_builder_.Finish(&dictionary)); @@ -1165,139 +1242,25 @@ Status DictionaryBuilder::FinishInternal(std::shared_ptr* o return Status::OK(); } -// -// FixedSizeBinaryType specializations -// - -template <> -const uint8_t* DictionaryBuilder::GetDictionaryValue( - typename TypeTraits::BuilderType& dictionary_builder, - int64_t index) { - return dictionary_builder.GetValue(index); -} - -template <> -Status DictionaryBuilder::FinishInternal( - std::shared_ptr* out) { - entry_id_offset_ += dict_builder_.length(); - - for (uint64_t index = 0, limit = dict_builder_.length(); index < limit; ++index) { - const Scalar value = GetDictionaryValue(dict_builder_, index); - RETURN_NOT_OK(overflow_dict_builder_.Append(value)); - } - - std::shared_ptr dictionary; - RETURN_NOT_OK(dict_builder_.Finish(&dictionary)); - - RETURN_NOT_OK(values_builder_.FinishInternal(out)); - (*out)->type = std::make_shared((*out)->type, dictionary); - - RETURN_NOT_OK(dict_builder_.Init(capacity_)); - RETURN_NOT_OK(values_builder_.Init(capacity_)); - - return Status::OK(); -} - -template <> -int64_t DictionaryBuilder::HashValue(const Scalar& value) { - return HashUtil::Hash(value, byte_width_, 0); -} - -template <> -bool DictionaryBuilder::SlotDifferent(hash_slot_t index, - const Scalar& value) { - const int32_t width = checked_cast(*type_).byte_width(); - - DCHECK_GE(index, 0); - Scalar other; - if (index >= entry_id_offset_) { - // Lookup delta dictionary - DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); - other = - GetDictionaryValue(dict_builder_, static_cast(index - entry_id_offset_)); - } else { - DCHECK_LT(index, overflow_dict_builder_.length()); - other = GetDictionaryValue(overflow_dict_builder_, static_cast(index)); - } - return memcmp(other, value, width) != 0; -} - // // StringType and BinaryType specializations // -#define BINARY_DICTIONARY_SPECIALIZATIONS(Type) \ - template <> \ - WrappedBinary DictionaryBuilder::GetDictionaryValue( \ - typename TypeTraits::BuilderType& dictionary_builder, int64_t index) { \ - int32_t v_len; \ - const uint8_t* v = dictionary_builder.GetValue( \ - static_cast(index - entry_id_offset_), &v_len); \ - return WrappedBinary(v, v_len); \ - } \ - \ - template <> \ - Status DictionaryBuilder::AppendDictionary(const WrappedBinary& value) { \ - return dict_builder_.Append(value.ptr_, value.length_); \ - } \ - \ - template <> \ - Status DictionaryBuilder::AppendArray(const Array& array) { \ - const BinaryArray& binary_array = checked_cast(array); \ - WrappedBinary value(nullptr, 0); \ - for (int64_t i = 0; i < array.length(); i++) { \ - if (array.IsNull(i)) { \ - RETURN_NOT_OK(AppendNull()); \ - } else { \ - value.ptr_ = binary_array.GetValue(i, &value.length_); \ - RETURN_NOT_OK(Append(value)); \ - } \ - } \ - return Status::OK(); \ - } \ - \ - template <> \ - int64_t DictionaryBuilder::HashValue(const WrappedBinary& value) { \ - return HashUtil::Hash(value.ptr_, value.length_, 0); \ - } \ - \ - template <> \ - bool DictionaryBuilder::SlotDifferent(hash_slot_t index, \ - const WrappedBinary& value) { \ - DCHECK_GE(index, 0); \ - const uint8_t* other_value; \ - int32_t other_length; \ - if (index >= entry_id_offset_) { \ - DCHECK_LT(index - entry_id_offset_, dict_builder_.length()); \ - other_value = dict_builder_.GetValue( \ - static_cast(index - entry_id_offset_), &other_length); \ - } else { \ - DCHECK_LT(index, overflow_dict_builder_.length()); \ - other_value = \ - overflow_dict_builder_.GetValue(static_cast(index), &other_length); \ - } \ - return other_length != value.length_ || \ - memcmp(other_value, value.ptr_, value.length_) != 0; \ - } \ - \ - template <> \ - Status DictionaryBuilder::FinishInternal(std::shared_ptr* out) { \ - entry_id_offset_ += dict_builder_.length(); \ - for (uint64_t index = 0, limit = dict_builder_.length(); index < limit; ++index) { \ - int32_t out_length; \ - const uint8_t* value = dict_builder_.GetValue(index, &out_length); \ - RETURN_NOT_OK(overflow_dict_builder_.Append(value, out_length)); \ - } \ - \ - std::shared_ptr dictionary; \ - RETURN_NOT_OK(dict_builder_.Finish(&dictionary)); \ - \ - RETURN_NOT_OK(values_builder_.FinishInternal(out)); \ - (*out)->type = std::make_shared((*out)->type, dictionary); \ - \ - RETURN_NOT_OK(dict_builder_.Init(capacity_)); \ - RETURN_NOT_OK(values_builder_.Init(capacity_)); \ - return Status::OK(); \ +#define BINARY_DICTIONARY_SPECIALIZATIONS(Type) \ + \ + template <> \ + Status DictionaryBuilder::AppendArray(const Array& array) { \ + const BinaryArray& binary_array = checked_cast(array); \ + WrappedBinary value(nullptr, 0); \ + for (int64_t i = 0; i < array.length(); i++) { \ + if (array.IsNull(i)) { \ + RETURN_NOT_OK(AppendNull()); \ + } else { \ + value.ptr_ = binary_array.GetValue(i, &value.length_); \ + RETURN_NOT_OK(Append(value)); \ + } \ + } \ + return Status::OK(); \ } BINARY_DICTIONARY_SPECIALIZATIONS(StringType); diff --git a/cpp/src/arrow/builder.h b/cpp/src/arrow/builder.h index d504cea926b3..bab5764d9c9e 100644 --- a/cpp/src/arrow/builder.h +++ b/cpp/src/arrow/builder.h @@ -837,6 +837,8 @@ class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder { /// \return size of values buffer so far int64_t value_data_length() const { return byte_builder_.length(); } + int32_t byte_width() const { return byte_width_; } + /// Temporary access to a value. /// /// This pointer becomes invalid on the next modifying operation. @@ -940,7 +942,7 @@ struct DictionaryScalar { template <> struct DictionaryScalar { - using type = uint8_t const*; + using type = const uint8_t*; }; } // namespace internal diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index 35bd226e46c9..f44b49584045 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -270,7 +270,7 @@ struct TypeTraits { using BuilderType = Decimal128Builder; constexpr static bool is_parameter_free = false; constexpr static bool is_binary_like = false; - constexpr static bool is_primitive = true; + constexpr static bool is_primitive = false; }; template <> From 89abac26f6967992620aad25fead25f00e91e296 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 31 May 2018 16:22:41 +0200 Subject: [PATCH 3/3] Use existing predicates instead of inventing new ones --- cpp/src/arrow/builder.cc | 10 +- cpp/src/arrow/compute/kernels/util-internal.h | 57 --------- cpp/src/arrow/type_traits.h | 117 ++++++++++-------- 3 files changed, 69 insertions(+), 115 deletions(-) diff --git a/cpp/src/arrow/builder.cc b/cpp/src/arrow/builder.cc index 775117dfa9c3..65018dee3267 100644 --- a/cpp/src/arrow/builder.cc +++ b/cpp/src/arrow/builder.cc @@ -905,8 +905,7 @@ struct DictionaryHashHelper {}; // DictionaryHashHelper implementation for primitive types template -struct DictionaryHashHelper::is_primitive>::type> { +struct DictionaryHashHelper> { using Builder = typename TypeTraits::BuilderType; using Scalar = typename DictionaryScalar::type; @@ -941,8 +940,7 @@ struct DictionaryHashHelper -struct DictionaryHashHelper< - T, typename std::enable_if::is_binary_like>::type> { +struct DictionaryHashHelper> { using Builder = typename TypeTraits::BuilderType; using Scalar = typename DictionaryScalar::type; @@ -978,8 +976,8 @@ struct DictionaryHashHelper< }; // DictionaryHashHelper implementation for FixedSizeBinaryType -template <> -struct DictionaryHashHelper { +template +struct DictionaryHashHelper> { using Builder = typename TypeTraits::BuilderType; using Scalar = typename DictionaryScalar::type; diff --git a/cpp/src/arrow/compute/kernels/util-internal.h b/cpp/src/arrow/compute/kernels/util-internal.h index bde267629f30..acfcb4bb79cf 100644 --- a/cpp/src/arrow/compute/kernels/util-internal.h +++ b/cpp/src/arrow/compute/kernels/util-internal.h @@ -29,63 +29,6 @@ namespace compute { class FunctionContext; -template -using is_number = std::is_base_of; - -template -struct has_c_type { - static constexpr bool value = - (std::is_base_of::value || std::is_base_of::value || - std::is_base_of::value || std::is_base_of::value); -}; - -template -struct is_8bit_int { - static constexpr bool value = - (std::is_same::value || std::is_same::value); -}; - -template -using enable_if_8bit_int = typename std::enable_if::value>::type; - -template -using enable_if_primitive_ctype = - typename std::enable_if::value>::type; - -template -using enable_if_date = typename std::enable_if::value>::type; - -template -using enable_if_time = typename std::enable_if::value>::type; - -template -using enable_if_timestamp = - typename std::enable_if::value>::type; - -template -using enable_if_has_c_type = typename std::enable_if::value>::type; - -template -using enable_if_null = typename std::enable_if::value>::type; - -template -using enable_if_binary = - typename std::enable_if::value>::type; - -template -using enable_if_boolean = - typename std::enable_if::value>::type; - -template -using enable_if_fixed_size_binary = - typename std::enable_if::value>::type; - -template -using enable_if_list = typename std::enable_if::value>::type; - -template -using enable_if_number = typename std::enable_if::value>::type; - template inline const T* GetValues(const ArrayData& data, int i) { return reinterpret_cast(data.buffers[i]->data()) + data.offset; diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index f44b49584045..331c349ec46c 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -26,6 +26,10 @@ namespace arrow { +// +// Per-type type traits +// + template struct TypeTraits {}; @@ -34,8 +38,6 @@ struct TypeTraits { using ArrayType = NullArray; using BuilderType = NullBuilder; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; }; template <> @@ -45,8 +47,6 @@ struct TypeTraits { using TensorType = UInt8Tensor; static inline int64_t bytes_required(int64_t elements) { return elements; } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint8(); } }; @@ -57,8 +57,6 @@ struct TypeTraits { using TensorType = Int8Tensor; static inline int64_t bytes_required(int64_t elements) { return elements; } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int8(); } }; @@ -72,8 +70,6 @@ struct TypeTraits { return elements * sizeof(uint16_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint16(); } }; @@ -87,8 +83,6 @@ struct TypeTraits { return elements * sizeof(int16_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int16(); } }; @@ -102,8 +96,6 @@ struct TypeTraits { return elements * sizeof(uint32_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint32(); } }; @@ -117,8 +109,6 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int32(); } }; @@ -132,8 +122,6 @@ struct TypeTraits { return elements * sizeof(uint64_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return uint64(); } }; @@ -147,8 +135,6 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return int64(); } }; @@ -161,8 +147,6 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return date64(); } }; @@ -175,8 +159,6 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return date32(); } }; @@ -189,8 +171,6 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; }; template <> @@ -202,8 +182,6 @@ struct TypeTraits { return elements * sizeof(int32_t); } constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; }; template <> @@ -215,8 +193,6 @@ struct TypeTraits { return elements * sizeof(int64_t); } constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; }; template <> @@ -229,8 +205,6 @@ struct TypeTraits { return elements * sizeof(uint16_t); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float16(); } }; @@ -244,8 +218,6 @@ struct TypeTraits { return static_cast(elements * sizeof(float)); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float32(); } }; @@ -259,8 +231,6 @@ struct TypeTraits { return static_cast(elements * sizeof(double)); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return float64(); } }; @@ -269,8 +239,6 @@ struct TypeTraits { using ArrayType = Decimal128Array; using BuilderType = Decimal128Builder; constexpr static bool is_parameter_free = false; - constexpr static bool is_binary_like = false; - constexpr static bool is_primitive = false; }; template <> @@ -282,8 +250,6 @@ struct TypeTraits { return BitUtil::BytesForBits(elements); } constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = true; - constexpr static bool is_binary_like = false; static inline std::shared_ptr type_singleton() { return boolean(); } }; @@ -292,8 +258,6 @@ struct TypeTraits { using ArrayType = StringArray; using BuilderType = StringBuilder; constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = true; static inline std::shared_ptr type_singleton() { return utf8(); } }; @@ -302,8 +266,6 @@ struct TypeTraits { using ArrayType = BinaryArray; using BuilderType = BinaryBuilder; constexpr static bool is_parameter_free = true; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = true; static inline std::shared_ptr type_singleton() { return binary(); } }; @@ -312,8 +274,6 @@ struct TypeTraits { using ArrayType = FixedSizeBinaryArray; using BuilderType = FixedSizeBinaryBuilder; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = false; }; template <> @@ -321,8 +281,6 @@ struct TypeTraits { using ArrayType = ListArray; using BuilderType = ListBuilder; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = false; }; template <> @@ -330,26 +288,81 @@ struct TypeTraits { using ArrayType = StructArray; using BuilderType = StructBuilder; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = false; }; template <> struct TypeTraits { using ArrayType = UnionArray; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = false; }; template <> struct TypeTraits { using ArrayType = DictionaryArray; constexpr static bool is_parameter_free = false; - constexpr static bool is_primitive = false; - constexpr static bool is_binary_like = false; }; +// +// Useful type predicates +// + +template +using is_number = std::is_base_of; + +template +struct has_c_type { + static constexpr bool value = + (std::is_base_of::value || std::is_base_of::value || + std::is_base_of::value || std::is_base_of::value); +}; + +template +struct is_8bit_int { + static constexpr bool value = + (std::is_same::value || std::is_same::value); +}; + +template +using enable_if_8bit_int = typename std::enable_if::value>::type; + +template +using enable_if_primitive_ctype = + typename std::enable_if::value>::type; + +template +using enable_if_date = typename std::enable_if::value>::type; + +template +using enable_if_time = typename std::enable_if::value>::type; + +template +using enable_if_timestamp = + typename std::enable_if::value>::type; + +template +using enable_if_has_c_type = typename std::enable_if::value>::type; + +template +using enable_if_null = typename std::enable_if::value>::type; + +template +using enable_if_binary = + typename std::enable_if::value>::type; + +template +using enable_if_boolean = + typename std::enable_if::value>::type; + +template +using enable_if_fixed_size_binary = + typename std::enable_if::value>::type; + +template +using enable_if_list = typename std::enable_if::value>::type; + +template +using enable_if_number = typename std::enable_if::value>::type; + namespace detail { // Not all type classes have a c_type