Search in sources :

Example 6 with LogicalTypeRoot

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

the class LogicalTypeMerging method findCommonNullableType.

@SuppressWarnings("ConstantConditions")
@Nullable
private static LogicalType findCommonNullableType(List<LogicalType> normalizedTypes, boolean hasRawType, boolean hasNullType) {
    // all RAW types must be equal
    if (hasRawType) {
        return findExactlySameType(normalizedTypes);
    }
    LogicalType resultType = null;
    for (LogicalType type : normalizedTypes) {
        final LogicalTypeRoot typeRoot = type.getTypeRoot();
        // NULL does not affect the result of this loop
        if (typeRoot == NULL) {
            continue;
        }
        // result type candidate
        if (resultType == null) {
            resultType = type;
        }
        // find special patterns
        final LogicalType patternType = findCommonTypePattern(resultType, type);
        if (patternType != null) {
            resultType = patternType;
            continue;
        }
        // for types of family CONSTRUCTED
        if (typeRoot == ARRAY) {
            return findCommonArrayType(normalizedTypes);
        } else if (typeRoot == MULTISET) {
            return findCommonMultisetType(normalizedTypes);
        } else if (typeRoot == MAP) {
            return findCommonMapType(normalizedTypes);
        } else if (typeRoot == ROW) {
            return findCommonRowType(normalizedTypes);
        }
        // this simplifies the following lines as we compare same interval families for example
        if (!areSimilarTypes(resultType, type)) {
            return null;
        }
        // for types of family CHARACTER_STRING or BINARY_STRING
        if (type.is(CHARACTER_STRING) | type.is(BINARY_STRING)) {
            final int length = combineLength(resultType, type);
            if (resultType.isAnyOf(VARCHAR, VARBINARY)) {
                // variable length types remain variable length types
                resultType = createStringType(resultType.getTypeRoot(), length);
            } else if (getLength(resultType) != getLength(type)) {
                // padding/modification of strings
                if (resultType.is(CHAR)) {
                    resultType = createStringType(VARCHAR, length);
                } else if (resultType.is(BINARY)) {
                    resultType = createStringType(VARBINARY, length);
                }
            } else {
                // for same type with same length
                resultType = createStringType(typeRoot, length);
            }
        } else // for EXACT_NUMERIC types
        if (type.is(EXACT_NUMERIC)) {
            if (resultType.is(EXACT_NUMERIC)) {
                resultType = createCommonExactNumericType(resultType, type);
            } else if (resultType.is(APPROXIMATE_NUMERIC)) {
                // the result is already approximate
                if (typeRoot == DECIMAL) {
                    // in case of DECIMAL we enforce DOUBLE
                    resultType = new DoubleType();
                }
            } else {
                return null;
            }
        } else // for APPROXIMATE_NUMERIC types
        if (type.is(APPROXIMATE_NUMERIC)) {
            if (resultType.is(APPROXIMATE_NUMERIC)) {
                resultType = createCommonApproximateNumericType(resultType, type);
            } else if (resultType.is(EXACT_NUMERIC)) {
                // the result was exact so far
                if (typeRoot == DECIMAL) {
                    // in case of DECIMAL we enforce DOUBLE
                    resultType = new DoubleType();
                } else {
                    // enforce an approximate result
                    resultType = type;
                }
            } else {
                return null;
            }
        } else // for DATE
        if (type.is(DATE)) {
            if (resultType.is(DATE)) {
                // for enabling findCommonTypePattern
                resultType = new DateType();
            } else {
                return null;
            }
        } else // for TIME
        if (type.is(TIME)) {
            if (resultType.is(TIME)) {
                resultType = new TimeType(combinePrecision(resultType, type));
            } else {
                return null;
            }
        } else // for TIMESTAMP
        if (type.is(TIMESTAMP)) {
            if (resultType.is(TIMESTAMP)) {
                resultType = createCommonTimestampType(resultType, type);
            } else {
                return null;
            }
        } else // for day-time intervals
        if (typeRoot == INTERVAL_DAY_TIME) {
            resultType = createCommonDayTimeIntervalType((DayTimeIntervalType) resultType, (DayTimeIntervalType) type);
        } else // for year-month intervals
        if (typeRoot == INTERVAL_YEAR_MONTH) {
            resultType = createCommonYearMonthIntervalType((YearMonthIntervalType) resultType, (YearMonthIntervalType) type);
        } else // other types are handled by findCommonCastableType
        {
            return null;
        }
    }
    // NULL type only
    if (resultType == null && hasNullType) {
        return new NullType();
    }
    return resultType;
}
Also used : DoubleType(org.apache.flink.table.types.logical.DoubleType) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) NullType(org.apache.flink.table.types.logical.NullType) DateType(org.apache.flink.table.types.logical.DateType) TimeType(org.apache.flink.table.types.logical.TimeType) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) Nullable(javax.annotation.Nullable)

Example 7 with LogicalTypeRoot

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

the class CommonExecSink method getFieldInfoForLengthEnforcer.

/**
 * Returns a List of {@link ConstraintEnforcer.FieldInfo}, each containing the info needed to
 * determine whether a string or binary value needs trimming and/or padding.
 */
private List<ConstraintEnforcer.FieldInfo> getFieldInfoForLengthEnforcer(RowType physicalType, LengthEnforcerType enforcerType) {
    LogicalTypeRoot staticType = null;
    LogicalTypeRoot variableType = null;
    int maxLength = 0;
    switch(enforcerType) {
        case CHAR:
            staticType = LogicalTypeRoot.CHAR;
            variableType = LogicalTypeRoot.VARCHAR;
            maxLength = CharType.MAX_LENGTH;
            break;
        case BINARY:
            staticType = LogicalTypeRoot.BINARY;
            variableType = LogicalTypeRoot.VARBINARY;
            maxLength = BinaryType.MAX_LENGTH;
    }
    final List<ConstraintEnforcer.FieldInfo> fieldsAndLengths = new ArrayList<>();
    for (int i = 0; i < physicalType.getFieldCount(); i++) {
        LogicalType type = physicalType.getTypeAt(i);
        boolean isStatic = type.is(staticType);
        // Should trim and possibly pad
        if ((isStatic && (LogicalTypeChecks.getLength(type) < maxLength)) || (type.is(variableType) && (LogicalTypeChecks.getLength(type) < maxLength))) {
            fieldsAndLengths.add(new ConstraintEnforcer.FieldInfo(i, LogicalTypeChecks.getLength(type), isStatic));
        } else if (isStatic) {
            // Should pad
            fieldsAndLengths.add(new ConstraintEnforcer.FieldInfo(i, null, isStatic));
        }
    }
    return fieldsAndLengths;
}
Also used : ConstraintEnforcer(org.apache.flink.table.runtime.operators.sink.ConstraintEnforcer) ArrayList(java.util.ArrayList) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot)

Example 8 with LogicalTypeRoot

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

the class IndexGeneratorFactory method createRuntimeIndexGenerator.

private static IndexGenerator createRuntimeIndexGenerator(String index, String[] fieldNames, DataType[] fieldTypes, IndexHelper indexHelper, ZoneId localTimeZoneId) {
    final String dynamicIndexPatternStr = indexHelper.extractDynamicIndexPatternStr(index);
    final String indexPrefix = index.substring(0, index.indexOf(dynamicIndexPatternStr));
    final String indexSuffix = index.substring(indexPrefix.length() + dynamicIndexPatternStr.length());
    if (indexHelper.checkIsDynamicIndexWithSystemTimeFormat(index)) {
        final String dateTimeFormat = indexHelper.extractDateFormat(index, LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
        return new AbstractTimeIndexGenerator(index, dateTimeFormat) {

            @Override
            public String generate(RowData row) {
                return indexPrefix.concat(LocalDateTime.now(localTimeZoneId).format(dateTimeFormatter)).concat(indexSuffix);
            }
        };
    }
    final boolean isDynamicIndexWithFormat = indexHelper.checkIsDynamicIndexWithFormat(index);
    final int indexFieldPos = indexHelper.extractIndexFieldPos(index, fieldNames, isDynamicIndexWithFormat);
    final LogicalType indexFieldType = fieldTypes[indexFieldPos].getLogicalType();
    final LogicalTypeRoot indexFieldLogicalTypeRoot = indexFieldType.getTypeRoot();
    // validate index field type
    indexHelper.validateIndexFieldType(indexFieldLogicalTypeRoot);
    // time extract dynamic index pattern
    final RowData.FieldGetter fieldGetter = RowData.createFieldGetter(indexFieldType, indexFieldPos);
    if (isDynamicIndexWithFormat) {
        final String dateTimeFormat = indexHelper.extractDateFormat(index, indexFieldLogicalTypeRoot);
        DynamicFormatter formatFunction = createFormatFunction(indexFieldType, indexFieldLogicalTypeRoot);
        return new AbstractTimeIndexGenerator(index, dateTimeFormat) {

            @Override
            public String generate(RowData row) {
                Object fieldOrNull = fieldGetter.getFieldOrNull(row);
                final String formattedField;
                // TODO we can possibly optimize it to use the nullability of the field
                if (fieldOrNull != null) {
                    formattedField = formatFunction.format(fieldOrNull, dateTimeFormatter);
                } else {
                    formattedField = "null";
                }
                return indexPrefix.concat(formattedField).concat(indexSuffix);
            }
        };
    }
    // general dynamic index pattern
    return new IndexGeneratorBase(index) {

        @Override
        public String generate(RowData row) {
            Object indexField = fieldGetter.getFieldOrNull(row);
            return indexPrefix.concat(indexField == null ? "null" : indexField.toString()).concat(indexSuffix);
        }
    };
}
Also used : RowData(org.apache.flink.table.data.RowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot)

Example 9 with LogicalTypeRoot

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

the class LogicalTypeMerging method findCommonChildrenTypes.

@Nullable
private static List<LogicalType> findCommonChildrenTypes(List<LogicalType> normalizedTypes) {
    final LogicalType firstType = normalizedTypes.get(0);
    final LogicalTypeRoot typeRoot = firstType.getTypeRoot();
    final int numberOfChildren = firstType.getChildren().size();
    for (LogicalType type : normalizedTypes) {
        // all types must have the same root
        if (type.getTypeRoot() != typeRoot) {
            return null;
        }
        // all types must have the same number of children
        if (type.getChildren().size() != numberOfChildren) {
            return null;
        }
    }
    // recursively compute column-wise least restrictive
    final List<LogicalType> resultChildren = new ArrayList<>(numberOfChildren);
    for (int i = 0; i < numberOfChildren; i++) {
        final Optional<LogicalType> childType = findCommonType(new ChildTypeView(normalizedTypes, i));
        if (!childType.isPresent()) {
            return null;
        }
        resultChildren.add(childType.get());
    }
    // no child should be empty at this point
    return resultChildren;
}
Also used : ArrayList(java.util.ArrayList) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Nullable(javax.annotation.Nullable)

Example 10 with LogicalTypeRoot

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

the class LogicalTypeMerging method findCommonType.

/**
 * Returns the most common, more general {@link LogicalType} for a given set of types. If such a
 * type exists, all given types can be casted to this more general type.
 *
 * <p>For example: {@code [INT, BIGINT, DECIMAL(2, 2)]} would lead to {@code DECIMAL(21, 2)}.
 *
 * <p>This class aims to be compatible with the SQL standard. It is inspired by Apache Calcite's
 * {@code SqlTypeFactoryImpl#leastRestrictive} method.
 */
public static Optional<LogicalType> findCommonType(List<LogicalType> types) {
    Preconditions.checkArgument(types.size() > 0, "List of types must not be empty.");
    // collect statistics first
    boolean hasRawType = false;
    boolean hasNullType = false;
    boolean hasNullableTypes = false;
    for (LogicalType type : types) {
        final LogicalTypeRoot typeRoot = type.getTypeRoot();
        if (typeRoot == RAW) {
            hasRawType = true;
        } else if (typeRoot == NULL) {
            hasNullType = true;
        }
        if (type.isNullable()) {
            hasNullableTypes = true;
        }
    }
    final List<LogicalType> normalizedTypes = types.stream().map(t -> t.copy(true)).collect(Collectors.toList());
    LogicalType foundType = findCommonNullableType(normalizedTypes, hasRawType, hasNullType);
    if (foundType == null) {
        foundType = findCommonCastableType(normalizedTypes);
    }
    if (foundType != null) {
        final LogicalType typeWithNullability = foundType.copy(hasNullableTypes);
        // NULL is reserved for untyped literals only
        if (typeWithNullability.is(NULL)) {
            return Optional.empty();
        }
        return Optional.of(typeWithNullability);
    }
    return Optional.empty();
}
Also used : Arrays(java.util.Arrays) INTERVAL(org.apache.flink.table.types.logical.LogicalTypeFamily.INTERVAL) AbstractList(java.util.AbstractList) MapType(org.apache.flink.table.types.logical.MapType) BINARY(org.apache.flink.table.types.logical.LogicalTypeRoot.BINARY) CharType(org.apache.flink.table.types.logical.CharType) MULTISET(org.apache.flink.table.types.logical.LogicalTypeRoot.MULTISET) NULL(org.apache.flink.table.types.logical.LogicalTypeRoot.NULL) DecimalType(org.apache.flink.table.types.logical.DecimalType) LogicalTypeChecks.getScale(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getScale) HOUR_TO_MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR_TO_MINUTE) Map(java.util.Map) TimeType(org.apache.flink.table.types.logical.TimeType) DECIMAL(org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL) DOUBLE(org.apache.flink.table.types.logical.LogicalTypeRoot.DOUBLE) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) INTERVAL_YEAR_MONTH(org.apache.flink.table.types.logical.LogicalTypeRoot.INTERVAL_YEAR_MONTH) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) TIMESTAMP_WITHOUT_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.SECOND) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) NullType(org.apache.flink.table.types.logical.NullType) HOUR_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR_TO_SECOND) EXACT_NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.EXACT_NUMERIC) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) VARCHAR(org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR) ARRAY(org.apache.flink.table.types.logical.LogicalTypeRoot.ARRAY) Optional(java.util.Optional) MONTH(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.MONTH) DAY(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY) LogicalTypeChecks.getPrecision(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getPrecision) YEAR(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.YEAR) TIMESTAMP_WITH_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITH_TIME_ZONE) LogicalTypeCasts.supportsImplicitCast(org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsImplicitCast) IntStream(java.util.stream.IntStream) BINARY_STRING(org.apache.flink.table.types.logical.LogicalTypeFamily.BINARY_STRING) TIMESTAMP(org.apache.flink.table.types.logical.LogicalTypeFamily.TIMESTAMP) BinaryType(org.apache.flink.table.types.logical.BinaryType) APPROXIMATE_NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.APPROXIMATE_NUMERIC) VARBINARY(org.apache.flink.table.types.logical.LogicalTypeRoot.VARBINARY) CHAR(org.apache.flink.table.types.logical.LogicalTypeRoot.CHAR) MINUTE_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.MINUTE_TO_SECOND) LogicalTypeChecks.getLength(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getLength) HashMap(java.util.HashMap) MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.MINUTE) RowType(org.apache.flink.table.types.logical.RowType) ArrayList(java.util.ArrayList) TimestampType(org.apache.flink.table.types.logical.TimestampType) DoubleType(org.apache.flink.table.types.logical.DoubleType) DAY_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_SECOND) CHARACTER_STRING(org.apache.flink.table.types.logical.LogicalTypeFamily.CHARACTER_STRING) MAP(org.apache.flink.table.types.logical.LogicalTypeRoot.MAP) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType) YEAR_TO_MONTH(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.YEAR_TO_MONTH) NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.NUMERIC) Nullable(javax.annotation.Nullable) ROW(org.apache.flink.table.types.logical.LogicalTypeRoot.ROW) MultisetType(org.apache.flink.table.types.logical.MultisetType) TIME(org.apache.flink.table.types.logical.LogicalTypeFamily.TIME) DateType(org.apache.flink.table.types.logical.DateType) VarCharType(org.apache.flink.table.types.logical.VarCharType) ArrayType(org.apache.flink.table.types.logical.ArrayType) DATE(org.apache.flink.table.types.logical.LogicalTypeRoot.DATE) RAW(org.apache.flink.table.types.logical.LogicalTypeRoot.RAW) DAY_TO_MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_MINUTE) DayTimeResolution(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution) YearMonthResolution(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution) DATETIME(org.apache.flink.table.types.logical.LogicalTypeFamily.DATETIME) TIMESTAMP_WITH_LOCAL_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) Internal(org.apache.flink.annotation.Internal) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) HOUR(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR) DAY_TO_HOUR(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Collections(java.util.Collections) INTERVAL_DAY_TIME(org.apache.flink.table.types.logical.LogicalTypeRoot.INTERVAL_DAY_TIME) LogicalType(org.apache.flink.table.types.logical.LogicalType) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot)

Aggregations

LogicalTypeRoot (org.apache.flink.table.types.logical.LogicalTypeRoot)12 LogicalType (org.apache.flink.table.types.logical.LogicalType)8 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3 DateType (org.apache.flink.table.types.logical.DateType)2 DayTimeIntervalType (org.apache.flink.table.types.logical.DayTimeIntervalType)2 DecimalType (org.apache.flink.table.types.logical.DecimalType)2 DoubleType (org.apache.flink.table.types.logical.DoubleType)2 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)2 AbstractList (java.util.AbstractList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Internal (org.apache.flink.annotation.Internal)1 CatalogException (org.apache.flink.table.catalog.exceptions.CatalogException)1