9#include <userver/utils/assert.hpp>
11#include <userver/storages/clickhouse/io/columns/column_wrapper.hpp>
13USERVER_NAMESPACE_BEGIN
22template <
typename ColumnType>
23class ColumnIterator
final {
25 using iterator_category = std::forward_iterator_tag;
26 using difference_type = std::ptrdiff_t;
27 using value_type =
typename ColumnType::cpp_type;
28 using reference = value_type&;
29 using pointer = value_type*;
31 ColumnIterator() =
default;
32 ColumnIterator(ColumnRef column);
34 static ColumnIterator End(ColumnRef column);
36 ColumnIterator operator++(
int);
37 ColumnIterator& operator++();
38 reference operator*()
const;
39 pointer operator->()
const;
41 bool operator==(
const ColumnIterator& other)
const;
42 bool operator!=(
const ColumnIterator& other)
const;
44 enum class IteratorPosition { kBegin, kEnd };
49 ColumnIterator(IteratorPosition iter_position, ColumnRef&& column);
51 class DataHolder
final {
53 DataHolder() =
default;
54 DataHolder(IteratorPosition iter_position, ColumnRef&& column);
56 DataHolder operator++(
int);
57 DataHolder& operator++();
58 value_type& UpdateValue();
59 bool operator==(
const DataHolder& other)
const;
61 value_type Get()
const;
69 std::optional<value_type> current_value_ = std::nullopt;
72 template <
typename T,
typename =
void>
73 struct IteratorDataHolder
final {
74 using type = DataHolder;
78 struct IteratorDataHolder<T, std::void_t<
typename T::iterator_data>> final {
79 using type =
typename T::iterator_data;
82 using data_holder =
typename IteratorDataHolder<ColumnType>::type;
84 mutable data_holder data_;
87template <
typename ColumnType>
88ColumnIterator<ColumnType>::ColumnIterator(ColumnRef column)
89 : ColumnIterator{IteratorPosition::kBegin, std::move(column)} {}
91template <
typename ColumnType>
92ColumnIterator<ColumnType> ColumnIterator<ColumnType>::End(ColumnRef column) {
93 return ColumnIterator{IteratorPosition::kEnd, std::move(column)};
96template <
typename ColumnType>
97ColumnIterator<ColumnType>::ColumnIterator(IteratorPosition iter_position, ColumnRef&& column)
98 : data_{data_holder{iter_position, std::move(column)}} {}
100template <
typename ColumnType>
101ColumnIterator<ColumnType> ColumnIterator<ColumnType>::operator++(
int) {
102 ColumnIterator<ColumnType> old{};
108template <
typename ColumnType>
109ColumnIterator<ColumnType>& ColumnIterator<ColumnType>::operator++() {
114template <
typename ColumnType>
115typename ColumnIterator<ColumnType>::reference ColumnIterator<ColumnType>::operator*()
const {
116 return data_.UpdateValue();
119template <
typename ColumnType>
120typename ColumnIterator<ColumnType>::pointer ColumnIterator<ColumnType>::operator->()
const {
121 return &data_.UpdateValue();
124template <
typename ColumnType>
125bool ColumnIterator<ColumnType>::operator==(
const ColumnIterator& other)
const {
126 return data_ == other.data_;
129template <
typename ColumnType>
130bool ColumnIterator<ColumnType>::operator!=(
const ColumnIterator<ColumnType>& other)
const {
131 return !((*
this) == other);
134template <
typename ColumnType>
135ColumnIterator<ColumnType>::DataHolder::DataHolder(IteratorPosition iter_position, ColumnRef&& column)
136 : column_{std::move(column)} {
137 switch (iter_position) {
138 case IteratorPosition::kBegin: {
142 case IteratorPosition::kEnd: {
143 ind_ = GetColumnSize(column_);
149template <
typename ColumnType>
150typename ColumnIterator<ColumnType>::DataHolder ColumnIterator<ColumnType>::DataHolder::operator++(
int) {
152 old.column_ = column_;
154 old.current_value_ = std::move_if_noexcept(current_value_);
155 current_value_.reset();
160template <
typename ColumnType>
161typename ColumnIterator<ColumnType>::DataHolder& ColumnIterator<ColumnType>::DataHolder::operator++() {
163 current_value_.reset();
168template <
typename ColumnType>
169typename ColumnIterator<ColumnType>::value_type& ColumnIterator<ColumnType>::DataHolder::UpdateValue() {
170 UASSERT(ind_ < GetColumnSize(column_));
171 if (!current_value_.has_value()) {
172 current_value_.emplace(Get());
174 return *current_value_;
177template <
typename ColumnType>
178bool ColumnIterator<ColumnType>::DataHolder::operator==(
const DataHolder& other)
const {
179 return ind_ == other.ind_ && column_.get() == other.column_.get();