Skip to content
Merged
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 @@ -88,10 +88,43 @@ public ValidationResult validate(
}

private int compare(Comparable value, Comparable bound) {
if (value instanceof BigDecimal && bound instanceof Number) {
return ((BigDecimal) value).compareTo(new BigDecimal(bound.toString()));
if (value instanceof Number && bound instanceof Number) {
return compareNumbers((Number) value, (Number) bound);
}
return compareAsNumberOrString(value, bound);
}

/**
* Compares two numeric values. The field value type (e.g. BIGINT/DOUBLE) may differ from the
* parsed bound type (Integer/Long/Double), so finite values are compared as {@link BigDecimal}
* to avoid {@link ClassCastException}. Non-finite values (NaN, Infinity) cannot be represented
* as {@link BigDecimal}, so they are compared as double - {@link Double#compare} sorts them
* beyond all finite values, so they are treated as out of range instead of crashing.
*/
private int compareNumbers(Number value, Number bound) {
double valueAsDouble = value.doubleValue();
double boundAsDouble = bound.doubleValue();
if (Double.isNaN(valueAsDouble)
|| Double.isInfinite(valueAsDouble)
|| Double.isNaN(boundAsDouble)
|| Double.isInfinite(boundAsDouble)) {
return Double.compare(valueAsDouble, boundAsDouble);
}
return new BigDecimal(value.toString()).compareTo(new BigDecimal(bound.toString()));
}

/**
* Compares a value and a bound of different types (e.g. a STRING field against a numeric bound,
* or a numeric field against a string bound) without throwing {@link ClassCastException}.
* Values are compared numerically when both can be parsed as {@link BigDecimal}, otherwise by
* their string representation.
*/
private int compareAsNumberOrString(Comparable value, Comparable bound) {
try {
return new BigDecimal(value.toString()).compareTo(new BigDecimal(bound.toString()));
} catch (NumberFormatException e) {
return value.toString().compareTo(bound.toString());
}
return value.compareTo(bound);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,129 @@ void rangeRuleShouldValidateDecimalFieldAgainstIntegerBounds() {

assertEquals(row, transform.map(row));
}

@Test
void rangeRuleShouldValidateBigintFieldAgainstIntegerBounds() {
SeaTunnelRowType inputRowType =
new SeaTunnelRowType(
new String[] {"quantity"}, new SeaTunnelDataType[] {BasicType.LONG_TYPE});
CatalogTable inputCatalogTable =
CatalogTableUtil.getCatalogTable("catalog", "db1", null, "source", inputRowType);
ReadonlyConfig config =
ReadonlyConfig.fromMap(
ImmutableMap.of(
"field_rules",
Arrays.asList(
ImmutableMap.of(
"field_name",
"quantity",
"rules",
Arrays.asList(
ImmutableMap.of(
"rule_type",
"RANGE",
"min_value",
0,
"max_value",
1000))))));
DataValidatorTransform transform = new DataValidatorTransform(config, inputCatalogTable);
SeaTunnelRow row = new SeaTunnelRow(new Object[] {500L});

assertEquals(row, transform.map(row));
}

@Test
void rangeRuleShouldValidateDoubleFieldAgainstIntegerBounds() {
SeaTunnelRowType inputRowType =
new SeaTunnelRowType(
new String[] {"score"}, new SeaTunnelDataType[] {BasicType.DOUBLE_TYPE});
CatalogTable inputCatalogTable =
CatalogTableUtil.getCatalogTable("catalog", "db1", null, "source", inputRowType);
ReadonlyConfig config =
ReadonlyConfig.fromMap(
ImmutableMap.of(
"field_rules",
Arrays.asList(
ImmutableMap.of(
"field_name",
"score",
"rules",
Arrays.asList(
ImmutableMap.of(
"rule_type",
"RANGE",
"min_value",
0,
"max_value",
100))))));
DataValidatorTransform transform = new DataValidatorTransform(config, inputCatalogTable);
SeaTunnelRow row = new SeaTunnelRow(new Object[] {85.5d});

assertEquals(row, transform.map(row));
}

@Test
void rangeRuleShouldRouteNonFiniteDoubleToErrorTableWithIntegerBounds() {
SeaTunnelRowType inputRowType =
new SeaTunnelRowType(
new String[] {"score"}, new SeaTunnelDataType[] {BasicType.DOUBLE_TYPE});
CatalogTable inputCatalogTable =
CatalogTableUtil.getCatalogTable("catalog", "db1", null, "source", inputRowType);
ReadonlyConfig config =
ReadonlyConfig.fromMap(
ImmutableMap.of(
"row_error_handle_way",
"ROUTE_TO_TABLE",
"row_error_handle_way.error_table",
"ffp",
"field_rules",
Arrays.asList(
ImmutableMap.of(
"field_name",
"score",
"rules",
Arrays.asList(
ImmutableMap.of(
"rule_type",
"RANGE",
"min_value",
0,
"max_value",
100))))));
DataValidatorTransform transform = new DataValidatorTransform(config, inputCatalogTable);
SeaTunnelRow row = new SeaTunnelRow(new Object[] {Double.NaN});

SeaTunnelRow routedRow = transform.map(row);
assertEquals("db1.ffp", routedRow.getTableId());
}

@Test
void rangeRuleShouldValidateNumericStringFieldAgainstIntegerBounds() {
SeaTunnelRowType inputRowType =
new SeaTunnelRowType(
new String[] {"quantity"}, new SeaTunnelDataType[] {BasicType.STRING_TYPE});
CatalogTable inputCatalogTable =
CatalogTableUtil.getCatalogTable("catalog", "db1", null, "source", inputRowType);
ReadonlyConfig config =
ReadonlyConfig.fromMap(
ImmutableMap.of(
"field_rules",
Arrays.asList(
ImmutableMap.of(
"field_name",
"quantity",
"rules",
Arrays.asList(
ImmutableMap.of(
"rule_type",
"RANGE",
"min_value",
0,
"max_value",
1000))))));
DataValidatorTransform transform = new DataValidatorTransform(config, inputCatalogTable);
SeaTunnelRow row = new SeaTunnelRow(new Object[] {"500"});

assertEquals(row, transform.map(row));
}
}
Loading