Skip to content

Commit

Permalink
Fix the incorrect size calculation logic of FileSink.
Browse files Browse the repository at this point in the history
  • Loading branch information
wypb committed Apr 10, 2024
1 parent 89de5d3 commit c8521c6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
6 changes: 4 additions & 2 deletions velox/dwio/common/FileSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ void FileSink::writeImpl(
DWIO_ENSURE(!isClosed(), "Cannot write to closed sink.");
uint64_t size = 0;
for (auto& buf : buffers) {
size += callback(buf);
auto writtenSize = callback(buf);
size += writtenSize;
size_ += writtenSize;
}
size_ += size;

if (stats_ != nullptr) {
stats_->incRawBytesWritten(size);
}
Expand Down
1 change: 1 addition & 0 deletions velox/dwio/common/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_executable(
ExecutorBarrierTest.cpp
OnDemandUnitLoaderTests.cpp
LocalFileSinkTest.cpp
MemorySinkTest.cpp
LoggedExceptionTest.cpp
ParallelForTest.cpp
RangeTests.cpp
Expand Down
58 changes: 58 additions & 0 deletions velox/dwio/common/tests/MemorySinkTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/

#include "velox/dwio/common/FileSink.h"

#include <gtest/gtest.h>

namespace facebook::velox::dwio::common {

class MemorySinkTest : public testing::Test {
protected:
static void SetUpTestCase() {
memory::MemoryManager::testingSetInstance({});
}
std::shared_ptr<velox::memory::MemoryPool> pool_{
memory::memoryManager()->addLeafPool()};
};

TEST_F(MemorySinkTest, create) {
std::string chars("abcdefghijklmnopqrst");
std::vector<DataBuffer<char>> buffers;

buffers.emplace_back(*pool_);
for (auto i = 0; i < 10; ++i) {
buffers.back().append(chars[i]);
}

buffers.emplace_back(*pool_);
for (auto i = 10; i < chars.length(); ++i) {
buffers.back().append(chars[i]);
}

EXPECT_EQ(buffers.size(), 2);

auto memorySink = std::make_unique<MemorySink>(
1024, dwio::common::FileSink::Options{.pool = pool_.get()});

ASSERT_TRUE(memorySink->isBuffered());
// Write data to MemorySink.
memorySink->write(buffers);
EXPECT_EQ(memorySink->size(), chars.length());
EXPECT_EQ(memorySink->data(), chars);
memorySink->close();
}
} // namespace facebook::velox::dwio::common

0 comments on commit c8521c6

Please sign in to comment.