diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java b/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java index 3c10a6af6d75f6..463d8a6e613c85 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java @@ -685,7 +685,12 @@ public static long sha256long(String str) { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(str.getBytes(StandardCharsets.UTF_8)); ByteBuffer buffer = ByteBuffer.wrap(hash); - return buffer.getLong(); + long result = buffer.getLong(); + // Handle Long.MIN_VALUE case to ensure non-negative ID generation + if (result == Long.MIN_VALUE) { + return str.hashCode(); + } + return result; } catch (NoSuchAlgorithmException e) { return str.hashCode(); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java b/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java index dc8419ddd1f56d..88403effa3199f 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java @@ -90,4 +90,16 @@ public void sha256longEcoding() { String str1 = "东方卫视"; Assertions.assertNotEquals(Util.sha256long(str), Util.sha256long(str1)); } + + @Test + public void sha256longHandlesLongMinValue() { + String testStr = "test_long_min_value_case"; + long result = Util.sha256long(testStr); + + Assertions.assertNotEquals(Long.MIN_VALUE, result, + "sha256long should not return Long.MIN_VALUE"); + + Assertions.assertEquals(result, Util.sha256long(testStr), + "Same input should produce same output"); + } }