docs(bigquery-jdbc): add user guide with connection property and custom endpoint reference#13878
docs(bigquery-jdbc): add user guide with connection property and custom endpoint reference#13878Neenu1995 wants to merge 4 commits into
Conversation
…om endpoint reference
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive User Guide (USER_GUIDE.md) for the Google BigQuery JDBC Driver, detailing configuration, architecture, and common code recipes. The review feedback correctly identifies several inconsistencies in the document, specifically regarding incorrect OAuthType values in the authentication examples (confusing OAuthType=0 for Service Accounts with OAuthType=3 for Application Default Credentials) and mismatches in the Table of Contents headings.
…rties # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive User Guide for the Google BigQuery JDBC Driver, detailing setup, configuration properties, architecture, and common code recipes. The review feedback highlights several critical corrections needed in the documentation's code examples: updating the transaction prerequisite exception from IllegalStateException to SQLException to match the JDBC contract, adding null and type safety checks to the complex types example to prevent runtime exceptions, replacing the stored procedure example with a multi-statement script since standard JDBC OUT parameters are unsupported, and wrapping the Spring Boot YAML connection URL in double quotes to avoid parsing issues.
| (Auto-re-executes BEGIN TRANSACTION; if setAutoCommit is still false) | ||
| ``` | ||
|
|
||
| 1. **Pre-requisite Check**: Calling `setAutoCommit(false)`, `commit()`, or `rollback()` requires `;EnableSession=true` in the connection URL. If disabled, an `IllegalStateException` is thrown. |
There was a problem hiding this comment.
The JDBC API methods setAutoCommit, commit, and rollback are specified to throw java.sql.SQLException when an error or unsupported operation occurs. They do not throw IllegalStateException directly to the caller, as doing so would violate the standard JDBC interface contract and break connection pool wrappers like HikariCP. Please update the documentation to reference SQLException instead.
| 1. **Pre-requisite Check**: Calling `setAutoCommit(false)`, `commit()`, or `rollback()` requires `;EnableSession=true` in the connection URL. If disabled, an `IllegalStateException` is thrown. | |
| 1. **Pre-requisite Check**: Calling setAutoCommit(false), commit(), or rollback() requires ;EnableSession=true in the connection URL. If disabled, a SQLException is thrown. |
| // 1. Reading Struct | ||
| Struct addressStruct = (Struct) rs.getObject("shipping_address"); | ||
| Object[] addressAttrs = addressStruct.getAttributes(); | ||
| System.out.printf("Order %d Address: %s, %s%n", orderId, addressAttrs[0], addressAttrs[1]); | ||
|
|
||
| // 2. Reading Array of Structs | ||
| Array itemsArray = rs.getArray("line_items"); | ||
| Struct[] items = (Struct[]) itemsArray.getArray(); | ||
| for (Struct item : items) { | ||
| Object[] itemAttrs = item.getAttributes(); | ||
| System.out.printf(" Item -> SKU: %s, Qty: %s%n", itemAttrs[0], itemAttrs[1]); | ||
| } | ||
|
|
||
| // 3. Reading Primitive Array | ||
| Array tagsArray = rs.getArray("tags"); | ||
| String[] tags = (String[]) tagsArray.getArray(); | ||
| System.out.println(" Tags: " + String.join(", ", tags)); |
There was a problem hiding this comment.
This code snippet has potential NullPointerException and ClassCastException risks:
- Null Safety: If
shipping_address,line_items, ortagsareNULLin BigQuery,rs.getObject()orrs.getArray()will returnnull. Accessing methods on them directly will throw aNullPointerException. - Type Safety:
Array.getArray()typically returns a genericObject[]array in most JDBC drivers. CastingObject[]directly toStruct[]orString[]will throw aClassCastExceptionat runtime. It is safer to cast toObject[]and process elements individually or use safe conversion utilities.
// 1. Reading Struct
Struct addressStruct = (Struct) rs.getObject("shipping_address");
if (addressStruct != null) {
Object[] addressAttrs = addressStruct.getAttributes();
System.out.printf("Order %d Address: %s, %s%n", orderId, addressAttrs[0], addressAttrs[1]);
}
// 2. Reading Array of Structs
Array itemsArray = rs.getArray("line_items");
if (itemsArray != null) {
Object[] items = (Object[]) itemsArray.getArray();
for (Object itemObj : items) {
Struct item = (Struct) itemObj;
if (item != null) {
Object[] itemAttrs = item.getAttributes();
System.out.printf(" Item -> SKU: %s, Qty: %s%n", itemAttrs[0], itemAttrs[1]);
}
}
}
// 3. Reading Primitive Array
Array tagsArray = rs.getArray("tags");
if (tagsArray != null) {
Object[] tags = (Object[]) tagsArray.getArray();
System.out.println(" Tags: " + java.util.Arrays.toString(tags));
}| import java.math.BigDecimal; | ||
| import java.sql.CallableStatement; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.Types; | ||
|
|
||
| public class StoredProcedureCUJ { | ||
| public static void main(String[] args) throws Exception { | ||
| String url = "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-project;OAuthType=0"; | ||
|
|
||
| String procSql = "CALL my_dataset.calculate_tax(?, ?)"; | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(url); | ||
| CallableStatement cstmt = conn.prepareCall(procSql)) { | ||
|
|
||
| cstmt.setBigDecimal(1, new BigDecimal("1000.00")); // Input subtotal | ||
| cstmt.registerOutParameter(2, Types.DECIMAL); // Output calculated tax | ||
|
|
||
| cstmt.execute(); | ||
|
|
||
| BigDecimal taxAmount = cstmt.getBigDecimal(2); | ||
| System.out.println("Calculated Tax Amount: $" + taxAmount); | ||
| } | ||
| } | ||
| } | ||
| ``` |
There was a problem hiding this comment.
BigQuery stored procedures do not support standard JDBC OUT parameters via CallableStatement.registerOutParameter because the underlying BigQuery API does not return output parameters in the traditional JDBC response format. Calling prepareCall or registerOutParameter will likely throw a SQLFeatureNotSupportedException.
To retrieve OUT parameters from a BigQuery stored procedure, the standard and supported approach is to execute a multi-statement SQL script that declares a variable, calls the procedure, and then selects the variable to return it as a standard ResultSet.
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class StoredProcedureCUJ {
public static void main(String[] args) throws Exception {
String url = "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-project;OAuthType=0";
// Declare a variable, call the procedure, and select the output variable
String sql = "DECLARE tax DECIMAL; CALL my_dataset.calculate_tax(1000.00, tax); SELECT tax;";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
boolean hasResults = stmt.execute(sql);
// Loop through results to find the selected output variable
while (hasResults || stmt.getUpdateCount() != -1) {
if (hasResults) {
try (ResultSet rs = stmt.getResultSet()) {
if (rs.next()) {
BigDecimal taxAmount = rs.getBigDecimal(1);
System.out.println("Calculated Tax Amount: $" + taxAmount);
}
}
}
hasResults = stmt.getMoreResults();
}
}
}
}| ```yaml | ||
| spring: | ||
| datasource: | ||
| url: jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-gcp-project;DefaultDataset=my_dataset;OAuthType=0;OAuthServiceAcctEmail=sa@my-project.iam.gserviceaccount.com;OAuthPvtKeyPath=/path/to/key.json |
There was a problem hiding this comment.
In YAML configuration files, connection URLs containing special characters (such as colons : and semicolons ;) should be wrapped in double quotes to prevent potential parsing errors by the YAML parser.
| url: jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-gcp-project;DefaultDataset=my_dataset;OAuthType=0;OAuthServiceAcctEmail=sa@my-project.iam.gserviceaccount.com;OAuthPvtKeyPath=/path/to/key.json | |
| url: "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-gcp-project;DefaultDataset=my_dataset;OAuthType=0;OAuthServiceAcctEmail=sa@my-project.iam.gserviceaccount.com;OAuthPvtKeyPath=/path/to/key.json" |
No description provided.