Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,10 @@ private static Map<String, String> parseUrlInternal(String url) {
continue;
}
}
map.put(PROPERTY_NAME_MAP.get(key), CharEscapers.decodeUriPath(kv[1].replace("+", "%2B")));
String propertyName = PROPERTY_NAME_MAP.get(key);
Comment thread
Neenu1995 marked this conversation as resolved.
String value = CharEscapers.decodeUriPath(kv[1].replace("+", "%2B"));
validateNonNegativeIntegerProperty(propertyName, value);
map.put(propertyName, value);
Comment thread
Neenu1995 marked this conversation as resolved.
}
return Collections.unmodifiableMap(map);
}
Expand Down Expand Up @@ -833,4 +836,37 @@ static Map<String, String> parsePropertiesMapFromValue(
}
return propertiesMap;
}

static void validateNonNegative(long val, String propertyName) {
if (val < 0) {
throw new BigQueryJdbcRuntimeException(
String.format(
"Invalid value for %s. It must be greater than or equal to 0.", propertyName));
}
}

private static void validateNonNegativeIntegerProperty(String propertyName, String value) {
if (isNonNegativeIntegerProperty(propertyName)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: isNonNegativeIntegerProperty

try {
long val = Long.parseLong(value);
validateNonNegative(val, propertyName);
} catch (NumberFormatException ex) {
throw new BigQueryJdbcRuntimeException(
String.format("Invalid value for %s. It must be a valid integer.", propertyName), ex);
}
}
}

private static boolean isNonNegativeIntegerProperty(String propertyName) {
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
return MAX_RESULTS_PROPERTY_NAME.equals(propertyName)
|| CONNECTION_POOL_SIZE_PROPERTY_NAME.equals(propertyName)
|| LISTENER_POOL_SIZE_PROPERTY_NAME.equals(propertyName)
|| HTAPI_MIN_TABLE_SIZE_PROPERTY_NAME.equals(propertyName)
|| HTAPI_ACTIVATION_RATIO_PROPERTY_NAME.equals(propertyName)
|| METADATA_FETCH_THREAD_COUNT_PROPERTY_NAME.equals(propertyName)
|| HTTP_CONNECT_TIMEOUT_PROPERTY_NAME.equals(propertyName)
|| HTTP_READ_TIMEOUT_PROPERTY_NAME.equals(propertyName)
|| SWA_ACTIVATION_ROW_COUNT_PROPERTY_NAME.equals(propertyName)
|| SWA_APPEND_ROW_COUNT_PROPERTY_NAME.equals(propertyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ public void setProjectId(String projectId) {
}

Comment thread
Neenu1995 marked this conversation as resolved.
public void setMaxResults(Long maxResults) {
if (maxResults != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
maxResults, BigQueryJdbcUrlUtility.MAX_RESULTS_PROPERTY_NAME);
}
this.maxResults = maxResults;
}

Expand Down Expand Up @@ -736,6 +740,10 @@ public Long getConnectionPoolSize() {
}

public void setConnectionPoolSize(Long connectionPoolSize) {
if (connectionPoolSize != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
connectionPoolSize, BigQueryJdbcUrlUtility.CONNECTION_POOL_SIZE_PROPERTY_NAME);
}
this.connectionPoolSize = connectionPoolSize;
}

Expand All @@ -746,14 +754,27 @@ public Long getListenerPoolSize() {
}

public void setListenerPoolSize(Long listenerPoolSize) {
if (listenerPoolSize != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
listenerPoolSize, BigQueryJdbcUrlUtility.LISTENER_POOL_SIZE_PROPERTY_NAME);
}
this.listenerPoolSize = listenerPoolSize;
}

public void setHighThroughputMinTableSize(Integer highThroughputMinTableSize) {
if (highThroughputMinTableSize != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
highThroughputMinTableSize, BigQueryJdbcUrlUtility.HTAPI_MIN_TABLE_SIZE_PROPERTY_NAME);
}
this.highThroughputMinTableSize = highThroughputMinTableSize;
}

public void setHighThroughputActivationRatio(Integer highThroughputActivationRatio) {
if (highThroughputActivationRatio != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
highThroughputActivationRatio,
BigQueryJdbcUrlUtility.HTAPI_ACTIVATION_RATIO_PROPERTY_NAME);
}
this.highThroughputActivationRatio = highThroughputActivationRatio;
}

Expand Down Expand Up @@ -1048,6 +1069,11 @@ public Integer getMetadataFetchThreadCount() {
}

public void setMetadataFetchThreadCount(Integer metadataFetchThreadCount) {
if (metadataFetchThreadCount != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
metadataFetchThreadCount,
BigQueryJdbcUrlUtility.METADATA_FETCH_THREAD_COUNT_PROPERTY_NAME);
}
this.metadataFetchThreadCount = metadataFetchThreadCount;
}

Expand Down Expand Up @@ -1126,6 +1152,10 @@ public Integer getHttpConnectTimeout() {
}

public void setHttpConnectTimeout(Integer httpConnectTimeout) {
if (httpConnectTimeout != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
httpConnectTimeout, BigQueryJdbcUrlUtility.HTTP_CONNECT_TIMEOUT_PROPERTY_NAME);
}
this.httpConnectTimeout = httpConnectTimeout;
}

Expand All @@ -1134,6 +1164,10 @@ public Integer getHttpReadTimeout() {
}

public void setHttpReadTimeout(Integer httpReadTimeout) {
if (httpReadTimeout != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
httpReadTimeout, BigQueryJdbcUrlUtility.HTTP_READ_TIMEOUT_PROPERTY_NAME);
}
this.httpReadTimeout = httpReadTimeout;
}

Expand All @@ -1154,6 +1188,10 @@ public Integer getSwaActivationRowCount() {
}

public void setSwaActivationRowCount(Integer swaActivationRowCount) {
if (swaActivationRowCount != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
swaActivationRowCount, BigQueryJdbcUrlUtility.SWA_ACTIVATION_ROW_COUNT_PROPERTY_NAME);
}
this.swaActivationRowCount = swaActivationRowCount;
}

Expand All @@ -1164,6 +1202,10 @@ public Integer getSwaAppendRowCount() {
}

public void setSwaAppendRowCount(Integer swaAppendRowCount) {
if (swaAppendRowCount != null) {
BigQueryJdbcUrlUtility.validateNonNegative(
swaAppendRowCount, BigQueryJdbcUrlUtility.SWA_APPEND_ROW_COUNT_PROPERTY_NAME);
}
this.swaAppendRowCount = swaAppendRowCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -206,4 +208,42 @@ public void testAppendPropertiesToURL_propertyWithSemicolon_isEscaped() throws E
assertThat(parsedProperties.get("ProjectId")).isEqualTo(complexValue);
assertFalse(parsedProperties.containsKey("ExtraProperty"));
}

@Test
public void testInvalidConnectionProperties() {
String url = "jdbc:bigquery://;MaxResults=-1";
assertThrows(BigQueryJdbcRuntimeException.class, () -> DataSource.fromUrl(url));

String url2 = "jdbc:bigquery://;ConnectionPoolSize=-2";
assertThrows(BigQueryJdbcRuntimeException.class, () -> DataSource.fromUrl(url2));
}
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated

@Test
public void testValidZeroConnectionProperties() {
String url = "jdbc:bigquery://;MaxResults=0";
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
assertDoesNotThrow(() -> DataSource.fromUrl(url));
}

@Test
public void testInvalidSetterValues() {
DataSource ds = new DataSource();
assertThrows(BigQueryJdbcRuntimeException.class, () -> ds.setMaxResults(-1L));
}

@Test
public void testNonNumericConnectionProperties() {
String url = "jdbc:bigquery://;MaxResults=abc";
assertThrows(BigQueryJdbcRuntimeException.class, () -> DataSource.fromUrl(url));
}

@Test
public void testUnrecognizedConnectionProperties() {
// Unrecognized key-value pair should be ignored (log warning, no exception)
String url = "jdbc:bigquery://;UnknownProperty=value";
assertDoesNotThrow(() -> DataSource.fromUrl(url));

// Malformed property (not key-value) should throw exception
String url2 = "jdbc:bigquery://;MalformedProperty";
assertThrows(BigQueryJdbcRuntimeException.class, () -> DataSource.fromUrl(url2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testPooledConnectionDataSourceFailInvalidConnectionURl() {
PooledConnectionDataSource pooledDataSource = new PooledConnectionDataSource();
pooledDataSource.setURL(connectionUrl);

assertThrows(NumberFormatException.class, () -> pooledDataSource.getPooledConnection());
assertThrows(BigQueryJdbcException.class, () -> pooledDataSource.getPooledConnection());
}

@Test
Expand Down
Loading