Skip to content

Commit

Permalink
Extract a method in FakerColumnHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick committed Dec 30, 2024
1 parent da9c899 commit 6145b06
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public record FakerColumnHandle(
{
requireNonNull(name, "name is null");
requireNonNull(type, "type is null");
requireNonNull(domain, "domain is null");
}

public static FakerColumnHandle of(int columnId, ColumnMetadata column, double defaultNullProbability)
Expand All @@ -63,20 +64,8 @@ public static FakerColumnHandle of(int columnId, ColumnMetadata column, double d
if (generator != null && !isCharacterColumn(column)) {
throw new TrinoException(INVALID_COLUMN_PROPERTY, "The `%s` property can only be set for CHAR, VARCHAR or VARBINARY columns".formatted(GENERATOR_PROPERTY));
}
Object min;
try {
min = Literal.parse((String) column.getProperties().get(MIN_PROPERTY), column.getType());
}
catch (IllegalArgumentException e) {
throw new TrinoException(INVALID_COLUMN_PROPERTY, "The `%s` property must be a valid %s literal".formatted(MIN_PROPERTY, column.getType().getDisplayName()), e);
}
Object max;
try {
max = Literal.parse((String) column.getProperties().get(MAX_PROPERTY), column.getType());
}
catch (IllegalArgumentException e) {
throw new TrinoException(INVALID_COLUMN_PROPERTY, "The `%s` property must be a valid %s literal".formatted(MAX_PROPERTY, column.getType().getDisplayName()), e);
}
Object min = propertyValue(column, MIN_PROPERTY);
Object max = propertyValue(column, MAX_PROPERTY);
Domain domain = Domain.all(column.getType());
if (min != null || max != null) {
if (isCharacterColumn(column)) {
Expand Down Expand Up @@ -114,6 +103,16 @@ private static boolean isCharacterColumn(ColumnMetadata column)
return column.getType() instanceof CharType || column.getType() instanceof VarcharType || column.getType() instanceof VarbinaryType;
}

private static Object propertyValue(ColumnMetadata column, String property)
{
try {
return Literal.parse((String) column.getProperties().get(property), column.getType());
}
catch (IllegalArgumentException e) {
throw new TrinoException(INVALID_COLUMN_PROPERTY, "The `%s` property must be a valid %s literal".formatted(property, column.getType().getDisplayName()), e);
}
}

private static Range range(Type type, Object min, Object max)
{
requireNonNull(type, "type is null");
Expand Down

0 comments on commit 6145b06

Please sign in to comment.