From ce31e01ca0da0c889fccf7a5a70f471faf1dabfa Mon Sep 17 00:00:00 2001 From: Keshav Dandeva Date: Thu, 12 Feb 2026 02:03:13 +0000 Subject: [PATCH 1/2] refactor(jdbc): connection properties and data source properties --- .../bigquery/jdbc/BigQueryConnection.java | 392 +++++----- .../bigquery/jdbc/BigQueryJdbcUrlUtility.java | 403 +++++----- .../jdbc/BigQueryJdbcUrlUtilityTest.java | 724 +----------------- 3 files changed, 403 insertions(+), 1116 deletions(-) diff --git a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java index 5c5d5bc84..f4a919f33 100644 --- a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java +++ b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java @@ -52,7 +52,10 @@ import java.sql.Statement; import java.time.Duration; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.ConcurrentModificationException; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; @@ -60,6 +63,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * An implementation of {@link java.sql.Connection} for establishing a connection with BigQuery and @@ -142,49 +146,42 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { this.sqlWarnings = new ArrayList<>(); this.transactionStarted = false; this.isClosed = false; - this.labels = BigQueryJdbcUrlUtility.parseLabels(url, connectionClassName); - this.maxBytesBilled = - BigQueryJdbcUrlUtility.parseMaximumBytesBilled(url, this.connectionClassName); - this.retryTimeoutInSeconds = - BigQueryJdbcUrlUtility.parseRetryTimeoutInSecs(url, this.connectionClassName); - this.retryTimeoutDuration = Duration.ofMillis(retryTimeoutInSeconds * 1000L); - this.retryInitialDelayInSeconds = - BigQueryJdbcUrlUtility.parseRetryInitialDelayInSecs(url, this.connectionClassName); - this.retryInitialDelayDuration = Duration.ofMillis(retryInitialDelayInSeconds * 1000L); - this.retryMaxDelayInSeconds = - BigQueryJdbcUrlUtility.parseRetryMaxDelayInSecs(url, this.connectionClassName); - this.retryMaxDelayDuration = Duration.ofMillis(retryMaxDelayInSeconds * 1000L); - this.jobTimeoutInSeconds = - BigQueryJdbcUrlUtility.parseJobTimeout(url, this.connectionClassName); - this.authProperties = - BigQueryJdbcOAuthUtility.parseOAuthProperties(url, this.connectionClassName); - this.catalog = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.PROJECT_ID_PROPERTY_NAME, - BigQueryOptions.getDefaultProjectId(), - this.connectionClassName); - this.universeDomain = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_UNIVERSE_DOMAIN_VALUE, - this.connectionClassName); - this.overrideProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(url, this.connectionClassName); - if (universeDomain != null) { - this.overrideProperties.put( - BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, universeDomain); + + Map parsedProperties = BigQueryJdbcUrlUtility.parseUrlProperties(url); + DataSource ds = new DataSource(); + BigQueryJdbcUrlUtility.setDataSourceProperties(ds, parsedProperties); + + this.authProperties = new HashMap<>(); + if (ds.getOAuthType() != 0) { + this.authProperties.put( + "OAuthType", BigQueryJdbcOAuthUtility.AuthType.values()[ds.getOAuthType()].name()); } + if (ds.getOAuthServiceAcctEmail() != null) { + this.authProperties.put("OAuthServiceAcctEmail", ds.getOAuthServiceAcctEmail()); + } + if (ds.getOAuthPvtKeyPath() != null) { + this.authProperties.put("OAuthPvtKeyPath", ds.getOAuthPvtKeyPath()); + } + if (ds.getOAuthClientId() != null) { + this.authProperties.put("OAuthClientId", ds.getOAuthClientId()); + } + if (ds.getOAuthClientSecret() != null) { + this.authProperties.put("OAuthClientSecret", ds.getOAuthClientSecret()); + } + if (ds.getOAuthRefreshToken() != null) { + this.authProperties.put("OAuthRefreshToken", ds.getOAuthRefreshToken()); + } + if (ds.getOAuthAccessToken() != null) { + this.authProperties.put("OAuthAccessToken", ds.getOAuthAccessToken()); + } + this.overrideProperties = new HashMap<>(); + + this.connectionClassName = getClass().getName(); this.credentials = BigQueryJdbcOAuthUtility.getCredentials( - authProperties, overrideProperties, this.connectionClassName); - String defaultDatasetString = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.DEFAULT_DATASET_PROPERTY_NAME, - null, - this.connectionClassName); + this.authProperties, this.overrideProperties, this.connectionClassName); + + String defaultDatasetString = ds.getDefaultDataset(); if (defaultDatasetString == null || defaultDatasetString.trim().isEmpty()) { this.defaultDataset = null; } else { @@ -199,177 +196,172 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { + " projectId.datasetId"); } } - this.location = - BigQueryJdbcUrlUtility.parseStringProperty( - url, BigQueryJdbcUrlUtility.LOCATION_PROPERTY_NAME, null, this.connectionClassName); + + this.location = ds.getLocation(); + this.enableHighThroughputAPI = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.ENABLE_HTAPI_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_ENABLE_HTAPI_VALUE, - this.connectionClassName); + ds.getEnableHighThroughputAPI() != null + ? ds.getEnableHighThroughputAPI() + : BigQueryJdbcUrlUtility.DEFAULT_ENABLE_HTAPI_VALUE; this.highThroughputMinTableSize = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.HTAPI_MIN_TABLE_SIZE_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_HTAPI_MIN_TABLE_SIZE_VALUE, - this.connectionClassName); + ds.getHighThroughputMinTableSize() != null + ? ds.getHighThroughputMinTableSize() + : BigQueryJdbcUrlUtility.DEFAULT_HTAPI_MIN_TABLE_SIZE_VALUE; this.highThroughputActivationRatio = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.HTAPI_ACTIVATION_RATIO_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_HTAPI_ACTIVATION_RATIO_VALUE, - this.connectionClassName); + ds.getHighThroughputActivationRatio() != null + ? ds.getHighThroughputActivationRatio() + : BigQueryJdbcUrlUtility.DEFAULT_HTAPI_ACTIVATION_RATIO_VALUE; + this.enableSession = + ds.getEnableSession() != null + ? ds.getEnableSession() + : BigQueryJdbcUrlUtility.DEFAULT_ENABLE_SESSION_VALUE; + this.unsupportedHTAPIFallback = + ds.getUnsupportedHTAPIFallback() != null + ? ds.getUnsupportedHTAPIFallback() + : BigQueryJdbcUrlUtility.DEFAULT_UNSUPPORTED_HTAPI_FALLBACK_VALUE; + this.useQueryCache = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.USE_QUERY_CACHE_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_USE_QUERY_CACHE, - this.connectionClassName); - this.useStatelessQueryMode = - BigQueryJdbcUrlUtility.parseJobCreationMode(url, this.connectionClassName); + ds.getUseQueryCache() != null + ? ds.getUseQueryCache() + : BigQueryJdbcUrlUtility.DEFAULT_USE_QUERY_CACHE; this.queryDialect = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.QUERY_DIALECT_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE, - this.connectionClassName); + ds.getQueryDialect() != null + ? ds.getQueryDialect() + : BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE; this.allowLargeResults = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.ALLOW_LARGE_RESULTS_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_ALLOW_LARGE_RESULTS, - this.connectionClassName); - this.destinationTable = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.LARGE_RESULTS_TABLE_PROPERTY_NAME, - null, - this.connectionClassName); - this.destinationDataset = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.LARGE_RESULTS_DATASET_PROPERTY_NAME, - null, - this.connectionClassName); + ds.getAllowLargeResults() != null + ? ds.getAllowLargeResults() + : BigQueryJdbcUrlUtility.DEFAULT_ALLOW_LARGE_RESULTS; + this.destinationTable = ds.getDestinationTable(); + this.destinationDataset = ds.getDestinationDataset(); this.destinationDatasetExpirationTime = - BigQueryJdbcUrlUtility.parseLongProperty( - url, - BigQueryJdbcUrlUtility.DESTINATION_DATASET_EXPIRATION_TIME_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_DESTINATION_DATASET_EXPIRATION_TIME_VALUE, - this.connectionClassName); - this.kmsKeyName = - BigQueryJdbcUrlUtility.parseStringProperty( - url, BigQueryJdbcUrlUtility.KMS_KEY_NAME_PROPERTY_NAME, null, this.connectionClassName); - Map proxyProperties = - BigQueryJdbcProxyUtility.parseProxyProperties(url, this.connectionClassName); - this.sslTrustStorePath = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.SSL_TRUST_STORE_PROPERTY_NAME, - null, - this.connectionClassName); - this.sslTrustStorePassword = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.SSL_TRUST_STORE_PWD_PROPERTY_NAME, - null, - this.connectionClassName); - this.httpConnectTimeout = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.HTTP_CONNECT_TIMEOUT_PROPERTY_NAME, - null, - this.connectionClassName); - this.httpReadTimeout = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.HTTP_READ_TIMEOUT_PROPERTY_NAME, - null, - this.connectionClassName); - this.httpTransportOptions = - BigQueryJdbcProxyUtility.getHttpTransportOptions( - proxyProperties, - this.sslTrustStorePath, - this.sslTrustStorePassword, - this.httpConnectTimeout, - this.httpReadTimeout, - this.connectionClassName); - this.transportChannelProvider = - BigQueryJdbcProxyUtility.getTransportChannelProvider( - proxyProperties, - this.sslTrustStorePath, - this.sslTrustStorePassword, - this.connectionClassName); - this.enableSession = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.ENABLE_SESSION_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_ENABLE_SESSION_VALUE, - this.connectionClassName); - this.unsupportedHTAPIFallback = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.UNSUPPORTED_HTAPI_FALLBACK_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_UNSUPPORTED_HTAPI_FALLBACK_VALUE, - this.connectionClassName); + ds.getDestinationDatasetExpirationTime() != null + ? ds.getDestinationDatasetExpirationTime() + : BigQueryJdbcUrlUtility.DEFAULT_DESTINATION_DATASET_EXPIRATION_TIME_VALUE; + this.kmsKeyName = ds.getKmsKeyName(); this.maxResults = - BigQueryJdbcUrlUtility.parseLongProperty( - url, - BigQueryJdbcUrlUtility.MAX_RESULTS_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_MAX_RESULTS_VALUE, - this.connectionClassName); - Map queryPropertiesMap = - BigQueryJdbcUrlUtility.parseQueryProperties(url, this.connectionClassName); - this.sessionInfoConnectionProperty = getSessionPropertyFromQueryProperties(queryPropertiesMap); - this.queryProperties = convertMapToConnectionPropertiesList(queryPropertiesMap); + ds.getMaxResults() != null + ? ds.getMaxResults() + : BigQueryJdbcUrlUtility.DEFAULT_MAX_RESULTS_VALUE; + this.jobTimeoutInSeconds = + ds.getJobTimeout() != null + ? ds.getJobTimeout() + : BigQueryJdbcUrlUtility.DEFAULT_JOB_TIMEOUT_VALUE; + + this.queryProperties = new ArrayList<>(); + if (ds.getQueryProperties() != null) { + for (Map.Entry entry : ds.getQueryProperties().entrySet()) { + this.queryProperties.add( + com.google.cloud.bigquery.ConnectionProperty.newBuilder() + .setKey(entry.getKey()) + .setValue(entry.getValue()) + .build()); + } + } + + this.universeDomain = ds.getUniverseDomain(); + if (this.universeDomain != null) { + if (this.overrideProperties == null) { + this.overrideProperties = new HashMap<>(); + } + this.overrideProperties.put( + BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, this.universeDomain); + } + + String additionalProjectsStr = ds.getAdditionalProjects(); + if (additionalProjectsStr != null && !additionalProjectsStr.trim().isEmpty()) { + this.additionalProjects = + Arrays.stream(additionalProjectsStr.split(",")) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .collect(Collectors.toList()); + } else { + this.additionalProjects = Collections.emptyList(); + } + this.enableWriteAPI = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.ENABLE_WRITE_API_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_ENABLE_WRITE_API_VALUE, - this.connectionClassName); - this.writeAPIActivationRowCount = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.SWA_ACTIVATION_ROW_COUNT_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_SWA_ACTIVATION_ROW_COUNT_VALUE, - this.connectionClassName); + ds.getEnableWriteAPI() != null + ? ds.getEnableWriteAPI() + : BigQueryJdbcUrlUtility.DEFAULT_ENABLE_WRITE_API_VALUE; this.writeAPIAppendRowCount = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.SWA_APPEND_ROW_COUNT_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_SWA_APPEND_ROW_COUNT_VALUE, - this.connectionClassName); - this.additionalProjects = - BigQueryJdbcUrlUtility.parseStringListProperty( - url, - BigQueryJdbcUrlUtility.ADDITIONAL_PROJECTS_PROPERTY_NAME, - this.connectionClassName); + ds.getSwaAppendRowCount() != null + ? ds.getSwaAppendRowCount() + : BigQueryJdbcUrlUtility.DEFAULT_SWA_APPEND_ROW_COUNT_VALUE; + this.writeAPIActivationRowCount = + ds.getSwaActivationRowCount() != null + ? ds.getSwaActivationRowCount() + : BigQueryJdbcUrlUtility.DEFAULT_SWA_ACTIVATION_ROW_COUNT_VALUE; this.filterTablesOnDefaultDataset = - BigQueryJdbcUrlUtility.parseBooleanProperty( - url, - BigQueryJdbcUrlUtility.FILTER_TABLES_ON_DEFAULT_DATASET_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_FILTER_TABLES_ON_DEFAULT_DATASET_VALUE, - this.connectionClassName); - this.requestGoogleDriveScope = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE, - this.connectionClassName); - this.metadataFetchThreadCount = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.METADATA_FETCH_THREAD_COUNT_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_METADATA_FETCH_THREAD_COUNT_VALUE, - this.connectionClassName); - this.requestReason = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.REQUEST_REASON_PROPERTY_NAME, - null, - this.connectionClassName); + ds.getFilterTablesOnDefaultDataset() != null + ? ds.getFilterTablesOnDefaultDataset() + : BigQueryJdbcUrlUtility.DEFAULT_FILTER_TABLES_ON_DEFAULT_DATASET_VALUE; + this.sslTrustStorePath = ds.getSSLTrustStorePath(); + this.sslTrustStorePassword = ds.getSSLTrustStorePassword(); + + if (ds.getHttpConnectTimeout() != null) { + this.httpConnectTimeout = ds.getHttpConnectTimeout(); + } else { + this.httpConnectTimeout = null; + } + if (ds.getHttpReadTimeout() != null) { + this.httpReadTimeout = ds.getHttpReadTimeout(); + } else { + this.httpReadTimeout = null; + } + + if (ds.getRequestGoogleDriveScope() != null) { + this.requestGoogleDriveScope = ds.getRequestGoogleDriveScope(); + } else { + this.requestGoogleDriveScope = + BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE; + } + + if (ds.getMetadataFetchThreadCount() != null) { + this.metadataFetchThreadCount = ds.getMetadataFetchThreadCount(); + } else { + this.metadataFetchThreadCount = + BigQueryJdbcUrlUtility.DEFAULT_METADATA_FETCH_THREAD_COUNT_VALUE; + } + + this.requestReason = ds.getRequestReason(); + this.labels = ds.getLabels() != null ? ds.getLabels() : new HashMap<>(); + this.maxBytesBilled = + ds.getMaximumBytesBilled() != null + ? ds.getMaximumBytesBilled() + : BigQueryJdbcUrlUtility.DEFAULT_MAX_BYTES_BILLED_VALUE; + + Integer jobCreationMode = ds.getJobCreationMode(); + if (jobCreationMode != null) { + if (jobCreationMode == 1) { + this.useStatelessQueryMode = false; + } else if (jobCreationMode == 2) { + this.useStatelessQueryMode = true; + } else { + throw new NumberFormatException( + String.format( + "Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for" + + " JOB_CREATION_OPTIONAL.", + BigQueryJdbcUrlUtility.JOB_CREATION_MODE_PROPERTY_NAME)); + } + } else { + this.useStatelessQueryMode = true; + } + + this.retryTimeoutInSeconds = + ds.getRetryTimeoutInSecs() != null + ? ds.getRetryTimeoutInSecs() + : BigQueryJdbcUrlUtility.DEFAULT_RETRY_TIMEOUT_IN_SECS_VALUE; + this.retryTimeoutDuration = Duration.ofMillis(this.retryTimeoutInSeconds * 1000L); + this.retryInitialDelayInSeconds = + ds.getRetryInitialDelayInSecs() != null + ? ds.getRetryInitialDelayInSecs() + : BigQueryJdbcUrlUtility.DEFAULT_RETRY_INITIAL_DELAY_VALUE; + this.retryInitialDelayDuration = Duration.ofMillis(this.retryInitialDelayInSeconds * 1000L); + this.retryMaxDelayInSeconds = + ds.getRetryMaxDelayInSecs() != null + ? ds.getRetryMaxDelayInSecs() + : BigQueryJdbcUrlUtility.DEFAULT_RETRY_MAX_DELAY_VALUE; + this.retryMaxDelayDuration = Duration.ofMillis(this.retryMaxDelayInSeconds * 1000L); HEADER_PROVIDER = createHeaderProvider(); this.bigQuery = getBigQueryConnection(); diff --git a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java index 50d0d33bb..967b008d5 100644 --- a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java +++ b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java @@ -28,10 +28,10 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.Set; +import java.util.TreeMap; import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; /** * This class implements all the methods that parse Connection property values from the Connection @@ -601,10 +601,19 @@ private BigQueryJdbcUrlUtility() {} * @return The String value of the property, or the default value if the property is not found. */ static String parseUriProperty(String uri, String property) { - Pattern pattern = Pattern.compile(String.format("(?is)(?:;|\\?)%s=(.*?)(?:;|$)", property)); - Matcher matcher = pattern.matcher(uri); - if (matcher.find() && matcher.groupCount() == 1) { - return CharEscapers.decodeUriPath(matcher.group(1)); + try { + Map props = parseUrlProperties(uri); + + for (Map.Entry entry : props.entrySet()) { + if (entry.getKey().equalsIgnoreCase(property)) { + return entry.getValue(); + } + } + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new BigQueryJdbcRuntimeException(e.getMessage()); } return null; } @@ -628,71 +637,6 @@ static String appendPropertiesToURL(String url, String callerClassName, Properti return urlBuilder.toString(); } - static boolean convertIntToBoolean(String value, String propertyName) { - int integerValue; - - try { - if (value.equalsIgnoreCase("true")) { - integerValue = 1; - } else if (value.equalsIgnoreCase("false")) { - integerValue = 0; - } else { - integerValue = Integer.parseInt(value); - } - - } catch (NumberFormatException ex) { - throw new IllegalArgumentException( - String.format( - "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for" - + " true.", - propertyName), - ex); - } - if (integerValue == 1) { - return true; - } else if (integerValue == 0) { - return false; - } else { - throw new IllegalArgumentException( - String.format( - "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for" - + " true.", - propertyName)); - } - } - - // todo just make it a map - static Map parseQueryProperties(String url, String callerClassName) { - return parsePropertiesMap(url, QUERY_PROPERTIES_NAME, callerClassName); - } - - static Map parseLabels(String url, String callerClassName) { - return parsePropertiesMap(url, LABELS_PROPERTY_NAME, callerClassName); - } - - static String parseStringProperty( - String url, String propertyName, String defaultValue, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String parsedValue = BigQueryJdbcUrlUtility.parseUriProperty(url, propertyName); - if (parsedValue != null) { - return parsedValue; - } - return defaultValue; - } - - static List parseStringListProperty( - String url, String propertyName, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String rawValue = parseStringProperty(url, propertyName, null, callerClassName); - if (rawValue == null || rawValue.trim().isEmpty()) { - return Collections.emptyList(); - } - return Arrays.stream(rawValue.split(",")) - .map(String::trim) - .filter(s -> !s.isEmpty()) - .collect(Collectors.toList()); - } - public static String parsePartnerTokenProperty(String url, String callerClassName) { LOG.finest("++enter++\t" + callerClassName); // This property is expected to be set by partners only. For more details on exact format @@ -717,45 +661,6 @@ public static String parsePartnerTokenProperty(String url, String callerClassNam return null; } - static Integer parseIntProperty( - String url, String propertyName, Integer defaultValue, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String parsedValue = BigQueryJdbcUrlUtility.parseUriProperty(url, propertyName); - if (parsedValue != null) { - try { - return Integer.parseInt(parsedValue); - } catch (NumberFormatException e) { - LOG.severe( - "Invalid integer value '%s' for property '%s'. Please provide a valid integer.", - parsedValue, propertyName); - throw new IllegalArgumentException( - String.format("Invalid integer value for property '%s': %s", propertyName, parsedValue), - e); - } - } - return defaultValue; - } - - static Long parseLongProperty( - String url, String propertyName, Long defaultValue, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String parsedValue = BigQueryJdbcUrlUtility.parseUriProperty(url, propertyName); - if (parsedValue != null) { - return Long.parseLong(parsedValue); - } - return defaultValue; - } - - static Boolean parseBooleanProperty( - String url, String propertyName, Boolean defaultValue, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String parsedValue = BigQueryJdbcUrlUtility.parseUriProperty(url, propertyName); - if (parsedValue != null) { - return convertIntToBoolean(parsedValue, propertyName); - } - return defaultValue; - } - public static Level parseLogLevel(String logLevelString) { int logLevel = logLevelString != null ? Integer.parseInt(logLevelString) : DEFAULT_LOG_LEVEL; switch (logLevel) { @@ -782,21 +687,8 @@ public static Level parseLogLevel(String logLevelString) { } } - static Map parseOverrideProperties(String url, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); + static Map parseOverridePropertiesString(String overridePropertiesString) { Map overrideProps = new HashMap<>(); - Pattern pattern = - Pattern.compile( - String.format( - "(?is)(%s|%s)=([^;]+)", - ENDPOINT_OVERRIDES_PROPERTY_NAME, PRIVATE_SERVICE_CONNECT_PROPERTY_NAME)); - Matcher matcher = pattern.matcher(url); - String overridePropertiesString; - if (matcher.find() && matcher.groupCount() >= 1) { - overridePropertiesString = matcher.group(2); - } else { - return overrideProps; - } for (String property : OVERRIDE_PROPERTIES) { Pattern propertyPattern = Pattern.compile(String.format("(?i)%s=(.*?)(?:[,;]|$)", property)); Matcher propertyMatcher = propertyPattern.matcher(overridePropertiesString); @@ -807,128 +699,175 @@ static Map parseOverrideProperties(String url, String callerClas return overrideProps; } - public static boolean parseJobCreationMode(String url, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - - String jobCreationMode = - BigQueryJdbcUrlUtility.parseUriProperty(url, JOB_CREATION_MODE_PROPERTY_NAME); - - if (jobCreationMode == null) { - LOG.fine( - "%s value not provided, defaulting to %s. Caller: %s", - JOB_CREATION_MODE_PROPERTY_NAME, DEFAULT_JOB_CREATION_MODE, callerClassName); - // Default Job creation mode is JOB_CREATION_OPTIONAL(2) - // which translates to options.setQueryPreviewEnabled(true) - return true; - } - if (jobCreationMode.equalsIgnoreCase("1")) { - return false; - } else if (jobCreationMode.equalsIgnoreCase("2")) { - return true; - } else { - throw new NumberFormatException( - String.format( - "Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for" - + " JOB_CREATION_OPTIONAL.", - JOB_CREATION_MODE_PROPERTY_NAME)); - } + private static boolean convertStrToIntBoolean(String value) { + if ("1".equals(value)) return true; + if ("0".equals(value)) return false; + return Boolean.parseBoolean(value); } - public static String parseBYOIDProperty(String url, String property, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - - String value = BigQueryJdbcUrlUtility.parseUriProperty(url, property); - String defaultValue = BigQueryJdbcUrlUtility.getConnectionPropertyDefaultValue(property); - if (value != null) { - return value; - } else if (defaultValue != null) { - return defaultValue; - } - return null; - } - - public static String getConnectionPropertyDefaultValue(String propertyName) { - // TODO: change how we store properties because this method has to go through all of them - for (BigQueryConnectionProperty property : VALID_PROPERTIES) { - if (property.getName().equals(propertyName)) { - return property.getDefaultValue(); - } - } - return null; - } - - public static long parseRetryTimeoutInSecs(String url, String callerClassName) { - return BigQueryJdbcUrlUtility.parseLongProperty( - url, - RETRY_TIMEOUT_IN_SECS_PROPERTY_NAME, - DEFAULT_RETRY_TIMEOUT_IN_SECS_VALUE, - callerClassName); - } - - public static long parseJobTimeout(String url, String callerClassName) { - return parseLongProperty( - url, JOB_TIMEOUT_PROPERTY_NAME, DEFAULT_JOB_TIMEOUT_VALUE, callerClassName); - } - - public static long parseRetryInitialDelayInSecs(String url, String callerClassName) { - return BigQueryJdbcUrlUtility.parseLongProperty( - url, RETRY_INITIAL_DELAY_PROPERTY_NAME, DEFAULT_RETRY_INITIAL_DELAY_VALUE, callerClassName); - } - - public static long parseRetryMaxDelayInSecs(String url, String callerClassName) { - return BigQueryJdbcUrlUtility.parseLongProperty( - url, RETRY_MAX_DELAY_PROPERTY_NAME, DEFAULT_RETRY_MAX_DELAY_VALUE, callerClassName); - } - - // Convenience Helper Methods - public static long parseConnectionPoolSize(String url, String callerClassName) { + public static Map parseUrlProperties(String url) throws IllegalArgumentException { + Map properties = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); if (url == null || url.isEmpty()) { - throw new BigQueryJdbcRuntimeException("Connection url is empty"); + return properties; } - return parseLongProperty( - url, - CONNECTION_POOL_SIZE_PROPERTY_NAME, - DEFAULT_CONNECTION_POOL_SIZE_VALUE, - callerClassName); - } - public static long parseListenerPoolSize(String url, String callerClassName) { - if (url == null || url.isEmpty()) { - throw new BigQueryJdbcRuntimeException("Connection url is empty"); + int sep = url.indexOf(';'); + if (sep == -1) { + return properties; } - return parseLongProperty( - url, LISTENER_POOL_SIZE_PROPERTY_NAME, DEFAULT_LISTENER_POOL_SIZE_VALUE, callerClassName); - } - public static long parseMaximumBytesBilled(String url, String callerClassName) { - if (url == null || url.isEmpty()) { - throw new BigQueryJdbcRuntimeException("Connection url is empty"); + String[] parts = url.substring(sep + 1).split(";"); + for (String part : parts) { + if (part.trim().isEmpty()) { + continue; + } + int eqIdx = part.indexOf('='); + if (eqIdx != -1) { + String key = part.substring(0, eqIdx).trim(); + String val = part.substring(eqIdx + 1).trim(); + properties.put(key, CharEscapers.decodeUriPath(val)); + } } - return parseLongProperty( - url, MAX_BYTES_BILLED_PROPERTY_NAME, DEFAULT_MAX_BYTES_BILLED_VALUE, callerClassName); + return properties; } - private static Map parsePropertiesMap( - String url, String propertyName, String callerClassName) { - LOG.finest("++enter++\t" + callerClassName); - String propertiesString = BigQueryJdbcUrlUtility.parseUriProperty(url, propertyName); - if (propertiesString == null || propertiesString.isEmpty()) { - LOG.fine("Unable to parse property name: %s from url: %s", propertyName, url); - return null; - } - Map propertiesMap = new HashMap<>(); - String[] keyValuePairs = propertiesString.split(","); + private static final java.util.Map> + PROPERTY_SETTERS = new java.util.HashMap<>(); + + static { + PROPERTY_SETTERS.put("projectid", (ds, val) -> ds.setProjectId(val)); + PROPERTY_SETTERS.put("defaultdataset", (ds, val) -> ds.setDefaultDataset(val)); + PROPERTY_SETTERS.put("oauthtype", (ds, val) -> ds.setOAuthType(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "enablehighthroughputapi", + (ds, val) -> ds.setEnableHighThroughputAPI(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put( + "highthroughputmintablesize", + (ds, val) -> ds.setHighThroughputMinTableSize(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "highthroughputactivationratio", + (ds, val) -> ds.setHighThroughputActivationRatio(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "unsupportedhtapifallback", + (ds, val) -> ds.setUnsupportedHTAPIFallback(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put("kmskeyname", (ds, val) -> ds.setKmsKeyName(val)); + PROPERTY_SETTERS.put( + "queryproperties", + (ds, val) -> { + Map querypropertiesMap = new java.util.HashMap<>(); + for (String kv : val.split(",")) { + String[] kvParts = kv.split("="); + if (kvParts.length == 2) { + querypropertiesMap.put(kvParts[0].trim(), kvParts[1].trim()); + } + } + ds.setQueryProperties(querypropertiesMap); + }); + PROPERTY_SETTERS.put("loglevel", (ds, val) -> ds.setLogLevel(val)); + PROPERTY_SETTERS.put( + "enablesession", (ds, val) -> ds.setEnableSession(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put("logpath", (ds, val) -> ds.setLogPath(val)); + PROPERTY_SETTERS.put("oauthserviceacctemail", (ds, val) -> ds.setOAuthServiceAcctEmail(val)); + PROPERTY_SETTERS.put("oauthpvtkeypath", (ds, val) -> ds.setOAuthPvtKeyPath(val)); + PROPERTY_SETTERS.put("oauthpvtkey", (ds, val) -> ds.setOAuthPvtKey(val)); + PROPERTY_SETTERS.put("oauthaccesstoken", (ds, val) -> ds.setOAuthAccessToken(val)); + PROPERTY_SETTERS.put("oauthrefreshtoken", (ds, val) -> ds.setOAuthRefreshToken(val)); + PROPERTY_SETTERS.put( + "usequerycache", (ds, val) -> ds.setUseQueryCache(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put("querydialect", (ds, val) -> ds.setQueryDialect(val)); + PROPERTY_SETTERS.put( + "allowlargeresults", (ds, val) -> ds.setAllowLargeResults(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put("largeresulttable", (ds, val) -> ds.setDestinationTable(val)); + PROPERTY_SETTERS.put("largeresultdataset", (ds, val) -> ds.setDestinationDataset(val)); + PROPERTY_SETTERS.put( + "largeresultsdatasetexpirationtime", + (ds, val) -> ds.setDestinationDatasetExpirationTime(Long.parseLong(val))); + PROPERTY_SETTERS.put("universedomain", (ds, val) -> ds.setUniverseDomain(val)); + PROPERTY_SETTERS.put("proxyhost", (ds, val) -> ds.setProxyHost(val)); + PROPERTY_SETTERS.put("proxyport", (ds, val) -> ds.setProxyPort(val)); + PROPERTY_SETTERS.put("proxyuid", (ds, val) -> ds.setProxyUid(val)); + PROPERTY_SETTERS.put("proxypwd", (ds, val) -> ds.setProxyPwd(val)); + PROPERTY_SETTERS.put("oauthclientid", (ds, val) -> ds.setOAuthClientId(val)); + PROPERTY_SETTERS.put("oauthclientsecret", (ds, val) -> ds.setOAuthClientSecret(val)); + PROPERTY_SETTERS.put( + "jobcreationmode", (ds, val) -> ds.setJobCreationMode(Integer.parseInt(val))); + PROPERTY_SETTERS.put("maxresults", (ds, val) -> ds.setMaxResults(Long.parseLong(val))); + PROPERTY_SETTERS.put("partnertoken", (ds, val) -> ds.setPartnerToken(val)); + PROPERTY_SETTERS.put( + "enablewriteapi", (ds, val) -> ds.setEnableWriteAPI(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put("additionalprojects", (ds, val) -> ds.setAdditionalProjects(val)); + PROPERTY_SETTERS.put( + "filtertablesondefaultdataset", + (ds, val) -> ds.setFilterTablesOnDefaultDataset(convertStrToIntBoolean(val))); + PROPERTY_SETTERS.put( + "requestgoogledrivescope", + (ds, val) -> ds.setRequestGoogleDriveScope(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "metadatafetchthreadcount", + (ds, val) -> ds.setMetadataFetchThreadCount(Integer.parseInt(val))); + PROPERTY_SETTERS.put("ssltruststore", (ds, val) -> ds.setSSLTrustStorePath(val)); + PROPERTY_SETTERS.put("ssltruststorepwd", (ds, val) -> ds.setSSLTrustStorePassword(val)); + PROPERTY_SETTERS.put( + "labels", + (ds, val) -> { + Map labelsMap = new java.util.HashMap<>(); + for (String kv : val.split(",")) { + String[] kvParts = kv.split("="); + if (kvParts.length == 2) { + labelsMap.put(kvParts[0].trim(), kvParts[1].trim()); + } + } + ds.setLabels(labelsMap); + }); + PROPERTY_SETTERS.put("requestreason", (ds, val) -> ds.setRequestReason(val)); + PROPERTY_SETTERS.put( + "maximumbytesbilled", (ds, val) -> ds.setMaximumBytesBilled(Long.parseLong(val))); + PROPERTY_SETTERS.put("timeout", (ds, val) -> ds.setRetryTimeoutInSecs(Long.parseLong(val))); + PROPERTY_SETTERS.put("jobtimeout", (ds, val) -> ds.setJobTimeout(Long.parseLong(val))); + PROPERTY_SETTERS.put( + "retryinitialdelay", (ds, val) -> ds.setRetryInitialDelayInSecs(Long.parseLong(val))); + PROPERTY_SETTERS.put( + "retrymaxdelay", (ds, val) -> ds.setRetryMaxDelayInSecs(Long.parseLong(val))); + PROPERTY_SETTERS.put( + "httpconnecttimeout", (ds, val) -> ds.setHttpConnectTimeout(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "httpreadtimeout", (ds, val) -> ds.setHttpReadTimeout(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "swa_appendrowcount", (ds, val) -> ds.setSwaAppendRowCount(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "swa_activationrowcount", (ds, val) -> ds.setSwaActivationRowCount(Integer.parseInt(val))); + PROPERTY_SETTERS.put( + "connectionpoolsize", (ds, val) -> ds.setConnectionPoolSize(Long.parseLong(val))); + PROPERTY_SETTERS.put( + "listenerpoolsize", (ds, val) -> ds.setListenerPoolSize(Long.parseLong(val))); + PROPERTY_SETTERS.put("location", (ds, val) -> ds.setLocation(val)); + } - for (String keyValuePair : keyValuePairs) { - String[] parts = keyValuePair.split("="); - if (parts.length == 2) { - propertiesMap.put(parts[0], parts[1]); + public static void setDataSourceProperties(DataSource ds, Map urlProps) + throws IllegalArgumentException { + for (Map.Entry entry : urlProps.entrySet()) { + String key = entry.getKey(); + String val = entry.getValue(); + java.util.function.BiConsumer setter = + PROPERTY_SETTERS.get(key.toLowerCase()); + if (setter != null) { + setter.accept(ds, val); } else { - LOG.warning( - "Invalid KeyValue pair: %s found in url: %s for property name: %s", - keyValuePair, url, propertyName); + boolean found = + BYOID_PROPERTIES.stream().anyMatch(key::equalsIgnoreCase) + || OVERRIDE_PROPERTIES.stream().anyMatch(key::equalsIgnoreCase); + if (!found + && !key.equalsIgnoreCase("Location") + && !key.equalsIgnoreCase("OAuthType") + && !key.equalsIgnoreCase("ProjectId") + && !key.equalsIgnoreCase("ServiceAccountImpersonationEmail") + && !key.equalsIgnoreCase("ServiceAccountImpersonationScopes") + && !key.equalsIgnoreCase("EndpointOverrides") + && !key.equalsIgnoreCase("ServiceAccountImpersonationTokenLifetime") + && !key.equalsIgnoreCase("universeDomain")) { + throw new IllegalArgumentException("Unknown connection property: " + key); + } } } - return propertiesMap; } } diff --git a/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtilityTest.java b/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtilityTest.java index b16d1a398..b5db1a7a8 100644 --- a/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtilityTest.java +++ b/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtilityTest.java @@ -17,19 +17,8 @@ package com.google.cloud.bigquery.jdbc; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; - -import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException; -import com.google.common.collect.Maps; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; + import java.util.Map; import java.util.Properties; import org.junit.Test; @@ -106,294 +95,81 @@ public void testConnectionPropertiesFromURI() { @Test public void testConnectionPropertiesFromURIMultiline() { String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;Multiline=value1\nvalue2\n;"; + "bigquery://https://www.googleapis.com/bigquery/v2:443;AdditionalProjects=value1\n" + + "value2\n" + + ";"; - assertThat(BigQueryJdbcUrlUtility.parseUriProperty(connection_uri, "Multiline")) - .isEqualTo("value1\nvalue2\n"); + assertThat(BigQueryJdbcUrlUtility.parseUriProperty(connection_uri, "AdditionalProjects")) + .isEqualTo("value1\nvalue2"); } @Test public void testConnectionPropertiesFromURIMultilineNoSemicolon() { String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;Multiline=value1\nvalue2"; + "bigquery://https://www.googleapis.com/bigquery/v2:443;AdditionalProjects=value1\nvalue2"; - assertThat(BigQueryJdbcUrlUtility.parseUriProperty(connection_uri, "Multiline")) + assertThat(BigQueryJdbcUrlUtility.parseUriProperty(connection_uri, "AdditionalProjects")) .isEqualTo("value1\nvalue2"); } @Test - public void testOverridePropertiesFromURICompatibility() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID=testProject;PrivateServiceConnectUris=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443;"; + public void testParseOverridePropertiesString() { + String overrideString = + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," + + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443"; - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); + Map parsed = + BigQueryJdbcUrlUtility.parseOverridePropertiesString(overrideString); - assertThat(parsedPSCProperties.get("BIGQUERY")) + assertThat(parsed.get("BIGQUERY")) .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) + assertThat(parsed.get("READ_API")) .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); } @Test - public void testOverridePropertiesDoesNotAffectOriginalParsersAtEnd() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PrivateServiceConnectUris=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443," - + "OAUTH2=https://oauth2-myprivateserver.p.googleapis.com;PROJECTID=testProject;"; - - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); - - assertThat(parsedPSCProperties.get("BIGQUERY")) - .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) - .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - assertThat(parsedPSCProperties.get("OAUTH2")) - .isEqualTo("https://oauth2-myprivateserver.p.googleapis.com"); - } - - @Test - public void testOverridePropertiesDoesNotParseOutsideOfPrivateServiceConnectUris() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PrivateServiceConnectUris=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443;" - // Hard to see but semicolon ends it here. - + "OAUTH2=https://oauth2-myprivateserver.p.googleapis.com;PROJECTID=testProject;"; - - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); - - assertThat(parsedPSCProperties.get("BIGQUERY")) - .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) - .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - assertThat(parsedPSCProperties.get("OAUTH2")).isNull(); - } - - @Test - public void testOverridePropertiesDoesNotParserPropertiesInMiddle() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PrivateServiceConnectUris=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com,OAUTHTYPE=2," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443," - + "OAUTH2=https://oauth2-myprivateserver.p.googleapis.com;"; - - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); - - assertThat(parsedPSCProperties.get("BIGQUERY")) - .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) - .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - assertThat(parsedPSCProperties.get("OAUTH2")) - .isEqualTo("https://oauth2-myprivateserver.p.googleapis.com"); - } - - @Test - public void testOverridePropertyBeforeProceedingOverrideParameterDoesNotParse() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com;" - + "PrivateServiceConnectUris=" - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443," - + "OAUTH2=https://oauth2-myprivateserver.p.googleapis.com;"; - - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); - - assertNull(parsedPSCProperties.get("BIGQUERY")); - assertThat(parsedPSCProperties.get("READ_API")) - .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - assertThat(parsedPSCProperties.get("OAUTH2")) - .isEqualTo("https://oauth2-myprivateserver.p.googleapis.com"); - } - - @Test - public void testOverridePropertiesFromURIGoogleExperience() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID=testProject;EndpointOverrides=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443;"; - - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); - - assertThat(parsedPSCProperties.get("BIGQUERY")) - .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) - .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - } - - @Test - public void testAllOverridePropertiesFromURIGoogleExperience() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID=testProject;EndpointOverrides=" - + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," + public void testParseOverridePropertiesString_AllTypes() { + String overrideString = + "BIGQUERY=https://bigquery-myprivateserver.p.googleapis.com," + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443," + "OAUTH2=https://oauth2-myprivateserver.p.googleapis.com," - + "STS=https://sts-myprivateserver.p.googleapis.com;"; + + "STS=https://sts-myprivateserver.p.googleapis.com"; - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); + Map parsed = + BigQueryJdbcUrlUtility.parseOverridePropertiesString(overrideString); - assertThat(parsedPSCProperties.get("BIGQUERY")) + assertThat(parsed.get("BIGQUERY")) .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) + assertThat(parsed.get("READ_API")) .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); - assertThat(parsedPSCProperties.get("OAUTH2")) - .isEqualTo("https://oauth2-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("STS")) - .isEqualTo("https://sts-myprivateserver.p.googleapis.com"); + assertThat(parsed.get("OAUTH2")).isEqualTo("https://oauth2-myprivateserver.p.googleapis.com"); + assertThat(parsed.get("STS")).isEqualTo("https://sts-myprivateserver.p.googleapis.com"); } @Test - public void testCaseSensitivityOverridePropertiesFromURI() { - String connection_uri = - "bigquery://https://www.googleapis.com/bigquery/v2:443;PROJECTID=testProject;endpointOverrides=" - + "bigQuery=https://bigquery-myprivateserver.p.googleapis.com," - + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443;"; + public void testParseOverridePropertiesString_CaseInsensitive() { + String overrideString = + "bigQuery=https://bigquery-myprivateserver.p.googleapis.com," + + "READ_API=https://bigquerystorage-myprivateserver.p.googleapis.com:443"; - Map parsedPSCProperties = - BigQueryJdbcUrlUtility.parseOverrideProperties(connection_uri, null); + Map parsed = + BigQueryJdbcUrlUtility.parseOverridePropertiesString(overrideString); - assertThat(parsedPSCProperties.get("BIGQUERY")) + assertThat(parsed.get("BIGQUERY")) .isEqualTo("https://bigquery-myprivateserver.p.googleapis.com"); - assertThat(parsedPSCProperties.get("READ_API")) + assertThat(parsed.get("READ_API")) .isEqualTo("https://bigquerystorage-myprivateserver.p.googleapis.com:443"); } @Test - public void testParseJobCreationModeDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;"; - - boolean jobCreationMode = BigQueryJdbcUrlUtility.parseJobCreationMode(connection_uri, null); - assertTrue(jobCreationMode); - } - - @Test - public void testParseJobCreationMode() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobCreationMode=1"; - - boolean jobCreationMode = BigQueryJdbcUrlUtility.parseJobCreationMode(connection_uri, null); - assertFalse(jobCreationMode); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobCreationMode=2"; - - jobCreationMode = BigQueryJdbcUrlUtility.parseJobCreationMode(connection_uri, null); - assertTrue(jobCreationMode); - } - - @Test - public void testParseJobCreationModeInvalidInteger() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobCreationMode=25"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseJobCreationMode(connection_uri, null)); - } - - @Test - public void testParseJobCreationModeInvalidString() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobCreationMode=JOB_CREATION_OPTIONAL"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseJobCreationMode(connection_uri, null)); - } - - @Test - public void testGetConnectionPropertyDefaultValue() { - assertEquals( - BigQueryJdbcUrlUtility.getConnectionPropertyDefaultValue("BYOID_TokenUri"), - "https://sts.googleapis.com/v1/token"); - } - - @Test - public void testParseRetryTimeoutInSecs() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "Timeout=10"; - - long retryTimeoutInSeconds = - BigQueryJdbcUrlUtility.parseRetryTimeoutInSecs(connection_uri, null); - assertEquals(10, retryTimeoutInSeconds); + public void testParseOverridePropertiesString_IgnoresUnknown() { + String overrideString = "BIGQUERY=https://bq.example.com,UNKNOWN=https://unknown.example.com"; - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "Timeout=20"; - - retryTimeoutInSeconds = BigQueryJdbcUrlUtility.parseRetryTimeoutInSecs(connection_uri, null); - assertEquals(20, retryTimeoutInSeconds); - } - - @Test - public void testParseRetryTimeoutInSecsDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; + Map parsed = + BigQueryJdbcUrlUtility.parseOverridePropertiesString(overrideString); - long retryTimeoutInSeconds = - BigQueryJdbcUrlUtility.parseRetryTimeoutInSecs(connection_uri, null); - assertEquals(0, retryTimeoutInSeconds); - } - - @Test - public void testParseRetryTimeoutSecondsInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "Timeout=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseRetryTimeoutInSecs(connection_uri, null)); - } - - public void testParseJobTimeout() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobTimeout=10"; - - long jobTimeout = BigQueryJdbcUrlUtility.parseJobTimeout(connection_uri, null); - assertEquals(10, jobTimeout); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobTimeout=20"; - - jobTimeout = BigQueryJdbcUrlUtility.parseJobTimeout(connection_uri, null); - assertEquals(20, jobTimeout); - } - - @Test - public void testParseJobTimeoutDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long jobTimeout = BigQueryJdbcUrlUtility.parseJobTimeout(connection_uri, null); - assertEquals(0L, jobTimeout); - } - - @Test - public void testParseJobTimeoutInvalid() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "JobTimeout=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseJobTimeout(connection_uri, null)); + assertThat(parsed.get("BIGQUERY")).isEqualTo("https://bq.example.com"); + assertThat(parsed).doesNotContainKey("UNKNOWN"); } @Test @@ -440,424 +216,4 @@ public void testParsePartnerTokenProperty() { result = BigQueryJdbcUrlUtility.parsePartnerTokenProperty(url, "testParsePartnerTokenProperty"); assertThat(result).isEqualTo(expected); } - - public void testParseRetryInitialDelayInSecs() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryInitialDelay=10"; - - long retryInitialDelaySeconds = - BigQueryJdbcUrlUtility.parseRetryInitialDelayInSecs(connection_uri, null); - assertEquals(10, retryInitialDelaySeconds); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryInitialDelay=20"; - - retryInitialDelaySeconds = - BigQueryJdbcUrlUtility.parseRetryInitialDelayInSecs(connection_uri, null); - assertEquals(20, retryInitialDelaySeconds); - } - - @Test - public void testParseRetryInitialDelayInSecsDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long retryInitialDelaySeconds = - BigQueryJdbcUrlUtility.parseRetryInitialDelayInSecs(connection_uri, null); - assertEquals(0, retryInitialDelaySeconds); - } - - @Test - public void testParseRetryInitialDelaySecondsInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryInitialDelay=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseRetryInitialDelayInSecs(connection_uri, null)); - } - - @Test - public void testParseRetryMaxDelayInSecs() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryMaxDelay=10"; - - long retryMaxDelaySeconds = - BigQueryJdbcUrlUtility.parseRetryMaxDelayInSecs(connection_uri, null); - assertEquals(10, retryMaxDelaySeconds); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryMaxDelay=20"; - - retryMaxDelaySeconds = BigQueryJdbcUrlUtility.parseRetryMaxDelayInSecs(connection_uri, null); - assertEquals(20, retryMaxDelaySeconds); - } - - @Test - public void testParseRetryMaxDelayInSecsDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long retryMaxDelaySeconds = - BigQueryJdbcUrlUtility.parseRetryMaxDelayInSecs(connection_uri, null); - assertEquals(0, retryMaxDelaySeconds); - } - - @Test - public void testParseRetryMaxDelaySecondsInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "RetryMaxDelay=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseRetryMaxDelayInSecs(connection_uri, null)); - } - - @Test - public void testParseRequestGoogleDriveScope_Default() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;"; - Integer value = - BigQueryJdbcUrlUtility.parseIntProperty( - url, - BigQueryJdbcUrlUtility.REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME, - BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE, - this.getClass().getName()); - assertEquals( - Integer.valueOf(BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE), value); - } - - // Connection Pool Size - - @Test - public void testParseConnectionPoolSize() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ConnectionPoolSize=10"; - long connectionPoolSize = BigQueryJdbcUrlUtility.parseConnectionPoolSize(connection_uri, null); - assertEquals(10, connectionPoolSize); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ConnectionPoolSize=20"; - - connectionPoolSize = BigQueryJdbcUrlUtility.parseConnectionPoolSize(connection_uri, null); - assertEquals(20, connectionPoolSize); - } - - @Test - public void testParseConnectionPoolSizeDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long connectionPoolSize = BigQueryJdbcUrlUtility.parseConnectionPoolSize(connection_uri, null); - assertEquals(10, connectionPoolSize); - } - - @Test - public void testParseConnectionPoolSizeDefaultNullConnectionUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseConnectionPoolSize(null, null)); - } - - @Test - public void testParseConnectionPoolSizeDefaultEmptyConnectionUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseConnectionPoolSize("", null)); - } - - @Test - public void testParseConnectionPoolSizeInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ConnectionPoolSize=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseConnectionPoolSize(connection_uri, null)); - } - - // Listener Pool Size - - @Test - public void testParseListenerPoolSize() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ListenerPoolSize=10"; - long listenerPoolSize = BigQueryJdbcUrlUtility.parseListenerPoolSize(connection_uri, null); - assertEquals(10, listenerPoolSize); - - connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ListenerPoolSize=20"; - - listenerPoolSize = BigQueryJdbcUrlUtility.parseListenerPoolSize(connection_uri, null); - assertEquals(20, listenerPoolSize); - } - - @Test - public void testParseListenerPoolSizeDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long listenerPoolSize = BigQueryJdbcUrlUtility.parseListenerPoolSize(connection_uri, null); - assertEquals(10, listenerPoolSize); - } - - @Test - public void testParseListenerPoolSizeDefaultNullConnectionUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseListenerPoolSize(null, null)); - } - - @Test - public void testParseListenerPoolSizeDefaultEmptyConnectionUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseListenerPoolSize("", null)); - } - - @Test - public void testParseListenerPoolSizeInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "ListenerPoolSize=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseListenerPoolSize(connection_uri, null)); - } - - @Test - public void testParseStringListProperty_NullOrEmpty() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;SomeProp="; - List result = - BigQueryJdbcUrlUtility.parseStringListProperty(url, "NonExistentProp", "TestClass"); - assertEquals(Collections.emptyList(), result); - - result = BigQueryJdbcUrlUtility.parseStringListProperty(url, "SomeProp", "TestClass"); - assertEquals(Collections.emptyList(), result); - - String urlWithEmptyList = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;ListProp=,,"; - result = - BigQueryJdbcUrlUtility.parseStringListProperty(urlWithEmptyList, "ListProp", "TestClass"); - assertEquals(Collections.emptyList(), result); - } - - @Test - public void testParseStringListProperty_SingleValue() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;ListProp=project1"; - List result = - BigQueryJdbcUrlUtility.parseStringListProperty(url, "ListProp", "TestClass"); - assertEquals(Collections.singletonList("project1"), result); - } - - @Test - public void testParseStringListProperty_MultipleValues() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;ListProp=project1,project2,project3"; - List result = - BigQueryJdbcUrlUtility.parseStringListProperty(url, "ListProp", "TestClass"); - assertEquals(Arrays.asList("project1", "project2", "project3"), result); - } - - @Test - public void testParseIntProperty_ValidInteger() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;SomeIntProp=123"; - Integer defaultValue = 0; - Integer result = - BigQueryJdbcUrlUtility.parseIntProperty(url, "SomeIntProp", defaultValue, "TestClass"); - assertEquals(Integer.valueOf(123), result); - } - - @Test - public void testParseIntProperty_PropertyNotPresent() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;SomeIntProp=123"; - Integer defaultValue = 42; - Integer result = - BigQueryJdbcUrlUtility.parseIntProperty(url, "MissingIntProp", defaultValue, "TestClass"); - assertEquals(defaultValue, result); - } - - @Test - public void testParseIntProperty_InvalidIntegerValue() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;InvalidIntProp=abc"; - Integer defaultValue = 77; - assertThrows( - IllegalArgumentException.class, - () -> - BigQueryJdbcUrlUtility.parseIntProperty( - url, "InvalidIntProp", defaultValue, "TestClass")); - } - - @Test - public void testParseIntProperty_EmptyStringValue() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=2;ProjectId=MyBigQueryProject;EmptyIntProp="; - Integer defaultValue = 88; - assertThrows( - IllegalArgumentException.class, - () -> - BigQueryJdbcUrlUtility.parseIntProperty( - url, "EmptyIntProp", defaultValue, "TestClass")); - } - - @Test - public void testParseMaxBytesBilled() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "MaximumBytesBilled=10000"; - - long maxBytesBilled = BigQueryJdbcUrlUtility.parseMaximumBytesBilled(connection_uri, null); - assertEquals(10000, maxBytesBilled); - } - - @Test - public void testParseMaxBytesBilledDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - long maxBytesBilled = BigQueryJdbcUrlUtility.parseMaximumBytesBilled(connection_uri, null); - assertEquals(0, maxBytesBilled); - } - - @Test - public void testParseMaxBytesBilledNullUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseMaximumBytesBilled(null, null)); - } - - @Test - public void testParseMaxBytesBilledEmptyUrl() { - assertThrows( - BigQueryJdbcRuntimeException.class, - () -> BigQueryJdbcUrlUtility.parseMaximumBytesBilled("", null)); - } - - @Test - public void testParseMaxBytesBilledInvalidLong() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "MaximumBytesBilled=invalid"; - - assertThrows( - NumberFormatException.class, - () -> BigQueryJdbcUrlUtility.parseMaximumBytesBilled(connection_uri, null)); - } - - @Test - public void testParseLabels() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "Labels=k1=v1,k2=v2,k3=v3;"; - - Map labels = BigQueryJdbcUrlUtility.parseLabels(connection_uri, null); - assertNotNull(labels); - assertFalse(labels.isEmpty()); - assertEquals(3, labels.size()); - - Map expected = - new HashMap() { - { - put("k1", "v1"); - put("k2", "v2"); - put("k3", "v3"); - } - }; - - assertTrue(Maps.difference(expected, labels).areEqual()); - } - - @Test - public void testParseLabelsEmpty() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;"; - - Map labels = BigQueryJdbcUrlUtility.parseLabels(connection_uri, null); - assertNull(labels); - } - - @Test - public void testParseHttpConnectTimeout() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "HttpConnectTimeout=10000"; - - Integer timeout = - BigQueryJdbcUrlUtility.parseIntProperty( - connection_uri, BigQueryJdbcUrlUtility.HTTP_CONNECT_TIMEOUT_PROPERTY_NAME, null, null); - assertEquals(Integer.valueOf(10000), timeout); - } - - @Test - public void testParseHttpConnectTimeoutDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - Integer timeout = - BigQueryJdbcUrlUtility.parseIntProperty( - connection_uri, BigQueryJdbcUrlUtility.HTTP_CONNECT_TIMEOUT_PROPERTY_NAME, null, null); - assertNull(timeout); - } - - @Test - public void testParseHttpReadTimeout() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject;" - + "HttpReadTimeout=20000"; - - Integer timeout = - BigQueryJdbcUrlUtility.parseIntProperty( - connection_uri, BigQueryJdbcUrlUtility.HTTP_READ_TIMEOUT_PROPERTY_NAME, null, null); - assertEquals(Integer.valueOf(20000), timeout); - } - - @Test - public void testParseHttpReadTimeoutDefault() { - String connection_uri = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;OAuthType=3;ProjectId=testProject"; - - Integer timeout = - BigQueryJdbcUrlUtility.parseIntProperty( - connection_uri, BigQueryJdbcUrlUtility.HTTP_READ_TIMEOUT_PROPERTY_NAME, null, null); - assertNull(timeout); - } - - @Test - public void testParseRequestReason() { - String url = - "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" - + "OAuthType=3;ProjectId=testProject;RequestReason=testingRequestReason;"; - String requestReason = - BigQueryJdbcUrlUtility.parseStringProperty( - url, - BigQueryJdbcUrlUtility.REQUEST_REASON_PROPERTY_NAME, - null, - "testParseRequestReason"); - assertEquals("testingRequestReason", requestReason); - } } From 6f986a2b798065649ec88ba5e7ace0c1e203cc4a Mon Sep 17 00:00:00 2001 From: Keshav Dandeva Date: Thu, 12 Feb 2026 02:03:28 +0000 Subject: [PATCH 2/2] chore: add missing properties to data source --- .../cloud/bigquery/jdbc/DataSource.java | 197 +++++++++++++++++- 1 file changed, 192 insertions(+), 5 deletions(-) diff --git a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java index b1501890b..9d52f0ed6 100644 --- a/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java +++ b/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java @@ -78,6 +78,19 @@ public class DataSource implements javax.sql.DataSource { private Integer metadataFetchThreadCount; private String sslTrustStorePath; private String sslTrustStorePassword; + private Map labels; + private String requestReason; + private Long maximumBytesBilled; + private Long retryTimeoutInSecs; + private Long retryInitialDelayInSecs; + private Long retryMaxDelayInSecs; + private Long jobTimeout; + private Integer httpConnectTimeout; + private Integer httpReadTimeout; + private Integer swaAppendRowCount; + private Integer swaActivationRowCount; + private Long connectionPoolSize; + private Long listenerPoolSize; // Make sure the JDBC driver class is loaded. static { @@ -283,6 +296,68 @@ private Properties createProperties() { BigQueryJdbcUrlUtility.SSL_TRUST_STORE_PWD_PROPERTY_NAME, String.valueOf(this.sslTrustStorePassword)); } + if (this.labels != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.LABELS_PROPERTY_NAME, this.labels.toString()); + } + if (this.requestReason != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.REQUEST_REASON_PROPERTY_NAME, this.requestReason); + } + if (this.maximumBytesBilled != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.MAX_BYTES_BILLED_PROPERTY_NAME, + String.valueOf(this.maximumBytesBilled)); + } + if (this.retryTimeoutInSecs != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.RETRY_TIMEOUT_IN_SECS_PROPERTY_NAME, + String.valueOf(this.retryTimeoutInSecs)); + } + if (this.retryInitialDelayInSecs != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.RETRY_INITIAL_DELAY_PROPERTY_NAME, + String.valueOf(this.retryInitialDelayInSecs)); + } + if (this.retryMaxDelayInSecs != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.RETRY_MAX_DELAY_PROPERTY_NAME, + String.valueOf(this.retryMaxDelayInSecs)); + } + if (this.jobTimeout != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.JOB_TIMEOUT_PROPERTY_NAME, String.valueOf(this.jobTimeout)); + } + if (this.httpConnectTimeout != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.HTTP_CONNECT_TIMEOUT_PROPERTY_NAME, + String.valueOf(this.httpConnectTimeout)); + } + if (this.httpReadTimeout != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.HTTP_READ_TIMEOUT_PROPERTY_NAME, + String.valueOf(this.httpReadTimeout)); + } + if (this.swaAppendRowCount != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.SWA_APPEND_ROW_COUNT_PROPERTY_NAME, + String.valueOf(this.swaAppendRowCount)); + } + if (this.swaActivationRowCount != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.SWA_ACTIVATION_ROW_COUNT_PROPERTY_NAME, + String.valueOf(this.swaActivationRowCount)); + } + if (this.connectionPoolSize != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.CONNECTION_POOL_SIZE_PROPERTY_NAME, + String.valueOf(this.connectionPoolSize)); + } + if (this.listenerPoolSize != null) { + connectionProperties.setProperty( + BigQueryJdbcUrlUtility.LISTENER_POOL_SIZE_PROPERTY_NAME, + String.valueOf(this.listenerPoolSize)); + } return connectionProperties; } @@ -343,7 +418,7 @@ public void setPartnerToken(String partnerToken) { this.partnerToken = partnerToken; } - public boolean getEnableHighThroughputAPI() { + public Boolean getEnableHighThroughputAPI() { return enableHighThroughputAPI; } @@ -351,11 +426,11 @@ public void setEnableHighThroughputAPI(Boolean enableHighThroughputAPI) { this.enableHighThroughputAPI = enableHighThroughputAPI; } - public int getHighThroughputMinTableSize() { + public Integer getHighThroughputMinTableSize() { return highThroughputMinTableSize; } - public int getHighThroughputActivationRatio() { + public Integer getHighThroughputActivationRatio() { return highThroughputActivationRatio; } @@ -387,11 +462,11 @@ public void setUnsupportedHTAPIFallback(Boolean unsupportedHTAPIFallback) { this.unsupportedHTAPIFallback = unsupportedHTAPIFallback; } - public boolean getUnsupportedHTAPIFallback() { + public Boolean getUnsupportedHTAPIFallback() { return this.unsupportedHTAPIFallback; } - public boolean getEnableSession() { + public Boolean getEnableSession() { return enableSession; } @@ -575,6 +650,14 @@ public void setJobCreationMode(Integer jobCreationMode) { this.jobCreationMode = jobCreationMode; } + public Long getMaxResults() { + return maxResults; + } + + public void setMaxResults(Long maxResults) { + this.maxResults = maxResults; + } + public Boolean getEnableWriteAPI() { return enableWriteAPI; } @@ -631,6 +714,110 @@ public void setSSLTrustStorePassword(String sslTrustStorePassword) { this.sslTrustStorePassword = sslTrustStorePassword; } + public Map getLabels() { + return labels; + } + + public void setLabels(Map labels) { + this.labels = labels; + } + + public String getRequestReason() { + return requestReason; + } + + public void setRequestReason(String requestReason) { + this.requestReason = requestReason; + } + + public Long getMaximumBytesBilled() { + return maximumBytesBilled; + } + + public void setMaximumBytesBilled(Long maximumBytesBilled) { + this.maximumBytesBilled = maximumBytesBilled; + } + + public Long getRetryTimeoutInSecs() { + return retryTimeoutInSecs; + } + + public void setRetryTimeoutInSecs(Long retryTimeoutInSecs) { + this.retryTimeoutInSecs = retryTimeoutInSecs; + } + + public Long getRetryInitialDelayInSecs() { + return retryInitialDelayInSecs; + } + + public void setRetryInitialDelayInSecs(Long retryInitialDelayInSecs) { + this.retryInitialDelayInSecs = retryInitialDelayInSecs; + } + + public Long getRetryMaxDelayInSecs() { + return retryMaxDelayInSecs; + } + + public void setRetryMaxDelayInSecs(Long retryMaxDelayInSecs) { + this.retryMaxDelayInSecs = retryMaxDelayInSecs; + } + + public Long getJobTimeout() { + return jobTimeout; + } + + public void setJobTimeout(Long jobTimeout) { + this.jobTimeout = jobTimeout; + } + + public Integer getHttpConnectTimeout() { + return httpConnectTimeout; + } + + public void setHttpConnectTimeout(Integer httpConnectTimeout) { + this.httpConnectTimeout = httpConnectTimeout; + } + + public Integer getHttpReadTimeout() { + return httpReadTimeout; + } + + public void setHttpReadTimeout(Integer httpReadTimeout) { + this.httpReadTimeout = httpReadTimeout; + } + + public Integer getSwaAppendRowCount() { + return swaAppendRowCount; + } + + public void setSwaAppendRowCount(Integer swaAppendRowCount) { + this.swaAppendRowCount = swaAppendRowCount; + } + + public Integer getSwaActivationRowCount() { + return swaActivationRowCount; + } + + public void setSwaActivationRowCount(Integer swaActivationRowCount) { + this.swaActivationRowCount = swaActivationRowCount; + } + + public Long getConnectionPoolSize() { + return connectionPoolSize; + } + + public void setConnectionPoolSize(Long connectionPoolSize) { + this.connectionPoolSize = connectionPoolSize; + } + + public Long getListenerPoolSize() { + return listenerPoolSize; + } + + public void setListenerPoolSize(Long listenerPoolSize) { + this.listenerPoolSize = listenerPoolSize; + } + @Override public PrintWriter getLogWriter() { return null;