Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions java-bigquery-jdbc/docs/USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ String url = "jdbc:bigquery://https://bigquery.googleapis.com:443"
| `JobCreationMode` | `2` | Job creation strategy: `1` (`JOB_CREATION_REQUIRED` - forces explicit query job creation for every query) or `2` (`JOB_CREATION_OPTIONAL` - default, allows queries to execute directly without creating explicit jobs when possible). |
| `MaxResults` | `10000` | Maximum number of rows returned per page during standard REST result set iteration. |
| `AllowLargeResults` | `true` | Allows large query results (required when using legacy SQL destination tables). |
| `LargeResultTable` | `null` | Destination table name for query result sets. If omitted when `LargeResultDataset` is set, the driver automatically generates a temporary table name (`_jdbc_tmp_<timestamp>`). |
| `LargeResultDataset` | `null` | Destination dataset name for query result sets. If the specified dataset does not exist, the driver automatically creates it. Under Legacy SQL mode, defaults to creating/using `_jdbc_tmp`. |
| `LargeResultsDatasetExpirationTime` | `3600000` (1 hour) | Expiration time (in milliseconds) for temporary destination tables in user-specified datasets. |
| `LargeResultTable` | `null` | Destination table name for query result sets. When specified, results are written with `WRITE_TRUNCATE`. When omitted while `LargeResultDataset` is set, the driver generates a unique temporary table (`temp_table_<uuid>`) with `WRITE_EMPTY`. |
| `LargeResultDataset` | `null` | Destination dataset name for query result sets. If the specified dataset does not exist, the driver automatically creates it. Under Legacy SQL mode when `LargeResultTable` is set without `LargeResultDataset`, defaults to `_google_jdbc`. |
| `LargeResultsDatasetExpirationTime` | `3600000` (1 hour) | Non-negative expiration time (in milliseconds) for temporary destination tables in auto-created destination datasets (`0` uses the default `3600000` ms). |
| `KMSKeyName` | `null` | Cloud KMS key resource name used for encrypting query results and destination tables. |

### Session & Transaction Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ private Builder(BigQuerySettings querySettings) {
Builder withDefaultValues() {
return setUseReadAPI(false) // Read API is disabled by default;
.setQueryDialect(BigQueryJdbcUrlUtility.DEFAULT_QUERY_DIALECT_VALUE)
.setDestinationDatasetExpirationTime(
BigQueryJdbcUrlUtility.DEFAULT_DESTINATION_DATASET_EXPIRATION_TIME_VALUE)
.setNumBufferedRows(
DEFAULT_NUM_BUFFERED_ROWS) // 10K records will be kept in the buffer (Blocking Queue);
.setMaxResultPerPage(BigQueryJdbcUrlUtility.DEFAULT_MAX_RESULTS_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -258,11 +256,15 @@ private BigQuerySettings generateBigQuerySettings() throws SQLException {
if (this.connection.getJobTimeoutInSeconds() > 0) {
querySettings.setJobTimeoutMs(this.connection.getJobTimeoutInSeconds() * 1000L);
}
if (this.connection.getDestinationTable() != null) {
querySettings.setDestinationTable(this.connection.getDestinationTable());
if (!isInternal()) {
if (this.connection.getDestinationTable() != null) {
querySettings.setDestinationTable(this.connection.getDestinationTable());
}
if (this.connection.getDestinationDataset() != null) {
querySettings.setDestinationDataset(this.connection.getDestinationDataset());
}
}
if (this.connection.getDestinationDataset() != null) {
querySettings.setDestinationDataset(this.connection.getDestinationDataset());
if (this.connection.getDestinationDatasetExpirationTime() > 0) {
querySettings.setDestinationDatasetExpirationTime(
this.connection.getDestinationDatasetExpirationTime());
}
Expand Down Expand Up @@ -365,8 +367,11 @@ private boolean executeImpl(String sql) throws SQLException {
logQueryExecutionStart(sql);
QueryJobConfiguration.Builder jobConfiguration = getJobConfig(sql);
jobConfiguration = applyParametersIfPresent(jobConfiguration);
if (isLargeResultsEnabled() && getQueryType(jobConfiguration.build(), null) == SqlType.SELECT) {
jobConfiguration = setDestinationDatasetAndTableInJobConfig(jobConfiguration);
if (isLargeResultsEnabled()) {
validateLargeResultsDestinationConfig();
if (getQueryType(jobConfiguration.build(), null) == SqlType.SELECT) {
jobConfiguration = setDestinationDatasetAndTableInJobConfig(jobConfiguration);
}
}
runQuery(sql, jobConfiguration.build());
return getCurrentResultSet() != null;
Expand Down Expand Up @@ -625,10 +630,14 @@ ExecuteResult executeJob(QueryJobConfiguration jobConfiguration)
throws InterruptedException, BigQueryException, BigQueryJdbcException {
LOG.finer("++enter++");
Job job = null;
// Location is not properly passed from the connection,
// so we need to explicitly set it;
// Do not set custom JobId here or it will disable jobless queries.
JobId jobId = JobId.newBuilder().setLocation(connection.getLocation()).build();
// Location is not properly passed from the connection, so we need to explicitly set it.
// Only populate a client JobId when destinationTable != null (which already disables jobless
// fast query), making jobs.insert retries idempotent for WRITE_EMPTY temp tables.
JobId.Builder jobIdBuilder = JobId.newBuilder().setLocation(connection.getLocation());
if (jobConfiguration.getDestinationTable() != null) {
jobIdBuilder.setJob(generateJobId());
}
JobId jobId = jobIdBuilder.build();
Object result = bigQuery.queryWithTimeout(jobConfiguration, jobId, null);
if (result instanceof TableResult) {
TableResult tableResult = (TableResult) result;
Expand Down Expand Up @@ -781,28 +790,44 @@ boolean isLargeResultsEnabled() {
return destinationDataset != null || destinationTable != null;
}

private void validateLargeResultsDestinationConfig() throws BigQueryJdbcException {
if (this.querySettings.getDestinationDataset() == null
&& this.querySettings.getDestinationTable() != null
&& !getUseLegacySql()) {
throw new BigQueryJdbcException(
"LargeResultDataset must be specified when LargeResultTable is set with Standard SQL.");
}
}

QueryJobConfiguration.Builder setDestinationDatasetAndTableInJobConfig(
QueryJobConfiguration.Builder jobConfigurationBuilder) throws BigQueryJdbcException {
String destinationTable = this.querySettings.getDestinationTable();
String configuredDestinationTable = this.querySettings.getDestinationTable();
String destinationDataset = this.querySettings.getDestinationDataset();
if (destinationDataset != null || destinationTable != null) {
if (destinationDataset != null) {
checkIfDatasetExistElseCreate(destinationDataset);
}
if (getUseLegacySql() && destinationDataset == null) {
checkIfDatasetExistElseCreate(DEFAULT_DATASET_NAME);
destinationDataset = DEFAULT_DATASET_NAME;
}
if (destinationTable == null) {
destinationTable = getDefaultDestinationTable();
}
return jobConfigurationBuilder
.setAllowLargeResults(this.querySettings.getAllowLargeResults())
.setDestinationTable(TableId.of(destinationDataset, destinationTable))
.setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED)
.setWriteDisposition(JobInfo.WriteDisposition.WRITE_TRUNCATE);
if (destinationDataset == null && configuredDestinationTable == null) {
return jobConfigurationBuilder;
}
return jobConfigurationBuilder;

validateLargeResultsDestinationConfig();
if (destinationDataset != null) {
checkIfDatasetExistElseCreate(destinationDataset);
} else {
checkIfDatasetExistElseCreate(DEFAULT_DATASET_NAME);
destinationDataset = DEFAULT_DATASET_NAME;
}

boolean isGeneratedTempTable = (configuredDestinationTable == null);
String targetTableName =
isGeneratedTempTable ? getDefaultDestinationTable() : configuredDestinationTable;
JobInfo.WriteDisposition writeDisposition =
isGeneratedTempTable
? JobInfo.WriteDisposition.WRITE_EMPTY
: JobInfo.WriteDisposition.WRITE_TRUNCATE;

return jobConfigurationBuilder
.setAllowLargeResults(this.querySettings.getAllowLargeResults())
.setDestinationTable(TableId.of(destinationDataset, targetTableName))
.setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED)
.setWriteDisposition(writeDisposition);
}

Job getNextJob() {
Expand Down Expand Up @@ -1713,9 +1738,7 @@ private void checkIfDatasetExistElseCreate(String datasetName) throws BigQueryJd
}

private String getDefaultDestinationTable() {
String timeOfCreation = String.valueOf(Instant.now().toEpochMilli());
String randomizedId = String.valueOf(new Random().nextInt(9999));
return DEFAULT_TABLE_NAME + timeOfCreation + randomizedId;
return DEFAULT_TABLE_NAME + UUID.randomUUID().toString().replace("-", "_");
}

@InternalApi
Expand Down Expand Up @@ -1992,8 +2015,10 @@ protected void logQueryExecutionStart(String sql) {
return;
}
String truncatedSql = truncateQuery(sql);
LOG.info("Executing query: " + truncatedSql);
LOG.info("Using query settings: " + this.querySettings.toString());
LOG.info("Executing query: %s", truncatedSql);
LOG.info(
"Using query settings: %s",
BigQueryConnection.sanitizeForLog(this.querySettings.toString()));
}

private String truncateQuery(String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,9 @@ public void setDestinationDataset(String destinationDataset) {
}

public void setDestinationDatasetExpirationTime(long destinationDatasetExpirationTime) {
validateNonNegative(
destinationDatasetExpirationTime,
BigQueryJdbcUrlUtility.DESTINATION_DATASET_EXPIRATION_TIME_PROPERTY_NAME);
this.destinationDatasetExpirationTime = destinationDatasetExpirationTime;
}

Expand Down
Loading
Loading