Search in sources :

Example 81 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class RowDataSerializerTest method testRowDataSerializerWithNestedRow.

private static Object[] testRowDataSerializerWithNestedRow() {
    final DataType nestedDataType = DataTypes.ROW(DataTypes.FIELD("ri", DataTypes.INT()), DataTypes.FIELD("rs", DataTypes.STRING()), DataTypes.FIELD("rb", DataTypes.BIGINT()));
    final DataType outerDataType = DataTypes.ROW(DataTypes.FIELD("i", DataTypes.INT()), DataTypes.FIELD("r", nestedDataType), DataTypes.FIELD("s", DataTypes.STRING()));
    final TypeSerializer<RowData> nestedSerializer = InternalSerializers.create(nestedDataType.getLogicalType());
    final RowDataSerializer outerSerializer = (RowDataSerializer) InternalSerializers.<RowData>create(outerDataType.getLogicalType());
    final GenericRowData outerRow1 = GenericRowData.of(12, GenericRowData.of(34, StringData.fromString("56"), 78L), StringData.fromString("910"));
    final RowData nestedRow1 = outerSerializer.toBinaryRow(outerRow1).getRow(1, 3);
    final GenericRowData outerRow2 = GenericRowData.of(12, GenericRowData.of(null, StringData.fromString("56"), 78L), null);
    final RowData nestedRow2 = outerSerializer.toBinaryRow(outerRow2).getRow(1, 3);
    return new Object[] { nestedSerializer, new RowData[] { nestedRow1, nestedRow2 } };
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) DataType(org.apache.flink.table.types.DataType) GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 82 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class RowDataSerializerTest method testRowDataSerializerWithComplexTypes.

private static Object[] testRowDataSerializerWithComplexTypes() {
    InternalTypeInfo<RowData> typeInfo = InternalTypeInfo.ofFields(new IntType(), new DoubleType(), VarCharType.STRING_TYPE, new ArrayType(new IntType()), new MapType(new IntType(), new IntType()));
    GenericRowData[] data = new GenericRowData[] { createRow(null, null, null, null, null), createRow(0, null, null, null, null), createRow(0, 0.0, null, null, null), createRow(0, 0.0, fromString("a"), null, null), createRow(1, 0.0, fromString("a"), null, null), createRow(1, 1.0, fromString("a"), null, null), createRow(1, 1.0, fromString("b"), null, null), createRow(1, 1.0, fromString("b"), createArray(1), createMap(new int[] { 1 }, new int[] { 1 })), createRow(1, 1.0, fromString("b"), createArray(1, 2), createMap(new int[] { 1, 4 }, new int[] { 1, 2 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3), createMap(new int[] { 1, 5 }, new int[] { 1, 3 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4), createMap(new int[] { 1, 6 }, new int[] { 1, 4 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4, 5), createMap(new int[] { 1, 7 }, new int[] { 1, 5 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4, 5, 6), createMap(new int[] { 1, 8 }, new int[] { 1, 6 })) };
    RowDataSerializer serializer = typeInfo.toRowSerializer();
    return new Object[] { serializer, data };
}
Also used : ArrayType(org.apache.flink.table.types.logical.ArrayType) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) DoubleType(org.apache.flink.table.types.logical.DoubleType) GenericRowData(org.apache.flink.table.data.GenericRowData) MapType(org.apache.flink.table.types.logical.MapType) IntType(org.apache.flink.table.types.logical.IntType)

Example 83 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class RowDataSerializerTest method createRow.

private static GenericRowData createRow(Object f0, Object f1, Object f2, Object f3, Object f4) {
    GenericRowData row = new GenericRowData(5);
    row.setField(0, f0);
    row.setField(1, f1);
    row.setField(2, f2);
    row.setField(3, f3);
    row.setField(4, f4);
    return row;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 84 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class RowDataTestUtil method toGenericRowDeeply.

public static GenericRowData toGenericRowDeeply(RowData rowData, List<LogicalType> types) {
    if (rowData instanceof GenericRowData) {
        return (GenericRowData) rowData;
    } else {
        int fieldNum = rowData.getArity();
        GenericRowData row = new GenericRowData(fieldNum);
        row.setRowKind(rowData.getRowKind());
        for (int i = 0; i < fieldNum; i++) {
            if (rowData.isNullAt(i)) {
                row.setField(i, null);
            } else {
                LogicalType type = types.get(i);
                RowData.FieldGetter fieldGetter = RowData.createFieldGetter(type, i);
                Object o = fieldGetter.getFieldOrNull(rowData);
                if (type instanceof RowType) {
                    o = toGenericRowDeeply((RowData) o, type.getChildren());
                }
                row.setField(i, o);
            }
        }
        return row;
    }
}
Also used : RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) GenericRowData(org.apache.flink.table.data.GenericRowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowType(org.apache.flink.table.types.logical.RowType)

Example 85 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class TableAssertionTest method testAssertRowDataWithConversion.

@Test
void testAssertRowDataWithConversion() {
    DataType dataType = ROW(FIELD("a", INT()), FIELD("b", STRING()), FIELD("c", ARRAY(BOOLEAN().notNull())));
    GenericRowData genericRowData = GenericRowData.of(10, StringData.fromString("my string"), new GenericArrayData(new boolean[] { true, false }));
    BinaryRowData binaryRowData = new RowDataSerializer((RowType) dataType.getLogicalType()).toBinaryRow(genericRowData);
    Row row = Row.of(10, "my string", new Boolean[] { true, false });
    // Test equality with RowData
    assertThat(binaryRowData).asGeneric(dataType).isEqualTo(genericRowData).isEqualTo(binaryRowData.copy());
    assertThatRows(binaryRowData).asGeneric(dataType).containsOnly(genericRowData).containsOnly(binaryRowData);
    // Test equality with Row
    assertThat(binaryRowData).asRow(dataType).isEqualTo(row);
    assertThatRows(binaryRowData).asRows(dataType).containsOnly(row);
}
Also used : GenericArrayData(org.apache.flink.table.data.GenericArrayData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) DataType(org.apache.flink.table.types.DataType) GenericRowData(org.apache.flink.table.data.GenericRowData) RowType(org.apache.flink.table.types.logical.RowType) Row(org.apache.flink.types.Row) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer) Test(org.junit.jupiter.api.Test)

Aggregations

GenericRowData (org.apache.flink.table.data.GenericRowData)94 RowData (org.apache.flink.table.data.RowData)32 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)16 Test (org.junit.Test)14 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)13 RowType (org.apache.flink.table.types.logical.RowType)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IntType (org.apache.flink.table.types.logical.IntType)11 List (java.util.List)9 LogicalType (org.apache.flink.table.types.logical.LogicalType)9 GenericArrayData (org.apache.flink.table.data.GenericArrayData)6 StringData (org.apache.flink.table.data.StringData)6 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 OutputStream (java.io.OutputStream)4 PrintStream (java.io.PrintStream)4 Collections (java.util.Collections)4 Random (java.util.Random)4 Consumer (java.util.function.Consumer)4