Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 681af66

Browse files
committed
Add enums for SourceColumnMatch
1 parent 099b71e commit 681af66

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ExternalTableDefinition.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) {
5757

5858
private static final long serialVersionUID = -5951580238459622025L;
5959

60+
public enum SourceColumnMatch {
61+
POSITION("POSITION"),
62+
NAME("NAME");
63+
64+
private final String option;
65+
66+
SourceColumnMatch(String option) {
67+
this.option = option;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return option;
73+
}
74+
}
75+
6076
@AutoValue.Builder
6177
public abstract static class Builder
6278
extends TableDefinition.Builder<ExternalTableDefinition, Builder> {
@@ -243,7 +259,7 @@ public Builder setMaxStaleness(String maxStaleness) {
243259
* reads the header row as column names and reorders columns to match the field names in the
244260
* schema.
245261
*/
246-
public abstract Builder setSourceColumnMatch(String sourceColumnMatch);
262+
public abstract Builder setSourceColumnMatch(SourceColumnMatch sourceColumnMatch);
247263

248264
/**
249265
* A list of strings represented as SQL NULL value in a CSV file. null_marker and null_markers
@@ -437,7 +453,7 @@ public HivePartitioningOptions getHivePartitioningOptions() {
437453

438454
/** Returns the strategy used to match loaded columns to the schema, either POSITION or NAME. */
439455
@Nullable
440-
public abstract String getSourceColumnMatch();
456+
public abstract SourceColumnMatch getSourceColumnMatch();
441457

442458
/** Returns a list of strings represented as SQL NULL value in a CSV file. */
443459
@Nullable
@@ -540,7 +556,9 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataC
540556
externalConfigurationPb.setTimestampFormat(getTimestampFormat());
541557
}
542558
if (getSourceColumnMatch() != null) {
543-
externalConfigurationPb.getCsvOptions().setSourceColumnMatch(getSourceColumnMatch());
559+
externalConfigurationPb
560+
.getCsvOptions()
561+
.setSourceColumnMatch(getSourceColumnMatch().toString());
544562
}
545563
if (getNullMarkers() != null) {
546564
externalConfigurationPb.getCsvOptions().setNullMarkers(getNullMarkers());
@@ -763,7 +781,8 @@ static ExternalTableDefinition fromPb(Table tablePb) {
763781
if (externalDataConfiguration.getCsvOptions() != null) {
764782
if (externalDataConfiguration.getCsvOptions().getSourceColumnMatch() != null) {
765783
builder.setSourceColumnMatch(
766-
externalDataConfiguration.getCsvOptions().getSourceColumnMatch());
784+
SourceColumnMatch.valueOf(
785+
externalDataConfiguration.getCsvOptions().getSourceColumnMatch()));
767786
}
768787
if (externalDataConfiguration.getCsvOptions().getNullMarkers() != null) {
769788
builder.setNullMarkers(externalDataConfiguration.getCsvOptions().getNullMarkers());
@@ -857,7 +876,8 @@ static ExternalTableDefinition fromExternalDataConfiguration(
857876
if (externalDataConfiguration.getCsvOptions() != null) {
858877
if (externalDataConfiguration.getCsvOptions().getSourceColumnMatch() != null) {
859878
builder.setSourceColumnMatch(
860-
externalDataConfiguration.getCsvOptions().getSourceColumnMatch());
879+
SourceColumnMatch.valueOf(
880+
externalDataConfiguration.getCsvOptions().getSourceColumnMatch()));
861881
}
862882
if (externalDataConfiguration.getCsvOptions().getNullMarkers() != null) {
863883
builder.setNullMarkers(externalDataConfiguration.getCsvOptions().getNullMarkers());

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/LoadJobConfiguration.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,26 @@ public final class LoadJobConfiguration extends JobConfiguration implements Load
6868
private final String datetimeFormat;
6969
private final String timeFormat;
7070
private final String timestampFormat;
71-
private final String sourceColumnMatch;
71+
private final SourceColumnMatch sourceColumnMatch;
7272
private final List<String> nullMarkers;
7373

74+
public enum SourceColumnMatch {
75+
SOURCE_COLUMN_MATCH_UNSPECIFIED("SOURCE_COLUMN_MATCH_UNSPECIFIED"),
76+
POSITION("POSITION"),
77+
NAME("NAME");
78+
79+
private final String option;
80+
81+
SourceColumnMatch(String option) {
82+
this.option = option;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return option;
88+
}
89+
}
90+
7491
public static final class Builder extends JobConfiguration.Builder<LoadJobConfiguration, Builder>
7592
implements LoadConfiguration.Builder {
7693

@@ -107,7 +124,7 @@ public static final class Builder extends JobConfiguration.Builder<LoadJobConfig
107124
private String datetimeFormat;
108125
private String timeFormat;
109126
private String timestampFormat;
110-
private String sourceColumnMatch;
127+
private SourceColumnMatch sourceColumnMatch;
111128
private List<String> nullMarkers;
112129

113130
private Builder() {
@@ -275,7 +292,8 @@ private Builder(com.google.api.services.bigquery.model.JobConfiguration configur
275292
this.timestampFormat = loadConfigurationPb.getTimestampFormat();
276293
}
277294
if (loadConfigurationPb.getSourceColumnMatch() != null) {
278-
this.sourceColumnMatch = loadConfigurationPb.getSourceColumnMatch();
295+
this.sourceColumnMatch =
296+
SourceColumnMatch.valueOf(loadConfigurationPb.getSourceColumnMatch());
279297
}
280298
if (loadConfigurationPb.getNullMarkers() != null) {
281299
this.nullMarkers = loadConfigurationPb.getNullMarkers();
@@ -530,7 +548,7 @@ public Builder setTimestampFormat(String timestampFormat) {
530548
* are matched by name. Otherwise, columns are matched by position. This is done to keep the
531549
* behavior backward-compatible.
532550
*/
533-
public Builder setSourceColumnMatch(String sourceColumnMatch) {
551+
public Builder setSourceColumnMatch(SourceColumnMatch sourceColumnMatch) {
534552
this.sourceColumnMatch = sourceColumnMatch;
535553
return this;
536554
}
@@ -768,7 +786,7 @@ public String getTimestampFormat() {
768786
}
769787

770788
/** Returns the strategy used to match loaded columns to the schema, either POSITION or NAME. */
771-
public String getSourceColumnMatch() {
789+
public SourceColumnMatch getSourceColumnMatch() {
772790
return sourceColumnMatch;
773791
}
774792

@@ -955,7 +973,7 @@ com.google.api.services.bigquery.model.JobConfiguration toPb() {
955973
loadConfigurationPb.setTimestampFormat(timestampFormat);
956974
}
957975
if (sourceColumnMatch != null) {
958-
loadConfigurationPb.setSourceColumnMatch(sourceColumnMatch);
976+
loadConfigurationPb.setSourceColumnMatch(sourceColumnMatch.toString());
959977
}
960978
if (nullMarkers != null) {
961979
loadConfigurationPb.setNullMarkers(nullMarkers);

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertNotEquals;
2121
import static org.junit.Assert.assertNotNull;
2222

23+
import com.google.cloud.bigquery.ExternalTableDefinition.SourceColumnMatch;
2324
import com.google.common.collect.ImmutableList;
2425
import java.util.List;
2526
import org.junit.Test;
@@ -66,7 +67,7 @@ public class ExternalTableDefinitionTest {
6667
private static final String DATETIME_FORMAT = "YYYY-MM-DD HH:MI:SS";
6768
private static final String TIME_FORMAT = "HH:MI:SS";
6869
private static final String TIMESTAMP_FORMAT = "YYYY-MM-DD HH:MI:SS";
69-
private static final String SOURCE_COLUMN_MATCH = "POSITION";
70+
private static final SourceColumnMatch SOURCE_COLUMN_MATCH = SourceColumnMatch.POSITION;
7071
private static final List<String> NULL_MARKERS = ImmutableList.of("SQL NULL", "SQL NULL");
7172
private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION =
7273
ExternalTableDefinition.newBuilder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS)

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigquery.JobInfo.CreateDisposition;
2222
import com.google.cloud.bigquery.JobInfo.SchemaUpdateOption;
2323
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
24+
import com.google.cloud.bigquery.LoadJobConfiguration.SourceColumnMatch;
2425
import com.google.cloud.bigquery.TimePartitioning.Type;
2526
import com.google.common.collect.ImmutableList;
2627
import com.google.common.collect.ImmutableMap;
@@ -82,7 +83,7 @@ public class LoadJobConfigurationTest {
8283
private static final String DATETIME_FORMAT = "YYYY-MM-DD HH:MI:SS";
8384
private static final String TIME_FORMAT = "HH:MI:SS";
8485
private static final String TIMESTAMP_FORMAT = "YYYY-MM-DD HH:MI:SS";
85-
private static final String SOURCE_COLUMN_MATCH = "POSITION";
86+
private static final SourceColumnMatch SOURCE_COLUMN_MATCH = SourceColumnMatch.POSITION;
8687
private static final List<String> NULL_MARKERS = ImmutableList.of("SQL NULL", "SQL NULL");
8788
private static final ConnectionProperty CONNECTION_PROPERTY =
8889
ConnectionProperty.newBuilder().setKey(KEY).setValue(VALUE).build();

0 commit comments

Comments
 (0)