AvroUtils { public static Schema typeFor(DataType dataType) { return typeFor(dataType, true); }  static Schema schemaFor(StructType structType); static Schema schemaFor(StructType structType, String record, String namespace); static Schema typeFor(DataType dataType); static Schema typeFor(DataType dataType, boolean isOptional); static StructType structTypeFor(Schema schema); static DataType dataTypeFor(Schema schemaType);  }
@Test public void toTypeSchemaIntNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.IntegerType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.INT), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaIntNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.IntegerType, false); assertEquals("Invalid type", Schema.Type.INT, schema.getType()); }
@Test public void toTypeSchemaLongNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.LongType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.LONG), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaLongNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.LongType, false); assertEquals("Invalid type", Schema.Type.LONG, schema.getType()); }
@Test public void toTypeSchemaNullNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.NullType); assertEquals("Invalid type", Schema.Type.NULL, schema.getType()); }
@Test public void toTypeSchemaNullNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.NullType, false); assertEquals("Invalid type", Schema.Type.NULL, schema.getType()); }
@Test public void toTypeSchemaDecimalNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createDecimalType()); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.BYTES), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.BYTES)) { assertEquals("Invalid LogicalType", LogicalTypes.decimal(10, 0), s.getLogicalType()); } } }
@Test public void toTypeSchemaDecimalNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createDecimalType(), false); assertEquals("Invalid type", Schema.Type.BYTES, schema.getType()); assertEquals("Invalid LogicalType", LogicalTypes.decimal(10, 0), schema.getLogicalType()); }
@Test public void toTypeSchemaArrayNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createArrayType(DataTypes.IntegerType, false)); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.ARRAY), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.ARRAY)) { assertEquals("Invalid element type", Schema.Type.INT, s.getElementType().getType()); } } }
@Test public void toTypeSchemaArrayNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createArrayType(DataTypes.IntegerType, false),false); assertEquals("Invalid type", Schema.Type.ARRAY, schema.getType()); assertEquals("Invalid element type", Schema.Type.INT, schema.getElementType().getType()); }
@Test public void toTypeSchemaArrayContainsNull() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createArrayType(DataTypes.IntegerType, true), false); assertEquals("Invalid type", Schema.Type.ARRAY, schema.getType()); assertEquals("Invalid element type", Schema.Type.UNION, schema.getElementType().getType()); assertEquals("Invalid union count", 2, schema.getElementType().getTypes().size()); for (Schema s : schema.getElementType().getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.INT), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaMapsNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createMapType(DataTypes.StringType, DataTypes.IntegerType, false)); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.MAP), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.MAP)) { assertEquals("Invalid value type", Schema.Type.INT, s.getValueType().getType()); } } }
@Test public void toTypeSchemaMapNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createMapType(DataTypes.StringType, DataTypes.IntegerType, false),false); assertEquals("Invalid type", Schema.Type.MAP, schema.getType()); assertEquals("Invalid value type", Schema.Type.INT, schema.getValueType().getType()); }
@Test public void toTypeSchemaMapContainsNull() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createMapType(DataTypes.StringType, DataTypes.IntegerType, true),false); assertEquals("Invalid type", Schema.Type.MAP, schema.getType()); assertEquals("Invalid value type", Schema.Type.UNION, schema.getValueType().getType()); assertEquals("Invalid union count", 2, schema.getValueType().getTypes().size()); for (Schema s : schema.getValueType().getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.INT), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaMapNonStringKey() { Schema schema = AvroUtils.typeFor(DataTypes.createMapType(DataTypes.FloatType, DataTypes.IntegerType, false),false); assertEquals("Invalid type", Schema.Type.MAP, schema.getType()); assertEquals("Invalid value type", Schema.Type.INT, schema.getValueType().getType()); }
@Test public void toTypeSchemaStructTypeNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createStructType( Lists.newArrayList( DataTypes.createStructField("field1", DataTypes.StringType, false) )), true); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.RECORD), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.RECORD)) { assertEquals("Invalid record name", "record0", s.getName()); assertEquals("Invalid field count", 1, s.getFields().size()); assertEquals("Invalid field name", "field1", s.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.STRING, s.getFields().get(0).schema().getType()); } } }
@Test public void toTypeSchemaStructTypeNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createStructType( Lists.newArrayList( DataTypes.createStructField("field1", DataTypes.StringType, false) )), false); assertEquals("Invalid type", Schema.Type.RECORD, schema.getType()); assertEquals("Invalid record name", "record0", schema.getName()); assertEquals("Invalid field count", 1, schema.getFields().size()); assertEquals("Invalid field name", "field1", schema.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.STRING, schema.getFields().get(0).schema().getType()); }
@Test public void toTypeSchemaStructTypeFieldNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.createStructType( Lists.newArrayList( DataTypes.createStructField("field1", DataTypes.StringType, true) )), false); assertEquals("Invalid type", Schema.Type.RECORD, schema.getType()); assertEquals("Invalid record name", "record0", schema.getName()); assertEquals("Invalid field count", 1, schema.getFields().size()); assertEquals("Invalid field name", "field1", schema.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.UNION, schema.getFields().get(0).schema().getType()); for (Schema s : schema.getFields().get(0).schema().getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.STRING), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaBooleanNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.BooleanType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.BOOLEAN), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaBooleanNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.BooleanType, false); assertEquals("Invalid type", Schema.Type.BOOLEAN, schema.getType()); }
@Test public void toTypeSchemaStringNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.StringType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.STRING), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaStringNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.StringType, false); assertEquals("Invalid type", Schema.Type.STRING, schema.getType()); }
@Test public void toTypeSchemaBinaryNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.BinaryType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.BYTES), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaBinaryNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.BinaryType, false); assertEquals("Invalid type", Schema.Type.BYTES, schema.getType()); }
@Test public void toTypeSchemaDateNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.DateType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.INT), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.INT)) { assertEquals("Invalid LogicalType", LogicalTypes.date(), s.getLogicalType()); } } }
@Test public void toTypeSchemaDateNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.DateType, false); assertEquals("Invalid type", Schema.Type.INT, schema.getType()); assertEquals("Invalid LogicalType", LogicalTypes.date(), schema.getLogicalType()); }
@Test public void toTypeSchemaTimestampNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.TimestampType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.LONG), is(Schema.Type.NULL))); if (s.getType().equals(Schema.Type.LONG)) { assertEquals("Invalid LogicalType", LogicalTypes.timestampMillis(), s.getLogicalType()); } } }
@Test public void toTypeSchemaTimestampNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.TimestampType, false); assertEquals("Invalid type", Schema.Type.LONG, schema.getType()); assertEquals("Invalid LogicalType", LogicalTypes.timestampMillis(), schema.getLogicalType()); }
@Test public void toTypeSchemaTypeDoubleNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.DoubleType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.DOUBLE), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaDoubleNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.DoubleType, false); assertEquals("Invalid type", Schema.Type.DOUBLE, schema.getType()); }
@Test public void toTypeSchemaFloatNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.FloatType); assertEquals("Invalid type", Schema.Type.UNION, schema.getType()); assertEquals("Invalid union size", 2, schema.getTypes().size()); for (Schema s : schema.getTypes()) { assertThat("Invalid union types", s.getType(), anyOf(is(Schema.Type.FLOAT), is(Schema.Type.NULL))); } }
@Test public void toTypeSchemaFloatNotNullable() throws Exception { Schema schema = AvroUtils.typeFor(DataTypes.FloatType, false); assertEquals("Invalid type", Schema.Type.FLOAT, schema.getType()); }