Skip to content

Commit

Permalink
Merge pull request #1953 from kuzudb/bool-bitpacking-fix
Browse files Browse the repository at this point in the history
Fix bool column chunk buffer size
  • Loading branch information
ray6080 committed Aug 28, 2023
2 parents feabe82 + 62d50f3 commit a1041bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
24 changes: 10 additions & 14 deletions src/include/storage/copier/column_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ class BoolColumnChunk : public ColumnChunk {

void append(ColumnChunk* other, common::offset_t startPosInOtherChunk,
common::offset_t startPosInChunk, uint32_t numValuesToAppend) final;

void resize(uint64_t capacity) final;

protected:
inline uint64_t numBytesForValues(common::offset_t numValues) const {
// 8 values per byte, and we need a buffer size which is a multiple of 8 bytes
return ceil(numValues / 8.0 / 8.0) * 8;
}

void initialize(common::offset_t capacity) final;
};

class NullColumnChunk : public BoolColumnChunk {
Expand All @@ -195,21 +205,7 @@ class NullColumnChunk : public BoolColumnChunk {
inline bool isNull(common::offset_t pos) const { return getValue<bool>(pos); }
inline void setNull(common::offset_t pos, bool isNull) { setValue(isNull, pos); }

void resize(uint64_t numValues) final;

inline void resetNullBuffer() { memset(buffer.get(), 0 /* non null */, bufferSize); }

protected:
inline uint64_t numBytesForValues(common::offset_t numValues) const {
// 8 values per byte, and we need a buffer size which is a multiple of 8 bytes
return ceil(numValues / 8.0 / 8.0) * 8;
}
inline void initialize(common::offset_t numValues) final {
numBytesPerValue = 0;
bufferSize = numBytesForValues(numValues);
// Each byte defaults to 0, indicating everything is non-null
buffer = std::make_unique<uint8_t[]>(bufferSize);
}
};

class FixedListColumnChunk : public ColumnChunk {
Expand Down
16 changes: 14 additions & 2 deletions src/storage/copier/column_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,26 @@ void ColumnChunk::copyVectorToBuffer(
}
}

void NullColumnChunk::resize(uint64_t numValues) {
auto numBytesAfterResize = numBytesForValues(numValues);
inline void BoolColumnChunk::initialize(common::offset_t capacity) {
numBytesPerValue = 0;
bufferSize = numBytesForValues(capacity);
buffer = std::make_unique<uint8_t[]>(bufferSize);
if (nullChunk) {
static_cast<BoolColumnChunk*>(nullChunk.get())->initialize(capacity);
}
}

void BoolColumnChunk::resize(uint64_t capacity) {
auto numBytesAfterResize = numBytesForValues(capacity);
assert(numBytesAfterResize > bufferSize);
auto reservedBuffer = std::make_unique<uint8_t[]>(numBytesAfterResize);
memset(reservedBuffer.get(), 0 /* non null */, numBytesAfterResize);
memcpy(reservedBuffer.get(), buffer.get(), bufferSize);
buffer = std::move(reservedBuffer);
bufferSize = numBytesAfterResize;
if (nullChunk) {
nullChunk->resize(capacity);
}
}

} // namespace storage
Expand Down

0 comments on commit a1041bf

Please sign in to comment.