userver: userver/storages/clickhouse/io/columns/array_column.hpp Source File
Loading...
Searching...
No Matches
array_column.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/clickhouse/io/columns/array_column.hpp
4/// @brief Array column support
5/// @ingroup userver_clickhouse_types
6
7#include <userver/utils/assert.hpp>
8
9#include <userver/storages/clickhouse/io/columns/column_includes.hpp>
10#include <userver/storages/clickhouse/io/columns/common_columns.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace storages::clickhouse::io::columns {
15
16struct ArrayColumnMeta final {
17 ColumnRef data;
18 ColumnRef offsets;
19};
20
21ColumnRef ConvertMetaToColumn(ArrayColumnMeta&& meta);
22ColumnRef ExtractArrayItem(const ColumnRef& column, std::size_t ind);
23
24/// @brief Represents ClickHouse Array(T) column,
25/// where T is a ClickhouseColumn as well
26template <typename T>
27class ArrayColumn final : public ClickhouseColumn<ArrayColumn<T>> {
28public:
29 using cpp_type = std::vector<typename T::cpp_type>;
30 using container_type = std::vector<cpp_type>;
31
32 ArrayColumn(ColumnRef column);
33
34 class ArrayDataHolder final {
35 public:
36 ArrayDataHolder() = default;
37 ArrayDataHolder(typename ColumnIterator<ArrayColumn<T>>::IteratorPosition iter_position, ColumnRef&& column);
38
39 ArrayDataHolder operator++(int);
40 ArrayDataHolder& operator++();
41 cpp_type& UpdateValue();
42
43 bool operator==(const ArrayDataHolder& other) const;
44
45 private:
46 ColumnRef inner_{};
47 std::size_t ind_{0};
48 std::optional<cpp_type> current_value_ = std::nullopt;
49 };
50 using iterator_data = ArrayDataHolder;
51
52 static ColumnRef Serialize(const container_type& from);
53 static cpp_type RetrieveElement(const ColumnRef& ref, std::size_t ind);
54};
55
56template <typename T>
57ArrayColumn<T>::ArrayDataHolder::ArrayDataHolder(
58 typename ColumnIterator<ArrayColumn<T>>::IteratorPosition iter_position,
59 ColumnRef&& column
60)
61 : inner_{std::move(column)}, ind_(iter_position == decltype(iter_position)::kEnd ? GetColumnSize(inner_) : 0) {}
62
63template <typename T>
64typename ArrayColumn<T>::ArrayDataHolder ArrayColumn<T>::ArrayDataHolder::operator++(int) {
65 ArrayDataHolder old{};
66 old.inner_ = inner_;
67 old.ind_ = ind_++;
68 old.current_value_ = std::move_if_noexcept(current_value_);
69 current_value_.reset();
70
71 return old;
72}
73
74template <typename T>
75typename ArrayColumn<T>::ArrayDataHolder& ArrayColumn<T>::ArrayDataHolder::operator++() {
76 ++ind_;
77 current_value_.reset();
78
79 return *this;
80}
81
82template <typename T>
83typename ArrayColumn<T>::cpp_type& ArrayColumn<T>::ArrayDataHolder::UpdateValue() {
84 UASSERT(ind_ < GetColumnSize(inner_));
85 if (!current_value_.has_value()) {
86 cpp_type item = RetrieveElement(inner_, ind_);
87 current_value_.emplace(std::move(item));
88 }
89
90 return *current_value_;
91}
92
93template <typename T>
94bool ArrayColumn<T>::ArrayDataHolder::operator==(const ArrayDataHolder& other) const {
95 return inner_.get() == other.inner_.get() && ind_ == other.ind_;
96}
97
98template <typename T>
99ArrayColumn<T>::ArrayColumn(ColumnRef column) : ClickhouseColumn<ArrayColumn>{column} {}
100
101template <typename T>
102ColumnRef ArrayColumn<T>::Serialize(const container_type& from) {
103 uint64_t cumulative_offset = 0;
104 std::vector<uint64_t> offsets;
105 offsets.reserve(from.size());
106
107 for (const auto& value : from) {
108 cumulative_offset += value.size();
109 offsets.push_back(cumulative_offset);
110 }
111 typename T::container_type values;
112 values.reserve(cumulative_offset);
113
114 for (const auto& value : from) {
115 for (const auto& item : value) {
116 values.push_back(item);
117 }
118 }
119
120 ArrayColumnMeta array_meta;
121 array_meta.offsets = UInt64Column::Serialize(offsets);
122 array_meta.data = T::Serialize(values);
123
124 return ConvertMetaToColumn(std::move(array_meta));
125}
126
127template <typename T>
128typename ArrayColumn<T>::cpp_type ArrayColumn<T>::RetrieveElement(const ColumnRef& ref, std::size_t ind) {
129 auto array_item = ExtractArrayItem(ref, ind);
130 T typed_column(array_item);
131
132 cpp_type result;
133 result.reserve(GetColumnSize(array_item));
134 for (auto it = typed_column.begin(); it != typed_column.end(); ++it) {
135 result.push_back(std::move(*it));
136 }
137 return result;
138}
139
140} // namespace storages::clickhouse::io::columns
141
142USERVER_NAMESPACE_END