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 2020/05/07 10:41:31 UTC

[GitHub] [incubator-doris] decster opened a new pull request #3508: [Memory Engine] add core column related classes

decster opened a new pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508


   This CL add core column related classes into the code base, including:
   
   - Buffer
   - ColumnBlock
   - ColumnDelta
   - Column
   - DeltaIndex
   - ColumnSchema and Schema
   
   Column read/write will added in the following PR
   
   Part of #3465
   


----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422582801



##########
File path: be/src/olap/memory/delta_index.h
##########
@@ -0,0 +1,76 @@
+// 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 <vector>
+
+#include "olap/memory/buffer.h"
+#include "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// DeltaIndex store all the updated rows' id(rowids) for a ColumnDelta.
+// Rowids are sorted and divided into blocks, each 64K rowid space is a
+// block. Since each block only have 64K id space, it can be store as uint16_t
+// rather than uint32_t to save memory.
+class DeltaIndex : public RefCountedThreadSafe<DeltaIndex> {
+public:
+    static const uint32_t npos = 0xffffffffu;
+
+    DeltaIndex() = default;
+
+    // get memory consumption
+    size_t memory() const;
+
+    // find rowid(rid) in the index,
+    // return index position if found, else return npos
+    uint32_t find_idx(uint32_t rid);
+
+    // get a block's index position range as [start, end)
+    void block_range(uint32_t bid, uint32_t* start, uint32_t* end) const {
+        if (bid < _block_ends.size()) {
+            *start = bid > 0 ? _block_ends[bid - 1] : 0;
+            *end = _block_ends[bid];
+        } else {
+            *start = 0;
+            *end = 0;
+        }
+    }
+
+    // Return true this index has any rowid belonging to this block

Review comment:
       fixed




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] kangkaisen commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
kangkaisen commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422596311



##########
File path: be/src/olap/memory/buffer.h
##########
@@ -0,0 +1,74 @@
+// 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 "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// A generic buffer holding column base and delta data
+// It can be considered as an array of any primitive type, but it does not
+// have compile time type information, user can use utility method as<T> to
+// get typed array view.
+class Buffer {
+public:
+    Buffer() = default;
+    ~Buffer();
+
+    // allocate memory for this buffer, with buffer byte size of bsize
+    Status alloc(size_t bsize);
+
+    // clear buffer, free memory
+    void clear();
+
+    // set all memory content to zero
+    void set_zero();
+
+    // return true if this buffer is not empty
+    operator bool() const { return _data != nullptr; }
+
+    // returns a direct pointer to the memory array
+    const uint8_t* data() const { return _data; }
+
+    // returns a direct pointer to the memory array
+    uint8_t* data() { return _data; }

Review comment:
       would better naming mutable_data().

##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,124 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);

Review comment:
       Why is 64 ? Which should be a const value.

##########
File path: be/src/olap/memory/column.h
##########
@@ -0,0 +1,101 @@
+// 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 "olap/memory/column_block.h"
+#include "olap/memory/column_delta.h"
+#include "olap/memory/common.h"
+#include "olap/memory/schema.h"
+
+namespace doris {
+namespace memory {
+
+class ColumnReader;
+class ColumnWriter;
+
+// Column store all the data of a column, including base and deltas.
+// It supports single-writer multi-reader concurrency.
+// It's properties are all immutable except _base and _versions.
+// _base and _versions use std::vector, which is basically thread-safe
+// in-practice for single-writer/multi-reader access, if there isn't
+// any over-capacity realloc or delta compaction/GC caused data change.
+// When these situations occur, we do a copy-on-write.
+//
+// TODO: add column read&writer
+class Column : public RefCountedThreadSafe<Column> {
+public:
+    static const uint32_t BLOCK_SIZE = 1 << 16;
+    static const uint32_t BLOCK_MASK = 0xffff;
+
+    // create a Column which provided column schema, underlying storage_type and initial version
+    Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version);
+
+    // copy-on-write a new Column with new capacity
+    Column(const Column& rhs, size_t new_base_capacity, size_t new_version_capacity);
+
+    // get column schema
+    const ColumnSchema& schema() { return _cs; }
+
+    // get memory usage in bytes
+    size_t memory() const;
+
+    string debug_string() const;
+
+    // read this Column at a specific version, get a reader for this Column
+    // support multiple concurrent readers
+    Status read(uint64_t version, std::unique_ptr<ColumnReader>* reader);
+
+    // write this Column, get a writer for this Column
+    // caller needs to make sure there is only one or no writer exists at any time
+    Status write(std::unique_ptr<ColumnWriter>* writer);
+
+private:
+    ColumnSchema _cs;
+    // For some types the storage_type may be different from actual type from schema.
+    // For example, string stored in dictionary, so column_block store a integer id,
+    // and the storage type may change as the dictionary grows, e.g. from uint8 to uint16
+    ColumnType _storage_type;
+    // base's position at _versions vector
+    ssize_t _base_idx;

Review comment:
       Why use ssize_t?  _base_idx could be negative?

##########
File path: be/src/olap/memory/column_block.h
##########
@@ -0,0 +1,53 @@
+// 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 "olap/memory/buffer.h"
+#include "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// ColumnBlock stores one block of data for a Column
+class ColumnBlock : public RefCountedThreadSafe<ColumnBlock> {
+public:
+    ColumnBlock() = default;
+
+    size_t memory() const;
+
+    Buffer& data() { return _data; }
+
+    Buffer& nulls() { return _nulls; }
+
+    Status alloc(size_t size, size_t esize);

Review comment:
       `esize ` means `element_size`? would better add a comment for this method.




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422755103



##########
File path: be/src/olap/memory/column_block.h
##########
@@ -0,0 +1,53 @@
+// 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 "olap/memory/buffer.h"
+#include "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// ColumnBlock stores one block of data for a Column
+class ColumnBlock : public RefCountedThreadSafe<ColumnBlock> {
+public:
+    ColumnBlock() = default;
+
+    size_t memory() const;
+
+    Buffer& data() { return _data; }
+
+    Buffer& nulls() { return _nulls; }
+
+    Status alloc(size_t size, size_t esize);

Review comment:
       fixed
   




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#issuecomment-626446729


   > @decster Hi, I have one question: Do we have a data structure to describe the block of rows for multi columns? Like Doris RowBlockV2, Arrow RecordBatch and TiDB Chunk.
   
   Not currently, but same blockid represents same group of rows, so we can always add them latter if needed. 


----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] kangkaisen commented on pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
kangkaisen commented on pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#issuecomment-626285200


   @decster Hi, I have one question:  Do we have a data structure to describe `the block of rows for multi columns`?  Like  Doris `RowBlockV2`, Arrow `RecordBatch` and TiDB `Chunk`.  


----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422748533



##########
File path: be/src/olap/memory/column_block.h
##########
@@ -0,0 +1,53 @@
+// 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 "olap/memory/buffer.h"
+#include "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// ColumnBlock stores one block of data for a Column
+class ColumnBlock : public RefCountedThreadSafe<ColumnBlock> {
+public:
+    ColumnBlock() = default;
+
+    size_t memory() const;
+
+    Buffer& data() { return _data; }
+
+    Buffer& nulls() { return _nulls; }
+
+    Status alloc(size_t size, size_t esize);

Review comment:
       Sure




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422507243



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,125 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);
+    _versions.reserve(64);
+    _versions.emplace_back(version);
+    DLOG(INFO) << "create %s" << debug_string();

Review comment:
       This log is only used in debug mode, like DCHECK, it help show more info when debugging, and it has no effect in release build. I can remove them if it's this project's convention.




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422756451



##########
File path: be/src/olap/memory/column.h
##########
@@ -0,0 +1,101 @@
+// 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 "olap/memory/column_block.h"
+#include "olap/memory/column_delta.h"
+#include "olap/memory/common.h"
+#include "olap/memory/schema.h"
+
+namespace doris {
+namespace memory {
+
+class ColumnReader;
+class ColumnWriter;
+
+// Column store all the data of a column, including base and deltas.
+// It supports single-writer multi-reader concurrency.
+// It's properties are all immutable except _base and _versions.
+// _base and _versions use std::vector, which is basically thread-safe
+// in-practice for single-writer/multi-reader access, if there isn't
+// any over-capacity realloc or delta compaction/GC caused data change.
+// When these situations occur, we do a copy-on-write.
+//
+// TODO: add column read&writer
+class Column : public RefCountedThreadSafe<Column> {
+public:
+    static const uint32_t BLOCK_SIZE = 1 << 16;
+    static const uint32_t BLOCK_MASK = 0xffff;
+
+    // create a Column which provided column schema, underlying storage_type and initial version
+    Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version);
+
+    // copy-on-write a new Column with new capacity
+    Column(const Column& rhs, size_t new_base_capacity, size_t new_version_capacity);
+
+    // get column schema
+    const ColumnSchema& schema() { return _cs; }
+
+    // get memory usage in bytes
+    size_t memory() const;
+
+    string debug_string() const;
+
+    // read this Column at a specific version, get a reader for this Column
+    // support multiple concurrent readers
+    Status read(uint64_t version, std::unique_ptr<ColumnReader>* reader);
+
+    // write this Column, get a writer for this Column
+    // caller needs to make sure there is only one or no writer exists at any time
+    Status write(std::unique_ptr<ColumnWriter>* writer);
+
+private:
+    ColumnSchema _cs;
+    // For some types the storage_type may be different from actual type from schema.
+    // For example, string stored in dictionary, so column_block store a integer id,
+    // and the storage type may change as the dictionary grows, e.g. from uint8 to uint16
+    ColumnType _storage_type;
+    // base's position at _versions vector
+    ssize_t _base_idx;

Review comment:
       This is for usage in Column.cpp, but I guess change code in for loop would be better. 
   
   `        for (ssize_t i = static_cast<ssize_t>(_base_idx) - 1; i >= 0; i--) {
   `
   
   fixed




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422438098



##########
File path: be/src/olap/memory/buffer.h
##########
@@ -0,0 +1,61 @@
+// 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 "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+class Buffer {

Review comment:
       fixed




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422748342



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,124 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);

Review comment:
       > @decster Hi, I have one question: Do we have a data structure to describe `the block of rows for multi columns`? Like Doris `RowBlockV2`, Arrow `RecordBatch` and TiDB `Chunk`.
   
   No, but the same block id and rowid represent same group of rows, so it's easy to add them if it's really necessary. But currently, I do not see any needs.




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422438126



##########
File path: be/src/olap/memory/CMakeLists.txt
##########
@@ -22,6 +22,12 @@ set(LIBRARY_OUTPUT_PATH "${BUILD_DIR}/src/olap/memory")
 set(EXECUTABLE_OUTPUT_PATH "${BUILD_DIR}/src/olap/memory")
   
 add_library(Memory STATIC
+	buffer.cpp

Review comment:
       fixed




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422748342



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,124 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);

Review comment:
       > @decster Hi, I have one question: Do we have a data structure to describe `the block of rows for multi columns`? Like Doris `RowBlockV2`, Arrow `RecordBatch` and TiDB `Chunk`.
   
   No, but the same block id and rowid represent same group of rows, so it's easy to add them if it's really necessary. But currently, I do not see any needs.




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] morningman commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422199223



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,125 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);
+    _versions.reserve(64);
+    _versions.emplace_back(version);
+    DLOG(INFO) << "create %s" << debug_string();

Review comment:
       What is DLOG(INFO) ? Why not using VLOG?

##########
File path: be/src/olap/memory/column.h
##########
@@ -0,0 +1,96 @@
+// 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 "olap/memory/column_block.h"
+#include "olap/memory/column_delta.h"
+#include "olap/memory/common.h"
+#include "olap/memory/schema.h"
+
+namespace doris {
+namespace memory {
+
+class ColumnReader;
+class ColumnWriter;
+
+// Column store all the data of a column, including base and deltas.
+// It supports single-writer multi-reader concurrency.
+// It's properties are all immutable except _base and _versions.
+// _base and _versions use std::vector, which is basically thread-safe
+// in-practice for single-writer/multi-reader access, if there isn't
+// any over-capacity realloc or delta compaction/GC caused data change.
+// When these situations occur, we do a copy-on-write.
+//
+// TODO: add column read&writer
+class Column : public RefCountedThreadSafe<Column> {
+public:
+    static const uint32_t BLOCK_SIZE = 1 << 16;
+    static const uint32_t BLOCK_MASK = 0xffff;
+
+    // create a Column which provided column schema, underlying storage_type and initial version
+    Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version);
+
+    // copy-on-write a new Column with new capacity
+    Column(const Column& rhs, size_t new_base_capacity, size_t new_version_capacity);
+
+    // get column schema
+    const ColumnSchema& schema() { return _cs; }
+
+    // get memory usage in bytes
+    size_t memory() const;
+
+    string debug_string() const;
+
+    // read this Column at a specific version, get a reader for this Column
+    // support multiple concurrent readers
+    Status read(uint64_t version, std::unique_ptr<ColumnReader>* reader);
+
+    // write this Column, get a writer for this Column
+    // caller needs to make sure there is only one or no writer exists at any time
+    Status write(std::unique_ptr<ColumnWriter>* writer);
+
+private:
+    ColumnSchema _cs;
+    // For some types the storage_type may be different from actual type from schema.
+    // For example, string stored in dictionary, so column_block store a integer id,
+    // and the storage type may change as the dictionary grows, e.g. from uint8 to uint16
+    ColumnType _storage_type;
+    // base's position at _versions vector
+    ssize_t _base_idx;
+    // base data, a vector of ColumnBlocks
+    vector<scoped_refptr<ColumnBlock>> _base;
+    struct VersionInfo {
+        VersionInfo() = default;
+        explicit VersionInfo(uint64_t version) : version(version) {}
+        uint64_t version = 0;
+        // null if it's base
+        scoped_refptr<ColumnDelta> delta;
+    };
+    // version vector
+    vector<VersionInfo> _versions;
+
+    Status capture_version(uint64_t version, vector<ColumnDelta*>* deltas,

Review comment:
       Comment

##########
File path: be/src/olap/memory/delta_index.h
##########
@@ -0,0 +1,76 @@
+// 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 <vector>
+
+#include "olap/memory/buffer.h"
+#include "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// DeltaIndex store all the updated rows' id(rowids) for a ColumnDelta.
+// Rowids are sorted and divided into blocks, each 64K rowid space is a
+// block. Since each block only have 64K id space, it can be store as uint16_t
+// rather than uint32_t to save memory.
+class DeltaIndex : public RefCountedThreadSafe<DeltaIndex> {
+public:
+    static const uint32_t npos = 0xffffffffu;
+
+    DeltaIndex() = default;
+
+    // get memory consumption
+    size_t memory() const;
+
+    // find rowid(rid) in the index,
+    // return index position if found, else return npos
+    uint32_t find_idx(uint32_t rid);
+
+    // get a block's index position range as [start, end)
+    void block_range(uint32_t bid, uint32_t* start, uint32_t* end) const {
+        if (bid < _block_ends.size()) {
+            *start = bid > 0 ? _block_ends[bid - 1] : 0;
+            *end = _block_ends[bid];
+        } else {
+            *start = 0;
+            *end = 0;
+        }
+    }
+
+    // Return true this index has any rowid belonging to this block

Review comment:
       ```suggestion
       // Return true if this index has any rowid belonging to this 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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422757108



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,124 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);

Review comment:
       It's the step size for base vector and version vector when over capacity and need to grow




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422755043



##########
File path: be/src/olap/memory/buffer.h
##########
@@ -0,0 +1,74 @@
+// 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 "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+// A generic buffer holding column base and delta data
+// It can be considered as an array of any primitive type, but it does not
+// have compile time type information, user can use utility method as<T> to
+// get typed array view.
+class Buffer {
+public:
+    Buffer() = default;
+    ~Buffer();
+
+    // allocate memory for this buffer, with buffer byte size of bsize
+    Status alloc(size_t bsize);
+
+    // clear buffer, free memory
+    void clear();
+
+    // set all memory content to zero
+    void set_zero();
+
+    // return true if this buffer is not empty
+    operator bool() const { return _data != nullptr; }
+
+    // returns a direct pointer to the memory array
+    const uint8_t* data() const { return _data; }
+
+    // returns a direct pointer to the memory array
+    uint8_t* data() { return _data; }

Review comment:
       There are already many places use this naming convention, and class in std also use this convention. 




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] morningman commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r421895904



##########
File path: be/src/olap/memory/CMakeLists.txt
##########
@@ -22,6 +22,12 @@ set(LIBRARY_OUTPUT_PATH "${BUILD_DIR}/src/olap/memory")
 set(EXECUTABLE_OUTPUT_PATH "${BUILD_DIR}/src/olap/memory")
   
 add_library(Memory STATIC
+	buffer.cpp

Review comment:
       unify the indent

##########
File path: be/src/olap/memory/buffer.h
##########
@@ -0,0 +1,61 @@
+// 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 "olap/memory/common.h"
+
+namespace doris {
+namespace memory {
+
+class Buffer {

Review comment:
       Comment for this class




----------------------------------------------------------------
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.

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


[GitHub] [incubator-doris] decster commented on a change in pull request #3508: [Memory Engine] add core column related classes

Posted by GitBox <gi...@apache.org>.
decster commented on a change in pull request #3508:
URL: https://github.com/apache/incubator-doris/pull/3508#discussion_r422755073



##########
File path: be/src/olap/memory/column.cpp
##########
@@ -0,0 +1,124 @@
+// 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/memory/column.h"
+
+namespace doris {
+namespace memory {
+
+Column::Column(const ColumnSchema& cs, ColumnType storage_type, uint64_t version)
+        : _cs(cs), _storage_type(storage_type), _base_idx(0) {
+    _base.reserve(64);

Review comment:
       fixed




----------------------------------------------------------------
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.

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