Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static BucketingFunction of(@Nullable DataLakeFormat lakeFormat) {
return new FlussBucketingFunction();
} else if (lakeFormat == DataLakeFormat.ICEBERG) {
return new IcebergBucketingFunction();
} else if (lakeFormat == DataLakeFormat.HUDI) {
return new HudiBucketingFunction();
} else {
throw new UnsupportedOperationException("Unsupported lake format: " + lakeFormat);
}
Expand Down
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
Expand Up @@ -20,6 +20,7 @@
import org.apache.fluss.config.TableConfig;
import org.apache.fluss.metadata.DataLakeFormat;
import org.apache.fluss.row.InternalRow;
import org.apache.fluss.row.encode.hudi.HudiKeyEncoder;
import org.apache.fluss.row.encode.iceberg.IcebergKeyEncoder;
import org.apache.fluss.row.encode.paimon.PaimonKeyEncoder;
import org.apache.fluss.types.RowType;
Expand Down Expand Up @@ -76,6 +77,14 @@ static KeyEncoder ofPrimaryKeyEncoder(
Optional<Integer> optKvFormatVersion = tableConfig.getKvFormatVersion();
DataLakeFormat dataLakeFormat = tableConfig.getDataLakeFormat().orElse(null);
int kvFormatVersion = optKvFormatVersion.orElse(1);

// Hudi's HudiKeyEncoder is lossy (4-byte hash); it must NOT be used for
// primary key encoding because different keys with the same List#hashCode
// would collide. Use CompactedKeyEncoder instead.
if (dataLakeFormat == DataLakeFormat.HUDI) {
return CompactedKeyEncoder.createKeyEncoder(rowType, keyFields);
}

if (kvFormatVersion == 1) {
return of(rowType, keyFields, dataLakeFormat);
}
Expand Down Expand Up @@ -129,6 +138,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);
Comment on lines +141 to +142
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice review, thanks.

} else {
throw new UnsupportedOperationException("Unsupported datalake format: " + lakeFormat);
}
Expand Down
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()];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

}
}
37 changes: 37 additions & 0 deletions fluss-lake/fluss-lake-hudi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@


<dependencies>
<dependency>
<groupId>org.apache.hudi</groupId>
<artifactId>hudi-flink${flink.major.version}-bundle</artifactId>
<version>${hudi.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.fluss</groupId>
<artifactId>fluss-common</artifactId>
Expand Down
Loading