private Schema getUuidType() { if (uuidTSchema == null) { uuidTSchema = Schema.createFixed(UUID_TYPE, null, KAA_NAMESPACE, UUID_SIZE); } return uuidTSchema; }
/** Constructor. */ public ByteWritableConverter() { mSchema = Schema.createFixed("Byte", "A single byte", "org.apache.avro.mapreduce", 1); }
public static GenericData.Fixed generateUuidObject() { Schema avroSchema = Schema.createFixed(UUID_TYPE, null, KAA_NAMESPACE, UUID_SIZE); return new GenericData.Fixed(avroSchema, generateUuidBytes()); }
final public Schema FixedDeclaration() throws ParseException { String name; Token sizeTok; jj_consume_token(FIXED); name = Identifier(); jj_consume_token(LPAREN); sizeTok = jj_consume_token(INTEGER_LITERAL); jj_consume_token(RPAREN); jj_consume_token(SEMICOLON); Schema s = Schema.createFixed(name, getDoc(), this.namespace, Integer.parseInt(sizeTok.image)); names.put(s.getFullName(), s); {if (true) return s;} throw new Error("Missing return statement in function"); }
@Test public void testFixedDecimalToFromJson() { Schema schema = Schema.createFixed("aDecimal", null, null, 4); LogicalTypes.decimal(9, 2).addToSchema(schema); Schema parsed = new Schema.Parser().parse(schema.toString(true)); Assert.assertEquals("Constructed and parsed schemas should match", schema, parsed); }
/** Configure this fixed type's size, and end its configuration. **/ public R size(int size) { Schema schema = Schema.createFixed(name(), super.doc(), space(), size); completeSchema(schema); return context().complete(schema); } }
/** Configure this fixed type's size, and end its configuration. **/ public R size(int size) { Schema schema = Schema.createFixed(name(), super.doc(), space(), size); completeSchema(schema); return context().complete(schema); } }
@Test public void testIsUnionOnFixed() { Schema schema = Schema.createFixed("name", "doc", "space", 10); assertFalse(schema.isUnion()); }
@Test public void testFixed() throws java.io.IOException { new ResolvingGrammarGenerator().generate (Schema.createFixed("MyFixed", null, null, 10), Schema.create(Schema.Type.BYTES)); new ResolvingGrammarGenerator().generate (Schema.create(Schema.Type.BYTES), Schema.createFixed("MyFixed", null, null, 10)); }
@Test public void testDecimalFailsWithNegativeScale() { final Schema schema = Schema.createFixed("aDecimal", null, null, 4); assertThrows("Should reject precision", IllegalArgumentException.class, "Invalid decimal scale: -2 (must be positive)", new Callable() { @Override public Object call() throws Exception { LogicalTypes.decimal(9, -2).addToSchema(schema); return null; } }); Assert.assertNull("Invalid logical type should not be set on schema", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test public void testDecimalFailsWithNegativePrecision() { final Schema schema = Schema.createFixed("aDecimal", null, null, 4); assertThrows("Should reject precision", IllegalArgumentException.class, "Invalid decimal precision: -9 (must be positive)", new Callable() { @Override public Object call() throws Exception { LogicalTypes.decimal(-9).addToSchema(schema); return null; } }); Assert.assertNull("Invalid logical type should not be set on schema", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test public void testDecimalFailsWithZeroPrecision() { final Schema schema = Schema.createFixed("aDecimal", null, null, 4); assertThrows("Should reject precision", IllegalArgumentException.class, "Invalid decimal precision: 0 (must be positive)", new Callable() { @Override public Object call() throws Exception { LogicalTypes.decimal(0).addToSchema(schema); return null; } }); Assert.assertNull("Invalid logical type should not be set on schema", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test public void testNestedNonNullNamespace2() { Schema inner1 = Schema.createFixed("InnerFixed", null, "space", 1); Schema inner2 = Schema.parse("{\"type\":\"record\",\"namespace\":\"space\",\"name\":" +"\"InnerRecord\",\"fields\":[]}"); Schema nullOuter = Schema.createRecord("Outer", null, null, false); nullOuter.setFields(Arrays.asList(new Field("f1", inner1, null, null), new Field("f2", inner2, null, null))); assertEquals(nullOuter, Schema.parse(nullOuter.toString())); }
@Test public void testDecimalScaleBoundedByPrecision() { final Schema schema = Schema.createFixed("aDecimal", null, null, 4); assertThrows("Should reject precision", IllegalArgumentException.class, "Invalid decimal scale: 10 (greater than precision: 9)", new Callable() { @Override public Object call() throws Exception { LogicalTypes.decimal(9, 10).addToSchema(schema); return null; } }); Assert.assertNull("Invalid logical type should not be set on schema", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test public void testToStringFixed() throws Exception { GenericData data = GenericData.get(); assertEquals("[97, 10, 98]", data.toString(new GenericData.Fixed( Schema.createFixed("test", null, null, 3), new byte[] {'a', '\n', 'b'}))); }
@Test public void testInvalidLogicalTypeIgnored() { final Schema schema = Schema.createFixed("aFixed", null, null, 2); schema.addProp("logicalType", "decimal"); schema.addProp("precision", 9); schema.addProp("scale", 2); Assert.assertNull("Should ignore invalid logical type", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test public void testDecimalDefaultScale() { Schema schema = Schema.createFixed("aDecimal", null, null, 4); // 4 bytes can hold up to 9 digits of precision LogicalTypes.decimal(9).addToSchema(schema); Assert.assertEquals("Scale should be a 0", 0, ((LogicalTypes.Decimal) LogicalTypes.fromSchemaIgnoreInvalid(schema)).getScale()); }
@Test public void testFixed() { Schema expected = Schema.createFixed("myfixed", null, null, 16); expected.addAlias("myOldFixed"); Schema schema = SchemaBuilder.fixed("myfixed") .aliases("myOldFixed").size(16); Assert.assertEquals(expected, schema); }
@Test public void testArrayOfFixed() { Schema fixedSchema = Schema.createFixed("someFixed", "", "", 2); GenericArray avroArray = CommonToGeneric.createArray(createCommonArray( fixedSchema, new DefaultCommonFixed(fixedSchema, new byte[]{1, 2}))); GenericFixed avroFixed = (GenericFixed) avroArray.get(0); assertArrayEquals(new byte[]{1, 2}, avroFixed.bytes()); }
@Test public void testDecimalFromSchema() { Schema schema = Schema.createFixed("aFixed", null, null, 4); schema.addProp("logicalType", "decimal"); schema.addProp("precision", 9); schema.addProp("scale", 2); LogicalType logicalType = LogicalTypes.fromSchemaIgnoreInvalid(schema); Assert.assertTrue("Should be a Decimal", logicalType instanceof LogicalTypes.Decimal); LogicalTypes.Decimal decimal = (LogicalTypes.Decimal) logicalType; Assert.assertEquals("Should have correct precision", 9, decimal.getPrecision()); Assert.assertEquals("Should have correct scale", 2, decimal.getScale()); }