Chunk.cpp 4.2 KB
Newer Older
N
Nikolai Kochetov 已提交
1 2
#include <Processors/Chunk.h>
#include <IO/WriteHelpers.h>
3
#include <IO/Operators.h>
N
Nikolai Kochetov 已提交
4 5 6 7

namespace DB
{

8 9
namespace ErrorCodes
{
A
Alexander Kuzmenkov 已提交
10
    extern const int LOGICAL_ERROR;
11 12 13
    extern const int POSITION_OUT_OF_BOUND;
}

N
Nikolai Kochetov 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Chunk::Chunk(DB::Columns columns_, UInt64 num_rows_) : columns(std::move(columns_)), num_rows(num_rows_)
{
    checkNumRowsIsConsistent();
}

Chunk::Chunk(Columns columns_, UInt64 num_rows_, ChunkInfoPtr chunk_info_)
    : columns(std::move(columns_)), num_rows(num_rows_), chunk_info(std::move(chunk_info_))
{
    checkNumRowsIsConsistent();
}

static Columns unmuteColumns(MutableColumns && mut_columns)
{
    Columns columns;
    columns.reserve(mut_columns.size());
    for (auto & col : mut_columns)
        columns.emplace_back(std::move(col));

    return columns;
}

Chunk::Chunk(MutableColumns columns_, UInt64 num_rows_)
    : columns(unmuteColumns(std::move(columns_))), num_rows(num_rows_)
{
38
    checkNumRowsIsConsistent();
N
Nikolai Kochetov 已提交
39 40 41 42 43
}

Chunk::Chunk(MutableColumns columns_, UInt64 num_rows_, ChunkInfoPtr chunk_info_)
    : columns(unmuteColumns(std::move(columns_))), num_rows(num_rows_), chunk_info(std::move(chunk_info_))
{
44
    checkNumRowsIsConsistent();
N
Nikolai Kochetov 已提交
45 46
}

47 48
Chunk Chunk::clone() const
{
N
Nikolai Kochetov 已提交
49
    return Chunk(getColumns(), getNumRows(), chunk_info);
50 51
}

N
Nikolai Kochetov 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
void Chunk::setColumns(Columns columns_, UInt64 num_rows_)
{
    columns = std::move(columns_);
    num_rows = num_rows_;
    checkNumRowsIsConsistent();
}

void Chunk::setColumns(MutableColumns columns_, UInt64 num_rows_)
{
    columns = unmuteColumns(std::move(columns_));
    num_rows = num_rows_;
    checkNumRowsIsConsistent();
}

void Chunk::checkNumRowsIsConsistent()
{
    for (auto & column : columns)
        if (column->size() != num_rows)
            throw Exception("Invalid number of rows in Chunk column " + column->getName()+ ": expected " +
                            toString(num_rows) + ", got " + toString(column->size()), ErrorCodes::LOGICAL_ERROR);
}

MutableColumns Chunk::mutateColumns()
{
    size_t num_columns = columns.size();
    MutableColumns mut_columns(num_columns);
    for (size_t i = 0; i < num_columns; ++i)
        mut_columns[i] = (*std::move(columns[i])).mutate();

    columns.clear();
    num_rows = 0;

    return mut_columns;
}

N
Nikolai Kochetov 已提交
87 88 89 90 91 92 93 94 95
MutableColumns Chunk::cloneEmptyColumns() const
{
    size_t num_columns = columns.size();
    MutableColumns mut_columns(num_columns);
    for (size_t i = 0; i < num_columns; ++i)
        mut_columns[i] = columns[i]->cloneEmpty();
    return mut_columns;
}

N
Nikolai Kochetov 已提交
96 97 98 99 100 101
Columns Chunk::detachColumns()
{
    num_rows = 0;
    return std::move(columns);
}

N
Nikolai Kochetov 已提交
102 103 104 105 106 107
void Chunk::addColumn(ColumnPtr column)
{
    if (column->size() != num_rows)
        throw Exception("Invalid number of rows in Chunk column " + column->getName()+ ": expected " +
                        toString(num_rows) + ", got " + toString(column->size()), ErrorCodes::LOGICAL_ERROR);

N
Nikolai Kochetov 已提交
108
    columns.emplace_back(std::move(column));
N
Nikolai Kochetov 已提交
109 110
}

111 112 113 114 115 116 117 118 119 120 121 122
void Chunk::erase(size_t position)
{
    if (columns.empty())
        throw Exception("Chunk is empty", ErrorCodes::POSITION_OUT_OF_BOUND);

    if (position >= columns.size())
        throw Exception("Position " + toString(position) + " out of bound in Chunk::erase(), max position = "
                        + toString(columns.size() - 1), ErrorCodes::POSITION_OUT_OF_BOUND);

    columns.erase(columns.begin() + position);
}

N
Nikolai Kochetov 已提交
123 124 125 126 127 128 129 130 131
UInt64 Chunk::bytes() const
{
    UInt64 res = 0;
    for (const auto & column : columns)
        res += column->byteSize();

    return res;
}

N
Nikolai Kochetov 已提交
132 133 134 135 136 137 138 139 140
UInt64 Chunk::allocatedBytes() const
{
    UInt64 res = 0;
    for (const auto & column : columns)
        res += column->allocatedBytes();

    return res;
}

141 142 143
std::string Chunk::dumpStructure() const
{
    WriteBufferFromOwnString out;
A
Alexey Milovidov 已提交
144
    for (const auto & column : columns)
145 146 147 148 149
        out << ' ' << column->dumpStructure();

    return out.str();
}

N
Nikolai Kochetov 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

void ChunkMissingValues::setBit(size_t column_idx, size_t row_idx)
{
    RowsBitMask & mask = rows_mask_by_column_id[column_idx];
    mask.resize(row_idx + 1);
    mask[row_idx] = true;
}

const ChunkMissingValues::RowsBitMask & ChunkMissingValues::getDefaultsBitmask(size_t column_idx) const
{
    static RowsBitMask none;
    auto it = rows_mask_by_column_id.find(column_idx);
    if (it != rows_mask_by_column_id.end())
        return it->second;
    return none;
}

}