-
Notifications
You must be signed in to change notification settings - Fork 70
feat: sort order #59
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
Merged
feat: sort order #59
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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/sort_field.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include "iceberg/transform.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/formatter.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
| /// \brief Get the relative sort direction name | ||
| constexpr std::string_view ToString(SortDirection direction) { | ||
| switch (direction) { | ||
| case SortDirection::kAscending: | ||
| return "asc"; | ||
| case SortDirection::kDescending: | ||
| return "desc"; | ||
| default: | ||
| return "invalid"; | ||
| } | ||
| } | ||
|
|
||
| /// \brief Get the relative null order name | ||
| constexpr std::string_view ToString(NullOrder null_order) { | ||
| switch (null_order) { | ||
| case NullOrder::kFirst: | ||
| return "nulls-first"; | ||
| case NullOrder::kLast: | ||
| return "nulls-last"; | ||
| default: | ||
| return "invalid"; | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| SortField::SortField(int32_t source_id, std::shared_ptr<TransformFunction> transform, | ||
| SortDirection direction, NullOrder null_order) | ||
| : source_id_(source_id), | ||
| transform_(std::move(transform)), | ||
| direction_(direction), | ||
| null_order_(null_order) {} | ||
|
|
||
| int32_t SortField::source_id() const { return source_id_; } | ||
|
|
||
| std::shared_ptr<TransformFunction> const& SortField::transform() const { | ||
| return transform_; | ||
| } | ||
|
|
||
| SortDirection SortField::direction() const { return direction_; } | ||
|
|
||
| NullOrder SortField::null_order() const { return null_order_; } | ||
|
|
||
| std::string SortField::ToString() const { | ||
| return std::format( | ||
| "sort_field(source_id={}, transform={}, direction={}, null_order={})", source_id_, | ||
| *transform_, iceberg::ToString(direction_), iceberg::ToString(null_order_)); | ||
| } | ||
|
|
||
| bool SortField::Equals(const SortField& other) const { | ||
| return source_id_ == other.source_id_ && *transform_ == *other.transform_ && | ||
| direction_ == other.direction_ && null_order_ == other.null_order_; | ||
| } | ||
|
|
||
| } // 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,95 @@ | ||
| /* | ||
| * 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/sort_field.h | ||
| /// A sort field in a sort order | ||
|
|
||
| #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 Sort direction in a partition, either ascending or descending | ||
| enum class SortDirection { | ||
| /// Ascending | ||
| kAscending, | ||
| /// Descending | ||
| kDescending, | ||
| }; | ||
|
|
||
| enum class NullOrder { | ||
| /// Nulls are sorted first | ||
| kFirst, | ||
| /// Nulls are sorted last | ||
| kLast, | ||
| }; | ||
|
|
||
| /// \brief a field with its transform. | ||
| class ICEBERG_EXPORT SortField : public util::Formattable { | ||
| public: | ||
| /// \brief Construct a field. | ||
| /// \param[in] source_id The source field ID. | ||
| /// \param[in] transform The transform function. | ||
| /// \param[in] direction The sort direction. | ||
| /// \param[in] null_order The null order. | ||
| SortField(int32_t source_id, std::shared_ptr<TransformFunction> transform, | ||
| SortDirection direction, NullOrder null_order); | ||
|
|
||
| /// \brief Get the source field ID. | ||
| int32_t source_id() const; | ||
|
|
||
| /// \brief Get the transform type. | ||
| const std::shared_ptr<TransformFunction>& transform() const; | ||
|
|
||
| /// \brief Get the sort direction. | ||
| SortDirection direction() const; | ||
|
|
||
| /// \brief Get the null order. | ||
| NullOrder null_order() const; | ||
|
|
||
| std::string ToString() const override; | ||
|
|
||
| friend bool operator==(const SortField& lhs, const SortField& rhs) { | ||
| return lhs.Equals(rhs); | ||
| } | ||
|
|
||
| friend bool operator!=(const SortField& lhs, const SortField& rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| private: | ||
| /// \brief Compare two fields for equality. | ||
| [[nodiscard]] bool Equals(const SortField& other) const; | ||
|
|
||
| int32_t source_id_; | ||
| std::shared_ptr<TransformFunction> transform_; | ||
| SortDirection direction_; | ||
| NullOrder null_order_; | ||
| }; | ||
|
|
||
| } // 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,48 @@ | ||
| /* | ||
| * 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/sort_order.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include "iceberg/util/formatter.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| SortOrder::SortOrder(int64_t order_id, std::vector<SortField> fields) | ||
| : order_id_(order_id), fields_(std::move(fields)) {} | ||
|
|
||
| int64_t SortOrder::order_id() const { return order_id_; } | ||
|
|
||
| std::span<const SortField> SortOrder::fields() const { return fields_; } | ||
|
|
||
| std::string SortOrder::ToString() const { | ||
| std::string repr = std::format("sort_order[order_id<{}>,\n", order_id_); | ||
| for (const auto& field : fields_) { | ||
| std::format_to(std::back_inserter(repr), " {}\n", field); | ||
| } | ||
| repr += "]"; | ||
| return repr; | ||
| } | ||
|
|
||
| bool SortOrder::Equals(const SortOrder& other) const { | ||
| return order_id_ == other.order_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,65 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| #include <cstdint> | ||
| #include <span> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/sort_field.h" | ||
| #include "iceberg/util/formattable.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief A sort order for a table | ||
| /// | ||
| /// A sort order is defined by a sort order id and a list of sort fields. | ||
| /// The order of the sort fields within the list defines the order in which the sort is | ||
| /// applied to the data. | ||
| class ICEBERG_EXPORT SortOrder : public util::Formattable { | ||
| public: | ||
| SortOrder(int64_t order_id, std::vector<SortField> fields); | ||
|
|
||
| /// \brief Get the sort order id. | ||
| int64_t order_id() const; | ||
|
|
||
| /// \brief Get the list of sort fields. | ||
| std::span<const SortField> fields() const; | ||
|
|
||
| std::string ToString() const override; | ||
|
|
||
| friend bool operator==(const SortOrder& lhs, const SortOrder& rhs) { | ||
| return lhs.Equals(rhs); | ||
| } | ||
|
|
||
| friend bool operator!=(const SortOrder& lhs, const SortOrder& rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| private: | ||
| /// \brief Compare two sort orders for equality. | ||
| bool Equals(const SortOrder& other) const; | ||
|
|
||
| int64_t order_id_; | ||
| std::vector<SortField> 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
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,82 @@ | ||
| /* | ||
| * 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/sort_field.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "iceberg/transform.h" | ||
| #include "iceberg/util/formatter.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
| class TestTransformFunction : public TransformFunction { | ||
| public: | ||
| TestTransformFunction() : TransformFunction(TransformType::kUnknown) {} | ||
| expected<ArrowArray, Error> Transform(const ArrowArray& input) override { | ||
| return unexpected( | ||
| Error{.kind = ErrorKind::kNotSupported, .message = "test transform function"}); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(SortFieldTest, Basics) { | ||
| { | ||
| const auto transform = std::make_shared<IdentityTransformFunction>(); | ||
| SortField field(1, transform, SortDirection::kAscending, NullOrder::kFirst); | ||
| EXPECT_EQ(1, field.source_id()); | ||
| EXPECT_EQ(*transform, *field.transform()); | ||
| EXPECT_EQ(SortDirection::kAscending, field.direction()); | ||
| EXPECT_EQ(NullOrder::kFirst, field.null_order()); | ||
| EXPECT_EQ( | ||
| "sort_field(source_id=1, transform=identity, direction=asc, " | ||
| "null_order=nulls-first)", | ||
| field.ToString()); | ||
| EXPECT_EQ( | ||
| "sort_field(source_id=1, transform=identity, direction=asc, " | ||
| "null_order=nulls-first)", | ||
| std::format("{}", field)); | ||
| } | ||
| } | ||
|
|
||
| TEST(SortFieldTest, Equality) { | ||
| auto test_transform = std::make_shared<TestTransformFunction>(); | ||
| auto identity_transform = std::make_shared<IdentityTransformFunction>(); | ||
|
|
||
| SortField field1(1, test_transform, SortDirection::kAscending, NullOrder::kFirst); | ||
zhjwpku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| SortField field2(2, test_transform, SortDirection::kAscending, NullOrder::kFirst); | ||
| SortField field3(1, identity_transform, SortDirection::kAscending, NullOrder::kFirst); | ||
| SortField field4(1, test_transform, SortDirection::kDescending, NullOrder::kFirst); | ||
| SortField field5(1, test_transform, SortDirection::kAscending, NullOrder::kLast); | ||
|
|
||
| ASSERT_EQ(field1, field1); | ||
| ASSERT_NE(field1, field2); | ||
| ASSERT_NE(field2, field1); | ||
| ASSERT_NE(field1, field3); | ||
| ASSERT_NE(field3, field1); | ||
| ASSERT_NE(field1, field4); | ||
| ASSERT_NE(field4, field1); | ||
| ASSERT_NE(field1, field5); | ||
| ASSERT_NE(field5, field1); | ||
| } | ||
| } // 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.