Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: add support for AvroOptions (#1630)
New AvroOptions class and wired into ExternalTableDefinition
- Loading branch information
1 parent
6bdbc2d
commit 10c1961
Showing
7 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- see http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html --> | ||
<differences> | ||
<difference> | ||
<differenceType>7006</differenceType> | ||
<className>com/google/cloud/bigquery/FormatOptions</className> | ||
<method>com.google.cloud.bigquery.FormatOptions avro()</method> | ||
<to>com.google.cloud.bigquery.AvroOptions</to> | ||
</difference> | ||
</differences> |
112 changes: 112 additions & 0 deletions
112
google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/AvroOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.bigquery; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Google BigQuery options for AVRO format. This class wraps some properties of AVRO files used by | ||
* BigQuery to parse external data. | ||
*/ | ||
public final class AvroOptions extends FormatOptions { | ||
|
||
private static final long serialVersionUID = 2293570529308612712L; | ||
|
||
private final Boolean useAvroLogicalTypes; | ||
|
||
public static final class Builder { | ||
|
||
private Boolean useAvroLogicalTypes; | ||
|
||
private Builder() {} | ||
|
||
private Builder(AvroOptions avroOptions) { | ||
this.useAvroLogicalTypes = avroOptions.useAvroLogicalTypes; | ||
} | ||
|
||
/** | ||
* [Optional] Sets whether BigQuery should interpret logical types as the corresponding BigQuery | ||
* data type (for example, TIMESTAMP), instead of using the raw type (for example, INTEGER). | ||
*/ | ||
public Builder setUseAvroLogicalTypes(boolean useAvroLogicalTypes) { | ||
this.useAvroLogicalTypes = useAvroLogicalTypes; | ||
return this; | ||
} | ||
|
||
/** Creates a {@code AvroOptions} object. */ | ||
public AvroOptions build() { | ||
return new AvroOptions(this); | ||
} | ||
} | ||
|
||
private AvroOptions(Builder builder) { | ||
super(FormatOptions.AVRO); | ||
this.useAvroLogicalTypes = builder.useAvroLogicalTypes; | ||
} | ||
|
||
/** | ||
* Returns whether BigQuery should interpret logical types as the corresponding BigQuery data type | ||
* (for example, TIMESTAMP), instead of using the raw type (for example, INTEGER). | ||
*/ | ||
public Boolean useAvroLogicalTypes() { | ||
return useAvroLogicalTypes; | ||
} | ||
|
||
public Builder toBuilder() { | ||
return new Builder(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("type", getType()) | ||
.add("useAvroLogicalTypes", useAvroLogicalTypes) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getType(), useAvroLogicalTypes); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj == this | ||
|| obj instanceof AvroOptions && Objects.equals(toPb(), ((AvroOptions) obj).toPb()); | ||
} | ||
|
||
com.google.api.services.bigquery.model.AvroOptions toPb() { | ||
com.google.api.services.bigquery.model.AvroOptions avroOptions = | ||
new com.google.api.services.bigquery.model.AvroOptions(); | ||
avroOptions.setUseAvroLogicalTypes(useAvroLogicalTypes); | ||
return avroOptions; | ||
} | ||
|
||
/** Returns a builder for a AvroOptions object. */ | ||
public static AvroOptions.Builder newBuilder() { | ||
return new AvroOptions.Builder(); | ||
} | ||
|
||
static AvroOptions fromPb(com.google.api.services.bigquery.model.AvroOptions avroOptions) { | ||
Builder builder = newBuilder(); | ||
if (avroOptions.getUseAvroLogicalTypes() != null) { | ||
builder.setUseAvroLogicalTypes(avroOptions.getUseAvroLogicalTypes()); | ||
} | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/AvroOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.bigquery; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class AvroOptionsTest { | ||
|
||
private static final Boolean USE_AVRO_LOGICAL_TYPES = true; | ||
private static final AvroOptions AVRO_OPTIONS = | ||
AvroOptions.newBuilder().setUseAvroLogicalTypes(USE_AVRO_LOGICAL_TYPES).build(); | ||
|
||
@Test | ||
public void testToBuilder() { | ||
compareAvroOptions(AVRO_OPTIONS, AVRO_OPTIONS.toBuilder().build()); | ||
AvroOptions avroOptions = AVRO_OPTIONS.toBuilder().setUseAvroLogicalTypes(false).build(); | ||
assertEquals(false, avroOptions.useAvroLogicalTypes()); | ||
avroOptions = avroOptions.toBuilder().setUseAvroLogicalTypes(true).build(); | ||
compareAvroOptions(AVRO_OPTIONS, avroOptions); | ||
} | ||
|
||
@Test | ||
public void testBuilder() { | ||
assertEquals(FormatOptions.AVRO, AVRO_OPTIONS.getType()); | ||
assertEquals(USE_AVRO_LOGICAL_TYPES, AVRO_OPTIONS.useAvroLogicalTypes()); | ||
} | ||
|
||
@Test | ||
public void testToAndFromPb() { | ||
compareAvroOptions(AVRO_OPTIONS, AvroOptions.fromPb(AVRO_OPTIONS.toPb())); | ||
AvroOptions avroOptions = | ||
AvroOptions.newBuilder().setUseAvroLogicalTypes(USE_AVRO_LOGICAL_TYPES).build(); | ||
compareAvroOptions(avroOptions, AvroOptions.fromPb(avroOptions.toPb())); | ||
} | ||
|
||
private void compareAvroOptions(AvroOptions expected, AvroOptions value) { | ||
assertEquals(expected, value); | ||
assertEquals(expected.useAvroLogicalTypes(), value.useAvroLogicalTypes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters