Java Code Examples for org.apache.flink.table.types.logical.LogicalTypeRoot

The following examples show how to use org.apache.flink.table.types.logical.LogicalTypeRoot. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean supportsConstructedCasting(
		LogicalType sourceType,
		LogicalType targetType,
		boolean allowExplicit) {
	final LogicalTypeRoot sourceRoot = sourceType.getTypeRoot();
	final LogicalTypeRoot targetRoot = targetType.getTypeRoot();
	// all constructed types can only be casted within the same type root
	if (sourceRoot == targetRoot) {
		final List<LogicalType> sourceChildren = sourceType.getChildren();
		final List<LogicalType> targetChildren = targetType.getChildren();
		if (sourceChildren.size() != targetChildren.size()) {
			return false;
		}
		for (int i = 0; i < sourceChildren.size(); i++) {
			if (!supportsCasting(sourceChildren.get(i), targetChildren.get(i), allowExplicit)) {
				return false;
			}
		}
		return true;
	}
	return false;
}
 
Example 2
Source Project: flink   Source File: CsvRowSchemaConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Convert {@link LogicalType} to {@link CsvSchema.ColumnType} based on Jackson's categories.
 */
private static CsvSchema.ColumnType convertType(String fieldName, LogicalType type) {
	if (STRING_TYPE_ROOTS.contains(type.getTypeRoot())) {
		return CsvSchema.ColumnType.STRING;
	} else if (NUMBER_TYPE_ROOTS.contains(type.getTypeRoot())) {
		return CsvSchema.ColumnType.NUMBER;
	} else if (BOOLEAN_TYPE_ROOTS.contains(type.getTypeRoot())) {
		return CsvSchema.ColumnType.BOOLEAN;
	} else if (type.getTypeRoot() == LogicalTypeRoot.ARRAY) {
		validateNestedField(fieldName, ((ArrayType) type).getElementType());
		return CsvSchema.ColumnType.ARRAY;
	} else if (type.getTypeRoot() == LogicalTypeRoot.ROW) {
		RowType rowType = (RowType) type;
		for (LogicalType fieldType : rowType.getChildren()) {
			validateNestedField(fieldName, fieldType);
		}
		return CsvSchema.ColumnType.ARRAY;
	} else {
		throw new IllegalArgumentException(
			"Unsupported type '" + type.asSummaryString() + "' for field '" + fieldName + "'.");
	}
}
 
Example 3
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 6 votes vote down vote up
private static @Nullable LogicalType findCommonCastableType(List<LogicalType> normalizedTypes) {
	LogicalType resultType = normalizedTypes.get(0);

	for (LogicalType type : normalizedTypes) {
		final LogicalTypeRoot typeRoot = type.getTypeRoot();

		// NULL does not affect the result of this loop
		if (typeRoot == NULL) {
			continue;
		}

		if (supportsImplicitCast(resultType, type)) {
			resultType = type;
		} else {
			if (!supportsImplicitCast(type, resultType)) {
				return null;
			}
		}
	}

	return resultType;
}
 
Example 4
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 6 votes vote down vote up
private static LogicalType createCommonTimestampType(LogicalType resultType, LogicalType type) {
	// same types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();
	final int precision = combinePrecision(resultType, type);

	// same type roots
	if (typeRoot == resultTypeRoot) {
		return createTimestampType(resultTypeRoot, precision);
	}

	// generalize to zoned type
	if (typeRoot == TIMESTAMP_WITH_TIME_ZONE ||
			resultTypeRoot == TIMESTAMP_WITH_TIME_ZONE) {
		return createTimestampType(TIMESTAMP_WITH_TIME_ZONE, precision);
	} else if (typeRoot == TIMESTAMP_WITH_LOCAL_TIME_ZONE ||
			resultTypeRoot == TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
		return createTimestampType(TIMESTAMP_WITH_LOCAL_TIME_ZONE, precision);
	}
	return createTimestampType(TIMESTAMP_WITHOUT_TIME_ZONE, precision);
}
 
Example 5
Source Project: flink   Source File: MySQLDialect.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<LogicalTypeRoot> unsupportedTypes() {
	// The data types used in Mysql are list at:
	// https://dev.mysql.com/doc/refman/8.0/en/data-types.html

	// TODO: We can't convert BINARY data type to
	//  PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO in LegacyTypeInfoDataTypeConverter.
	return Arrays.asList(
		LogicalTypeRoot.BINARY,
		LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE,
		LogicalTypeRoot.TIMESTAMP_WITH_TIME_ZONE,
		LogicalTypeRoot.INTERVAL_YEAR_MONTH,
		LogicalTypeRoot.INTERVAL_DAY_TIME,
		LogicalTypeRoot.ARRAY,
		LogicalTypeRoot.MULTISET,
		LogicalTypeRoot.MAP,
		LogicalTypeRoot.ROW,
		LogicalTypeRoot.DISTINCT_TYPE,
		LogicalTypeRoot.STRUCTURED_TYPE,
		LogicalTypeRoot.NULL,
		LogicalTypeRoot.RAW,
		LogicalTypeRoot.SYMBOL,
		LogicalTypeRoot.UNRESOLVED
	);
}
 
Example 6
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean supportsConstructedCasting(
		LogicalType sourceType,
		LogicalType targetType,
		boolean allowExplicit) {
	final LogicalTypeRoot sourceRoot = sourceType.getTypeRoot();
	final LogicalTypeRoot targetRoot = targetType.getTypeRoot();
	// all constructed types can only be casted within the same type root
	if (sourceRoot == targetRoot) {
		final List<LogicalType> sourceChildren = sourceType.getChildren();
		final List<LogicalType> targetChildren = targetType.getChildren();
		if (sourceChildren.size() != targetChildren.size()) {
			return false;
		}
		for (int i = 0; i < sourceChildren.size(); i++) {
			if (!supportsCasting(sourceChildren.get(i), targetChildren.get(i), allowExplicit)) {
				return false;
			}
		}
		return true;
	}
	return false;
}
 
Example 7
Source Project: flink   Source File: LogicalTypeParser.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private LogicalType parseLegacyType() {
	nextToken(TokenType.BEGIN_PARAMETER);
	nextToken(TokenType.LITERAL_STRING);
	final String rootString = tokenAsString();

	nextToken(TokenType.LIST_SEPARATOR);
	nextToken(TokenType.LITERAL_STRING);
	final String typeInfoString = tokenAsString();
	nextToken(TokenType.END_PARAMETER);

	try {
		final LogicalTypeRoot root = LogicalTypeRoot.valueOf(rootString);
		final TypeInformation typeInfo = TypeStringUtils.readTypeInfo(typeInfoString);
		return new LegacyTypeInformationType<>(root, typeInfo);
	} catch (Throwable t) {
		throw parsingError(
				"Unable to restore the Legacy type of '" + typeInfoString + "' with " +
						"type root '" + rootString + "'.", t);
	}
}
 
Example 8
Source Project: flink   Source File: DataTypePrecisionFixer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public DataType visit(FieldsDataType fieldsDataType) {
	final List<DataType> fieldDataTypes = fieldsDataType.getChildren();
	if (logicalType.getTypeRoot() == LogicalTypeRoot.ROW) {
		final List<String> fieldNames = getFieldNames(logicalType);
		DataTypes.Field[] fields = IntStream.range(0, fieldDataTypes.size())
			.mapToObj(i -> {
				final DataType oldFieldType = fieldDataTypes.get(i);
				final DataType newFieldType = oldFieldType.accept(
					new DataTypePrecisionFixer(logicalType.getChildren().get(i)));
				return DataTypes.FIELD(fieldNames.get(i), newFieldType);
			})
			.toArray(DataTypes.Field[]::new);
		return DataTypes.ROW(fields).bridgedTo(fieldsDataType.getConversionClass());
	}
	throw new UnsupportedOperationException("Unsupported logical type : " + logicalType);
}
 
Example 9
Source Project: flink   Source File: LogicalTypeMerging.java    License: Apache License 2.0 6 votes vote down vote up
private static LogicalType createCommonTimestampType(LogicalType resultType, LogicalType type) {
	// same types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();
	final int precision = combinePrecision(resultType, type);

	// same type roots
	if (typeRoot == resultTypeRoot) {
		return createTimestampType(resultTypeRoot, precision);
	}

	// generalize to zoned type
	if (typeRoot == TIMESTAMP_WITH_TIME_ZONE ||
			resultTypeRoot == TIMESTAMP_WITH_TIME_ZONE) {
		return createTimestampType(TIMESTAMP_WITH_TIME_ZONE, precision);
	} else if (typeRoot == TIMESTAMP_WITH_LOCAL_TIME_ZONE ||
			resultTypeRoot == TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
		return createTimestampType(TIMESTAMP_WITH_LOCAL_TIME_ZONE, precision);
	}
	return createTimestampType(TIMESTAMP_WITHOUT_TIME_ZONE, precision);
}
 
Example 10
Source Project: flink   Source File: LogicalTypeMerging.java    License: Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example 11
Source Project: pulsar-flink   Source File: SchemaUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static org.apache.pulsar.client.api.Schema sqlType2PulsarSchema(DataType flinkType) throws IncompatibleSchemaException {
    if (flinkType instanceof AtomicDataType) {
        LogicalTypeRoot type = flinkType.getLogicalType().getTypeRoot();
        switch (type) {
            case BOOLEAN:
                return BooleanSchema.of();
            case VARBINARY:
                return BytesSchema.of();
            case DATE:
                return DateSchema.of();
            case VARCHAR:
                return org.apache.pulsar.client.api.Schema.STRING;
            case TIMESTAMP_WITHOUT_TIME_ZONE:
                return TimestampSchema.of();
            case TINYINT:
                return ByteSchema.of();
            case DOUBLE:
                return DoubleSchema.of();
            case FLOAT:
                return FloatSchema.of();
            case INTEGER:
                return IntSchema.of();
            case BIGINT:
                return LongSchema.of();
            case SMALLINT:
                return ShortSchema.of();
            default:
                throw new IncompatibleSchemaException(String.format("%s is not supported by Pulsar yet", flinkType.toString()), null);
        }
    } else if (flinkType instanceof FieldsDataType) {
        return avroSchema2PulsarSchema(sqlType2AvroSchema(flinkType));
    }
    throw new IncompatibleSchemaException(String.format("%s is not supported by Pulsar yet", flinkType.toString()), null);
}
 
Example 12
Source Project: flink   Source File: HiveTableSource.java    License: Apache License 2.0 5 votes vote down vote up
private Object restorePartitionValueFromFromType(String valStr, DataType type) {
	LogicalTypeRoot typeRoot = type.getLogicalType().getTypeRoot();
	//note: it's not a complete list ofr partition key types that Hive support, we may need add more later.
	switch (typeRoot) {
		case CHAR:
		case VARCHAR:
			return valStr;
		case BOOLEAN:
			return Boolean.parseBoolean(valStr);
		case TINYINT:
			return Integer.valueOf(valStr).byteValue();
		case SMALLINT:
			return Short.valueOf(valStr);
		case INTEGER:
			return Integer.valueOf(valStr);
		case BIGINT:
			return Long.valueOf(valStr);
		case FLOAT:
			return Float.valueOf(valStr);
		case DOUBLE:
			return Double.valueOf(valStr);
		case DATE:
			return Date.valueOf(valStr);
		default:
			break;
	}
	throw new FlinkHiveException(
			new IllegalArgumentException(String.format("Can not convert %s to type %s for partition value", valStr, type)));
}
 
Example 13
Source Project: flink   Source File: ComparableTypeStrategy.java    License: Apache License 2.0 5 votes vote down vote up
private boolean areComparableWithNormalizedNullability(LogicalType firstType, LogicalType secondType) {
	// A hack to support legacy types. To be removed when we drop the legacy types.
	if (firstType instanceof LegacyTypeInformationType ||
			secondType instanceof LegacyTypeInformationType) {
		return true;
	}

	// everything is comparable with null, it should return null in that case
	if (hasRoot(firstType, LogicalTypeRoot.NULL) || hasRoot(secondType, LogicalTypeRoot.NULL)) {
		return true;
	}

	if (firstType.getTypeRoot() == secondType.getTypeRoot()) {
		return areTypesOfSameRootComparable(firstType, secondType);
	}

	if (hasFamily(firstType, LogicalTypeFamily.NUMERIC) && hasFamily(secondType, LogicalTypeFamily.NUMERIC)) {
		return true;
	}

	// DATE + ALL TIMESTAMPS
	if (hasFamily(firstType, LogicalTypeFamily.DATETIME) && hasFamily(secondType, LogicalTypeFamily.DATETIME)) {
		return true;
	}

	// VARCHAR + CHAR (we do not compare collations here)
	if (hasFamily(firstType, LogicalTypeFamily.CHARACTER_STRING) &&
			hasFamily(secondType, LogicalTypeFamily.CHARACTER_STRING)) {
		return true;
	}

	// VARBINARY + BINARY
	if (hasFamily(firstType, LogicalTypeFamily.BINARY_STRING) &&
			hasFamily(secondType, LogicalTypeFamily.BINARY_STRING)) {
		return true;
	}

	return false;
}
 
Example 14
Source Project: flink   Source File: PlannerTypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isPrimitive(LogicalTypeRoot root) {
	switch (root) {
		case BOOLEAN:
		case TINYINT:
		case SMALLINT:
		case INTEGER:
		case BIGINT:
		case FLOAT:
		case DOUBLE:
			return true;
		default:
			return false;
	}
}
 
Example 15
Source Project: flink   Source File: LegacyTypeInfoDataTypeConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType toDataType(TypeInformation<?> typeInfo) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (typeInfo instanceof TimeIndicatorTypeInfo) {
		return convertToTimeAttributeType((TimeIndicatorTypeInfo) typeInfo);
	}

	final DataType foundDataType = typeInfoDataTypeMap.get(typeInfo);
	if (foundDataType != null) {
		return foundDataType;
	}

	if (typeInfo instanceof RowTypeInfo) {
		return convertToRowType((RowTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return convertToArrayType(
			typeInfo.getTypeClass(),
			((ObjectArrayTypeInfo) typeInfo).getComponentInfo());
	}

	else if (typeInfo instanceof BasicArrayTypeInfo) {
		return createLegacyType(LogicalTypeRoot.ARRAY, typeInfo);
	}

	else if (typeInfo instanceof MultisetTypeInfo) {
		return convertToMultisetType(((MultisetTypeInfo) typeInfo).getElementTypeInfo());
	}

	else if (typeInfo instanceof MapTypeInfo) {
		return convertToMapType((MapTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof CompositeType) {
		return createLegacyType(LogicalTypeRoot.STRUCTURED_TYPE, typeInfo);
	}

	return createLegacyType(LogicalTypeRoot.ANY, typeInfo);
}
 
Example 16
Source Project: flink   Source File: TypeStrategies.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private static boolean isDecimalComputation(LogicalType type1, LogicalType type2) {
	// both must be exact numeric
	if (!hasFamily(type1, LogicalTypeFamily.EXACT_NUMERIC) || !hasFamily(type2, LogicalTypeFamily.EXACT_NUMERIC)) {
		return false;
	}
	// one decimal must be present
	return hasRoot(type1, LogicalTypeRoot.DECIMAL) || hasRoot(type2, LogicalTypeRoot.DECIMAL);
}
 
Example 17
Source Project: flink   Source File: LogicalTypeChecks.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<String> defaultMethod(LogicalType logicalType) {
	// legacy
	if (hasRoot(logicalType, LogicalTypeRoot.STRUCTURED_TYPE)) {
		return Arrays.asList(
			((CompositeType<?>) ((LegacyTypeInformationType<?>) logicalType).getTypeInformation())
				.getFieldNames());
	}
	return super.defaultMethod(logicalType);
}
 
Example 18
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 5 votes vote down vote up
CastingRuleBuilder implicitFromFamily(LogicalTypeFamily... sourceFamilies) {
	for (LogicalTypeFamily family : sourceFamilies) {
		for (LogicalTypeRoot root : LogicalTypeRoot.values()) {
			if (root.getFamilies().contains(family)) {
				this.implicitSourceTypes.add(root);
			}
		}
	}
	return this;
}
 
Example 19
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Should be called after {@link #explicitFromFamily(LogicalTypeFamily...)} to remove previously
 * added types.
 */
CastingRuleBuilder explicitNotFromFamily(LogicalTypeFamily... sourceFamilies) {
	for (LogicalTypeFamily family : sourceFamilies) {
		for (LogicalTypeRoot root : LogicalTypeRoot.values()) {
			if (root.getFamilies().contains(family)) {
				this.explicitSourceTypes.remove(root);
			}
		}
	}
	return this;
}
 
Example 20
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the most common type of a set of types. It determines a type to which all given types
 * can be casted.
 *
 * <p>For example: {@code [INT, BIGINT, DECIMAL(2, 2)]} would lead to {@code DECIMAL(21, 2)}.
 */
public static Optional<LogicalType> findCommonType(List<LogicalType> types) {
	Preconditions.checkArgument(types.size() > 0, "List of types must not be empty.");

	// collect statistics first
	boolean hasAnyType = false;
	boolean hasNullType = false;
	boolean hasNullableTypes = false;
	for (LogicalType type : types) {
		final LogicalTypeRoot typeRoot = type.getTypeRoot();
		if (typeRoot == ANY) {
			hasAnyType = 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, hasAnyType, hasNullType);
	if (foundType == null) {
		foundType = findCommonCastableType(normalizedTypes);
	}

	if (foundType != null) {
		final LogicalType typeWithNullability = foundType.copy(hasNullableTypes);
		return Optional.of(typeWithNullability);
	}
	return Optional.empty();
}
 
Example 21
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example 22
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 5 votes vote down vote up
private static LogicalType createTimestampType(LogicalTypeRoot typeRoot, int precision) {
	switch (typeRoot) {
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return new TimestampType(precision);
		case TIMESTAMP_WITH_TIME_ZONE:
			return new ZonedTimestampType(precision);
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			return new LocalZonedTimestampType(precision);
		default:
			throw new IllegalArgumentException();
	}
}
 
Example 23
Source Project: flink   Source File: LogicalTypeGeneralization.java    License: Apache License 2.0 5 votes vote down vote up
private static LogicalType createStringType(LogicalTypeRoot typeRoot, int length) {
	switch (typeRoot) {
		case CHAR:
			return new CharType(length);
		case VARCHAR:
			return new VarCharType(length);
		case BINARY:
			return new BinaryType(length);
		case VARBINARY:
			return new VarBinaryType(length);
		default:
			throw new IllegalArgumentException();
	}
}
 
Example 24
Source Project: flink   Source File: CollectionDataType.java    License: Apache License 2.0 5 votes vote down vote up
private static Class<?> ensureArrayConversionClass(
		LogicalType logicalType,
		DataType elementDataType,
		@Nullable Class<?> clazz) {
	// arrays are a special case because their default conversion class depends on the
	// conversion class of the element type
	if (logicalType.getTypeRoot() == LogicalTypeRoot.ARRAY && clazz == null) {
		return Array.newInstance(elementDataType.getConversionClass(), 0).getClass();
	}
	return clazz;
}
 
Example 25
Source Project: flink   Source File: AggregateOperationFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Boolean defaultMethod(LogicalType logicalType) {
	if (logicalType.getTypeRoot() == LogicalTypeRoot.ANY) {
		// we don't know anything about the ANY type, we don't know if it is comparable and hashable.
		return false;
	} else if (logicalType instanceof LegacyTypeInformationType) {
		return ((LegacyTypeInformationType) logicalType).getTypeInformation().isKeyType();
	}

	return logicalType.getChildren().stream().allMatch(c -> c.accept(this));
}
 
Example 26
Source Project: flink   Source File: LegacyTypeInfoDataTypeConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType toDataType(TypeInformation<?> typeInfo) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (typeInfo instanceof TimeIndicatorTypeInfo) {
		return convertToTimeAttributeType((TimeIndicatorTypeInfo) typeInfo);
	}

	final DataType foundDataType = typeInfoDataTypeMap.get(typeInfo);
	if (foundDataType != null) {
		return foundDataType;
	}

	if (typeInfo instanceof RowTypeInfo) {
		return convertToRowType((RowTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return convertToArrayType(
			typeInfo.getTypeClass(),
			((ObjectArrayTypeInfo) typeInfo).getComponentInfo());
	}

	else if (typeInfo instanceof BasicArrayTypeInfo) {
		return createLegacyType(LogicalTypeRoot.ARRAY, typeInfo);
	}

	else if (typeInfo instanceof MultisetTypeInfo) {
		return convertToMultisetType(((MultisetTypeInfo) typeInfo).getElementTypeInfo());
	}

	else if (typeInfo instanceof MapTypeInfo) {
		return convertToMapType((MapTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof CompositeType) {
		return createLegacyType(LogicalTypeRoot.STRUCTURED_TYPE, typeInfo);
	}

	return createLegacyType(LogicalTypeRoot.RAW, typeInfo);
}
 
Example 27
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Should be called after {@link #explicitFromFamily(LogicalTypeFamily...)} to remove previously
 * added types.
 */
CastingRuleBuilder explicitNotFromFamily(LogicalTypeFamily... sourceFamilies) {
	for (LogicalTypeFamily family : sourceFamilies) {
		for (LogicalTypeRoot root : LogicalTypeRoot.values()) {
			if (root.getFamilies().contains(family)) {
				this.explicitSourceTypes.remove(root);
			}
		}
	}
	return this;
}
 
Example 28
Source Project: flink   Source File: LogicalTypeCasts.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visit(VarCharType targetType) {
	if (sourceType.isNullable() && !targetType.isNullable()) {
		return false;
	}
	// CHAR and VARCHAR are very compatible within bounds
	if ((hasRoot(sourceType, LogicalTypeRoot.CHAR) || hasRoot(sourceType, LogicalTypeRoot.VARCHAR)) &&
			getLength(sourceType) <= targetType.getLength()) {
		return true;
	}
	return defaultMethod(targetType);
}
 
Example 29
Source Project: flink-connectors   Source File: FlinkPravegaTableSource.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Declares a field of the schema to be the processing time attribute.
 *
 * @param proctimeAttribute The name of the field that becomes the processing time field.
 */
protected void setProctimeAttribute(String proctimeAttribute) {
    if (proctimeAttribute != null) {
        // validate that field exists and is of correct type
        Optional<DataType> tpe = schema.getFieldDataType(proctimeAttribute);
        if (!tpe.isPresent()) {
            throw new ValidationException("Processing time attribute " + proctimeAttribute + " is not present in TableSchema.");
        } else if (tpe.get().getLogicalType().getTypeRoot() != LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) {
            throw new ValidationException("Processing time attribute " + proctimeAttribute + " is not of type TIMESTAMP.");
        }
    }
    this.proctimeAttribute = proctimeAttribute;
}
 
Example 30
Source Project: flink-connectors   Source File: FlinkPravegaTableSource.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Declares a list of fields to be rowtime attributes.
 *
 * @param rowtimeAttributeDescriptors The descriptors of the rowtime attributes.
 */
protected void setRowtimeAttributeDescriptors(List<RowtimeAttributeDescriptor> rowtimeAttributeDescriptors) {
    // validate that all declared fields exist and are of correct type
    for (RowtimeAttributeDescriptor desc : rowtimeAttributeDescriptors) {
        String rowtimeAttribute = desc.getAttributeName();
        Optional<DataType> tpe = schema.getFieldDataType(rowtimeAttribute);
        if (!tpe.isPresent()) {
            throw new ValidationException("Rowtime attribute " + rowtimeAttribute + " is not present in TableSchema.");
        } else if (tpe.get().getLogicalType().getTypeRoot() != LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) {
            throw new ValidationException("Rowtime attribute " + rowtimeAttribute + " is not of type TIMESTAMP.");
        }
    }
    this.rowtimeAttributeDescriptors = rowtimeAttributeDescriptors;
}