Search in sources :

Example 6 with FieldsDataType

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());
}
Also used : DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) Arrays(java.util.Arrays) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) StructuredType(org.apache.flink.table.types.logical.StructuredType) MapType(org.apache.flink.table.types.logical.MapType) RowField(org.apache.flink.table.types.logical.RowType.RowField) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) Projection(org.apache.flink.table.connector.Projection) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) List(java.util.List) Stream(java.util.stream.Stream) CollectionDataType(org.apache.flink.table.types.CollectionDataType) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) Optional(java.util.Optional) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) LogicalTypeUtils.toInternalConversionClass(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.toInternalConversionClass) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) Column(org.apache.flink.table.catalog.Column) RowType(org.apache.flink.table.types.logical.RowType) TimestampKind(org.apache.flink.table.types.logical.TimestampKind) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) FieldsDataType(org.apache.flink.table.types.FieldsDataType) DISTINCT_TYPE(org.apache.flink.table.types.logical.LogicalTypeRoot.DISTINCT_TYPE) LogicalTypeUtils.removeTimeAttributes(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.removeTimeAttributes) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) Nullable(javax.annotation.Nullable) ROW(org.apache.flink.table.types.logical.LogicalTypeRoot.ROW) MultisetType(org.apache.flink.table.types.logical.MultisetType) LogicalTypeChecks.getFieldNames(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getFieldNames) LogicalTypeUtils.getAtomicName(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.getAtomicName) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) LogicalTypeChecks.isCompositeType(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.isCompositeType) LogicalTypeDefaultVisitor(org.apache.flink.table.types.logical.utils.LogicalTypeDefaultVisitor) DataTypes(org.apache.flink.table.api.DataTypes) TypeTransformation(org.apache.flink.table.types.inference.TypeTransformation) ArrayType(org.apache.flink.table.types.logical.ArrayType) STRUCTURED_TYPE(org.apache.flink.table.types.logical.LogicalTypeRoot.STRUCTURED_TYPE) DataTypeVisitor(org.apache.flink.table.types.DataTypeVisitor) ExtractionUtils.primitiveToWrapper(org.apache.flink.table.types.extraction.ExtractionUtils.primitiveToWrapper) Internal(org.apache.flink.annotation.Internal) Collections(java.util.Collections) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) FieldsDataType(org.apache.flink.table.types.FieldsDataType) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 7 with FieldsDataType

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));
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType)

Example 8 with FieldsDataType

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);
}
Also used : RowField(org.apache.flink.table.types.logical.RowType.RowField) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) StructuredType(org.apache.flink.table.types.logical.StructuredType) MapType(org.apache.flink.table.types.logical.MapType) CharType(org.apache.flink.table.types.logical.CharType) RawType(org.apache.flink.table.types.logical.RawType) DecimalType(org.apache.flink.table.types.logical.DecimalType) BooleanType(org.apache.flink.table.types.logical.BooleanType) RowField(org.apache.flink.table.types.logical.RowType.RowField) Map(java.util.Map) ExtractionUtils.validateStructuredClass(org.apache.flink.table.types.extraction.ExtractionUtils.validateStructuredClass) TimeType(org.apache.flink.table.types.logical.TimeType) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) NullType(org.apache.flink.table.types.logical.NullType) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) CollectionDataType(org.apache.flink.table.types.CollectionDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) Optional(java.util.Optional) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) BinaryType(org.apache.flink.table.types.logical.BinaryType) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) IntType(org.apache.flink.table.types.logical.IntType) PublicEvolving(org.apache.flink.annotation.PublicEvolving) HashMap(java.util.HashMap) FloatType(org.apache.flink.table.types.logical.FloatType) RowType(org.apache.flink.table.types.logical.RowType) TimestampType(org.apache.flink.table.types.logical.TimestampType) DoubleType(org.apache.flink.table.types.logical.DoubleType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType) TypeInfoDataTypeConverter(org.apache.flink.table.types.utils.TypeInfoDataTypeConverter) Nullable(javax.annotation.Nullable) AbstractDataType(org.apache.flink.table.types.AbstractDataType) MultisetType(org.apache.flink.table.types.logical.MultisetType) BigIntType(org.apache.flink.table.types.logical.BigIntType) DateType(org.apache.flink.table.types.logical.DateType) VarCharType(org.apache.flink.table.types.logical.VarCharType) ArrayType(org.apache.flink.table.types.logical.ArrayType) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) UnresolvedDataType(org.apache.flink.table.types.UnresolvedDataType) DataTypeHint(org.apache.flink.table.annotation.DataTypeHint) DayTimeResolution(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution) YearMonthResolution(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) AbstractDataType(org.apache.flink.table.types.AbstractDataType) UnresolvedDataType(org.apache.flink.table.types.UnresolvedDataType) RowType(org.apache.flink.table.types.logical.RowType)

Example 9 with FieldsDataType

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);
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredType(org.apache.flink.table.types.logical.StructuredType) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType)

Example 10 with FieldsDataType

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);
}
Also used : FieldsDataType(org.apache.flink.table.types.FieldsDataType) BooleanType(org.apache.flink.table.types.logical.BooleanType) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) StructuredType(org.apache.flink.table.types.logical.StructuredType)

Aggregations

DataType (org.apache.flink.table.types.DataType)14 FieldsDataType (org.apache.flink.table.types.FieldsDataType)14 StructuredType (org.apache.flink.table.types.logical.StructuredType)11 StructuredAttribute (org.apache.flink.table.types.logical.StructuredType.StructuredAttribute)8 BigIntType (org.apache.flink.table.types.logical.BigIntType)6 IntType (org.apache.flink.table.types.logical.IntType)6 LogicalType (org.apache.flink.table.types.logical.LogicalType)5 CollectionDataType (org.apache.flink.table.types.CollectionDataType)4 KeyValueDataType (org.apache.flink.table.types.KeyValueDataType)4 BooleanType (org.apache.flink.table.types.logical.BooleanType)4 MapType (org.apache.flink.table.types.logical.MapType)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Nullable (javax.annotation.Nullable)3 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)3 Test (org.junit.Test)3 Timestamp (java.sql.Timestamp)2 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2