rdblue commented on code in PR #5513: URL: https://github.com/apache/iceberg/pull/5513#discussion_r945205142
########## api/src/main/java/org/apache/iceberg/util/BucketUtil.java: ########## @@ -0,0 +1,91 @@ +/* + * 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. + */ +package org.apache.iceberg.util; + +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.UUID; +import org.apache.iceberg.relocated.com.google.common.hash.HashFunction; +import org.apache.iceberg.relocated.com.google.common.hash.Hashing; + +/** + * Contains the logic for hashing various types for use with the {@code bucket} partition + * transformations + */ +public class BucketUtil { + + private static final HashFunction MURMUR3 = Hashing.murmur3_32_fixed(); + + private BucketUtil() {} + + public static int hashInteger(Integer value) { + return MURMUR3.hashLong(value.longValue()).asInt(); + } + + public static int hashLong(Long value) { + return MURMUR3.hashLong(value).asInt(); + } + + public static int hashFloat(Float value) { + return MURMUR3.hashLong(Double.doubleToLongBits((double) value)).asInt(); + } + + public static int hashDouble(Double value) { + return MURMUR3.hashLong(Double.doubleToLongBits(value)).asInt(); + } + + public static int hashCharSequence(CharSequence value) { + return MURMUR3.hashString(value, StandardCharsets.UTF_8).asInt(); + } + + public static int hashByteBuffer(ByteBuffer value) { + if (value.hasArray()) { + return MURMUR3 + .hashBytes( + value.array(), + value.arrayOffset() + value.position(), + value.arrayOffset() + value.remaining()) Review Comment: I just realized that this isn't correct. It has been wrong for years, evidently. [`HashFunction.hashBytes`](https://guava.dev/releases/19.0/api/docs/com/google/common/hash/HashFunction.html#hashBytes(byte[],%20int,%20int)) accepts a length. `value.remaining()` is that length. Looks like this was working because `arrayOffset` was never non-zero. That's because the only way to get a `ByteBuffer` with a non-zero `arrayOffset` (as far as I can tell) is to use `ByteBuffer.slice()`, which creates a copy of the `ByteBuffer` and sets it. Since `slice` doesn't allow setting the `position` and `limit`, everywhere that I've been able to find uses `duplicate()` and then sets `position` and `limit` because there's no need to limit the start or capacity of the `ByteBuffer` when the backing array is not limited. Allocation and wrapping byte arrays always produces `arrayOffset=0`. Here's a test that catches this. @kbendick, can you add this to `TestBucketing` along with the fix here (remove `value.arrayOffset()`)? ```java @Test public void testByteBufferOnHeapArrayOffset() { byte[] bytes = randomBytes(128); ByteBuffer raw = ByteBuffer.wrap(bytes, 5, 100); ByteBuffer buffer = raw.slice(); Assert.assertEquals("Buffer arrayOffset should be 5", 5, buffer.arrayOffset()); Bucket<ByteBuffer> bucketFunc = Bucket.get(Types.BinaryType.get(), 100); Assert.assertEquals( "HeapByteBuffer hash should match hash for correct slice", hashBytes(bytes, 5, 100), bucketFunc.hash(buffer)); // verify that the buffer was not modified Assert.assertEquals("Buffer position should be 0", 0, buffer.position()); Assert.assertEquals("Buffer limit should not change", 100, buffer.limit()); } ``` -- 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]
