|
| 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/sort_field.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "iceberg/transform.h" |
| 27 | +#include "iceberg/util/formatter.h" |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +namespace { |
| 32 | +class TestTransformFunction : public TransformFunction { |
| 33 | + public: |
| 34 | + TestTransformFunction() : TransformFunction(TransformType::kUnknown) {} |
| 35 | + expected<ArrowArray, Error> Transform(const ArrowArray& input) override { |
| 36 | + return unexpected( |
| 37 | + Error{.kind = ErrorKind::kNotSupported, .message = "test transform function"}); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +TEST(SortFieldTest, Basics) { |
| 44 | + { |
| 45 | + const auto transform = std::make_shared<IdentityTransformFunction>(); |
| 46 | + SortField field(1, transform, SortDirection::kAscending, NullOrder::kFirst); |
| 47 | + EXPECT_EQ(1, field.source_id()); |
| 48 | + EXPECT_EQ(*transform, *field.transform()); |
| 49 | + EXPECT_EQ(SortDirection::kAscending, field.sort_direction()); |
| 50 | + EXPECT_EQ(NullOrder::kFirst, field.null_order()); |
| 51 | + EXPECT_EQ( |
| 52 | + "SortField(source_id=1, transform=identity, sort_direction=asc, " |
| 53 | + "null_order=nulls-first)", |
| 54 | + field.ToString()); |
| 55 | + EXPECT_EQ( |
| 56 | + "SortField(source_id=1, transform=identity, sort_direction=asc, " |
| 57 | + "null_order=nulls-first)", |
| 58 | + std::format("{}", field)); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +TEST(SortFieldTest, Equality) { |
| 63 | + auto test_transform = std::make_shared<TestTransformFunction>(); |
| 64 | + auto identity_transform = std::make_shared<IdentityTransformFunction>(); |
| 65 | + |
| 66 | + SortField field1(1, test_transform, SortDirection::kAscending, NullOrder::kFirst); |
| 67 | + SortField field2(2, test_transform, SortDirection::kAscending, NullOrder::kFirst); |
| 68 | + SortField field3(1, identity_transform, SortDirection::kAscending, NullOrder::kFirst); |
| 69 | + SortField field4(1, test_transform, SortDirection::kDescending, NullOrder::kFirst); |
| 70 | + SortField field5(1, test_transform, SortDirection::kAscending, NullOrder::kLast); |
| 71 | + |
| 72 | + ASSERT_EQ(field1, field1); |
| 73 | + ASSERT_NE(field1, field2); |
| 74 | + ASSERT_NE(field2, field1); |
| 75 | + ASSERT_NE(field1, field3); |
| 76 | + ASSERT_NE(field3, field1); |
| 77 | + ASSERT_NE(field1, field4); |
| 78 | + ASSERT_NE(field4, field1); |
| 79 | + ASSERT_NE(field1, field5); |
| 80 | + ASSERT_NE(field5, field1); |
| 81 | +} |
| 82 | +} // namespace iceberg |
0 commit comments