fix: leading zeroes incorrect for index key comparator (#6934)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
修复索引比较会因为全是 0 的字符串与其他字符串可能相等的问题

原因是遇到了全是 0 的字符串会因为跳过前导 0 的逻辑导致全部忽略了

#### Does this PR introduce a user-facing change?
```release-note
None
```
pull/6939/head v2.20.7
guqing 2024-10-23 20:32:20 +08:00 committed by GitHub
parent 2c234ab3eb
commit 17e9f2be1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 15 deletions

View File

@ -60,14 +60,6 @@ public class KeyComparator implements Comparator<String> {
int i = startA;
int j = startB;
// Skip leading zeros for both numbers
while (i < a.length() && a.charAt(i) == '0') {
i++;
}
while (j < b.length() && b.charAt(j) == '0') {
j++;
}
// Compare lengths of remaining digits
int lengthA = countDigits(a, i);
int lengthB = countDigits(b, j);
@ -114,13 +106,6 @@ public class KeyComparator implements Comparator<String> {
int i = startA;
int j = startB;
while (i < pointA && a.charAt(i) == '0') {
i++;
}
while (j < pointB && b.charAt(j) == '0') {
j++;
}
int lengthA = pointA - i;
int lengthB = pointB - j;
if (lengthA != lengthB) {
@ -150,6 +135,7 @@ public class KeyComparator implements Comparator<String> {
j++;
}
// If one number has more digits left, and they're not all zeroes, it is larger
while (i < a.length() && Character.isDigit(a.charAt(i))) {
if (a.charAt(i) != '0') {
return 1;

View File

@ -388,6 +388,13 @@ class KeyComparatorTest {
assertThat(comparator.compare("124", "123")).isGreaterThan(0);
// Leading zeros
assertThat(comparator.compare("00123", "123") > 0).isTrue();
assertThat(comparator.compare("0", "0")).isEqualTo(0);
assertThat(comparator.compare("0", "0000")).isLessThan(0);
assertThat(comparator.compare("0x", "0")).isGreaterThan(0);
assertThat(comparator.compare("0", "1")).isLessThan(0);
assertThat(comparator.compare("1", "0")).isGreaterThan(0);
assertThat(comparator.compare("001", "0")).isGreaterThan(0);
assertThat(comparator.compare("0x5e", "0000")).isLessThan(0);
}
@Test
@ -430,6 +437,7 @@ class KeyComparatorTest {
assertThat(comparator.compare("123.46", "123.45")).isGreaterThan(0);
// Decimal equivalence
assertThat(comparator.compare("123.5", "123.50")).isLessThan(0);
assertThat(comparator.compare("123.0005", "123.00050")).isLessThan(0);
}
@Test