Skip to content

Commit b4f20f7

Browse files
committed
use std::visit + static_assert
1 parent 9a8af01 commit b4f20f7

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/iceberg/expression/literal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace iceberg {
3232

3333
/// \brief Literal is a literal value that is associated with a primitive type.
3434
class ICEBERG_EXPORT Literal {
35-
private:
35+
public:
3636
/// \brief Sentinel value to indicate that the literal value is below the valid range
3737
/// of a specific primitive type. It can happen when casting a literal to a narrower
3838
/// primitive type.
@@ -48,8 +48,6 @@ class ICEBERG_EXPORT Literal {
4848
bool operator==(const AboveMax&) const = default;
4949
std::strong_ordering operator<=>(const AboveMax&) const = default;
5050
};
51-
52-
public:
5351
using Value = std::variant<bool, // for boolean
5452
int32_t, // for int, date
5553
int64_t, // for long, timestamp, timestamp_tz, time

src/iceberg/transform_function.cc

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <cassert>
2323
#include <chrono>
24+
#include <type_traits>
2425
#include <utility>
2526

2627
#include "iceberg/type.h"
@@ -68,41 +69,29 @@ Result<std::optional<Literal>> BucketTransform::Transform(const Literal& literal
6869
literal.ToString(), source_type()->ToString());
6970
}
7071
int32_t hash_value = 0;
71-
switch (source_type()->type_id()) {
72-
case TypeId::kInt:
73-
case TypeId::kDate: {
74-
auto value = std::get<int32_t>(literal.value());
75-
MurmurHash3_x86_32(&value, sizeof(int32_t), 0, &hash_value);
76-
break;
77-
}
78-
case TypeId::kLong:
79-
case TypeId::kTime:
80-
case TypeId::kTimestamp:
81-
case TypeId::kTimestampTz: {
82-
auto value = std::get<int64_t>(literal.value());
83-
MurmurHash3_x86_32(&value, sizeof(int64_t), 0, &hash_value);
84-
break;
85-
}
86-
case TypeId::kDecimal:
87-
case TypeId::kUuid: {
88-
auto value = std::get<std::array<uint8_t, 16>>(literal.value());
89-
MurmurHash3_x86_32(value.data(), sizeof(uint8_t) * 16, 0, &hash_value);
90-
break;
91-
}
92-
case TypeId::kString: {
93-
auto value = std::get<std::string>(literal.value());
94-
MurmurHash3_x86_32(value.data(), value.size(), 0, &hash_value);
95-
break;
96-
}
97-
case TypeId::kFixed:
98-
case TypeId::kBinary: {
99-
auto value = std::get<std::vector<uint8_t>>(literal.value());
100-
MurmurHash3_x86_32(value.data(), value.size(), 0, &hash_value);
101-
break;
102-
}
103-
default:
104-
std::unreachable();
105-
}
72+
std::visit(
73+
[&](auto&& value) {
74+
using T = std::decay_t<decltype(value)>;
75+
if constexpr (std::is_same_v<T, int32_t>) {
76+
MurmurHash3_x86_32(&value, sizeof(int32_t), 0, &hash_value);
77+
} else if constexpr (std::is_same_v<T, int64_t>) {
78+
MurmurHash3_x86_32(&value, sizeof(int64_t), 0, &hash_value);
79+
} else if constexpr (std::is_same_v<T, std::array<uint8_t, 16>>) {
80+
MurmurHash3_x86_32(value.data(), sizeof(uint8_t) * 16, 0, &hash_value);
81+
} else if constexpr (std::is_same_v<T, std::string>) {
82+
MurmurHash3_x86_32(value.data(), value.size(), 0, &hash_value);
83+
} else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
84+
MurmurHash3_x86_32(value.data(), value.size(), 0, &hash_value);
85+
} else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, float> ||
86+
std::is_same_v<T, double> ||
87+
std::is_same_v<T, Literal::BelowMin> ||
88+
std::is_same_v<T, Literal::AboveMax>) {
89+
std::unreachable();
90+
} else {
91+
static_assert(false, "Unhandled type in BucketTransform::Transform");
92+
}
93+
},
94+
literal.value());
10695

10796
// Calculate the bucket index
10897
int32_t bucket_index =

0 commit comments

Comments
 (0)