Skip to content

Commit 5ae407c

Browse files
authored
branch-3.0: [bugfix](catalog) replace Math.abs with bitwise AND to ensure non-negative ID generation #55183 (#55708)
### What problem does this PR solve? pick: #55183
1 parent e235255 commit 5ae407c

File tree

2 files changed

+24
-7
lines changed
  • fe/fe-core/src

2 files changed

+24
-7
lines changed

fe/fe-core/src/main/java/org/apache/doris/common/util/Util.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class Util {
6666
private static final long DEFAULT_EXEC_CMD_TIMEOUT_MS = 600000L;
6767

6868
private static final String[] ORDINAL_SUFFIX
69-
= new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
69+
= new String[] {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"};
7070

7171
private static final List<String> REGEX_ESCAPES
7272
= Lists.newArrayList("\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|");
@@ -389,7 +389,7 @@ public static long getLongPropertyOrDefault(String valStr, long defaultVal, Pred
389389
}
390390

391391
public static double getDoublePropertyOrDefault(String valStr, double defaultVal, Predicate<Double> pred,
392-
String hintMsg) throws AnalysisException {
392+
String hintMsg) throws AnalysisException {
393393
if (Strings.isNullOrEmpty(valStr)) {
394394
return defaultVal;
395395
}
@@ -498,8 +498,8 @@ public static InputStream getInputStreamFromUrl(String urlStr, String encodedAut
498498

499499
public static boolean showHiddenColumns() {
500500
return ConnectContext.get() != null && (
501-
ConnectContext.get().getSessionVariable().showHiddenColumns()
502-
|| ConnectContext.get().getSessionVariable().skipStorageEngineMerge());
501+
ConnectContext.get().getSessionVariable().showHiddenColumns()
502+
|| ConnectContext.get().getSessionVariable().skipStorageEngineMerge());
503503
}
504504

505505
public static String escapeSingleRegex(String s) {
@@ -700,7 +700,12 @@ public static long sha256long(String str) {
700700
MessageDigest digest = MessageDigest.getInstance("SHA-256");
701701
byte[] hash = digest.digest(str.getBytes(StandardCharsets.UTF_8));
702702
ByteBuffer buffer = ByteBuffer.wrap(hash);
703-
return buffer.getLong();
703+
long result = buffer.getLong();
704+
// Handle Long.MIN_VALUE case to ensure non-negative ID generation
705+
if (result == Long.MIN_VALUE) {
706+
return str.hashCode();
707+
}
708+
return result;
704709
} catch (NoSuchAlgorithmException e) {
705710
return str.hashCode();
706711
}

fe/fe-core/src/test/java/org/apache/doris/common/util/UtilTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public void getRootCauseWithSuppressedMessageRootCauseWithMessageWithMultiSuppre
4545
rootCause.addSuppressed(new Exception("Suppressed message2"));
4646
Assertions.assertEquals(
4747
"java.lang.Exception: Root cause message"
48-
+ " With suppressed[0]:Suppressed message"
49-
+ " With suppressed[1]:Suppressed message2",
48+
+ " With suppressed[0]:Suppressed message"
49+
+ " With suppressed[1]:Suppressed message2",
5050
Util.getRootCauseWithSuppressedMessage(rootCause));
5151
}
5252

@@ -90,4 +90,16 @@ public void sha256longEcoding() {
9090
String str1 = "东方卫视";
9191
Assertions.assertNotEquals(Util.sha256long(str), Util.sha256long(str1));
9292
}
93+
94+
@Test
95+
public void sha256longHandlesLongMinValue() {
96+
String testStr = "test_long_min_value_case";
97+
long result = Util.sha256long(testStr);
98+
99+
Assertions.assertNotEquals(Long.MIN_VALUE, result,
100+
"sha256long should not return Long.MIN_VALUE");
101+
102+
Assertions.assertEquals(result, Util.sha256long(testStr),
103+
"Same input should produce same output");
104+
}
93105
}

0 commit comments

Comments
 (0)