Search in sources :

Example 96 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class DynamicSinkUtils method createConsumedType.

/**
 * Returns the {@link DataType} that a sink should consume as the output from the runtime.
 *
 * <p>The format looks as follows: {@code PHYSICAL COLUMNS + PERSISTED METADATA COLUMNS}
 */
private static RowType createConsumedType(ResolvedSchema schema, DynamicTableSink sink) {
    final Map<String, DataType> metadataMap = extractMetadataMap(sink);
    final Stream<RowField> physicalFields = schema.getColumns().stream().filter(Column::isPhysical).map(c -> new RowField(c.getName(), c.getDataType().getLogicalType()));
    final Stream<RowField> metadataFields = createRequiredMetadataKeys(schema, sink).stream().map(k -> new RowField(k, metadataMap.get(k).getLogicalType()));
    final List<RowField> rowFields = Stream.concat(physicalFields, metadataFields).collect(Collectors.toList());
    return new RowType(false, rowFields);
}
Also used : RowField(org.apache.flink.table.types.logical.RowType.RowField) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) RowType(org.apache.flink.table.types.logical.RowType)

Example 97 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class DynamicSinkUtils method validateSchemaAndApplyImplicitCast.

/**
 * Checks if the given query can be written into the given sink's table schema.
 *
 * <p>It checks whether field types are compatible (types should be equal including precisions).
 * If types are not compatible, but can be implicitly cast, a cast projection will be applied.
 * Otherwise, an exception will be thrown.
 */
public static RelNode validateSchemaAndApplyImplicitCast(RelNode query, ResolvedSchema sinkSchema, String tableDebugName, DataTypeFactory dataTypeFactory, FlinkTypeFactory typeFactory) {
    final RowType queryType = FlinkTypeFactory.toLogicalRowType(query.getRowType());
    final List<RowField> queryFields = queryType.getFields();
    final RowType sinkType = (RowType) fixSinkDataType(dataTypeFactory, sinkSchema.toSinkRowDataType()).getLogicalType();
    final List<RowField> sinkFields = sinkType.getFields();
    if (queryFields.size() != sinkFields.size()) {
        throw createSchemaMismatchException("Different number of columns.", tableDebugName, queryFields, sinkFields);
    }
    boolean requiresCasting = false;
    for (int i = 0; i < sinkFields.size(); i++) {
        final LogicalType queryColumnType = queryFields.get(i).getType();
        final LogicalType sinkColumnType = sinkFields.get(i).getType();
        if (!supportsImplicitCast(queryColumnType, sinkColumnType)) {
            throw createSchemaMismatchException(String.format("Incompatible types for sink column '%s' at position %s.", sinkFields.get(i).getName(), i), tableDebugName, queryFields, sinkFields);
        }
        if (!supportsAvoidingCast(queryColumnType, sinkColumnType)) {
            requiresCasting = true;
        }
    }
    if (requiresCasting) {
        final RelDataType castRelDataType = typeFactory.buildRelNodeRowType(sinkType);
        return RelOptUtil.createCastRel(query, castRelDataType, true);
    }
    return query;
}
Also used : RowField(org.apache.flink.table.types.logical.RowType.RowField) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) RelDataType(org.apache.calcite.rel.type.RelDataType) RelHint(org.apache.calcite.rel.hint.RelHint)

Example 98 with RowType

use of org.apache.flink.table.types.logical.RowType 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);
}
Also used : Arrays(java.util.Arrays) INTERVAL(org.apache.flink.table.types.logical.LogicalTypeFamily.INTERVAL) AbstractList(java.util.AbstractList) MapType(org.apache.flink.table.types.logical.MapType) BINARY(org.apache.flink.table.types.logical.LogicalTypeRoot.BINARY) CharType(org.apache.flink.table.types.logical.CharType) MULTISET(org.apache.flink.table.types.logical.LogicalTypeRoot.MULTISET) NULL(org.apache.flink.table.types.logical.LogicalTypeRoot.NULL) DecimalType(org.apache.flink.table.types.logical.DecimalType) LogicalTypeChecks.getScale(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getScale) HOUR_TO_MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR_TO_MINUTE) Map(java.util.Map) TimeType(org.apache.flink.table.types.logical.TimeType) DECIMAL(org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL) DOUBLE(org.apache.flink.table.types.logical.LogicalTypeRoot.DOUBLE) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) INTERVAL_YEAR_MONTH(org.apache.flink.table.types.logical.LogicalTypeRoot.INTERVAL_YEAR_MONTH) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) TIMESTAMP_WITHOUT_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE) SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.SECOND) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) NullType(org.apache.flink.table.types.logical.NullType) HOUR_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR_TO_SECOND) EXACT_NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.EXACT_NUMERIC) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) VARCHAR(org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR) ARRAY(org.apache.flink.table.types.logical.LogicalTypeRoot.ARRAY) Optional(java.util.Optional) MONTH(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.MONTH) DAY(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY) LogicalTypeChecks.getPrecision(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getPrecision) YEAR(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.YEAR) TIMESTAMP_WITH_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITH_TIME_ZONE) LogicalTypeCasts.supportsImplicitCast(org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsImplicitCast) IntStream(java.util.stream.IntStream) BINARY_STRING(org.apache.flink.table.types.logical.LogicalTypeFamily.BINARY_STRING) TIMESTAMP(org.apache.flink.table.types.logical.LogicalTypeFamily.TIMESTAMP) BinaryType(org.apache.flink.table.types.logical.BinaryType) APPROXIMATE_NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.APPROXIMATE_NUMERIC) VARBINARY(org.apache.flink.table.types.logical.LogicalTypeRoot.VARBINARY) CHAR(org.apache.flink.table.types.logical.LogicalTypeRoot.CHAR) MINUTE_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.MINUTE_TO_SECOND) LogicalTypeChecks.getLength(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getLength) HashMap(java.util.HashMap) MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.MINUTE) RowType(org.apache.flink.table.types.logical.RowType) ArrayList(java.util.ArrayList) TimestampType(org.apache.flink.table.types.logical.TimestampType) DoubleType(org.apache.flink.table.types.logical.DoubleType) DAY_TO_SECOND(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_SECOND) CHARACTER_STRING(org.apache.flink.table.types.logical.LogicalTypeFamily.CHARACTER_STRING) MAP(org.apache.flink.table.types.logical.LogicalTypeRoot.MAP) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType) YEAR_TO_MONTH(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution.YEAR_TO_MONTH) NUMERIC(org.apache.flink.table.types.logical.LogicalTypeFamily.NUMERIC) Nullable(javax.annotation.Nullable) ROW(org.apache.flink.table.types.logical.LogicalTypeRoot.ROW) MultisetType(org.apache.flink.table.types.logical.MultisetType) TIME(org.apache.flink.table.types.logical.LogicalTypeFamily.TIME) DateType(org.apache.flink.table.types.logical.DateType) VarCharType(org.apache.flink.table.types.logical.VarCharType) ArrayType(org.apache.flink.table.types.logical.ArrayType) DATE(org.apache.flink.table.types.logical.LogicalTypeRoot.DATE) RAW(org.apache.flink.table.types.logical.LogicalTypeRoot.RAW) DAY_TO_MINUTE(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_MINUTE) DayTimeResolution(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution) YearMonthResolution(org.apache.flink.table.types.logical.YearMonthIntervalType.YearMonthResolution) DATETIME(org.apache.flink.table.types.logical.LogicalTypeFamily.DATETIME) TIMESTAMP_WITH_LOCAL_TIME_ZONE(org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) Internal(org.apache.flink.annotation.Internal) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) HOUR(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.HOUR) DAY_TO_HOUR(org.apache.flink.table.types.logical.DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Collections(java.util.Collections) INTERVAL_DAY_TIME(org.apache.flink.table.types.logical.LogicalTypeRoot.INTERVAL_DAY_TIME) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowType(org.apache.flink.table.types.logical.RowType) Nullable(javax.annotation.Nullable)

Example 99 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class LogicalTypeUtils method renameRowFields.

/**
 * Renames the fields of the given {@link RowType}.
 */
public static RowType renameRowFields(RowType rowType, List<String> newFieldNames) {
    Preconditions.checkArgument(rowType.getFieldCount() == newFieldNames.size(), "Row length and new names must match.");
    final List<RowField> newFields = IntStream.range(0, rowType.getFieldCount()).mapToObj(pos -> {
        final RowField oldField = rowType.getFields().get(pos);
        return new RowField(newFieldNames.get(pos), oldField.getType(), oldField.getDescription().orElse(null));
    }).collect(Collectors.toList());
    return new RowType(rowType.isNullable(), newFields);
}
Also used : RowField(org.apache.flink.table.types.logical.RowType.RowField) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) IntStream(java.util.stream.IntStream) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) MapData(org.apache.flink.table.data.MapData) StructuredType(org.apache.flink.table.types.logical.StructuredType) DecimalData(org.apache.flink.table.data.DecimalData) RowType(org.apache.flink.table.types.logical.RowType) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) StringData(org.apache.flink.table.data.StringData) TimestampType(org.apache.flink.table.types.logical.TimestampType) ArrayData(org.apache.flink.table.data.ArrayData) List(java.util.List) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) RawValueData(org.apache.flink.table.data.RawValueData) RowField(org.apache.flink.table.types.logical.RowType.RowField) Internal(org.apache.flink.annotation.Internal) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) RowType(org.apache.flink.table.types.logical.RowType)

Example 100 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class RandomGeneratorVisitor method visit.

@Override
public DataGeneratorContainer visit(RowType rowType) {
    List<DataGeneratorContainer> fieldContainers = rowType.getFields().stream().map(field -> {
        String fieldName = name + "." + field.getName();
        return field.getType().accept(new RandomGeneratorVisitor(fieldName, config));
    }).collect(Collectors.toList());
    ConfigOption<?>[] options = fieldContainers.stream().flatMap(container -> container.getOptions().stream()).toArray(ConfigOption[]::new);
    DataGenerator[] generators = fieldContainers.stream().map(DataGeneratorContainer::getGenerator).toArray(DataGenerator[]::new);
    return DataGeneratorContainer.of(new RowDataGenerator(generators, rowType.getFieldNames()), options);
}
Also used : ConfigOptions(org.apache.flink.configuration.ConfigOptions) GenericArrayData(org.apache.flink.table.data.GenericArrayData) TinyIntType(org.apache.flink.table.types.logical.TinyIntType) IntType(org.apache.flink.table.types.logical.IntType) FloatType(org.apache.flink.table.types.logical.FloatType) MapType(org.apache.flink.table.types.logical.MapType) RowType(org.apache.flink.table.types.logical.RowType) CharType(org.apache.flink.table.types.logical.CharType) TimestampType(org.apache.flink.table.types.logical.TimestampType) DecimalType(org.apache.flink.table.types.logical.DecimalType) DoubleType(org.apache.flink.table.types.logical.DoubleType) ReadableConfig(org.apache.flink.configuration.ReadableConfig) BooleanType(org.apache.flink.table.types.logical.BooleanType) RowDataGenerator(org.apache.flink.connector.datagen.table.types.RowDataGenerator) DecimalDataRandomGenerator(org.apache.flink.connector.datagen.table.types.DecimalDataRandomGenerator) GenericMapData(org.apache.flink.table.data.GenericMapData) Duration(java.time.Duration) Map(java.util.Map) DataGeneratorMapper(org.apache.flink.connector.datagen.table.types.DataGeneratorMapper) RandomGenerator(org.apache.flink.streaming.api.functions.source.datagen.RandomGenerator) ConfigOption(org.apache.flink.configuration.ConfigOption) YearMonthIntervalType(org.apache.flink.table.types.logical.YearMonthIntervalType) MultisetType(org.apache.flink.table.types.logical.MultisetType) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) DataGenerator(org.apache.flink.streaming.api.functions.source.datagen.DataGenerator) TimestampData(org.apache.flink.table.data.TimestampData) ConfigOptions.key(org.apache.flink.configuration.ConfigOptions.key) BigIntType(org.apache.flink.table.types.logical.BigIntType) DayTimeIntervalType(org.apache.flink.table.types.logical.DayTimeIntervalType) Set(java.util.Set) VarCharType(org.apache.flink.table.types.logical.VarCharType) ArrayType(org.apache.flink.table.types.logical.ArrayType) Collectors(java.util.stream.Collectors) SmallIntType(org.apache.flink.table.types.logical.SmallIntType) StringData(org.apache.flink.table.data.StringData) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) Internal(org.apache.flink.annotation.Internal) ZonedTimestampType(org.apache.flink.table.types.logical.ZonedTimestampType) ConfigOption(org.apache.flink.configuration.ConfigOption) RowDataGenerator(org.apache.flink.connector.datagen.table.types.RowDataGenerator) DataGenerator(org.apache.flink.streaming.api.functions.source.datagen.DataGenerator) RowDataGenerator(org.apache.flink.connector.datagen.table.types.RowDataGenerator)

Aggregations

RowType (org.apache.flink.table.types.logical.RowType)212 RowData (org.apache.flink.table.data.RowData)108 LogicalType (org.apache.flink.table.types.logical.LogicalType)59 DataType (org.apache.flink.table.types.DataType)57 Transformation (org.apache.flink.api.dag.Transformation)50 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)46 TableException (org.apache.flink.table.api.TableException)37 Test (org.junit.Test)36 GenericRowData (org.apache.flink.table.data.GenericRowData)33 ArrayList (java.util.ArrayList)28 List (java.util.List)28 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)26 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)25 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)22 TableConfig (org.apache.flink.table.api.TableConfig)19 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 TimestampType (org.apache.flink.table.types.logical.TimestampType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)17 Collections (java.util.Collections)16 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)16