Skip to content

The Field mode normalization for null values is incorrect and sets it as an empty String. #2730

@keskival

Description

@keskival

The Field mode normalization for null values is incorrect and sets it as an empty String.

/** Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used. */
public Builder setMode(Mode mode) {
this.mode = mode != null ? mode.name() : Data.<String>nullOf(String.class);
return this;
}

Instead of setting the mode String as "" here, it should be set to Field.Mode.NULLABLE.name().

This causes the following problem: When the Field is deserialized from protobuf response from BigQuery, it might have mode set to null. Specifically this seems to happen when a table has been created with SQL CREATE TABLE as it doesn't support setting the field as NULLABLE explicitly.

Steps to reproduce

Create a BigQuery table with some nullable columns using CREATE TABLE.

Get the schema from Java with bigQuery.getTable().getSchema(), compare with a Schema you have created by setting the mode to Field.Mode.NULLABLE explicitly using Field.equals(field2).

Expected result: Both Schemas are equal.

Result: Schemas are inequal. This is because comparison is done for Protobuf representation where an empty String is a placeholder for null.

Code example

  @Test
  public void assertThatSchemaEqualsComparisonIsDeterministic() {
    final Field field1 = Field
      .of("field1", LegacySQLTypeName.INTEGER)
      .toBuilder()
      .setMode(Field.Mode.NULLABLE)
      .build();
    final Field field1b = Field
      .of("field1", LegacySQLTypeName.INTEGER)
      .toBuilder()
      .setMode(null)
      .build();
    final Schema schema1 = Schema.of(field1);
    final Schema schema2 = Schema.of(field1b);
    assertThat(schema1.equals(schema2)).isEqualTo(true);
  }

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/java-bigquery API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions