-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add partition field/partition spec #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+665
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/partition_field.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include "iceberg/transform.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/formatter.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| PartitionField::PartitionField(int32_t source_id, int32_t field_id, std::string name, | ||
| std::shared_ptr<TransformFunction> transform) | ||
| : source_id_(source_id), | ||
| field_id_(field_id), | ||
| name_(std::move(name)), | ||
| transform_(std::move(transform)) {} | ||
|
|
||
| int32_t PartitionField::source_id() const { return source_id_; } | ||
|
|
||
| int32_t PartitionField::field_id() const { return field_id_; } | ||
|
|
||
| std::string_view PartitionField::name() const { return name_; } | ||
|
|
||
| std::shared_ptr<TransformFunction> const& PartitionField::transform() const { | ||
| return transform_; | ||
| } | ||
|
|
||
| std::string PartitionField::ToString() const { | ||
| return std::format("{} ({} {}({}))", name_, field_id_, *transform_, source_id_); | ||
| } | ||
|
|
||
| bool PartitionField::Equals(const PartitionField& other) const { | ||
| return source_id_ == other.source_id_ && field_id_ == other.field_id_ && | ||
| name_ == other.name_ && *transform_ == *other.transform_; | ||
| } | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/partition_field.h | ||
| /// A partition field in a partition spec | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/type_fwd.h" | ||
| #include "iceberg/util/formattable.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief a field with its transform. | ||
| class ICEBERG_EXPORT PartitionField : public util::Formattable { | ||
| public: | ||
| /// \brief Construct a field. | ||
| /// \param[in] source_id The source field ID. | ||
| /// \param[in] field_id The partition field ID. | ||
| /// \param[in] name The partition field name. | ||
| /// \param[in] transform The transform function. | ||
| PartitionField(int32_t source_id, int32_t field_id, std::string name, | ||
| std::shared_ptr<TransformFunction> transform); | ||
|
|
||
| /// \brief Get the source field ID. | ||
| int32_t source_id() const; | ||
|
|
||
| /// \brief Get the partition field ID. | ||
| int32_t field_id() const; | ||
|
|
||
| /// \brief Get the partition field name. | ||
| std::string_view name() const; | ||
|
|
||
| /// \brief Get the transform type. | ||
| std::shared_ptr<TransformFunction> const& transform() const; | ||
|
|
||
| std::string ToString() const override; | ||
|
|
||
| friend bool operator==(const PartitionField& lhs, const PartitionField& rhs) { | ||
| return lhs.Equals(rhs); | ||
| } | ||
|
|
||
| friend bool operator!=(const PartitionField& lhs, const PartitionField& rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| private: | ||
| /// \brief Compare two fields for equality. | ||
| [[nodiscard]] bool Equals(const PartitionField& other) const; | ||
|
|
||
| int32_t source_id_; | ||
| int32_t field_id_; | ||
| std::string name_; | ||
| std::shared_ptr<TransformFunction> transform_; | ||
| }; | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/partition_spec.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include "iceberg/schema.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/formatter.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| PartitionSpec::PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id, | ||
| std::vector<PartitionField> fields) | ||
| : schema_(std::move(schema)), spec_id_(spec_id), fields_(std::move(fields)) {} | ||
|
|
||
| const std::shared_ptr<Schema>& PartitionSpec::schema() const { return schema_; } | ||
|
|
||
| int32_t PartitionSpec::spec_id() const { return spec_id_; } | ||
|
|
||
| std::span<const PartitionField> PartitionSpec::fields() const { return fields_; } | ||
|
|
||
| std::string PartitionSpec::ToString() const { | ||
| std::string repr = std::format("partition_spec[spec_id<{}>,\n", spec_id_); | ||
| for (const auto& field : fields_) { | ||
| std::format_to(std::back_inserter(repr), " {}\n", field); | ||
gty404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| repr += "]"; | ||
| return repr; | ||
| } | ||
|
|
||
| bool PartitionSpec::Equals(const PartitionSpec& other) const { | ||
| return *schema_ == *other.schema_ && spec_id_ == other.spec_id_ && | ||
| fields_ == other.fields_; | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/partition_spec.h | ||
| /// Partition specs for Iceberg tables. | ||
|
|
||
| #include <cstdint> | ||
| #include <span> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/partition_field.h" | ||
| #include "iceberg/util/formattable.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief A partition spec for a Table. | ||
| /// | ||
| /// A partition spec is a list of partition fields, along with a unique integer ID. A | ||
| /// Table may have different partition specs over its lifetime due to partition spec | ||
| /// evolution. | ||
| class ICEBERG_EXPORT PartitionSpec : public util::Formattable { | ||
| public: | ||
| PartitionSpec(std::shared_ptr<Schema> schema, int32_t spec_id, | ||
| std::vector<PartitionField> fields); | ||
| /// \brief Get the table schema | ||
| const std::shared_ptr<Schema>& schema() const; | ||
| /// \brief Get the spec ID. | ||
| int32_t spec_id() const; | ||
| /// \brief Get a view of the partition fields. | ||
| std::span<const PartitionField> fields() const; | ||
|
|
||
| std::string ToString() const override; | ||
|
|
||
| friend bool operator==(const PartitionSpec& lhs, const PartitionSpec& rhs) { | ||
| return lhs.Equals(rhs); | ||
| } | ||
|
|
||
| friend bool operator!=(const PartitionSpec& lhs, const PartitionSpec& rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| private: | ||
| /// \brief Compare two partition specs for equality. | ||
| [[nodiscard]] bool Equals(const PartitionSpec& other) const; | ||
|
|
||
| std::shared_ptr<Schema> schema_; | ||
| const int32_t spec_id_; | ||
| std::vector<PartitionField> fields_; | ||
| }; | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/transform.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
| /// \brief Get the relative transform name | ||
| constexpr std::string_view ToString(TransformType type) { | ||
| switch (type) { | ||
| case TransformType::kUnknown: | ||
| return "unknown"; | ||
| case TransformType::kIdentity: | ||
| return "identity"; | ||
| case TransformType::kBucket: | ||
| return "bucket"; | ||
| case TransformType::kTruncate: | ||
| return "truncate"; | ||
| case TransformType::kYear: | ||
| return "year"; | ||
| case TransformType::kMonth: | ||
| return "month"; | ||
| case TransformType::kDay: | ||
| return "day"; | ||
| case TransformType::kHour: | ||
| return "hour"; | ||
| case TransformType::kVoid: | ||
| return "void"; | ||
| default: | ||
| return "invalid"; | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| TransformFunction::TransformFunction(TransformType type) : transform_type_(type) {} | ||
|
|
||
| TransformType TransformFunction::transform_type() const { return transform_type_; } | ||
|
|
||
| std::string TransformFunction::ToString() const { | ||
| return std::format("{}", iceberg::ToString(transform_type_)); | ||
| } | ||
|
|
||
| bool TransformFunction::Equals(const TransformFunction& other) const { | ||
| return transform_type_ == other.transform_type_; | ||
| } | ||
|
|
||
| IdentityTransformFunction::IdentityTransformFunction() | ||
| : TransformFunction(TransformType::kIdentity) {} | ||
|
|
||
| expected<ArrowArray, Error> IdentityTransformFunction::Transform( | ||
| const ArrowArray& input) { | ||
| return unexpected<Error>({.kind = ErrorKind::kNotSupported, | ||
| .message = "IdentityTransformFunction::Transform"}); | ||
| } | ||
|
|
||
| } // namespace iceberg |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.