@Override public TypeInformation<Row> getProducedType() { return new RowTypeInfo(this.fieldTypeInfos); } }
@Override public TypeInformation<Row> getOutputType() { return new RowTypeInfo(fieldTypes); }
@Override public TypeInformation<Row> getOutputType() { return new RowTypeInfo(fieldTypes, fieldNames); }
/** * Returns type information for {@link org.apache.flink.types.Row} with fields of the given types and * with given names. A row must not be null. * * <p>A row is a fixed-length, null-aware composite type for storing multiple values in a * deterministic field order. Every field can be null independent of the field's type. * The type of row fields cannot be automatically inferred; therefore, it is required to provide * type information whenever a row is used. * * <p>The schema of rows can have up to <code>Integer.MAX_VALUE</code> fields, however, all row instances * must strictly adhere to the schema defined by the type info. * * <p>Example use: {@code ROW_NAMED(new String[]{"name", "number"}, Types.STRING, Types.INT)}. * * @param fieldNames array of field names * @param types array of field types */ public static TypeInformation<Row> ROW_NAMED(String[] fieldNames, TypeInformation<?>... types) { return new RowTypeInfo(types, fieldNames); }
@Override protected RowTypeInfo[] getTestData() { return new RowTypeInfo[] { new RowTypeInfo(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO), new RowTypeInfo(BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.BOOLEAN_TYPE_INFO), new RowTypeInfo(typeList), new RowTypeInfo( new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO}, new String[]{"int", "int2"}) }; }
@Override public TypeInformation<Row> getProducedType() { // split the fieldNames 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); }
@Override public TypeInformation<Row> getReturnType() { return new RowTypeInfo(getFieldTypes(), getFieldNames()); }
/** * Creates a {@link RowTypeInfo} with projected fields. * * @param rowType The original RowTypeInfo whose fields are projected * @param fieldMapping The field mapping of the projection * @return A RowTypeInfo with projected fields. */ public static RowTypeInfo projectFields(RowTypeInfo rowType, int[] fieldMapping) { TypeInformation[] fieldTypes = new TypeInformation[fieldMapping.length]; String[] fieldNames = new String[fieldMapping.length]; for (int i = 0; i < fieldMapping.length; i++) { fieldTypes[i] = rowType.getTypeAt(fieldMapping[i]); fieldNames[i] = rowType.getFieldNames()[fieldMapping[i]]; } return new RowTypeInfo(fieldTypes, fieldNames); } }
@Test(expected = IllegalArgumentException.class) public void testWrongNumberOfFieldNames() { new RowTypeInfo(typeList, new String[]{"int", "string"}); // number of field names should be equal to number of types, go fail }
@BeforeClass public static void setUp() throws Exception { TypeInformation<?>[] fieldTypes = new TypeInformation[numberOfFields]; for (int i = 0; i < numberOfFields; i++) { fieldTypes[i] = BasicTypeInfo.STRING_TYPE_INFO; } typeInfo = new RowTypeInfo(fieldTypes); }
@Test(expected = IllegalArgumentException.class) public void testDuplicateCustomFieldNames() { new RowTypeInfo(typeList, new String[]{"int", "string", "string"}); // field names should not be the same, go fail }
private TypeInformation[] getFieldTypes() { String[] famNames = hBaseSchema.getFamilyNames(); TypeInformation<?>[] fieldTypes = new TypeInformation[hBaseSchema.getFamilyNames().length]; int i = 0; for (String family : famNames) { fieldTypes[i] = new RowTypeInfo(hBaseSchema.getQualifierTypes(family), hBaseSchema.getQualifierNames(family)); i++; } return fieldTypes; }
private static TypeSerializer<Row> stringLongRowSupplier() { RowTypeInfo rowTypeInfo = new RowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO); return rowTypeInfo.createSerializer(new ExecutionConfig()); } }
@Test public void testSchemaEquals() { final RowTypeInfo row1 = new RowTypeInfo( new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO}, new String[] {"field1", "field2"}); final RowTypeInfo row2 = new RowTypeInfo( new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO}, new String[] {"field1", "field2"}); assertTrue(row1.schemaEquals(row2)); final RowTypeInfo other1 = new RowTypeInfo( new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO}, new String[] {"otherField", "field2"}); final RowTypeInfo other2 = new RowTypeInfo( new TypeInformation[]{BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO}, new String[] {"field1", "field2"}); assertFalse(row1.schemaEquals(other1)); assertFalse(row1.schemaEquals(other2)); }
@Test public void testGetFlatFields() { RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"}); List<FlatFieldDescriptor> result = new ArrayList<>(); typeInfo1.getFlatFields("row.*", 0, result); assertEquals(2, result.size()); assertEquals( new FlatFieldDescriptor(1, BasicTypeInfo.SHORT_TYPE_INFO).toString(), result.get(0).toString()); assertEquals( new FlatFieldDescriptor(2, BasicTypeInfo.BIG_DEC_TYPE_INFO).toString(), result.get(1).toString()); result.clear(); typeInfo1.getFlatFields("string", 0, result); assertEquals(1, result.size()); assertEquals( new FlatFieldDescriptor(3, BasicTypeInfo.STRING_TYPE_INFO).toString(), result.get(0).toString()); }
@Test public void testNestedRowTypeInfo() { RowTypeInfo typeInfo = new RowTypeInfo(typeList); assertEquals("Row(f0: Short, f1: BigDecimal)", typeInfo.getTypeAt("f1").toString()); assertEquals("Short", typeInfo.getTypeAt("f1.f0").toString()); }
@Test public void testCustomFieldNames() { String[] fieldNames = new String[]{"int", "row", "string"}; RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"}); assertArrayEquals(new String[]{"int", "row", "string"}, typeInfo1.getFieldNames()); assertEquals(BasicTypeInfo.STRING_TYPE_INFO, typeInfo1.getTypeAt("string")); assertEquals(BasicTypeInfo.STRING_TYPE_INFO, typeInfo1.getTypeAt(2)); assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, typeInfo1.getTypeAt("row.0")); assertEquals(BasicTypeInfo.BIG_DEC_TYPE_INFO, typeInfo1.getTypeAt("row.f1")); // change the names in fieldNames fieldNames[1] = "composite"; RowTypeInfo typeInfo2 = new RowTypeInfo(typeList, fieldNames); // make sure the field names are deep copied assertArrayEquals(new String[]{"int", "row", "string"}, typeInfo1.getFieldNames()); assertArrayEquals(new String[]{"int", "composite", "string"}, typeInfo2.getFieldNames()); }
@Test public void testGetTypeAt() { RowTypeInfo typeInfo = new RowTypeInfo(typeList); assertArrayEquals(new String[]{"f0", "f1", "f2"}, typeInfo.getFieldNames()); assertEquals(BasicTypeInfo.STRING_TYPE_INFO, typeInfo.getTypeAt("f2")); assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, typeInfo.getTypeAt("f1.f0")); assertEquals(BasicTypeInfo.BIG_DEC_TYPE_INFO, typeInfo.getTypeAt("f1.1")); }
@Test public void testRow() { Row row = new Row(2); row.setField(0, "string"); row.setField(1, 15); TypeInformation<Row> rowInfo = TypeExtractor.getForObject(row); Assert.assertEquals(rowInfo.getClass(), RowTypeInfo.class); Assert.assertEquals(2, rowInfo.getArity()); Assert.assertEquals( new RowTypeInfo( BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO), rowInfo); Row nullRow = new Row(2); TypeInformation<Row> genericRowInfo = TypeExtractor.getForObject(nullRow); Assert.assertEquals(genericRowInfo, new GenericTypeInfo<>(Row.class)); }
@Test public void testRowSerializer() { TypeInformation<Row> typeInfo = new RowTypeInfo( BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO); Row row1 = new Row(2); row1.setField(0, 1); row1.setField(1, "a"); Row row2 = new Row(2); row2.setField(0, 2); row2.setField(1, null); TypeSerializer<Row> serializer = typeInfo.createSerializer(new ExecutionConfig()); RowSerializerTestInstance instance = new RowSerializerTestInstance(serializer, row1, row2); instance.testAll(); }