SchemaUtils { public static Schema mergeWideningNullable(Schema schema1, Schema schema2) { if (schema1.getFieldCount() != schema2.getFieldCount()) { throw new IllegalArgumentException( "Cannot merge schemas with different numbers of fields. " + "schema1: " + schema1 + " schema2: " + schema2); } Schema.Builder builder = Schema.builder(); for (int i = 0; i < schema1.getFieldCount(); ++i) { String name = schema1.getField(i).getName(); builder.addField( name, widenNullableTypes(schema1.getField(i).getType(), schema2.getField(i).getType())); } return builder.build(); }  static Schema mergeWideningNullable(Schema schema1, Schema schema2);  }
@Test public void testWidenPrimitives() { Schema schema1 = Schema.builder() .addField("field1", FieldType.INT32) .addNullableField("field2", FieldType.STRING) .build(); Schema schema2 = Schema.builder() .addNullableField("field3", FieldType.INT32) .addField("field4", FieldType.STRING) .build(); Schema expected = Schema.builder() .addNullableField("field1", FieldType.INT32) .addNullableField("field2", FieldType.STRING) .build(); assertEquals(expected, SchemaUtils.mergeWideningNullable(schema1, schema2)); }
@Test public void testWidenNested() { Schema schema1 = Schema.builder() .addField("field1", FieldType.INT32) .addNullableField("field2", FieldType.STRING) .build(); Schema schema2 = Schema.builder() .addNullableField("field3", FieldType.INT32) .addField("field4", FieldType.STRING) .build(); Schema top1 = Schema.builder().addField("top1", FieldType.row(schema1)).build(); Schema top2 = Schema.builder().addField("top2", FieldType.row(schema2)).build(); Schema expected = Schema.builder() .addNullableField("field1", FieldType.INT32) .addNullableField("field2", FieldType.STRING) .build(); Schema expectedTop = Schema.builder().addField("top1", FieldType.row(expected)).build(); assertEquals(expectedTop, SchemaUtils.mergeWideningNullable(top1, top2)); }
@Test public void testWidenArray() { Schema schema1 = Schema.builder().addArrayField("field1", FieldType.INT32).build(); Schema schema2 = Schema.builder().addArrayField("field1", FieldType.INT32.withNullable(true)).build(); Schema expected = Schema.builder().addArrayField("field1", FieldType.INT32.withNullable(true)).build(); assertEquals(expected, SchemaUtils.mergeWideningNullable(schema1, schema2)); }
@Test public void testWidenIterable() { Schema schema1 = Schema.builder().addIterableField("field1", FieldType.INT32).build(); Schema schema2 = Schema.builder().addIterableField("field1", FieldType.INT32.withNullable(true)).build(); Schema expected = Schema.builder().addIterableField("field1", FieldType.INT32.withNullable(true)).build(); assertEquals(expected, SchemaUtils.mergeWideningNullable(schema1, schema2)); }
@Test public void testWidenMap() { Schema schema1 = Schema.builder().addMapField("field1", FieldType.INT32, FieldType.INT32).build(); Schema schema2 = Schema.builder() .addMapField( "field1", FieldType.INT32.withNullable(true), FieldType.INT32.withNullable(true)) .build(); Schema expected = Schema.builder() .addMapField( "field1", FieldType.INT32.withNullable(true), FieldType.INT32.withNullable(true)) .build(); assertEquals(expected, SchemaUtils.mergeWideningNullable(schema1, schema2)); }