|
21 | 21 |
|
22 | 22 | #include <cassert> |
23 | 23 | #include <chrono> |
| 24 | +#include <type_traits> |
24 | 25 | #include <utility> |
25 | 26 |
|
26 | 27 | #include "iceberg/type.h" |
@@ -68,41 +69,29 @@ Result<std::optional<Literal>> BucketTransform::Transform(const Literal& literal |
68 | 69 | literal.ToString(), source_type()->ToString()); |
69 | 70 | } |
70 | 71 | 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()); |
106 | 95 |
|
107 | 96 | // Calculate the bucket index |
108 | 97 | int32_t bucket_index = |
|
0 commit comments