Java Code Examples for org.apache.flink.table.types.logical.LogicalType#getChildren()

The following examples show how to use org.apache.flink.table.types.logical.LogicalType#getChildren() . 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: LogicalTypeCasts.java    From flink with 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 File: TableSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a mapping from field name to data type, the field name can be a nested field.
 * This is mainly used for validating whether the rowtime attribute (might be nested) exists
 * in the schema. During creating, it also validates whether there is duplicate field names.
 *
 * <p>For example, a "f0" field of ROW type has two nested fields "q1" and "q2". Then the
 * mapping will be ["f0" -> ROW, "f0.q1" -> INT, "f0.q2" -> STRING].
 *
 * <pre>
 * {@code
 *     f0 ROW<q1 INT, q2 STRING>
 * }
 * </pre>
 *
 * @param fieldNameToType Field name to type mapping that to update
 * @param fieldName       Name of this field, e.g. "q1" or "q2" in the above example
 * @param fieldType       Data type of this field
 * @param parentFieldName Field name of parent type, e.g. "f0" in the above example
 */
private static void validateAndCreateNameToTypeMapping(
		Map<String, LogicalType> fieldNameToType,
		String fieldName,
		LogicalType fieldType,
		String parentFieldName) {
	String fullFieldName = parentFieldName.isEmpty() ? fieldName : parentFieldName + "." + fieldName;
	LogicalType oldType = fieldNameToType.put(fullFieldName, fieldType);
	if (oldType != null) {
		throw new ValidationException("Field names must be unique. Duplicate field: '" + fullFieldName + "'");
	}
	if (isCompositeType(fieldType) && !(fieldType instanceof LegacyTypeInformationType)) {
		final List<String> fieldNames = LogicalTypeChecks.getFieldNames(fieldType);
		final List<LogicalType> fieldTypes = fieldType.getChildren();
		IntStream.range(0, fieldNames.size())
			.forEach(i ->
				validateAndCreateNameToTypeMapping(
					fieldNameToType,
					fieldNames.get(i),
					fieldTypes.get(i),
					fullFieldName));
	}
}
 
Example 3
Source File: ComparableTypeStrategy.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean areConstructedTypesComparable(LogicalType firstType, LogicalType secondType) {
	List<LogicalType> firstChildren = firstType.getChildren();
	List<LogicalType> secondChildren = secondType.getChildren();

	if (firstChildren.size() != secondChildren.size()) {
		return false;
	}

	for (int i = 0; i < firstChildren.size(); i++) {
		if (!areComparable(firstChildren.get(i), secondChildren.get(i))) {
			return false;
		}
	}

	return true;
}
 
Example 4
Source File: LogicalTypeCasts.java    From flink with 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 5
Source File: LogicalTypeCasts.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected Boolean defaultMethod(LogicalType targetType) {
	// quick path
	if (sourceType == targetType) {
		return true;
	}

	if (sourceType.isNullable() && !targetType.isNullable() ||
			sourceType.getClass() != targetType.getClass() || // TODO drop this line once we remove legacy types
			sourceType.getTypeRoot() != targetType.getTypeRoot()) {
		return false;
	}

	final List<LogicalType> sourceChildren = sourceType.getChildren();
	final List<LogicalType> targetChildren = targetType.getChildren();
	if (sourceChildren.isEmpty()) {
		// handles all types that are not of family CONSTRUCTED or USER DEFINED
		return sourceType.equals(targetType) || sourceType.copy(true).equals(targetType);
	} else {
		// handles all types of CONSTRUCTED family as well as distinct types
		return supportsAvoidingCast(sourceChildren, targetChildren);
	}
}
 
Example 6
Source File: PlannerTypeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Can the two types operate with each other.
 * Such as:
 * 1.CodeGen: equal, cast, assignment.
 * 2.Join keys.
 */
public static boolean isInteroperable(LogicalType t1, LogicalType t2) {
	if (t1.getTypeRoot().getFamilies().contains(CHARACTER_STRING) &&
			t2.getTypeRoot().getFamilies().contains(CHARACTER_STRING)) {
		return true;
	}
	if (t1.getTypeRoot().getFamilies().contains(BINARY_STRING) &&
			t2.getTypeRoot().getFamilies().contains(BINARY_STRING)) {
		return true;
	}
	if (t1.getTypeRoot() != t2.getTypeRoot()) {
		return false;
	}

	switch (t1.getTypeRoot()) {
		case ARRAY:
		case MAP:
		case MULTISET:
		case ROW:
			List<LogicalType> children1 = t1.getChildren();
			List<LogicalType> children2 = t2.getChildren();
			if (children1.size() != children2.size()) {
				return false;
			}
			for (int i = 0; i < children1.size(); i++) {
				if (!isInteroperable(children1.get(i), children2.get(i))) {
					return false;
				}
			}
			return true;
		default:
			return t1.copy(true).equals(t2.copy(true));
	}
}
 
Example 7
Source File: PlannerTypeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Now in the conversion to the TypeInformation from DataType, type may loose some information
 * about nullable and precision. So we add this method to do a soft check.
 *
 * <p>The difference of {@link #isInteroperable} is ignore decimal precision.
 *
 * <p>Now not ignore timestamp precision, because we only support one precision for timestamp type now.
 */
public static boolean isAssignable(LogicalType t1, LogicalType t2) {
	// Soft check for CharType, it is converted to String TypeInformation and loose char information.
	if (t1.getTypeRoot().getFamilies().contains(CHARACTER_STRING) &&
			t2.getTypeRoot().getFamilies().contains(CHARACTER_STRING)) {
		return true;
	}
	if (t1.getTypeRoot().getFamilies().contains(BINARY_STRING) &&
			t2.getTypeRoot().getFamilies().contains(BINARY_STRING)) {
		return true;
	}
	if (t1.getTypeRoot() != t2.getTypeRoot()) {
		return false;
	}

	switch (t1.getTypeRoot()) {
		case DECIMAL:
			return true;
		default:
			if (t1.getChildren().isEmpty()) {
				return t1.copy(true).equals(t2.copy(true));
			} else {
				List<LogicalType> children1 = t1.getChildren();
				List<LogicalType> children2 = t2.getChildren();
				if (children1.size() != children2.size()) {
					return false;
				}
				for (int i = 0; i < children1.size(); i++) {
					if (!isAssignable(children1.get(i), children2.get(i))) {
						return false;
					}
				}
				return true;
			}
	}
}
 
Example 8
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected Boolean defaultMethod(LogicalType thatType) {
	checkNotNull(thatType);
	if (thisType == thatType) {
		return true;
	}
	if (thisType.getClass() != thatType.getClass() ||
		thisType.isNullable() != thatType.isNullable() ||
		thisType.getTypeRoot() != thatType.getTypeRoot()) {
		return false;
	}

	List<LogicalType> thisChildren = thisType.getChildren();
	List<LogicalType> thatChildren = thatType.getChildren();
	if (thisChildren.size() != thatChildren.size()) {
		return false;
	}
	if (thisChildren.isEmpty()) {
		// if it is an atomic type, delegate to equals method.
		return thisType.equals(thatType);
	} else {
		// if it is composite type, only need to check children types
		for (int i = 0; i < thisChildren.size(); i++) {
			LogicalType thisChild = thisChildren.get(i);
			LogicalType thatChild = thatChildren.get(i);
			if (!areTypesCompatible(thisChild, thatChild)) {
				return false;
			}
		}
		return true;
	}
}
 
Example 9
Source File: PlannerTypeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Can the two types operate with each other.
 * Such as:
 * 1.CodeGen: equal, cast, assignment.
 * 2.Join keys.
 */
public static boolean isInteroperable(LogicalType t1, LogicalType t2) {
	if (t1.getTypeRoot().getFamilies().contains(CHARACTER_STRING) &&
			t2.getTypeRoot().getFamilies().contains(CHARACTER_STRING)) {
		return true;
	}
	if (t1.getTypeRoot().getFamilies().contains(BINARY_STRING) &&
			t2.getTypeRoot().getFamilies().contains(BINARY_STRING)) {
		return true;
	}
	if (t1.getTypeRoot() != t2.getTypeRoot()) {
		return false;
	}

	switch (t1.getTypeRoot()) {
		case ARRAY:
		case MAP:
		case MULTISET:
		case ROW:
			List<LogicalType> children1 = t1.getChildren();
			List<LogicalType> children2 = t2.getChildren();
			if (children1.size() != children2.size()) {
				return false;
			}
			for (int i = 0; i < children1.size(); i++) {
				if (!isInteroperable(children1.get(i), children2.get(i))) {
					return false;
				}
			}
			return true;
		default:
			return t1.copy(true).equals(t2.copy(true));
	}
}
 
Example 10
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the field types of row and structured types.
 */
public static List<LogicalType> getFieldTypes(LogicalType logicalType) {
	if (logicalType instanceof DistinctType) {
		return getFieldTypes(((DistinctType) logicalType).getSourceType());
	}
	return logicalType.getChildren();
}
 
Example 11
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected Optional<LogicalType> defaultMethod(LogicalType logicalType) {
	if (predicate.test(logicalType)) {
		return Optional.of(logicalType);
	}
	for (LogicalType child : logicalType.getChildren()) {
		final Optional<LogicalType> foundType = child.accept(this);
		if (foundType.isPresent()) {
			return foundType;
		}
	}
	return Optional.empty();
}
 
Example 12
Source File: PlannerTypeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Now in the conversion to the TypeInformation from DataType, type may loose some information
 * about nullable and precision. So we add this method to do a soft check.
 *
 * <p>The difference of {@link #isInteroperable} is ignore precisions.
 */
public static boolean isAssignable(LogicalType t1, LogicalType t2) {
	// Soft check for CharType, it is converted to String TypeInformation and loose char information.
	if (t1.getTypeRoot().getFamilies().contains(CHARACTER_STRING) &&
			t2.getTypeRoot().getFamilies().contains(CHARACTER_STRING)) {
		return true;
	}
	if (t1.getTypeRoot().getFamilies().contains(BINARY_STRING) &&
			t2.getTypeRoot().getFamilies().contains(BINARY_STRING)) {
		return true;
	}
	if (t1.getTypeRoot() != t2.getTypeRoot()) {
		return false;
	}

	switch (t1.getTypeRoot()) {
		// only support precisions for DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
		// still consider precision for others (e.g. TIME).
		// TODO: add other precision types here in the future
		case DECIMAL:
		case TIMESTAMP_WITHOUT_TIME_ZONE:
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			return true;
		default:
			if (t1.getChildren().isEmpty()) {
				return t1.copy(true).equals(t2.copy(true));
			} else {
				List<LogicalType> children1 = t1.getChildren();
				List<LogicalType> children2 = t2.getChildren();
				if (children1.size() != children2.size()) {
					return false;
				}
				for (int i = 0; i < children1.size(); i++) {
					if (!isAssignable(children1.get(i), children2.get(i))) {
						return false;
					}
				}
				return true;
			}
	}
}