in seatunnel-connectors-v2/connector-fake/src/main/java/org/apache/seatunnel/connectors/seatunnel/fake/source/FakeDataGenerator.java [212:345]
private Object randomColumnValue(Column column) {
SeaTunnelDataType<?> fieldType = column.getDataType();
switch (fieldType.getSqlType()) {
case ARRAY:
ArrayType<?, ?> arrayType = (ArrayType<?, ?>) fieldType;
SeaTunnelDataType<?> elementType = arrayType.getElementType();
int length = fakeConfig.getArraySize();
Object array = Array.newInstance(elementType.getTypeClass(), length);
for (int i = 0; i < length; i++) {
Object value = randomColumnValue(column.copy(elementType));
Array.set(array, i, value);
}
return array;
case MAP:
MapType<?, ?> mapType = (MapType<?, ?>) fieldType;
SeaTunnelDataType<?> keyType = mapType.getKeyType();
SeaTunnelDataType<?> valueType = mapType.getValueType();
HashMap<Object, Object> objectMap = new HashMap<>();
int mapSize = fakeConfig.getMapSize();
for (int i = 0; i < mapSize; i++) {
Object key = randomColumnValue(column.copy(keyType));
Object value = randomColumnValue(column.copy(valueType));
objectMap.put(key, value);
}
return objectMap;
case STRING:
return value(column, String::toString, fakeDataRandomUtils::randomString);
case BOOLEAN:
return value(column, Boolean::parseBoolean, fakeDataRandomUtils::randomBoolean);
case TINYINT:
return value(column, Byte::parseByte, fakeDataRandomUtils::randomTinyint);
case SMALLINT:
return value(column, Short::parseShort, fakeDataRandomUtils::randomSmallint);
case INT:
return value(column, Integer::parseInt, fakeDataRandomUtils::randomInt);
case BIGINT:
return value(column, Long::parseLong, fakeDataRandomUtils::randomBigint);
case FLOAT:
return value(column, Float::parseFloat, fakeDataRandomUtils::randomFloat);
case DOUBLE:
return value(column, Double::parseDouble, fakeDataRandomUtils::randomDouble);
case DECIMAL:
return value(column, BigDecimal::new, fakeDataRandomUtils::randomBigDecimal);
case NULL:
return null;
case BYTES:
return value(column, String::getBytes, fakeDataRandomUtils::randomBytes);
case DATE:
return value(
column,
defaultValue -> {
if (defaultValue.equalsIgnoreCase(CURRENT_DATE)) {
return LocalDate.now();
}
DateTimeFormatter dateTimeFormatter =
DateUtils.matchDateFormatter(defaultValue);
return LocalDate.parse(
defaultValue,
dateTimeFormatter == null
? DateTimeFormatter.ISO_LOCAL_DATE
: dateTimeFormatter);
},
fakeDataRandomUtils::randomLocalDate);
case TIME:
return value(
column,
defaultValue -> {
if (defaultValue.equalsIgnoreCase(CURRENT_TIME)) {
return LocalTime.now();
}
return LocalTime.parse(defaultValue, DateTimeFormatter.ISO_LOCAL_TIME);
},
fakeDataRandomUtils::randomLocalTime);
case TIMESTAMP:
return value(
column,
defaultValue -> {
if (defaultValue.equalsIgnoreCase(CURRENT_TIMESTAMP)) {
return LocalDateTime.now();
}
DateTimeFormatter dateTimeFormatter =
DateTimeUtils.matchDateTimeFormatter(defaultValue);
return LocalDateTime.parse(
defaultValue,
dateTimeFormatter == null
? DateTimeFormatter.ISO_LOCAL_DATE_TIME
: dateTimeFormatter);
},
fakeDataRandomUtils::randomLocalDateTime);
case TIMESTAMP_TZ:
return value(
column,
defaultValue -> {
if (defaultValue.equalsIgnoreCase(CURRENT_TIMESTAMP)) {
return OffsetDateTime.now();
}
DateTimeFormatter dateTimeFormatter =
DateTimeUtils.matchDateTimeFormatter(defaultValue);
return OffsetDateTime.parse(
defaultValue,
dateTimeFormatter == null
? DateTimeFormatter.ISO_OFFSET_DATE_TIME
: dateTimeFormatter);
},
c ->
fakeDataRandomUtils
.randomLocalDateTime(c)
.atZone(ZoneId.systemDefault())
.toOffsetDateTime());
case ROW:
SeaTunnelDataType<?>[] fieldTypes = ((SeaTunnelRowType) fieldType).getFieldTypes();
Object[] objects = new Object[fieldTypes.length];
for (int i = 0; i < fieldTypes.length; i++) {
Object object = randomColumnValue(column.copy(fieldTypes[i]));
objects[i] = object;
}
return new SeaTunnelRow(objects);
case BINARY_VECTOR:
return fakeDataRandomUtils.randomBinaryVector(column);
case FLOAT_VECTOR:
return fakeDataRandomUtils.randomFloatVector(column);
case FLOAT16_VECTOR:
return fakeDataRandomUtils.randomFloat16Vector(column);
case BFLOAT16_VECTOR:
return fakeDataRandomUtils.randomBFloat16Vector(column);
case SPARSE_FLOAT_VECTOR:
return fakeDataRandomUtils.randomSparseFloatVector(column);
default:
// never got in there
throw new FakeConnectorException(
CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE,
"SeaTunnel Fake source connector not support this data type");
}
}