Skip to content

Commit 720f908

Browse files
committed
Add partition field/partition spec/transform
1 parent faf9cc8 commit 720f908

14 files changed

+658
-2
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ set(ICEBERG_SOURCES
2323
schema.cc
2424
schema_field.cc
2525
schema_internal.cc
26-
type.cc)
26+
type.cc
27+
type.cc
28+
transform.cc
29+
partition_field.cc
30+
partition_spec.cc)
2731

2832
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
2933
set(ICEBERG_SHARED_BUILD_INTERFACE_LIBS)

src/iceberg/error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum class ErrorKind {
3434
kCommitStateUnknown,
3535
kInvalidSchema,
3636
kInvalidArgument,
37+
kNotSupported,
3738
};
3839

3940
/// \brief Error with a kind and a message.

src/iceberg/exception.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ class ICEBERG_EXPORT IcebergError : public std::runtime_error {
3838
explicit IcebergError(const std::string& what) : std::runtime_error(what) {}
3939
};
4040

41+
#define ICEBERG_CHECK(condition, ...) \
42+
do { \
43+
if (!(condition)) [[unlikely]] { \
44+
throw iceberg::IcebergError(std::format(__VA_ARGS__)); \
45+
} \
46+
} while (0)
47+
4148
} // namespace iceberg

src/iceberg/partition_field.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_field.h"
21+
22+
#include <format>
23+
24+
#include "iceberg/transform.h"
25+
#include "iceberg/type.h"
26+
#include "iceberg/util/formatter.h"
27+
28+
namespace iceberg {
29+
30+
PartitionField::PartitionField(int32_t source_field_id, int32_t field_id,
31+
std::string name,
32+
std::shared_ptr<TransformFunction> transform)
33+
: source_field_id_(source_field_id),
34+
field_id_(field_id),
35+
name_(std::move(name)),
36+
transform_(std::move(transform)) {}
37+
38+
int32_t PartitionField::source_field_id() const { return source_field_id_; }
39+
40+
int32_t PartitionField::field_id() const { return field_id_; }
41+
42+
std::string_view PartitionField::name() const { return name_; }
43+
44+
std::shared_ptr<TransformFunction> const& PartitionField::transform() const {
45+
return transform_;
46+
}
47+
48+
std::string PartitionField::ToString() const {
49+
return std::format("{} ({} {}({}))", name_, field_id_, *transform_, source_field_id_);
50+
}
51+
52+
bool PartitionField::Equals(const PartitionField& other) const {
53+
return source_field_id_ == other.source_field_id_ && field_id_ == other.field_id_ &&
54+
name_ == other.name_ && *transform_ == *other.transform_;
55+
}
56+
57+
} // namespace iceberg

src/iceberg/partition_field.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
22+
/// \file iceberg/partition_field.h
23+
/// A partition field in a partition spec
24+
25+
#include <cstdint>
26+
#include <memory>
27+
#include <string>
28+
#include <string_view>
29+
30+
#include "iceberg/iceberg_export.h"
31+
#include "iceberg/type_fwd.h"
32+
#include "iceberg/util/formattable.h"
33+
34+
namespace iceberg {
35+
36+
/// \brief a field with its transform.
37+
class ICEBERG_EXPORT PartitionField : public util::Formattable {
38+
public:
39+
/// \brief Construct a field.
40+
/// \param[in] source_id The source field ID.
41+
/// \param[in] field_id The partition field ID.
42+
/// \param[in] name The partition field name.
43+
/// \param[in] transform The transform function.
44+
PartitionField(int32_t source_id, int32_t field_id, std::string name,
45+
std::shared_ptr<TransformFunction> transform);
46+
47+
/// \brief Get the source field ID.
48+
[[nodiscard]] int32_t source_field_id() const;
49+
50+
/// \brief Get the partition field ID.
51+
[[nodiscard]] int32_t field_id() const;
52+
53+
/// \brief Get the partition field name.
54+
[[nodiscard]] std::string_view name() const;
55+
56+
/// \brief Get the transform type.
57+
[[nodiscard]] std::shared_ptr<TransformFunction> const& transform() const;
58+
59+
[[nodiscard]] std::string ToString() const override;
60+
61+
friend bool operator==(const PartitionField& lhs, const PartitionField& rhs) {
62+
return lhs.Equals(rhs);
63+
}
64+
65+
friend bool operator!=(const PartitionField& lhs, const PartitionField& rhs) {
66+
return !(lhs == rhs);
67+
}
68+
69+
private:
70+
/// \brief Compare two fields for equality.
71+
[[nodiscard]] bool Equals(const PartitionField& other) const;
72+
73+
int32_t source_field_id_;
74+
int32_t field_id_;
75+
std::string name_;
76+
std::shared_ptr<TransformFunction> transform_;
77+
};
78+
79+
} // namespace iceberg

src/iceberg/partition_spec.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_spec.h"
21+
22+
#include <format>
23+
24+
#include "iceberg/exception.h"
25+
#include "iceberg/schema.h"
26+
#include "iceberg/type.h"
27+
#include "iceberg/util/formatter.h"
28+
29+
namespace iceberg {
30+
31+
PartitionSpec::PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id,
32+
std::vector<PartitionField> fields)
33+
: schema_(std::move(schema)), spec_id_(spec_id), fields_(std::move(fields)) {}
34+
35+
const std::shared_ptr<Schema>& PartitionSpec::schema() const { return schema_; }
36+
37+
int32_t PartitionSpec::spec_id() const { return spec_id_; }
38+
39+
std::span<const PartitionField> PartitionSpec::fields() const { return fields_; }
40+
41+
std::string PartitionSpec::ToString() const {
42+
std::string repr = "partition_spec(<";
43+
for (const auto& field : fields_) {
44+
std::format_to(std::back_inserter(repr), " {}\n", field);
45+
}
46+
repr += ">";
47+
return repr;
48+
}
49+
50+
bool PartitionSpec::Equals(const PartitionSpec& other) const {
51+
return *schema_ == *other.schema_ && spec_id_ == other.spec_id_ &&
52+
fields_ == other.fields_;
53+
}
54+
55+
} // namespace iceberg

src/iceberg/partition_spec.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
22+
/// \file iceberg/partition_spec.h
23+
/// Partition specs for Iceberg tables.
24+
25+
#include <cstdint>
26+
#include <span>
27+
#include <string>
28+
#include <vector>
29+
30+
#include "iceberg/iceberg_export.h"
31+
#include "iceberg/partition_field.h"
32+
33+
namespace iceberg {
34+
35+
/// \brief A partition spec for a Table.
36+
///
37+
/// A partition spec is a list of partition fields, along with a unique integer ID. A
38+
/// Table may have different partition specs over its lifetime due to partition spec
39+
/// evolution.
40+
class ICEBERG_EXPORT PartitionSpec {
41+
public:
42+
virtual ~PartitionSpec() = default;
43+
PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id,
44+
std::vector<PartitionField> fields);
45+
/// \brief Get the partition schema
46+
[[nodiscard]] const std::shared_ptr<Schema>& schema() const;
47+
/// \brief Get the spec ID.
48+
[[nodiscard]] int32_t spec_id() const;
49+
/// \brief Get a view of the partition fields.
50+
[[nodiscard]] virtual std::span<const PartitionField> fields() const;
51+
52+
[[nodiscard]] std::string ToString() const;
53+
54+
friend bool operator==(const PartitionSpec& lhs, const PartitionSpec& rhs) {
55+
return lhs.Equals(rhs);
56+
}
57+
58+
friend bool operator!=(const PartitionSpec& lhs, const PartitionSpec& rhs) {
59+
return !(lhs == rhs);
60+
}
61+
62+
private:
63+
/// \brief Compare two partition specs for equality.
64+
[[nodiscard]] bool Equals(const PartitionSpec& other) const;
65+
66+
std::shared_ptr<Schema> schema_;
67+
const int32_t spec_id_;
68+
std::vector<PartitionField> fields_;
69+
};
70+
71+
} // namespace iceberg

src/iceberg/transform.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/transform.h"
21+
22+
#include <format>
23+
24+
namespace iceberg {
25+
26+
TransformFunction::TransformFunction(TransformType type) : transform_type_(type) {}
27+
28+
TransformType TransformFunction::transform_type() const { return transform_type_; }
29+
30+
std::string TransformFunction::ToString() const {
31+
return std::format("{}", iceberg::ToString(transform_type_));
32+
}
33+
34+
bool TransformFunction::Equals(const TransformFunction& other) const {
35+
return transform_type_ == other.transform_type_;
36+
}
37+
38+
} // namespace iceberg

0 commit comments

Comments
 (0)