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 @@ -26,6 +26,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/** Validation rule to check if a numeric value is within a specified range. */
@Data
@NoArgsConstructor
Expand Down Expand Up @@ -62,7 +64,7 @@ public ValidationResult validate(

// Check minimum value
if (minValue != null) {
int minComparison = comparableValue.compareTo(minValue);
int minComparison = compare(comparableValue, minValue);
if (minInclusive ? minComparison < 0 : minComparison <= 0) {
return ValidationResult.failure(
customMessage != null
Expand All @@ -73,7 +75,7 @@ public ValidationResult validate(

// Check maximum value
if (maxValue != null) {
int maxComparison = comparableValue.compareTo(maxValue);
int maxComparison = compare(comparableValue, maxValue);
if (maxInclusive ? maxComparison > 0 : maxComparison >= 0) {
return ValidationResult.failure(
customMessage != null
Expand All @@ -85,6 +87,13 @@ public ValidationResult validate(
return ValidationResult.success();
}

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

@Override
public String getRuleName() {
return "RANGE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.catalog.CatalogTableUtil;
import org.apache.seatunnel.api.table.type.BasicType;
import org.apache.seatunnel.api.table.type.DecimalType;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -140,4 +142,34 @@ void routeToTableShouldRespectQualifiedErrorTablePath() {
assertEquals("db1.source", producedTables.get(0).getTablePath().toString());
assertEquals("db2.ffp", producedTables.get(1).getTablePath().toString());
}

@Test
void rangeRuleShouldValidateDecimalFieldAgainstIntegerBounds() {
SeaTunnelRowType inputRowType =
new SeaTunnelRowType(
new String[] {"amount"}, new SeaTunnelDataType[] {new DecimalType(10, 2)});
CatalogTable inputCatalogTable =
CatalogTableUtil.getCatalogTable("catalog", "db1", null, "source", inputRowType);
ReadonlyConfig config =
ReadonlyConfig.fromMap(
ImmutableMap.of(
"field_rules",
Arrays.asList(
ImmutableMap.of(
"field_name",
"amount",
"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[] {new BigDecimal("99.99")});

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