Search in sources :

Example 86 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class SchemaTranslator method patchDataTypeFromDeclaredSchema.

private static DataType patchDataTypeFromDeclaredSchema(DataTypeFactory dataTypeFactory, DataType inputDataType, List<UnresolvedColumn> declaredColumns) {
    final List<UnresolvedPhysicalColumn> physicalColumns = declaredColumns.stream().filter(SchemaTranslator::isPhysical).map(UnresolvedPhysicalColumn.class::cast).collect(Collectors.toList());
    DataType patchedDataType = inputDataType;
    for (UnresolvedPhysicalColumn physicalColumn : physicalColumns) {
        patchedDataType = patchDataTypeFromColumn(dataTypeFactory, patchedDataType, physicalColumn);
    }
    return patchedDataType;
}
Also used : DataType(org.apache.flink.table.types.DataType) AbstractDataType(org.apache.flink.table.types.AbstractDataType) UnresolvedPhysicalColumn(org.apache.flink.table.api.Schema.UnresolvedPhysicalColumn)

Example 87 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class DecimalDivideTypeStrategy 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 DecimalType decimalType = LogicalTypeMerging.findDivisionDecimalType(getPrecision(dividend), getScale(dividend), getPrecision(divisor), getScale(divisor));
    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 88 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class DecimalPlusTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    final LogicalType addend1 = argumentDataTypes.get(0).getLogicalType();
    final LogicalType addend2 = argumentDataTypes.get(1).getLogicalType();
    // a hack to make legacy types possible until we drop them
    if (addend1 instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(0));
    }
    if (addend2 instanceof LegacyTypeInformationType) {
        return Optional.of(argumentDataTypes.get(1));
    }
    if (!isDecimalComputation(addend1, addend2)) {
        return Optional.empty();
    }
    final DecimalType decimalType = LogicalTypeMerging.findAdditionDecimalType(getPrecision(addend1), getScale(addend1), getPrecision(addend2), getScale(addend2));
    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 89 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class GetTypeStrategy method inferType.

@Override
public Optional<DataType> inferType(CallContext callContext) {
    List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    DataType rowDataType = argumentDataTypes.get(0);
    Optional<DataType> result = Optional.empty();
    Optional<String> fieldName = callContext.getArgumentValue(1, String.class);
    if (fieldName.isPresent()) {
        result = DataTypeUtils.getField(rowDataType, fieldName.get());
    }
    Optional<Integer> fieldIndex = callContext.getArgumentValue(1, Integer.class);
    if (fieldIndex.isPresent()) {
        result = DataTypeUtils.getField(rowDataType, fieldIndex.get());
    }
    return result.map(type -> {
        if (rowDataType.getLogicalType().isNullable()) {
            return type.nullable();
        } else {
            return type;
        }
    });
}
Also used : DataType(org.apache.flink.table.types.DataType)

Example 90 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class MapInputTypeStrategy method inferInputTypes.

@Override
public Optional<List<DataType>> inferInputTypes(CallContext callContext, boolean throwOnFailure) {
    List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
    if (argumentDataTypes.size() == 0) {
        return Optional.empty();
    }
    List<LogicalType> keyTypes = new ArrayList<>();
    List<LogicalType> valueTypes = new ArrayList<>();
    for (int i = 0; i < argumentDataTypes.size(); i++) {
        LogicalType logicalType = argumentDataTypes.get(i).getLogicalType();
        if (i % 2 == 0) {
            keyTypes.add(logicalType);
        } else {
            valueTypes.add(logicalType);
        }
    }
    Optional<LogicalType> commonKeyType = LogicalTypeMerging.findCommonType(keyTypes);
    Optional<LogicalType> commonValueType = LogicalTypeMerging.findCommonType(valueTypes);
    if (!commonKeyType.isPresent() || !commonValueType.isPresent()) {
        return Optional.empty();
    }
    DataType keyType = TypeConversions.fromLogicalToDataType(commonKeyType.get());
    DataType valueType = TypeConversions.fromLogicalToDataType(commonValueType.get());
    return Optional.of(IntStream.range(0, argumentDataTypes.size()).mapToObj(idx -> {
        if (idx % 2 == 0) {
            return keyType;
        } else {
            return valueType;
        }
    }).collect(Collectors.toList()));
}
Also used : ArrayList(java.util.ArrayList) DataType(org.apache.flink.table.types.DataType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Aggregations

DataType (org.apache.flink.table.types.DataType)260 Test (org.junit.Test)72 RowType (org.apache.flink.table.types.logical.RowType)59 LogicalType (org.apache.flink.table.types.logical.LogicalType)58 RowData (org.apache.flink.table.data.RowData)54 List (java.util.List)38 FieldsDataType (org.apache.flink.table.types.FieldsDataType)32 ValidationException (org.apache.flink.table.api.ValidationException)31 ArrayList (java.util.ArrayList)29 Collectors (java.util.stream.Collectors)24 AtomicDataType (org.apache.flink.table.types.AtomicDataType)24 Map (java.util.Map)23 Internal (org.apache.flink.annotation.Internal)23 TableException (org.apache.flink.table.api.TableException)23 HashMap (java.util.HashMap)22 GenericRowData (org.apache.flink.table.data.GenericRowData)22 Row (org.apache.flink.types.Row)22 TableSchema (org.apache.flink.table.api.TableSchema)20 TypeConversions.fromLogicalToDataType (org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType)19 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)18