use of org.apache.flink.table.types.logical.LogicalTypeFamily in project flink by apache.
the class ValueDataTypeConverter method extractDataType.
/**
* Returns the clearly identifiable data type if possible. For example, {@code 12L} can be
* expressed as {@code DataTypes.BIGINT().notNull()}. However, for example, {@code null} could
* be any type and is not supported.
*
* <p>All types of the {@link LogicalTypeFamily#PREDEFINED} family, symbols, and arrays are
* supported.
*/
public static Optional<DataType> extractDataType(Object value) {
if (value == null) {
return Optional.empty();
}
DataType convertedDataType = null;
if (value instanceof String) {
convertedDataType = convertToCharType((String) value);
} else // byte arrays have higher priority than regular arrays
if (value instanceof byte[]) {
convertedDataType = convertToBinaryType((byte[]) value);
} else if (value instanceof BigDecimal) {
convertedDataType = convertToDecimalType((BigDecimal) value);
} else if (value instanceof java.time.LocalTime) {
convertedDataType = convertToTimeType((java.time.LocalTime) value);
} else if (value instanceof java.time.LocalDateTime) {
convertedDataType = convertToTimestampType(((java.time.LocalDateTime) value).getNano());
} else if (value instanceof java.sql.Timestamp) {
convertedDataType = convertToTimestampType(((java.sql.Timestamp) value).getNanos());
} else if (value instanceof java.time.ZonedDateTime) {
convertedDataType = convertToZonedTimestampType(((java.time.ZonedDateTime) value).getNano());
} else if (value instanceof java.time.OffsetDateTime) {
convertedDataType = convertToZonedTimestampType(((java.time.OffsetDateTime) value).getNano());
} else if (value instanceof java.time.Instant) {
convertedDataType = convertToLocalZonedTimestampType(((java.time.Instant) value).getNano());
} else if (value instanceof java.time.Period) {
convertedDataType = convertToYearMonthIntervalType(((java.time.Period) value).getYears());
} else if (value instanceof java.time.Duration) {
final java.time.Duration duration = (java.time.Duration) value;
convertedDataType = convertToDayTimeIntervalType(duration.toDays(), duration.getNano());
} else if (value instanceof Object[]) {
// don't let the class-based extraction kick in if array elements differ
return convertToArrayType((Object[]) value).map(dt -> dt.notNull().bridgedTo(value.getClass()));
}
final Optional<DataType> resultType;
if (convertedDataType != null) {
resultType = Optional.of(convertedDataType);
} else {
// class-based extraction is possible for BOOLEAN, TINYINT, SMALLINT, INT, FLOAT,
// DOUBLE,
// DATE, TIME with java.sql.Time, and arrays of primitive types
resultType = ClassDataTypeConverter.extractDataType(value.getClass());
}
return resultType.map(dt -> dt.notNull().bridgedTo(value.getClass()));
}
Aggregations