Search in sources :

Example 21 with RowTypeInfo

use of org.apache.flink.api.java.typeutils.RowTypeInfo in project flink by apache.

the class DataFormatConvertersTest method testTypes.

@Test
public void testTypes() {
    for (int i = 0; i < simpleTypes.length; i++) {
        test(simpleTypes[i], simpleValues[i]);
    }
    test(new RowTypeInfo(simpleTypes), new Row(simpleTypes.length));
    test(new RowTypeInfo(simpleTypes), Row.ofKind(RowKind.DELETE, simpleValues));
    test(InternalTypeInfo.ofFields(VarCharType.STRING_TYPE, new IntType()), GenericRowData.of(StringData.fromString("hehe"), 111));
    test(InternalTypeInfo.ofFields(VarCharType.STRING_TYPE, new IntType()), GenericRowData.of(null, null));
    test(new DecimalDataTypeInfo(10, 5), null);
    test(new DecimalDataTypeInfo(10, 5), DecimalDataUtils.castFrom(5.555, 10, 5));
    test(Types.BIG_DEC, null);
    {
        DataFormatConverter converter = getConverter(Types.BIG_DEC);
        Assert.assertTrue(Arrays.deepEquals(new Object[] { converter.toInternal(converter.toExternal(DecimalDataUtils.castFrom(5, 19, 18))) }, new Object[] { DecimalDataUtils.castFrom(5, 19, 18) }));
    }
    test(new ListTypeInfo<>(Types.STRING), null);
    test(new ListTypeInfo<>(Types.STRING), Arrays.asList("ahah", "xx"));
    test(BasicArrayTypeInfo.DOUBLE_ARRAY_TYPE_INFO, new Double[] { 1D, 5D });
    test(BasicArrayTypeInfo.DOUBLE_ARRAY_TYPE_INFO, new Double[] { null, null });
    test(ObjectArrayTypeInfo.getInfoFor(Types.STRING), new String[] { null, null });
    test(ObjectArrayTypeInfo.getInfoFor(Types.STRING), new String[] { "haha", "hehe" });
    test(ObjectArrayTypeInfo.getInfoFor(Types.STRING), new String[] { "haha", "hehe" }, new String[] { "aa", "bb" });
    test(new MapTypeInfo<>(Types.STRING, Types.INT), null);
    HashMap<String, Integer> map = new HashMap<>();
    map.put("haha", 1);
    map.put("hah1", 5);
    map.put(null, null);
    test(new MapTypeInfo<>(Types.STRING, Types.INT), map);
    Tuple2 tuple2 = new Tuple2<>(5, 10);
    TupleTypeInfo tupleTypeInfo = new TupleTypeInfo<>(tuple2.getClass(), Types.INT, Types.INT);
    test(tupleTypeInfo, tuple2);
    test(TypeExtractor.createTypeInfo(MyPojo.class), new MyPojo(1, 3));
}
Also used : HashMap(java.util.HashMap) DecimalDataTypeInfo(org.apache.flink.table.runtime.typeutils.DecimalDataTypeInfo) DataFormatConverter(org.apache.flink.table.data.util.DataFormatConverters.DataFormatConverter) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) IntType(org.apache.flink.table.types.logical.IntType) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 22 with RowTypeInfo

use of org.apache.flink.api.java.typeutils.RowTypeInfo in project flink by apache.

the class KafkaTableSinkTestBase method testConfiguration.

@Test
public void testConfiguration() {
    KafkaTableSink kafkaTableSink = createTableSink();
    KafkaTableSink newKafkaTableSink = kafkaTableSink.configure(FIELD_NAMES, FIELD_TYPES);
    assertNotSame(kafkaTableSink, newKafkaTableSink);
    assertArrayEquals(FIELD_NAMES, newKafkaTableSink.getFieldNames());
    assertArrayEquals(FIELD_TYPES, newKafkaTableSink.getFieldTypes());
    assertEquals(new RowTypeInfo(FIELD_TYPES), newKafkaTableSink.getOutputType());
}
Also used : RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) Test(org.junit.Test)

Example 23 with RowTypeInfo

use of org.apache.flink.api.java.typeutils.RowTypeInfo in project flink by apache.

the class HBaseTableSource method getReturnType.

@Override
public TypeInformation<Row> getReturnType() {
    String[] famNames = schema.getFamilyNames();
    TypeInformation<?>[] typeInfos = new TypeInformation[famNames.length];
    int i = 0;
    for (String family : famNames) {
        typeInfos[i] = new RowTypeInfo(schema.getQualifierTypes(family), schema.getQualifierNames(family));
        i++;
    }
    return new RowTypeInfo(typeInfos, famNames);
}
Also used : RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 24 with RowTypeInfo

use of org.apache.flink.api.java.typeutils.RowTypeInfo in project flink by apache.

the class PythonTableUtils method converter.

private static Function<Object, Object> converter(final TypeInformation<?> dataType, final ExecutionConfig config) {
    if (dataType.equals(Types.BOOLEAN())) {
        return b -> b instanceof Boolean ? b : null;
    }
    if (dataType.equals(Types.BYTE())) {
        return c -> {
            if (c instanceof Byte) {
                return c;
            }
            if (c instanceof Short) {
                return ((Short) c).byteValue();
            }
            if (c instanceof Integer) {
                return ((Integer) c).byteValue();
            }
            if (c instanceof Long) {
                return ((Long) c).byteValue();
            }
            return null;
        };
    }
    if (dataType.equals(Types.SHORT())) {
        return c -> {
            if (c instanceof Byte) {
                return ((Byte) c).shortValue();
            }
            if (c instanceof Short) {
                return c;
            }
            if (c instanceof Integer) {
                return ((Integer) c).shortValue();
            }
            if (c instanceof Long) {
                return ((Long) c).shortValue();
            }
            return null;
        };
    }
    if (dataType.equals(Types.INT())) {
        return c -> {
            if (c instanceof Byte) {
                return ((Byte) c).intValue();
            }
            if (c instanceof Short) {
                return ((Short) c).intValue();
            }
            if (c instanceof Integer) {
                return c;
            }
            if (c instanceof Long) {
                return ((Long) c).intValue();
            }
            return null;
        };
    }
    if (dataType.equals(Types.LONG())) {
        return c -> {
            if (c instanceof Byte) {
                return ((Byte) c).longValue();
            }
            if (c instanceof Short) {
                return ((Short) c).longValue();
            }
            if (c instanceof Integer) {
                return ((Integer) c).longValue();
            }
            if (c instanceof Long) {
                return c;
            }
            return null;
        };
    }
    if (dataType.equals(Types.FLOAT())) {
        return c -> {
            if (c instanceof Float) {
                return c;
            }
            if (c instanceof Double) {
                return ((Double) c).floatValue();
            }
            return null;
        };
    }
    if (dataType.equals(Types.DOUBLE())) {
        return c -> {
            if (c instanceof Float) {
                return ((Float) c).doubleValue();
            }
            if (c instanceof Double) {
                return c;
            }
            return null;
        };
    }
    if (dataType.equals(Types.DECIMAL())) {
        return c -> c instanceof BigDecimal ? c : null;
    }
    if (dataType.equals(Types.SQL_DATE())) {
        return c -> {
            if (c instanceof Integer) {
                long millisLocal = ((Integer) c).longValue() * 86400000;
                long millisUtc = millisLocal - PythonTableUtils.getOffsetFromLocalMillis(millisLocal);
                return new Date(millisUtc);
            }
            return null;
        };
    }
    if (dataType.equals(Types.SQL_TIME())) {
        return c -> c instanceof Integer || c instanceof Long ? new Time(((Number) c).longValue() / 1000) : null;
    }
    if (dataType.equals(Types.SQL_TIMESTAMP())) {
        return c -> c instanceof Integer || c instanceof Long ? new Timestamp(((Number) c).longValue() / 1000) : null;
    }
    if (dataType.equals(org.apache.flink.api.common.typeinfo.Types.INSTANT)) {
        return c -> c instanceof Integer || c instanceof Long ? Instant.ofEpochMilli(((Number) c).longValue() / 1000) : null;
    }
    if (dataType.equals(Types.INTERVAL_MILLIS())) {
        return c -> c instanceof Integer || c instanceof Long ? ((Number) c).longValue() / 1000 : null;
    }
    if (dataType.equals(Types.STRING())) {
        return c -> c != null ? c.toString() : null;
    }
    if (dataType.equals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO)) {
        return c -> {
            if (c instanceof String) {
                return ((String) c).getBytes(StandardCharsets.UTF_8);
            }
            if (c instanceof byte[]) {
                return c;
            }
            return null;
        };
    }
    if (dataType instanceof PrimitiveArrayTypeInfo || dataType instanceof BasicArrayTypeInfo || dataType instanceof ObjectArrayTypeInfo) {
        TypeInformation<?> elementType = dataType instanceof PrimitiveArrayTypeInfo ? ((PrimitiveArrayTypeInfo<?>) dataType).getComponentType() : dataType instanceof BasicArrayTypeInfo ? ((BasicArrayTypeInfo<?, ?>) dataType).getComponentInfo() : ((ObjectArrayTypeInfo<?, ?>) dataType).getComponentInfo();
        boolean primitive = dataType instanceof PrimitiveArrayTypeInfo;
        Function<Object, Object> elementConverter = converter(elementType, config);
        BiFunction<Integer, Function<Integer, Object>, Object> arrayConstructor = arrayConstructor(elementType, primitive);
        return c -> {
            int length = -1;
            Function<Integer, Object> elementGetter = null;
            if (c instanceof List) {
                length = ((List<?>) c).size();
                elementGetter = i -> elementConverter.apply(((List<?>) c).get(i));
            }
            if (c != null && c.getClass().isArray()) {
                length = Array.getLength(c);
                elementGetter = i -> elementConverter.apply(Array.get(c, i));
            }
            if (elementGetter != null) {
                return arrayConstructor.apply(length, elementGetter);
            }
            return null;
        };
    }
    if (dataType instanceof MapTypeInfo) {
        Function<Object, Object> keyConverter = converter(((MapTypeInfo<?, ?>) dataType).getKeyTypeInfo(), config);
        Function<Object, Object> valueConverter = converter(((MapTypeInfo<?, ?>) dataType).getValueTypeInfo(), config);
        return c -> c instanceof Map ? ((Map<?, ?>) c).entrySet().stream().collect(Collectors.toMap(e -> keyConverter.apply(e.getKey()), e -> valueConverter.apply(e.getValue()))) : null;
    }
    if (dataType instanceof RowTypeInfo) {
        TypeInformation<?>[] fieldTypes = ((RowTypeInfo) dataType).getFieldTypes();
        List<Function<Object, Object>> fieldConverters = Arrays.stream(fieldTypes).map(x -> PythonTableUtils.converter(x, config)).collect(Collectors.toList());
        return c -> {
            if (c != null && c.getClass().isArray()) {
                int length = Array.getLength(c);
                if (length - 1 != fieldTypes.length) {
                    throw new IllegalStateException("Input row doesn't have expected number of values required by the schema. " + fieldTypes.length + " fields are required while " + (length - 1) + " values are provided.");
                }
                Row row = new Row(length - 1);
                row.setKind(RowKind.fromByteValue(((Number) Array.get(c, 0)).byteValue()));
                for (int i = 0; i < row.getArity(); i++) {
                    row.setField(i, fieldConverters.get(i).apply(Array.get(c, i + 1)));
                }
                return row;
            }
            return null;
        };
    }
    if (dataType instanceof TupleTypeInfo) {
        TypeInformation<?>[] fieldTypes = ((TupleTypeInfo<?>) dataType).getFieldTypes();
        List<Function<Object, Object>> fieldConverters = Arrays.stream(fieldTypes).map(x -> PythonTableUtils.converter(x, config)).collect(Collectors.toList());
        return c -> {
            if (c != null && c.getClass().isArray()) {
                int length = Array.getLength(c);
                if (length != fieldTypes.length) {
                    throw new IllegalStateException("Input tuple doesn't have expected number of values required by the schema. " + fieldTypes.length + " fields are required while " + length + " values are provided.");
                }
                Tuple tuple = Tuple.newInstance(length);
                for (int i = 0; i < tuple.getArity(); i++) {
                    tuple.setField(fieldConverters.get(i).apply(Array.get(c, i)), i);
                }
                return tuple;
            }
            return null;
        };
    }
    return c -> {
        if (c.getClass() != byte[].class || dataType instanceof PickledByteArrayTypeInfo) {
            return c;
        }
        // other typeinfos will use the corresponding serializer to deserialize data.
        byte[] b = (byte[]) c;
        TypeSerializer<?> dataSerializer = dataType.createSerializer(config);
        ByteArrayInputStreamWithPos bais = new ByteArrayInputStreamWithPos();
        DataInputViewStreamWrapper baisWrapper = new DataInputViewStreamWrapper(bais);
        bais.setBuffer(b, 0, b.length);
        try {
            return dataSerializer.deserialize(baisWrapper);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to deserialize the object with datatype " + dataType, e);
        }
    };
}
Also used : CollectionInputFormat(org.apache.flink.api.java.io.CollectionInputFormat) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) Time(java.sql.Time) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) BiFunction(java.util.function.BiFunction) LocalDateTime(java.time.LocalDateTime) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) Types(org.apache.flink.table.api.Types) ObjectArrayTypeInfo(org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) Function(java.util.function.Function) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) BigDecimal(java.math.BigDecimal) Map(java.util.Map) LocalTime(java.time.LocalTime) MapTypeInfo(org.apache.flink.api.java.typeutils.MapTypeInfo) InputFormat(org.apache.flink.api.common.io.InputFormat) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Tuple(org.apache.flink.api.java.tuple.Tuple) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) PickledByteArrayTypeInfo(org.apache.flink.streaming.api.typeinfo.python.PickledByteArrayTypeInfo) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Date(java.sql.Date) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) List(java.util.List) RowKind(org.apache.flink.types.RowKind) LocalDate(java.time.LocalDate) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Internal(org.apache.flink.annotation.Internal) Row(org.apache.flink.types.Row) PickledByteArrayTypeInfo(org.apache.flink.streaming.api.typeinfo.python.PickledByteArrayTypeInfo) Time(java.sql.Time) LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) Timestamp(java.sql.Timestamp) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) List(java.util.List) IOException(java.io.IOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) LocalDate(java.time.LocalDate) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) ObjectArrayTypeInfo(org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo) MapTypeInfo(org.apache.flink.api.java.typeutils.MapTypeInfo) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) Row(org.apache.flink.types.Row) Map(java.util.Map) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) Tuple(org.apache.flink.api.java.tuple.Tuple)

Example 25 with RowTypeInfo

use of org.apache.flink.api.java.typeutils.RowTypeInfo in project flink by apache.

the class MinByOperatorTest method testMinByRowTypeInfoKeyFieldsForUnsortedGrouping.

/**
 * Validates that no ClassCastException happens should not fail e.g. like in FLINK-8255.
 */
@Test(expected = InvalidProgramException.class)
public void testMinByRowTypeInfoKeyFieldsForUnsortedGrouping() {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    TypeInformation[] types = new TypeInformation[] { Types.INT, Types.INT };
    String[] fieldNames = new String[] { "id", "value" };
    RowTypeInfo rowTypeInfo = new RowTypeInfo(types, fieldNames);
    UnsortedGrouping groupDs = env.fromCollection(Collections.singleton(new Row(2)), rowTypeInfo).groupBy(0);
    groupDs.minBy(1);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) UnsortedGrouping(org.apache.flink.api.java.operators.UnsortedGrouping) Row(org.apache.flink.types.Row) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Aggregations

RowTypeInfo (org.apache.flink.api.java.typeutils.RowTypeInfo)50 Test (org.junit.Test)34 Row (org.apache.flink.types.Row)32 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)26 Configuration (org.apache.flink.configuration.Configuration)16 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)15 ArrayList (java.util.ArrayList)10 Transformation (org.apache.flink.api.dag.Transformation)8 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)8 SourceTransformation (org.apache.flink.streaming.api.transformations.SourceTransformation)8 TwoInputTransformation (org.apache.flink.streaming.api.transformations.TwoInputTransformation)8 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)6 PythonKeyedProcessOperator (org.apache.flink.streaming.api.operators.python.PythonKeyedProcessOperator)6 IOException (java.io.IOException)4 MapTypeInfo (org.apache.flink.api.java.typeutils.MapTypeInfo)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 LocalDateTime (java.time.LocalDateTime)3 PrimitiveArrayTypeInfo (org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo)3