Java Code Examples for org.apache.flink.table.types.DataType#equals()

The following examples show how to use org.apache.flink.table.types.DataType#equals() . 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 File: TypeInferenceUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Adapts the call's argument if necessary.
 *
 * <p>This includes casts that need to be inserted, reordering of arguments (*), or insertion of default
 * values (*) where (*) is future work.
 */
private static AdaptedCallContext adaptArguments(
		CallContext callContext,
		List<DataType> expectedTypes) {

	final List<DataType> actualTypes = callContext.getArgumentDataTypes();
	for (int pos = 0; pos < actualTypes.size(); pos++) {
		final DataType expectedType = expectedTypes.get(pos);
		final DataType actualType = actualTypes.get(pos);

		if (!actualType.equals(expectedType) && !canCast(actualType, expectedType)) {
			throw new ValidationException(
				String.format(
					"Invalid argument type at position %d. Data type %s expected but %s passed.",
					pos,
					expectedType,
					actualType));
		}
	}

	return new AdaptedCallContext(callContext, expectedTypes);
}
 
Example 2
Source File: ValueDataTypeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Optional<DataType> extractElementTypeFromValues(Object[] array) {
	DataType elementType = null;
	for (Object element : array) {
		// null values are wildcard array elements
		if (element == null) {
			continue;
		}

		final Optional<DataType> possibleElementType = extractDataType(element);
		if (!possibleElementType.isPresent()) {
			return Optional.empty();
		}

		// for simplification, we assume that array elements can always be nullable
		// otherwise mismatches could occur when dealing with nested arrays
		final DataType extractedElementType = possibleElementType.get().nullable();

		// ensure that all elements have the same type;
		// in theory the logic could be improved by converting an array with elements
		// [CHAR(1), CHAR(2)] into an array of CHAR(2) but this can lead to value
		// modification (i.e. adding spaces) which is not intended.
		if (elementType != null && !extractedElementType.equals(elementType)) {
			return Optional.empty();
		}
		elementType = extractedElementType;
	}

	return Optional.ofNullable(elementType)
		.map(DataTypes::ARRAY);
}
 
Example 3
Source File: ValueDataTypeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Optional<DataType> extractElementTypeFromValues(Object[] array) {
	DataType elementType = null;
	for (Object element : array) {
		// null values are wildcard array elements
		if (element == null) {
			continue;
		}

		final Optional<DataType> possibleElementType = extractDataType(element);
		if (!possibleElementType.isPresent()) {
			return Optional.empty();
		}

		// for simplification, we assume that array elements can always be nullable
		// otherwise mismatches could occur when dealing with nested arrays
		final DataType extractedElementType = possibleElementType.get().nullable();

		// ensure that all elements have the same type;
		// in theory the logic could be improved by converting an array with elements
		// [CHAR(1), CHAR(2)] into an array of CHAR(2) but this can lead to value
		// modification (i.e. adding spaces) which is not intended.
		if (elementType != null && !extractedElementType.equals(elementType)) {
			return Optional.empty();
		}
		elementType = extractedElementType;
	}

	return Optional.ofNullable(elementType)
		.map(DataTypes::ARRAY);
}