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

Commit c4e8eab

Browse files
committed
chore: address gemini feedback
1 parent 2d2da43 commit c4e8eab

6 files changed

Lines changed: 135 additions & 105 deletions

File tree

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
147147
DataSource ds = DataSource.fromUrl(url);
148148

149149
this.labels = ds.getLabels() != null ? ds.getLabels() : new java.util.HashMap<>();
150-
this.maxBytesBilled =
151-
ds.getMaximumBytesBilled() != null
152-
? ds.getMaximumBytesBilled()
153-
: BigQueryJdbcUrlUtility.DEFAULT_MAX_BYTES_BILLED_VALUE;
150+
this.maxBytesBilled = ds.getMaximumBytesBilled();
154151
this.retryTimeoutInSeconds = ds.getTimeout();
155152
this.retryTimeoutDuration = Duration.ofMillis(retryTimeoutInSeconds * 1000L);
156153
this.retryInitialDelayInSeconds = ds.getRetryInitialDelay();
@@ -160,24 +157,12 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
160157
this.jobTimeoutInSeconds = ds.getJobTimeout();
161158
this.authProperties =
162159
BigQueryJdbcOAuthUtility.parseOAuthProperties(ds, this.connectionClassName);
163-
this.catalog =
164-
ds.getProjectId() != null ? ds.getProjectId() : BigQueryOptions.getDefaultProjectId();
160+
this.catalog = ds.getProjectId();
165161
this.universeDomain = ds.getUniverseDomain();
166162

167-
this.overrideProperties = new java.util.HashMap<>();
168-
if (ds.getOAuth2TokenUri() != null) {
169-
this.overrideProperties.put(
170-
BigQueryJdbcUrlUtility.OAUTH2_TOKEN_URI_PROPERTY_NAME, ds.getOAuth2TokenUri());
171-
}
172-
if (ds.getEndpointOverrides() != null) {
173-
this.overrideProperties.put(
174-
BigQueryJdbcUrlUtility.ENDPOINT_OVERRIDES_PROPERTY_NAME, ds.getEndpointOverrides());
175-
}
176-
if (ds.getPrivateServiceConnect() != null) {
177-
this.overrideProperties.put(
178-
BigQueryJdbcUrlUtility.PRIVATE_SERVICE_CONNECT_PROPERTY_NAME,
179-
ds.getPrivateServiceConnect());
180-
}
163+
this.overrideProperties =
164+
BigQueryJdbcUrlUtility.parseOverrideProperties(
165+
this.connectionUrl, this.connectionClassName);
181166
if (this.universeDomain != null) {
182167
this.overrideProperties.put(
183168
BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, this.universeDomain);
@@ -206,7 +191,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
206191
this.highThroughputActivationRatio = ds.getHighThroughputActivationRatio();
207192
this.useQueryCache = ds.getUseQueryCache();
208193
Integer jobCreationMode = ds.getJobCreationMode();
209-
if (jobCreationMode == null || jobCreationMode == 2) {
194+
if (jobCreationMode == 2) {
210195
this.useStatelessQueryMode = true;
211196
} else if (jobCreationMode == 1) {
212197
this.useStatelessQueryMode = false;
@@ -224,47 +209,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
224209
this.destinationDataset = ds.getDestinationDataset();
225210
this.destinationDatasetExpirationTime = ds.getDestinationDatasetExpirationTime();
226211
this.kmsKeyName = ds.getKmsKeyName();
227-
Map<String, String> proxyProperties = new java.util.HashMap<>();
228-
if (ds.getProxyHost() != null)
229-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, ds.getProxyHost());
230-
if (ds.getProxyPort() != null)
231-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, ds.getProxyPort());
232-
if (ds.getProxyUid() != null)
233-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME, ds.getProxyUid());
234-
if (ds.getProxyPwd() != null)
235-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME, ds.getProxyPwd());
236-
String proxyHost = ds.getProxyHost();
237-
String proxyPort = ds.getProxyPort();
238-
String proxyUid = ds.getProxyUid();
239-
String proxyPwd = ds.getProxyPwd();
240-
241-
if (proxyPort != null
242-
&& !java.util.regex.Pattern.compile(
243-
"^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
244-
.matcher(proxyPort)
245-
.find()) {
246-
throw new IllegalArgumentException(
247-
"Illegal port number provided %s. Please provide a valid port number.");
248-
}
249-
250-
boolean isMissingProxyHostOrPortWhenProxySet =
251-
(proxyHost == null && proxyPort != null) || (proxyHost != null && proxyPort == null);
252-
if (isMissingProxyHostOrPortWhenProxySet) {
253-
throw new IllegalArgumentException(
254-
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
255-
+ " occurs.");
256-
}
257-
boolean isMissingProxyUidOrPwdWhenAuthSet =
258-
(proxyUid == null && proxyPwd != null) || (proxyUid != null && proxyPwd == null);
259-
if (isMissingProxyUidOrPwdWhenAuthSet) {
260-
throw new IllegalArgumentException(
261-
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
262-
}
263-
boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null;
264-
if (isProxyAuthSetWithoutProxySettings) {
265-
throw new IllegalArgumentException(
266-
"Proxy authentication provided via connection string with no proxy host or port set.");
267-
}
212+
Map<String, String> proxyProperties =
213+
BigQueryJdbcProxyUtility.parseProxyProperties(ds, this.connectionClassName);
268214

269215
this.sslTrustStorePath = ds.getSSLTrustStorePath();
270216
this.sslTrustStorePassword = ds.getSSLTrustStorePassword();
@@ -291,37 +237,19 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
291237
Map<String, String> queryPropertiesMap = ds.getQueryProperties();
292238
this.sessionInfoConnectionProperty = getSessionPropertyFromQueryProperties(queryPropertiesMap);
293239
this.queryProperties = convertMapToConnectionPropertiesList(queryPropertiesMap);
294-
this.enableWriteAPI =
295-
ds.getEnableWriteAPI() != null
296-
? ds.getEnableWriteAPI()
297-
: BigQueryJdbcUrlUtility.DEFAULT_ENABLE_WRITE_API_VALUE;
298-
this.writeAPIActivationRowCount =
299-
ds.getSwaActivationRowCount() != null
300-
? ds.getSwaActivationRowCount()
301-
: BigQueryJdbcUrlUtility.DEFAULT_SWA_ACTIVATION_ROW_COUNT_VALUE;
302-
this.writeAPIAppendRowCount =
303-
ds.getSwaAppendRowCount() != null
304-
? ds.getSwaAppendRowCount()
305-
: BigQueryJdbcUrlUtility.DEFAULT_SWA_APPEND_ROW_COUNT_VALUE;
240+
this.enableWriteAPI = ds.getEnableWriteAPI();
241+
this.writeAPIActivationRowCount = ds.getSwaActivationRowCount();
242+
this.writeAPIAppendRowCount = ds.getSwaAppendRowCount();
306243

307244
String additionalProjectsStr = ds.getAdditionalProjects();
308245
this.additionalProjects =
309246
(additionalProjectsStr == null || additionalProjectsStr.isEmpty())
310247
? java.util.Collections.emptyList()
311248
: java.util.Arrays.asList(additionalProjectsStr.split(","));
312249

313-
this.filterTablesOnDefaultDataset =
314-
ds.getFilterTablesOnDefaultDataset() != null
315-
? ds.getFilterTablesOnDefaultDataset()
316-
: BigQueryJdbcUrlUtility.DEFAULT_FILTER_TABLES_ON_DEFAULT_DATASET_VALUE;
317-
this.requestGoogleDriveScope =
318-
ds.getRequestGoogleDriveScope() != null
319-
? ds.getRequestGoogleDriveScope()
320-
: BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE;
321-
this.metadataFetchThreadCount =
322-
ds.getMetadataFetchThreadCount() != null
323-
? ds.getMetadataFetchThreadCount()
324-
: BigQueryJdbcUrlUtility.DEFAULT_METADATA_FETCH_THREAD_COUNT_VALUE;
250+
this.filterTablesOnDefaultDataset = ds.getFilterTablesOnDefaultDataset();
251+
this.requestGoogleDriveScope = ds.getRequestGoogleDriveScope();
252+
this.metadataFetchThreadCount = ds.getMetadataFetchThreadCount();
325253
this.requestReason = ds.getRequestReason();
326254
this.connectionPoolSize = ds.getConnectionPoolSize();
327255
this.listenerPoolSize = ds.getListenerPoolSize();

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,52 @@ final class BigQueryJdbcProxyUtility {
6161

6262
private BigQueryJdbcProxyUtility() {}
6363

64+
static Map<String, String> parseProxyProperties(DataSource ds, String callerClassName) {
65+
LOG.finest("++enter++\t" + callerClassName);
66+
Map<String, String> proxyProperties = new HashMap<>();
67+
String proxyHost = ds.getProxyHost();
68+
if (proxyHost != null) {
69+
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, proxyHost);
70+
}
71+
String proxyPort = ds.getProxyPort();
72+
if (proxyPort != null) {
73+
if (!Pattern.compile(validPortRegex).matcher(proxyPort).find()) {
74+
throw new IllegalArgumentException(
75+
String.format(
76+
"Illegal port number provided %s. Please provide a valid port number.", proxyPort));
77+
}
78+
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, proxyPort);
79+
}
80+
String proxyUid = ds.getProxyUid();
81+
if (proxyUid != null) {
82+
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME, proxyUid);
83+
}
84+
String proxyPwd = ds.getProxyPwd();
85+
if (proxyPwd != null) {
86+
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME, proxyPwd);
87+
}
88+
89+
boolean isMissingProxyHostOrPortWhenProxySet =
90+
(proxyHost == null && proxyPort != null) || (proxyHost != null && proxyPort == null);
91+
if (isMissingProxyHostOrPortWhenProxySet) {
92+
throw new IllegalArgumentException(
93+
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
94+
+ " occurs.");
95+
}
96+
boolean isMissingProxyUidOrPwdWhenAuthSet =
97+
(proxyUid == null && proxyPwd != null) || (proxyUid != null && proxyPwd == null);
98+
if (isMissingProxyUidOrPwdWhenAuthSet) {
99+
throw new IllegalArgumentException(
100+
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
101+
}
102+
boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null;
103+
if (isProxyAuthSetWithoutProxySettings) {
104+
throw new IllegalArgumentException(
105+
"Proxy authentication provided via connection string with no proxy host or port set.");
106+
}
107+
return proxyProperties;
108+
}
109+
64110
static Map<String, String> parseProxyProperties(String URL, String callerClassName) {
65111
LOG.finest("++enter++\t" + callerClassName);
66112
Map<String, String> proxyProperties = new HashMap<>();

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import com.google.common.base.Splitter;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.net.UrlEscapers;
25+
import java.io.UnsupportedEncodingException;
26+
import java.net.URLDecoder;
27+
import java.nio.charset.StandardCharsets;
2528
import java.util.Arrays;
2629
import java.util.Collections;
2730
import java.util.HashMap;
@@ -772,6 +775,36 @@ static String parsePartnerTokenProperty(String url, String callerClassName) {
772775
return parseUriProperty(url, PARTNER_TOKEN_PROPERTY_NAME);
773776
}
774777

778+
static Map<String, String> parseOverrideProperties(String url, String callerClassName) {
779+
LOG.finest("++enter++\t" + callerClassName);
780+
Map<String, String> overrideProps = new HashMap<>();
781+
Pattern pattern =
782+
Pattern.compile(
783+
String.format(
784+
"(?is)(%s|%s)=([^;]+)",
785+
ENDPOINT_OVERRIDES_PROPERTY_NAME, PRIVATE_SERVICE_CONNECT_PROPERTY_NAME));
786+
Matcher matcher = pattern.matcher(url);
787+
String overridePropertiesString;
788+
if (matcher.find() && matcher.groupCount() >= 1) {
789+
try {
790+
overridePropertiesString =
791+
URLDecoder.decode(matcher.group(2), StandardCharsets.UTF_8.name());
792+
} catch (UnsupportedEncodingException e) {
793+
throw new BigQueryJdbcRuntimeException(e);
794+
}
795+
} else {
796+
return overrideProps;
797+
}
798+
for (String property : OVERRIDE_PROPERTIES) {
799+
Pattern propertyPattern = Pattern.compile(String.format("(?i)%s=(.*?)(?:[,;]|$)", property));
800+
Matcher propertyMatcher = propertyPattern.matcher(overridePropertiesString);
801+
if (propertyMatcher.find() && propertyMatcher.groupCount() >= 1) {
802+
overrideProps.put(property, propertyMatcher.group(1));
803+
}
804+
}
805+
return overrideProps;
806+
}
807+
775808
public static Level parseLogLevel(String logLevelString) {
776809
int logLevel = logLevelString != null ? Integer.parseInt(logLevelString) : DEFAULT_LOG_LEVEL;
777810
switch (logLevel) {

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,9 @@ public void setURL(String URL) {
614614
}
615615

616616
public String getProjectId() {
617-
return projectId;
617+
return projectId != null
618+
? projectId
619+
: com.google.cloud.bigquery.BigQueryOptions.getDefaultProjectId();
618620
}
619621

620622
public void setProjectId(String projectId) {
@@ -809,8 +811,8 @@ public void setProxyPwd(String proxyPwd) {
809811
this.proxyPwd = proxyPwd;
810812
}
811813

812-
public int getOAuthType() {
813-
return oAuthType;
814+
public Integer getOAuthType() {
815+
return oAuthType != null ? oAuthType : BigQueryJdbcUrlUtility.DEFAULT_OAUTH_TYPE_VALUE;
814816
}
815817

816818
public void setOAuthType(Integer oAuthType) {
@@ -936,7 +938,9 @@ public void setJobCreationMode(Integer jobCreationMode) {
936938
}
937939

938940
public Boolean getEnableWriteAPI() {
939-
return enableWriteAPI;
941+
return enableWriteAPI != null
942+
? enableWriteAPI
943+
: BigQueryJdbcUrlUtility.DEFAULT_ENABLE_WRITE_API_VALUE;
940944
}
941945

942946
public void setEnableWriteAPI(Boolean enableWriteAPI) {
@@ -952,23 +956,29 @@ public void setAdditionalProjects(String additionalProjects) {
952956
}
953957

954958
public Boolean getFilterTablesOnDefaultDataset() {
955-
return filterTablesOnDefaultDataset;
959+
return filterTablesOnDefaultDataset != null
960+
? filterTablesOnDefaultDataset
961+
: BigQueryJdbcUrlUtility.DEFAULT_FILTER_TABLES_ON_DEFAULT_DATASET_VALUE;
956962
}
957963

958964
public void setFilterTablesOnDefaultDataset(Boolean filterTablesOnDefaultDataset) {
959965
this.filterTablesOnDefaultDataset = filterTablesOnDefaultDataset;
960966
}
961967

962968
public Integer getRequestGoogleDriveScope() {
963-
return requestGoogleDriveScope;
969+
return requestGoogleDriveScope != null
970+
? requestGoogleDriveScope
971+
: BigQueryJdbcUrlUtility.DEFAULT_REQUEST_GOOGLE_DRIVE_SCOPE_VALUE;
964972
}
965973

966974
public void setRequestGoogleDriveScope(Integer requestGoogleDriveScope) {
967975
this.requestGoogleDriveScope = requestGoogleDriveScope;
968976
}
969977

970978
public Integer getMetadataFetchThreadCount() {
971-
return metadataFetchThreadCount;
979+
return metadataFetchThreadCount != null
980+
? metadataFetchThreadCount
981+
: BigQueryJdbcUrlUtility.DEFAULT_METADATA_FETCH_THREAD_COUNT_VALUE;
972982
}
973983

974984
public void setMetadataFetchThreadCount(Integer metadataFetchThreadCount) {
@@ -1062,23 +1072,29 @@ public void setHttpReadTimeout(Integer httpReadTimeout) {
10621072
}
10631073

10641074
public Long getMaximumBytesBilled() {
1065-
return maximumBytesBilled;
1075+
return maximumBytesBilled != null
1076+
? maximumBytesBilled
1077+
: BigQueryJdbcUrlUtility.DEFAULT_MAX_BYTES_BILLED_VALUE;
10661078
}
10671079

10681080
public void setMaximumBytesBilled(Long maximumBytesBilled) {
10691081
this.maximumBytesBilled = maximumBytesBilled;
10701082
}
10711083

10721084
public Integer getSwaActivationRowCount() {
1073-
return swaActivationRowCount;
1085+
return swaActivationRowCount != null
1086+
? swaActivationRowCount
1087+
: BigQueryJdbcUrlUtility.DEFAULT_SWA_ACTIVATION_ROW_COUNT_VALUE;
10741088
}
10751089

10761090
public void setSwaActivationRowCount(Integer swaActivationRowCount) {
10771091
this.swaActivationRowCount = swaActivationRowCount;
10781092
}
10791093

10801094
public Integer getSwaAppendRowCount() {
1081-
return swaAppendRowCount;
1095+
return swaAppendRowCount != null
1096+
? swaAppendRowCount
1097+
: BigQueryJdbcUrlUtility.DEFAULT_SWA_APPEND_ROW_COUNT_VALUE;
10821098
}
10831099

10841100
public void setSwaAppendRowCount(Integer swaAppendRowCount) {

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionDataSource.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public PooledConnection getPooledConnection() throws SQLException {
4141
"Cannot get pooled connection: unable to get underlying physical connection");
4242
}
4343
Long connectionPoolSize = this.getConnectionPoolSize();
44+
if (connectionPoolSize != null
45+
&& connectionPoolSize == BigQueryJdbcUrlUtility.DEFAULT_CONNECTION_POOL_SIZE_VALUE) {
46+
String connectionURl = ((BigQueryConnection) bqConnection).getConnectionUrl();
47+
String parsedValue =
48+
BigQueryJdbcUrlUtility.parseUriProperty(
49+
connectionURl, BigQueryJdbcUrlUtility.CONNECTION_POOL_SIZE_PROPERTY_NAME);
50+
if (parsedValue != null) {
51+
connectionPoolSize = Long.parseLong(parsedValue);
52+
}
53+
}
4454
if (connectionPoolManager == null) {
4555
connectionPoolManager = new PooledConnectionListener(connectionPoolSize);
4656
}

google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public void testInvalidTokenUriForAuthType0() {
9090
+ "EndpointOverrides=OAuth2=brokenuri{};";
9191
Map<String, String> oauthProperties =
9292
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
93-
Map<String, String> overrideProperties = new HashMap<>();
94-
overrideProperties.put(BigQueryJdbcUrlUtility.OAUTH2_TOKEN_URI_PROPERTY_NAME, "brokenuri{}");
93+
Map<String, String> overrideProperties =
94+
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
9595

9696
try {
9797
BigQueryJdbcOAuthUtility.getCredentials(oauthProperties, overrideProperties, null);
@@ -234,9 +234,8 @@ public void testGenerateUserAuthURLOverrideOauthEndpoint() {
234234
+ ";";
235235
Map<String, String> authProperties =
236236
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
237-
Map<String, String> overrideProperties = new HashMap<>();
238-
overrideProperties.put(
239-
BigQueryJdbcUrlUtility.OAUTH2_TOKEN_URI_PROPERTY_NAME, overrideTokenSeverURI.toString());
237+
Map<String, String> overrideProperties =
238+
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
240239

241240
UserAuthorizer userAuthorizer =
242241
BigQueryJdbcOAuthUtility.getUserAuthorizer(
@@ -275,10 +274,8 @@ public void testParseOverridePropsForRefreshTokenAuth() {
275274

276275
Map<String, String> authProperties =
277276
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
278-
Map<String, String> overrideProperties = new HashMap<>();
279-
overrideProperties.put(
280-
BigQueryJdbcUrlUtility.OAUTH2_TOKEN_URI_PROPERTY_NAME,
281-
"https://oauth2-private.p.googleapis.com/token");
277+
Map<String, String> overrideProperties =
278+
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
282279

283280
UserCredentials userCredentials =
284281
BigQueryJdbcOAuthUtility.getPreGeneratedRefreshTokenCredentials(

0 commit comments

Comments
 (0)