|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/partition_summary_internal.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +#include "iceberg/expression/literal.h" |
| 25 | +#include "iceberg/manifest_list.h" |
| 26 | +#include "iceberg/result.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +Status PartitionFieldStats::Update(const Literal& value) { |
| 31 | + if (type_->type_id() != value.type()->type_id()) { |
| 32 | + return InvalidArgument("value is not compatible with type"); |
| 33 | + } |
| 34 | + |
| 35 | + if (value.IsNull()) { |
| 36 | + contains_null_ = true; |
| 37 | + return {}; |
| 38 | + } |
| 39 | + |
| 40 | + if (value.IsNan()) { |
| 41 | + contains_nan_ = true; |
| 42 | + return {}; |
| 43 | + } |
| 44 | + |
| 45 | + if (!lower_bound_ || value < *lower_bound_) { |
| 46 | + lower_bound_ = value; |
| 47 | + } |
| 48 | + if (!upper_bound_ || value > *upper_bound_) { |
| 49 | + upper_bound_ = value; |
| 50 | + } |
| 51 | + return {}; |
| 52 | +} |
| 53 | + |
| 54 | +PartitionFieldSummary PartitionFieldStats::Finish() const { |
| 55 | + PartitionFieldSummary summary; |
| 56 | + summary.contains_null = contains_null_; |
| 57 | + summary.contains_nan = contains_nan_; |
| 58 | + if (lower_bound_) { |
| 59 | + summary.lower_bound = lower_bound_->Serialize().value(); |
| 60 | + } |
| 61 | + if (upper_bound_) { |
| 62 | + summary.upper_bound = upper_bound_->Serialize().value(); |
| 63 | + } |
| 64 | + return summary; |
| 65 | +} |
| 66 | + |
| 67 | +Status PartitionSummary::Update(const std::vector<Literal>& partition) { |
| 68 | + if (partition.size() != field_stats_.size()) { |
| 69 | + return InvalidArgument("partition size does not match field stats size"); |
| 70 | + } |
| 71 | + |
| 72 | + for (size_t i = 0; i < partition.size(); i++) { |
| 73 | + ICEBERG_RETURN_UNEXPECTED(field_stats_[i].Update(partition[i])); |
| 74 | + } |
| 75 | + return {}; |
| 76 | +} |
| 77 | + |
| 78 | +std::vector<PartitionFieldSummary> PartitionSummary::Summaries() const { |
| 79 | + std::vector<PartitionFieldSummary> summaries; |
| 80 | + for (const auto& field_stat : field_stats_) { |
| 81 | + summaries.push_back(field_stat.Finish()); |
| 82 | + } |
| 83 | + return summaries; |
| 84 | +} |
| 85 | + |
| 86 | +Result<std::unique_ptr<PartitionSummary>> PartitionSummary::Make( |
| 87 | + const StructType& partition_type) { |
| 88 | + std::vector<PartitionFieldStats> field_stats; |
| 89 | + for (const auto& field : partition_type.fields()) { |
| 90 | + field_stats.emplace_back(field.type()); |
| 91 | + } |
| 92 | + return std::unique_ptr<PartitionSummary>(new PartitionSummary(std::move(field_stats))); |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace iceberg |
0 commit comments