|
| 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/sort_field.h |
| 23 | +/// A sort field in a sort order |
| 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 Sort direction in a partition, either ascending or descending |
| 38 | +enum class SortDirection { |
| 39 | + /// Ascending |
| 40 | + kAscending, |
| 41 | + /// Descending |
| 42 | + kDescending, |
| 43 | +}; |
| 44 | + |
| 45 | +enum class NullOrder { |
| 46 | + /// Nulls are sorted first |
| 47 | + kFirst, |
| 48 | + /// Nulls are sorted last |
| 49 | + kLast, |
| 50 | +}; |
| 51 | + |
| 52 | +/// \brief a field with its transform. |
| 53 | +class ICEBERG_EXPORT SortField : public util::Formattable { |
| 54 | + public: |
| 55 | + /// \brief Construct a field. |
| 56 | + /// \param[in] source_id The source field ID. |
| 57 | + /// \param[in] transform The transform function. |
| 58 | + /// \param[in] direction The sort direction. |
| 59 | + /// \param[in] null_order The null order. |
| 60 | + SortField(int32_t source_id, std::shared_ptr<TransformFunction> transform, |
| 61 | + SortDirection direction, NullOrder null_order); |
| 62 | + |
| 63 | + /// \brief Get the source field ID. |
| 64 | + int32_t source_id() const; |
| 65 | + |
| 66 | + /// \brief Get the transform type. |
| 67 | + const std::shared_ptr<TransformFunction>& transform() const; |
| 68 | + |
| 69 | + /// \brief Get the sort direction. |
| 70 | + SortDirection direction() const; |
| 71 | + |
| 72 | + /// \brief Get the null order. |
| 73 | + NullOrder null_order() const; |
| 74 | + |
| 75 | + std::string ToString() const override; |
| 76 | + |
| 77 | + friend bool operator==(const SortField& lhs, const SortField& rhs) { |
| 78 | + return lhs.Equals(rhs); |
| 79 | + } |
| 80 | + |
| 81 | + friend bool operator!=(const SortField& lhs, const SortField& rhs) { |
| 82 | + return !(lhs == rhs); |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + /// \brief Compare two fields for equality. |
| 87 | + [[nodiscard]] bool Equals(const SortField& other) const; |
| 88 | + |
| 89 | + int32_t source_id_; |
| 90 | + std::shared_ptr<TransformFunction> transform_; |
| 91 | + SortDirection direction_; |
| 92 | + NullOrder null_order_; |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace iceberg |
0 commit comments