Skip to content

Commit

Permalink
[AJ-1279] Refactor check for supported data type conversion (#491)
Browse files Browse the repository at this point in the history
* Refactor check for supported data type conversion

* Add comment
  • Loading branch information
nawatts authored Feb 5, 2024
1 parent 233bb77 commit 23715b1
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ public class RecordDao {
@Value("${twds.streaming.fetch.size:5000}")
int fetchSize;

private static final Set<DataTypeMapping> dataTypesSupportedForTypeConversion =
Set.of(
DataTypeMapping.STRING,
DataTypeMapping.ARRAY_OF_STRING,
DataTypeMapping.NUMBER,
DataTypeMapping.ARRAY_OF_NUMBER,
DataTypeMapping.BOOLEAN,
DataTypeMapping.ARRAY_OF_BOOLEAN,
DataTypeMapping.DATE,
DataTypeMapping.ARRAY_OF_DATE,
DataTypeMapping.DATE_TIME,
DataTypeMapping.ARRAY_OF_DATE_TIME);

private static final Set<Set<DataTypeMapping>> invalidDataTypeConversions =
/**
* Each member of this set is expected to be a set of two data types. The presence of a set of
* types in this set implies that conversions between the types are supported (in either
* direction). Note that arrays do not need to be listed separately here. For example, a set of
* STRING and NUMBER implies that all the following conversions are supported: STRING to NUMBER,
* NUMBER to STRING, ARRAY_OF_STRING to ARRAY_OF_NUMBER, and ARRAY_OF_NUMBER to ARRAY_OF_STRING.
*/
private static final Set<Set<DataTypeMapping>> supportedDataTypeConversions =
Set.of(
Set.of(DataTypeMapping.BOOLEAN, DataTypeMapping.DATE),
Set.of(DataTypeMapping.BOOLEAN, DataTypeMapping.DATE_TIME));
Set.of(DataTypeMapping.STRING, DataTypeMapping.NUMBER),
Set.of(DataTypeMapping.STRING, DataTypeMapping.BOOLEAN),
Set.of(DataTypeMapping.STRING, DataTypeMapping.DATE),
Set.of(DataTypeMapping.STRING, DataTypeMapping.DATE_TIME),
Set.of(DataTypeMapping.NUMBER, DataTypeMapping.BOOLEAN),
Set.of(DataTypeMapping.NUMBER, DataTypeMapping.DATE),
Set.of(DataTypeMapping.NUMBER, DataTypeMapping.DATE_TIME),
Set.of(DataTypeMapping.DATE, DataTypeMapping.DATE_TIME));

/**
* These error codes are expected when a valid update attribute data type request fails because
Expand Down Expand Up @@ -1104,15 +1104,20 @@ public void updateAttributeDataType(
}
}

private boolean isDataTypeConversionSupported(
DataTypeMapping dataType, DataTypeMapping newDataType) {
DataTypeMapping baseType = dataType.getBaseType();
DataTypeMapping newBaseType = newDataType.getBaseType();
return baseType.equals(newBaseType)
|| supportedDataTypeConversions.contains(Set.of(baseType, newBaseType));
}

@VisibleForTesting
String getPostgresTypeConversionExpression(
String attribute, DataTypeMapping dataType, DataTypeMapping newDataType) {
// Some data types are not yet supported.
// Some conversions don't make sense / are invalid.
if (!dataTypesSupportedForTypeConversion.contains(dataType)
|| !dataTypesSupportedForTypeConversion.contains(newDataType)
|| invalidDataTypeConversions.contains(
Set.copyOf(List.of(dataType.getBaseType(), newDataType.getBaseType())))) {
if (!isDataTypeConversionSupported(dataType, newDataType)) {
throw new IllegalArgumentException(
"Unable to convert attribute from %s to %s"
.formatted(dataType.name(), newDataType.name()));
Expand Down

0 comments on commit 23715b1

Please sign in to comment.