You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/12/28 17:06:32 UTC

[GitHub] [doris] github-actions[bot] commented on a diff in pull request #15399: [draft] Support spill to disk: sort

github-actions[bot] commented on code in PR #15399:
URL: https://github.com/apache/doris/pull/15399#discussion_r1058481553


##########
be/src/vec/common/sort/sorter.h:
##########
@@ -50,6 +51,29 @@ class MergeSorterState {
     int64_t _offset;
 };
 
+class OuterMergeSorterState {
+public:
+    OuterMergeSorterState(SortDescription& sort_description)
+        : _sort_description(sort_description) {}
+
+    Status add_sorted_block(Block&& block);
+
+    Status merge_blocks();
+
+private:
+    void _build_merge_tree(int num_blocks);
+
+    Status _do_merge();
+
+private:

Review Comment:
   warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers]
   
   ```suggestion
   
   ```
   **be/src/vec/common/sort/sorter.h:62:** previously declared here
   ```cpp
   private:
   ^
   ```
   



##########
be/src/vec/core/spill_block_reader.h:
##########
@@ -0,0 +1,46 @@
+// 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 "vec/core/block.h"
+#include "io/fs/file_reader.h"
+
+namespace doris {
+namespace vectorized {
+// Read data spilled to local file.
+class SpillBlockReader {
+public:
+    SpillBlockReader(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();
+
+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/spill_block_reader.h:34:** previously declared here
   ```cpp
   private:
   ^
   ```
   



##########
be/test/vec/core/block_spill_test.cpp:
##########
@@ -0,0 +1,190 @@
+// 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 "util/file_utils.h"
+#include "runtime/block_spill_manager.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;
+        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,190 @@
+// 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 "util/file_utils.h"
+#include "runtime/block_spill_manager.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;
+        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/src/vec/core/spill_block_reader.cpp:
##########
@@ -0,0 +1,95 @@
+// 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 "olap/iterators.h"
+#include "vec/core/spill_block_reader.h"
+#include "io/fs/file_system.h"
+#include "io/file_factory.h"
+
+namespace doris {
+namespace vectorized {
+Status SpillBlockReader::_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 SpillBlockReader::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/spill_block_reader.cpp:86:** remove std::move call here
   ```cpp
       block = std::move(Block(pb_block));
               ^
   ```
   



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org