public static Object applyLogicalType(Schema.Field field, Object value) {
    if (field == null || field.schema() == null) {
      return value;
    }

    Schema fieldSchema = resolveUnionSchema(field.schema());
    return applySchemaTypeLogic(fieldSchema, value);
  }
@Test
  public void testLogicalTypesWithUnionSchema() {
    String valString1 = "125.24350000";
    ByteBuffer amount1 = decimalToBytes(new BigDecimal(valString1), 8);
    // union schema for the logical field with "null" and "decimal" as types
    String fieldSchema1 = "{\"type\":\"record\",\"name\":\"Event\",\"fields\":[{\"name\":\"amount\","
        + "\"type\":[\"null\",{\"type\":\"bytes\",\"logicalType\":\"decimal\",\"precision\":20,\"scale\":8}]}]}";
    Schema schema1 = new Schema.Parser().parse(fieldSchema1);
    Object result1 = AvroSchemaUtil.applyLogicalType(schema1.getField("amount"), amount1);
    Assert.assertTrue(result1 instanceof BigDecimal);
    Assert.assertEquals(valString1, ((BigDecimal) result1).toPlainString());

    String valString2 = "125.53172";
    ByteBuffer amount2 = decimalToBytes(new BigDecimal(valString2), 5);
    // "null" not present within the union schema for the logical field amount
    String fieldSchema2 = "{\"type\":\"record\",\"name\":\"Event\",\"fields\":[{\"name\":\"amount\","
        + "\"type\":[{\"type\":\"bytes\",\"logicalType\":\"decimal\",\"precision\":20,\"scale\":5}]}]}";
    Schema schema2 = new Schema.Parser().parse(fieldSchema2);
    Object result2 = AvroSchemaUtil.applyLogicalType(schema2.getField("amount"), amount2);
    Assert.assertTrue(result2 instanceof BigDecimal);
    Assert.assertEquals(valString2, ((BigDecimal) result2).toPlainString());

    String valString3 = "211.53172864999";
    ByteBuffer amount3 = decimalToBytes(new BigDecimal(valString3), 11);
    // "null" present at the second position within the union schema for the logical field amount
    String fieldSchema3 = "{\"type\":\"record\",\"name\":\"Event\",\"fields\":[{\"name\":\"amount\","
        + "\"type\":[{\"type\":\"bytes\",\"logicalType\":\"decimal\",\"precision\":20,\"scale\":11}, \"null\"]}]}";
    Schema schema3 = new Schema.Parser().parse(fieldSchema3);
    Object result3 = AvroSchemaUtil.applyLogicalType(schema3.getField("amount"), amount3);
    Assert.assertTrue(result3 instanceof BigDecimal);
    Assert.assertEquals(valString3, ((BigDecimal) result3).toPlainString());
  }