ClickHouse/dbms/src/Processors/Chunk.h

118 lines
3.3 KiB
C++
Raw Normal View History

2019-02-19 18:41:18 +00:00
#pragma once
#include <Columns/IColumn.h>
2019-04-05 12:29:17 +00:00
#include <unordered_map>
2019-02-19 18:41:18 +00:00
namespace DB
{
class ChunkInfo
{
public:
virtual ~ChunkInfo() = default;
2019-04-05 11:43:28 +00:00
ChunkInfo() = default;
2019-02-19 18:41:18 +00:00
};
using ChunkInfoPtr = std::shared_ptr<const ChunkInfo>;
class Chunk
{
public:
Chunk() = default;
2019-06-18 08:25:27 +00:00
Chunk(const Chunk & other) = delete;
2019-06-18 10:33:02 +00:00
Chunk(Chunk && other) noexcept
: columns(std::move(other.columns))
, num_rows(other.num_rows)
, chunk_info(std::move(other.chunk_info))
{
other.num_rows = 0;
}
2019-02-19 18:41:18 +00:00
Chunk(Columns columns_, UInt64 num_rows_);
Chunk(Columns columns_, UInt64 num_rows_, ChunkInfoPtr chunk_info_);
Chunk(MutableColumns columns_, UInt64 num_rows_);
Chunk(MutableColumns columns_, UInt64 num_rows_, ChunkInfoPtr chunk_info_);
2019-06-18 08:25:27 +00:00
Chunk & operator=(const Chunk & other) = delete;
2019-06-18 10:33:02 +00:00
Chunk & operator=(Chunk && other) noexcept
{
columns = std::move(other.columns);
chunk_info = std::move(other.chunk_info);
num_rows = other.num_rows;
other.num_rows = 0;
return *this;
}
2019-02-19 18:41:18 +00:00
2019-06-18 08:25:27 +00:00
Chunk clone() const;
2019-06-19 18:30:02 +00:00
void swap(Chunk & other)
{
columns.swap(other.columns);
chunk_info.swap(other.chunk_info);
std::swap(num_rows, other.num_rows);
}
void clear()
{
num_rows = 0;
columns.clear();
chunk_info.reset();
}
2019-02-19 18:41:18 +00:00
const Columns & getColumns() const { return columns; }
void setColumns(Columns columns_, UInt64 num_rows_);
void setColumns(MutableColumns columns_, UInt64 num_rows_);
Columns detachColumns();
MutableColumns mutateColumns();
2019-02-26 18:40:08 +00:00
/** Get empty columns with the same types as in block. */
MutableColumns cloneEmptyColumns() const;
2019-02-19 18:41:18 +00:00
const ChunkInfoPtr & getChunkInfo() const { return chunk_info; }
void setChunkInfo(ChunkInfoPtr chunk_info_) { chunk_info = std::move(chunk_info_); }
UInt64 getNumRows() const { return num_rows; }
UInt64 getNumColumns() const { return columns.size(); }
bool hasNoRows() const { return num_rows == 0; }
bool hasNoColumns() const { return columns.empty(); }
bool empty() const { return hasNoRows() && hasNoColumns(); }
operator bool() const { return !empty(); }
2019-02-19 18:41:18 +00:00
void erase(size_t position);
2019-02-19 18:41:18 +00:00
2019-03-04 14:56:09 +00:00
UInt64 bytes() const;
2019-02-26 18:40:08 +00:00
UInt64 allocatedBytes() const;
std::string dumpStructure() const;
2019-02-19 18:41:18 +00:00
private:
Columns columns;
UInt64 num_rows = 0;
ChunkInfoPtr chunk_info;
void checkNumRowsIsConsistent();
};
using Chunks = std::vector<Chunk>;
/// Block extension to support delayed defaults. AddingDefaultsProcessor uses it to replace missing values with column defaults.
class ChunkMissingValues : public ChunkInfo
{
public:
using RowsBitMask = std::vector<bool>; /// a bit per row for a column
const RowsBitMask & getDefaultsBitmask(size_t column_idx) const;
void setBit(size_t column_idx, size_t row_idx);
bool empty() const { return rows_mask_by_column_id.empty(); }
size_t size() const { return rows_mask_by_column_id.size(); }
void clear() { rows_mask_by_column_id.clear(); }
private:
using RowsMaskByColumnId = std::unordered_map<size_t, RowsBitMask>;
/// If rows_mask_by_column_id[column_id][row_id] is true related value in Block should be replaced with column default.
/// It could contain less columns and rows then related block.
RowsMaskByColumnId rows_mask_by_column_id;
};
}