10#include <userver/utils/assert.hpp>
12#include <userver/storages/clickhouse/io/columns/column_includes.hpp>
13#include <userver/storages/clickhouse/io/columns/common_columns.hpp>
15USERVER_NAMESPACE_BEGIN
19struct NullableColumnMeta
final {
24NullableColumnMeta ExtractNullableMeta(
const ColumnRef& column);
26ColumnRef ConvertMetaToColumn(NullableColumnMeta&& meta);
33 using cpp_type = std::optional<
typename T::cpp_type>;
34 using container_type = std::vector<cpp_type>;
36 class NullableDataHolder
final {
38 NullableDataHolder() =
default;
40 typename ColumnIterator<NullableColumn<T>>::IteratorPosition iter_position,
44 NullableDataHolder operator++(
int);
45 NullableDataHolder& operator++();
47 cpp_type& UpdateValue();
49 bool operator==(
const NullableDataHolder& other)
const;
53 typename ColumnIterator<NullableColumn<T>>::IteratorPosition iter_position,
54 NullableColumnMeta&& meta
57 UInt8Column::iterator nulls_;
58 typename T::iterator inner_;
59 cpp_type current_value_ = std::nullopt;
60 bool has_value_ =
false;
62 using iterator_data = NullableDataHolder;
64 NullableColumn(ColumnRef column);
66 static ColumnRef Serialize(
const container_type& from);
70NullableColumn<T>::NullableColumn(ColumnRef column) : ClickhouseColumn<NullableColumn>{column} {}
73NullableColumn<T>::NullableDataHolder::NullableDataHolder(
74 typename ColumnIterator<NullableColumn<T>>::IteratorPosition iter_position,
77 : NullableDataHolder(iter_position, ExtractNullableMeta(column)) {}
80NullableColumn<T>::NullableDataHolder::NullableDataHolder(
81 typename ColumnIterator<NullableColumn<T>>::IteratorPosition iter_position,
82 NullableColumnMeta&& meta
84 : nulls_{iter_position ==
decltype(iter_position)::kEnd ? UInt8Column{meta.nulls}.end() : UInt8Column{meta.nulls}.begin()},
85 inner_{iter_position ==
decltype(iter_position)::kEnd ? T{meta.inner}.end() : T{meta.inner}.begin()} {}
88typename NullableColumn<T>::NullableDataHolder NullableColumn<T>::NullableDataHolder::operator++(
int) {
89 NullableDataHolder old{};
90 old.nulls_ = nulls_++;
91 old.inner_ = inner_++;
92 old.current_value_ = std::move_if_noexcept(current_value_);
93 old.has_value_ = std::exchange(has_value_,
false);
99typename NullableColumn<T>::NullableDataHolder& NullableColumn<T>::NullableDataHolder::operator++() {
102 current_value_.reset();
109typename NullableColumn<T>::cpp_type& NullableColumn<T>::NullableDataHolder::UpdateValue() {
112 current_value_.reset();
114 current_value_.emplace(std::move_if_noexcept(*inner_));
119 return current_value_;
123bool NullableColumn<T>::NullableDataHolder::operator==(
const NullableDataHolder& other)
const {
124 return nulls_ == other.nulls_ && inner_ == other.inner_;
128ColumnRef NullableColumn<T>::Serialize(
const container_type& from) {
129 typename T::container_type values;
130 values.reserve(from.size());
131 std::vector<uint8_t> nulls;
132 nulls.reserve(from.size());
134 for (
auto& opt_v : from) {
135 nulls.push_back(
static_cast<uint8_t>(opt_v.has_value() ? 0 : 1));
137 if (opt_v.has_value()) {
138 values.push_back(*opt_v);
140 values.push_back(
typename T::cpp_type{});
144 NullableColumnMeta nullable_meta;
145 nullable_meta.nulls = UInt8Column::Serialize(nulls);
146 nullable_meta.inner = T::Serialize(values);
147 return ConvertMetaToColumn(std::move(nullable_meta));