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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
