zhjwpku commented on code in PR #238:
URL: https://github.com/apache/iceberg-cpp/pull/238#discussion_r2434376539


##########
src/iceberg/util/bucket_util.cc:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/util/bucket_util.h"
+
+#include <utility>
+
+#include "iceberg/type.h"
+#include "iceberg/util/endian.h"
+#include "iceberg/util/macros.h"
+#include "iceberg/util/murmurhash3_internal.h"
+
+namespace iceberg {
+
+namespace {
+template <TypeId type_id>
+int32_t HashLiteral(const Literal& literal) {
+  std::unreachable();
+}
+
+template <>
+int32_t HashLiteral<TypeId::kInt>(const Literal& literal) {
+  return BucketUtils::HashInt(std::get<int32_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kDate>(const Literal& literal) {
+  return BucketUtils::HashInt(std::get<int32_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kLong>(const Literal& literal) {
+  return BucketUtils::HashLong(std::get<int64_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kTime>(const Literal& literal) {
+  return BucketUtils::HashLong(std::get<int64_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kTimestamp>(const Literal& literal) {
+  return BucketUtils::HashLong(std::get<int64_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kTimestampTz>(const Literal& literal) {
+  return BucketUtils::HashLong(std::get<int64_t>(literal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kDecimal>(const Literal& literal) {
+  const auto& decimal = std::get<Decimal>(literal.value());
+  return BucketUtils::HashBytes(Decimal::ToBigEndian(decimal.value()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kString>(const Literal& literal) {
+  const auto& str = std::get<std::string>(literal.value());
+  return BucketUtils::HashBytes(
+      std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(str.data()), 
str.size()));
+}
+
+template <>
+int32_t HashLiteral<TypeId::kUuid>(const Literal& literal) {
+  const auto& uuid = std::get<Uuid>(literal.value());
+  return BucketUtils::HashBytes(uuid.bytes());
+}
+
+template <>
+int32_t HashLiteral<TypeId::kBinary>(const Literal& literal) {
+  const auto& binary = std::get<std::vector<uint8_t>>(literal.value());
+  return BucketUtils::HashBytes(binary);
+}
+
+template <>
+int32_t HashLiteral<TypeId::kFixed>(const Literal& literal) {
+  const auto& fixed = std::get<std::vector<uint8_t>>(literal.value());
+  return BucketUtils::HashBytes(fixed);
+}
+
+}  // namespace
+
+int32_t BucketUtils::HashBytes(std::span<const uint8_t> bytes) {
+  int32_t hash_value = 0;
+  MurmurHash3_x86_32(bytes.data(), bytes.size(), 0, &hash_value);
+  return hash_value;
+}
+
+int32_t BucketUtils::HashLong(int64_t value) {
+  int32_t hash_value = 0;
+  value = ToLittleEndian(value);
+  MurmurHash3_x86_32(&value, sizeof(int64_t), 0, &hash_value);
+  return hash_value;
+}
+
+#define DISPATCH_HASH_LITERAL(TYPE_ID)          \
+  case TYPE_ID:                                 \
+    hash_value = HashLiteral<TYPE_ID>(literal); \
+    break;
+
+Result<Literal> BucketUtils::BucketIndex(const Literal& literal, int32_t 
num_buckets) {
+  ICEBERG_DCHECK(num_buckets > 0, "Number of buckets must be positive");

Review Comment:
   `num_buckets` has been validated in `BucketTransform::Make`. I initially 
assumed this would only be used internally, but since it can also be called by 
users, I'll update it to return an error when `num_buckets <= 0`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to