diff --git a/velox/buffer/Buffer.cpp b/velox/buffer/Buffer.cpp index 2152d3f80d9f..806959295401 100644 --- a/velox/buffer/Buffer.cpp +++ b/velox/buffer/Buffer.cpp @@ -61,9 +61,7 @@ BufferPtr Buffer::slice( size_t offset, size_t length, memory::MemoryPool* pool) { - if (!buffer) { - return nullptr; - } + VELOX_CHECK_NOT_NULL(buffer, "Buffer must not be null."); if (offset % 8 == 0) { return sliceBufferZeroCopy( diff --git a/velox/buffer/Buffer.h b/velox/buffer/Buffer.h index e880b9c4b5c4..09778f4c0d14 100644 --- a/velox/buffer/Buffer.h +++ b/velox/buffer/Buffer.h @@ -181,8 +181,7 @@ class Buffer { /// Otherwise return a BufferView into the original buffer (with shared /// ownership of original buffer). /// - /// @param buffer A pointer to the buffer to be sliced. If this is null, the - /// function returns a null pointer. + /// @param buffer A pointer to the buffer to be sliced. Must not be null. /// @param offset The element position in the buffer where the slice begins. /// Must be less or equal than the buffer size. /// @param length The number of elements to include in the slice. Must be @@ -195,9 +194,7 @@ class Buffer { size_t offset, size_t length, memory::MemoryPool* pool) { - if (!buffer) { - return nullptr; - } + VELOX_CHECK_NOT_NULL(buffer, "Buffer must not be null."); return sliceBufferZeroCopy( sizeof(T), is_pod_like_v, buffer, offset, length); } @@ -698,5 +695,6 @@ class BufferView : public Buffer { Releaser const releaser_; }; + } // namespace velox } // namespace facebook diff --git a/velox/buffer/tests/BufferTest.cpp b/velox/buffer/tests/BufferTest.cpp index 39c84533d1d1..902d578c1ceb 100644 --- a/velox/buffer/tests/BufferTest.cpp +++ b/velox/buffer/tests/BufferTest.cpp @@ -466,6 +466,9 @@ TEST_F(BufferTest, sliceBigintBuffer) { VELOX_ASSERT_THROW( Buffer::slice(bufferPtr, 5, 6, pool_.get()), "Length must be less than or equal to 5."); + VELOX_ASSERT_THROW( + Buffer::slice(nullptr, 5, 6, pool_.get()), + "Buffer must not be null."); } TEST_F(BufferTest, sliceBooleanBuffer) { diff --git a/velox/vector/BaseVector.h b/velox/vector/BaseVector.h index 8420a1e3ba79..705c11dc9cb4 100644 --- a/velox/vector/BaseVector.h +++ b/velox/vector/BaseVector.h @@ -883,7 +883,7 @@ class BaseVector { } BufferPtr sliceNulls(vector_size_t offset, vector_size_t length) const { - return Buffer::slice(nulls_, offset, length, pool_); + return nulls_ ? Buffer::slice(nulls_, offset, length, pool_) : nulls_; } TypePtr type_; diff --git a/velox/vector/ComplexVector.cpp b/velox/vector/ComplexVector.cpp index 3d0ef74c721d..1ef5f5d29c0f 100644 --- a/velox/vector/ComplexVector.cpp +++ b/velox/vector/ComplexVector.cpp @@ -1186,8 +1186,10 @@ VectorPtr ArrayVector::slice(vector_size_t offset, vector_size_t length) const { type_, sliceNulls(offset, length), length, - Buffer::slice(offsets_, offset, length, pool_), - Buffer::slice(sizes_, offset, length, pool_), + offsets_ ? Buffer::slice(offsets_, offset, length, pool_) + : offsets_, + sizes_ ? Buffer::slice(sizes_, offset, length, pool_) + : sizes_, elements_); } @@ -1485,8 +1487,10 @@ VectorPtr MapVector::slice(vector_size_t offset, vector_size_t length) const { type_, sliceNulls(offset, length), length, - Buffer::slice(offsets_, offset, length, pool_), - Buffer::slice(sizes_, offset, length, pool_), + offsets_ ? Buffer::slice(offsets_, offset, length, pool_) + : offsets_, + sizes_ ? Buffer::slice(sizes_, offset, length, pool_) + : sizes_, keys_, values_); } diff --git a/velox/vector/DictionaryVector-inl.h b/velox/vector/DictionaryVector-inl.h index 562ee06a6a4f..fe1a6fed756e 100644 --- a/velox/vector/DictionaryVector-inl.h +++ b/velox/vector/DictionaryVector-inl.h @@ -185,7 +185,9 @@ VectorPtr DictionaryVector::slice(vector_size_t offset, vector_size_t length) this->sliceNulls(offset, length), length, valueVector(), - Buffer::slice(indices_, offset, length, this->pool_)); + indices_ + ? Buffer::slice(indices_, offset, length, this->pool_) + : indices_); } template diff --git a/velox/vector/FlatVector-inl.h b/velox/vector/FlatVector-inl.h index 3ab6f1ced798..8551d8d8839b 100644 --- a/velox/vector/FlatVector-inl.h +++ b/velox/vector/FlatVector-inl.h @@ -423,7 +423,8 @@ VectorPtr FlatVector::slice(vector_size_t offset, vector_size_t length) this->type_, this->sliceNulls(offset, length), length, - Buffer::slice(values_, offset, length, this->pool_), + values_ ? Buffer::slice(values_, offset, length, this->pool_) + : values_, std::vector(stringBuffers_)); }