Skip to content

Commit

Permalink
Refactor: Centralize valid write preferences in BigQueryExecute.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanks committed Dec 12, 2024
1 parent 1a239a4 commit fa4a6d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ public static final class Config extends AbstractBigQueryActionConfig {
// Sn = a * (1 - r^n) / (r - 1)
public static final long DEFULT_MAX_RETRY_DURATION_SECONDS = 63L;
public static final int DEFAULT_READ_TIMEOUT = 120;
public static final Set<String> VALID_WRITE_PREFERENCES = ImmutableSet.of(
JobInfo.WriteDisposition.WRITE_EMPTY.name(),
JobInfo.WriteDisposition.WRITE_APPEND.name(),
JobInfo.WriteDisposition.WRITE_TRUNCATE.name()
);

@Description("Dialect of the SQL command. The value must be 'legacy' or 'standard'. " +
"If set to 'standard', the query will use BigQuery's standard SQL: " +
Expand Down Expand Up @@ -569,15 +574,10 @@ public void validate(FailureCollector failureCollector, Map<String, String> argu
}

void validateWritePreference(FailureCollector failureCollector, String writePreference) {
Set<String> validPreferences = ImmutableSet.of(
JobInfo.WriteDisposition.WRITE_EMPTY.name(),
JobInfo.WriteDisposition.WRITE_APPEND.name(),
JobInfo.WriteDisposition.WRITE_TRUNCATE.name()
);
if (!validPreferences.contains(writePreference)) {
if (!VALID_WRITE_PREFERENCES.contains(writePreference)) {
failureCollector.addFailure(
String.format("Invalid write preference '%s'. Allowed values are '%s'.",
writePreference, validPreferences.toString()
writePreference, VALID_WRITE_PREFERENCES.toString()
),
"Please provide a valid write preference."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,13 @@ public void testValidateRetryConfigurationWithInvalidReadTimeout() {

@Test
public void testValidateWritePreferenceWithInvalidValue() {
Set<String> validPreferences = ImmutableSet.of(
JobInfo.WriteDisposition.WRITE_EMPTY.name(),
JobInfo.WriteDisposition.WRITE_APPEND.name(),
JobInfo.WriteDisposition.WRITE_TRUNCATE.name()
);
config.validateWritePreference(failureCollector, "INVALID_PREFERENCE");

// Assert validation failure
Assert.assertEquals(1, failureCollector.getValidationFailures().size());
Assert.assertEquals(
String.format("Invalid write preference 'INVALID_PREFERENCE'. Allowed values are '%s'.",
validPreferences.toString()
config.VALID_WRITE_PREFERENCES.toString()
),
failureCollector.getValidationFailures().get(0).getMessage()
);
Expand Down

0 comments on commit fa4a6d7

Please sign in to comment.