public Builder addStringField(String name) { fields.add(Field.of(name, FieldType.STRING)); return this; }
public Builder addDoubleField(String name) { fields.add(Field.of(name, FieldType.DOUBLE)); return this; }
public Builder addInt64Field(String name) { fields.add(Field.of(name, FieldType.INT64)); return this; }
@Test public void testConsistentWithEqualsArrayOfBytes() throws Exception { FieldType fieldType = FieldType.array(FieldType.BYTES); Schema schema = Schema.of(Schema.Field.of("f1", fieldType)); RowCoder coder = RowCoder.of(schema); List<byte[]> list1 = Collections.singletonList(new byte[] {1, 2, 3, 4}); Row row1 = Row.withSchema(schema).addValue(list1).build(); List<byte[]> list2 = Collections.singletonList(new byte[] {1, 2, 3, 4}); Row row2 = Row.withSchema(schema).addValue(list2).build(); Assume.assumeTrue(coder.consistentWithEquals()); CoderProperties.coderConsistentWithEquals(coder, row1, row2); }
@Test public void testCreatesArrayArray() { List<List<Integer>> data = Lists.<List<Integer>>newArrayList(Lists.newArrayList(1, 2, 3, 4)); Schema type = Stream.of(Schema.Field.of("array", FieldType.array(FieldType.array(FieldType.INT32)))) .collect(toSchema()); Row row = Row.withSchema(type).addArray(data).build(); assertEquals(data, row.getArray("array")); }
@Test public void testWrongIndex() { Schema schema = Schema.of(Field.of("f_byte", FieldType.BYTE)); thrown.expect(IndexOutOfBoundsException.class); schema.getField(1); }
@Test public void testArrayOfRowSchema() { Schema nestedSchema = Schema.of(Field.of("f1_str", FieldType.STRING)); FieldType arrayType = FieldType.array(FieldType.row(nestedSchema)); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }
@Test public void testNestedSchema() { Schema nestedSchema = Schema.of(Field.of("f1_str", FieldType.STRING)); Schema schema = Schema.of(Field.of("nested", FieldType.row(nestedSchema))); Field inner = schema.getField("nested").getType().getRowSchema().getField("f1_str"); assertEquals("f1_str", inner.getName()); assertEquals(FieldType.STRING, inner.getType()); }
/** Aggregate all values of a set of fields into an output field. */ <CombineInputT, AccumT, CombineOutputT> Inner<T> aggregateFields( FieldAccessDescriptor fieldsToAggregate, CombineFn<CombineInputT, AccumT, CombineOutputT> fn, String outputFieldName) { return aggregateFields( fieldsToAggregate, fn, Field.of(outputFieldName, FieldTypeDescriptors.fieldTypeForJavaType(fn.getOutputType()))); }
@Test(expected = IllegalArgumentException.class) public void testIgnoreNullFail() { Schema inputSchema = Schema.of(Schema.Field.nullable("f0", Schema.FieldType.INT32)); Schema outputSchema = Schema.of(Schema.Field.of("f0", Schema.FieldType.INT32)); Cast.castRow(Row.withSchema(inputSchema).addValue(null).build(), inputSchema, outputSchema); }
@Test public void testNarrowingNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Narrowing.of().apply(input, output); assertThat(errors, empty()); } }
public Builder addByteArrayField(String name) { fields.add(Field.of(name, FieldType.BYTES)); return this; }
private Schema generateSchema( FieldTypeGenerator fieldTypeGenerator, SourceOfRandomness random, GenerationStatus status) { return IntStream.range(0, status.size() + 1) .mapToObj( i -> Field.of("field_" + i, fieldTypeGenerator.generate(random, status)) .withNullable(true)) .collect(toSchema()); } }
@Test public void testCreatesArrayArrayWithNullElement() { List<List<Integer>> data = Lists.<List<Integer>>newArrayList(Lists.newArrayList(1, null, 3, null), null); Schema type = Stream.of( Schema.Field.of( "array", FieldType.array(FieldType.array(FieldType.INT32, true), true))) .collect(toSchema()); Row row = Row.withSchema(type).addArray(data).build(); assertEquals(data, row.getArray("array")); }
@Test public void testWideningNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Widening.of().apply(input, output); Cast.CompatibilityError expected = Cast.CompatibilityError.create( Arrays.asList("f0"), "Can't cast nullable field to non-nullable field"); assertThat(errors, containsInAnyOrder(expected)); }
@Test public void testThrowsForIncorrectNumberOfFields() { Schema type = Stream.of( Schema.Field.of("f_int", FieldType.INT32), Schema.Field.of("f_str", FieldType.STRING), Schema.Field.of("f_double", FieldType.DOUBLE)) .collect(toSchema()); thrown.expect(IllegalArgumentException.class); Row.withSchema(type).addValues(1, "2").build(); }
@Test public void testRejectsNullRecord() { Schema type = Stream.of(Schema.Field.of("f_int", Schema.FieldType.INT32)).collect(toSchema()); thrown.expect(IllegalArgumentException.class); Row.nullRow(type); }
@Test public void testWrongName() { Schema schema = Schema.of(Field.of("f_byte", FieldType.BYTE)); thrown.expect(IllegalArgumentException.class); schema.getField("f_string"); }
@Test public void testNestedArraySchema() { FieldType arrayType = FieldType.array(FieldType.array(FieldType.STRING)); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }
@Test public void testArraySchema() { FieldType arrayType = FieldType.array(FieldType.STRING); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }