use of org.apache.flink.table.types.logical.NullType 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;
}
use of org.apache.flink.table.types.logical.NullType in project flink by apache.
the class LogicalTypeJsonSerdeTest method testLogicalTypeSerde.
private static List<LogicalType> testLogicalTypeSerde() {
final List<LogicalType> types = Arrays.asList(new BooleanType(), new TinyIntType(), new SmallIntType(), new IntType(), new BigIntType(), new FloatType(), new DoubleType(), new DecimalType(10), new DecimalType(15, 5), CharType.ofEmptyLiteral(), new CharType(), new CharType(5), VarCharType.ofEmptyLiteral(), new VarCharType(), new VarCharType(5), BinaryType.ofEmptyLiteral(), new BinaryType(), new BinaryType(100), VarBinaryType.ofEmptyLiteral(), new VarBinaryType(), new VarBinaryType(100), new DateType(), new TimeType(), new TimeType(3), new TimestampType(), new TimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(), new ZonedTimestampType(3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(), new LocalZonedTimestampType(3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR), new DayTimeIntervalType(false, DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR, 3, 6), new YearMonthIntervalType(YearMonthIntervalType.YearMonthResolution.YEAR_TO_MONTH), new YearMonthIntervalType(false, YearMonthIntervalType.YearMonthResolution.MONTH, 2), new ZonedTimestampType(), new LocalZonedTimestampType(), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new SymbolType<>(), new ArrayType(new IntType(false)), new ArrayType(new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new ZonedTimestampType(false, TimestampKind.ROWTIME, 3)), new ArrayType(new TimestampType()), new ArrayType(CharType.ofEmptyLiteral()), new ArrayType(VarCharType.ofEmptyLiteral()), new ArrayType(BinaryType.ofEmptyLiteral()), new ArrayType(VarBinaryType.ofEmptyLiteral()), new MapType(new BigIntType(), new IntType(false)), new MapType(new TimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType()), new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()), new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()), new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()), new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), new MultisetType(new IntType(false)), new MultisetType(new TimestampType()), new MultisetType(new TimestampType(true, TimestampKind.ROWTIME, 3)), new MultisetType(CharType.ofEmptyLiteral()), new MultisetType(VarCharType.ofEmptyLiteral()), new MultisetType(BinaryType.ofEmptyLiteral()), new MultisetType(VarBinaryType.ofEmptyLiteral()), RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)), RowType.of(new LogicalType[] { new BigIntType(), new IntType(false), new VarCharType(200) }, new String[] { "f1", "f2", "f3" }), RowType.of(new TimestampType(false, TimestampKind.ROWTIME, 3), new TimestampType(false, TimestampKind.REGULAR, 3), new ZonedTimestampType(false, TimestampKind.ROWTIME, 3), new ZonedTimestampType(false, TimestampKind.REGULAR, 3), new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3), new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3), new LocalZonedTimestampType(false, TimestampKind.REGULAR, 3)), RowType.of(CharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()), // registered structured type
StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType"), PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).comparison(StructuredType.StructuredComparison.FULL).setFinal(false).setInstantiable(false).superType(StructuredType.newBuilder(ObjectIdentifier.of("cat", "db", "structuredType2")).attributes(Collections.singletonList(new StructuredType.StructuredAttribute("f0", new BigIntType(false)))).build()).description("description for StructuredType").build(), // unregistered structured type
StructuredType.newBuilder(PojoClass.class).attributes(Arrays.asList(new StructuredType.StructuredAttribute("f0", new IntType(true)), new StructuredType.StructuredAttribute("f1", new BigIntType(true)), new StructuredType.StructuredAttribute("f2", new VarCharType(200), "desc"))).build(), // registered distinct type
DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(5)).build(), DistinctType.newBuilder(ObjectIdentifier.of("cat", "db", "distinctType"), new VarCharType(false, 5)).build(), // custom RawType
new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE), // external RawType
new RawType<>(Row.class, ExternalSerializer.of(DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
final List<LogicalType> mutableTypes = new ArrayList<>(types);
// RawType for MapView
addRawTypesForMapView(mutableTypes, new VarCharType(100), new VarCharType(100));
addRawTypesForMapView(mutableTypes, new VarCharType(100), new BigIntType());
addRawTypesForMapView(mutableTypes, new BigIntType(), new VarCharType(100));
addRawTypesForMapView(mutableTypes, new BigIntType(), new BigIntType());
// RawType for ListView
addRawTypesForListView(mutableTypes, new VarCharType(100));
addRawTypesForListView(mutableTypes, new BigIntType());
// RawType for custom MapView
mutableTypes.add(DataViewUtils.adjustDataViews(MapView.newMapViewDataType(DataTypes.STRING().toInternal(), DataTypes.STRING().bridgedTo(byte[].class)), false).getLogicalType());
final List<LogicalType> allTypes = new ArrayList<>();
// consider nullable
for (LogicalType type : mutableTypes) {
allTypes.add(type.copy(true));
allTypes.add(type.copy(false));
}
// ignore nullable for NullType
allTypes.add(new NullType());
return allTypes;
}
Aggregations