You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "fhan688 (via GitHub)" <gi...@apache.org> on 2026/05/14 13:37:40 UTC

[PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

fhan688 opened a new pull request, #3316:
URL: https://github.com/apache/fluss/pull/3316

   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Linked issue: https://github.com/apache/fluss/issues/3274
   
   Introduce Hudi's bucketing strategy into Fluss so that the Fluss server/client can compute the same bucket id as Hudi's BucketIdentifier when tiering data into a Hudi table with bucket index. This is a prerequisite for the upcoming HudiLakeWriter and HudiCompaction PRs, which need to route records to the correct Hudi bucket file.
   
   ### Brief change log
   
   fluss-common (production code)
   
   - BucketingFunction.of(...) — add DataLakeFormat.HUDI branch that returns HudiBucketingFunction.
   
   - HudiBucketingFunction — implements BucketingFunction. Decodes a 4-byte big-endian int produced by HudiKeyEncoder and computes (hash & Integer.MAX_VALUE) % numBuckets, matching Hudi's BucketIdentifier.getBucketId(List<String>, int). Includes strict input validation (bucketKey must be exactly 4 bytes, numBuckets must be positive).
   
   - KeyEncoder.createKeyEncoder(...) — add DataLakeFormat.HUDI branch that returns HudiKeyEncoder.
   
   - HudiKeyEncoder — implements KeyEncoder. Computes List<String>.hashCode() inline (h = 31*h + elementStringHash) over the stringified key fields, avoiding intermediate ArrayList/String.valueOf allocations on the hot path. For common numeric types (int, long, byte, short, boolean) the string hash code is computed without materializing the string. Null fields are encoded as "__null__" placeholder (aligned with Hudi's KeyGenUtils.NULL_RECORDKEY_PLACEHOLDER) to avoid collision with the literal string "null".
   
   fluss-lake-hudi (test & build)
   
   - HudiBucketingFunctionTest — 13 test cases covering:
   
   1. Single-field types: INT, BIGINT, STRING, DECIMAL, TIMESTAMP_NTZ
   
   2. Additional types: BOOLEAN, TINYINT, SMALLINT, FLOAT, DATE, TIME, TIMESTAMP_LTZ
   
   3. Composite (multi-field) bucket keys with and without null fields
   
   4. Null field uses placeholder (not literal "null") — regression test
   
   5. Illegal input: bucketKey null / wrong length / numBuckets ≤ 0
   
   6. Boundary: numBuckets=1, Integer.MIN_VALUE hash, negative hash sign-bit handling
   
   7. All tests cross-validate against Hudi's BucketIdentifier.getBucketId(List<String>, int)
   
   - pom.xml — add hudi-flink${flink.major.version}-bundle with <scope>test</scope> so it is only available during unit tests and does not leak into the runtime classpath.
   
   ### Tests
   
   HudiBucketingFunctionTest (13 test cases, all passing):
   
   - testIntegerHash / testLongHash / testStringHash / testDecimalHash / testTimestampEncodingHash — original single-field coverage
   
   - testNullFieldUsesPlaceholder / testNullFieldDoesNotCollideWithLiteralNullString — null handling
   
   - testBucketingRejectsInvalidBucketKey / testBucketingRejectsNonPositiveNumBuckets — input validation
   
   - testCompositeBucketKeyMatchesHudiFieldValueRecordKey / testCompositeBucketKeyWithNullFieldUsesPlaceholder — multi-field keys
   
   - testBooleanAndIntegralTypes / testDateAndTimeTypes / testTimestampLtzType — type coverage
   
   - testBucketingNumBucketsBoundaryValues — boundary conditions
   
   ### API and Format
   
   No API or storage format changes. This PR only adds new implementations behind existing interfaces (BucketingFunction and KeyEncoder) for a new DataLakeFormat.HUDI enum value that was already defined.
   
   ### Documentation
   
   No new user-facing documentation required. This is an internal bucketing strategy implementation.
   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "fhan688 (via GitHub)" <gi...@apache.org>.
fhan688 commented on code in PR #3316:
URL: https://github.com/apache/fluss/pull/3316#discussion_r3264470268


##########
fluss-common/src/main/java/org/apache/fluss/row/encode/hudi/HudiKeyEncoder.java:
##########
@@ -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()];

Review Comment:
   Thanks — re-read Hudi's `KeyGenUtils.extractRecordKeysByFields` in [Hudi master/release-1.x](https://github.com/apache/hudi). The parser has two modes that we now mirror in `HudiKeyEncoder`:
   
   Single-part recordKey (no `,` and no `:`) → returned verbatim, placeholder NOT round-tripped.
   Composite recordKey → split on `:`/`,`, with `"__null__"` → `null` and `"__empty__"` → `""` before `List#hashCode()`.
   Two findings while implementing the alignment:
   
   The :-in-value concern in your comment is actually handled correctly by Hudi's look-ahead loop (the `commaPosition < keyValueSep1` retry), so `TIMESTAMP_LTZ` values like `2023-10-25T10:01:13.182Z` are fine — confirmed by a new end-to-end test that goes through `BucketIdentifier.getBucketId(recordKey, "f1,f2", n)`.
   The real divergence sources are (a) values containing , (Hudi doesn't escape) and (b) null/empty placeholder round-trip in composite mode. Both are now handled: the encoder produces `List<String>` elements element-wise equal to Hudi's parsed list, and rejects unrepresentable inputs (`,` in value, literal placeholder collision) up front.
   Result: bucket id is bit-for-bit equal to Hudi's `BucketIdentifier` across all 21 tests, including the recordKey-string overload that exercises Hudi's production parse path.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "XuQianJin-Stars (via GitHub)" <gi...@apache.org>.
XuQianJin-Stars commented on PR #3316:
URL: https://github.com/apache/fluss/pull/3316#issuecomment-4516811264

   +1


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "fhan688 (via GitHub)" <gi...@apache.org>.
fhan688 closed pull request #3316: [lake/hudi] Introduce HudiBucketingFunction for bucket strategy
URL: https://github.com/apache/fluss/pull/3316


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "fhan688 (via GitHub)" <gi...@apache.org>.
fhan688 commented on code in PR #3316:
URL: https://github.com/apache/fluss/pull/3316#discussion_r3245959491


##########
fluss-common/src/main/java/org/apache/fluss/row/encode/KeyEncoder.java:
##########
@@ -129,6 +130,8 @@ static KeyEncoder of(
             return CompactedKeyEncoder.createKeyEncoder(rowType, keyFields);
         } else if (lakeFormat == DataLakeFormat.ICEBERG) {
             return new IcebergKeyEncoder(rowType, keyFields);
+        } else if (lakeFormat == DataLakeFormat.HUDI) {
+            return new HudiKeyEncoder(rowType, keyFields);

Review Comment:
   nice review, thanks.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
luoyuxia merged PR #3316:
URL: https://github.com/apache/fluss/pull/3316


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3316:
URL: https://github.com/apache/fluss/pull/3316#discussion_r3245707641


##########
fluss-common/src/main/java/org/apache/fluss/row/encode/KeyEncoder.java:
##########
@@ -129,6 +130,8 @@ static KeyEncoder of(
             return CompactedKeyEncoder.createKeyEncoder(rowType, keyFields);
         } else if (lakeFormat == DataLakeFormat.ICEBERG) {
             return new IcebergKeyEncoder(rowType, keyFields);
+        } else if (lakeFormat == DataLakeFormat.HUDI) {
+            return new HudiKeyEncoder(rowType, keyFields);

Review Comment:
   This factory is used by both `ofBucketKeyEncoder` and `ofPrimaryKeyEncoder`; returning `HudiKeyEncoder` here makes Hudi primary keys encode to only a 4-byte hash. Different record keys with the same `List<String>.hashCode()` would be treated as the same Fluss primary key, so Hudi should be special-cased so only bucket-key encoding uses this lossy hash while primary-key encoding remains collision-free (for example via the compacted encoder).



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "XuQianJin-Stars (via GitHub)" <gi...@apache.org>.
XuQianJin-Stars commented on code in PR #3316:
URL: https://github.com/apache/fluss/pull/3316#discussion_r3254682804


##########
fluss-common/src/main/java/org/apache/fluss/row/encode/hudi/HudiKeyEncoder.java:
##########
@@ -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()];
+        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);

Review Comment:
   `String.valueOf(value)` at line 86 will produce `"[B@xxxx"` for `byte[]`, and reference-style strings for `BinaryArrayData / BinaryMapData / BinaryRowData`. This means a Hudi table with such a column declared as a bucket key would compute non-reproducible bucket ids.
   Please reject unsupported types in the constructor by checking `keyDataType.getTypeRoot()` and only allowing primitive/decimal/string/temporal types. Throw `IllegalArgumentException` for the rest, with a clear message.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "XuQianJin-Stars (via GitHub)" <gi...@apache.org>.
XuQianJin-Stars commented on code in PR #3316:
URL: https://github.com/apache/fluss/pull/3316#discussion_r3254678186


##########
fluss-common/src/main/java/org/apache/fluss/row/encode/hudi/HudiKeyEncoder.java:
##########
@@ -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()];

Review Comment:
   The encoded hash here is `values.hashCode()` over a `List<String>` built directly from each key field's `toString()`. However, Hudi's production path goes through `BucketIdentifier#getBucketId(HoodieKey, indexKeyFields, numBuckets)`, which **parses the record-key string by splitting on `:` and `,`**. As soon as a key field's string form contains `:` or `,` (very common for `TIMESTAMP_LTZ`, e.g. `2023-10-25T10:01:13.182Z`, or any user string with a comma), the `List<String>` Hudi reconstructs differs from the one we hash here, and the resulting bucket id will diverge from Hudi's.
   Note that `HudiBucketingFunctionTest#testTimestampLtzType` only validates against the `BucketIdentifier.getBucketId(List<String>, int)` overload, which sidesteps this parsing step. Please add an end-to-end test that goes through the `HoodieKey` overload, and either escape `:` / `,` inside `stringifyForRecordKey`, or document the limitation explicitly in the class Javadoc.



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [lake/hudi] Introduce HudiBucketingFunction for bucket strategy [fluss]

Posted by "fhan688 (via GitHub)" <gi...@apache.org>.
fhan688 commented on PR #3316:
URL: https://github.com/apache/fluss/pull/3316#issuecomment-4456539457

   please help review, thanks!
   @XuQianJin-Stars   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org