Skip to content

Commit

Permalink
Improve integer matching on consistency check
Browse files Browse the repository at this point in the history
  • Loading branch information
sandynz committed Nov 17, 2023
1 parent 67925cd commit 1fc56f6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,20 @@ public static boolean recordsEquals(final Map<String, Object> thisRecord, final
return true;
}

/**
* Whether column values are matched or not.
*
* @param equalsBuilder equals builder
* @param thisColumnValue this column value
* @param thatColumnValue that column value
* @return true if matched, otherwise false
*/
@SneakyThrows(SQLException.class)
private static boolean isMatched(final EqualsBuilder equalsBuilder, final Object thisColumnValue, final Object thatColumnValue) {
public static boolean isMatched(final EqualsBuilder equalsBuilder, final Object thisColumnValue, final Object thatColumnValue) {
equalsBuilder.reset();
if (isInteger(thisColumnValue) && isInteger(thatColumnValue)) {
return isIntegerEquals((Number) thisColumnValue, (Number) thatColumnValue);
}
if (thisColumnValue instanceof SQLXML && thatColumnValue instanceof SQLXML) {
return ((SQLXML) thisColumnValue).getString().equals(((SQLXML) thatColumnValue).getString());
}
Expand All @@ -80,6 +91,17 @@ private static boolean isMatched(final EqualsBuilder equalsBuilder, final Object
return equalsBuilder.append(thisColumnValue, thatColumnValue).isEquals();
}

private static boolean isInteger(final Object value) {
if (!(value instanceof Number)) {
return false;
}
return value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte;
}

private static boolean isIntegerEquals(final Number one, final Number another) {
return one.longValue() == another.longValue();
}

/**
* Check two BigDecimal whether equals or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -66,12 +65,12 @@ public boolean equals(final Object o) {
return false;
}
final RecordSingleTableInventoryCalculatedResult that = (RecordSingleTableInventoryCalculatedResult) o;
if (recordsCount != that.recordsCount || !Objects.equals(maxUniqueKeyValue, that.maxUniqueKeyValue)) {
EqualsBuilder equalsBuilder = new EqualsBuilder();
if (recordsCount != that.recordsCount || !DataConsistencyCheckUtils.isMatched(equalsBuilder, maxUniqueKeyValue, that.maxUniqueKeyValue)) {
log.warn("Record count or max unique key value not match, recordCount1={}, recordCount2={}, maxUniqueKeyValue1={}, maxUniqueKeyValue2={}.",
recordsCount, that.recordsCount, maxUniqueKeyValue, that.maxUniqueKeyValue);
return false;
}
EqualsBuilder equalsBuilder = new EqualsBuilder();
Iterator<Map<String, Object>> thisRecordsIterator = records.iterator();
Iterator<Map<String, Object>> thatRecordsIterator = that.records.iterator();
while (thisRecordsIterator.hasNext() && thatRecordsIterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.data.pipeline.core.consistencycheck;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
Expand All @@ -25,6 +26,16 @@

class DataConsistencyCheckUtilsTest {

@Test
void assertIsIntegerEquals() {
EqualsBuilder equalsBuilder = new EqualsBuilder();
String value = "123";
Long longValue = Long.parseLong(value);
assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Integer.parseInt(value)));
assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Short.parseShort(value)));
assertTrue(DataConsistencyCheckUtils.isMatched(equalsBuilder, longValue, Byte.parseByte(value)));
}

@Test
void assertIsBigDecimalEquals() {
BigDecimal one = BigDecimal.valueOf(3322, 1);
Expand Down

0 comments on commit 1fc56f6

Please sign in to comment.