Search in sources :

Example 21 with LogicalType

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

the class KafkaConnectorOptionsUtil method createValueFormatProjection.

/**
 * Creates an array of indices that determine which physical fields of the table schema to
 * include in the value format.
 *
 * <p>See {@link KafkaConnectorOptions#VALUE_FORMAT}, {@link
 * KafkaConnectorOptions#VALUE_FIELDS_INCLUDE}, and {@link
 * KafkaConnectorOptions#KEY_FIELDS_PREFIX} for more information.
 */
public static int[] createValueFormatProjection(ReadableConfig options, DataType physicalDataType) {
    final LogicalType physicalType = physicalDataType.getLogicalType();
    Preconditions.checkArgument(physicalType.is(LogicalTypeRoot.ROW), "Row data type expected.");
    final int physicalFieldCount = LogicalTypeChecks.getFieldCount(physicalType);
    final IntStream physicalFields = IntStream.range(0, physicalFieldCount);
    final String keyPrefix = options.getOptional(KEY_FIELDS_PREFIX).orElse("");
    final ValueFieldsStrategy strategy = options.get(VALUE_FIELDS_INCLUDE);
    if (strategy == ValueFieldsStrategy.ALL) {
        if (keyPrefix.length() > 0) {
            throw new ValidationException(String.format("A key prefix is not allowed when option '%s' is set to '%s'. " + "Set it to '%s' instead to avoid field overlaps.", VALUE_FIELDS_INCLUDE.key(), ValueFieldsStrategy.ALL, ValueFieldsStrategy.EXCEPT_KEY));
        }
        return physicalFields.toArray();
    } else if (strategy == ValueFieldsStrategy.EXCEPT_KEY) {
        final int[] keyProjection = createKeyFormatProjection(options, physicalDataType);
        return physicalFields.filter(pos -> IntStream.of(keyProjection).noneMatch(k -> k == pos)).toArray();
    }
    throw new TableException("Unknown value fields strategy:" + strategy);
}
Also used : DynamicTableFactory(org.apache.flink.table.factories.DynamicTableFactory) IntStream(java.util.stream.IntStream) DeliveryGuarantee(org.apache.flink.connector.base.DeliveryGuarantee) DataType(org.apache.flink.table.types.DataType) FlinkException(org.apache.flink.util.FlinkException) ConfigOptions(org.apache.flink.configuration.ConfigOptions) Arrays(java.util.Arrays) SCAN_STARTUP_TIMESTAMP_MILLIS(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.SCAN_STARTUP_TIMESTAMP_MILLIS) KEY_FORMAT(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.KEY_FORMAT) TOPIC(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.TOPIC) TRANSACTIONAL_ID_PREFIX(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.TRANSACTIONAL_ID_PREFIX) DELIVERY_GUARANTEE(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.DELIVERY_GUARANTEE) TOPIC_PATTERN(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.TOPIC_PATTERN) HashMap(java.util.HashMap) VALUE_FIELDS_INCLUDE(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.VALUE_FIELDS_INCLUDE) InstantiationUtil(org.apache.flink.util.InstantiationUtil) ReadableConfig(org.apache.flink.configuration.ReadableConfig) Map(java.util.Map) ConfigOption(org.apache.flink.configuration.ConfigOption) FORMAT(org.apache.flink.table.factories.FactoryUtil.FORMAT) FlinkFixedPartitioner(org.apache.flink.streaming.connectors.kafka.partitioner.FlinkFixedPartitioner) SCAN_STARTUP_MODE(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.SCAN_STARTUP_MODE) RowData(org.apache.flink.table.data.RowData) Properties(java.util.Properties) KafkaTopicPartition(org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicPartition) ValueFieldsStrategy(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.ValueFieldsStrategy) Configuration(org.apache.flink.configuration.Configuration) TableException(org.apache.flink.table.api.TableException) VALUE_FORMAT(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.VALUE_FORMAT) ScanStartupMode(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.ScanStartupMode) Preconditions(org.apache.flink.util.Preconditions) FlinkKafkaPartitioner(org.apache.flink.streaming.connectors.kafka.partitioner.FlinkKafkaPartitioner) StartupMode(org.apache.flink.streaming.connectors.kafka.config.StartupMode) List(java.util.List) SINK_PARTITIONER(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.SINK_PARTITIONER) FactoryUtil(org.apache.flink.table.factories.FactoryUtil) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) Optional(java.util.Optional) SCAN_STARTUP_SPECIFIC_OFFSETS(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.SCAN_STARTUP_SPECIFIC_OFFSETS) Internal(org.apache.flink.annotation.Internal) Pattern(java.util.regex.Pattern) KEY_FIELDS_PREFIX(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.KEY_FIELDS_PREFIX) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) KEY_FIELDS(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.KEY_FIELDS) TableException(org.apache.flink.table.api.TableException) ValidationException(org.apache.flink.table.api.ValidationException) ValueFieldsStrategy(org.apache.flink.streaming.connectors.kafka.table.KafkaConnectorOptions.ValueFieldsStrategy) LogicalType(org.apache.flink.table.types.logical.LogicalType) IntStream(java.util.stream.IntStream)

Example 22 with LogicalType

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

the class RowDataToJsonConverters method createArrayConverter.

private RowDataToJsonConverter createArrayConverter(ArrayType type) {
    final LogicalType elementType = type.getElementType();
    final RowDataToJsonConverter elementConverter = createConverter(elementType);
    final ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(elementType);
    return (mapper, reuse, value) -> {
        ArrayNode node;
        // reuse could be a NullNode if last record is null.
        if (reuse == null || reuse.isNull()) {
            node = mapper.createArrayNode();
        } else {
            node = (ArrayNode) reuse;
            node.removeAll();
        }
        ArrayData array = (ArrayData) value;
        int numElements = array.size();
        for (int i = 0; i < numElements; i++) {
            Object element = elementGetter.getElementOrNull(array, i);
            node.add(elementConverter.convert(mapper, null, element));
        }
        return node;
    };
}
Also used : ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) Arrays(java.util.Arrays) IntType(org.apache.flink.table.types.logical.IntType) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) MapType(org.apache.flink.table.types.logical.MapType) RowType(org.apache.flink.table.types.logical.RowType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) BigDecimal(java.math.BigDecimal) SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) ISO_LOCAL_DATE(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) MultisetType(org.apache.flink.table.types.logical.MultisetType) ISO8601_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_FORMAT) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) MapData(org.apache.flink.table.data.MapData) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) TableException(org.apache.flink.table.api.TableException) DecimalData(org.apache.flink.table.data.DecimalData) ArrayType(org.apache.flink.table.types.logical.ArrayType) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) Serializable(java.io.Serializable) SQL_TIME_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIME_FORMAT) ArrayData(org.apache.flink.table.data.ArrayData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) LogicalType(org.apache.flink.table.types.logical.LogicalType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) ArrayData(org.apache.flink.table.data.ArrayData)

Example 23 with LogicalType

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

the class TableSchema method validateAndCreateNameToTypeMapping.

/**
 * Creates a mapping from field name to data type, the field name can be a nested field. This is
 * mainly used for validating whether the rowtime attribute (might be nested) exists in the
 * schema. During creating, it also validates whether there is duplicate field names.
 *
 * <p>For example, a "f0" field of ROW type has two nested fields "q1" and "q2". Then the
 * mapping will be ["f0" -> ROW, "f0.q1" -> INT, "f0.q2" -> STRING].
 *
 * <pre>{@code
 * f0 ROW<q1 INT, q2 STRING>
 * }</pre>
 *
 * @param fieldNameToType Field name to type mapping that to update
 * @param fieldName Name of this field, e.g. "q1" or "q2" in the above example
 * @param fieldType Data type of this field
 * @param parentFieldName Field name of parent type, e.g. "f0" in the above example
 */
private static void validateAndCreateNameToTypeMapping(Map<String, LogicalType> fieldNameToType, String fieldName, LogicalType fieldType, String parentFieldName) {
    String fullFieldName = parentFieldName.isEmpty() ? fieldName : parentFieldName + "." + fieldName;
    LogicalType oldType = fieldNameToType.put(fullFieldName, fieldType);
    if (oldType != null) {
        throw new ValidationException("Field names must be unique. Duplicate field: '" + fullFieldName + "'");
    }
    if (isCompositeType(fieldType) && !(fieldType instanceof LegacyTypeInformationType)) {
        final List<String> fieldNames = LogicalTypeChecks.getFieldNames(fieldType);
        final List<LogicalType> fieldTypes = fieldType.getChildren();
        IntStream.range(0, fieldNames.size()).forEach(i -> validateAndCreateNameToTypeMapping(fieldNameToType, fieldNames.get(i), fieldTypes.get(i), fullFieldName));
    }
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType)

Example 24 with LogicalType

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

the class DataTypeUtilsTest method testExpandDistinctType.

@Test
public void testExpandDistinctType() {
    FieldsDataType dataType = (FieldsDataType) ROW(FIELD("f0", INT()), FIELD("f1", STRING()), FIELD("f2", TIMESTAMP(5).bridgedTo(Timestamp.class)), FIELD("f3", TIMESTAMP(3)));
    LogicalType originalLogicalType = dataType.getLogicalType();
    DistinctType distinctLogicalType = DistinctType.newBuilder(ObjectIdentifier.of("catalog", "database", "type"), originalLogicalType).build();
    DataType distinctDataType = new FieldsDataType(distinctLogicalType, dataType.getChildren());
    ResolvedSchema schema = DataTypeUtils.expandCompositeTypeToSchema(distinctDataType);
    assertThat(schema).isEqualTo(ResolvedSchema.of(Column.physical("f0", INT()), Column.physical("f1", STRING()), Column.physical("f2", TIMESTAMP(5).bridgedTo(Timestamp.class)), Column.physical("f3", TIMESTAMP(3).bridgedTo(LocalDateTime.class))));
}
Also used : LocalDateTime(java.time.LocalDateTime) FieldsDataType(org.apache.flink.table.types.FieldsDataType) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) DataType(org.apache.flink.table.types.DataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 25 with LogicalType

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

the class InternalDataUtils method toGenericRow.

static GenericRowData toGenericRow(RowData rowData, LogicalType logicalType) {
    final List<LogicalType> fieldTypes = LogicalTypeChecks.getFieldTypes(logicalType);
    final GenericRowData row = new GenericRowData(fieldTypes.size());
    row.setRowKind(rowData.getRowKind());
    for (int i = 0; i < fieldTypes.size(); i++) {
        if (rowData.isNullAt(i)) {
            row.setField(i, null);
        } else {
            LogicalType fieldType = fieldTypes.get(i);
            RowData.FieldGetter fieldGetter = RowData.createFieldGetter(fieldType, i);
            row.setField(i, toGenericInternalData(fieldGetter.getFieldOrNull(rowData), fieldType));
        }
    }
    return row;
}
Also used : RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) GenericRowData(org.apache.flink.table.data.GenericRowData)

Aggregations

LogicalType (org.apache.flink.table.types.logical.LogicalType)192 DataType (org.apache.flink.table.types.DataType)53 RowType (org.apache.flink.table.types.logical.RowType)53 RowData (org.apache.flink.table.data.RowData)45 List (java.util.List)29 ArrayList (java.util.ArrayList)28 TableException (org.apache.flink.table.api.TableException)25 TimestampType (org.apache.flink.table.types.logical.TimestampType)25 Internal (org.apache.flink.annotation.Internal)21 IntType (org.apache.flink.table.types.logical.IntType)21 Map (java.util.Map)20 ValidationException (org.apache.flink.table.api.ValidationException)20 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)19 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)17 Test (org.junit.Test)17 BigIntType (org.apache.flink.table.types.logical.BigIntType)16 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)16 GenericRowData (org.apache.flink.table.data.GenericRowData)15 Arrays (java.util.Arrays)14