public static Optional<TableSchema> getUpdatedSchema(
      TableSchema oldSchema, TableSchema newSchema) {
    Result updatedFields = getUpdatedSchema(oldSchema.getFieldsList(), newSchema.getFieldsList());
    if (updatedFields.isEquivalent()) {
      return Optional.empty();
    } else {
      return updatedFields
          .getFields()
          .map(
              tableFieldSchemas ->
                  TableSchema.newBuilder().addAllFields(tableFieldSchemas).build());
    }
  }
@Test
  public void testSchemaUpdate() {
    TableSchema baseSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .build();
    TableSchema schema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(baseSchema.getFieldsList()))
            .build();
    TableSchema topSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(schema.getFieldsList()))
            .build();

    TableSchema newBaseSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("d").setType(TableFieldSchema.Type.STRING))
            .build();
    TableSchema newSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(newBaseSchema.getFieldsList()))
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("d").setType(TableFieldSchema.Type.STRING))
            .build();

    TableSchema newTopSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(newSchema.getFieldsList()))
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .build();

    TableSchema expectedSchemaBaseSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("d").setType(TableFieldSchema.Type.STRING))
            .build();
    TableSchema expectedSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("b").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder().setName("c").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(expectedSchemaBaseSchema.getFieldsList()))
            .addFields(
                TableFieldSchema.newBuilder().setName("d").setType(TableFieldSchema.Type.STRING))
            .build();
    TableSchema expectedTopSchema =
        TableSchema.newBuilder()
            .addFields(
                TableFieldSchema.newBuilder().setName("a").setType(TableFieldSchema.Type.STRING))
            .addFields(
                TableFieldSchema.newBuilder()
                    .setName("nested")
                    .setType(TableFieldSchema.Type.STRUCT)
                    .addAllFields(expectedSchema.getFieldsList()))
            .build();

    TableSchema updatedTopSchema =
        TableSchemaUpdateUtils.getUpdatedSchema(topSchema, newTopSchema).get();
    assertEquals(expectedTopSchema, updatedTopSchema);
  }