Search in sources :

Example 1 with LogicalTypeFamily

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()));
}
Also used : Objects(java.util.Objects) DataType(org.apache.flink.table.types.DataType) BinaryType(org.apache.flink.table.types.logical.BinaryType) BigDecimal(java.math.BigDecimal) AtomicDataType(org.apache.flink.table.types.AtomicDataType) Stream(java.util.stream.Stream) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) DataTypes(org.apache.flink.table.api.DataTypes) CharType(org.apache.flink.table.types.logical.CharType) BigDecimal(java.math.BigDecimal) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType)

Aggregations

BigDecimal (java.math.BigDecimal)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 Internal (org.apache.flink.annotation.Internal)1 DataTypes (org.apache.flink.table.api.DataTypes)1 AtomicDataType (org.apache.flink.table.types.AtomicDataType)1 DataType (org.apache.flink.table.types.DataType)1 BinaryType (org.apache.flink.table.types.logical.BinaryType)1 CharType (org.apache.flink.table.types.logical.CharType)1 LogicalTypeFamily (org.apache.flink.table.types.logical.LogicalTypeFamily)1