-
Notifications
You must be signed in to change notification settings - Fork 541
[lake/hudi] Introduce HudiBucketingFunction for bucket strategy #3316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c709a1c
8ee308e
ff163bc
4e1098c
b0726db
68e4130
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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.fluss.bucketing; | ||
|
|
||
| /** | ||
| * An implementation of {@link BucketingFunction} to follow Hudi's bucketing strategy. | ||
| * | ||
| * <p>The bucket id is computed in the same way as Hudi's {@code | ||
| * org.apache.hudi.index.bucket.BucketIdentifier#getBucketId(String, String, int)}: take a 32-bit | ||
| * integer hash that is encoded as a fixed 4-byte big-endian array by {@code HudiKeyEncoder}, mask | ||
| * its sign bit and modulo by {@code numBuckets}. | ||
| */ | ||
| public class HudiBucketingFunction implements BucketingFunction { | ||
|
|
||
| /** Length of a Hudi-encoded bucket key, in bytes (a single big-endian {@code int}). */ | ||
| private static final int ENCODED_KEY_LENGTH = 4; | ||
|
|
||
| @Override | ||
| public int bucketing(byte[] bucketKey, int numBuckets) { | ||
| if (bucketKey == null) { | ||
| throw new IllegalArgumentException("bucketKey must not be null"); | ||
| } | ||
| if (bucketKey.length != ENCODED_KEY_LENGTH) { | ||
| throw new IllegalArgumentException( | ||
| "bucketKey must be exactly " | ||
| + ENCODED_KEY_LENGTH | ||
| + " bytes for Hudi bucketing, but got " | ||
| + bucketKey.length | ||
| + " bytes. The bucket key bytes are expected to be produced by HudiKeyEncoder."); | ||
| } | ||
| if (numBuckets <= 0) { | ||
| throw new IllegalArgumentException( | ||
| "numBuckets must be positive, but got " + numBuckets); | ||
| } | ||
|
|
||
| // Decode 4-byte big-endian int produced by HudiKeyEncoder via bit operations | ||
| // to avoid allocating a ByteBuffer instance on this hot path. | ||
| int restored = | ||
| ((bucketKey[0] & 0xFF) << 24) | ||
| | ((bucketKey[1] & 0xFF) << 16) | ||
| | ((bucketKey[2] & 0xFF) << 8) | ||
| | (bucketKey[3] & 0xFF); | ||
|
|
||
| return (restored & Integer.MAX_VALUE) % numBuckets; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * 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.fluss.row.encode.hudi; | ||
|
|
||
| import org.apache.fluss.row.BinaryString; | ||
| import org.apache.fluss.row.InternalRow; | ||
| import org.apache.fluss.row.encode.KeyEncoder; | ||
| import org.apache.fluss.types.DataType; | ||
| import org.apache.fluss.types.RowType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * An implementation of {@link KeyEncoder} to follow Hudi's encoding strategy. | ||
| * | ||
| * <p>The encoded bytes are a 4-byte big-endian representation of {@code List<String>.hashCode()} | ||
| * over the stringified key fields, which matches the way Hudi's {@code BucketIdentifier} hashes a | ||
| * record key. Null fields are replaced by {@link #NULL_RECORDKEY_PLACEHOLDER} so that an explicit | ||
| * null and the literal string {@code "null"} no longer collide in the hash space. | ||
| */ | ||
| public class HudiKeyEncoder implements KeyEncoder { | ||
|
|
||
| /** | ||
| * Placeholder used to represent a {@code null} key field when computing the record-key hash. It | ||
| * is intentionally aligned with Hudi's {@code KeyGenUtils.NULL_RECORDKEY_PLACEHOLDER} so that | ||
| * the resulting bucket id stays identical to what Hudi would compute on its side. | ||
| */ | ||
| public static final String NULL_RECORDKEY_PLACEHOLDER = "__null__"; | ||
|
|
||
| private final InternalRow.FieldGetter[] fieldGetters; | ||
|
|
||
| public HudiKeyEncoder(RowType rowType, List<String> keys) { | ||
| // for getting key fields out of fluss internal row | ||
| fieldGetters = new InternalRow.FieldGetter[keys.size()]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The encoded hash here is |
||
| for (int i = 0; i < keys.size(); i++) { | ||
| int keyIndex = rowType.getFieldIndex(keys.get(i)); | ||
| DataType keyDataType = rowType.getTypeAt(keyIndex); | ||
| fieldGetters[i] = InternalRow.createFieldGetter(keyDataType, keyIndex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] encodeKey(InternalRow row) { | ||
| // Build the same string list that Hudi would build out of a record key, so the | ||
| // resulting List#hashCode() — and therefore the bucket id — match Hudi's own | ||
| // BucketIdentifier#getBucketId. | ||
| List<String> values = new ArrayList<>(fieldGetters.length); | ||
| for (InternalRow.FieldGetter fieldGetter : fieldGetters) { | ||
| Object value = fieldGetter.getFieldOrNull(row); | ||
| values.add(stringifyForRecordKey(value)); | ||
| } | ||
| int hashCode = values.hashCode(); | ||
|
|
||
| // 4-byte big-endian, decoded symmetrically by HudiBucketingFunction. | ||
| return new byte[] { | ||
| (byte) (hashCode >>> 24), | ||
| (byte) (hashCode >>> 16), | ||
| (byte) (hashCode >>> 8), | ||
| (byte) hashCode | ||
| }; | ||
| } | ||
|
|
||
| private static String stringifyForRecordKey(Object value) { | ||
| if (value == null) { | ||
| return NULL_RECORDKEY_PLACEHOLDER; | ||
| } | ||
| if (value instanceof BinaryString) { | ||
| return value.toString(); | ||
| } | ||
| return String.valueOf(value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice review, thanks.