Search in sources :

Example 21 with DecimalType

use of org.apache.flink.table.types.logical.DecimalType in project flink by apache.

the class DataFormatConverters method getPrecision.

private static Tuple2<Integer, Integer> getPrecision(LogicalType logicalType) {
    Tuple2<Integer, Integer> ps = new Tuple2<>();
    if (logicalType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) logicalType;
        ps.f0 = decimalType.getPrecision();
        ps.f1 = decimalType.getScale();
    } else {
        TypeInformation typeInfo = ((LegacyTypeInformationType) logicalType).getTypeInformation();
        if (typeInfo instanceof BigDecimalTypeInfo) {
            BigDecimalTypeInfo decimalType = (BigDecimalTypeInfo) typeInfo;
            ps.f0 = decimalType.precision();
            ps.f1 = decimalType.scale();
        } else if (typeInfo instanceof DecimalDataTypeInfo) {
            DecimalDataTypeInfo decimalType = (DecimalDataTypeInfo) typeInfo;
            ps.f0 = decimalType.precision();
            ps.f1 = decimalType.scale();
        } else {
            ps.f0 = DecimalDataUtils.DECIMAL_SYSTEM_DEFAULT.getPrecision();
            ps.f1 = DecimalDataUtils.DECIMAL_SYSTEM_DEFAULT.getScale();
        }
    }
    return ps;
}
Also used : Tuple2(org.apache.flink.api.java.tuple.Tuple2) DecimalDataTypeInfo(org.apache.flink.table.runtime.typeutils.DecimalDataTypeInfo) DecimalType(org.apache.flink.table.types.logical.DecimalType) BigDecimalTypeInfo(org.apache.flink.table.runtime.typeutils.BigDecimalTypeInfo) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 22 with DecimalType

use of org.apache.flink.table.types.logical.DecimalType in project flink by apache.

the class BinaryWriter method write.

// --------------------------------------------------------------------------------------------
/**
 * @deprecated Use {@link #createValueSetter(LogicalType)} for avoiding logical types during
 *     runtime.
 */
@Deprecated
static void write(BinaryWriter writer, int pos, Object o, LogicalType type, TypeSerializer<?> serializer) {
    switch(type.getTypeRoot()) {
        case BOOLEAN:
            writer.writeBoolean(pos, (boolean) o);
            break;
        case TINYINT:
            writer.writeByte(pos, (byte) o);
            break;
        case SMALLINT:
            writer.writeShort(pos, (short) o);
            break;
        case INTEGER:
        case DATE:
        case TIME_WITHOUT_TIME_ZONE:
        case INTERVAL_YEAR_MONTH:
            writer.writeInt(pos, (int) o);
            break;
        case BIGINT:
        case INTERVAL_DAY_TIME:
            writer.writeLong(pos, (long) o);
            break;
        case TIMESTAMP_WITHOUT_TIME_ZONE:
            TimestampType timestampType = (TimestampType) type;
            writer.writeTimestamp(pos, (TimestampData) o, timestampType.getPrecision());
            break;
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            LocalZonedTimestampType lzTs = (LocalZonedTimestampType) type;
            writer.writeTimestamp(pos, (TimestampData) o, lzTs.getPrecision());
            break;
        case FLOAT:
            writer.writeFloat(pos, (float) o);
            break;
        case DOUBLE:
            writer.writeDouble(pos, (double) o);
            break;
        case CHAR:
        case VARCHAR:
            writer.writeString(pos, (StringData) o);
            break;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            writer.writeDecimal(pos, (DecimalData) o, decimalType.getPrecision());
            break;
        case ARRAY:
            writer.writeArray(pos, (ArrayData) o, (ArrayDataSerializer) serializer);
            break;
        case MAP:
        case MULTISET:
            writer.writeMap(pos, (MapData) o, (MapDataSerializer) serializer);
            break;
        case ROW:
        case STRUCTURED_TYPE:
            writer.writeRow(pos, (RowData) o, (RowDataSerializer) serializer);
            break;
        case RAW:
            writer.writeRawValue(pos, (RawValueData<?>) o, (RawValueDataSerializer<?>) serializer);
            break;
        case BINARY:
        case VARBINARY:
            writer.writeBinary(pos, (byte[]) o);
            break;
        default:
            throw new UnsupportedOperationException("Not support type: " + type);
    }
}
Also used : LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TimestampType(org.apache.flink.table.types.logical.TimestampType) DecimalType(org.apache.flink.table.types.logical.DecimalType)

Example 23 with DecimalType

use of org.apache.flink.table.types.logical.DecimalType in project flink by apache.

the class TypeInfoDataTypeConverter method fromDataTypeToTypeInfo.

public static TypeInformation<?> fromDataTypeToTypeInfo(DataType dataType) {
    Class<?> clazz = dataType.getConversionClass();
    if (clazz.isPrimitive()) {
        final TypeInformation<?> foundTypeInfo = primitiveDataTypeTypeInfoMap.get(clazz.getName());
        if (foundTypeInfo != null) {
            return foundTypeInfo;
        }
    }
    LogicalType logicalType = fromDataTypeToLogicalType(dataType);
    switch(logicalType.getTypeRoot()) {
        case TIMESTAMP_WITHOUT_TIME_ZONE:
            TimestampType timestampType = (TimestampType) logicalType;
            int precision = timestampType.getPrecision();
            if (timestampType.getKind() == TimestampKind.REGULAR) {
                return clazz == TimestampData.class ? new TimestampDataTypeInfo(precision) : (clazz == LocalDateTime.class ? ((3 == precision) ? Types.LOCAL_DATE_TIME : new LegacyLocalDateTimeTypeInfo(precision)) : ((3 == precision) ? Types.SQL_TIMESTAMP : new LegacyTimestampTypeInfo(precision)));
            } else {
                return TypeConversions.fromDataTypeToLegacyInfo(dataType);
            }
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            LocalZonedTimestampType lzTs = (LocalZonedTimestampType) logicalType;
            int precisionLzTs = lzTs.getPrecision();
            if (lzTs.getKind() == TimestampKind.REGULAR) {
                return clazz == TimestampData.class ? new TimestampDataTypeInfo(precisionLzTs) : (clazz == Instant.class ? ((3 == precisionLzTs) ? Types.INSTANT : new LegacyInstantTypeInfo(precisionLzTs)) : TypeConversions.fromDataTypeToLegacyInfo(dataType));
            } else {
                return TypeConversions.fromDataTypeToLegacyInfo(dataType);
            }
        case DECIMAL:
            DecimalType decimalType = (DecimalType) logicalType;
            return clazz == DecimalData.class ? new DecimalDataTypeInfo(decimalType.getPrecision(), decimalType.getScale()) : new BigDecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
        case CHAR:
        case // ignore precision
        VARCHAR:
            return clazz == StringData.class ? StringDataTypeInfo.INSTANCE : BasicTypeInfo.STRING_TYPE_INFO;
        case BINARY:
        case // ignore precision
        VARBINARY:
            return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
        case INTERVAL_YEAR_MONTH:
            return TimeIntervalTypeInfo.INTERVAL_MONTHS;
        case INTERVAL_DAY_TIME:
            return TimeIntervalTypeInfo.INTERVAL_MILLIS;
        case ARRAY:
            if (dataType instanceof CollectionDataType && !isPrimitive(((CollectionDataType) dataType).getElementDataType().getLogicalType())) {
                return ObjectArrayTypeInfo.getInfoFor(fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
            } else {
                return TypeConversions.fromDataTypeToLegacyInfo(dataType);
            }
        case MAP:
            KeyValueDataType mapType = (KeyValueDataType) dataType;
            return new MapTypeInfo(fromDataTypeToTypeInfo(mapType.getKeyDataType()), fromDataTypeToTypeInfo(mapType.getValueDataType()));
        case MULTISET:
            return MultisetTypeInfo.getInfoFor(fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType()));
        case ROW:
            if (RowData.class.isAssignableFrom(dataType.getConversionClass())) {
                return InternalTypeInfo.of((RowType) fromDataTypeToLogicalType(dataType));
            } else if (Row.class == dataType.getConversionClass()) {
                RowType logicalRowType = (RowType) logicalType;
                return new RowTypeInfo(dataType.getChildren().stream().map(TypeInfoDataTypeConverter::fromDataTypeToTypeInfo).toArray(TypeInformation[]::new), logicalRowType.getFieldNames().toArray(new String[0]));
            } else {
                return TypeConversions.fromDataTypeToLegacyInfo(dataType);
            }
        case RAW:
            if (logicalType instanceof RawType) {
                return ExternalTypeInfo.of(dataType);
            }
            return TypeConversions.fromDataTypeToLegacyInfo(dataType);
        default:
            return TypeConversions.fromDataTypeToLegacyInfo(dataType);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TimestampData(org.apache.flink.table.data.TimestampData) DecimalDataTypeInfo(org.apache.flink.table.runtime.typeutils.DecimalDataTypeInfo) CollectionDataType(org.apache.flink.table.types.CollectionDataType) LogicalTypeDataTypeConverter.fromDataTypeToLogicalType(org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) LegacyInstantTypeInfo(org.apache.flink.table.runtime.typeutils.LegacyInstantTypeInfo) DecimalData(org.apache.flink.table.data.DecimalData) TimestampType(org.apache.flink.table.types.logical.TimestampType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) BigDecimalTypeInfo(org.apache.flink.table.runtime.typeutils.BigDecimalTypeInfo) RawType(org.apache.flink.table.types.logical.RawType) TimestampDataTypeInfo(org.apache.flink.table.runtime.typeutils.TimestampDataTypeInfo) Instant(java.time.Instant) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) LegacyLocalDateTimeTypeInfo(org.apache.flink.table.runtime.typeutils.LegacyLocalDateTimeTypeInfo) MapTypeInfo(org.apache.flink.api.java.typeutils.MapTypeInfo) LegacyTimestampTypeInfo(org.apache.flink.table.runtime.typeutils.LegacyTimestampTypeInfo) DecimalType(org.apache.flink.table.types.logical.DecimalType) Row(org.apache.flink.types.Row) StringData(org.apache.flink.table.data.StringData)

Example 24 with DecimalType

use of org.apache.flink.table.types.logical.DecimalType in project flink by apache.

the class DecimalModTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType dividend = argumentDataTypes.get(0).getLogicalType();
    final LogicalType divisor = argumentDataTypes.get(1).getLogicalType();
    // a hack to make legacy types possible until we drop them
    if (dividend instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(0));
    }
    if (divisor instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(1));
    }
    if (!isDecimalComputation(dividend, divisor)) {
        return Optional.empty();
    }
    final int dividendScale = getScale(dividend);
    final int divisorScale = getScale(divisor);
    if (dividendScale == 0 && divisorScale == 0) {
        return Optional.of(argumentDataTypes.get(1));
    }
    final DecimalType decimalType = LogicalTypeMerging.findModuloDecimalType(getPrecision(dividend), dividendScale, getPrecision(divisor), divisorScale);
    return Optional.of(fromLogicalToDataType(decimalType));
}
Also used : DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLogicalToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) DecimalType(org.apache.flink.table.types.logical.DecimalType) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType)

Example 25 with DecimalType

use of org.apache.flink.table.types.logical.DecimalType in project flink by apache.

the class DecimalTimesTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType factor1 = argumentDataTypes.get(0).getLogicalType();
    final LogicalType factor2 = argumentDataTypes.get(1).getLogicalType();
    // a hack to make legacy types possible until we drop them
    if (factor1 instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(0));
    }
    if (factor2 instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(1));
    }
    if (!isDecimalComputation(factor1, factor2)) {
        return Optional.empty();
    }
    final DecimalType decimalType = LogicalTypeMerging.findMultiplicationDecimalType(getPrecision(factor1), getScale(factor1), getPrecision(factor2), getScale(factor2));
    return Optional.of(fromLogicalToDataType(decimalType));
}
Also used : DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLogicalToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) DecimalType(org.apache.flink.table.types.logical.DecimalType) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType)

Aggregations

DecimalType (org.apache.flink.table.types.logical.DecimalType)27 LogicalType (org.apache.flink.table.types.logical.LogicalType)14 RowType (org.apache.flink.table.types.logical.RowType)12 TimestampType (org.apache.flink.table.types.logical.TimestampType)12 ArrayType (org.apache.flink.table.types.logical.ArrayType)8 BigIntType (org.apache.flink.table.types.logical.BigIntType)8 IntType (org.apache.flink.table.types.logical.IntType)8 BigDecimal (java.math.BigDecimal)7 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)7 SmallIntType (org.apache.flink.table.types.logical.SmallIntType)7 TinyIntType (org.apache.flink.table.types.logical.TinyIntType)7 VarBinaryType (org.apache.flink.table.types.logical.VarBinaryType)7 VarCharType (org.apache.flink.table.types.logical.VarCharType)7 DataType (org.apache.flink.table.types.DataType)6 BooleanType (org.apache.flink.table.types.logical.BooleanType)6 DoubleType (org.apache.flink.table.types.logical.DoubleType)6 FloatType (org.apache.flink.table.types.logical.FloatType)6 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)6 ArrayList (java.util.ArrayList)5 DateType (org.apache.flink.table.types.logical.DateType)5