From 7168fad9d3c392c5b7cc7e6e06c7f963ccf08036 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Tue, 26 May 2026 17:26:22 +0000 Subject: [PATCH] fix(bigquery-jdbc): add proper version to BigQueryDriver --- .../jdbc/BigQueryDatabaseMetaData.java | 64 ++-------- .../cloud/bigquery/jdbc/BigQueryDriver.java | 8 +- .../utils/BigQueryJdbcVersionUtility.java | 116 ++++++++++++++++++ .../bigquery/jdbc/BigQueryDriverTest.java | 5 +- 4 files changed, 129 insertions(+), 64 deletions(-) create mode 100644 java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/utils/BigQueryJdbcVersionUtility.java diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java index 1c6834feb27d..f11d1e3787ef 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java @@ -42,6 +42,7 @@ import com.google.cloud.bigquery.TableDefinition; import com.google.cloud.bigquery.TableId; import com.google.cloud.bigquery.exception.BigQueryJdbcException; +import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility; import com.google.common.collect.ImmutableMap; import java.io.BufferedReader; import java.io.IOException; @@ -61,7 +62,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Properties; + import java.util.Scanner; import java.util.Set; import java.util.concurrent.BlockingQueue; @@ -73,7 +74,7 @@ import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; + import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Pattern; @@ -92,7 +93,7 @@ class BigQueryDatabaseMetaData implements DatabaseMetaData { private static final String DATABASE_PRODUCT_NAME = "Google BigQuery"; private static final String DATABASE_PRODUCT_VERSION = "2.0"; private static final String DRIVER_NAME = "GoogleJDBCDriverForGoogleBigQuery"; - private static final String DRIVER_DEFAULT_VERSION = "0.0.0"; + private static final String SCHEMA_TERM = "Dataset"; private static final String CATALOG_TERM = "Project"; private static final String PROCEDURE_TERM = "Procedure"; @@ -143,18 +144,11 @@ class BigQueryDatabaseMetaData implements DatabaseMetaData { BigQueryConnection connection; private final BigQuery bigquery; private final int metadataFetchThreadCount; - private static final AtomicReference parsedDriverVersion = new AtomicReference<>(null); - private static final AtomicReference parsedDriverMajorVersion = - new AtomicReference<>(null); - private static final AtomicReference parsedDriverMinorVersion = - new AtomicReference<>(null); - BigQueryDatabaseMetaData(BigQueryConnection connection) { this.URL = connection.getConnectionUrl(); this.connection = connection; this.bigquery = connection.getBigQuery(); this.metadataFetchThreadCount = connection.getMetadataFetchThreadCount(); - loadDriverVersionProperties(); } @Override @@ -223,17 +217,17 @@ public String getDriverName() { @Override public String getDriverVersion() { - return parsedDriverVersion.get() != null ? parsedDriverVersion.get() : DRIVER_DEFAULT_VERSION; + return BigQueryJdbcVersionUtility.getDriverVersion(); } @Override public int getDriverMajorVersion() { - return parsedDriverMajorVersion.get() != null ? parsedDriverMajorVersion.get() : 0; + return BigQueryJdbcVersionUtility.getDriverMajorVersion(); } @Override public int getDriverMinorVersion() { - return parsedDriverMinorVersion.get() != null ? parsedDriverMinorVersion.get() : 0; + return BigQueryJdbcVersionUtility.getDriverMinorVersion(); } @Override @@ -5288,48 +5282,4 @@ String replaceSqlParameters(String sql, String... params) throws SQLException { return String.format(sql, (Object[]) params); } - private void loadDriverVersionProperties() { - if (parsedDriverVersion.get() != null) { - return; - } - Properties props = new Properties(); - try (InputStream input = - getClass().getResourceAsStream("/com/google/cloud/bigquery/jdbc/dependencies.properties")) { - if (input == null) { - String errorMessage = - "Could not find dependencies.properties. Driver version information is unavailable."; - IllegalStateException ex = new IllegalStateException(errorMessage); - LOG.severe(errorMessage, ex); - throw ex; - } - props.load(input); - String versionString = props.getProperty("version.jdbc"); - if (versionString == null || versionString.trim().isEmpty()) { - String errorMessage = - "The property version.jdbc not found or empty in dependencies.properties."; - IllegalStateException ex = new IllegalStateException(errorMessage); - LOG.severe(errorMessage, ex); - throw ex; - } - parsedDriverVersion.compareAndSet(null, versionString.trim()); - String[] parts = versionString.split("\\."); - if (parts.length < 2) { - return; - } - parsedDriverMajorVersion.compareAndSet(null, Integer.parseInt(parts[0])); - String minorPart = parts[1]; - String numericMinor = minorPart.replaceAll("[^0-9].*", ""); - if (!numericMinor.isEmpty()) { - parsedDriverMinorVersion.compareAndSet(null, Integer.parseInt(numericMinor)); - } - } catch (IOException | NumberFormatException e) { - String errorMessage = - "Error reading dependencies.properties. Driver version information is" - + " unavailable. Error: " - + e.getMessage(); - IllegalStateException ex = new IllegalStateException(errorMessage, e); - LOG.severe(errorMessage, ex); - throw ex; - } - } } diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java index 5b138873b399..96362d32fc5a 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java @@ -18,6 +18,7 @@ import com.google.cloud.bigquery.exception.BigQueryJdbcException; import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException; +import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility; import io.grpc.LoadBalancerRegistry; import io.grpc.internal.PickFirstLoadBalancerProvider; import java.io.IOException; @@ -55,9 +56,6 @@ public class BigQueryDriver implements Driver { private static final BigQueryJdbcCustomLogger LOG = new BigQueryJdbcCustomLogger(BigQueryDriver.class.getName()); - // TODO: update this when JDBC goes GA - private static final int JDBC_MAJOR_VERSION = 0; - private static final int JDBC_MINOR_VERSION = 1; static BigQueryDriver registeredBigqueryJdbcDriver; static { @@ -232,13 +230,13 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { @Override public int getMajorVersion() { LOG.finest("++enter++"); - return JDBC_MAJOR_VERSION; + return BigQueryJdbcVersionUtility.getDriverMajorVersion(); } @Override public int getMinorVersion() { LOG.finest("++enter++"); - return JDBC_MINOR_VERSION; + return BigQueryJdbcVersionUtility.getDriverMinorVersion(); } @Override diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/utils/BigQueryJdbcVersionUtility.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/utils/BigQueryJdbcVersionUtility.java new file mode 100644 index 000000000000..977ee255ee43 --- /dev/null +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/utils/BigQueryJdbcVersionUtility.java @@ -0,0 +1,116 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery.jdbc.utils; + +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +/** Utility class to load and parse the JDBC driver version from dependencies.properties. */ +public final class BigQueryJdbcVersionUtility { + + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcVersionUtility.class.getName()); + + private static final AtomicReference parsedDriverVersion = new AtomicReference<>(null); + private static final AtomicReference parsedDriverMajorVersion = + new AtomicReference<>(null); + private static final AtomicReference parsedDriverMinorVersion = + new AtomicReference<>(null); + + private BigQueryJdbcVersionUtility() { + // Utility class, static methods only. + } + + /** + * Gets the full driver version string. + * + * @return the driver version string, e.g., "1.0.0-SNAPSHOT" + */ + public static String getDriverVersion() { + ensureLoaded(); + return parsedDriverVersion.get(); + } + + /** + * Gets the driver major version. + * + * @return the major version number + */ + public static int getDriverMajorVersion() { + ensureLoaded(); + return parsedDriverMajorVersion.get() != null ? parsedDriverMajorVersion.get() : 0; + } + + /** + * Gets the driver minor version. + * + * @return the minor version number + */ + public static int getDriverMinorVersion() { + ensureLoaded(); + return parsedDriverMinorVersion.get() != null ? parsedDriverMinorVersion.get() : 0; + } + + private static void ensureLoaded() { + if (parsedDriverVersion.get() != null) { + return; + } + Properties props = new Properties(); + try (InputStream input = + BigQueryJdbcVersionUtility.class.getResourceAsStream( + "/com/google/cloud/bigquery/jdbc/dependencies.properties")) { + if (input == null) { + String errorMessage = + "Could not find dependencies.properties. Driver version information is unavailable."; + IllegalStateException ex = new IllegalStateException(errorMessage); + LOG.severe(errorMessage, ex); + throw ex; + } + props.load(input); + String versionString = props.getProperty("version.jdbc"); + if (versionString == null || versionString.trim().isEmpty()) { + String errorMessage = + "The property version.jdbc not found or empty in dependencies.properties."; + IllegalStateException ex = new IllegalStateException(errorMessage); + LOG.severe(errorMessage, ex); + throw ex; + } + parsedDriverVersion.compareAndSet(null, versionString.trim()); + String[] parts = versionString.split("\\."); + if (parts.length < 2) { + return; + } + parsedDriverMajorVersion.compareAndSet(null, Integer.parseInt(parts[0])); + String minorPart = parts[1]; + String numericMinor = minorPart.replaceAll("[^0-9].*", ""); + if (!numericMinor.isEmpty()) { + parsedDriverMinorVersion.compareAndSet(null, Integer.parseInt(numericMinor)); + } + } catch (IOException | NumberFormatException e) { + String errorMessage = + "Error reading dependencies.properties. Driver version information is" + + " unavailable. Error: " + + e.getMessage(); + IllegalStateException ex = new IllegalStateException(errorMessage, e); + LOG.severe(errorMessage, ex); + throw ex; + } + } +} diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDriverTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDriverTest.java index 85eb254038f3..904c9df2c4f7 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDriverTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDriverTest.java @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.cloud.bigquery.jdbc.utils.BigQueryJdbcVersionUtility; import java.sql.Connection; import java.sql.DriverPropertyInfo; import java.sql.SQLException; @@ -76,12 +77,12 @@ public void testGetPropertyInfoReturnsValidProperties() { @Test public void testGetMajorVersionMatchesDriverMajorVersion() { - assertThat(bigQueryDriver.getMajorVersion()).isEqualTo(0); + assertThat(bigQueryDriver.getMajorVersion()).isEqualTo(BigQueryJdbcVersionUtility.getDriverMajorVersion()); } @Test public void testGetMinorVersionMatchesDriverMinorVersion() { - assertThat(bigQueryDriver.getMinorVersion()).isEqualTo(1); + assertThat(bigQueryDriver.getMinorVersion()).isEqualTo(BigQueryJdbcVersionUtility.getDriverMinorVersion()); } @Test