use of org.apache.flink.table.types.FieldsDataType in project flink by apache.
the class DataTypeUtils method stripRowPrefix.
/**
* Removes a string prefix from the fields of the given row data type.
*/
public static DataType stripRowPrefix(DataType dataType, String prefix) {
Preconditions.checkArgument(dataType.getLogicalType().is(ROW), "Row data type expected.");
final RowType rowType = (RowType) dataType.getLogicalType();
final List<String> newFieldNames = rowType.getFieldNames().stream().map(s -> {
if (s.startsWith(prefix)) {
return s.substring(prefix.length());
}
return s;
}).collect(Collectors.toList());
final LogicalType newRowType = LogicalTypeUtils.renameRowFields(rowType, newFieldNames);
return new FieldsDataType(newRowType, dataType.getConversionClass(), dataType.getChildren());
}
use of org.apache.flink.table.types.FieldsDataType in project flink by apache.
the class ValuesOperationFactory method convertRowToExpectedType.
private Optional<ResolvedExpression> convertRowToExpectedType(ResolvedExpression sourceExpression, FieldsDataType targetDataType, ExpressionResolver.PostResolverFactory postResolverFactory) {
List<DataType> targetDataTypes = targetDataType.getChildren();
List<ResolvedExpression> resolvedChildren = sourceExpression.getResolvedChildren();
if (resolvedChildren.size() != targetDataTypes.size()) {
return Optional.empty();
}
ResolvedExpression[] castedChildren = new ResolvedExpression[resolvedChildren.size()];
for (int i = 0; i < resolvedChildren.size(); i++) {
boolean typesMatch = resolvedChildren.get(i).getOutputDataType().getLogicalType().equals(targetDataTypes.get(i).getLogicalType());
if (typesMatch) {
castedChildren[i] = resolvedChildren.get(i);
}
ResolvedExpression child = resolvedChildren.get(i);
DataType targetChildDataType = targetDataTypes.get(i);
Optional<ResolvedExpression> castedChild = convertToExpectedType(child, targetChildDataType, postResolverFactory);
if (!castedChild.isPresent()) {
return Optional.empty();
} else {
castedChildren[i] = castedChild.get();
}
}
return Optional.of(postResolverFactory.row(targetDataType, castedChildren));
}
use of org.apache.flink.table.types.FieldsDataType in project flink by apache.
the class DataTypes method ROW.
/**
* Data type of a sequence of fields. A field consists of a field name, field type, and an
* optional description. The most specific type of a row of a table is a row type. In this case,
* each column of the row corresponds to the field of the row type that has the same ordinal
* position as the column.
*
* <p>Compared to the SQL standard, an optional field description simplifies the handling with
* complex structures.
*
* <p>Use {@link #FIELD(String, DataType)} or {@link #FIELD(String, DataType, String)} to
* construct fields.
*
* @see RowType
*/
public static DataType ROW(Field... fields) {
final List<RowField> logicalFields = Stream.of(fields).map(f -> Preconditions.checkNotNull(f, "Field definition must not be null.")).map(f -> new RowField(f.name, f.dataType.getLogicalType(), f.description)).collect(Collectors.toList());
final List<DataType> fieldDataTypes = Stream.of(fields).map(f -> f.dataType).collect(Collectors.toList());
return new FieldsDataType(new RowType(logicalFields), fieldDataTypes);
}
use of org.apache.flink.table.types.FieldsDataType in project flink by apache.
the class DataTypeExtractorTest method getPojoWithRawSelfReferenceDataType.
private static DataType getPojoWithRawSelfReferenceDataType() {
final StructuredType.Builder builder = StructuredType.newBuilder(PojoWithRawSelfReference.class);
builder.attributes(Arrays.asList(new StructuredAttribute("integer", new IntType()), new StructuredAttribute("reference", dummyRaw(PojoWithRawSelfReference.class).getLogicalType())));
builder.setFinal(true);
builder.setInstantiable(true);
final StructuredType structuredType = builder.build();
final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.INT(), dummyRaw(PojoWithRawSelfReference.class));
return new FieldsDataType(structuredType, PojoWithRawSelfReference.class, fieldDataTypes);
}
use of org.apache.flink.table.types.FieldsDataType in project flink by apache.
the class DataTypeExtractorTest method getInnerTupleDataType.
private static DataType getInnerTupleDataType() {
final StructuredType.Builder builder = StructuredType.newBuilder(Tuple2.class);
builder.attributes(Arrays.asList(new StructuredAttribute("f0", VarCharType.STRING_TYPE), new StructuredAttribute("f1", new BooleanType())));
builder.setFinal(true);
builder.setInstantiable(true);
final StructuredType structuredType = builder.build();
final List<DataType> fieldDataTypes = Arrays.asList(DataTypes.STRING(), DataTypes.BOOLEAN());
return new FieldsDataType(structuredType, Tuple2.class, fieldDataTypes);
}
Aggregations