aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/clickhouse/client/columns/array.cpp
blob: 8a83c36f72f92b3097c9a476ec13fbf6fe5d768f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
87
#include "array.h"

#include <util/generic/yexception.h>

namespace NClickHouse {
    TColumnArray::TColumnArray(TColumnRef data)
        : TColumn(TType::CreateArray(data->Type()))
        , Data_(data)
        , Offsets_(TColumnUInt64::Create())
    {
    }

    TColumnArray::TColumnArray(TColumnRef data, TVector<ui64>&& offsets)
        : TColumn(TType::CreateArray(data->Type()))
        , Data_(data)
        , Offsets_(TColumnUInt64::Create(std::move(offsets)))
    {
    }

    TIntrusivePtr<TColumnArray> TColumnArray::Create(TColumnRef data) {
        return new TColumnArray(data);
    }

    TIntrusivePtr<TColumnArray> TColumnArray::Create(TColumnRef data, TVector<ui64>&& offsets) {
        return new TColumnArray(data, std::move(offsets));
    }

    void TColumnArray::AppendAsColumn(TColumnRef array) {
        if (!Data_->Type()->IsEqual(array->Type())) {
            ythrow yexception()
                << "can't append column of type " << array->Type()->GetName() << " "
                << "to column type " << Data_->Type()->GetName();
        }

        if (Offsets_->Size() == 0) {
            Offsets_->Append(array->Size());
        } else {
            Offsets_->Append((*Offsets_)[Offsets_->Size() - 1] + array->Size());
        }

        Data_->Append(array);
    }

    void TColumnArray::Append(TColumnRef column) {
        if (auto col = column->As<TColumnArray>()) {
            if (!col->Data_->Type()->IsEqual(Data_->Type())) {
                return;
            }

            for (size_t i = 0; i < col->Size(); ++i) {
                AppendAsColumn(col->GetAsColumn(i));
            }
        }
    }

    TColumnRef TColumnArray::GetAsColumn(size_t n) const {
        return Data_->Slice(GetOffset(n), GetSize(n));
    }

    bool TColumnArray::Load(TCodedInputStream* input, size_t rows) {
        if (!Offsets_->Load(input, rows)) {
            return false;
        }
        if (!Data_->Load(input, (*Offsets_)[rows - 1])) {
            return false;
        }
        return true;
    }

    void TColumnArray::Save(TCodedOutputStream* output) {
        Offsets_->Save(output);
        Data_->Save(output);
    }

    size_t TColumnArray::Size() const {
        return Offsets_->Size();
    }

    size_t TColumnArray::GetOffset(size_t n) const {
        return (n == 0) ? 0 : (*Offsets_)[n - 1];
    }

    size_t TColumnArray::GetSize(size_t n) const {
        return (n == 0) ? (*Offsets_)[n] : ((*Offsets_)[n] - (*Offsets_)[n - 1]);
    }

}