iceberg-cpp
Loading...
Searching...
No Matches
partition_spec.h
Go to the documentation of this file.
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#pragma once
21
24
25#include <cstdint>
26#include <memory>
27#include <optional>
28#include <span>
29#include <string>
30#include <unordered_map>
31#include <vector>
32
33#include "iceberg/iceberg_export.h"
35#include "iceberg/result.h"
36#include "iceberg/type_fwd.h"
38#include "iceberg/util/lazy.h"
39
40namespace iceberg {
41
47class ICEBERG_EXPORT PartitionSpec : public util::Formattable {
48 public:
49 static constexpr int32_t kInitialSpecId = 0;
52 static constexpr int32_t kLegacyPartitionDataIdStart = 1000;
53 static constexpr int32_t kInvalidPartitionFieldId = -1;
54
56 static const std::shared_ptr<PartitionSpec>& Unpartitioned();
57
59 int32_t spec_id() const;
60
62 std::span<const PartitionField> fields() const;
63
65 Result<std::unique_ptr<StructType>> PartitionType(const Schema& schema) const;
66
68 Result<std::string> PartitionPath(const PartitionValues& data) const;
69
73 bool CompatibleWith(const PartitionSpec& other) const;
74
75 std::string ToString() const override;
76
77 int32_t last_assigned_field_id() const { return last_assigned_field_id_; }
78
79 friend bool operator==(const PartitionSpec& lhs, const PartitionSpec& rhs) {
80 return lhs.Equals(rhs);
81 }
82
88 Status Validate(const Schema& schema, bool allow_missing_fields) const;
89
90 // \brief Validates the partition field names are unique within the partition spec and
91 // schema.
92 static Status ValidatePartitionName(const Schema& schema, const PartitionSpec& spec);
93
98 using PartitionFieldRef = std::reference_wrapper<const PartitionField>;
99 Result<std::vector<PartitionFieldRef>> GetFieldsBySourceId(int32_t source_id) const;
100
110 static Result<std::unique_ptr<PartitionSpec>> Make(
111 const Schema& schema, int32_t spec_id, std::vector<PartitionField> fields,
112 bool allow_missing_fields,
113 std::optional<int32_t> last_assigned_field_id = std::nullopt);
114
122 static Result<std::unique_ptr<PartitionSpec>> Make(
123 int32_t spec_id, std::vector<PartitionField> fields,
124 std::optional<int32_t> last_assigned_field_id = std::nullopt);
125
126 static bool HasSequentialFieldIds(const PartitionSpec& spec);
127
128 private:
135 PartitionSpec(int32_t spec_id, std::vector<PartitionField> fields,
136 std::optional<int32_t> last_assigned_field_id = std::nullopt);
137
139 bool Equals(const PartitionSpec& other) const;
140
142 static Status ValidateRedundantPartitions(const PartitionSpec& spec);
143
144 using SourceIdToFieldsMap = std::unordered_map<int32_t, std::vector<PartitionFieldRef>>;
145 static Result<SourceIdToFieldsMap> InitSourceIdToFieldsMap(const PartitionSpec&);
146
147 const int32_t spec_id_;
148 std::vector<PartitionField> fields_;
149 int32_t last_assigned_field_id_;
150 Lazy<InitSourceIdToFieldsMap> source_id_to_fields_;
151};
152
153} // namespace iceberg
A partition spec for a Table.
Definition partition_spec.h:47
std::reference_wrapper< const PartitionField > PartitionFieldRef
Get the partition fields by source ID.
Definition partition_spec.h:98
StructLike wrapper for a vector of literals that represent partition values.
Definition partition_values.h:36
A schema for a Table.
Definition schema.h:49
Interface for objects that can be formatted via std::format.
Definition formattable.h:36