1#include "duckdb/storage/statistics/column_statistics.hpp"
2#include "duckdb/common/serializer.hpp"
3
4namespace duckdb {
5
6ColumnStatistics::ColumnStatistics(BaseStatistics stats_p) : stats(std::move(stats_p)) {
7 if (DistinctStatistics::TypeIsSupported(type: stats.GetType())) {
8 distinct_stats = make_uniq<DistinctStatistics>();
9 }
10}
11ColumnStatistics::ColumnStatistics(BaseStatistics stats_p, unique_ptr<DistinctStatistics> distinct_stats_p)
12 : stats(std::move(stats_p)), distinct_stats(std::move(distinct_stats_p)) {
13}
14
15shared_ptr<ColumnStatistics> ColumnStatistics::CreateEmptyStats(const LogicalType &type) {
16 return make_shared<ColumnStatistics>(args: BaseStatistics::CreateEmpty(type));
17}
18
19void ColumnStatistics::Merge(ColumnStatistics &other) {
20 stats.Merge(other: other.stats);
21 if (distinct_stats) {
22 distinct_stats->Merge(other: *other.distinct_stats);
23 }
24}
25
26BaseStatistics &ColumnStatistics::Statistics() {
27 return stats;
28}
29
30bool ColumnStatistics::HasDistinctStats() {
31 return distinct_stats.get();
32}
33
34DistinctStatistics &ColumnStatistics::DistinctStats() {
35 if (!distinct_stats) {
36 throw InternalException("DistinctStats called without distinct_stats");
37 }
38 return *distinct_stats;
39}
40
41void ColumnStatistics::SetDistinct(unique_ptr<DistinctStatistics> distinct) {
42 this->distinct_stats = std::move(distinct);
43}
44
45void ColumnStatistics::UpdateDistinctStatistics(Vector &v, idx_t count) {
46 if (!distinct_stats) {
47 return;
48 }
49 auto &d_stats = (DistinctStatistics &)*distinct_stats;
50 d_stats.Update(update&: v, count);
51}
52
53shared_ptr<ColumnStatistics> ColumnStatistics::Copy() const {
54 return make_shared<ColumnStatistics>(args: stats.Copy(), args: distinct_stats ? distinct_stats->Copy() : nullptr);
55}
56void ColumnStatistics::Serialize(Serializer &serializer) const {
57 stats.Serialize(serializer);
58 serializer.WriteOptional(element: distinct_stats);
59}
60
61shared_ptr<ColumnStatistics> ColumnStatistics::Deserialize(Deserializer &source, const LogicalType &type) {
62 auto stats = BaseStatistics::Deserialize(source, type);
63 auto distinct_stats = source.ReadOptional<DistinctStatistics>();
64 return make_shared<ColumnStatistics>(args: stats.Copy(), args: std::move(distinct_stats));
65}
66
67} // namespace duckdb
68