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

The following examples show how to use org.apache.flink.table.types.logical.LogicalType#copy() . 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: LogicalTypeGeneralization.java    From flink with 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 2
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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 (hasRoot(typeWithNullability, NULL)) {
			return Optional.empty();
		}
		return Optional.of(typeWithNullability);
	}
	return Optional.empty();
}
 
Example 3
Source File: LogicalTypeDuplicator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected LogicalType defaultMethod(LogicalType logicalType) {
	return logicalType.copy();
}
 
Example 4
Source File: LogicalTypeDuplicator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected LogicalType defaultMethod(LogicalType logicalType) {
	return logicalType.copy();
}
 
Example 5
Source File: OrcSplitReaderUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * See {@code org.apache.flink.table.catalog.hive.util.HiveTypeUtil}.
 */
public static TypeDescription logicalTypeToOrcType(LogicalType type) {
	type = type.copy(true);
	switch (type.getTypeRoot()) {
		case CHAR:
			return TypeDescription.createChar().withMaxLength(((CharType) type).getLength());
		case VARCHAR:
			int len = ((VarCharType) type).getLength();
			if (len == VarCharType.MAX_LENGTH) {
				return TypeDescription.createString();
			} else {
				return TypeDescription.createVarchar().withMaxLength(len);
			}
		case BOOLEAN:
			return TypeDescription.createBoolean();
		case VARBINARY:
			if (type.equals(DataTypes.BYTES().getLogicalType())) {
				return TypeDescription.createBinary();
			} else {
				throw new UnsupportedOperationException(
						"Not support other binary type: " + type);
			}
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			return TypeDescription.createDecimal()
					.withScale(decimalType.getScale())
					.withPrecision(decimalType.getPrecision());
		case TINYINT:
			return TypeDescription.createByte();
		case SMALLINT:
			return TypeDescription.createShort();
		case INTEGER:
			return TypeDescription.createInt();
		case BIGINT:
			return TypeDescription.createLong();
		case FLOAT:
			return TypeDescription.createFloat();
		case DOUBLE:
			return TypeDescription.createDouble();
		case DATE:
			return TypeDescription.createDate();
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return TypeDescription.createTimestamp();
		case ARRAY:
			ArrayType arrayType = (ArrayType) type;
			return TypeDescription.createList(logicalTypeToOrcType(arrayType.getElementType()));
		case MAP:
			MapType mapType = (MapType) type;
			return TypeDescription.createMap(
					logicalTypeToOrcType(mapType.getKeyType()),
					logicalTypeToOrcType(mapType.getValueType()));
		case ROW:
			RowType rowType = (RowType) type;
			TypeDescription struct = TypeDescription.createStruct();
			for (int i = 0; i < rowType.getFieldCount(); i++) {
				struct.addField(
						rowType.getFieldNames().get(i),
						logicalTypeToOrcType(rowType.getChildren().get(i)));
			}
			return struct;
		default:
			throw new UnsupportedOperationException("Unsupported type: " + type);
	}
}
 
Example 6
Source File: LogicalTypesTest.java    From flink with Apache License 2.0 3 votes vote down vote up
private static void testNullability(LogicalType nullableType) {
	final LogicalType notNullInstance = nullableType.copy(false);

	assertNotEquals(nullableType, notNullInstance);

	assertFalse(notNullInstance.isNullable());
}
 
Example 7
Source File: LogicalTypesTest.java    From flink with Apache License 2.0 3 votes vote down vote up
private static void testNullability(LogicalType nullableType) {
	final LogicalType notNullInstance = nullableType.copy(false);

	assertNotEquals(nullableType, notNullInstance);

	assertFalse(notNullInstance.isNullable());
}