Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'Write past Buffer capacity()' Bug #139

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions velox/vector/BaseVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,12 @@ void BaseVector::ensureWritable(
// The copy-on-write size is the max of the writable row set and the
// vector.
//
// If the vector is constant, this logic is tricky: we first try to use the
// vector size (in case the size is not set to kMaxElements), otherwise we
// fallback to the 'rows' size. We don't use rows.size() inadvertently because
// in many cases this function is used with SelectivityVector::empty().
auto targetSize = (*result)->isConstantEncoding()
? (((*result)->size() != kMaxElements) ? (*result)->size() : rows.size())
: (std::max<vector_size_t>(rows.size(), (*result)->size()));
// If the vector size is kMaxElements then this is a constant vector's special
// case. In this case we use the selectivity vector size no matter if its
// empty. Otherwise we use the larger one as target size.
auto targetSize = (*result)->size() == kMaxElements
? rows.size()
: std::max<vector_size_t>(rows.size(), (*result)->size());

auto copy =
BaseVector::create(isUnknownType ? type : resultType, targetSize, pool);
Expand Down
33 changes: 33 additions & 0 deletions velox/vector/tests/EnsureWritableVectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,39 @@ TEST_F(EnsureWritableVectorTest, constant) {
EXPECT_EQ(size, constant->size());
}

// If constant has smaller size, check that we follow the selectivity vector
// size.
{
const vector_size_t selectivityVectorSize = 100;
auto constant = BaseVector::createConstant(
variant::create<TypeKind::BIGINT>(123), 1, pool_.get());
BaseVector::ensureWritable(
SelectivityVector::empty(selectivityVectorSize),
BIGINT(),
pool_.get(),
&constant);
EXPECT_EQ(VectorEncoding::Simple::FLAT, constant->encoding());
EXPECT_EQ(selectivityVectorSize, constant->size());
}

// If constant has larger size, check that we follow the constant vector
// size.
{
const vector_size_t selectivityVectorSize = 100;
const vector_size_t constantVectorSize = 200;
auto constant = BaseVector::createConstant(
variant::create<TypeKind::BIGINT>(123),
constantVectorSize,
pool_.get());
BaseVector::ensureWritable(
SelectivityVector::empty(selectivityVectorSize),
BIGINT(),
pool_.get(),
&constant);
EXPECT_EQ(VectorEncoding::Simple::FLAT, constant->encoding());
EXPECT_EQ(constantVectorSize, constant->size());
}

// If constant has max size, check that we follow the selectivity vector size.
{
const vector_size_t selectivityVectorSize = 100;
Expand Down