Skip to content

Commit 2332ff1

Browse files
authored
fix(bq jdbc): allow & ignore unknown parameters (#12352)
1 parent 0a865bf commit 2332ff1

File tree

4 files changed

+99
-36
lines changed

4 files changed

+99
-36
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,16 @@ private static Map<String, String> parseUrlInternal(String url) {
706706
if (kv.length != 2 || !PROPERTY_NAME_MAP.containsKey(key)) {
707707
String ref = (kv.length == 2) ? key : part;
708708
String safeRef = ref.length() > 32 ? ref.substring(0, 32) + "..." : ref;
709-
throw new BigQueryJdbcRuntimeException(
710-
String.format("Wrong value or unknown setting: %s", safeRef));
709+
// Some tools can pass unknown keys. In order not to break compatibility, throw
710+
// an exception only with incorrect format, otherwise log an error.
711+
if (kv.length != 2) {
712+
throw new BigQueryJdbcRuntimeException(
713+
String.format("Wrong value or unknown setting: %s", safeRef));
714+
} else {
715+
LOG.warning("Wrong value or unknown setting: %s", safeRef);
716+
continue;
717+
}
711718
}
712-
713719
map.put(PROPERTY_NAME_MAP.get(key), CharEscapers.decodeUriPath(kv[1].replace("+", "%2B")));
714720
}
715721
return Collections.unmodifiableMap(map);

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
package com.google.cloud.bigquery.jdbc;
1717

1818
import static com.google.common.truth.Truth.assertThat;
19-
import static org.junit.jupiter.api.Assertions.fail;
2019

21-
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
22-
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
2320
import java.sql.Connection;
2421
import java.sql.DriverPropertyInfo;
2522
import java.sql.SQLException;
@@ -98,15 +95,13 @@ public void testJDBCCompliantReturnsFalse() {
9895
}
9996

10097
@Test
101-
public void testConnectWithInvalidUrlChainsException() {
102-
try {
103-
bigQueryDriver.connect(
104-
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;InvalidProperty=Value",
105-
new Properties());
106-
fail("Expected SQLException");
107-
} catch (SQLException e) {
108-
assertThat((Throwable) e).isInstanceOf(BigQueryJdbcException.class);
109-
assertThat(e.getCause()).isInstanceOf(BigQueryJdbcRuntimeException.class);
110-
}
98+
public void testConnectWithInvalidUrlChainsNoException() throws SQLException {
99+
Connection connection =
100+
bigQueryDriver.connect(
101+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
102+
+ "OAuthType=2;OAuthAccessToken=redactedToken;ProjectId=t;"
103+
+ "InvalidProperty=Value",
104+
new Properties());
105+
assertThat(connection.isClosed()).isFalse();
111106
}
112107
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery.jdbc;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.logging.Handler;
22+
import java.util.logging.LogRecord;
23+
import java.util.logging.Logger;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.BeforeEach;
26+
27+
public abstract class BigQueryJdbcLoggingBaseTest extends BigQueryJdbcBaseTest {
28+
29+
protected List<LogRecord> capturedLogs = new ArrayList<>();
30+
private Handler handler;
31+
private Logger logger;
32+
private long threadId;
33+
34+
@BeforeEach
35+
public void setUpLogValidator() {
36+
logger = BigQueryJdbcRootLogger.getRootLogger();
37+
capturedLogs.clear();
38+
threadId = Thread.currentThread().getId();
39+
handler =
40+
new Handler() {
41+
@Override
42+
public void publish(LogRecord record) {
43+
if (record.getThreadID() == threadId) {
44+
capturedLogs.add(record);
45+
}
46+
}
47+
48+
@Override
49+
public void flush() {}
50+
51+
@Override
52+
public void close() throws SecurityException {}
53+
};
54+
logger.addHandler(handler);
55+
}
56+
57+
@AfterEach
58+
public void tearDownLogValidator() {
59+
if (logger != null && handler != null) {
60+
logger.removeHandler(handler);
61+
}
62+
}
63+
64+
protected boolean assertLogContains(String snippet) {
65+
return capturedLogs.stream().anyMatch(r -> r.getMessage().contains(snippet));
66+
}
67+
}

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
package com.google.cloud.bigquery.jdbc;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertNull;
22-
import static org.junit.jupiter.api.Assertions.assertThrows;
2323

24-
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
2524
import java.util.Collections;
2625
import java.util.Map;
2726
import java.util.Properties;
2827
import org.junit.jupiter.api.Test;
2928

30-
public class BigQueryJdbcUrlUtilityTest {
29+
public class BigQueryJdbcUrlUtilityTest extends BigQueryJdbcLoggingBaseTest {
3130

3231
@Test
3332
public void testParsePropertyWithNoDefault() {
@@ -41,27 +40,25 @@ public void testParsePropertyWithNoDefault() {
4140
}
4241

4342
@Test
44-
public void testParseUrlWithUnknownProperty_throwsException() {
43+
public void testParseUrlWithUnknownProperty_no_exception() {
4544
String url =
4645
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
4746
+ "ProjectId=MyBigQueryProject;"
4847
+ "UnknownProperty=SomeValue";
4948

50-
assertThrows(
51-
BigQueryJdbcRuntimeException.class,
52-
() -> BigQueryJdbcUrlUtility.parseUriProperty(url, "ProjectId"));
49+
BigQueryJdbcUrlUtility.parseUriProperty(url, "ProjectId");
50+
assertThat(assertLogContains("Wrong value or unknown setting")).isTrue();
5351
}
5452

5553
@Test
56-
public void testParseUrlWithTypo_throwsException() {
54+
public void testParseUrlWithTypo_no_exception() {
5755
String url =
5856
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
5957
+ "ProjectId=MyBigQueryProject;"
6058
+ "ProjeectId=TypoValue";
6159

62-
assertThrows(
63-
BigQueryJdbcRuntimeException.class,
64-
() -> BigQueryJdbcUrlUtility.parseUriProperty(url, "ProjectId"));
60+
assertDoesNotThrow(() -> BigQueryJdbcUrlUtility.parseUriProperty(url, "ProjectId"));
61+
assertThat(assertLogContains("Wrong value or unknown setting")).isTrue();
6562
}
6663

6764
@Test
@@ -72,7 +69,7 @@ public void testParsePropertyWithDefault() {
7269
+ "OAuthAccessToken=RedactedToken";
7370

7471
String result = BigQueryJdbcUrlUtility.parseUriProperty(url, "OAuthType");
75-
assertThat(result).isEqualTo(null);
72+
assertThat(result).isNull();
7673
}
7774

7875
@Test
@@ -143,14 +140,12 @@ public void testParseUrl_longUnknownProperty_sanitized() {
143140
String longKey = String.join("", Collections.nCopies(50, "a"));
144141
String url = "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" + longKey + "=value";
145142

146-
BigQueryJdbcRuntimeException e =
147-
assertThrows(
148-
BigQueryJdbcRuntimeException.class, () -> BigQueryJdbcUrlUtility.parseUrl(url));
149-
150-
assertThat(e.getMessage()).contains("Wrong value or unknown setting: ");
151-
assertThat(e.getMessage()).contains("...");
152-
assertThat(e.getMessage()).doesNotContain(longKey);
153-
assertThat(e.getMessage().length()).isLessThan(100);
143+
assertDoesNotThrow(() -> BigQueryJdbcUrlUtility.parseUrl(url));
144+
String message = capturedLogs.get(0).getMessage();
145+
assertThat(message).contains("Wrong value or unknown setting: ");
146+
assertThat(message).contains("...");
147+
assertThat(message).doesNotContain(longKey);
148+
assertThat(message.length()).isLessThan(100);
154149
}
155150

156151
@Test

0 commit comments

Comments
 (0)