use of org.apache.flink.table.types.logical.LogicalTypeRoot.NULL in project flink by apache.
the class LogicalTypeMerging method findCommonRowType.
@Nullable
private static LogicalType findCommonRowType(List<LogicalType> normalizedTypes) {
final List<LogicalType> children = findCommonChildrenTypes(normalizedTypes);
if (children == null) {
return null;
}
final RowType firstType = (RowType) normalizedTypes.get(0);
final List<RowType.RowField> newFields = IntStream.range(0, children.size()).mapToObj(pos -> {
final LogicalType newType = children.get(pos);
final RowType.RowField originalField = firstType.getFields().get(pos);
if (originalField.getDescription().isPresent()) {
return new RowType.RowField(originalField.getName(), newType, originalField.getDescription().get());
} else {
return new RowType.RowField(originalField.getName(), newType);
}
}).collect(Collectors.toList());
return new RowType(newFields);
}
use of org.apache.flink.table.types.logical.LogicalTypeRoot.NULL in project flink by apache.
the class ValuesOperationFactory method create.
/**
* Creates a valid {@link ValuesQueryOperation} operation.
*
* <p>It derives a row type based on {@link LogicalTypeMerging}. It flattens any row
* constructors. It does not flatten ROWs which are a result of e.g. a function call.
*
* <p>The resulting schema can be provided manually. If it is not, the schema will be
* automatically derived from the types of the expressions.
*/
QueryOperation create(@Nullable ResolvedSchema expectedSchema, List<ResolvedExpression> resolvedExpressions, ExpressionResolver.PostResolverFactory postResolverFactory) {
List<List<ResolvedExpression>> resolvedRows = unwrapFromRowConstructor(resolvedExpressions);
if (expectedSchema != null) {
verifyAllSameSize(resolvedRows, expectedSchema.getColumnCount());
}
ResolvedSchema schema = Optional.ofNullable(expectedSchema).orElseGet(() -> extractSchema(resolvedRows));
List<List<ResolvedExpression>> castedExpressions = resolvedRows.stream().map(row -> convertTopLevelExpressionToExpectedRowType(postResolverFactory, schema.getColumnDataTypes(), row)).collect(Collectors.toList());
return new ValuesQueryOperation(castedExpressions, schema);
}
use of org.apache.flink.table.types.logical.LogicalTypeRoot.NULL in project flink by apache.
the class LogicalTypeMerging method findCommonType.
/**
* 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 (typeWithNullability.is(NULL)) {
return Optional.empty();
}
return Optional.of(typeWithNullability);
}
return Optional.empty();
}
Aggregations