Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Label BigQuery jobs with Trino query id #16665

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.cloud.bigquery.InsertAllResponse;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobInfo.CreateDisposition;
import com.google.cloud.bigquery.JobStatistics;
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
import com.google.cloud.bigquery.QueryJobConfiguration;
Expand All @@ -35,11 +34,13 @@
import com.google.cloud.http.BaseHttpServiceException;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.airlift.log.Logger;
import io.airlift.units.Duration;
import io.trino.collect.cache.EvictableCacheBuilder;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.connector.TableNotFoundException;

Expand All @@ -65,6 +66,8 @@
import static io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY;
import static io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_INVALID_STATEMENT;
import static io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_LISTING_DATASET_ERROR;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.createDisposition;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.isQueryResultsCacheEnabled;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
Expand All @@ -75,19 +78,22 @@ public class BigQueryClient
{
private static final Logger log = Logger.get(BigQueryClient.class);

private final ConnectorSession session;
private final BigQuery bigQuery;
private final ViewMaterializationCache materializationCache;
private final boolean caseInsensitiveNameMatching;
private final LoadingCache<String, List<Dataset>> remoteDatasetCache;
private final Optional<String> configProjectId;

public BigQueryClient(
ConnectorSession session,
BigQuery bigQuery,
boolean caseInsensitiveNameMatching,
ViewMaterializationCache materializationCache,
Duration metadataCacheTtl,
Optional<String> configProjectId)
{
this.session = requireNonNull(session, "session is null");
this.bigQuery = requireNonNull(bigQuery, "bigQuery is null");
this.materializationCache = requireNonNull(materializationCache, "materializationCache is null");
this.caseInsensitiveNameMatching = caseInsensitiveNameMatching;
Expand Down Expand Up @@ -266,29 +272,37 @@ public void executeUpdate(QueryJobConfiguration job)
{
log.debug("Execute query: %s", job.getQuery());
try {
bigQuery.query(job);
bigQuery.query(setQueryLabel(job));
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BigQueryException(BaseHttpServiceException.UNKNOWN_CODE, format("Failed to run the query [%s]", job.getQuery()), e);
}
}

public TableResult query(String sql, boolean useQueryResultsCache, CreateDisposition createDisposition)
public TableResult query(String sql)
{
log.debug("Execute query: %s", sql);
QueryJobConfiguration job = QueryJobConfiguration.newBuilder(sql)
.setUseQueryCache(isQueryResultsCacheEnabled(session))
.setCreateDisposition(createDisposition(session))
.build();
try {
return bigQuery.query(QueryJobConfiguration.newBuilder(sql)
.setUseQueryCache(useQueryResultsCache)
.setCreateDisposition(createDisposition)
.build());
return bigQuery.query(setQueryLabel(job));
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BigQueryException(BaseHttpServiceException.UNKNOWN_CODE, format("Failed to run the query [%s]", sql), e);
}
}

private QueryJobConfiguration setQueryLabel(QueryJobConfiguration job)
{
return job.toBuilder()
.setLabels(ImmutableMap.of("trino_query_id", session.getQueryId()))
.build();
}

public Schema getSchema(String sql)
{
log.debug("Get schema from query: %s", sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public BigQueryClient create(ConnectorSession session)

protected BigQueryClient createBigQueryClient(ConnectorSession session)
{
return new BigQueryClient(createBigQuery(session), caseInsensitiveNameMatching, materializationCache, metadataCacheTtl, projectId);
return new BigQueryClient(session, createBigQuery(session), caseInsensitiveNameMatching, materializationCache, metadataCacheTtl, projectId);
}

protected BigQuery createBigQuery(ConnectorSession session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ public void setTableComment(ConnectorSession session, ConnectorTableHandle table
quote(remoteTableName.getDatasetName()),
quote(remoteTableName.getTableName()));
client.executeUpdate(QueryJobConfiguration.newBuilder(sql)
.setQuery(sql)
.addPositionalParameter(QueryParameterValue.string(newComment.orElse(null)))
.build());
}
Expand All @@ -663,7 +662,6 @@ public void setColumnComment(ConnectorSession session, ConnectorTableHandle tabl
quote(remoteTableName.getTableName()),
quote(column.getName()));
client.executeUpdate(QueryJobConfiguration.newBuilder(sql)
.setQuery(sql)
.addPositionalParameter(QueryParameterValue.string(newComment.orElse(null)))
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.createDisposition;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.isQueryResultsCacheEnabled;
import static java.util.Objects.requireNonNull;

public class BigQueryPageSourceProvider
Expand Down Expand Up @@ -116,8 +114,6 @@ private ConnectorPageSource createQueryPageSource(ConnectorSession session, BigQ
table,
columnHandles.stream().map(BigQueryColumnHandle::getName).collect(toImmutableList()),
columnHandles.stream().map(BigQueryColumnHandle::getTrinoType).collect(toImmutableList()),
filter,
isQueryResultsCacheEnabled(session),
createDisposition(session));
filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.JobInfo.CreateDisposition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableResult;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -84,9 +83,7 @@ public BigQueryQueryPageSource(
BigQueryTableHandle table,
List<String> columnNames,
List<Type> columnTypes,
Optional<String> filter,
boolean useQueryResultsCache,
CreateDisposition createDisposition)
Optional<String> filter)
{
requireNonNull(client, "client is null");
requireNonNull(table, "table is null");
Expand All @@ -97,7 +94,7 @@ public BigQueryQueryPageSource(
this.columnTypes = ImmutableList.copyOf(columnTypes);
this.pageBuilder = new PageBuilder(columnTypes);
String sql = buildSql(table, client.getProjectId(), ImmutableList.copyOf(columnNames), filter);
this.tableResult = client.query(sql, useQueryResultsCache, createDisposition);
this.tableResult = client.query(sql);
}

private static String buildSql(BigQueryTableHandle table, String projectId, List<String> columnNames, Optional<String> filter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.createDisposition;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.isQueryResultsCacheEnabled;
import static io.trino.plugin.bigquery.BigQuerySessionProperties.isSkipViewMaterialization;
import static io.trino.plugin.bigquery.BigQueryUtil.isWildcardTable;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
Expand Down Expand Up @@ -160,7 +158,7 @@ private List<BigQuerySplit> createEmptyProjection(ConnectorSession session, Tabl
if (filter.isPresent()) {
// count the rows based on the filter
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql, isQueryResultsCacheEnabled(session), createDisposition(session));
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
}
else {
Expand All @@ -170,7 +168,7 @@ private List<BigQuerySplit> createEmptyProjection(ConnectorSession session, Tabl
// (and there's no mechanism to trigger an on-demand flush). This can lead to incorrect results for queries with empty projections.
if (tableInfo.getDefinition().getType() == TABLE || tableInfo.getDefinition().getType() == VIEW) {
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql, isQueryResultsCacheEnabled(session), createDisposition(session));
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
}
else {
Expand Down