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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to