iceberg-cpp
Loading...
Searching...
No Matches
sort_field.h
Go to the documentation of this file.
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
24
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <string_view>
29
30#include "iceberg/iceberg_export.h"
31#include "iceberg/result.h"
32#include "iceberg/type_fwd.h"
34
35namespace iceberg {
36
38enum class SortDirection {
43};
45ICEBERG_EXPORT constexpr std::string_view ToString(SortDirection direction) {
46 switch (direction) {
47 case SortDirection::kAscending:
48 return "asc";
49 case SortDirection::kDescending:
50 return "desc";
51 default:
52 return "invalid";
53 }
54}
56ICEBERG_EXPORT constexpr Result<SortDirection> SortDirectionFromString(
57 std::string_view str) {
58 if (str == "asc") return SortDirection::kAscending;
59 if (str == "desc") return SortDirection::kDescending;
60 return InvalidArgument("Invalid SortDirection string: {}", str);
61}
62
63enum class NullOrder {
65 kFirst,
67 kLast,
68};
70ICEBERG_EXPORT constexpr std::string_view ToString(NullOrder null_order) {
71 switch (null_order) {
72 case NullOrder::kFirst:
73 return "nulls-first";
74 case NullOrder::kLast:
75 return "nulls-last";
76 default:
77 return "invalid";
78 }
79}
81ICEBERG_EXPORT constexpr Result<NullOrder> NullOrderFromString(std::string_view str) {
82 if (str == "nulls-first") return NullOrder::kFirst;
83 if (str == "nulls-last") return NullOrder::kLast;
84 return InvalidArgument("Invalid NullOrder string: {}", str);
85}
86
88class ICEBERG_EXPORT SortField : public util::Formattable {
89 public:
95 SortField(int32_t source_id, std::shared_ptr<Transform> transform,
96 SortDirection direction, NullOrder null_order);
97
99 int32_t source_id() const;
100
102 const std::shared_ptr<Transform>& transform() const;
103
105 SortDirection direction() const;
106
108 NullOrder null_order() const;
109
111 bool Satisfies(const SortField& other) const;
112
113 std::string ToString() const override;
114
115 friend bool operator==(const SortField& lhs, const SortField& rhs) {
116 if (&lhs == &rhs) {
117 return true;
118 }
119 return lhs.Equals(rhs);
120 }
121
122 private:
124 [[nodiscard]] bool Equals(const SortField& other) const;
125
126 int32_t source_id_;
127 std::shared_ptr<Transform> transform_;
128 SortDirection direction_;
129 NullOrder null_order_;
130};
131
132} // namespace iceberg
a field with its transform.
Definition sort_field.h:88
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
ICEBERG_EXPORT constexpr Result< NullOrder > NullOrderFromString(std::string_view str)
Get the relative null order from name.
Definition sort_field.h:81
SortDirection
Sort direction in a partition, either ascending or descending.
Definition sort_field.h:38
@ kDescending
Descending.
ICEBERG_EXPORT constexpr Result< SortDirection > SortDirectionFromString(std::string_view str)
Get the relative sort direction from name.
Definition sort_field.h:56
NullOrder
Definition sort_field.h:63
@ kFirst
Nulls are sorted first.
@ kLast
Nulls are sorted last.