Skip to content

Commit be03f0a

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

14 files changed

+669
-1
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ set(ICEBERG_SOURCES
2323
schema.cc
2424
schema_field.cc
2525
schema_internal.cc
26+
partition_field.cc
27+
partition_spec.cc
28+
transform.cc
2629
type.cc)
2730

2831
set(ICEBERG_STATIC_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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_id, int32_t field_id, std::string name,
31+
std::shared_ptr<TransformFunction> transform)
32+
: source_id_(source_id),
33+
field_id_(field_id),
34+
name_(std::move(name)),
35+
transform_(std::move(transform)) {}
36+
37+
int32_t PartitionField::source_id() const { return source_id_; }
38+
39+
int32_t PartitionField::field_id() const { return field_id_; }
40+
41+
std::string_view PartitionField::name() const { return name_; }
42+
43+
std::shared_ptr<TransformFunction> const& PartitionField::transform() const {
44+
return transform_;
45+
}
46+
47+
std::string PartitionField::ToString() const {
48+
return std::format("{} ({} {}({}))", name_, field_id_, *transform_, source_id_);
49+
}
50+
51+
bool PartitionField::Equals(const PartitionField& other) const {
52+
return source_id_ == other.source_id_ && field_id_ == other.field_id_ &&
53+
name_ == other.name_ && *transform_ == *other.transform_;
54+
}
55+
56+
} // namespace iceberg

src/iceberg/partition_field.h

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

src/iceberg/partition_spec.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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/schema.h"
25+
#include "iceberg/type.h"
26+
#include "iceberg/util/formatter.h"
27+
28+
namespace iceberg {
29+
30+
PartitionSpec::PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id,
31+
std::vector<PartitionField> fields)
32+
: schema_(std::move(schema)), spec_id_(spec_id), fields_(std::move(fields)) {}
33+
34+
const std::shared_ptr<Schema>& PartitionSpec::schema() const { return schema_; }
35+
36+
int32_t PartitionSpec::spec_id() const { return spec_id_; }
37+
38+
std::span<const PartitionField> PartitionSpec::fields() const { return fields_; }
39+
40+
std::string PartitionSpec::ToString() const {
41+
std::string repr = std::format("partition_spec[spec_id<{}>,\n", spec_id_);
42+
for (const auto& field : fields_) {
43+
std::format_to(std::back_inserter(repr), " {}\n", field);
44+
}
45+
repr += "]";
46+
return repr;
47+
}
48+
49+
bool PartitionSpec::Equals(const PartitionSpec& other) const {
50+
return *schema_ == *other.schema_ && spec_id_ == other.spec_id_ &&
51+
fields_ == other.fields_;
52+
}
53+
54+
} // 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+
#include "iceberg/util/formattable.h"
33+
34+
namespace iceberg {
35+
36+
/// \brief A partition spec for a Table.
37+
///
38+
/// A partition spec is a list of partition fields, along with a unique integer ID. A
39+
/// Table may have different partition specs over its lifetime due to partition spec
40+
/// evolution.
41+
class ICEBERG_EXPORT PartitionSpec : public util::Formattable {
42+
public:
43+
PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id,
44+
std::vector<PartitionField> fields);
45+
/// \brief Get the table schema
46+
const std::shared_ptr<Schema>& schema() const;
47+
/// \brief Get the spec ID.
48+
int32_t spec_id() const;
49+
/// \brief Get a view of the partition fields.
50+
std::span<const PartitionField> fields() const;
51+
52+
std::string ToString() const override;
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)