Skip to content

Commit 890a6dc

Browse files
committed
Simpler fix for ARROW-6861
1 parent 6f2c904 commit 890a6dc

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

cpp/src/arrow/array/builder_dict.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,6 @@ class DictionaryBuilderBase : public ArrayBuilder {
239239
Status Resize(int64_t capacity) override {
240240
ARROW_RETURN_NOT_OK(CheckCapacity(capacity, capacity_));
241241
capacity = std::max(capacity, kMinBuilderCapacity);
242-
243-
if (capacity_ == 0) {
244-
// Initialize hash table
245-
// XXX should we let the user pass additional size heuristics?
246-
delta_offset_ = 0;
247-
}
248242
ARROW_RETURN_NOT_OK(indices_builder_.Resize(capacity));
249243
capacity_ = indices_builder_.capacity();
250244
return Status::OK();
@@ -266,6 +260,10 @@ class DictionaryBuilderBase : public ArrayBuilder {
266260

267261
// Update internals for further uses of this DictionaryBuilder
268262
delta_offset_ = memo_table_->size();
263+
264+
// ARROW-6861: Manually set capacity/length/null count until we can fix up
265+
// Reset to not reset the memo table in ARROW-6869
266+
capacity_ = length_ = null_count_ = 0;
269267
indices_builder_.Reset();
270268

271269
return Status::OK();
@@ -380,6 +378,7 @@ class DictionaryBuilder : public internal::DictionaryBuilderBase<AdaptiveIntBuil
380378
const uint8_t* valid_bytes = NULLPTR) {
381379
int64_t null_count_before = this->indices_builder_.null_count();
382380
ARROW_RETURN_NOT_OK(this->indices_builder_.AppendValues(values, length, valid_bytes));
381+
this->capacity_ = this->indices_builder_.capacity();
383382
this->length_ += length;
384383
this->null_count_ += this->indices_builder_.null_count() - null_count_before;
385384
return Status::OK();
@@ -402,6 +401,7 @@ class Dictionary32Builder : public internal::DictionaryBuilderBase<Int32Builder,
402401
const uint8_t* valid_bytes = NULLPTR) {
403402
int64_t null_count_before = this->indices_builder_.null_count();
404403
ARROW_RETURN_NOT_OK(this->indices_builder_.AppendValues(values, length, valid_bytes));
404+
this->capacity_ = this->indices_builder_.capacity();
405405
this->length_ += length;
406406
this->null_count_ += this->indices_builder_.null_count() - null_count_before;
407407
return Status::OK();

cpp/src/arrow/array_dict_test.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,46 @@ TYPED_TEST(TestDictionaryBuilder, Dictionary32_BasicPrimitive) {
304304
ASSERT_TRUE(expected.Equals(result));
305305
}
306306

307+
TYPED_TEST(TestDictionaryBuilder, FinishResetBehavior) {
308+
// ARROW-6861
309+
using c_type = typename TypeParam::c_type;
310+
auto type = std::make_shared<TypeParam>();
311+
312+
Dictionary32Builder<TypeParam> builder;
313+
314+
ASSERT_OK(builder.Append(static_cast<c_type>(1)));
315+
ASSERT_OK(builder.AppendNull());
316+
ASSERT_OK(builder.Append(static_cast<c_type>(1)));
317+
ASSERT_OK(builder.Append(static_cast<c_type>(2)));
318+
319+
// Properties from indices_builder propagated
320+
ASSERT_LT(0, builder.capacity());
321+
ASSERT_LT(0, builder.null_count());
322+
ASSERT_EQ(4, builder.length());
323+
324+
std::shared_ptr<Array> result;
325+
ASSERT_OK(builder.Finish(&result));
326+
327+
// Everything reset
328+
ASSERT_EQ(0, builder.capacity());
329+
ASSERT_EQ(0, builder.length());
330+
ASSERT_EQ(0, builder.null_count());
331+
}
332+
333+
TEST(TestDictionaryBuilderAdHoc, AppendIndicesUpdateCapacity) {
334+
DictionaryBuilder<Int32Type> builder;
335+
Dictionary32Builder<Int32Type> builder32;
336+
337+
std::vector<int32_t> indices_i32 = {0, 1, 2};
338+
std::vector<int64_t> indices_i64 = {0, 1, 2};
339+
340+
ASSERT_OK(builder.AppendIndices(indices_i64.data(), 3));
341+
ASSERT_OK(builder32.AppendIndices(indices_i32.data(), 3));
342+
343+
ASSERT_LT(0, builder.capacity());
344+
ASSERT_LT(0, builder32.capacity());
345+
}
346+
307347
TEST(TestStringDictionaryBuilder, Basic) {
308348
// Build the dictionary Array
309349
StringDictionaryBuilder builder;

0 commit comments

Comments
 (0)