github-actions[bot] commented on code in PR #15399: URL: https://github.com/apache/doris/pull/15399#discussion_r1059029410
########## be/src/vec/core/block_spill_reader.cpp: ########## @@ -0,0 +1,98 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 "vec/core/block_spill_reader.h" + +#include "io/file_factory.h" +#include "io/fs/file_system.h" +#include "olap/iterators.h" + +namespace doris { +namespace vectorized { +Status BlockSpillReader::_init() { + std::unique_ptr<io::FileSystem> file_system; + FileSystemProperties system_properties; + system_properties.system_type = TFileType::FILE_LOCAL; + + FileDescription file_description; + file_description.path = _file_path; + + RETURN_IF_ERROR(FileFactory::create_file_reader(nullptr, system_properties, file_description, + &file_system, &_file_reader)); + + size_t file_size = _file_reader->size(); + + std::unique_ptr<char[]> buff(new char[sizeof(size_t)]); + Slice result(buff.get(), sizeof(size_t)); + + // read block count + size_t bytes_read = 0; + RETURN_IF_ERROR(_file_reader->read_at(file_size - sizeof(size_t), result, {}, &bytes_read)); + _block_count = *((size_t*)result.data); + + // read block start offsets + size_t read_offset = file_size - (_block_count + 1) * sizeof(size_t); + buff.reset(new char[_block_count * sizeof(size_t)]); + result.data = buff.get(); + result.size = _block_count * sizeof(size_t); + + RETURN_IF_ERROR(_file_reader->read_at(read_offset, result, {}, &bytes_read)); + DCHECK(bytes_read == _block_count * sizeof(size_t)); + + _block_start_offsets.resize(_block_count + 1); + for (size_t i = 0; i < _block_count; ++i) { + _block_start_offsets[i] = *(size_t*)(result.data + i * sizeof(size_t)); + } + _block_start_offsets[_block_count] = file_size - (_block_count + 1) * sizeof(size_t); + + return Status::OK(); +} + +Status BlockSpillReader::read(Block& block, bool& eof) { + eof = false; + if (!_file_reader) { + RETURN_IF_ERROR(_init()); + } + + if (_read_block_index >= _block_count) { + eof = true; + return Status::OK(); + } + + size_t bytes_to_read = + _block_start_offsets[_read_block_index + 1] - _block_start_offsets[_read_block_index]; + std::unique_ptr<char[]> buff(new char[bytes_to_read]); + Slice result(buff.get(), bytes_to_read); + + size_t bytes_read = 0; + RETURN_IF_ERROR(_file_reader->read_at(_block_start_offsets[_read_block_index], result, {}, + &bytes_read)); + DCHECK(bytes_read == bytes_to_read); + + PBlock pb_block; + if (!pb_block.ParseFromArray(result.data, result.size)) { + return Status::InternalError("Failed to read spilled block"); + } + block = std::move(Block(pb_block)); Review Comment: warning: moving a temporary object prevents copy elision [clang-diagnostic-pessimizing-move] ```cpp block = std::move(Block(pb_block)); ^ ``` **be/src/vec/core/block_spill_reader.cpp:89:** remove std::move call here ```cpp block = std::move(Block(pb_block)); ^ ``` ########## be/src/vec/core/block_spill_reader.h: ########## @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#pragma once + +#include "io/fs/file_reader.h" +#include "vec/core/block.h" + +namespace doris { +namespace vectorized { +// Read data spilled to local file. +class BlockSpillReader { +public: + BlockSpillReader(const std::string& file_path) : _file_path(file_path) {} + + // read a small block, set eof to true if no more data to read + Status read(Block& block, bool& eof); + + Status close() { + _file_reader.reset(); + return Status::OK(); + } + +private: + Status _init(); + +private: Review Comment: warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers] ```suggestion ``` **be/src/vec/core/block_spill_reader.h:37:** previously declared here ```cpp private: ^ ``` ########## be/test/vec/core/block_spill_test.cpp: ########## @@ -0,0 +1,422 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 <gtest/gtest.h> + +#include "runtime/block_spill_manager.h" +#include "util/file_utils.h" +#include "vec/columns/column_array.h" +#include "vec/columns/column_decimal.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_array.h" +#include "vec/data_types/data_type_bitmap.h" +#include "vec/data_types/data_type_date.h" +#include "vec/data_types/data_type_date_time.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" + +namespace doris { +static const uint32_t MAX_PATH_LEN = 1024; + +static const std::string TMP_DATA_DIR = "block_spill_test"; + +std::string test_data_dir; +std::shared_ptr<BlockSpillManager> block_spill_manager; + +class TestBlockSpill : public testing::Test { +public: + static void SetUpTestSuite() { + char buffer[MAX_PATH_LEN]; + EXPECT_NE(getcwd(buffer, MAX_PATH_LEN), nullptr); + test_data_dir = std::string(buffer) + "/" + TMP_DATA_DIR; + std::cout << "test data dir: " << test_data_dir << "\n"; + FileUtils::remove_all(test_data_dir); + FileUtils::create_dir(test_data_dir); + + std::vector<StorePath> paths; + paths.emplace_back(test_data_dir, -1); + block_spill_manager = std::make_shared<BlockSpillManager>(paths); + } + + static void TearDownTestSuite() { FileUtils::remove_all(test_data_dir); } + +protected: + void SetUp() {} Review Comment: warning: annotate this function with 'override' or (rarely) 'final' [modernize-use-override] ```suggestion void SetUp() override {} ``` ########## be/test/vec/core/block_spill_test.cpp: ########## @@ -0,0 +1,422 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 <gtest/gtest.h> + +#include "runtime/block_spill_manager.h" +#include "util/file_utils.h" +#include "vec/columns/column_array.h" +#include "vec/columns/column_decimal.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_array.h" +#include "vec/data_types/data_type_bitmap.h" +#include "vec/data_types/data_type_date.h" +#include "vec/data_types/data_type_date_time.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" + +namespace doris { +static const uint32_t MAX_PATH_LEN = 1024; + +static const std::string TMP_DATA_DIR = "block_spill_test"; + +std::string test_data_dir; +std::shared_ptr<BlockSpillManager> block_spill_manager; + +class TestBlockSpill : public testing::Test { +public: + static void SetUpTestSuite() { + char buffer[MAX_PATH_LEN]; + EXPECT_NE(getcwd(buffer, MAX_PATH_LEN), nullptr); + test_data_dir = std::string(buffer) + "/" + TMP_DATA_DIR; + std::cout << "test data dir: " << test_data_dir << "\n"; + FileUtils::remove_all(test_data_dir); + FileUtils::create_dir(test_data_dir); + + std::vector<StorePath> paths; + paths.emplace_back(test_data_dir, -1); + block_spill_manager = std::make_shared<BlockSpillManager>(paths); + } + + static void TearDownTestSuite() { FileUtils::remove_all(test_data_dir); } + +protected: + void SetUp() {} + + void TearDown() {} Review Comment: warning: annotate this function with 'override' or (rarely) 'final' [modernize-use-override] ```suggestion void TearDown() override {} ``` ########## be/test/vec/core/block_spill_test.cpp: ########## @@ -0,0 +1,422 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 <gtest/gtest.h> + +#include "runtime/block_spill_manager.h" +#include "util/file_utils.h" +#include "vec/columns/column_array.h" +#include "vec/columns/column_decimal.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_array.h" +#include "vec/data_types/data_type_bitmap.h" +#include "vec/data_types/data_type_date.h" +#include "vec/data_types/data_type_date_time.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" + +namespace doris { +static const uint32_t MAX_PATH_LEN = 1024; + +static const std::string TMP_DATA_DIR = "block_spill_test"; + +std::string test_data_dir; +std::shared_ptr<BlockSpillManager> block_spill_manager; + +class TestBlockSpill : public testing::Test { +public: + static void SetUpTestSuite() { + char buffer[MAX_PATH_LEN]; + EXPECT_NE(getcwd(buffer, MAX_PATH_LEN), nullptr); + test_data_dir = std::string(buffer) + "/" + TMP_DATA_DIR; + std::cout << "test data dir: " << test_data_dir << "\n"; + FileUtils::remove_all(test_data_dir); + FileUtils::create_dir(test_data_dir); + + std::vector<StorePath> paths; + paths.emplace_back(test_data_dir, -1); + block_spill_manager = std::make_shared<BlockSpillManager>(paths); + } + + static void TearDownTestSuite() { FileUtils::remove_all(test_data_dir); } + +protected: + void SetUp() {} + + void TearDown() {} +}; + +TEST_F(TestBlockSpill, TestInt) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + auto vec = vectorized::ColumnVector<int>::create(); + auto& data = vec->get_data(); + for (int i = 0; i < total_rows; ++i) { + data.push_back(i); + } + vectorized::DataTypePtr data_type(std::make_shared<vectorized::DataTypeInt32>()); + vectorized::ColumnWithTypeAndName type_and_name(vec->get_ptr(), data_type, + "spill_block_test_int"); + vectorized::Block block({type_and_name}); + + auto spill_block_writer = block_spill_manager->get_writer(batch_size); + spill_block_writer.write(block); + + auto spill_block_reader = block_spill_manager->get_reader(spill_block_writer.get_id()); + bool eof = false; + vectorized::Block block_read; + + for (int i = 0; i < batch_num; ++i) { + spill_block_reader.read(block_read, eof); + ASSERT_FALSE(eof); + EXPECT_EQ(block_read.rows(), batch_size); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnVector<int>*)column.get(); + for (size_t j = 0; j < batch_size; ++j) { + EXPECT_EQ(real_column->get_int(j), j + i * batch_size); + } + } + + spill_block_reader.read(block_read, eof); + ASSERT_TRUE(eof); + EXPECT_EQ(block_read.rows(), 1); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnVector<int>*)column.get(); + EXPECT_EQ(real_column->get_int(0), batch_size * 3); +} + +TEST_F(TestBlockSpill, TestIntNullable) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + auto vec = vectorized::ColumnVector<int>::create(); + auto nullable_vec = vectorized::make_nullable(std::move(vec)); + auto* raw_nullable_vec = (vectorized::ColumnNullable*)nullable_vec.get(); + for (int i = 0; i < total_rows; ++i) { + if ((i + 1) % batch_size == 0) { + raw_nullable_vec->insert_data(nullptr, 0); + } else { + raw_nullable_vec->insert_data((const char*)&i, 4); + } + } + auto data_type = vectorized::make_nullable(std::make_shared<vectorized::DataTypeInt32>()); + vectorized::ColumnWithTypeAndName type_and_name(nullable_vec->get_ptr(), data_type, + "spill_block_test_int_nullable"); + vectorized::Block block({type_and_name}); + + auto spill_block_writer = block_spill_manager->get_writer(batch_size); + spill_block_writer.write(block); + + auto spill_block_reader = block_spill_manager->get_reader(spill_block_writer.get_id()); + bool eof = false; + vectorized::Block block_read; + + for (int i = 0; i < batch_num; ++i) { + spill_block_reader.read(block_read, eof); + ASSERT_FALSE(eof); + EXPECT_EQ(block_read.rows(), batch_size); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnNullable*)column.get(); + const auto& int_column = + (const vectorized::ColumnVector<int>&)(real_column->get_nested_column()); + for (size_t j = 0; j < batch_size; ++j) { + if ((j + 1) % batch_size == 0) { + ASSERT_TRUE(real_column->is_null_at(j)); + } else { + EXPECT_EQ(int_column.get_int(j), j + i * batch_size); + } + } + } + + spill_block_reader.read(block_read, eof); + ASSERT_TRUE(eof); + EXPECT_EQ(block_read.rows(), 1); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnNullable*)column.get(); + const auto& int_column = + (const vectorized::ColumnVector<int>&)(real_column->get_nested_column()); + EXPECT_EQ(int_column.get_int(0), batch_size * 3); +} +TEST_F(TestBlockSpill, TestString) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + auto strcol = vectorized::ColumnString::create(); + for (int i = 0; i < total_rows; ++i) { + std::string is = std::to_string(i); + strcol->insert_data(is.c_str(), is.size()); + } + vectorized::DataTypePtr string_type(std::make_shared<vectorized::DataTypeString>()); + vectorized::ColumnWithTypeAndName test_string(strcol->get_ptr(), string_type, + "spill_block_test_string"); + vectorized::Block block({test_string}); + + auto spill_block_writer = block_spill_manager->get_writer(batch_size); + Status st = spill_block_writer.write(block); + EXPECT_TRUE(st.ok()); + + auto spill_block_reader = block_spill_manager->get_reader(spill_block_writer.get_id()); + bool eof = false; + vectorized::Block block_read; + + for (int i = 0; i < batch_num; ++i) { + st = spill_block_reader.read(block_read, eof); + EXPECT_TRUE(st.ok()); + ASSERT_FALSE(eof); + EXPECT_EQ(block_read.rows(), batch_size); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnString*)column.get(); + for (size_t j = 0; j < batch_size; ++j) { + EXPECT_EQ(real_column->get_data_at(j), StringRef(std::to_string(j + i * batch_size))); + } + } + + spill_block_reader.read(block_read, eof); + ASSERT_TRUE(eof); + EXPECT_EQ(block_read.rows(), 1); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnString*)column.get(); + EXPECT_EQ(real_column->get_data_at(0), StringRef(std::to_string(batch_size * 3))); +} +TEST_F(TestBlockSpill, TestStringNullable) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + auto strcol = vectorized::ColumnString::create(); + auto nullable_vec = vectorized::make_nullable(std::move(strcol)); + auto* raw_nullable_vec = (vectorized::ColumnNullable*)nullable_vec.get(); + for (int i = 0; i < total_rows; ++i) { + if ((i + 1) % batch_size == 0) { + raw_nullable_vec->insert_data(nullptr, 0); + } else { + std::string is = std::to_string(i); + raw_nullable_vec->insert_data(is.c_str(), is.size()); + } + } + auto data_type = vectorized::make_nullable(std::make_shared<vectorized::DataTypeString>()); + vectorized::ColumnWithTypeAndName type_and_name(nullable_vec->get_ptr(), data_type, + "spill_block_test_string_nullable"); + vectorized::Block block({type_and_name}); + + auto spill_block_writer = block_spill_manager->get_writer(batch_size); + Status st = spill_block_writer.write(block); + EXPECT_TRUE(st.ok()); + + auto spill_block_reader = block_spill_manager->get_reader(spill_block_writer.get_id()); + bool eof = false; + vectorized::Block block_read; + + for (int i = 0; i < batch_num; ++i) { + st = spill_block_reader.read(block_read, eof); + EXPECT_TRUE(st.ok()); + ASSERT_FALSE(eof); + EXPECT_EQ(block_read.rows(), batch_size); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnNullable*)column.get(); + const auto& string_column = + (const vectorized::ColumnString&)(real_column->get_nested_column()); + for (size_t j = 0; j < batch_size; ++j) { + if ((j + 1) % batch_size == 0) { + ASSERT_TRUE(real_column->is_null_at(j)); + } else { + EXPECT_EQ(string_column.get_data_at(j), + StringRef(std::to_string(j + i * batch_size))); + } + } + } + + st = spill_block_reader.read(block_read, eof); + EXPECT_TRUE(st.ok()); + ASSERT_TRUE(eof); + EXPECT_EQ(block_read.rows(), 1); + auto column = block_read.get_by_position(0).column; + auto* real_column = (vectorized::ColumnNullable*)column.get(); + const auto& string_column = (const vectorized::ColumnString&)(real_column->get_nested_column()); + EXPECT_EQ(string_column.get_data_at(0), StringRef(std::to_string(batch_size * 3))); +} +TEST_F(TestBlockSpill, TestDecimal) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + + vectorized::DataTypePtr decimal_data_type(doris::vectorized::create_decimal(27, 9, true)); + auto decimal_column = decimal_data_type->create_column(); + auto& decimal_data = ((vectorized::ColumnDecimal<vectorized::Decimal<vectorized::Int128>>*) + decimal_column.get()) + ->get_data(); + for (int i = 0; i < total_rows; ++i) { + __int128_t value = i * pow(10, 9) + i * pow(10, 8); + decimal_data.push_back(value); + } + vectorized::ColumnWithTypeAndName test_decimal(decimal_column->get_ptr(), decimal_data_type, + "spill_block_test_decimal"); + vectorized::Block block({test_decimal}); + + auto spill_block_writer = block_spill_manager->get_writer(batch_size); + auto st = spill_block_writer.write(block); + EXPECT_TRUE(st.ok()); + + auto spill_block_reader = block_spill_manager->get_reader(spill_block_writer.get_id()); + bool eof = false; + vectorized::Block block_read; + + for (int i = 0; i < batch_num; ++i) { + st = spill_block_reader.read(block_read, eof); + EXPECT_TRUE(st.ok()); + ASSERT_FALSE(eof); + EXPECT_EQ(block_read.rows(), batch_size); + auto column = block_read.get_by_position(0).column; + auto* real_column = + (vectorized::ColumnDecimal<vectorized::Decimal<vectorized::Int128>>*)column.get(); + for (size_t j = 0; j < batch_size; ++j) { + __int128_t value = (j + i * batch_size) * (pow(10, 9) + pow(10, 8)); + EXPECT_EQ(real_column->get_element(j), value); + } + } + + st = spill_block_reader.read(block_read, eof); + EXPECT_TRUE(st.ok()); + ASSERT_TRUE(eof); + EXPECT_EQ(block_read.rows(), 1); + auto column = block_read.get_by_position(0).column; + auto* real_column = + (vectorized::ColumnDecimal<vectorized::Decimal<vectorized::Int128>>*)column.get(); + EXPECT_EQ(real_column->get_element(0), batch_size * 3 * (pow(10, 9) + pow(10, 8))); +} +TEST_F(TestBlockSpill, TestDecimalNullable) { + int batch_size = 3; // rows in a block + int batch_num = 3; + int total_rows = batch_size * batch_num + 1; + + vectorized::DataTypePtr decimal_data_type(doris::vectorized::create_decimal(27, 9, true)); + auto base_col = vectorized::make_nullable(std::move(decimal_data_type->create_column())); Review Comment: warning: moving a temporary object prevents copy elision [clang-diagnostic-pessimizing-move] ```cpp auto base_col = vectorized::make_nullable(std::move(decimal_data_type->create_column())); ^ ``` **be/test/vec/core/block_spill_test.cpp:311:** remove std::move call here ```cpp auto base_col = vectorized::make_nullable(std::move(decimal_data_type->create_column())); ^ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
