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


##########
src/iceberg/expression/literal.cc:
##########
@@ -554,4 +554,51 @@ Result<Literal> LiteralCaster::CastTo(const Literal& 
literal,
                       target_type->ToString());
 }
 
+// LiteralValueHash implementation
+std::size_t LiteralValueHash::operator()(const Literal::Value& value) const 
noexcept {
+  return std::visit(
+      [](const auto& v) -> std::size_t {
+        using T = std::decay_t<decltype(v)>;
+
+        constexpr size_t kHashPrime = 0x9e3779b9;
+
+        if constexpr (std::is_same_v<T, std::monostate>) {
+          return 0;
+        } else if constexpr (std::is_same_v<T, Literal::BelowMin>) {
+          return std::numeric_limits<std::size_t>::min();
+        } else if constexpr (std::is_same_v<T, Literal::AboveMax>) {
+          return std::numeric_limits<std::size_t>::max();
+        } else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, 
int32_t> ||
+                             std::is_same_v<T, int64_t> || std::is_same_v<T, 
float> ||
+                             std::is_same_v<T, double> ||
+                             std::is_same_v<T, std::string>) {
+          return std::hash<T>{}(v);
+        } else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
+          std::size_t hash = 0;
+          for (size_t i = 0; i < v.size(); ++i) {
+            hash ^= std::hash<uint8_t>{}(v[i]) + kHashPrime + (hash << 6) + 
(hash >> 2);

Review Comment:
   > Hashing identical bytes at different positions can produce same hash 
(e.g., [1,2] and [2,1] might collide). 
   
   I don't think this statement is correct, the left and right shifts should 
ensure a difference.
   I ran a quick demo on Godbolt [1], and as you can see, the hashes of [1,2] 
and [2,1] are different.
   
   [1] https://godbolt.org/z/4ss9E749q
   



-- 
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