Search in sources :

Example 16 with RowType

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

the class AvroSchemaConverterTest method testInvalidTimeTypeAvroSchemaConversion.

@Test
public void testInvalidTimeTypeAvroSchemaConversion() {
    RowType rowType = (RowType) TableSchema.builder().field("a", DataTypes.STRING()).field("b", DataTypes.TIME(6)).build().toRowDataType().getLogicalType();
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Avro does not support TIME type with precision: 6, it only supports precision less than 3.");
    AvroSchemaConverter.convertToSchema(rowType);
}
Also used : RowType(org.apache.flink.table.types.logical.RowType) Test(org.junit.Test)

Example 17 with RowType

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

the class CsvFormatFactory method createEncodingFormat.

@Override
public EncodingFormat<SerializationSchema<RowData>> createEncodingFormat(DynamicTableFactory.Context context, ReadableConfig formatOptions) {
    FactoryUtil.validateFactoryOptions(this, formatOptions);
    CsvCommons.validateFormatOptions(formatOptions);
    return new EncodingFormat<SerializationSchema<RowData>>() {

        @Override
        public SerializationSchema<RowData> createRuntimeEncoder(DynamicTableSink.Context context, DataType consumedDataType) {
            final RowType rowType = (RowType) consumedDataType.getLogicalType();
            final CsvRowDataSerializationSchema.Builder schemaBuilder = new CsvRowDataSerializationSchema.Builder(rowType);
            configureSerializationSchema(formatOptions, schemaBuilder);
            return schemaBuilder.build();
        }

        @Override
        public ChangelogMode getChangelogMode() {
            return ChangelogMode.insertOnly();
        }
    };
}
Also used : EncodingFormat(org.apache.flink.table.connector.format.EncodingFormat) RowData(org.apache.flink.table.data.RowData) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType)

Example 18 with RowType

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

the class CsvToRowDataConverters method createRowConverter.

public CsvToRowDataConverter createRowConverter(RowType rowType, boolean isTopLevel) {
    final CsvToRowDataConverter[] fieldConverters = rowType.getFields().stream().map(RowType.RowField::getType).map(this::createNullableConverter).toArray(CsvToRowDataConverter[]::new);
    final String[] fieldNames = rowType.getFieldNames().toArray(new String[0]);
    final int arity = fieldNames.length;
    return jsonNode -> {
        int nodeSize = jsonNode.size();
        if (nodeSize != 0) {
            validateArity(arity, nodeSize, ignoreParseErrors);
        } else {
            return null;
        }
        GenericRowData row = new GenericRowData(arity);
        for (int i = 0; i < arity; i++) {
            JsonNode field;
            // Jackson only supports mapping by name in the first level
            if (isTopLevel) {
                field = jsonNode.get(fieldNames[i]);
            } else {
                field = jsonNode.get(i);
            }
            try {
                if (field == null) {
                    row.setField(i, null);
                } else {
                    row.setField(i, fieldConverters[i].convert(field));
                }
            } catch (Throwable t) {
                throw new RuntimeException(String.format("Fail to deserialize at field: %s.", fieldNames[i]), t);
            }
        }
        return row;
    };
}
Also used : Array(java.lang.reflect.Array) GenericArrayData(org.apache.flink.table.data.GenericArrayData) LocalDateTime(java.time.LocalDateTime) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) 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) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) LocalTime(java.time.LocalTime) TimeType(org.apache.flink.table.types.logical.TimeType) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) DecimalData(org.apache.flink.table.data.DecimalData) IOException(java.io.IOException) ArrayType(org.apache.flink.table.types.logical.ArrayType) Serializable(java.io.Serializable) Date(java.sql.Date) Converter(org.apache.flink.formats.common.Converter) StringData(org.apache.flink.table.data.StringData) LogicalType(org.apache.flink.table.types.logical.LogicalType) DateTimeFormatter(java.time.format.DateTimeFormatter) Internal(org.apache.flink.annotation.Internal) GenericRowData(org.apache.flink.table.data.GenericRowData) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)

Example 19 with RowType

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

the class CsvRowDataSerDeSchemaTest method testDeserializationWithTypesMismatch.

@Test
public void testDeserializationWithTypesMismatch() {
    DataType dataType = ROW(FIELD("f0", STRING()), FIELD("f1", INT()), FIELD("f2", INT()));
    RowType rowType = (RowType) dataType.getLogicalType();
    CsvRowDataDeserializationSchema.Builder deserSchemaBuilder = new CsvRowDataDeserializationSchema.Builder(rowType, InternalTypeInfo.of(rowType));
    String data = "Test,1,Test";
    String errorMessage = "Fail to deserialize at field: f2.";
    try {
        deserialize(deserSchemaBuilder, data);
        fail("expecting exception message:" + errorMessage);
    } catch (Throwable t) {
        assertThat(t, FlinkMatchers.containsMessage(errorMessage));
    }
}
Also used : DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) StringData.fromString(org.apache.flink.table.data.StringData.fromString) Test(org.junit.Test)

Example 20 with RowType

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

the class CsvRowDataSerDeSchemaTest method testDeserialization.

@SuppressWarnings("unchecked")
private Row testDeserialization(boolean allowParsingErrors, boolean allowComments, String string) throws Exception {
    DataType dataType = ROW(FIELD("f0", STRING()), FIELD("f1", INT()), FIELD("f2", STRING()));
    RowType rowType = (RowType) dataType.getLogicalType();
    CsvRowDataDeserializationSchema.Builder deserSchemaBuilder = new CsvRowDataDeserializationSchema.Builder(rowType, InternalTypeInfo.of(rowType)).setIgnoreParseErrors(allowParsingErrors).setAllowComments(allowComments);
    RowData deserializedRow = deserialize(deserSchemaBuilder, string);
    return (Row) DataFormatConverters.getConverterForDataType(dataType).toExternal(deserializedRow);
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) Row(org.apache.flink.types.Row)

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